0

我正在尝试实现一个简单的 kd 树,但是我在内存管理方面做错了,或者我正在尝试访问不存在的东西(ei 程序编译,但是当我运行时崩溃它)。这是我认为问题出在的部分:

void tree::addpoint (node* leaf, const my_vec &pointdata, int ref)
{
  my_vec center(2);
  int indicator;

  indicator = findquad (leaf, pointdata);

  if ( leaf->child[indicator] == NULL )
  {
    if ( (indicator%2) > 0)
        center[0] = leaf->center[0] + leaf->boxsize/4;
    else
        center[0] = leaf->center[0] - leaf->boxsize/4;
    if ( indicator > 1 )
        center[1] = leaf->center[1] + leaf->boxsize/4;
    else
        center[1] = leaf->center[1] - leaf->boxsize/4;
    leaf->child[indicator] = new node;
    leaf->child[indicator]->point = pointdata;
    leaf->child[indicator]->ref = ref;
    leaf->child[indicator]->center = center;
    leaf->child[indicator]->boxsize = leaf->boxsize/2;
    leaf->child[indicator]->IsReal = true;
    leaf->child[indicator]->child.resize(4);
    for (int i=1; i<4; i++)
        leaf->child[indicator]->child[i] = NULL;
  }
  else
    addpoint (leaf->child[indicator], pointdata, ref);

  if (leaf->IsReal)
  {
    leaf->IsReal = false;
    addpoint (leaf, pointdata, ref);
  }   
}

int tree::findquad(node *leaf, const my_vec& pointdata)
// For a given node 'node', find the proper octraturequadrature for a point located at   'pointdata'.
{
  bool north, east;
  int indicator = 0, end;

  end = pointdata.size() - 1;

  east = pointdata[0] >= leaf->center[0];
  north = pointdata[end] >= leaf->center[1];

  if (east)
    indicator = indicator + 2;
  if (north)
    indicator = indicator + 1;
  return(indicator);
}
4

0 回答 0