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