Beispiel #1
0
toxi::geom::Matrix4x4 toxi::geom::Matrix4x4::lookAt(  Vec3D & eye, Vec3D & target, Vec3D & up )
{
	Vec3D f = eye.sub( target ).normalize();
	Vec3D s = up.cross( f ).normalize();
	Vec3D t = f.cross( s ).normalize();
	return set( s.getX(), s.getY(), s.getZ(), -s.dot( eye ), t.getX(), t.getY(), t.getZ(), -t.dot( eye ), f.getX(),
		f.getY(), f.getZ(), -f.dot( eye ), 0, 0, 0, 1 );
}
Beispiel #2
0
// using vector to a point, unit normal vector to a plane, and a vector
//   to a point in the plane, find distance from point to plane
static double distancePointToPlane(const Vec3D& point,
								   const Vec3D& unitN,
								   const Vec3D& pointInPlane)
{
	Vec3D diff = point - pointInPlane;
	return unitN.dot(diff);
}
Beispiel #3
0
toxi::geom::Vec3D toxi::geom::Triangle3D::toBarycentric( Vec3D & p )
{
    Vec3D e = b.sub( a ).cross( c.sub( a ) );
    Vec3D n = e.getNormalized();

    // Compute twice area of triangle ABC
    float areaABC = n.dot( e );
    // Compute lambda1
    float areaPBC = n.dot( b.sub( p ).cross( c.sub( p ) ) );
    float l1 = areaPBC / areaABC;

    // Compute lambda2
    float areaPCA = n.dot( c.sub( p ).cross( a.sub( p ) ) );
    float l2 = areaPCA / areaABC;

    // Compute lambda3
    float l3 = 1.0f - l1 - l2;

    return Vec3D(l1, l2, l3);
}
Beispiel #4
0
bool toxi::geom::AABB::planeBoxOverlap( Vec3D & normal, const float & d, const Vec3D & maxBox )
{
	Vec3D vmin = Vec3D();
	Vec3D vmax = Vec3D();

	if (normal.getX() > 0.0f) {
		vmin.setX( -maxBox.getX() );
		vmax.setX( maxBox.getX() );
	} else {
		vmin.setX( maxBox.getX() );
		vmax.setX( -maxBox.getX() );
	}

	if (normal.getY() > 0.0f) {
		vmin.setY( -maxBox.getY() );
		vmax.setY( maxBox.getY() );
	} else {
		vmin.setY( maxBox.getY() );
		vmax.setY( -maxBox.getY() );
	}

	if (normal.getZ() > 0.0f) {
		vmin.setZ( -maxBox.getZ() );
		vmax.setZ( maxBox.getZ() );
	} else {
		vmin.setZ( maxBox.getZ() );
		vmax.setZ( -maxBox.getZ() );
	}
	if (normal.dot(vmin) + d > 0.0f) {
		return false;
	}
	if (normal.dot(vmax) + d >= 0.0f) {
		return true;
	}
	return false;
}
Beispiel #5
0
int TriangleMeshData::createDebugMesh( CFly::Object* obj , CFly::Material* mat )
{
	struct vtx_t
	{
		vtx_t( Vec3D const& v , Vec3D const& c)
			:vtx(v),color(c){}
		Vec3D vtx;
		Vec3D color;
	};

	std::vector< vtx_t > vtxVec;
	std::vector< TriangleIndex > triVec;
	vtxVec.reserve( m_triVec.size() * 3 );
	triVec.reserve( m_triVec.size() );
	int index = -1;
	for (int i = 0; i < m_triVec.size(); ++i )
	{
		TriangleIndex& tri = m_triVec[i];
		Vec3D& v0 = m_vertexVec[ tri.v[0] ]; 
		Vec3D& v1 = m_vertexVec[ tri.v[1] ]; 
		Vec3D& v2 = m_vertexVec[ tri.v[2] ]; 
		Vec3D normal = ( v1 - v0 ).cross( v2 - v1 );

		float len = sqrt( normal.dot(normal) );
		float c = ( 2 + (normal.y + normal.z)/len )/ 4 ;

		Vec3D color( c ,c , c );
		vtxVec.push_back( vtx_t( v0 , color ) );
		vtxVec.push_back( vtx_t( v1 , color ) );
		vtxVec.push_back( vtx_t( v2 , color ) );

		TriangleIndex t;
		t.v[0] = ++index;
		t.v[1] = ++index;
		t.v[2] = ++index;

		triVec.push_back( t );
	}

	return obj->createIndexedTriangle( 
		mat , CFVT_XYZ_CF1 , 
		(float*) &vtxVec[0] , vtxVec.size() , 
		&( triVec[0].v[0] ) , triVec.size() );
}
Beispiel #6
0
static double vecLength(Vec3D vec)
{
	return std::sqrt(vec.dot(vec));
}
Beispiel #7
0
toxi::geom::Vec3D toxi::geom::Triangle3D::closestPointOnSurface( Vec3D & p )
{
    Vec3D ab = b.sub( a );
    Vec3D ac = c.sub( a );
    Vec3D bc = c.sub( b );

    Vec3D pa = p.sub( a );
    Vec3D pb = p.sub( b );
    Vec3D pc = p.sub( c );

    Vec3D ap = a.sub( p );
    Vec3D bp = b.sub( p );
    Vec3D cp = c.sub( p );

    // Compute parametric position s for projection P' of P on AB,
    // P' = A + s*AB, s = snom/(snom+sdenom)
    float snom = pa.dot( ab );

    // Compute parametric position t for projection P' of P on AC,
    // P' = A + t*AC, s = tnom/(tnom+tdenom)
    float tnom = pa.dot( ac );

    if ( snom <= 0.0f && tnom <= 0.0f ) {
        return a; // Vertex region early out
    }

    float sdenom = pb.dot( a.sub( b ) );
    float tdenom = pc.dot( a.sub( c ) );

    // Compute parametric position u for projection P' of P on BC,
    // P' = B + u*BC, u = unom/(unom+udenom)
    float unom = pb.dot( bc );
    float udenom = pc.dot( b.sub( c ) );

    if ( sdenom <= 0.0f && unom <= 0.0f ) {
        return b; // Vertex region early out
    }
    if ( tdenom <= 0.0f && udenom <= 0.0f ) {
        return c; // Vertex region early out
    }

    // P is outside (or on) AB if the triple scalar product [N PA PB] <= 0
    Vec3D n = ab.cross( ac );
    float vc = n.dot( ap.crossSelf( bp ) );

    // If P outside AB and within feature region of AB,
    // return projection of P onto AB
    if ( vc <= 0.0f && snom >= 0.0f && sdenom >= 0.0f ) {
        // return a + snom / (snom + sdenom) * ab;
        return a.add( ab.scaleSelf( snom / ( snom + sdenom ) ) );
    }

    // P is outside (or on) BC if the triple scalar product [N PB PC] <= 0
    float va = n.dot( bp.crossSelf( cp ) );
    // If P outside BC and within feature region of BC,
    // return projection of P onto BC
    if ( va <= 0.0f && unom >= 0.0f && udenom >= 0.0f ) {
        // return b + unom / (unom + udenom) * bc;
        return b.add( bc.scaleSelf( unom / ( unom + udenom ) ) );
    }

    // P is outside (or on) CA if the triple scalar product [N PC PA] <= 0
    float vb = n.dot( cp.crossSelf( ap ) );
    // If P outside CA and within feature region of CA,
    // return projection of P onto CA
    if ( vb <= 0.0f && tnom >= 0.0f && tdenom >= 0.0f ) {
        // return a + tnom / (tnom + tdenom) * ac;
        return a.add( ac.scaleSelf( tnom / ( tnom + tdenom ) ) );
    }

    // P must project inside face region. Compute Q using barycentric
    // coordinates
    float u = va / ( va + vb + vc );
    float v = vb / ( va + vb + vc );
    float w = 1.0f - u - v; // = vc / (va + vb + vc)
    // return u * a + v * b + w * c;
    return a.scale( u ).addSelf( b.scale( v ) ).addSelf( c.scale( w ) );
}