File: UnplacedParallelepiped.cpp

package info (click to toggle)
vecgeom 1.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,928 kB
  • sloc: cpp: 88,717; ansic: 6,894; python: 1,035; sh: 582; sql: 538; makefile: 29
file content (226 lines) | stat: -rw-r--r-- 9,130 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
// This file is part of VecGeom and is distributed under the
// conditions in the file LICENSE.txt in the top directory.
// For the full list of authors see CONTRIBUTORS.txt and `git log`.

/// @file source/UnplacedParallelepiped.cpp
/// @author Created by Johannes de Fine Licht
/// @author Modified and completed by Mihaela Gheata, Evgueni Tcherniaev

#include "VecGeom/volumes/UnplacedParallelepiped.h"

#include <stdio.h>
#include "VecGeom/management/VolumeFactory.h"
#include "VecGeom/volumes/SpecializedParallelepiped.h"
#include "VecGeom/volumes/utilities/GenerationUtilities.h"
#include "VecGeom/base/RNG.h"

namespace vecgeom {
inline namespace VECGEOM_IMPL_NAMESPACE {

#ifndef VECCORE_CUDA
SolidMesh *UnplacedParallelepiped::CreateMesh3D(Transformation3D const &trans, size_t nSegments) const
{
  SolidMesh *sm = new SolidMesh();
  sm->ResetMesh(8, 6);

  Precision dx = fPara.fDimensions[0] * 2;
  Precision dy = fPara.fDimensions[1] * 2;
  Precision dz = fPara.fDimensions[2] * 2;

  Precision gamma = fPara.fPhi;
  Precision alpha = fPara.fTheta;
  Precision beta  = fPara.fAlpha;

  Precision intermediate = (std::cos(alpha) - std::cos(beta) * std::cos(gamma)) / std::sin(gamma);

  Vector3D<Precision> a = Vector3D<Precision>(dx, 0, 0);
  Vector3D<Precision> b = Vector3D<Precision>(dy * std::cos(gamma), dy * std::sin(gamma), 0);
  Vector3D<Precision> c =
      Vector3D<Precision>(dz * std::cos(beta), dz * intermediate,
                          dz * std::sqrt(1 - std::cos(beta) * std::cos(beta) - intermediate * intermediate));

  Utils3D::Vec_t vertices[] = {a, a + b, a + b + c, a + c, Vector3D<Precision>(), b, b + c, c};

  // subtract to move the origin to center
  Vector3D<Precision> origin = (a + b + c) * 0.5;
  for (auto &vertex : vertices)
    vertex -= origin;

  sm->SetVertices(vertices, 8);
  sm->TransformVertices(trans);

  sm->AddPolygon(4, {0, 1, 2, 3}, true);
  sm->AddPolygon(4, {4, 7, 6, 5}, true);
  sm->AddPolygon(4, {0, 4, 5, 1}, true);
  sm->AddPolygon(4, {1, 5, 6, 2}, true);
  sm->AddPolygon(4, {2, 6, 7, 3}, true);
  sm->AddPolygon(4, {3, 7, 4, 0}, true);
  return sm;
}
#endif

//______________________________________________________________________________
VECCORE_ATT_HOST_DEVICE
void UnplacedParallelepiped::Print() const
{
  printf("UnplacedParallelepiped {%.2f, %.2f, %.2f, %.2f, %.2f, %.2f}", GetX(), GetY(), GetZ(), GetTanAlpha(),
         GetTanThetaCosPhi(), GetTanThetaSinPhi());
}

//______________________________________________________________________________
void UnplacedParallelepiped::Print(std::ostream &os) const
{
  os << "UnplacedParallelepiped {" << GetX() << ", " << GetY() << ", " << GetZ() << ", " << GetTanAlpha() << ", "
     << GetTanThetaCosPhi() << ", " << GetTanThetaSinPhi();
}

//______________________________________________________________________________
VECCORE_ATT_HOST_DEVICE
void UnplacedParallelepiped::Extent(Vector3D<Precision> &aMin, Vector3D<Precision> &aMax) const
{
  // Returns the full 3D cartesian extent of the solid.
  Precision dx = fPara.fDimensions[0] + fPara.fDimensions[1] * Abs(fPara.fTanAlpha) +
                 fPara.fDimensions[2] * Abs(fPara.fTanThetaCosPhi);
  Precision dy = fPara.fDimensions[1] + fPara.fDimensions[2] * Abs(fPara.fTanThetaSinPhi);
  Precision dz = fPara.fDimensions[2];
  aMin.Set(-dx, -dy, -dz);
  aMax.Set(dx, dy, dz);
}

//______________________________________________________________________________
Vector3D<Precision> UnplacedParallelepiped::SamplePointOnSurface() const
{
  // Set array for selection of facet
  Precision surf[6];
  surf[0] = fPara.fAreas[0];
  surf[1] = surf[0] + fPara.fAreas[0];
  surf[2] = surf[1] + fPara.fAreas[1];
  surf[3] = surf[2] + fPara.fAreas[1];
  surf[4] = surf[3] + fPara.fAreas[2];
  surf[5] = surf[4] + fPara.fAreas[2];

  // Select facet
  Precision select = surf[5] * RNG::Instance().uniform();
  int isurf        = 5;
  if (select <= surf[4]) isurf = 4;
  if (select <= surf[3]) isurf = 3;
  if (select <= surf[2]) isurf = 2;
  if (select <= surf[1]) isurf = 1;
  if (select <= surf[0]) isurf = 0;

  // Set working variables
  Precision Dx               = fPara.fDimensions[0];
  Precision Dy               = fPara.fDimensions[1];
  Precision Dz               = fPara.fDimensions[2];
  Precision DyTanAlpha       = Dy * fPara.fTanAlpha;
  Precision DzTanThetaCosPhi = Dz * fPara.fTanThetaCosPhi;
  Precision DzTanThetaSinPhi = Dz * fPara.fTanThetaSinPhi;

  // Sample random point
  Vector3D<Precision> p0, p1, p2;
  switch (isurf) {
  case 0: // -x
    p0.Set(-DzTanThetaCosPhi - DyTanAlpha - Dx, -DzTanThetaSinPhi - Dy, -Dz);
    p1.Set(-DzTanThetaCosPhi + DyTanAlpha - Dx, -DzTanThetaSinPhi + Dy, -Dz);
    p2.Set(DzTanThetaCosPhi - DyTanAlpha - Dx, DzTanThetaSinPhi - Dy, Dz);
    break;
  case 1: // +x
    p0.Set(-DzTanThetaCosPhi - DyTanAlpha + Dx, -DzTanThetaSinPhi - Dy, -Dz);
    p1.Set(DzTanThetaCosPhi - DyTanAlpha + Dx, DzTanThetaSinPhi - Dy, Dz);
    p2.Set(-DzTanThetaCosPhi + DyTanAlpha + Dx, -DzTanThetaSinPhi + Dy, -Dz);
    break;
  case 2: // -y
    p0.Set(-DzTanThetaCosPhi - DyTanAlpha - Dx, -DzTanThetaSinPhi - Dy, -Dz);
    p1.Set(DzTanThetaCosPhi - DyTanAlpha - Dx, DzTanThetaSinPhi - Dy, Dz);
    p2.Set(-DzTanThetaCosPhi - DyTanAlpha + Dx, -DzTanThetaSinPhi - Dy, -Dz);
    break;
  case 3: // +y
    p0.Set(-DzTanThetaCosPhi + DyTanAlpha - Dx, -DzTanThetaSinPhi + Dy, -Dz);
    p1.Set(-DzTanThetaCosPhi + DyTanAlpha + Dx, -DzTanThetaSinPhi + Dy, -Dz);
    p2.Set(DzTanThetaCosPhi + DyTanAlpha - Dx, DzTanThetaSinPhi + Dy, Dz);
    break;
  case 4: // -z
    p0.Set(-DzTanThetaCosPhi - DyTanAlpha - Dx, -DzTanThetaSinPhi - Dy, -Dz);
    p1.Set(-DzTanThetaCosPhi - DyTanAlpha + Dx, -DzTanThetaSinPhi - Dy, -Dz);
    p2.Set(-DzTanThetaCosPhi + DyTanAlpha - Dx, -DzTanThetaSinPhi + Dy, -Dz);
    break;
  case 5: // +z
    p0.Set(DzTanThetaCosPhi - DyTanAlpha - Dx, DzTanThetaSinPhi - Dy, Dz);
    p1.Set(DzTanThetaCosPhi + DyTanAlpha - Dx, DzTanThetaSinPhi + Dy, Dz);
    p2.Set(DzTanThetaCosPhi - DyTanAlpha + Dx, DzTanThetaSinPhi - Dy, Dz);
    break;
  }
  Precision u = RNG::Instance().uniform();
  Precision v = RNG::Instance().uniform();
  return p0 + u * (p1 - p0) + v * (p2 - p0);
}

//______________________________________________________________________________
template <TranslationCode transCodeT, RotationCode rotCodeT>
VECCORE_ATT_DEVICE
VPlacedVolume *UnplacedParallelepiped::Create(LogicalVolume const *const logical_volume,
                                              Transformation3D const *const transformation,
#ifdef VECCORE_CUDA
                                              const int id, const int copy_no, const int child_id,
#endif
                                              VPlacedVolume *const placement)
{

  return CreateSpecializedWithPlacement<SpecializedParallelepiped<transCodeT, rotCodeT>>(
#ifdef VECCORE_CUDA
      logical_volume, transformation, id, copy_no, child_id, placement); // TODO: add bounding box?
#else
      logical_volume, transformation, placement);
#endif
}

//______________________________________________________________________________
VECCORE_ATT_DEVICE
VPlacedVolume *UnplacedParallelepiped::SpecializedVolume(LogicalVolume const *const volume,
                                                         Transformation3D const *const transformation,
                                                         const TranslationCode trans_code, const RotationCode rot_code,
#ifdef VECCORE_CUDA
                                                         const int id, const int copy_no, const int child_id,
#endif
                                                         VPlacedVolume *const placement) const
{
  return VolumeFactory::CreateByTransformation<UnplacedParallelepiped>(volume, transformation, trans_code, rot_code,
#ifdef VECCORE_CUDA
                                                                       id, copy_no, child_id,
#endif
                                                                       placement);
}

#ifdef VECGEOM_CUDA_INTERFACE

//______________________________________________________________________________
DevicePtr<cuda::VUnplacedVolume> UnplacedParallelepiped::CopyToGpu(
    DevicePtr<cuda::VUnplacedVolume> const in_gpu_ptr) const
{
  return CopyToGpuImpl<UnplacedParallelepiped>(in_gpu_ptr, GetX(), GetY(), GetZ(), GetAlpha(), GetTheta(), GetPhi());
}

//______________________________________________________________________________
DevicePtr<cuda::VUnplacedVolume> UnplacedParallelepiped::CopyToGpu() const
{
  return CopyToGpuImpl<UnplacedParallelepiped>();
}

#endif // VECGEOM_CUDA_INTERFACE

} // namespace VECGEOM_IMPL_NAMESPACE

#ifdef VECCORE_CUDA

namespace cxx {

template size_t DevicePtr<cuda::UnplacedParallelepiped>::SizeOf();
template void DevicePtr<cuda::UnplacedParallelepiped>::Construct(const Precision x, const Precision y,
                                                                 const Precision z, const Precision alpha,
                                                                 const Precision theta, const Precision phi) const;

} // namespace cxx

#endif

} // namespace vecgeom