Geant4  10.00.p02
UUtils.cc
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 // 19.10.12 Marek Gayer
14 // --------------------------------------------------------------------
15 
16 #include <iostream>
17 #include <iomanip>
18 #include <fstream>
19 #include <sstream>
20 
21 #include "UVector3.hh"
22 #include "UTransform3D.hh"
23 #include "UUtils.hh"
24 #include "VUSolid.hh"
25 
26 using namespace std;
27 
28 //______________________________________________________________________________
30 {
31  // The goal of this method is to convert the quantities min and max (representing the
32  // bounding box of a given solid in its local frame) to the main frame, using
33  // "transformation"
34  UVector3 vertices[8] = // Detemination of the vertices thanks to the extension of each solid:
35  {
36  UVector3(min.x, min.y, min.z), // 1st vertice:
37  UVector3(min.x, max.y, min.z), // 2nd vertice:
38  UVector3(max.x, max.y, min.z),
39  UVector3(max.x, min.y, min.z),
40  UVector3(min.x, min.y, max.z),
41  UVector3(min.x, max.y, max.z),
42  UVector3(max.x, max.y, max.z),
43  UVector3(max.x, min.y, max.z)
44  };
45 
46  min.Set(kInfinity);
47  max.Set(-kInfinity);
48 
49  // Loop on th vertices
50  int limit = sizeof(vertices) / sizeof(UVector3);
51  for (int i = 0 ; i < limit; i++)
52  {
53  // From local frame to the gobal one:
54  // Current positions on the three axis:
55  UVector3 current = transformation.GlobalPoint(vertices[i]);
56 
57  // If need be, replacement of the min & max values:
58  if (current.x > max.x) max.x = current.x;
59  if (current.x < min.x) min.x = current.x;
60 
61  if (current.y > max.y) max.y = current.y;
62  if (current.y < min.y) min.y = current.y;
63 
64  if (current.z > max.z) max.z = current.z;
65  if (current.z < min.z) min.z = current.z;
66  }
67 }
68 
69 double UUtils::Random(double min, double max)
70 {
71  // srand((unsigned)time(NULL));
72  double number = (double) rand() / RAND_MAX;
73  double res = min + number * (max - min);
74  return res;
75 }
76 
77 string UUtils::ToString(int number)
78 {
79  std::stringstream ss;
80  ss << number;
81  return ss.str();
82 }
83 
84 string UUtils::ToString(double number)
85 {
86  std::stringstream ss;
87  ss << number;
88  return ss.str();
89 }
90 
91 int UUtils::FileSize(const std::string& filePath)
92 {
93  std::streampos fsize = 0;
94  std::ifstream file(filePath.c_str(), std::ios::binary);
95 
96  fsize = file.tellg();
97  file.seekg(0, std::ios::end);
98  fsize = file.tellg() - fsize;
99  file.close();
100 
101  return fsize;
102 }
103 
104 
105 int UUtils::StrPos(const string& haystack, const string& needle)
106 {
107  int sleng = haystack.length();
108  int nleng = needle.length();
109 
110  if (sleng == 0 || nleng == 0)
111  return -1;
112 
113  for (int i = 0, j = 0; i < sleng; j = 0, i++)
114  {
115  while (i + j < sleng && j < nleng && haystack[i + j] == needle[j])
116  j++;
117  if (j == nleng)
118  return i;
119  }
120  return -1;
121 }
122 void UUtils:: Exception(const char* originOfException,
123  const char* exceptionCode,
124  ExceptionSeverity severity,
125  int level,
126  const char* description)
127 
128 {
129  bool toBeAborted = true;
130  static const std::string es_banner
131  = "\n-------- EEEE ------- UException-START -------- EEEE -------\n";
132  static const std::string ee_banner
133  = "\n-------- EEEE ------- UException-END --------- EEEE -------\n";
134  static const std::string ws_banner
135  = "\n-------- WWWW ------- UException-START -------- WWWW -------\n";
136  static const std::string we_banner
137  = "\n-------- WWWW -------- UException-END --------- WWWW -------\n";
138  std::ostringstream message;
139  message << "\n*** ExceptionHandler is not defined ***\n"
140  << "*** Exception : " << exceptionCode << std::endl
141  << " issued by : " << originOfException << std::endl
142  << description << std::endl;
143  switch (severity)
144  {
145  case FatalError:
146  std::cerr << es_banner << message.str() << "*** Fatal Exception ***"
147  << ee_banner << std::endl;
148  break;
150  std::cerr << es_banner << message.str() << "*** Fatal Error In Argument ***"
151  << ee_banner << std::endl;
152  break;
153  case Error:
154  std::cerr << es_banner << message.str() << "*** Error ***" << level
155  << ee_banner << std::endl;
156  break;
157  case Warning:
158  std::cerr << ws_banner << message.str() << "*** This is just a warning message ***"
159  << we_banner << std::endl;
160  toBeAborted = false;
161  break;
162  default:
163  std::cout << ws_banner << message.str()
164  << "*** This is just a message for your information. ***"
165  << we_banner << std::endl;
166  toBeAborted = false;
167  break;
168  }
169 
170  if (toBeAborted)
171  {
172 
173  std::cerr << std::endl << "*** GException: Aborting execution ***" << std::endl;
174  abort();
175  }
176 
177 
178 }
179 
180 
181 /*
182 void UTessellatedSolid::ImportFromSTLFile(std::string filename)
183 {
184 vector <UTriangularFacet *> fFacets;
185 
186 USTL::ReadFromBinaryFile(filename, fFacets);
187 
188 int size = fFacets.size();
189 for (int i = 0; i < size; ++i)
190 {
191 UTriangularFacet *facet = fFacets[i];
192 AddFacet(facet);
193 }
194 SetSolidClosed(true);
195 }
196 */
197 
198 /*
199 int size = fFacets.size();
200 for (int j = 0; j < 100; ++j) //2.418 , 2.511
201 for (int i = 0; i < size; ++i)
202 {
203 UFacet &facet = *facetsi[j];
204 a += facet.GetNumberOfVertices();
205 }
206 if (a % rand() == -1) cout << a;
207 */
208 
209 /*
210 for (int j = 0; j < 100; ++j) //2.917 3.01
211 {
212 int size = fFacets.size();
213 for (int i = 0; i < size; ++i)
214 {
215 UFacet &facet = *fFacets[i];
216 a += facet.GetNumberOfVertices();
217 }
218 }
219 */
220 
221 /*
222 for (int j = 0; j < 100; ++j) // 2.589
223 {
224 std::vector<UFacet *>::const_iterator i, begin = fFacets.begin(), end = fFacets.end();
225 for (i = begin; i < end; ++i)
226 {
227 UFacet &facet = *(*i);
228 a += facet.GetNumberOfVertices();
229 }
230 }
231 
232 return location;
233 */
static const G4double kInfinity
Definition: geomdefs.hh:42
int FileSize(const std::string &filePath)
Definition: UUtils.cc:91
Definition: UUtils.hh:35
double x
Definition: UVector3.hh:136
ExceptionSeverity
Definition: UUtils.hh:34
UVector3 GlobalPoint(const UVector3 &local) const
void Set(double xx, double yy, double zz)
Definition: UVector3.hh:245
T max(const T t1, const T t2)
brief Return the largest of the two arguments
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
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
double z
Definition: UVector3.hh:138
int StrPos(const std::string &haystack, const std::string &needle)
Definition: UUtils.cc:105
double Random(double min=0.0, double max=1.0)
Definition: UUtils.cc:69
double y
Definition: UVector3.hh:137
void TransformLimits(UVector3 &min, UVector3 &max, const UTransform3D &transformation)
Definition: UUtils.cc:29