File: TestSExtru.cpp

package info (click to toggle)
vecgeom 1.2.8%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 24,016 kB
  • sloc: cpp: 88,803; ansic: 6,888; python: 1,035; sh: 582; sql: 538; makefile: 23
file content (335 lines) | stat: -rw-r--r-- 12,598 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "VecGeom/volumes/PlanarPolygon.h"
#include "VecGeom/volumes/PolygonalShell.h"
#include "VecGeom/volumes/SExtru.h"
#include "VecGeom/volumes/utilities/VolumeUtilities.h"
#include "VecGeom/base/SOA3D.h"
#include "VecGeom/volumes/UnplacedSExtruVolume.h"

#ifdef VECGEOM_ROOT
#include "TGeoPolygon.h"
#include "TGeoXtru.h"
#include "TGeoBBox.h"
#endif

#include <iostream>
#include <cmath>
#include <chrono>

using namespace vecgeom;
using namespace std::chrono;

__attribute__((noinline)) void TimeVecGeom(PlanarPolygon const &poly, SOA3D<Precision> const &container, int &count)
{
  high_resolution_clock::time_point t1 = high_resolution_clock::now();
  const auto S                         = container.size();
  for (size_t i = 0; i < S; ++i) {
    if (poly.Contains(container[i])) count++;
  }
  high_resolution_clock::time_point t2 = high_resolution_clock::now();
  duration<double> time_span           = duration_cast<duration<double>>(t2 - t1);
  std::cout << "It took me " << time_span.count() << " seconds.\n";
}

__attribute__((noinline)) void TimeVecGeomSafety(PlanarPolygon const &poly, SOA3D<Precision> const &container,
                                                 double &totalsafety)
{
  high_resolution_clock::time_point t1 = high_resolution_clock::now();
  const auto S                         = container.size();
  for (size_t i = 0; i < S; ++i) {
    int segmentid;
    totalsafety += std::sqrt(poly.SafetySqr(container[i], segmentid));
  }
  high_resolution_clock::time_point t2 = high_resolution_clock::now();
  duration<double> time_span           = duration_cast<duration<double>>(t2 - t1);
  std::cout << "It took me " << time_span.count() << " seconds.\n";
}

__attribute__((noinline)) void TimeVecGeomDistanceToIn(UnplacedSExtruVolume const &poly,
                                                       SOA3D<Precision> const &pointcontainer,
                                                       SOA3D<Precision> const &dircontainer, int &hitcount)
{
  high_resolution_clock::time_point t1 = high_resolution_clock::now();
  const auto S                         = pointcontainer.size();
  for (size_t i = 0; i < S; ++i) {
    auto d = poly.DistanceToIn(pointcontainer[i], dircontainer[i]);
    //  std::cerr << "VECGEOM D " << pointcontainer[i] << " " << dircontainer[i] << "\n";
    // std::cerr << "VECGEOM D " << i << " " << d << "\n";
    if (d < 1E30) hitcount++;
  }
  high_resolution_clock::time_point t2 = high_resolution_clock::now();
  duration<double> time_span           = duration_cast<duration<double>>(t2 - t1);
  std::cout << "It took me " << time_span.count() << " seconds.\n";
}

__attribute__((noinline)) void TimeVecGeomDistanceToOut(UnplacedSExtruVolume const &poly,
                                                        SOA3D<Precision> const &pointcontainer,
                                                        SOA3D<Precision> const &dircontainer, double &totaldistout)
{
  high_resolution_clock::time_point t1 = high_resolution_clock::now();
  const auto S                         = pointcontainer.size();
  for (size_t i = 0; i < S; ++i) {
    auto d = poly.DistanceToOut(pointcontainer[i], dircontainer[i]);
    // std::cerr << "VG DO " << i << " " << d << "\n";
    totaldistout += d;
  }
  high_resolution_clock::time_point t2 = high_resolution_clock::now();
  duration<double> time_span           = duration_cast<duration<double>>(t2 - t1);
  std::cout << "It took me " << time_span.count() << " seconds.\n";
}

#ifdef VECGEOM_ROOT
__attribute__((noinline)) void TimeTGeo(TGeoPolygon const &poly, SOA3D<Precision> const &container, int &count)
{
  high_resolution_clock::time_point t1 = high_resolution_clock::now();
  const auto S                         = container.size();
  for (size_t i = 0; i < S; ++i) {
    auto p      = container[i];
    double x[3] = {p.x(), p.y(), p.z()};
    if (poly.Contains(x)) count++;
  }
  high_resolution_clock::time_point t2 = high_resolution_clock::now();
  duration<double> time_span           = duration_cast<duration<double>>(t2 - t1);
  std::cout << "It took me " << time_span.count() << " seconds.\n";
}

__attribute__((noinline)) void TimeTGeoDistanceToIn(TGeoXtru const &xtru, SOA3D<Precision> const &pointcontainer,
                                                    SOA3D<Precision> const &dircontainer, int &count)
{
  high_resolution_clock::time_point t1 = high_resolution_clock::now();
  const auto S                         = pointcontainer.size();
  for (size_t i = 0; i < S; ++i) {
    auto p      = pointcontainer[i];
    auto dir    = dircontainer[i];
    double x[3] = {p.x(), p.y(), p.z()};
    double d[3] = {dir.x(), dir.y(), dir.z()};
    auto dist   = xtru.TGeoXtru::DistFromOutside(x, d);
    // std::cerr << "ROOT D " << i << " " << dist << "\n";
    if (dist < 1E30) count++;
  }
  high_resolution_clock::time_point t2 = high_resolution_clock::now();
  duration<double> time_span           = duration_cast<duration<double>>(t2 - t1);
  std::cout << "It took me " << time_span.count() << " seconds.\n";
}

__attribute__((noinline)) void TimeTGeoDistanceToOut(TGeoXtru const &xtru, SOA3D<Precision> const &pointcontainer,
                                                     SOA3D<Precision> const &dircontainer, double &totaldistout)
{
  high_resolution_clock::time_point t1 = high_resolution_clock::now();
  const auto S                         = pointcontainer.size();
  for (size_t i = 0; i < S; ++i) {
    auto p      = pointcontainer[i];
    auto dir    = dircontainer[i];
    double x[3] = {p.x(), p.y(), p.z()};
    double d[3] = {dir.x(), dir.y(), dir.z()};
    auto dist   = xtru.TGeoXtru::DistFromInside(x, d);
    // std::cerr << "ROOT DO " << i << " " << dist << "\n";
    totaldistout += dist;
  }
  high_resolution_clock::time_point t2 = high_resolution_clock::now();
  duration<double> time_span           = duration_cast<duration<double>>(t2 - t1);
  std::cout << "It took me " << time_span.count() << " seconds.\n";
}

__attribute__((noinline)) void TimeTGeoDistanceToIn(TGeoBBox const &box, SOA3D<Precision> const &pointcontainer,
                                                    SOA3D<Precision> const &dircontainer, int &count)
{
  high_resolution_clock::time_point t1 = high_resolution_clock::now();
  const auto S                         = pointcontainer.size();
  for (size_t i = 0; i < S; ++i) {
    auto p      = pointcontainer[i];
    auto dir    = dircontainer[i];
    double x[3] = {p.x(), p.y(), p.z()};
    double d[3] = {dir.x(), dir.y(), dir.z()};
    auto dist   = box.TGeoBBox::DistFromOutside(x, d);
    // std::cerr << "ROOTB D " << i << " " << dist << "\n";
    if (dist < 1E30) count++;
  }
  high_resolution_clock::time_point t2 = high_resolution_clock::now();
  duration<double> time_span           = duration_cast<duration<double>>(t2 - t1);
  std::cout << "It took me " << time_span.count() << " seconds.\n";
}

__attribute__((noinline)) void TimeTGeoSafety(TGeoPolygon const &poly, SOA3D<Precision> const &container,
                                              double &totalsafety)
{
  high_resolution_clock::time_point t1 = high_resolution_clock::now();
  const auto S                         = container.size();
  for (size_t i = 0; i < S; ++i) {
    int segmentid;
    const auto p = container[i];
    // const Precision x[3] = {p.x(),p.y(),p.z()};
    totalsafety += poly.Safety(&Vector3D<double>(p)[0], segmentid);
  }
  high_resolution_clock::time_point t2 = high_resolution_clock::now();
  duration<double> time_span           = duration_cast<duration<double>>(t2 - t1);
  std::cout << "It took me " << time_span.count() << " seconds.\n";
}
#endif

int main()
{
  //  const int N = 100;
  //  Precision x[N], y[N];

  ////  for (size_t i = 0; i < N; ++i) {
  ////    x[i] = std::sin(i * (2. * M_PI) / N);
  ////    y[i] = std::cos(i * (2. * M_PI) / N);
  ////  }

  ////  box-like (has to be clockwise order)
  constexpr int N = 4;
  Precision x[N]  = {-4, -4, 4, 4};
  Precision y[N]  = {-4, 4, 4, -4};

#ifdef VECGEOM_ROOT
  double xd[N] = {-4, -4, 4, 4};
  double yd[N] = {-4, 4, 4, -4};
  TGeoBBox box(4, 4, 10);
#endif

  // Precision x[6]={0,1,1.5,1.5,1.0,-0.5};
  // Precision y[6]={0,0,1,2,2,1};
  vecgeom::PlanarPolygon poly(N, x, y);

  std::cerr << "convexity" << poly.IsConvex() << "\n";

  vecgeom::PolygonalShell shell(N, x, y, -10., 10.);
  auto d = shell.DistanceToIn(Vector3D<Precision>(-100, 0., 0.), Vector3D<Precision>(1., 0., 0.));
  std::cerr << "distance " << d << "\n";

  vecgeom::UnplacedSExtruVolume vol(N, x, y, -10., 10.);
  {
    auto d = vol.DistanceToIn(Vector3D<Precision>(-100, 0., 0.), Vector3D<Precision>(1., 0., 0.));
    std::cerr << "distance " << d << "\n";
  }
  {
    auto d = vol.DistanceToIn(Vector3D<Precision>(0, 0., 20), Vector3D<Precision>(0., 0., -1.));
    std::cerr << "distance " << d << "\n";
  }
  {
    auto d = vol.DistanceToIn(Vector3D<Precision>(0, 0., -20), Vector3D<Precision>(0., 0., 1.));
    std::cerr << "distance " << d << "\n";
  }

  std::cerr << poly.OnSegment<Precision, Precision, bool>(0, 1.0, 2.0) << "\n";
  std::cerr << poly.OnSegment<Precision, Precision, bool>(0, x[0], y[0]) << "\n";

  //  for (size_t i = 0; i < N; ++i) {
  //   // midpoints should be on Segment
  //    std::cerr << poly.OnSegment<Precision,Precision,bool>(i, 0.5*(x[i]+x[(i-1)%N]), 0.5*(y[i]+y[(i-1)%N])) << "\n";
  //  }

  Vector3D<Precision> lower(poly.GetMinX() - 1, poly.GetMinY() - 1, -15);
  Vector3D<Precision> upper(poly.GetMaxX() + 1, poly.GetMaxY() + 1, 15);

  SOA3D<Precision> container;
  volumeUtilities::FillRandomPoints(lower, upper, container);

  SOA3D<Precision> pointsout(100000);
  SOA3D<Precision> pointsin(100000);
  SOA3D<Precision> dircontainer(100000);
  volumeUtilities::FillRandomPoints<SOA3D<Precision>, UnplacedSExtruVolume, true>(lower, upper, vol, pointsout);
  volumeUtilities::FillRandomPoints<SOA3D<Precision>, UnplacedSExtruVolume, false>(lower, upper, vol, pointsin);
  volumeUtilities::FillRandomDirections(dircontainer);

  UnplacedSExtruVolume dummy(N, x, y, -10, 10);
  Vector3D<Precision> n;
  bool v = dummy.Normal(Vector3D<Precision>(-4, 0., 0.), n);
  std::cerr << n << " valid " << v << "\n";
  v = dummy.Normal(Vector3D<Precision>(0, 4., 0.), n);
  std::cerr << n << " valid " << v << "\n";
  v = dummy.Normal(Vector3D<Precision>(4, 0., 0.), n);
  std::cerr << n << " valid " << v << "\n";
  v = dummy.Normal(Vector3D<Precision>(0, -4., 0.), n);
  std::cerr << n << " valid " << v << "\n";
  v = dummy.Normal(Vector3D<Precision>(0, 0., 10.), n);
  std::cerr << n << " valid " << v << "\n";
  v = dummy.Normal(Vector3D<Precision>(0, 0., -10.), n);
  std::cerr << n << " valid " << v << "\n";

#ifdef VECGEOM_ROOT
  TGeoPolygon geopoly(N);
  geopoly.SetXY(xd, yd);
  geopoly.FinishPolygon();

  TGeoXtru geoxtru(2);
  geoxtru.DefinePolygon(N, xd, yd);
  geoxtru.DefineSection(0, -10, 0., 0., 1.);
  geoxtru.DefineSection(1, 10, 0., 0., 1.);
  {
    double p[3] = {-100, 0., 0.};
    double d[3] = {1, 0., 0.};

    std::cerr << "ROOT distance " << geoxtru.DistFromOutside(p, d) << "\n";
  }
  {
    double p[3] = {0, 0., 20.};
    double d[3] = {0, 0., -1.};

    std::cerr << "ROOT distance " << geoxtru.DistFromOutside(p, d) << "\n";
  }
  {
    double p[3] = {0, 0., -20.};
    double d[3] = {0, 0., 1.};

    std::cerr << "ROOT distance " << geoxtru.DistFromOutside(p, d) << "\n";
  }
// return 1;
#endif

  int counter = 0;
  TimeVecGeom(poly, container, counter);
  std::cerr << counter << "\n";

  {
    int counter = 0;
    TimeVecGeomDistanceToIn(vol, pointsout, dircontainer, counter);
    std::cerr << "HITTING :" << counter << "\n";
  }

  {
    double out = 0;
    TimeVecGeomDistanceToOut(vol, pointsin, dircontainer, out);
    std::cerr << "accum DO :" << out << "\n";
  }

#ifdef VECGEOM_ROOT
  counter = 0;
  TimeTGeo(geopoly, container, counter);
  std::cerr << counter << "\n";
  {
    counter = 0;
    TimeTGeoDistanceToIn(geoxtru, pointsout, dircontainer, counter);
    std::cerr << "ROOT HITTING :" << counter << "\n";
  }

  {
    double out = 0.;
    TimeTGeoDistanceToOut(geoxtru, pointsin, dircontainer, out);
    std::cerr << "ROOT accum DO :" << out << "\n";
  }

  {
    counter = 0;
    TimeTGeoDistanceToIn(box, pointsout, dircontainer, counter);
    std::cerr << "ROOT HITTING :" << counter << "\n";
  }

#endif

  {
    double totals = 0;
    TimeVecGeomSafety(poly, container, totals);
    std::cerr << totals << "\n";
  }

#ifdef VECGEOM_ROOT
  {
    double totals = 0;
    TimeTGeoSafety(geopoly, container, totals);
    std::cerr << totals << "\n";
  }
#endif

  return 0;
}