dilevin / computer-graphics-bounding-volume-hierarchy

Computer Graphics Assignment about Bounding Volume Hierarchies
6 stars 5 forks source link

Clarification on "descendant" pointer in Object ray_intersect #52

Open NPTP opened 4 years ago

NPTP commented 4 years ago

In the context of AABBTree::ray_intersect().

We have the "descendant" object pointer. From what I understand we want this to point to the descendant of the AABBTree which the ray has hit, by passing it up recursive calls through the tree. The only possible descendants of AABBTrees are child AABBTrees, triangles and cloudpoints. Since AABBTrees can only represent bounding boxes, i.e., empty space, it makes no sense to have "descendant" point to them. So we are only concerned with pointing to primitives, should there be a hit.

However, these two primitives' implementation of ray_intersect() do not use the "descendant" pointer in any way. So, are we meant to check in AABBTree::ray_intersect(), say by using casting, if the children are more AABBTrees or if they are primitives, and use that information to set "descendant", rather than having any Object point to itself as the "descendant"?

Thanks!

fnnbrr commented 4 years ago

[not a TA/instructor, take this with a grain of salt]

Yes, you can "test" by casting as seen in this example from the handout:

    std::shared_ptr<AABBTree> aabb = std::dynamic_pointer_cast<AABBTree>(obj);
    // Test whether cast succeed
    if(aabb)
    {
      // Hooray. We can do AABBTree-specific operations on `aabb` now.
      std::cout<<"This object is an AABBTree."<<std::endl;
    }else
    {
      // Hooray. Now we know `obj` does _not_ point to an AABBTree. Hint, hint.
      std::cout<<"This object is not an AABBTree."<<std::endl;
    }

Another approach: If you're getting a SEGFAULT because of trying to use a null pointer, is there a way you can check for null pointers and correctly assign the pointer accordingly?