File: Rectangles.h

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 (221 lines) | stat: -rw-r--r-- 6,053 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
/// \file Rectangles.h
/// \author Johannes de Fine Licht (johannes.definelicht@cern.ch)

#ifndef VECGEOM_VOLUMES_RECTANGLES_H_
#define VECGEOM_VOLUMES_RECTANGLES_H_

#include "VecGeom/base/Global.h"

#include "VecGeom/base/AlignedBase.h"
#include "VecGeom/base/SOA3D.h"
#include "VecGeom/base/Vector3D.h"
#include "VecGeom/volumes/Planes.h"

namespace vecgeom {

VECGEOM_DEVICE_FORWARD_DECLARE(class Rectangles;);
VECGEOM_DEVICE_DECLARE_CONV(class, Rectangles);

inline namespace VECGEOM_IMPL_NAMESPACE {

/// \class Rectangles
///
/// \brief Stores a number of rectangles in SOA form to allow vectorized
///        operations.
///
/// To allow efficient computation, two corners, one normalized side and
/// the plane equation in which the rectangle lies is stored.
/// If the set of rectangles are assumed to be convex, the convex methods
/// can be called for faster computation, falling back on the implementations
/// for planes.
class Rectangles : public AlignedBase {

  //   fCorners[0]
  //             p0----------
  //       |      |         |
  // (Normalized) |         |
  //    fSides    |   -o-   |
  //       |      |         |
  //       v      |         |
  //             p1---------p2
  //                          fCorners[1]

private:
  Planes fPlanes;
  SOA3D<Precision> fSides;
  SOA3D<Precision> fCorners[2];

public:
  typedef SOA3D<Precision> Corners_t[2];

  VECCORE_ATT_HOST_DEVICE
  Rectangles(int size);

  VECCORE_ATT_HOST_DEVICE
  ~Rectangles();

  VECCORE_ATT_HOST_DEVICE
  VECGEOM_FORCE_INLINE
  int size() const;

  VECCORE_ATT_HOST_DEVICE
  VECGEOM_FORCE_INLINE
  Vector3D<Precision> GetNormal(int i) const;

  VECCORE_ATT_HOST_DEVICE
  VECGEOM_FORCE_INLINE
  SOA3D<Precision> const &GetNormals() const;

  VECCORE_ATT_HOST_DEVICE
  VECGEOM_FORCE_INLINE
  Precision GetDistance(int i) const;

  VECCORE_ATT_HOST_DEVICE
  VECGEOM_FORCE_INLINE
  Array<Precision> const &GetDistances() const;

  VECCORE_ATT_HOST_DEVICE
  inline Vector3D<Precision> GetCenter(int i) const;

  VECCORE_ATT_HOST_DEVICE
  inline Vector3D<Precision> GetCorner(int i, int j) const;

  VECCORE_ATT_HOST_DEVICE
  VECGEOM_FORCE_INLINE
  Corners_t const &GetCorners() const;

  VECCORE_ATT_HOST_DEVICE
  inline Vector3D<Precision> GetSide(int i) const;

  VECCORE_ATT_HOST_DEVICE
  VECGEOM_FORCE_INLINE
  SOA3D<Precision> const &GetSides() const;

  VECCORE_ATT_HOST_DEVICE
  void Set(int index, Vector3D<Precision> const &p0, Vector3D<Precision> const &p1, Vector3D<Precision> const &p2);

  VECCORE_ATT_HOST_DEVICE
  inline Precision Distance(Vector3D<Precision> const &point, Vector3D<Precision> const &direction) const;
};

VECCORE_ATT_HOST_DEVICE
int Rectangles::size() const
{
  return fPlanes.size();
}

VECCORE_ATT_HOST_DEVICE
Vector3D<Precision> Rectangles::GetNormal(int i) const
{
  return fPlanes.GetNormal(i);
}

VECCORE_ATT_HOST_DEVICE
SOA3D<Precision> const &Rectangles::GetNormals() const
{
  return fPlanes.GetNormals();
}

VECCORE_ATT_HOST_DEVICE
Precision Rectangles::GetDistance(int i) const
{
  return fPlanes.GetDistance(i);
}

VECCORE_ATT_HOST_DEVICE
Array<Precision> const &Rectangles::GetDistances() const
{
  return fPlanes.GetDistances();
}

VECCORE_ATT_HOST_DEVICE
Vector3D<Precision> Rectangles::GetCenter(int i) const
{
  return -GetDistance(i) * GetNormal(i);
}

VECCORE_ATT_HOST_DEVICE
Vector3D<Precision> Rectangles::GetCorner(int i, int j) const
{
  return Vector3D<Precision>(fCorners[i][0][j], fCorners[i][1][j], fCorners[i][2][j]);
}

VECCORE_ATT_HOST_DEVICE
Rectangles::Corners_t const &Rectangles::GetCorners() const
{
  return fCorners;
}

VECCORE_ATT_HOST_DEVICE
Vector3D<Precision> Rectangles::GetSide(int i) const
{
  return Vector3D<Precision>(fSides[0][i], fSides[1][i], fSides[2][i]);
}

VECCORE_ATT_HOST_DEVICE
SOA3D<Precision> const &Rectangles::GetSides() const
{
  return fSides;
}

VECCORE_ATT_HOST_DEVICE
void Rectangles::Set(int index, Vector3D<Precision> const &p0, Vector3D<Precision> const &p1,
                     Vector3D<Precision> const &p2)
{

  // Store corners and sides
  fCorners[0].set(index, p0);
  fCorners[1].set(index, p2);
  Vector3D<Precision> side = p1 - p0;
  side.Normalize();
  fSides.set(index, side);

  // Compute plane equation to retrieve normal and distance to origin
  // ax + by + cz + d = 0
  Precision a, b, c, d;
  a = p0[1] * (p1[2] - p2[2]) + p1[1] * (p2[2] - p0[2]) + p2[1] * (p0[2] - p1[2]);
  b = p0[2] * (p1[0] - p2[0]) + p1[2] * (p2[0] - p0[0]) + p2[2] * (p0[0] - p1[0]);
  c = p0[0] * (p1[1] - p2[1]) + p1[0] * (p2[1] - p0[1]) + p2[0] * (p0[1] - p1[1]);
  d = -p0[0] * (p1[1] * p2[2] - p2[1] * p1[2]) - p1[0] * (p2[1] * p0[2] - p0[1] * p2[2]) -
      p2[0] * (p0[1] * p1[2] - p1[1] * p0[2]);
  Vector3D<Precision> normal(a, b, c);
  // Normalize the plane equation
  // (ax + by + cz + d) / sqrt(a^2 + b^2 + c^2) = 0 =>
  // n0*x + n1*x + n2*x + p = 0
  Precision inverseLength = 1. / normal.Length();
  normal *= inverseLength;
  d *= inverseLength;
  if (d >= 0) {
    // Ensure normal is pointing away from origin
    normal = -normal;
    d      = -d;
  }

  fPlanes.Set(index, normal, d);
}

VECCORE_ATT_HOST_DEVICE
Precision Rectangles::Distance(Vector3D<Precision> const &point, Vector3D<Precision> const &direction) const
{
  Precision bestDistance = kInfLength;
  for (int i = 0, iMax = size(); i < iMax; ++i) {
    Vector3D<Precision> normal       = GetNormal(i);
    Vector3D<Precision> side         = GetSide(i);
    Precision t                      = -(normal.Dot(point) + GetDistance(i)) / normal.Dot(direction);
    Vector3D<Precision> intersection = point + t * direction;
    Vector3D<Precision> fromP0       = intersection - fCorners[0][i];
    Vector3D<Precision> fromP2       = intersection - fCorners[1][i];
    if (t >= 0 && t < bestDistance && side.Dot(fromP0) >= 0 && (-side).Dot(fromP2) >= 0) {
      bestDistance = t;
    }
  }
  return bestDistance;
}

std::ostream &operator<<(std::ostream &os, Rectangles const &rhs);

} // End inline impl namespace

} // End global namespace

#endif // VECGEOM_VOLUMES_RECTANGLES_H_