mardi 24 février 2015

Need help Segmentation Fault in C++


I'm playing around with Octrees...and i've been getting "Program received signal SIGSEGV, Segmentation fault".....here are "sections" of my code and the comments showing which line the debugger stopped at and saying Segmentation fault....I was thinking it may be dealing with my "class Octree" constructor but I've no idea how to repair it....really need your help...if you still don't get the whole picture please do ask which part of the code you need....I'm using Codeblocks...I've tested on 3 different 3D models.... 1. i_pts = 2560 --> worked , 2. i_pts = 13476 --> didn't work, 3. i_pts 79938 --> didn't work


In Octree.h



class OctreePoint
{
private:
Point position;
public:
OctreePoint();
OctreePoint(const Point& pos);
~OctreePoint();

//....other functions
};

class Octree
{
private:
Point origin; // The physical center of this node
Point halfDimension; // Half the width/height/depth of this node
Octree *children[8]; // Pointers to child octants
OctreePoint *data; // Data point to be stored at a node

public:
Octree(const Point& _origin, const Point& _halfDimension);
~Octree();

//....other functions
};


In Octree.cpp



Octree::Octree(const Point& _origin, const Point& _halfDimension)
:origin(_origin), halfDimension(_halfDimension), data(NULL)
{
for(int i=0; i<8; i++)
children[i] = NULL;
}

Octree::~Octree()
{
for(int i=0; i<8; i++)
delete children[i];
}

int Octree::getOctantContainingPoint(const Point& point) const
{
int oct = 0;
if(point.x >= origin.x) oct |= 4;
if(point.y >= origin.y) oct |= 2;
if(point.z >= origin.z) oct |= 1;
return oct;
}

bool Octree::isLeafNode() const{ return children[0] == NULL; }

void Octree::inserts(OctreePoint* point)
{
if(isLeafNode()){
if(data==NULL){
data = point;
return;
}
else{
OctreePoint* oldPoint = data;
data = NULL;

for(int i=0; i<8; i++){
Point newOrigin = origin;
newOrigin.x += halfDimension.x * (i&4 ? .5f : -.5f);
newOrigin.y += halfDimension.y * (i&2 ? .5f : -.5f);
newOrigin.z += halfDimension.z * (i&1 ? .5f : -.5f);
children[i] = new Octree(newOrigin, halfDimension*.5f);
}

children[getOctantContainingPoint(oldPoint->getPosition())]->inserts(oldPoint);
children[getOctantContainingPoint(point->getPosition())]->inserts(point);
}
}
else{
int octant = getOctantContainingPoint(point->getPosition());
children[octant]->inserts(point); /// <<------Segmentation fault here.
}
}


In Main.cpp



Point* pts = (Point*) malloc(N_pts*sizeof(Point));
Octree *Oct;
OctreePoint *octPoints;

int main()
{
//....
Oct = new Octree(Point(0.0,58.5,-1.10,0), Point(41.0,119.0,48.0,0)); // <<---these values will be different for different 3D models....
octPoints = new OctreePoint[i_pts];
for(int i=0; i<i_pts; i++)
{
f_in >> pts[i];
octPoints[i].setPosition(pts[i]);
Oct->inserts(octPoints + i); /// <<-----
}
//....
}



Aucun commentaire:

Enregistrer un commentaire