// Amazing function - it takes a triangle, and a plane, and
slices our
// triange..producing the 3 new triangles. Returns 0 if it
didn't cut
// the triangle - 1 or 2 depending on how many times it cut the
triangle
// on the positive side of the triangle. It uses an array of 3
triangles
// so we can also use the cut triangle(s) for something else
incase we
// use this function for something special :)
// Returns 3 for all on the positive side of plane
// Returns 0 for all on the negative side of plane
// Returns 1 for 1 tri tnew[0] on positive side and tri[1] and
tri[2] on neg side
// Returns 2 for 2 tri tnew[0] & tnew[1] on pos side and of
course tri[2] on neg
int
ClipTriangle( Triangle t,
Triangle tnew[],
Vector3 vPointOnPlane,
Vector3 vPlaneNormal )
{
// Checking - not necessary though
vPlaneNormal = Vector3.normalize(vPlaneNormal);
// Clipping
Vector3 p0 =
new
Vector3( t.tx0, t.ty0, t.tz0 );
Vector3 p1 =
new
Vector3( t.tx1, t.ty1, t.tz1 );
Vector3 p2 =
new
Vector3( t.tx2, t.ty2, t.tz2 );
// Okay, lets see if our triangle actually cuts the plane
float
k = Vector3.dot( vPlaneNormal, vPointOnPlane );
float
a0 = Vector3.dot( vPlaneNormal, p0 );
float
a1 = Vector3.dot( vPlaneNormal, p1 );
float
a2 = Vector3.dot( vPlaneNormal, p2 );
// Determine how many points are on the positive side of our plane
int
iCount = 0;
boolean
p0in =
false;
boolean
p1in =
false;
boolean
p2in =
false;
if(
(k - a0)> 0 ){ iCount++; p0in=true; }
if(
(k - a1)> 0 ){ iCount++; p1in=true; }
if(
(k - a2)> 0 ){ iCount++; p2in=true; }
//System.out.println("iCount = " + iCount );
// If our triangle is fully on one side, or fully on the
other side
if(
iCount==0)
return
3;
// All on the positive side on
plane
if( iCount==3)
return
0;
// all on the negative side of
plane
// These two vectors will hold the two points on the plane
that
// actually cut the triangle.
// For example if we go from PointA to PointB on our
triangle,
// and it goes through the plane, the point where A->B cuts
the
// plane is called px0 for example.
Vector3 px0 =
new
Vector3();
Vector3 px1 =
new
Vector3();
// Now we have the two points on the plane that make
// up the intersection points, that we can construct
// the three new triangles from.
if(iCount==1
)
{
Vector3 pA =
new
Vector3();
Vector3 pB =
new
Vector3();
Vector3 pC =
new
Vector3();
if(
p0in ){ pA=p0; pB=p1; pC=p2; };
if(
p1in ){ pA=p1; pB=p0; pC=p2; };
if(
p2in ){ pA=p2; pB=p1; pC=p0; };
px0 = PointOnPlane( pA
/*vStart*/, pB
/*vEnd*/,
vPlaneNormal, vPointOnPlane );
px1 = PointOnPlane( pA
/*vStart*/, pC
/*vEnd*/,
vPlaneNormal, vPointOnPlane );
Triangle t1 =
new
Triangle( pA, px0, px1, t.tc );
tnew[0] = t1;
Triangle t2 =
new
Triangle( pB, px1, pC, t.tc );
Triangle t3 =
new
Triangle( pB, px0, px1, t.tc );
tnew[1] = t2;
tnew[2] = t3;
/*
System.out.println("p0in: " + p0in + " p1in: " + p1in + "
p2in:" + p2in );
System.out.println("p0 x:" + p0.m_x + " y:" + p0.m_y + "
z:" + p0.m_z );
System.out.println("p1 x:" + p1.m_x + " y:" + p1.m_y + "
z:" + p1.m_z );
System.out.println("p2 x:" + p2.m_x + " y:" + p2.m_y + "
z:" + p2.m_z );
Triangle ta = tnew[0];
System.out.println("newp0 x:" + ta.x0 + " y:" + ta.y0 + "
z:" + ta.z0 );
System.out.println("newp1 x:" + ta.x1 + " y:" + ta.y1 + "
z:" + ta.z1 );
System.out.println("newp2 x:" + ta.x2 + " y:" + ta.y2 + "
z:" + ta.z2 );
*/
return
1;
}//
End if(iCount==1)
// We are here!..so two points are inside our plane
if(iCount==2)
{
Vector3 pA =
new
Vector3();
Vector3 pB =
new
Vector3();
Vector3 pC =
new
Vector3();
//System.out.println("p0in: " + p0in + " p1in: " + p1in + " p2in:" + p2in
);
if(
!p0in ){ pA=p1; pB=p2; pC=p0; };
if(
!p1in ){ pA=p0; pB=p2; pC=p1; };
if(
!p2in ){ pA=p0; pB=p1; pC=p2; };
px0 = PointOnPlane( pB
/*vStart*/, pC
/*vEnd*/,
vPlaneNormal, vPointOnPlane );
px1 = PointOnPlane( pA
/*vStart*/, pC
/*vEnd*/,
vPlaneNormal, vPointOnPlane );
Triangle t1 =
new
Triangle( pC, px0, px1, t.tc );
// red
tnew[2] = t1;
Triangle t2 =
new
Triangle( pB, pA, px0, t.tc );
// dark red
Triangle t3 =
new
Triangle( pA, px0, px1, t.tc );
// green
tnew[0] = t2;
tnew[1] = t3;
return
2;
}//
End if(iCount==1)
return
3;
}//
End ClipTriangle(..)
//
find the distance between a ray and a plane.
public
float
distRayPlane(Vector3 vStart,
Vector3 vEnd,
Vector3 vnPlaneNormal,
Vector3 vPointOnPlane)
{
float
cosAlpha;
float
deltaD;
float
planeD = Vector3.dot( vnPlaneNormal, vPointOnPlane );
Vector3 vRayVector = Vector3.subtract(vEnd, vStart);
Vector3 vnRayVector = Vector3.normalize(vRayVector);
cosAlpha = Vector3.dot( vnPlaneNormal, vnRayVector );
// parallel to the plane (alpha=90)
if
( Math.abs(cosAlpha) < 0.001f )
return
Vector3.length(vRayVector);
deltaD = planeD - Vector3.dot(vStart, vnPlaneNormal);
float
l = (deltaD/cosAlpha);
//float k = Vector3.dot( vnPlaneNormal, vPointOnPlane );
//float a0 = Vector3.dot( vnPlaneNormal, vStart );
//l = (k - a0)/cosAlpha;
return
l;
}//
End of distRayPlane(..)
public
Vector3 PointOnPlane( Vector3 vStart, Vector3 vEnd,
Vector3 vPlaneNormal, Vector3 vPointOnPlane )
{
Vector3 vn = Vector3.subtract(vEnd,vStart);
vn = Vector3.normalize(vn);
float
Length =
distRayPlane(vStart,vEnd,vPlaneNormal,vPointOnPlane);
Vector3 px = Vector3.scale( vn, Length );
px = Vector3.add( vStart, px );
return
px;
}//
End of PointOnPlane(..)
|