alecjacobson / computer-graphics-bounding-volume-hierarchy

Computer Graphics Assignment about Bounding Volume Hierarchies
123 stars 45 forks source link

I might have found a bug in your code ... ? #52

Closed Mohan-Zhang-u closed 4 years ago

Mohan-Zhang-u commented 5 years ago

in the file intersections.cpp, the functions triangle_tree(VA,FA) and triangle_tree(VB,FB) seems to change FA and FB implicitly.

if you change the file intersections.cpp from

73 std::shared_ptr rootA = triangle_tree(VA,FA); 74 std::shared_ptr rootB = triangle_tree(VB,FB);

to

73 std::cout << "the following six rows are fine." << std::endl; std::cout << VA.row(FA(8218,0)) << std::endl; std::cout << VA.row(FA(8218,1)) << std::endl; std::cout << VA.row(FA(8218,2)) << std::endl; std::cout << VB.row(FB(1305,0)) << std::endl; std::cout << VB.row(FB(1305,1)) << std::endl; std::cout << VB.row(FB(1305,2)) << std::endl; std::shared_ptr rootA = triangle_tree(VA,FA); std::shared_ptr rootB = triangle_tree(VB,FB); std::cout << "now black magic starts." << std::endl; std::cout << VA.row(FA(8218,0)) << std::endl; 84 std::cout << VA.row(FA(8218,1)) << std::endl; std::cout << VA.row(FA(8218,2)) << std::endl; std::cout << VB.row(FB(1305,0)) << std::endl; std::cout << VB.row(FB(1305,1)) << std::endl; 88 std::cout << VB.row(FB(1305,2)) << std::endl;

you will probably find a segmentation fault when executing ./intersections ../data/knight.obj ../data/cheburashka.obj till line 84.

Please correct me if I'm wrong! Thanks a lot for your time! intersections.cpp.txt

abhimadan commented 5 years ago

I can't quite reproduce the problem you saw, but there's another problem with your code. When I run it in release mode, I actually get a segfault at line 76 (i.e., after the first two print statements). This actually makes sense: notice that you get the following output when you run ./intersections with the default arguments (which use the knight and cheburashka meshes):

  |VA| 2002
  |FA| 4000

  |VB| 6669
  |FB| 13334

This means that FA doesn't have anything at index 8218, and it only seems to work for the first two vertices of the face because it's reading other allocated memory.

Sure enough, when I run it in debug mode (after moving the tree construction and these print statements to be before the brute force intersection section), I get this assertion failure (which was removed in release mode) when trying to print the first vertex:

Assertion failed: (row >= 0 && row < rows() && col >= 0 && col < cols()), function operator()

Are you sure all three vertices and the black magic statement were printed before your segfault?