Geant4  10.01
UUtils.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * This Software is part of the AIDA Unified Solids Library package *
4 // * See: https://aidasoft.web.cern.ch/USolids *
5 // ********************************************************************
6 //
7 // $Id:$
8 //
9 // --------------------------------------------------------------------
10 //
11 // UUtils
12 //
13 // Description:
14 //
15 // Utility namespace providing common constants and mathematical utilities.
16 //
17 // 19.10.12 Marek Gayer
18 // --------------------------------------------------------------------
19 
20 #ifndef USOLIDS_UUtils
21 #define USOLIDS_UUtils
22 
23 #include <iostream>
24 #include <fstream>
25 #include <limits>
26 #include <cmath>
27 #include <cfloat>
28 #include <vector>
29 #include <algorithm>
30 
31 struct UVector3;
32 class UTransform3D;
33 
36 
37 namespace UUtils
38 {
39 
40  // Sign
41  inline short Sign(short a, short b);
42  inline int Sign(int a, int b);
43  inline long Sign(long a, long b);
44  inline float Sign(float a, float b);
45  inline double Sign(double a, double b);
46 
47  // Trigonometric
48  static const double kPi = 3.14159265358979323846;
49  static const double kTwoPi = 2.0 * kPi;
50  static const double kRadToDeg = 180.0 / kPi;
51  static const double kDegToRad = kPi / 180.0;
52  static const double kSqrt2 = 1.4142135623730950488016887242097;
53  static const double kInfinity = DBL_MAX;
54 
55  static const double kMeshAngleDefault = (kPi / 4); // Angle for mesh `wedges' in rads
56  static const int kMinMeshSections = 3; // Min wedges+1 to make
57  static const int kMaxMeshSections = 37; // max wedges+1 to make
58 
59  inline double Infinity();
60 
61  inline double ASin(double);
62  inline double ACos(double);
63  inline double ATan(double);
64  inline double ATan2(double, double);
65 
66  //Warnings and Errors Messages
67  void Exception(const char* originOfException,
68  const char* exceptionCode,
69  ExceptionSeverity severity,
70  int level,
71  const char* description);
72 
73 
74  // Comparing floating points
75  inline bool AreEqualAbs(double af, double bf, double epsilon)
76  {
77  //return true if absolute difference between af and bf is less than epsilon
78  return std::abs(af - bf) < epsilon;
79  }
80  inline bool AreEqualRel(double af, double bf, double relPrec)
81  {
82  //return true if relative difference between af and bf is less than relPrec
83  return std::abs(af - bf) <= 0.5 * relPrec * (std::abs(af) + std::abs(bf));
84  }
85 
86  // Locate Min, Max element number in an array
87  long LocMin(long n, const double* a);
88  long LocMax(long n, const double* a);
89 
90  // TransformLimits: Use the transformation to convert the local limits defined
91  // by min/max vectors to the master frame. Returns modified limits.
92  void TransformLimits(UVector3& min, UVector3& max, const UTransform3D& transformation);
93 
94  double Random(double min = 0.0, double max = 1.0);
95 
96  // Templates:
97  template<typename T>
98  struct CompareDesc
99  {
100 
101  CompareDesc(T d) : fData(d) {}
102 
103  template<typename Index>
104  bool operator()(Index i1, Index i2)
105  {
106  return *(fData + i1) > *(fData + i2);
107  }
108 
109  T fData;
110  };
111 
112  template<typename T>
113  struct CompareAsc
114  {
115 
116  CompareAsc(T d) : fData(d) {}
117 
118  template<typename Index>
119  bool operator()(Index i1, Index i2)
120  {
121  return *(fData + i1) < *(fData + i2);
122  }
123 
124  T fData;
125  };
126 
127  std::string ToString(int number);
128  std::string ToString(double number);
129 
130  int FileSize(const std::string& filePath);
131 
132  int StrPos(const std::string& haystack, const std::string& needle);
133 
134  inline double GetRadiusInRing(double rmin, double rmax);
135 
136  template <class T>
137  inline T sqr(const T& x)
138  {
139  return x * x;
140  }
141 
142  inline bool StrEnds(std::string const& fullString, std::string const& ending)
143  {
144  if (fullString.length() >= ending.length())
145  {
146  return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
147  }
148  else
149  {
150  return false;
151  }
152  }
153 }
154 
155 inline double UUtils::GetRadiusInRing(double rmin, double rmax)
156 {
157  // Generate radius in annular ring according to uniform area
158  //
159  if (rmin <= 0.)
160  {
161  return rmax * std::sqrt(Random());
162  }
163  if (rmin != rmax)
164  {
165  return std::sqrt(Random()
166  * (sqr(rmax) - sqr(rmin)) + sqr(rmin));
167  }
168  return rmin;
169 }
170 
171 //____________________________________________________________________________
172 inline double UUtils::Infinity()
173 {
174  // returns an infinity as defined by the IEEE standard
175  return std::numeric_limits<double>::infinity();
176 }
177 
178 //---- Sign --------------------------------------------------------------------
179 inline short UUtils::Sign(short a, short b)
180 {
181  return (b >= 0) ? std::abs(a) : -std::abs(a);
182 }
183 
184 inline int UUtils::Sign(int a, int b)
185 {
186  return (b >= 0) ? std::abs(a) : -std::abs(a);
187 }
188 
189 inline long UUtils::Sign(long a, long b)
190 {
191  return (b >= 0) ? std::abs(a) : -std::abs(a);
192 }
193 
194 inline float UUtils::Sign(float a, float b)
195 {
196  return (b >= 0) ? std::abs(a) : -std::abs(a);
197 }
198 
199 inline double UUtils::Sign(double a, double b)
200 {
201  return (b >= 0) ? std::abs(a) : -std::abs(a);
202 }
203 
204 
205 //---- Trigonometric------------------------------------------------------------
206 inline double UUtils::ASin(double x)
207 {
208  if (x < -1.) return -kPi / 2;
209  if (x > 1.) return kPi / 2;
210  return std::asin(x);
211 }
212 
213 inline double UUtils::ACos(double x)
214 {
215  if (x < -1.) return kPi;
216  if (x > 1.) return 0;
217  return std::acos(x);
218 }
219 
220 
221 inline double UUtils::ATan2(double y, double x)
222 {
223  if (x != 0) return std::atan2(y, x);
224  if (y == 0) return 0;
225  if (y > 0) return kPi / 2;
226  else return -kPi / 2;
227 }
228 
229 #endif
static const double kDegToRad
Definition: UUtils.hh:51
bool operator()(Index i1, Index i2)
Definition: UUtils.hh:104
bool AreEqualAbs(double af, double bf, double epsilon)
Definition: UUtils.hh:75
double ACos(double)
Definition: UUtils.hh:213
static const double kRadToDeg
Definition: UUtils.hh:50
double ATan(double)
int FileSize(const std::string &filePath)
Definition: UUtils.cc:91
double ASin(double)
Definition: UUtils.hh:206
G4double a
Definition: TRTMaterials.hh:39
static const int kMaxMeshSections
Definition: UUtils.hh:57
Definition: UUtils.hh:35
double Infinity()
Definition: UUtils.hh:172
Definition: UUtils.hh:35
ExceptionSeverity
Definition: UUtils.hh:34
bool AreEqualRel(double af, double bf, double relPrec)
Definition: UUtils.hh:80
static const double kInfinity
Definition: UUtils.hh:53
static const double kTwoPi
Definition: UUtils.hh:49
bool StrEnds(std::string const &fullString, std::string const &ending)
Definition: UUtils.hh:142
double ATan2(double, double)
Definition: UUtils.hh:221
const G4int n
T sqr(const T &x)
Definition: UUtils.hh:137
double GetRadiusInRing(double rmin, double rmax)
Definition: UUtils.hh:155
T max(const T t1, const T t2)
brief Return the largest of the two arguments
bool operator()(Index i1, Index i2)
Definition: UUtils.hh:119
static const double kMeshAngleDefault
Definition: UUtils.hh:55
static const double kPi
Definition: UUtils.hh:48
static const double kSqrt2
Definition: UUtils.hh:52
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
static const int kMinMeshSections
Definition: UUtils.hh:56
void Exception(const char *originOfException, const char *exceptionCode, ExceptionSeverity severity, int level, const char *description)
Definition: UUtils.cc:122
std::string ToString(int number)
Definition: UUtils.cc:77
T sqr(const T &x)
Definition: templates.hh:145
short Sign(short a, short b)
Definition: UUtils.hh:179
long LocMin(long n, const double *a)
int StrPos(const std::string &haystack, const std::string &needle)
Definition: UUtils.cc:105
#define DBL_MAX
Definition: templates.hh:83
double Random(double min=0.0, double max=1.0)
Definition: UUtils.cc:69
Definition: UUtils.hh:37
void TransformLimits(UVector3 &min, UVector3 &max, const UTransform3D &transformation)
Definition: UUtils.cc:29
long LocMax(long n, const double *a)