I am trying to find bounding box volume of an STL file using the following code. For most of the files it is working fine but for files it gives strange result, means very high X, Y and Z values. Please help me to fix it.
enter code here
float length, breadth, height;
float minx = 0, maxx = 0,
miny=0, maxy=0,
minz=0, maxz=0;
public double signedVolumeOfTriangle(float[] p1, float[] p2, float[] p3)
{
double v321 = p3[0] * p2[1] * p1[2];
double v231 = p2[0] * p3[1] * p1[2];
double v312 = p3[0] * p1[1] * p2[2];
double v132 = p1[0] * p3[1] * p2[2];
double v213 = p2[0] * p1[1] * p3[2];
double v123 = p1[0] * p2[1] * p3[2];
double vol = (1.0 / 6.0) * (-v321 + v231 + v312 - v132 - v213 + v123);
if (Math.Min(Math.Min(p1[0], p2[0]), p3[0]) < minx)
{
minx = Math.Min(Math.Min(p1[0], p2[0]), p3[0]);
}
else if (Math.Max(Math.Max(p1[0], p2[0]), p3[0]) > maxx)
{
maxx = Math.Max(Math.Max(p1[0], p2[0]), p3[0]);
}
if (Math.Min(Math.Min(p1[1], p2[1]), p3[1]) < miny)
{
miny = Math.Min(Math.Min(p1[1], p2[1]), p3[1]);
}
else if (Math.Max(Math.Max(p1[1], p2[1]), p3[1]) > maxy)
{
maxy = Math.Max(Math.Max(p1[1], p2[1]), p3[1]);
}
if (Math.Min(Math.Min(p1[2], p2[2]), p3[2]) < minz)
{
minz = Math.Min(Math.Min(p1[2], p2[2]), p3[2]);
}
else if (Math.Max(Math.Max(p1[2], p2[2]), p3[2]) > maxz)
{
maxz = Math.Max(Math.Max(p1[2], p2[2]), p3[2]);
}
return vol;
}
public float[] getBoundingBox()
{
length = maxx - minx;
breadth = maxy - miny;
height = maxz - minz;
return new float[] { length, breadth, height };
}
Thanks
ayha