Exemple #1
0
// Returns true if the vector belongs to triangle v1v2v3.
// Returns false otherwise.
bool Real3D::belongsToTriangle(const Real3D& v1,
								  const Real3D& v2,
								  const Real3D& v3,
								  REAL zTolerance) const
{
	// Checks that the vector belongs to the triangle's bounding box.
/*
	TODO: compare with the BB using Math::TOLERANCE.
	if((this->v[0] > v1.v[0]) &&(this->v[0] > v2.v[0]) && (this->v[0] > v3.v[0]))
		return false;
	if((this->v[0] < v1.v[0]) &&(this->v[0] < v2.v[0]) && (this->v[0] < v3.v[0]))
		return false;
	if((this->v[1] > v1.v[1]) &&(this->v[1] > v2.v[1]) && (this->v[1] > v3.v[1]))
		return false;
	if((this->v[1] < v1.v[1]) &&(this->v[1] < v2.v[1]) && (this->v[1] < v3.v[1]))
		return false;
	if((this->v[2] > v1.v[2]) &&(this->v[2] > v2.v[2]) && (this->v[2] > v3.v[2]))
		return false;
	if((this->v[2] < v1.v[2]) &&(this->v[2] < v2.v[2]) && (this->v[2] < v3.v[2]))
		return false;
*/
	// Computes the triangle normal versor.
	Real3D n = (v2-v1)^(v3-v1);
	// If the triangle is degenerate returns false;
	REAL nmod2 = n.sqrmod();
	if(nmod2 < Math::TOLERANCE2)
		return false;
	// If the point doesn't belong to the triangle's plane return false.
	REAL t = n*(*this-v1);
	if(t*t > zTolerance*zTolerance*nmod2)
		return false;

	// Computes the normals to the triangles' edges.
	Real3D normal = ( (v2-v1)^n ).vers();
	t = (*this-v1)*normal;
	if(t > Math::TOLERANCE)
		return false;

	normal = ( (v3-v2)^n ).vers();
	t = (*this-v2)*normal;
	if(t > Math::TOLERANCE)
		return false;

	normal = ( (v1-v3)^n ).vers();
	t = (*this-v3)*normal;
	if(t > Math::TOLERANCE)
		return false;

	return true;
}
Exemple #2
0
// Reflects a point w.r.t. a plane through "origin",
// whose normal vector is "normal".
Real3D Real3D::reflect(const Real3D& origin, const Real3D& normal) const
{
	Real3D v(*this);
	REAL sqrmod = normal.sqrmod();
	REAL scal_prod;

	// If the normal vector is null returns this with no reflection at all.
	if(sqrmod >= 1E-20) {
		v -= origin;

		if(fabs(1. - sqrmod) < Math::TOLERANCE) {
			scal_prod = normal*v;
			v -= 2*scal_prod*normal;
		} else {
			// If the normal vector is not normalized computes the normalized vector.
			Real3D actualNormal(normal.vers());
			scal_prod = actualNormal*v;
			v -= 2*scal_prod*actualNormal;
		}
	}
	return v;
}