Some reasons as to why an Axis Aligned Bounding box wouldn't encompass the whole mesh.

Started by
4 comments, last by Enerjak 10 years, 4 months ago

OK, this has been annoying me for awhile now....I'm trying to generate an axis-aligned-bounding-box for an object I got the class okish but when I try to add points to it based on the vertices of the mesh (in this case, a circle...) I get this will number when I try to draw it....

http://puu.sh/5rZua.jpg

before you ask, no, that's not a tinted screen over everything, that is suppose to be an axis aligned bounding box around one of the circle. Here is how I'm doing it.


void Circle::draw()
{
	glPushMatrix();
	glTranslated(mPosition.getX() + mTranslation.getX(),mPosition.getY() + mTranslation.getY(),0);
	//glRotated(mSpin,0,0,1);
	glScaled(mScale.getX(),mScale.getY(),0);
	glTranslated(-mPosition.getX(),-mPosition.getY(),0);

	glPopMatrix();

	Vector3* vec = new Vector3();
	mBoundingBox->setEntity(this);
	mBoundingBox->setBounds(-FLT_MAX,-FLT_MAX,-FLT_MAX,
							 FLT_MAX, FLT_MAX, FLT_MAX);
	for(int i = 0; i < Vertices.size();i++)
	{
		VertexPC* v = Vertices[i];
		mBoundingBox->addPoint(*v);
		
	}
	glBegin(GL_POLYGON);
	// draw the circle.
	glColor4d(this->mRed,this->mGreen,this->mBlue,this->mAlpha);
	for(int i = 0; i < this->getNumVertices(); i++)
	{
		VertexPC* v = getVertex(i);
		glVertex2d(v->getVertX() + mPosition.getX(), v->getVertY() + mPosition.getY());
	}

	glEnd();
	

	
	if(bTurnDebugOn)
	{
		glPushMatrix();
		glTranslated(mPosition.getX(), 
			mPosition.getY(),0.0);
		glScaled(mScale.getX(),mScale.getY(),mScale.getZ());
		glTranslated(-mPosition.getX(),mPosition.getY(),0);
		glPopMatrix();
		glLineWidth(5);
		glBegin(GL_LINES);
		glColor4d(1,0,0,1);


		glVertex2d(mPosition.getX(),mPosition.getY());
		glVertex2d(mPosition.getX() + 50, mPosition.getY());
		glColor4d(0,1,0,1);
		glVertex2d(mPosition.getX(),mPosition.getY());
		glVertex2d(mPosition.getX(), mPosition.getY() + 50);



		glEnd();
		glPopMatrix();

		glPushMatrix();
		glTranslated(mPosition.getX(),mPosition.getY(),0);
		glScaled(1,1,1);
		glTranslated(-mPosition.getX(),-mPosition.getY(),0);

		// try to draw the aabb
		glBegin(GL_QUADS);
		glColor4d(mDebugRed,mDebugGreen,mDebugBlue,mDebugAlpha);
		double minX = mBoundingBox->getMin().getX() + mPosition.getX();
		double minY = mBoundingBox->getMin().getY() + mPosition.getY();
		double maxX = mBoundingBox->getMax().getX() + mPosition.getX();
		double maxY = mBoundingBox->getMax().getY() + mPosition.getY();

		glVertex2d(minX,minY);
		glVertex2d(minX,maxY);
		glVertex2d(maxX,maxY);
		glVertex2d(maxX,minY);

		glEnd();
		glPopMatrix();
	}
}

this is in the draw function so I don't know what I'm doing wrong or missing something. any advice would help.

Oh I feel I should post the circle's constructor also.


Circle::Circle()
	: Entity()
{
	this->mNumSegments = 0;
	this->mAngle = 0.0;
	this->mRadius = 0.0;
	mBoundingBox = new AxisAlignedBoundingBox();
	double minX =  -FLT_MAX;
	double minY =  -FLT_MAX;
	double maxX = FLT_MAX;
	double maxY = FLT_MAX;
	//mMin.setVector(-FLT_MAX,-FLT_MAX,-FLT_MAX);
	//mMax.setVector( FLT_MAX, FLT_MAX, FLT_MAX);
	
}

I hope something comes out of this.....

Advertisement

As far as I understand you, the bbox is much too big, isn't it? Without knowledge of BoundingBox::setBounds(...) and BoundingBox::addPoint(...) giving any advice means guessing, but it seems me that the initialization sets the bbox to virtually infinity and adding any points in finite space would not change this.

I'm saying this because in the 2nd code snippet there are these lines


double minX = -FLT_MAX;
double minY = -FLT_MAX;
double maxX = FLT_MAX;
double maxY = FLT_MAX;

A usual way of determining the boundary would look like so:


void BoundingBox::addPoint(VertexPC const& vertex) {
    if(vertex.x < mMinX) {
        mMinX = vertex.x;
    }
    if(vertex.x > mMaxX) {
        mMaxX = vertex.x;
    }
    // similarly for y
}

This works if mMinX is initialized to FLT_MAX and mMaxX is initialized to -FLT_MAX; that is the opposite of what you show in the OP. So, how does BoundingBox::setBounds(...) works with its parameters?

set bounds:


void setBounds(const Vector3 & min, const Vector3 & max)
	{
		setBounds(min.getX(),min.getY(),min.getZ(),
				  max.getX(),max.getY(),max.getZ());
	}
	// set the bounds for the bounding box.
	void setBounds(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
		setMin(minX,minY,minZ);
		setMax(maxX,maxY,maxZ);
	}

add point:


void AxisAlignedBoundingBox::addPoint(VertexPC &p)
{
	// adds x to min and max
	if(p.getVertX() < this->getMin().getX()) this->getMin().setX(p.getVertX());
	if(p.getVertX() > this->getMax().getX()) this->getMax().setX(p.getVertX());
	// adds y to min and max.
	if(p.getVertY() < this->getMin().getY()) this->getMin().setY(p.getVertY());
	if(p.getVertY() > this->getMax().getY()) this->getMax().setY(p.getVertY());
	// adds z to min and max.
	if(p.getVertZ() < this->getMin().getZ()) this->getMin().setZ(p.getVertZ());
	if(p.getVertZ() > this->getMax().getZ()) this->getMax().setZ(p.getVertZ());
}

Also, the Axis Aligned Bounding box constructor:


AxisAlignedBoundingBox::AxisAlignedBoundingBox()
{
	mMin.setVector(-FLT_MAX,-FLT_MAX,-FLT_MAX);
	mMax.setVector( FLT_MAX, FLT_MAX, FLT_MAX);
	mCenter.setVector(0,0,0);
	mEntity = nullptr;
	this->Center = 0.0;
}

Hope that shows the problem.


... Hope that shows the problem.

That shows exactly what I've described as the potential reason!



AxisAlignedBoundingBox::AxisAlignedBoundingBox()
{
	mMin.setVector(-FLT_MAX,-FLT_MAX,-FLT_MAX);
	mMax.setVector( FLT_MAX, FLT_MAX, FLT_MAX);
	mCenter.setVector(0,0,0);
	mEntity = nullptr;
	this->Center = 0.0;
} 

Ah, that constructor is bogus. Your min is already the smallest thing possible, and the max is the largest thing possible.

Thank you for all your advice. I appreciate all the help you've given me. I managed to follow your advice and fixed it. May the force be with you!

This topic is closed to new replies.

Advertisement