-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoundedNode.hpp
323 lines (281 loc) · 16.1 KB
/
BoundedNode.hpp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//
// BoundingBoxNode.hpp
// Curvature
//
// Created by Simon Demeule on 2019-09-28.
// Copyright © 2019 Simon Demeule. All rights reserved.
//
#pragma once
#include <glm/glm.hpp>
#include <vector>
#include "BoundedNode.hpp"
#include "BoundedObjectIntersection.hpp"
#include "BoundedObject.hpp"
#include "Ray.hpp"
#include "DistanceMeasure.hpp"
// bounding box node, part of a BVH tree
template <class Object, class ObjectIntersection>
class BoundedNode : public BoundedObject {
public:
BoundedNode* nodeParent;
BoundedNode* nodeLeft;
BoundedNode* nodeRight;
Object* object;
ObjectIntersection incrementDepth(ObjectIntersection intersection, int objectIncrement, int boxIncrement) {
intersection.objectIntersectionDepth += objectIncrement;
intersection.boxIntersectionDepth += boxIncrement;
return intersection;
}
ObjectIntersection closestIntersection(Ray ray) {
if(object != nullptr) {
// base case, node is leaf, call object intersection function
return object->intersection(ray);
} else {
// recursive case, node is internal
// check intersection with children bounding box first
BoundedObjectIntersection intersectionBoxLeft = nodeLeft->boundingBox.intersection(ray);
BoundedObjectIntersection intersectionBoxRight = nodeRight->boundingBox.intersection(ray);
if(!intersectionBoxLeft.exists && !intersectionBoxRight.exists) {
// none exist
ObjectIntersection intersection;
intersection.exists = false;
return incrementDepth(intersection, 0, 2);
} else if(intersectionBoxLeft.exists && !intersectionBoxRight.exists) {
// left exists
return incrementDepth(nodeLeft->closestIntersection(ray), 0, 2);
} else if(!intersectionBoxLeft.exists && intersectionBoxRight.exists) {
// right exists
return incrementDepth(nodeRight->closestIntersection(ray), 0, 2);
} else {
// both exist
BoundedObjectIntersection intersectionBoxClosest;
BoundedObjectIntersection intersectionBoxFurthest;
BoundedNode* nodeClosest;
BoundedNode* nodeFurthest;
if(intersectionBoxLeft.distance < intersectionBoxRight.distance) {
intersectionBoxClosest = intersectionBoxLeft;
intersectionBoxFurthest = intersectionBoxRight;
nodeClosest = nodeLeft;
nodeFurthest = nodeRight;
} else {
intersectionBoxClosest = intersectionBoxRight;
intersectionBoxFurthest = intersectionBoxLeft;
nodeClosest = nodeRight;
nodeFurthest = nodeLeft;
}
ObjectIntersection intersectionClosest = nodeClosest->closestIntersection(ray);
if(intersectionClosest.exists) {
// the closest intersection exists
if(intersectionClosest.distance < intersectionBoxFurthest.distance && intersectionBoxClosest.distance != intersectionBoxFurthest.distance) {
// the intersection in the closest node happens before the start of the furthest bounding box, meaning it occludes it. we don't need to compute the full intersection within the furthest node
// this also excludes the case where both intersection box distances are equal
return incrementDepth(intersectionClosest, 0, 2);
} else {
// the intersection in the furthest node might be in front of the one in the closest node. we must compute it
ObjectIntersection intersectionFurthest = nodeFurthest->closestIntersection(ray);
if(!intersectionFurthest.exists || intersectionClosest.distance < intersectionFurthest.distance) {
// the furthest intersection doesn't exist, or the closest node has the closest intersection
return incrementDepth(intersectionClosest, intersectionFurthest.objectIntersectionDepth, intersectionFurthest.boxIntersectionDepth + 2);
} else {
// the furthest node has the closest intersection
return incrementDepth(intersectionFurthest, intersectionClosest.objectIntersectionDepth, intersectionClosest.boxIntersectionDepth + 2);;
}
}
} else {
// the closest intersection doesn't exist
ObjectIntersection intersectionFurthest = nodeFurthest->closestIntersection(ray);
if(intersectionFurthest.exists) {
// the furthest exists
return incrementDepth(intersectionFurthest, intersectionClosest.objectIntersectionDepth, intersectionClosest.boxIntersectionDepth + 2);
} else {
// the furthest intersection doesn't exist
ObjectIntersection intersection;
intersection.exists = false;
return incrementDepth(intersection, intersectionClosest.objectIntersectionDepth + intersectionFurthest.objectIntersectionDepth, intersectionClosest.boxIntersectionDepth + intersectionFurthest.boxIntersectionDepth + 2);
}
}
}
}
}
ObjectIntersection closestIntersectionExcluding(Ray ray, Object* objectExcluded) {
if(object != nullptr) {
// base case, node is leaf, call object intersection function
ObjectIntersection intersectionObject = object->intersection(ray);
if(intersectionObject.object != objectExcluded) {
// object is not the excluded one
return intersectionObject;
} else {
// object is the excluded one, cancel
ObjectIntersection intersection;
intersection.exists = false;
return intersection;
}
} else {
// recursive case, node is internal
// check intersection with children bounding box first
BoundedObjectIntersection intersectionBoxLeft = nodeLeft->boundingBox.intersection(ray);
BoundedObjectIntersection intersectionBoxRight = nodeRight->boundingBox.intersection(ray);
if(!intersectionBoxLeft.exists && !intersectionBoxRight.exists) {
// none exist
ObjectIntersection intersection;
intersection.exists = false;
return incrementDepth(intersection, 0, 2);
} else if(intersectionBoxLeft.exists && !intersectionBoxRight.exists) {
// left exists
return incrementDepth(nodeLeft->closestIntersectionExcluding(ray, objectExcluded), 0, 2);
} else if(!intersectionBoxLeft.exists && intersectionBoxRight.exists) {
// right exists
return incrementDepth(nodeRight->closestIntersectionExcluding(ray, objectExcluded), 0, 2);
} else {
// both exist
BoundedObjectIntersection intersectionBoxClosest;
BoundedObjectIntersection intersectionBoxFurthest;
BoundedNode* nodeClosest;
BoundedNode* nodeFurthest;
if(intersectionBoxLeft.distance < intersectionBoxRight.distance) {
intersectionBoxClosest = intersectionBoxLeft;
intersectionBoxFurthest = intersectionBoxRight;
nodeClosest = nodeLeft;
nodeFurthest = nodeRight;
} else {
intersectionBoxClosest = intersectionBoxRight;
intersectionBoxFurthest = intersectionBoxLeft;
nodeClosest = nodeRight;
nodeFurthest = nodeLeft;
}
ObjectIntersection intersectionClosest = nodeClosest->closestIntersectionExcluding(ray, objectExcluded);
if(intersectionClosest.exists) {
// the closest intersection exists
if(intersectionClosest.distance < intersectionBoxFurthest.distance && intersectionBoxClosest.distance != intersectionBoxFurthest.distance) {
// the intersection in the closest node happens before the start of the furthest bounding box, meaning it occludes it. we don't need to compute the full intersection within the furthest node
// this also excludes the case where both intersection box distances are equal
return incrementDepth(intersectionClosest, 0, 2);
} else {
// the intersection in the furthest node might be in front of the one in the closest node. we must compute it
ObjectIntersection intersectionFurthest = nodeFurthest->closestIntersectionExcluding(ray, objectExcluded);
if(!intersectionFurthest.exists || intersectionClosest.distance < intersectionFurthest.distance) {
// the furthest intersection doesn't exist, or the closest node has the closest intersection
return incrementDepth(intersectionClosest, intersectionFurthest.objectIntersectionDepth, intersectionFurthest.boxIntersectionDepth + 2);
} else {
// the furthest node has the closest intersection
return incrementDepth(intersectionFurthest, intersectionClosest.objectIntersectionDepth, intersectionClosest.boxIntersectionDepth + 2);;
}
}
} else {
// the closest intersection doesn't exist
ObjectIntersection intersectionFurthest = nodeFurthest->closestIntersectionExcluding(ray, objectExcluded);
if(intersectionFurthest.exists) {
// the furthest exists
return incrementDepth(intersectionFurthest, intersectionClosest.objectIntersectionDepth, intersectionClosest.boxIntersectionDepth + 2);
} else {
// the furthest intersection doesn't exist
ObjectIntersection intersection;
intersection.exists = false;
return incrementDepth(intersection, intersectionClosest.objectIntersectionDepth + intersectionFurthest.objectIntersectionDepth, intersectionClosest.boxIntersectionDepth + intersectionFurthest.boxIntersectionDepth + 2);
}
}
}
}
}
DistanceMeasure incrementDepth(DistanceMeasure distance, int objectIncrement, int boxIncrement) {
distance.objectDistanceDepth += objectIncrement;
distance.boxDistanceDepth += boxIncrement;
return distance;
}
DistanceMeasure distance(glm::vec3 point) {
if(object != nullptr) {
// base case, node is leaf, call object distance function
DistanceMeasure result = object->distance(point);
return result;
} else {
// recursive case, node is internal
DistanceMeasure distanceLeftBox = nodeLeft->boundingBox.distance(point);
DistanceMeasure distanceRightBox = nodeRight->boundingBox.distance(point);
DistanceMeasure distanceClosestBox;
DistanceMeasure distanceFurthestBox;
BoundedNode* nodeClosest;
BoundedNode* nodeFurthest;
if(distanceLeftBox.distance < distanceRightBox.distance) {
distanceClosestBox = distanceLeftBox;
distanceFurthestBox = distanceRightBox;
nodeClosest = nodeLeft;
nodeFurthest = nodeRight;
} else {
distanceClosestBox = distanceRightBox;
distanceFurthestBox = distanceLeftBox;
nodeClosest = nodeRight;
nodeFurthest = nodeLeft;
}
DistanceMeasure distanceClosest = nodeClosest->distance(point);
if(distanceClosest.distance < distanceFurthestBox.distance) {
// distance to object in closest node is closer than the start of the other node's bounding box. no need to recur on the furthest node.
return incrementDepth(distanceClosest, 0, 2);
} else {
// we can't optimize here, must compute both full distances
DistanceMeasure distanceFurthest = nodeFurthest->distance(point);
if(distanceClosest.distance < distanceFurthest.distance) {
return incrementDepth(distanceClosest, distanceFurthest.objectDistanceDepth, distanceFurthest.boxDistanceDepth + 2);
} else {
return incrementDepth(distanceFurthest, distanceClosest.objectDistanceDepth, distanceClosest.boxDistanceDepth + 2);
}
}
}
}
// this uses lists instead of vectors to speedup merging recursive branches
std::list<Object*> encompassingObjects(glm::vec3 point) {
if(object != nullptr) {
// base case, node is leaf, return object
std::list<Object*> list = {object};
return list;
} else {
// recursive case, node is internal
bool encompassingLeft = nodeLeft->boundingBox.containmentTest(point);
bool encompassingRight = nodeRight->boundingBox.containmentTest(point);
if(!encompassingLeft && !encompassingRight) {
// neither encompasses
std::list<Object*> list = {};
return list;
} else if(encompassingLeft && !encompassingRight) {
// left encompasses
return nodeLeft->encompassingObjects(point);
} else if(!encompassingLeft && encompassingRight) {
// right encompasses
return nodeRight->encompassingObjects(point);
} else {
// both encompass
std::list<Object*> listLeft = nodeLeft->encompassingObjects(point);
std::list<Object*> listRight = nodeRight->encompassingObjects(point);
// splice lists into left
listLeft.splice(listLeft.end(), listRight);
return listLeft;
}
}
}
// this uses lists instead of vectors to speedup merging recursive branches
void encompassingObjects(glm::vec3 point, std::list<Object*> &objects) {
if(object != nullptr) {
// base case, node is leaf, return object
objects.push_back(object);
return;
} else {
// recursive case, node is internal
bool encompassingLeft = nodeLeft->boundingBox.containmentTest(point);
bool encompassingRight = nodeRight->boundingBox.containmentTest(point);
if(!encompassingLeft && !encompassingRight) {
// neither encompasses
return;
} else if(encompassingLeft && !encompassingRight) {
// left encompasses
return nodeLeft->encompassingObjects(point, objects);
} else if(!encompassingLeft && encompassingRight) {
// right encompasses
return nodeRight->encompassingObjects(point, objects);
} else {
// both encompass
nodeLeft->encompassingObjects(point, objects);
nodeRight->encompassingObjects(point, objects);
return;
}
}
}
};