ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SurfaceVisualizationBase.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SurfaceVisualizationBase.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
13 
31 
32 #include <fstream>
33 #include <sstream>
34 #include <string>
35 
36 namespace Acts {
37 namespace SurfaceVisualization {
38 
46 static inline std::string test(IVisualization& helper, bool triangulate,
47  const std::string& tag) {
48  auto gctx = GeometryContext();
49  auto identity = std::make_shared<Transform3D>(Transform3D::Identity());
50  std::stringstream cStream;
51 
52  double halfPhiSector = M_PI / 4.;
53  double centralPhi = M_PI / 2.;
54 
58  auto write = [&](const std::string& path, bool clear = true) -> void {
59  std::string wpath = path + tag;
60  helper.write(wpath);
61 
62  // write again for the caracter count
63  cStream << std::setprecision(4);
64  helper.write(cStream);
65  if (clear) {
66  helper.clear();
67  }
68  };
69 
70  //----------------------------------------------------
71  // Cone Surface section
72  IVisualization::ColorType coneColor = {252, 160, 0};
73  std::vector<std::shared_ptr<ConeSurface>> coneSurfaces;
74 
75  double coneAlpha = 0.245;
76  double coneMinZ = 0.;
77  double coneCutZ = 2.;
78  double coneMaxZ = 10.;
79  // Full Cone
80  auto coneBounds =
81  std::make_shared<ConeBounds>(coneAlpha, -coneCutZ, coneMaxZ);
82  auto cone = Surface::makeShared<ConeSurface>(identity, coneBounds);
83  coneSurfaces.push_back(cone);
84  Visualization::drawSurface(helper, *cone, gctx, Transform3D::Identity(), 72,
85  triangulate, coneColor);
86  write("Surfaces_ConeSurface");
87 
88  // Sectoral Cone
89  coneBounds = std::make_shared<ConeBounds>(coneAlpha, coneMinZ, coneMaxZ,
90  halfPhiSector);
91  cone = Surface::makeShared<ConeSurface>(identity, coneBounds);
92  coneSurfaces.push_back(cone);
93  Visualization::drawSurface(helper, *cone, gctx, Transform3D::Identity(), 72,
94  triangulate, coneColor);
95  write("Surfaces_ConeSurfaceSector");
96 
97  // Sectoral Cone Shifted
98  coneBounds = std::make_shared<ConeBounds>(coneAlpha, coneCutZ, coneMaxZ,
99  halfPhiSector, centralPhi);
100  cone = Surface::makeShared<ConeSurface>(identity, coneBounds);
101  coneSurfaces.push_back(cone);
102  Visualization::drawSurface(helper, *cone, gctx, Transform3D::Identity(), 72,
103  triangulate, coneColor);
104  write("Surfaces_ConeSurfaceSectorShifted");
105 
106  // All in one for radial bounds
107  std::vector<Transform3D> threeCones = {
108  Transform3D(Translation3D{-0.5 * coneMaxZ, 0., 0.}),
109  Transform3D(Translation3D{0., 0., 0.}),
110  Transform3D(Translation3D{0.75 * coneMaxZ, 0., 0.})};
111 
112  for (size_t ic = 0; ic < coneSurfaces.size(); ++ic) {
113  Visualization::drawSurface(helper, *coneSurfaces[ic], gctx, threeCones[ic],
114  72, triangulate, coneColor);
115  }
116  write("Surfaces_All_ConeSurfaces");
117 
118  //----------------------------------------------------
119  // Cylinder surface section
120  IVisualization::ColorType cylinderColor = {0, 196, 252};
121  std::vector<std::shared_ptr<CylinderSurface>> cylinderSurfaces;
122 
123  double cylinderRadius = 5.;
124  double cylinderHalfZ = 10.;
125 
126  // Full Cylinder
127  auto cylinderBounds =
128  std::make_shared<CylinderBounds>(cylinderRadius, cylinderHalfZ);
129  auto cylinder =
130  Surface::makeShared<CylinderSurface>(identity, cylinderBounds);
131  cylinderSurfaces.push_back(cylinder);
132  Visualization::drawSurface(helper, *cylinder, gctx, Transform3D::Identity(),
133  72, triangulate, cylinderColor);
134  write("Surfaces_CylinderSurface");
135  // Sectoral Cone
136  cylinderBounds = std::make_shared<CylinderBounds>(
137  cylinderRadius, cylinderHalfZ, halfPhiSector);
138  cylinder = Surface::makeShared<CylinderSurface>(identity, cylinderBounds);
139  cylinderSurfaces.push_back(cylinder);
140  Visualization::drawSurface(helper, *cylinder, gctx, Transform3D::Identity(),
141  72, triangulate, cylinderColor);
142  write("Surfaces_CylinderSurfaceSector");
143 
144  // Sectoral Cone Shifted
145  cylinderBounds = std::make_shared<CylinderBounds>(
146  cylinderRadius, cylinderHalfZ, halfPhiSector, centralPhi);
147  cylinder = Surface::makeShared<CylinderSurface>(identity, cylinderBounds);
148  cylinderSurfaces.push_back(cylinder);
149  Visualization::drawSurface(helper, *cylinder, gctx, Transform3D::Identity(),
150  72, triangulate, cylinderColor);
151  write("Surfaces_CylinderSurfaceSectorShifted");
152 
153  // All in one for radial bounds
154  std::vector<Transform3D> threeCylinders = {
155  Transform3D(Translation3D{-2 * cylinderRadius, 0., 0.}),
156  Transform3D(Translation3D{0., 0., 0.}),
157  Transform3D(Translation3D{2.5 * cylinderRadius, 0., 0.})};
158 
159  for (size_t ic = 0; ic < cylinderSurfaces.size(); ++ic) {
160  Visualization::drawSurface(helper, *cylinderSurfaces[ic], gctx,
161  threeCylinders[ic], 72, triangulate,
162  cylinderColor);
163  }
164  write("Surfaces_All_CylinderSurfaces");
165 
168  auto writeBoundingBox2D = [&](const RectangleBounds& rBounds,
169  const std::string& path) -> void {
170  IVisualization::ColorType bbColor = {126, 126, 126};
171 
172  std::string bbPath = path + tag + "_bbox";
173 
174  auto bbBounds = std::make_shared<RectangleBounds>(rBounds);
175  auto bbSurface = Surface::makeShared<PlaneSurface>(identity, bbBounds);
176  Visualization::drawSurface(helper, *bbSurface, gctx,
177  Transform3D::Identity(), 72, triangulate,
178  bbColor);
179  write(bbPath);
180  };
181 
182  //----------------------------------------------------
183  // Disc Surface section
184  IVisualization::ColorType discColor = {126, 252, 0};
185 
186  double discRmin = 5.;
187  double discRmax = 10.;
188 
189  std::vector<std::shared_ptr<DiscSurface>> radialSurfaces;
190 
191  // Full Disc
192  auto radialBounds = std::make_shared<RadialBounds>(0., discRmax);
193  auto disc = Surface::makeShared<DiscSurface>(identity, radialBounds);
194  radialSurfaces.push_back(disc);
195  Visualization::drawSurface(helper, *disc, gctx, Transform3D::Identity(), 72,
196  triangulate, discColor);
197  write("Surfaces_DiscSurfaceFull");
198 
199  // Full Sectoral Disc
200  radialBounds = std::make_shared<RadialBounds>(0., discRmax, halfPhiSector);
201  disc = Surface::makeShared<DiscSurface>(identity, radialBounds);
202  radialSurfaces.push_back(disc);
203  Visualization::drawSurface(helper, *disc, gctx, Transform3D::Identity(), 72,
204  triangulate, discColor);
205  write("Surfaces_DiscSurfaceFullSector");
206 
207  // Full Sectoral Shifted Disc
208  radialBounds =
209  std::make_shared<RadialBounds>(0., discRmax, halfPhiSector, centralPhi);
210  disc = Surface::makeShared<DiscSurface>(identity, radialBounds);
211  radialSurfaces.push_back(disc);
212  Visualization::drawSurface(helper, *disc, gctx, Transform3D::Identity(), 72,
213  triangulate, discColor);
214  write("Surfaces_DiscSurfaceFullSectorShifted");
215 
216  // Full Ring
217  radialBounds = std::make_shared<RadialBounds>(discRmin, discRmax);
218  disc = Surface::makeShared<DiscSurface>(identity, radialBounds);
219  radialSurfaces.push_back(disc);
220  Visualization::drawSurface(helper, *disc, gctx, Transform3D::Identity(), 72,
221  triangulate, discColor);
222  write("Surfaces_DiscSurfaceRing");
223 
224  // Full Sectoral Rin g
225  radialBounds =
226  std::make_shared<RadialBounds>(discRmin, discRmax, halfPhiSector);
227  disc = Surface::makeShared<DiscSurface>(identity, radialBounds);
228  radialSurfaces.push_back(disc);
229  Visualization::drawSurface(helper, *disc, gctx, Transform3D::Identity(), 72,
230  triangulate, discColor);
231  write("Surfaces_DiscSurfaceRingSector");
232 
233  // Full Sectoral Shifted Ring
234  radialBounds = std::make_shared<RadialBounds>(discRmin, discRmax,
235  halfPhiSector, centralPhi);
236  disc = Surface::makeShared<DiscSurface>(identity, radialBounds);
237  radialSurfaces.push_back(disc);
238  Visualization::drawSurface(helper, *disc, gctx, Transform3D::Identity(), 72,
239  triangulate, discColor);
240  write("Surfaces_DiscSurfaceRingSectorShifted");
241 
242  // All in one for radial bounds
243  std::vector<Transform3D> sixDiscs = {
244  Transform3D(Translation3D{-2.0 * discRmax, 1.5 * discRmax, 0.}),
245  Transform3D(Translation3D{0., 1.5 * discRmax, 0.}),
246  Transform3D(Translation3D{2.5 * discRmax, 1.5 * discRmax, 0.}),
247  Transform3D(Translation3D{-2.0 * discRmax, -1.5 * discRmax, 0.}),
248  Transform3D(Translation3D{0., -1.5 * discRmax, 0.}),
249  Transform3D(Translation3D{2.5 * discRmax, -1.5 * discRmax, 0.})};
250  for (size_t ir = 0; ir < radialSurfaces.size(); ++ir) {
251  Visualization::drawSurface(helper, *radialSurfaces[ir], gctx, sixDiscs[ir],
252  72, triangulate, discColor);
253  }
254  write("Surfaces_All_DiscSurfaces_RadialBounds");
255 
256  std::vector<std::shared_ptr<DiscSurface>> anomalDiscSurfaces;
257  IVisualization::ColorType discAnomalColor = {94, 186, 2};
258 
259  double annulusMinPhi = 0.75;
260  double annulusMaxPhi = 1.35;
261  Vector2D offset(-4., 2.);
262  auto annulus = std::make_shared<AnnulusBounds>(
263  discRmin, discRmax, annulusMinPhi, annulusMaxPhi, offset);
264  disc = Surface::makeShared<DiscSurface>(identity, annulus);
265  anomalDiscSurfaces.push_back(disc);
266  Visualization::drawSurface(helper, *disc, gctx, Transform3D::Identity(), 72,
267  triangulate, discAnomalColor);
268  write("Surfaces_DiscAnulusBounds");
269 
270  double discTrapezoidHxRmin = 3.;
271  double discTrapezoidHxRmax = 6.;
272  auto discTrapezoid = std::make_shared<DiscTrapezoidBounds>(
273  discTrapezoidHxRmin, discTrapezoidHxRmax, discRmin, discRmax);
274  disc = Surface::makeShared<DiscSurface>(identity, discTrapezoid);
275  anomalDiscSurfaces.push_back(disc);
276  Visualization::drawSurface(helper, *disc, gctx, Transform3D::Identity(), 72,
277  triangulate, discAnomalColor);
278  write("Surfaces_DiscTrapezoidBounds");
279 
280  // All in one for radial bounds
281  std::vector<Transform3D> twoAnomalDiscs = {
282  Transform3D(Translation3D{-5., 0., 0.}),
283  Transform3D(Translation3D{5., 0., 0.})};
284  for (size_t id = 0; id < anomalDiscSurfaces.size(); ++id) {
285  Visualization::drawSurface(helper, *anomalDiscSurfaces[id], gctx,
286  sixDiscs[id], 72, triangulate, discAnomalColor);
287  }
288  write("Surfaces_All_DiscSurfaces_AnomalBounds");
289 
290  //----------------------------------------------------
291  // Plane Surface section
292  IVisualization::ColorType planeColor = {0, 0, 255};
293  std::vector<std::shared_ptr<PlaneSurface>> planarSurfaces;
294 
295  // Ellipse shaped : Full Ellipse
296  double ellipseR0min = 2;
297  double ellipseR0max = 4;
298  double ellipseR1min = 3;
299  double ellipseR1max = 6;
300  std::string name = "Surfaces_PlaneSurfaceEllipse";
301  auto ellipse =
302  std::make_shared<EllipseBounds>(0., 0., ellipseR1min, ellipseR1max);
303  auto plane = Surface::makeShared<PlaneSurface>(identity, ellipse);
304  planarSurfaces.push_back(plane);
305  Visualization::drawSurface(helper, *plane, gctx, Transform3D::Identity(), 72,
306  triangulate, planeColor);
307  write(name);
308  writeBoundingBox2D(ellipse->boundingBox(), name);
309 
310  // Ellipse shaped : Ring Ellipse
311  name = "Surfaces_PlaneSurfaceEllipseRing";
312  ellipse = std::make_shared<EllipseBounds>(ellipseR0min, ellipseR0max,
313  ellipseR1min, ellipseR1max);
314  plane = Surface::makeShared<PlaneSurface>(identity, ellipse);
315  planarSurfaces.push_back(plane);
316  Visualization::drawSurface(helper, *plane, gctx, Transform3D::Identity(), 72,
317  triangulate, planeColor);
318  write(name);
319  writeBoundingBox2D(ellipse->boundingBox(), name);
320 
321  // Ellipse shaped : Ring Ellipse Sector
322  name = "Surfaces_PlaneSurfaceEllipseRingSector";
323  ellipse = std::make_shared<EllipseBounds>(
324  ellipseR0min, ellipseR0max, ellipseR1min, ellipseR1max, halfPhiSector);
325  plane = Surface::makeShared<PlaneSurface>(identity, ellipse);
326  planarSurfaces.push_back(plane);
327  Visualization::drawSurface(helper, *plane, gctx, Transform3D::Identity(), 72,
328  triangulate, planeColor);
329  write(name);
330  writeBoundingBox2D(ellipse->boundingBox(), name);
331 
332  // ConvexPolygon shaped example: Triangle
333  name = "Surfaces_PlaneSurfaceTriangleRegular";
334  std::vector<Vector2D> tvertices = {{-3, -1.5}, {3, -1.5}, {0, 4.5}};
335  auto triangle = std::make_shared<ConvexPolygonBounds<3>>(tvertices);
336  plane = Surface::makeShared<PlaneSurface>(identity, triangle);
337  planarSurfaces.push_back(plane);
338  Visualization::drawSurface(helper, *plane, gctx, Transform3D::Identity(), 72,
339  triangulate, planeColor);
340  write(name);
341  writeBoundingBox2D(triangle->boundingBox(), name);
342 
343  // ConvexPolygon shaped example: Triangle
344  name = "Surfaces_PlaneSurfaceTriangleGeneral";
345  tvertices = {{-1., 4.5}, {4, 6.5}, {3, 8.5}};
346  triangle = std::make_shared<ConvexPolygonBounds<3>>(tvertices);
347  plane = Surface::makeShared<PlaneSurface>(identity, triangle);
348  planarSurfaces.push_back(plane);
349  Visualization::drawSurface(helper, *plane, gctx, Transform3D::Identity(), 72,
350  triangulate, planeColor);
351  write(name);
352  writeBoundingBox2D(triangle->boundingBox(), name);
353 
354  // ConvexPolygon shaped example: Triangle
355  name = "Surfaces_PlaneSurfaceConvexPolygonGeneral";
356  tvertices = {{-1., 4.5}, {4, 6.5}, {6, 8.5}, {0, 10.5}, {-3, 6.2}};
357  auto dynamicpolygon =
358  std::make_shared<ConvexPolygonBounds<PolygonDynamic>>(tvertices);
359  plane = Surface::makeShared<PlaneSurface>(identity, dynamicpolygon);
360  planarSurfaces.push_back(plane);
361  Visualization::drawSurface(helper, *plane, gctx, Transform3D::Identity(), 72,
362  triangulate, planeColor);
363  write(name);
364  writeBoundingBox2D(dynamicpolygon->boundingBox(), name);
365 
366  // Diamond shaped
367  name = "Surfaces_PlaneSurfaceDiamond";
368  auto diamond = std::make_shared<DiamondBounds>(3., 6., 2., 2., 4.);
369  plane = Surface::makeShared<PlaneSurface>(identity, diamond);
370  planarSurfaces.push_back(plane);
371  Visualization::drawSurface(helper, *plane, gctx, Transform3D::Identity(), 72,
372  triangulate, planeColor);
373  write(name);
374  writeBoundingBox2D(diamond->boundingBox(), name);
375 
376  // Rectangle plane
377  name = "Surfaces_PlaneSurfaceRectangle";
378  auto rectangle = std::make_shared<RectangleBounds>(2., 3.);
379  plane = Surface::makeShared<PlaneSurface>(identity, rectangle);
380  planarSurfaces.push_back(plane);
381  Visualization::drawSurface(helper, *plane, gctx, Transform3D::Identity(), 72,
382  triangulate, planeColor);
383  write(name);
384  writeBoundingBox2D(rectangle->boundingBox(), name);
385 
386  // Off-centered Rectangle plane:
387  name = "Surfaces_PlaneSurfaceRectangleOffcentered";
388  rectangle =
389  std::make_shared<RectangleBounds>(Vector2D{1., 2.}, Vector2D{15., 12.});
390  plane = Surface::makeShared<PlaneSurface>(identity, rectangle);
391  Visualization::drawSurface(helper, *plane, gctx, Transform3D::Identity(), 72,
392  triangulate, planeColor);
393  write(name);
394  writeBoundingBox2D(rectangle->boundingBox(), name);
395 
396  // Off-centered Rectangle plane:
397  name = "Surfaces_PlaneSurfaceTrapezoid";
398  auto trapezoid = std::make_shared<TrapezoidBounds>(2., 5., 3.);
399  plane = Surface::makeShared<PlaneSurface>(identity, trapezoid);
400  planarSurfaces.push_back(plane);
401  Visualization::drawSurface(helper, *plane, gctx, Transform3D::Identity(), 72,
402  triangulate, planeColor);
403  write(name);
404  writeBoundingBox2D(trapezoid->boundingBox(), name);
405 
406  // All planes
407  std::vector<Transform3D> ninePlanes = {
408  Transform3D(Translation3D{-10., -10., 0.}),
409  Transform3D(Translation3D{0., -10., 0.}),
410  Transform3D(Translation3D{10., -10., 0.}),
411  Transform3D(Translation3D{-10., 0., 0.}),
412  Transform3D(Translation3D{0., -6., 0.}),
413  Transform3D(Translation3D{10., -8., 0.}),
414  Transform3D(Translation3D{-10, 10., 0.}),
415  Transform3D(Translation3D{0., 10., 0.}),
416  Transform3D(Translation3D{10., 10., 0.})};
417  for (size_t ip = 0; ip < planarSurfaces.size(); ++ip) {
418  Visualization::drawSurface(helper, *planarSurfaces[ip], gctx,
419  ninePlanes[ip], 72, triangulate, planeColor);
420  }
421  write("Surfaces_All_PlaneSurfaces");
422 
423  //----------------------------------------------------
424  // Straw Surface section
425  IVisualization::ColorType strawColor = {255, 0, 0};
426 
427  name = "Surfaces_StrawSurface";
428  auto tube = std::make_shared<LineBounds>(2., 20.);
429  auto straw = Surface::makeShared<StrawSurface>(identity, tube);
430  Visualization::drawSurface(helper, *straw, gctx, Transform3D::Identity(), 72,
431  triangulate, strawColor);
432  write(name);
433 
434  return cStream.str();
435 }
436 
437 } // namespace SurfaceVisualization
438 } // namespace Acts