Geant4  10.00.p01
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 #include "UVector3.hh"
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  ExceptionSeverity 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  int SaveVectorToExternalFile(const std::vector<double>& vector, const std::string& filename);
129  int SaveVectorToExternalFile(const std::vector<UVector3>& vector, const std::string& filename);
130  int SaveVectorToExternalFile(const std::vector<int>& vector, const std::string& filename);
131 
132  std::string ToString(int number);
133  std::string ToString(double number);
134 
135  int FileSize(const std::string& filePath);
136 
137  int StrPos(const std::string& haystack, const std::string& needle);
138 
139  inline double GetRadiusInRing(double rmin, double rmax);
140 
141  template <class T>
142  inline T sqr(const T& x)
143  {
144  return x * x;
145  }
146 
147  inline bool StrEnds(std::string const& fullString, std::string const& ending)
148  {
149  if (fullString.length() >= ending.length())
150  {
151  return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
152  }
153  else
154  {
155  return false;
156  }
157  }
158 }
159 
160 inline double UUtils::GetRadiusInRing(double rmin, double rmax)
161 {
162  // Generate radius in annular ring according to uniform area
163  //
164  if (rmin <= 0.)
165  {
166  return rmax * std::sqrt(Random());
167  }
168  if (rmin != rmax)
169  {
170  return std::sqrt(Random()
171  * (sqr(rmax) - sqr(rmin)) + sqr(rmin));
172  }
173  return rmin;
174 }
175 
176 //____________________________________________________________________________
177 inline double UUtils::Infinity()
178 {
179  // returns an infinity as defined by the IEEE standard
180  return std::numeric_limits<double>::infinity();
181 }
182 
183 //---- Sign --------------------------------------------------------------------
184 inline short UUtils::Sign(short a, short b)
185 {
186  return (b >= 0) ? std::abs(a) : -std::abs(a);
187 }
188 
189 inline int UUtils::Sign(int a, int b)
190 {
191  return (b >= 0) ? std::abs(a) : -std::abs(a);
192 }
193 
194 inline long UUtils::Sign(long a, long b)
195 {
196  return (b >= 0) ? std::abs(a) : -std::abs(a);
197 }
198 
199 inline float UUtils::Sign(float a, float b)
200 {
201  return (b >= 0) ? std::abs(a) : -std::abs(a);
202 }
203 
204 inline double UUtils::Sign(double a, double b)
205 {
206  return (b >= 0) ? std::abs(a) : -std::abs(a);
207 }
208 
209 
210 //---- Trigonometric------------------------------------------------------------
211 inline double UUtils::ASin(double x)
212 {
213  if (x < -1.) return -kPi / 2;
214  if (x > 1.) return kPi / 2;
215  return std::asin(x);
216 }
217 
218 inline double UUtils::ACos(double x)
219 {
220  if (x < -1.) return kPi;
221  if (x > 1.) return 0;
222  return std::acos(x);
223 }
224 
225 
226 inline double UUtils::ATan2(double y, double x)
227 {
228  if (x != 0) return std::atan2(y, x);
229  if (y == 0) return 0;
230  if (y > 0) return kPi / 2;
231  else return -kPi / 2;
232 }
233 
234 #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
double ACos(double)
Definition: UUtils.hh:218
static const double kRadToDeg
Definition: UUtils.hh:51
double ATan(double)
int FileSize(const std::string &filePath)
Definition: UUtils.cc:146
double ASin(double)
Definition: UUtils.hh:211
G4double a
Definition: TRTMaterials.hh:39
static const int kMaxMeshSections
Definition: UUtils.hh:58
Definition: UUtils.hh:36
double Infinity()
Definition: UUtils.hh:177
Definition: UUtils.hh:36
ExceptionSeverity
Definition: UUtils.hh:35
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:147
double ATan2(double, double)
Definition: UUtils.hh:226
const G4int n
T sqr(const T &x)
Definition: UUtils.hh:142
double GetRadiusInRing(double rmin, double rmax)
Definition: UUtils.hh:160
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
int SaveVectorToExternalFile(const std::vector< double > &vector, const std::string &filename)
Definition: UUtils.cc:77
static const int kMinMeshSections
Definition: UUtils.hh:57
void Exception(const char *originOfException, const char *exceptionCode, ExceptionSeverity severity, int level, const char *description)
Definition: UUtils.cc:177
std::string ToString(int number)
Definition: UUtils.cc:132
T sqr(const T &x)
Definition: templates.hh:145
short Sign(short a, short b)
Definition: UUtils.hh:184
long LocMin(long n, const double *a)
int StrPos(const std::string &haystack, const std::string &needle)
Definition: UUtils.cc:160
#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)