Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 int UUtils::SaveVectorToExternalFile(const vector<double>& vector, const string& filename)
78 {
79  ofstream file(filename.c_str());
80 
81  // NEW: set precision, use exponential, precision 4 digits
82  if (file.is_open())
83  {
84  int size = vector.size();
85  file.precision(16);
86  for (int i = 0; i < size; i++)
87  {
88  double value = vector[i];
89  file << value << "\n";
90  }
91  return 0;
92  }
93  return 1;
94 }
95 
96 
97 int UUtils::SaveVectorToExternalFile(const vector<int>& vector, const string& filename)
98 {
99  ofstream file(filename.c_str());
100 
101  if (file.is_open())
102  {
103  int size = vector.size();
104  for (int i = 0; i < size; i++)
105  {
106  int value = vector[i];
107  file << value << "\n";
108  }
109  return 0;
110  }
111  return 1;
112 }
113 
114 int UUtils::SaveVectorToExternalFile(const vector<UVector3>& vector, const string& filename)
115 {
116  ofstream file(filename.c_str());
117 
118  if (file.is_open())
119  {
120  int size = vector.size();
121  file.precision(16);
122  for (int i = 0; i < size; i++)
123  {
124  const UVector3& vec = vector[i];
125  file << vec.x << "\t" << vec.y << "\t" << vec.z << "\n";
126  }
127  return 0;
128  }
129  return 1;
130 }
131 
132 string UUtils::ToString(int number)
133 {
134  std::stringstream ss;
135  ss << number;
136  return ss.str();
137 }
138 
139 string UUtils::ToString(double number)
140 {
141  std::stringstream ss;
142  ss << number;
143  return ss.str();
144 }
145 
146 int UUtils::FileSize(const std::string& filePath)
147 {
148  std::streampos fsize = 0;
149  std::ifstream file(filePath.c_str(), std::ios::binary);
150 
151  fsize = file.tellg();
152  file.seekg(0, std::ios::end);
153  fsize = file.tellg() - fsize;
154  file.close();
155 
156  return fsize;
157 }
158 
159 
160 int UUtils::StrPos(const string& haystack, const string& needle)
161 {
162  int sleng = haystack.length();
163  int nleng = needle.length();
164 
165  if (sleng == 0 || nleng == 0)
166  return -1;
167 
168  for (int i = 0, j = 0; i < sleng; j = 0, i++)
169  {
170  while (i + j < sleng && j < nleng && haystack[i + j] == needle[j])
171  j++;
172  if (j == nleng)
173  return i;
174  }
175  return -1;
176 }
177 void UUtils:: Exception(const char* originOfException,
178  const char* exceptionCode,
179  ExceptionSeverity severity,
180  int level,
181  const char* description)
182 
183 {
184  bool toBeAborted = true;
185  static const std::string es_banner
186  = "\n-------- EEEE ------- UException-START -------- EEEE -------\n";
187  static const std::string ee_banner
188  = "\n-------- EEEE ------- UException-END --------- EEEE -------\n";
189  static const std::string ws_banner
190  = "\n-------- WWWW ------- UException-START -------- WWWW -------\n";
191  static const std::string we_banner
192  = "\n-------- WWWW -------- UException-END --------- WWWW -------\n";
193  std::ostringstream message;
194  message << "\n*** ExceptionHandler is not defined ***\n"
195  << "*** Exception : " << exceptionCode << std::endl
196  << " issued by : " << originOfException << std::endl
197  << description << std::endl;
198  switch (severity)
199  {
200  case FatalError:
201  std::cerr << es_banner << message.str() << "*** Fatal Exception ***"
202  << ee_banner << std::endl;
203  break;
205  std::cerr << es_banner << message.str() << "*** Fatal Error In Argument ***"
206  << ee_banner << std::endl;
207  break;
208  case Error:
209  std::cerr << es_banner << message.str() << "*** Error ***" << level
210  << ee_banner << std::endl;
211  break;
212  case Warning:
213  std::cerr << ws_banner << message.str() << "*** This is just a warning message ***"
214  << we_banner << std::endl;
215  toBeAborted = false;
216  break;
217  default:
218  std::cout << ws_banner << message.str()
219  << "*** This is just a message for your information. ***"
220  << we_banner << std::endl;
221  toBeAborted = false;
222  break;
223  }
224 
225  if (toBeAborted)
226  {
227 
228  std::cerr << std::endl << "*** GException: Aborting execution ***" << std::endl;
229  abort();
230  }
231 
232 
233 }
234 
235 
236 /*
237 void UTessellatedSolid::ImportFromSTLFile(std::string filename)
238 {
239 vector <UTriangularFacet *> fFacets;
240 
241 USTL::ReadFromBinaryFile(filename, fFacets);
242 
243 int size = fFacets.size();
244 for (int i = 0; i < size; ++i)
245 {
246 UTriangularFacet *facet = fFacets[i];
247 AddFacet(facet);
248 }
249 SetSolidClosed(true);
250 }
251 */
252 
253 /*
254 int size = fFacets.size();
255 for (int j = 0; j < 100; ++j) //2.418 , 2.511
256 for (int i = 0; i < size; ++i)
257 {
258 UFacet &facet = *facetsi[j];
259 a += facet.GetNumberOfVertices();
260 }
261 if (a % rand() == -1) cout << a;
262 */
263 
264 /*
265 for (int j = 0; j < 100; ++j) //2.917 3.01
266 {
267 int size = fFacets.size();
268 for (int i = 0; i < size; ++i)
269 {
270 UFacet &facet = *fFacets[i];
271 a += facet.GetNumberOfVertices();
272 }
273 }
274 */
275 
276 /*
277 for (int j = 0; j < 100; ++j) // 2.589
278 {
279 std::vector<UFacet *>::const_iterator i, begin = fFacets.begin(), end = fFacets.end();
280 for (i = begin; i < end; ++i)
281 {
282 UFacet &facet = *(*i);
283 a += facet.GetNumberOfVertices();
284 }
285 }
286 
287 return location;
288 */
int FileSize(const std::string &filePath)
Definition: UUtils.cc:146
Definition: UUtils.hh:36
double x
Definition: UVector3.hh:136
ExceptionSeverity
Definition: UUtils.hh:35
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
int SaveVectorToExternalFile(const std::vector< double > &vector, const std::string &filename)
Definition: UUtils.cc:77
const XML_Char int const XML_Char * value
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
double z
Definition: UVector3.hh:138
int StrPos(const std::string &haystack, const std::string &needle)
Definition: UUtils.cc:160
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