r/GraphicsProgramming • • Mar 06 '24

Bounded Volume Hierarchy Implementation in C++: Segmentation errors

Hello all,

I'm to implement a bounded volume hierarchy in C++ for a computer graphics class. This includes the constructor, which runs in O(N log N) time complexity and a method for intersecting the hierarchy which has to run in O(log N). Both are recursive. I'm honestly pretty new to C++ and a slow learner, so it took me a while to just get the code compiled. Now that I have, all my tests are throwing segmentation faults and I have no idea what's causing it.

All the code that I wrote is in the boundedVolumeNode.cpp. That's where the constructor and intersect method are. The constructor takes 2 iterators, start and end, which point to a vector of shared pointers to bounded primitives. The start iterator points to the start of the vector, and the end iterator points to the element after the last element of the vector. Because of this, the constructor's end parameter is exclusive while the start parameter is inclusive.

The steps I took are as follows: First I initialize the node's bounding box by iterating through the bounded primitives and adding each one's bounding box to the node's bounding box. Then, if there are more than 2 primitives in the vector (number of shapes in the box) I partition the bounded primitives using a pivot that is the bounding box's center along a random axis. After that, I use a partitioning algorithm similar to the one used in quicksort for O(N log N) complexity (key difference is that the pivot isn't necessarily in the vector). I made sure the i iterator is always pointing to the last element that is less than the pivot after partitioning. I pass each partition to _left and _right to construct new nodes. _left's constructor takes (start, std::next(i)) which has elements less than the pivot, while _right's constructor takes (std::next(i), end) which has elements greater than or equal to the pivot. If there are 2 or fewer primitives in the vector, then those primitives become leaf nodes and the recursion can stop.

The intersect method is much simpler: If the ray doesn't hit the bounding box, return an empty intersection point. Otherwise, recursively call the intersect method on _left and _right and return the nearest intersection point. Note that intersect() is an abstract method in the boundedPrimitive class and boundedVolumeNode inherits from boundedPrimitive along with other bounded primitives like bounded triangles, so an intersection point will be returned if _left or _right is a bounded primitive other than boundedVolumeNode.

Apologies for such a big post. I'd be so grateful for any advice on how to fix the segmentation faults. I'd also really appreciate any advice in general; like whether or not the code will work correctly even after the segmentation faults are fixed. My professor canceled office hours today and it would probably take me days, if not weeks, to learn how to debug segmentation faults, so my options for help are limited. Thank you all!

Here is the code:

boundedVolumeNode.cpp: https://pastebin.com/aYZpZRkv

boundedVolumeNode.h: https://pastebin.com/BtSgGqgn

boundedPrimitive.cpp: https://pastebin.com/k9hn8Sip

boundedPrimitive.h: https://pastebin.com/2md2aGDU

boundingBox.cpp: https://pastebin.com/LX3R5A4n

boundingBox.h: https://pastebin.com/xv5Q87xx

3 Upvotes

15 comments sorted by

View all comments

2

u/saddung Mar 06 '24

Run it with Visual Studio(not code), it will show you exactly where the seg faults are triggered.