----- Original Message -----
From: "R K Shyam Prakash" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, March 15, 2004 6:49 AM
Subject: [JAVA3D] Vlume Calculation


> Hi,
>         I have a 3d model represented by a set of triangles. Assuming
> that the model does not have any holes in it, is there any algorithm
> using which I can calculate its volume? Can some body point me in the
> right direction.
>
> Thanks
> Shyam

Hi Shyam,
For convex bodies simply find a point inside the body and summarize the
tetraeders spanned by a triangle and this point. for non-convex bodies split
into convex bodies. i recently wrote a method which might help you.
Bye Chris

/** calculate volume of a tetraeder defined by 4 points
* @param p array of 4 points
* @return volume if error null
*/
public static double volumeTetraeder(Point[] p) {
//Volume is a sixth of the absolut value of
//the paralellepiped of the three vectors
//see http://www.tu-clausthal.de/~rzppk/cscript/kap7/tetra_struct.c
if ((p == null) || (p.length != 4)) {
System.err.println("ERROR: Geometry.volumeTetraeder:"
+ " Internal Error: parameter count");
//alternatively throw exception here
//throw new Exception("");
return Double.NaN;
}
//check for null entries here?
double[][] a = new double[3][3];
double s1, s2, s3, s4, s5, s6;
//set up matrix
for (int i = 0; i < 3; i++) {
a[i][0] = p[0].x - p[i + 1].x;
a[i][1] = p[0].y - p[i + 1].y;
a[i][2] = p[0].z - p[i + 1].z;
}
//calculate determinate of 3x3 matrix, rule of Sarrus
s1 = a[0][0] * a[1][1] * a[2][2];
s2 = a[0][1] * a[1][2] * a[2][0];
s3 = a[0][2] * a[1][0] * a[2][1];
s4 = a[0][2] * a[1][1] * a[2][0];
s5 = a[0][0] * a[2][1] * a[1][2];
s6 = a[2][2] * a[1][0] * a[0][1];
return Math.abs((s1 + s2 + s3 - s4 - s5 - s6) / 6.0);
}


//test volumeTetraheder with tree pyramids in a half cube
Point p000 = new Point(0,0,0);
Point p010 = new Point(0,1,0);
Point p100 = new Point(1,0,0);
Point p110 = new Point(1,1,0);
Point p001 = new Point(0,0,1);
Point p011 = new Point(0,1,1);
Point p101 = new Point(1,0,1);
Point p111 = new Point(1,1,1);
double v1 = volumeTetraeder(p000, p100, p110, p001);
double v2 = volumeTetraeder(p110, p101, p001, p111);
double v3 = volumeTetraeder(p100, p110, p101, p001);
double v = v1 + v2 + v3;
System.out.println("the following volume should be 0.5");
System.out.println("volume " + v);

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to