ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeometryVisualization.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GeometryVisualization.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 
22 
23 namespace Acts {
24 
25 namespace Visualization {
26 
36 static inline void drawSurface(
37  IVisualization& helper, const Surface& surface, const GeometryContext& gctx,
38  const Transform3D& transform = Transform3D::Identity(), size_t lseg = 72,
39  bool triangulate = false,
40  const IVisualization::ColorType& color = {120, 120, 120}) {
41  // Drawing the polyhedron representation of surfaces
42  Polyhedron phedron = surface.polyhedronRepresentation(gctx, lseg);
43  phedron.move(transform);
44  phedron.draw(helper, triangulate, color);
45 }
46 
58 template <typename volume_t>
59 inline void drawVolume(IVisualization& helper, const volume_t& volume,
60  const GeometryContext& gctx,
61  const Transform3D& transform = Transform3D::Identity(),
62  size_t lseg = 72, bool triangulate = false,
63  const IVisualization::ColorType& color = {120, 120,
64  120}) {
65  // Drawing the polyhedron representation of surfaces
66  auto bSurfaces = volume.boundarySurfaces();
67  for (const auto& bs : bSurfaces) {
68  drawSurface(helper, bs->surfaceRepresentation(), gctx, transform, lseg,
69  triangulate, color);
70  }
71 }
72 
84 static inline void drawSegmentBase(
85  IVisualization& helper, const Vector3D& start, const Vector3D& end,
86  double thickness, int arrows = 0, double arrowLength = 0.,
87  double arrowWidth = 0., size_t lseg = 72,
88  const IVisualization::ColorType& color = {120, 120, 120}) {
89  // Draw the parameter shaft and cone
90  auto direction = Vector3D(end - start).normalized();
91  double hlength = 0.5 * Vector3D(end - start).norm();
92 
93  auto unitVectors = makeCurvilinearUnitVectors(direction);
94  RotationMatrix3D lrotation;
95  lrotation.col(0) = unitVectors.first;
96  lrotation.col(1) = unitVectors.second;
97  lrotation.col(2) = direction;
98 
99  Vector3D lcenter = 0.5 * (start + end);
100  double alength = arrowLength * (2 * hlength);
101 
102  if (arrows == 2) {
103  hlength -= alength;
104  } else if (arrows != 0) {
105  hlength -= 0.5 * alength;
106  lcenter -= Vector3D(arrows * 0.5 * alength * direction);
107  }
108 
109  // Line - draw a line
110  if (thickness > 0.) {
111  auto ltransform = std::make_shared<Transform3D>(Transform3D::Identity());
112  ltransform->prerotate(lrotation);
113  ltransform->pretranslate(lcenter);
114 
115  auto lbounds = std::make_shared<CylinderBounds>(thickness, hlength);
116  auto line = Surface::makeShared<CylinderSurface>(ltransform, lbounds);
117 
118  drawSurface(helper, *line, GeometryContext(), Transform3D::Identity(), lseg,
119  false, color);
120  } else {
121  helper.line(start, end, color);
122  }
123 
124  // Arrowheads - if configured
125  if (arrows != 0) {
126  double awith = thickness * arrowWidth;
127  double alpha = atan2(thickness * arrowWidth, alength);
128  auto plateBounds = std::make_shared<RadialBounds>(thickness, awith);
129 
130  if (arrows > 0) {
131  auto aetransform = std::make_shared<Transform3D>(Transform3D::Identity());
132  aetransform->prerotate(lrotation);
133  aetransform->pretranslate(end);
134  // Arrow cone
135  auto coneBounds = std::make_shared<ConeBounds>(alpha, -alength, 0.);
136  auto cone = Surface::makeShared<ConeSurface>(aetransform, coneBounds);
137  drawSurface(helper, *cone, GeometryContext(), Transform3D::Identity(),
138  lseg, false, color);
139  // Arrow end plate
140  auto aptransform = std::make_shared<Transform3D>(Transform3D::Identity());
141  aptransform->prerotate(lrotation);
142  aptransform->pretranslate(Vector3D(end - alength * direction));
143 
144  auto plate = Surface::makeShared<DiscSurface>(aptransform, plateBounds);
145  drawSurface(helper, *plate, GeometryContext(), Transform3D::Identity(),
146  lseg, false, color);
147  }
148  if (arrows < 0 or arrows == 2) {
149  auto astransform = std::make_shared<Transform3D>(Transform3D::Identity());
150  astransform->prerotate(lrotation);
151  astransform->pretranslate(start);
152 
153  // Arrow cone
154  auto coneBounds = std::make_shared<ConeBounds>(alpha, 0., alength);
155  auto cone = Surface::makeShared<ConeSurface>(astransform, coneBounds);
156  drawSurface(helper, *cone, GeometryContext(), Transform3D::Identity(),
157  lseg, false, color);
158  // Arrow end plate
159  auto aptransform = std::make_shared<Transform3D>(Transform3D::Identity());
160  aptransform->prerotate(lrotation);
161  aptransform->pretranslate(Vector3D(start + alength * direction));
162 
163  auto plate = Surface::makeShared<DiscSurface>(aptransform, plateBounds);
164  drawSurface(helper, *plate, GeometryContext(), Transform3D::Identity(),
165  lseg, false, color);
166  }
167  }
168 }
169 
178 static inline void drawSegment(IVisualization& helper, const Vector3D& start,
179  const Vector3D& end, double thickness,
180  size_t lseg = 72,
182  20, 120, 120}) {
183  drawSegmentBase(helper, start, end, thickness, 0, 0., 0., lseg, color);
184 }
185 
196 static inline void drawArrowBackward(
197  IVisualization& helper, const Vector3D& start, const Vector3D& end,
198  double thickness, double arrowLength, double arrowWidth, size_t lseg = 72,
199  const IVisualization::ColorType& color = {120, 120, 120}) {
200  drawSegmentBase(helper, start, end, thickness, -1, arrowLength, arrowWidth,
201  lseg, color);
202 }
203 
214 static inline void drawArrowForward(
215  IVisualization& helper, const Vector3D& start, const Vector3D& end,
216  double thickness, double arrowLength, double arrowWidth, size_t lseg = 72,
217  const IVisualization::ColorType& color = {120, 120, 120}) {
218  drawSegmentBase(helper, start, end, thickness, 1, arrowLength, arrowWidth,
219  lseg, color);
220 }
221 
232 static inline void drawArrowsBoth(
233  IVisualization& helper, const Vector3D& start, const Vector3D& end,
234  double thickness, double arrowLength, double arrowWidth, size_t lseg = 72,
235  const IVisualization::ColorType& color = {120, 120, 120}) {
236  drawSegmentBase(helper, start, end, thickness, 2, arrowLength, arrowWidth,
237  lseg, color);
238 }
239 
240 } // namespace Visualization
241 
242 } // namespace Acts