Example #1
0
 void VUTOctreeNode::init( const Box3f & bbox, unsigned int maxIndicesPerOctel )
 {
   m_box = bbox;
   m_center = bbox.getCenter();
   m_data = new VUTOctreeIndices( maxIndicesPerOctel );
   m_nodes = NULL;
 }
Example #2
0
bool Line::intersect( const Box3f& box, Vec3f& enter, Vec3f& exit ) const
{
	if (box.isEmpty())
	{
		return false;
	}

	const Vec3f	&pos = getPosition(), &dir = getDirection();
	const Vec3f	&max = box.getMax(), &min = box.getMin();
	Vec3f		points[8], inter, bary;
	Plane		plane;
	int		i, v0, v1, v2;
	bool		front = false, valid, validIntersection = false;

	//
	// First, check the distance from the ray to the center
	// of the box.  If that distance is greater than 1/2
	// the diagonal distance, there is no intersection
	// diff is the vector from the closest point on the ray to the center
	// dist2 is the square of the distance from the ray to the center
	// radi2 is the square of 1/2 the diagonal length of the bounding box
	//
	float	t = (box.getCenter() - pos).dot(dir);
	Vec3f	diff(pos + dir * t - box.getCenter());
	float	dist2 = diff.dot(diff);
	float	radi2 = (max - min).dot(max - min) * 0.25f;

	if (dist2 > radi2)
	{
		return false;
	}

	// set up the eight coords of the corners of the box
	for(i = 0; i < 8; i++)
	{
		points[i].setValue(i & 01 ? min[0] : max[0],
				   i & 02 ? min[1] : max[1],
				   i & 04 ? min[2] : max[2]);
	}

	// intersect the 12 triangles.
	for(i = 0; i < 12; i++) 
	{
		switch(i) 
		{
		case  0: v0 = 2; v1 = 1; v2 = 0; break;		// +z
		case  1: v0 = 2; v1 = 3; v2 = 1; break;

		case  2: v0 = 4; v1 = 5; v2 = 6; break;		// -z
		case  3: v0 = 6; v1 = 5; v2 = 7; break;

		case  4: v0 = 0; v1 = 6; v2 = 2; break;		// -x
		case  5: v0 = 0; v1 = 4; v2 = 6; break;

		case  6: v0 = 1; v1 = 3; v2 = 7; break;		// +x
		case  7: v0 = 1; v1 = 7; v2 = 5; break;

		case  8: v0 = 1; v1 = 4; v2 = 0; break;		// -y
		case  9: v0 = 1; v1 = 5; v2 = 4; break;

		case 10: v0 = 2; v1 = 7; v2 = 3; break;		// +y
		case 11: v0 = 2; v1 = 6; v2 = 7; break;

		default:
			assert(false && "Must never happened");
			v0 = v1 = v2 = 0; // to remove a warning.
		}

		valid = intersect(	points[v0], points[v1], points[v2],
									inter, bary, front);

		if ( valid )
		{
			if	(front) 
			{
				enter = inter;
				validIntersection = valid;
			}
			else
			{
				exit = inter;
				validIntersection = valid;
			}
		}
	}

	return validIntersection;
}