Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 696127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:01:52+00:00 2026-05-14T03:01:52+00:00

I have the following recursive code and it doesn’t behave as expected (see details

  • 0

I have the following recursive code and it doesn’t behave as expected (see details below):

R3Intersection ComputeIntersectionNode(R3Ray *ray, R3Node *node)
{
  R3Intersection closest_inter;
  R3Intersection child_inter;
  R3Intersection shape_inter;

  double least_t = DBL_MAX;

  // check for intersection with shape
  if(node->shape != NULL)
  {
    shape_inter = ComputeIntersectionShape(ray, node->shape);
    if(shape_inter.hit == 1)
      closest_inter = shape_inter;
  }

  // go through all the children and for each child, compute
  // the closest intersection with ray
  for(int i = 0; i < node->children.size(); i++)
  {
    // compute intersection with children[i] and ray
    child_inter = ComputeIntersectionNode(ray, node->children[i]);

    // if there's an intersection with the child node and
    // it is the closest intersection, set closest intersection
    if(child_inter.hit == 1 && fabs(child_inter.t) < fabs(least_t))
      closest_inter = child_inter;
  }

  return closest_inter;
}

This ComputeIntersectionNode(...), in addition to the recursive calls, is also being called for multiple rays in the program. To test this function, I run it for 4 rays and 4 nodes (or more precisely, one root of type node, which doesn’t have a shape, but has 4 children, each of which have one shape). For testing, each ray intersects exactly one node/shape.

When I run the code in GDB for the first ray, it first passes the root through the code, which doesn’t have a shape, so then it goes directly to the children. It computes the first child’s intersection correctly and sets the closest_inter variable correctly as well, which gets returned to the highest level of recursion and child_inter as well as closest_inter are set here with child_inter.hit = 1;

Then, the second child is processed. ComputeIntersectionShape(...) returns no intersection with the second child (shape_inter.hit == 0😉 – this is expected behavior. However, when the function returns to the highest level of recursion, for some reason child_inter.hit is set to 1 (but should be set to 0).

Any suggestions?

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:01:52+00:00Added an answer on May 14, 2026 at 3:01 am

    I think your problem is caused by the fact you are returning a default-initialized R3Intersection (ie. you don’t return the shape_inter when it doesn’t intersect). Depending on its default constructor, you might get what you see.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following recursive code and i am getting a stackoverflow exception. I
I have the following code to recursively search for files through a directory, which
I have following fiddle: http://jsfiddle.net/BFSH4/ As you see there are two issues: The h1
I have the following code snippet from my PowerShell script that... Loops through a
I have 2 related Linq to SQL questions. Please see the image below to
Suppose I have following code def foo(x:Int):Unit = { if (x == 1) println
I have the following code snippet. I'm trying to list all the files in
Have a look at the following code: class A(object): defaults = {'a': 1} def
I have the following Clojure code to calculate a number with a certain factorable
I have the following code which recursively operates on each element within a List

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.