2
我最近提交了一個關於體積計算的問題,在this thread得到了一些很大的幫助,並開發了一個函數來計算Three JS中封閉網格的體積。對於Face3網格,變量face4是一個真/假,將臉分爲兩個三角形。我的幾何主要是自定義的Face4構造。0細分的體積計算錯誤
function volumeCalc(object, face4){
if (face4 == false) {
for (var i=0; i<object.geometry.faces.length; i++) {
var pA = (object.geometry.faces[i].a);
var qA = (object.geometry.faces[i].b);
var rA = (object.geometry.faces[i].c);
var Px = object.geometry.vertices[pA].x;
var Py = object.geometry.vertices[pA].y;
var Pz = object.geometry.vertices[pA].z;
Pxlist.push(Px);
Pylist.push(Py);
Pzlist.push(Pz);
var Qx = object.geometry.vertices[qA].x;
var Qy = object.geometry.vertices[qA].y;
var Qz = object.geometry.vertices[qA].z;
Qxlist.push(Qx);
Qylist.push(Qy);
Qzlist.push(Qz);
var Rx = object.geometry.vertices[rA].x;
var Ry = object.geometry.vertices[rA].y;
var Rz = object.geometry.vertices[rA].z;
Rxlist.push(Rx);
Rxlist.push(Ry);
Rzlist.push(Rz);
}
} else {
for (var i=0; i<object.geometry.faces.length; i++) {
var pA = (object.geometry.faces[i].b);
var qA = (object.geometry.faces[i].c);
var rA = (object.geometry.faces[i].d);
var Px = object.geometry.vertices[pA].x;
var Py = object.geometry.vertices[pA].y;
var Pz = object.geometry.vertices[pA].z;
Pxlist.push(Px);
Pylist.push(Py);
Pzlist.push(Pz);
var Qx = object.geometry.vertices[qA].x;
var Qy = object.geometry.vertices[qA].y;
var Qz = object.geometry.vertices[qA].z;
Qxlist.push(Qx);
Qylist.push(Qy);
Qzlist.push(Qz);
var Rx = object.geometry.vertices[rA].x;
var Ry = object.geometry.vertices[rA].y;
var Rz = object.geometry.vertices[rA].z;
Rxlist.push(Rx);
Rxlist.push(Ry);
Rzlist.push(Rz);
}
}
for (i=0;i < Pxlist.length; i++){
pv = Pxlist[i]*Qylist[i]*Rzlist[i]; + Pylist[i]*Qzlist[i]*Rxlist[i]; + Pzlist[i]*Qxlist[i]*Rylist[i]; - Pxlist[i]*Qzlist[i]*Rylist[i]; - Pylist[i]*Qxlist[i]*Rzlist[i]; -Pzlist[i]*Qylist[i]*Rxlist[i];
pvlist.push(pv);
}
// calculate the total of these, I'm using _.js to add all values in the list.
addUp = (_.reduce(pvlist, function(memo, num){ return memo + num; }, 0))/6;
這是基於下式
pv = Px*Qy*Rz + Py*Qz*Rx + Pz*Qx*Ry - Px*Qz*Ry - Py*Qx*Rz - Pz*Qy*Rx;
我創建網格之前執行對THREE.Geometry以下:
geom.mergeVertices();
geom.computeCentroids();
geom.computeFaceNormals();
geom.computeVertexNormals();
var modifier = new THREE.SubdivisionModifier(x);
modifier.modify(geom);
體積計算是在所有的情況下正確細分其中x> 0。 當x = 0或沒有細分時,它是不正確的,數量級。
任何想法?同時也會意識到如何清理功能以供其他人使用,這是我第一次嘗試開源貢獻。謝謝。