Helios++
Helios software for LiDAR simulations
RayIntersectionTest.h
1 #pragma once
2 
3 #include <BaseTest.h>
4 #include <DetailedVoxel.h>
5 #include <maths/RayUtils.h>
6 
7 namespace HeliosTests{
8 
15 public:
16  // *** CONSTRUCTOR *** //
17  // ********************* //
21  RayIntersectionTest() : BaseTest("Ray intersection test"){}
22 
23  // *** R U N *** //
24  // *************** //
28  bool run() override;
29 
30 protected:
31  // *** U T I L S *** //
32  // ******************* //
39  bool checkIntersection(std::vector<double> it){
40  if(it.empty()) return false;
41  if(it[0] < 0.0 && it[1] < 0.0) return false;
42  return true;
43  }
44 };
45 
47  // Create voxels
48  DetailedVoxel dv1(
49  6, 6, 6, 2,
50  std::vector<int>({0, 49}),
51  std::vector<double>({
52  0, 89.7859174, 0.0222306, 0, 0.1843913,
53  0.1244739, 0.0644808, 3.1595603, 1, 0,
54  0
55  })
56  );
57  dv1.part = std::make_shared<ScenePart>();
58  dv1.part->onRayIntersectionMode = "TRANSMITTIVE";
59  DetailedVoxel dv2(
60  6, 6, -2, 2,
61  std::vector<int>({0, 120}),
62  std::vector<double>({
63  0, 89.9096456, 0.061645, 0, 0.1772976,
64  0.3734215, 0.124348, 14.9217589, 1, 0,
65  0
66  })
67  );
68  dv2.part = std::make_shared<ScenePart>();
69  dv2.part->onRayIntersectionMode = "TRANSMITTIVE";
70 
71 
72  // Create rays (o := originWaypoint , v := normalized director vector)
73  glm::dvec3 o1(6, 6, 9); // Ray 1 originWaypoint
74  glm::dvec3 v1(0, 0, -1); // Ray 1 direction
75  glm::dvec3 o2(-6, 6, 6); // Ray 2 originWaypoint
76  glm::dvec3 v2(1, 0, 0); // Ray 2 direction
77  glm::dvec3 o3(-6, 4, -5.5); // Ray 3 originWaypoint
78  glm::dvec3 v3(0.968, 0.061, 0.242); // Ray 3 direction
79  glm::dvec3 o4(2, 4, -10); // Ray 4 originWaypoint
80  glm::dvec3 v4(0.371, 0.019, 0.928); // Ray 4 direction
81  glm::dvec3 o5(9, 4, -10); // Ray 5 originWaypoint
82  glm::dvec3 v5(0.707, 0.035, 0.707); // Ray 5 direction
83  glm::dvec3 o6(6, 6, 9); // Ray 6 originWaypoint
84  glm::dvec3 v6(0, 0, 1); // Ray 6 direction
85  glm::dvec3 o7(7, 6, 3); // Ray 7 originWaypoint
86  glm::dvec3 v7(0.40824829, 0.81649658, 0.40824829); // Ray 7 direction
87 
88 
89  // Validate simple intersections are as expected
90  if(!checkIntersection(dv1.getRayIntersection(o1, v1)))
91  return false; // WRONG: r1 must hit dv1 but didnt
92  if(!checkIntersection(dv1.getRayIntersection(o2, v2)))
93  return false; // WRONG: r2 must hit dv1 but didnt
94  if(checkIntersection(dv1.getRayIntersection(o3, v3)))
95  return false; // WRONG: r3 must NOT hit dv1 but did
96  if(!checkIntersection(dv1.getRayIntersection(o4, v4)))
97  return false; // WRONG: r4 must hit dv1 but didnt
98  if(checkIntersection(dv1.getRayIntersection(o5, v5)))
99  return false; // WRONG: r5 must NOT hit dv1 but did
100  if(checkIntersection(dv1.getRayIntersection(o6, v6)))
101  return false; // WRONG: r6 must NOT hit dv1 but did
102  if(!checkIntersection(dv1.getRayIntersection(o7 ,v7)))
103  return false; // WRONG: r7 must hit dv1 bud didnt
104  if(!checkIntersection(dv2.getRayIntersection(o1, v1)))
105  return false; // WRONG: r1 must hit dv2 but didnt
106  if(checkIntersection(dv2.getRayIntersection(o2, v2)))
107  return false; // WRONG: r2 must NOT hit dv2 but did
108  if(!checkIntersection(dv2.getRayIntersection(o3, v3)))
109  return false; // WRONG: r3 must hit dv2 but didnt
110  if(!checkIntersection(dv2.getRayIntersection(o4, v4)))
111  return false; // WRONG: r4 must hit dv2 but didnt
112  if(checkIntersection(dv2.getRayIntersection(o5, v5)))
113  return false; // WRONG: r5 must NOT hit dv2 but did
114  if(checkIntersection(dv2.getRayIntersection(o6, v6)))
115  return false; // WRONG: r6 must NOT hit dv2 but did
116  if(checkIntersection(dv2.getRayIntersection(o7, v7)))
117  return false; // WRONG: r7 must NOT hit dv2 but did
118 
119 
120  // Prepare semitransparent voxel test
121  UniformNoiseSource<double> uns("1", 0.0, 1.0);
127  glm::dvec3 so(0, 0, 0); // Sub ray originWaypoint
128 
129 
130  // Semitransparent voxel test for ray1
131  so = o1; // Subray originWaypoint
132  std::vector<double> it = dv1.getRayIntersection(so, v1);
133  glm::dvec3 iip(so+it[0]*v1); // Inside Intersection Point
134  glm::dvec3 oip = RayUtils::obtainPointAfterTraversing(
135  *dv1.getAABB(), so, v1, 0.0); // Outside Intersection Point
136  double ints = 0.0; // Intensity (i) not needed for testing, only length (l)
138  dv1.onRayIntersection(uns, v1, iip, oip, ints);
139  if(!ihr.canRayContinue()) return false; // Ray must be able to continue
140 
141  so = oip + 0.00001 * v1;
142  it = dv2.getRayIntersection(so, v1);
143  iip = glm::dvec3(so+it[0]*v1);
144  oip = RayUtils::obtainPointAfterTraversing(*dv2.getAABB(), so, v1, 0.0);
145  ihr = dv2.onRayIntersection(uns, v1, iip, oip, ints);
146  if(ihr.canRayContinue()) return true; // Ray must be able to continue
147 
148  // Semitransparent voxel test for ray2
149  so = o2;
150  it = dv1.getRayIntersection(so, v2);
151  iip = glm::dvec3(so+it[0]*v2);
152  oip = RayUtils::obtainPointAfterTraversing(*dv1.getAABB(), so, v2, 0.0);
153  ihr = dv1.onRayIntersection(uns, v2, iip, oip, ints);
154  if(!ihr.canRayContinue()) return false; // Ray must be able to continue
155 
156  // Semitransparent voxel test for ray3
157  so = o3;
158  it = dv2.getRayIntersection(so, v3);
159  iip = glm::dvec3(so+it[0]*v3);
160  oip = RayUtils::obtainPointAfterTraversing(*dv2.getAABB(), so, v3, 0.0);
161  ihr = dv2.onRayIntersection(uns, v3, iip, oip, ints);
162  if(ihr.canRayContinue()) return false; // Ray must NOT be able to continue
163 
164  // Semitransparent voxel test for ray4
165  so = o4;
166  it = dv2.getRayIntersection(so, v4);
167  iip = glm::dvec3(so+it[0]*v4);
168  oip = RayUtils::obtainPointAfterTraversing(*dv2.getAABB(), so, v4, 0.0);
169  ihr = dv2.onRayIntersection(uns, v4, iip, oip, ints);
170  if(ihr.canRayContinue()) return false; // Ray must NOT be able to continue
171 
172  so = oip + 0.00001 * v4;
173  it = dv1.getRayIntersection(so, v4);
174  iip = glm::dvec3(so+it[0]*v4);
175  oip = RayUtils::obtainPointAfterTraversing(*dv1.getAABB(), so, v4, 0.0);
176  ihr = dv1.onRayIntersection(uns, v4, iip, oip, ints);
177  if(!ihr.canRayContinue()) return false; // Ray must be able to continue
178 
179  // Semitransparent voxel test for ray7
180  so = o7;
181  it = dv1.getRayIntersection(so, v7);
182  iip = glm::dvec3(so+it[0]*v7);
183  oip = RayUtils::obtainPointAfterTraversing(*dv1.getAABB(), so, v7, 0.0);
184  ihr = dv1.onRayIntersection(uns, v7, iip, oip, ints);
185  if(!ihr.canRayContinue()) return false; // Ray must be able to continue
186 
187 
188 
189  // Successfully reached end of test
190  return true;
191 }
192 
193 }
Test for ray intersection and corresponding handling (if any)
Definition: RayIntersectionTest.h:14
RayIntersectionTest()
Ray intersection test constructor.
Definition: RayIntersectionTest.h:21
bool checkIntersection(std::vector< double > it)
Check if the intersection returned by the getIntersection method is valid or not. ...
Definition: RayIntersectionTest.h:39
IntersectionHandlingResult onRayIntersection(NoiseSource< double > &uniformNoiseSource, glm::dvec3 &rayDirection, glm::dvec3 const &insideIntersectionPoint, glm::dvec3 const &outsideIntersectionPoint, double rayIntensity) override
Define ray intersection handling for DetailedVoxel primitive.
Definition: DetailedVoxel.cpp:25
bool canRayContinue()
Check whether the ray can continue after intersection or not.
Definition: IntersectionHandlingResult.h:47
static glm::dvec3 obtainPointAfterTraversing(AABB const &aabb, glm::dvec3 const &origin, glm::dvec3 const &direction, double eps=0.00001)
Obtain the point immediately after finishing traversing given bounding box, with an offset specified ...
Definition: RayUtils.cpp:5
BaseTest class.
Definition: BaseTest.h:19
Class which extends Voxel to support AMAPVox format with extra features.
Definition: DetailedVoxel.h:12
Output class for intersection handling methods.
Definition: IntersectionHandlingResult.h:13
AABB * getAABB() override
Definition: Voxel.cpp:37
bool run() override
Definition: RayIntersectionTest.h:46
std::vector< double > getRayIntersection(const glm::dvec3 &rayOrigin, const glm::dvec3 &rayDir) override
Definition: Voxel.cpp:107
Definition: BaseTest.h:6
std::shared_ptr< ScenePart > part
Shared pointer to the scene part the primitive belongs to.
Definition: Primitive.h:37