File: UnplacedTet.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 (280 lines) | stat: -rw-r--r-- 9,647 bytes parent folder | download
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
// 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/UnplacedTet.cpp
/// @author Raman Sehgal, Evgueni Tcherniaev

#include "VecGeom/volumes/UnplacedTet.h"
#include "VecGeom/management/VolumeFactory.h"
#include "VecGeom/volumes/SpecializedTet.h"
#include "VecGeom/base/RNG.h"
#include <stdio.h>
namespace vecgeom {
inline namespace VECGEOM_IMPL_NAMESPACE {

VECCORE_ATT_HOST_DEVICE
UnplacedTet::UnplacedTet()
{
  // default constructor
  fGlobalConvexity = true;
  ComputeBBox();
}

VECCORE_ATT_HOST_DEVICE
UnplacedTet::UnplacedTet(const Vector3D<Precision> &p0, const Vector3D<Precision> &p1, const Vector3D<Precision> &p2,
                         const Vector3D<Precision> &p3)
    : fTet(p0, p1, p2, p3)
{
  fGlobalConvexity = true;
  ComputeBBox();
}

VECCORE_ATT_HOST_DEVICE
void UnplacedTet::Extent(Vector3D<Precision> &aMin, Vector3D<Precision> &aMax) const
{
  /* Returns the full 3D cartesian extent of the solid.
  ** TODO : Logic to calculate the extent of Tet and assign it to aMin and aMax
  */
  aMin = aMax = fTet.fVertex[0];
  for (int i = 1; i < 4; ++i) {
    aMin.x() = Min(aMin.x(), fTet.fVertex[i].x());
    aMin.y() = Min(aMin.y(), fTet.fVertex[i].y());
    aMin.z() = Min(aMin.z(), fTet.fVertex[i].z());

    aMax.x() = Max(aMax.x(), fTet.fVertex[i].x());
    aMax.y() = Max(aMax.y(), fTet.fVertex[i].y());
    aMax.z() = Max(aMax.z(), fTet.fVertex[i].z());
  }
}

Vector3D<Precision> UnplacedTet::SamplePointOnSurface() const
{
  /* TODO : Logic to sample point on the surface of Tet
  **
  ** Return the sample Vector3D<Precision> point
  **
  ** Vector3D<Precision> point = <Logic to sample point on the surface of Tet>
  **
  ** return point
  */
  // Set areas
  //
  Precision sface[4];
  for (int i = 0; i < 4; ++i) {
    int i0   = (i + 0) % 4;
    int i1   = (i + 1) % 4;
    int i2   = (i + 2) % 4;
    sface[i] = 0.5 * ((fTet.fVertex[i1] - fTet.fVertex[i0]).Cross(fTet.fVertex[i2] - fTet.fVertex[i0])).Mag();
  }
  for (int i = 1; i < 4; ++i) {
    sface[i] += sface[i - 1];
  }

  // Select face
  //
  Precision select = sface[3] * RNG::Instance().uniform(); // G4UniformRand();
  int k            = 3;
  if (select <= sface[2]) k = 2;
  if (select <= sface[1]) k = 1;
  if (select <= sface[0]) k = 0;

  // Generate point
  //
  int i0      = (k + 0) % 4;
  int i1      = (k + 1) % 4;
  int i2      = (k + 2) % 4;
  Precision u = RNG::Instance().uniform();
  Precision v = RNG::Instance().uniform();
  if (u + v > 1.) {
    u = 1. - u;
    v = 1. - v;
  }
  return (1. - u - v) * fTet.fVertex[i0] + u * fTet.fVertex[i1] + v * fTet.fVertex[i2];
}

std::string UnplacedTet::GetEntityType() const
{
  return "Tet\n";
}

VECCORE_ATT_HOST_DEVICE
void UnplacedTet::GetParametersList(int, Precision *aArray) const
{
  // TODO : Set the aArray elements
}

// VECCORE_ATT_HOST_DEVICE
std::ostream &UnplacedTet::StreamInfo(std::ostream &os) const
// Definition taken from UTet
{
  int oldprc = os.precision(16);
  os << "-----------------------------------------------------------\n"
     //  << "     *** Dump for solid - " << GetName() << " ***\n"
     //  << "     ===================================================\n"

     << " Solid type: UTet\n"
     << " Parameters: \n"

     //    << "       Anchor Point: " << fTet.fAnchor << " mm \n"
     //    << "       Point P2: " << fTet.fP2 << " mm \n"
     //    << "       Point P3: " << fTet.fP3 << " mm \n"
     //    << "       Point P4: " << fTet.fP4 << " mm \n"
     << "-----------------------------------------------------------\n";
  os.precision(oldprc);

  return os;
}

VECCORE_ATT_HOST_DEVICE
void UnplacedTet::Print() const {
  auto const& vtx = fTet.fVertex;
  printf("UnplacedTet {V0=(%.2f, %.2f, %.2f), V1=(%.2f, %.2f, %.2f), V2=(%.2f, %.2f, %.2f), V3=(%.2f, %.2f, %.2f)}\n",
      vtx[0].x(), vtx[0].y(), vtx[0].z(), vtx[1].x(), vtx[1].y(), vtx[1].z(),
      vtx[2].x(), vtx[2].y(), vtx[2].z(), vtx[3].x(), vtx[3].y(), vtx[3].z() );
}

void UnplacedTet::Print(std::ostream &os) const {
  auto const& vtx = fTet.fVertex;
  os << "UnplacedTet {"
     <<"V0=("<< vtx[0].x() <<"; "<< vtx[0].y() <<"; "<< vtx[0].z() <<"), "
     <<"V1=("<< vtx[1].x() <<"; "<< vtx[1].y() <<"; "<< vtx[1].z() <<"), "
     <<"V2=("<< vtx[2].x() <<"; "<< vtx[2].y() <<"; "<< vtx[2].z() <<"), "
     <<"V3=("<< vtx[3].x() <<"; "<< vtx[3].y() <<"; "<< vtx[3].z() <<") }\n";
}

#ifndef VECCORE_CUDA
SolidMesh *UnplacedTet::CreateMesh3D(Transformation3D const &trans, const size_t nFaces) const
{
  SolidMesh *sm = new SolidMesh();
  sm->ResetMesh(4, 4);
  sm->SetVertices(fTet.fVertex, 4);
  sm->TransformVertices(trans);

  Vector3D<Precision> n0 =
      (sm->GetVertices()[1] - sm->GetVertices()[0]).Cross(sm->GetVertices()[2] - sm->GetVertices()[0]);
  Vector3D<Precision> n1 =
      (sm->GetVertices()[2] - sm->GetVertices()[1]).Cross(sm->GetVertices()[3] - sm->GetVertices()[1]);
  Vector3D<Precision> n2 =
      (sm->GetVertices()[3] - sm->GetVertices()[2]).Cross(sm->GetVertices()[0] - sm->GetVertices()[2]);
  Vector3D<Precision> n3 =
      (sm->GetVertices()[0] - sm->GetVertices()[3]).Cross(sm->GetVertices()[1] - sm->GetVertices()[3]);

  if (n0.Dot(sm->GetVertices()[3] - sm->GetVertices()[0]) > 0) {
    sm->AddPolygon(3, {1, 0, 2}, true);
  } else {
    sm->AddPolygon(3, {0, 1, 2}, true);
  }

  if (n1.Dot(sm->GetVertices()[0] - sm->GetVertices()[1]) > 0) {
    sm->AddPolygon(3, {2, 1, 3}, true);
  } else {
    sm->AddPolygon(3, {1, 2, 3}, true);
  }
  if (n2.Dot(sm->GetVertices()[1] - sm->GetVertices()[2]) > 0) {
    sm->AddPolygon(3, {3, 2, 0}, true);
  } else {
    sm->AddPolygon(3, {2, 3, 0}, true);
  }
  if (n3.Dot(sm->GetVertices()[2] - sm->GetVertices()[3]) > 0) {
    sm->AddPolygon(3, {0, 3, 1}, true);
  } else {
    sm->AddPolygon(3, {3, 0, 1}, true);
  }

  return sm;
}
#endif

#ifndef VECCORE_CUDA
template <TranslationCode trans_code, RotationCode rot_code>
VPlacedVolume *UnplacedTet::Create(LogicalVolume const *const logical_volume,
                                   Transformation3D const *const transformation, VPlacedVolume *const placement)
{
  if (placement) {
    new (placement) SpecializedTet<trans_code, rot_code>(logical_volume, transformation);
    return placement;
  }
  return new SpecializedTet<trans_code, rot_code>(logical_volume, transformation);
}

VPlacedVolume *UnplacedTet::SpecializedVolume(LogicalVolume const *const volume,
                                              Transformation3D const *const transformation,
                                              const TranslationCode trans_code, const RotationCode rot_code,
                                              VPlacedVolume *const placement) const
{
  return VolumeFactory::CreateByTransformation<UnplacedTet>(volume, transformation, trans_code, rot_code, placement);
}
#else

template <TranslationCode trans_code, RotationCode rot_code>
VECCORE_ATT_DEVICE
VPlacedVolume *UnplacedTet::Create(LogicalVolume const *const logical_volume,
                                   Transformation3D const *const transformation, const int id, const int copy_no,
                                   const int child_id, VPlacedVolume *const placement)
{
  if (placement) {
    new (placement) SpecializedTet<trans_code, rot_code>(logical_volume, transformation, id, copy_no, child_id);
    return placement;
  }
  return new SpecializedTet<trans_code, rot_code>(logical_volume, transformation, id, copy_no, child_id);
}

VECCORE_ATT_DEVICE
VPlacedVolume *UnplacedTet::SpecializedVolume(LogicalVolume const *const volume,
                                              Transformation3D const *const transformation,
                                              const TranslationCode trans_code, const RotationCode rot_code,
                                              const int id, const int copy_no, const int child_id,
                                              VPlacedVolume *const placement) const
{
  return VolumeFactory::CreateByTransformation<UnplacedTet>(volume, transformation, trans_code, rot_code, id, copy_no,
                                                            child_id, placement);
}

#endif

#ifdef VECGEOM_CUDA_INTERFACE

DevicePtr<cuda::VUnplacedVolume> UnplacedTet::CopyToGpu(DevicePtr<cuda::VUnplacedVolume> const in_gpu_ptr) const
{
  // Copy vertices on GPU, then create the object

  Precision *p0 = AllocateOnGpu<Precision>(3 * sizeof(Precision));
  Precision *p1 = AllocateOnGpu<Precision>(3 * sizeof(Precision));
  Precision *p2 = AllocateOnGpu<Precision>(3 * sizeof(Precision));
  Precision *p3 = AllocateOnGpu<Precision>(3 * sizeof(Precision));

  vecgeom::CopyToGpu(&fTet.fVertex[0].x(), p0, 3 * sizeof(Precision));
  vecgeom::CopyToGpu(&fTet.fVertex[1].x(), p1, 3 * sizeof(Precision));
  vecgeom::CopyToGpu(&fTet.fVertex[2].x(), p2, 3 * sizeof(Precision));
  vecgeom::CopyToGpu(&fTet.fVertex[3].x(), p3, 3 * sizeof(Precision));

  DevicePtr<cuda::VUnplacedVolume> gpugentet = CopyToGpuImpl<UnplacedTet>(in_gpu_ptr, p0, p1, p2, p3);
  FreeFromGpu(p0);
  FreeFromGpu(p1);
  FreeFromGpu(p2);
  FreeFromGpu(p3);

  return gpugentet;
}

DevicePtr<cuda::VUnplacedVolume> UnplacedTet::CopyToGpu() const
{
  return CopyToGpuImpl<UnplacedTet>();
}

#endif // VECGEOM_CUDA_INTERFACE

} // namespace VECGEOM_IMPL_NAMESPACE

#ifdef VECCORE_CUDA

namespace cxx {

template size_t DevicePtr<cuda::UnplacedTet>::SizeOf();
template void DevicePtr<cuda::UnplacedTet>::Construct(Precision *, Precision *, Precision *, Precision *) const;

} // namespace cxx

#endif
} // namespace vecgeom