Sign in to follow this  
daniel_i_l

Recursion

Recommended Posts

I have a BSP tree and want to get the min and max heights (of the heightmap) of each area. The BSP class looks like this:
struct BSP{
 float height, width, depth;
 float centerX, centerY, centerZ;
 MinMax limits;
 BSP *p_sec1, *p_sec2, *p_sec3, *p_sec4;
 bool found_limits;
 bool is_leaf;
};

class CBspTree{
 public: 
   CBspTree();
  ~CBspTree();
   int number_of_parts;
   void GetHeightMap(BYTE *pH){p_HeightMap = pH;}
   void GetBoxes(){map->limits = FindLimits(map);}
   void RenderMap();
   VERTEX PickedCenter;
   void Picking(CVector3 line[]);
 private:
   BSP* map;
   BYTE* p_HeightMap;
   bool color;
   void InitTree(BSP* node, float x, float y, float h, float w);
   void EraseBSP(BSP* node);
   void Render(BSP* node);
   void PickCenter(BSP* node);
   CVector3 v_Line[2];
   MinMax FindLimits(BSP* node);
   MinMax FindLimitsOfArea(float blX, float blY, float trX, float trY);
};

And the code to implement the height finding looks like this:
MinMax CBspTree :: FindLimitsOfArea(float blX, float trX, float blY, float trY)
{
  MinMax limit;
  float y;
  
  limit.min = 10000;  limit.max = -10000;
  for(int i = blX; i <= trX; i+=STEP_SIZE)
   {
     for(int j = blY; j <= trY; j+=STEP_SIZE)
      {
        y = (float)(SCALE_HEIGHT*Height(p_HeightMap, (float)i, (float)j ));  
        if(y>limit.max)
         limit.max = y;
        if(y<limit.min)
         limit.min = y;
         }
         
   }
  //limit.max=500;
  return(limit);
}



MinMax CBspTree :: FindLimits(BSP* node)
{
   if(!node->is_leaf)
    {
      node->limits.min = Smallest(FindLimits(node->p_sec1).min,
                                  FindLimits(node->p_sec2).min,
                                  FindLimits(node->p_sec3).min,
                                  FindLimits(node->p_sec4).min);
      
      node->limits.max =  Biggest(FindLimits(node->p_sec1).max,
                                  FindLimits(node->p_sec2).max,
                                  FindLimits(node->p_sec3).max,
                                  FindLimits(node->p_sec4).max);
      
      node->centerY = (node->limits.min + node->limits.max)/2;
      node->height = node->limits.max - node->limits.min;
      node->found_limits = true;
      return(node->limits);
    }
   else
    {
      //node->limits.max = Height(p_HeightMap, node->centerX, node->centerZ);
      //node->limits.min = 20;
      node->limits = FindLimitsOfArea(node->centerX - node->width/2, node->centerX + node->width/2,
                                      node->centerZ - node->depth/2, node->centerZ + node->depth/2);

      //limits.max=200;
      node->centerY = (node->limits.min + node->limits.max)/2;
      node->height = node->limits.max - node->limits.min;
      node->found_limits = true;
      return(node->limits);
    }
}

But for some reason for limits.max of for example: (node->p_sec1->limits.max) I'm getting 10000 for every heightmap and even if I enable (//limits.max=200; ) towards the end. What could the problem be? Thanks.

Share this post


Link to post
Share on other sites
Walk through it in your debugger and watch the values as you proceed through the function. Thats probabbly the easiest way to determine whats going on.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this