新浪博客

不规则六面体体积计算

2016-06-30 17:19阅读:
1、在不规则六面体中找一点P,将六面体的六个面(Face1---Face6)与P不规则五面体,
2、将不规则五面体,中(Face1-Face6)每一个面劈分成2个不规则的四面体
(如face1由abcd(顺时针或逆时针)四个点这分成P-abc,P-bcd)
3、计算每个四面体的体积
任意四面体P-ABC,记PA=a,PB=b,PC=c,cos角APB=x,cos角BPC=y,cos角CPA=z,
则:V=1/6*abc*sqrt(1+2xyz-x^2-y^2-z^2)
4、相加不规则六面体的体积
C#源码
///
/// 获取中心点
///
///
private JPoint3D CenterPoint()
{
//计算中心点
float topX = (LeftBackTop.X + LeftFrontTop.X + RightBackTop.X + RightFrontTop.X) / 4;
float topY = (LeftBackTop.Y + LeftFrontTop.Y + RightBackTop.Y + RightFrontTop.Y) / 4;
float topZ = (LeftBackTop.Z + LeftFrontTop.Z + Rig
htBackTop.Z + RightFrontTop.Z) / 4;
float bottomX = (LeftBackBottom.X + LeftFrontBottom.X + RightBackBottom.X + RightFrontBottom.X) / 4;
float bottomY = (LeftBackBottom.Y + LeftFrontBottom.Y + RightBackBottom.Y + RightFrontBottom.Y) / 4;
float bottomZ = (LeftBackBottom.Z + LeftFrontBottom.Z + RightBackBottom.Z + RightFrontBottom.Z) / 4;
float x = (topX + bottomX) / 2;
float y = (topY + bottomY) / 2;
float z = (topZ + bottomZ) / 2;
JPoint3D pCenter = new JPoint3D(x, y, z);
return pCenter;
}
public float Volume()
{
float fVolume = 0.0f;
//劈分成12个不规则四面体;
JPoint3D pCenter = CenterPoint();
//上
float pPA = Distance(LeftBackTop, pCenter);
float pPB = Distance(LeftFrontTop, pCenter);
float pPC = Distance(RightBackTop, pCenter);
float pAB = Distance(LeftBackTop, LeftFrontTop);
float pAC = Distance(LeftBackTop, RightBackTop);
float pBC = Distance(LeftFrontTop, RightBackTop);
float x = Cos(pPA, pPB, pAB);
float y = Cos(pPB, pPC, pBC);
float z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z))/6;
pPA = Distance(LeftFrontTop, pCenter);
pPB = Distance(RightFrontTop, pCenter);
pPC = Distance(RightBackTop, pCenter);
pAB = Distance(RightFrontTop, LeftFrontTop);
pAC = Distance(LeftFrontTop, RightBackTop);
pBC = Distance(RightFrontTop, RightBackTop);
x = Cos(pPA, pPB, pAB);
y = Cos(pPB, pPC, pBC);
z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z)) / 6;
//下
pPA = Distance(LeftFrontBottom, pCenter);
pPB = Distance(RightFrontBottom, pCenter);
pPC = Distance(RightBackBottom, pCenter);
pAB = Distance(RightFrontBottom, LeftFrontBottom);
pAC = Distance(LeftFrontBottom, RightBackBottom);
pBC = Distance(RightFrontBottom, RightBackBottom);
x = Cos(pPA, pPB, pAB);
y = Cos(pPB, pPC, pBC);
z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z)) / 6;
pPA = Distance(LeftFrontBottom, pCenter);
pPB = Distance(RightFrontBottom, pCenter);
pPC = Distance(RightBackBottom, pCenter);
pAB = Distance(RightFrontBottom, LeftFrontBottom);
pAC = Distance(LeftFrontBottom, RightBackBottom);
pBC = Distance(RightFrontBottom, RightBackBottom);
x = Cos(pPA, pPB, pAB);
y = Cos(pPB, pPC, pBC);
z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z)) / 6;
//左
pPA = Distance(LeftFrontTop, pCenter);
pPB = Distance(LeftBackTop, pCenter);
pPC = Distance(LeftBackBottom, pCenter);
pAB = Distance(LeftFrontTop, LeftBackTop);
pAC = Distance(LeftFrontTop, LeftBackBottom);
pBC = Distance(LeftBackTop, LeftBackBottom);
x = Cos(pPA, pPB, pAB);
y = Cos(pPB, pPC, pBC);
z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z)) / 6;
pPA = Distance(LeftFrontBottom, pCenter);
pPB = Distance(LeftBackBottom, pCenter);
pPC = Distance(LeftFrontTop, pCenter);
pAB = Distance(LeftFrontBottom, LeftBackBottom);
pAC = Distance(LeftFrontBottom, LeftFrontTop);
pBC = Distance(LeftBackBottom, LeftFrontTop);
x = Cos(pPA, pPB, pAB);
y = Cos(pPB, pPC, pBC);
z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z)) / 6;
//右
pPA = Distance(RightFrontTop, pCenter);
pPB = Distance(RightBackTop, pCenter);
pPC = Distance(RightBackBottom, pCenter);
pAB = Distance(RightFrontTop, RightBackTop);
pAC = Distance(RightFrontTop, RightBackBottom);
pBC = Distance(RightBackTop, RightBackBottom);
x = Cos(pPA, pPB, pAB);
y = Cos(pPB, pPC, pBC);
z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z)) / 6;
pPA = Distance(RightFrontBottom, pCenter);
pPB = Distance(RightBackBottom, pCenter);
pPC = Distance(RightFrontTop, pCenter);
pAB = Distance(RightFrontBottom, RightBackBottom);
pAC = Distance(RightFrontBottom, RightFrontTop);
pBC = Distance(RightBackBottom, RightFrontTop);
x = Cos(pPA, pPB, pAB);
y = Cos(pPB, pPC, pBC);
z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z)) / 6;
//前
pPA = Distance(RightFrontTop, pCenter);
pPB = Distance(RightFrontBottom, pCenter);
pPC = Distance(LeftFrontTop, pCenter);
pAB = Distance(RightFrontBottom, RightFrontTop);
pAC = Distance(RightFrontTop, LeftFrontTop);
pBC = Distance(RightFrontBottom, LeftFrontTop);
x = Cos(pPA, pPB, pAB);
y = Cos(pPB, pPC, pBC);
z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z)) / 6;
pPA = Distance(LeftFrontBottom, pCenter);
pPB = Distance(RightFrontBottom, pCenter);
pPC = Distance(LeftFrontTop, pCenter);
pAB = Distance(RightFrontBottom, LeftFrontBottom);
pAC = Distance(LeftFrontBottom, LeftFrontTop);
pBC = Distance(RightFrontBottom, LeftFrontTop);
x = Cos(pPA, pPB, pAB);
y = Cos(pPB, pPC, pBC);
z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z)) / 6;
//后
pPA = Distance(RightBackTop, pCenter);
pPB = Distance(RightBackBottom, pCenter);
pPC = Distance(LeftBackTop, pCenter);
pAB = Distance(RightBackBottom, RightBackTop);
pAC = Distance(RightBackTop, LeftBackTop);
pBC = Distance(RightBackBottom, LeftBackTop);
x = Cos(pPA, pPB, pAB);
y = Cos(pPB, pPC, pBC);
z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z)) / 6;
pPA = Distance(LeftBackBottom, pCenter);
pPB = Distance(RightBackBottom, pCenter);
pPC = Distance(LeftBackTop, pCenter);
pAB = Distance(RightBackBottom, LeftBackBottom);
pAC = Distance(LeftBackBottom, LeftBackTop);
pBC = Distance(RightBackBottom, LeftBackTop);
x = Cos(pPA, pPB, pAB);
y = Cos(pPB, pPC, pBC);
z = Cos(pPA, pPC, pAC);
fVolume += (float)(pPA * pPB * pPC * Math.Sqrt(1 + 2 * x * y * z - x * x - y * y - z * z)) / 6;
return fVolume;
}
public float Distance(JPoint3D p1,JPoint3D p2)
{
return (float)Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2) + Math.Pow(p1.Z - p2.Z, 2));
}
public float Cos(float a,float b,float c)
{
return (float)(a * a + b * b - c * c) / (2 * a * b);
}

我的更多文章

下载客户端阅读体验更佳

APP专享