r/GraphicsProgramming • u/Derek9132 • 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
4
u/fgennari Mar 06 '24
Just taking a look at the first file, boundedVolumeNode.cpp, I see in tree building you have a loop "while (j != end)", and then right after the loop you dereference <j>, which is equal to <end>: "if ((*j)->boundingbox().center()[axis] < pivot)". Isn't this one off the end of the vector for the last partition? I think you can just fold those two conditions at the end into the loop.
And it would also help to add some asserts. For example, "assert(start < end)" and "assert(i < end)" in the places where you dereference <i>.
1
u/Derek9132 Mar 06 '24
Thank you so much. Yes, this was the issue, and I'm in the process of adding asserts to fix up some other seg faults.
11
u/fgennari Mar 06 '24
Have you tried creating a debug build and stepping through the debugger to see what line is failing?
10
3
2
u/zCybeRz Mar 06 '24
Segfaults are usually caused by your program accessing memory it shouldn't be. Common causes are indexed buffer overflows and uninitialised object pointers.
There are two easy ways to debug them, for both options you should build with debug info (-g flag in gcc).
A library called address sanitizer. Nowadays compiler toolchains come with this and in gcc it can be enabled with the build flag -fsanitize=address. This adds checks for when your program accesses memory it shouldn't, and will print whenever it happens.
GDB debugger. This should also come with your mingw-gcc. Run it with "gdb <yourapp>", and then from inside gdb "r <yourargs>" to start running it in the debugger. It will stop when the program exits, and you can then run "bt" to get a backtrace of the call stack when it exited (crashed). It will show you the line it crashed on, and you can work out from there which pointers are out of bounds.
2
u/saddung Mar 06 '24
Run it with Visual Studio(not code), it will show you exactly where the seg faults are triggered.
1
u/wen_mars Mar 06 '24
Whenever I get a segfault and it's not immediately obvious why, I compile a debug build and run it in valgrind. Not only does it tell me what line caused the segfault, it often also reveals other problems with my code that may be the root cause of the bug or will cause other bugs in the future.
-4
u/wm_lex_dev Mar 06 '24
boundingbox(void) const
Using void for no-argument functions comes from C, it is not the recommended style for C++.
return intersectionPoint();
The more modern way to write this is return { };, leaving the return type to be inferred by the compiler.
2
u/fgennari Mar 06 '24
That's not really relevant to the problem and is up to the particular style guide you follow. At work we always pass "void" to C++ functions, I assume because it's more readable. And I've never seen that return syntax used before in a project I worked on. Though I can see how it could be useful in some situations.
1
u/wm_lex_dev Mar 06 '24 edited Mar 06 '24
Plenty of real-world projects are using older styles, and it's not the end of the world. But for newer projects there's no reason to write like it's the 80's.
There's no accounting for personal style and taste, but functions with
voidparameters come from an earlier language (C) where functions declared with no parameters could take any number of parameters. The syntax only exists in C++ for legacy support.The change from old-school constructors to braces, a.k.a. aggregate-initialization and list-initialization, started in C++11 well over 10 years ago. There are a number of tangible advantages to the newer syntax, for example if you do not write a constructor then it will just feed the values within the braces directly into the class's fields. And it won't allow for narrowing conversions.
People do still argue for the older syntax when the constructor is more like a complex function and less like moving plain data into fields, but that's not what's happening here anyway.
7
u/waramped Mar 06 '24
Just showing us the code isn't helpful, show us the errors you are getting at runtime. Where in the code is it segfaulting? What's the output until that point? Is it crashing immediately or does it run for a while? Have you stepped through it at all yourself?