Advertisement

2 Octree Questions

Started by August 07, 2000 12:04 AM
5 comments, last by Esap1 24 years, 4 months ago
I have two Octree Questions, one simple, and one not so simple. 1. Why do I get an Access Violation in this code in my Octree Node class? ~COctree() { if(child[0])delete child[0];//HERE=Access Violation if(child[1])delete child[1]; if(child[2])delete child[2]; if(child[3])delete child[3]; if(child[4])delete child[4]; if(child[5])delete child[5]; if(child[6])delete child[6]; if(child[7])delete child[7]; if(Vertex)delete [] Vertex; if(box)delete box; } 2. I send a list of Vertices to the Build function in the Octree class. My function puts the vertices in each Octree Leaf. Though before drawing I need to have Faces to draw. How should I do it? PS: My face class is just 3 Index''s into a list of Vertices. Thanks,
I assume that''s COctree::~COctree().

Did you initialize all your children (child[0..7]) to NULL on initialization? If not, it might point to something other than 0, in which case your if statement would mistakenly assume that you allocated memory for it.

If you did both of those things, then you''ll probably have to post more code unless I''m missing something obvious too.
Advertisement
Hope this Helps, heres the FULL class:
[source]

class COctree
{

protected:
//Member Variables
COctree *child[8];
unsigned int numVertex;
CVertex *Vertex;
CBox *box;
bool isroot;
bool isOK;

public:
//Member Functions
void Init(){isOK=true;}
void End(){isOK=false;}
bool IsOK(){return isOK;}

bool isLeaf();
bool build(CVertex *Points,
const unsigned int count,
const unsigned int threshold,
const unsigned int maximumDepth,
const unsigned int currentDepth = 0);
void Draw();
unsigned int getnumVertex(){return numVertex;}
COctree* getOctant(unsigned short id){return child[id];}
CBox* getBox(){return box;}

int traverse();

//Constructor/Destructor
COctree()
{
child[0]=0;
child[1]=0;
child[2]=0;
child[3]=0;
child[4]=0;
child[5]=0;
child[6]=0;
child[7]=0;
Vertex=0;
box=0;
}
~COctree()
{
if(child[0])delete child[0];
if(child[1])delete child[1];
if(child[2])delete child[2];
if(child[3])delete child[3];
if(child[4])delete child[4];
if(child[5])delete child[5];
if(child[6])delete child[6];
if(child[7])delete child[7];
if(Vertex)delete [] Vertex;
if(box)delete box;
}


};
[source]
The code don't give me any problems.

Edited by - Geradian on August 7, 2000 3:04:24 PM
Should I post the Build function. Because Im pretty sure its something in there thats causing me to have an Access Violation.
Well, heres the Big build function. I wrote it all myself. It sorts points into the Octree Nodes, tell me if you see any problems with memory or something:
    bool	COctree::build(CVertex *Points, const unsigned int count, const unsigned int threshold,			const unsigned int maximumDepth,  			const unsigned int currentDepth){	unsigned int i;	unsigned int NewCount=0;	unsigned int TempCount=0;	float cxmin = 0.0, cxmax = 0.0;    	float cymin = 0.0, cymax = 0.0;    float czmin = 0.0, czmax = 0.0;	float bigest=0;			//if root 	if(currentDepth==0)	{		for(i = 0; i < count; i++) 		{                			//FIX THIS!!!!!!!						// Minimums.			if(Points<i>.getx() < cxmin)	            cxmin = Points[i].getx();               			if(Points[i].gety() < cymin)	            cymin = Points[i].gety();                			if(Points[i].getz() < czmin)	            czmin = Points[i].getz();                			// Maximums.			if(Points[i].getx() > cxmax)	            cxmax = Points[i].getx();                			if(Points[i].gety() > cymax)			    cymax = Points[i].gety();                			if(Points[i].getz() > czmax)			    czmax = Points[i].getz();    		}				if(-cxmin>bigest)bigest= -cxmin;		if(-cymin>bigest)bigest= -cymin;		if(-czmin>bigest)bigest= -czmin;		if(cxmax>bigest)bigest=cxmax;		if(cymax>bigest)bigest=cymax;		if(czmax>bigest)bigest=czmax;				cxmin=cymin=czmin=-bigest;		cxmax=cymax=czmax=bigest;				box = new CBox(cxmin,cxmax,	cymin,cymax, czmin,czmax);	}		for(i=0; i<count; i++)	{		if(box->CheckVertex(Points[i])==true)TempCount++;	}	 		CVertex *NewPoints = new CVertex[TempCount];		//Check if it is a Leaf	if(TempCount<=threshold || currentDepth>= maximumDepth)	{		//WE HAVE A LEAF		numVertex = TempCount;		Vertex = new CVertex [count];		memcpy(Vertex, Points, sizeof(CVertex) * TempCount);		return true;        	}	for(i = 0; i < count; i++)	{			if(NewCount > threshold || currentDepth < maximumDepth)		{			if(box->CheckVertex(Points[i])==true)			{				NewPoints[NewCount] = Points[i];				NewCount++;			}					}				}		numVertex = NewCount;		for(i = 0; i < 8; i++)	{            		child[i] = new COctree;	}								// Octant 0.	child[0]->box = new CBox(		box->xmin,		box->xmin + ((box->xmax - box->xmin) * 0.5f),		box->ymin,		box->ymin + ((box->ymax - box->ymin) * 0.5f),		box->zmin,		box->zmin + ((box->zmax - box->zmin) * 0.5f));		// Octant 1.	child[1]->box = new CBox(		box->xmin,		box->xmin + ((box->xmax - box->xmin) * 0.5f),		box->ymin,		box->ymin + ((box->ymax - box->ymin) * 0.5f),		box->zmin + ((box->zmax - box->zmin) * 0.5f),		box->zmax);		// Octant 2.	child[2]->box = new CBox(		box->xmin,		box->xmin + ((box->xmax - box->xmin) * 0.5f),		box->ymin + ((box->ymax - box->ymin) * 0.5f),		box->ymax,		box->zmin,		box->zmin + ((box->zmax - box->zmin) * 0.5f));		// Octant 3.	child[3]->box = new CBox(	box->xmin,		box->xmin + ((box->xmax - box->xmin) * 0.5f),		box->ymin + ((box->ymax - box->ymin) * 0.5f),		box->ymax,		box->zmin + ((box->zmax - box->zmin) * 0.5f),		box->zmax);		// Octant 4.	child[4]->box = new CBox(		box->xmin + ((box->xmax - box->xmin) * 0.5f),		box->xmax,		box->ymin,		box->ymin + ((box->ymax - box->ymin) * 0.5f),		box->zmin,		box->zmin + ((box->zmax - box->zmin) * 0.5f));		// Octant 5.	child[5]->box = new CBox(		box->xmin + ((box->xmax - box->xmin) * 0.5f),		box->xmax,		box->ymin,		box->ymin + ((box->ymax - box->ymin) * 0.5f),		box->zmin + ((box->zmax - box->zmin) * 0.5f),		box->zmax);		// Octant 6.	child[6]->box = new CBox(		box->xmin + ((box->xmax - box->xmin) * 0.5f),		box->xmax,		box->ymin + ((box->ymax - box->ymin) * 0.5f),		box->ymax,		box->zmin,		box->zmin + ((box->zmax - box->zmin) * 0.5f));		// Octant 7.	child[7]->box = new CBox(		box->xmin + ((box->xmax - box->xmin) * 0.5f),		box->xmax,		box->ymin + ((box->ymax - box->ymin) * 0.5f),		box->ymax,		box->zmin + ((box->zmax - box->zmin) * 0.5f),		box->zmax);		for(i = 0; i < 8; i++)		child[i]->build(NewPoints, NewCount, threshold, maximumDepth, currentDepth+1);				return true;}    
Advertisement
Does the code look ok, because I get and Access Violation when the Destructor is run when the program tries to exit.

This topic is closed to new replies.

Advertisement