我想从一组面孔中删除重复的面孔 - 我在下面尝试了一些代码,但我不知道如何完成它。
首先,我很惊讶地发现:
new THREE.Vector3(0,0,0) == new THREE.Vector3(0,0,0)
产生错误(我希望它产生True),而且下面的代码也会产生错误(我希望它会产生True)。
var triangleGeometry = new THREE.Geometry();
triangleGeometry.vertices.push(new THREE.Vector3( 0.0, 1.0, 0.0));
triangleGeometry.vertices.push(new THREE.Vector3(-1.0, -1.0, 0.0));
triangleGeometry.vertices.push(new THREE.Vector3( 1.0, -1.0, 0.0));
triangleGeometry.faces.push(new THREE.Face3(0, 1, 2));
var triangleGeometry2 = new THREE.Geometry();
triangleGeometry2.vertices.push(new THREE.Vector3( 0.0, 1.0, 0.0));
triangleGeometry2.vertices.push(new THREE.Vector3(-1.0, -1.0, 0.0));
triangleGeometry2.vertices.push(new THREE.Vector3( 1.0, -1.0, 0.0));
triangleGeometry2.faces.push(new THREE.Face3(0, 1, 2));
triangleGeometry2.faces[0] === triangleGeometry.faces[0] - yields false
至于我的代码,以确定一个面是否已经在面数组中,我写了以下内容:
function faceInArray(arrayOfFaces,face)
{ // https://stackoverflow.com/questions/29759480/how-to-customize-object-equality-for-javascript-set
// Determine whether a face is in an array of faces
// The ES6 Set object does not have any compare methods or custom compare extensibility.
// For this reason this function will be called before adding an face to an array of faces
// to ensure that duplicate faces are not placed in an array
for(let i = 0; i < arrayOfFaces.length; i++)
{
vertexaFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].a]
vertexbFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].b]
vertexcFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].c]
vertexaFace = buildingGeometry.vertices[face.a]
vertexbFace = buildingGeometry.vertices[face.b]
vertexcFace = buildingGeometry.vertices[face.c]
// Compare the vertices in each face I'm not sure how to do this?
}
}
现在我不确定如何从这里开始,因为简单地检查vertex1 == vertex2不起作用,因为我在第一个代码块中进行了演示。在比较每张脸时,我真的需要提取x,y和z坐标吗?此外,顶点的顺序是否重要?
答案 0 :(得分:2)
这不起作用的原因:new THREE.Vector3(0,0,0) == new THREE.Vector3(0,0,0)
在这种情况下,==
是否检查两个值是否是对同一对象的引用。但是你的矢量是碰巧具有相同x,y和z值的不同物体。你应该在Vector3上使用three.js equals
函数:
new THREE.Vector3(0,0,0).equals(new THREE.Vector3(0,0,0))
所以你的功能可以像这样工作:
function faceInArray(arrayOfFaces, face) {
for(let i = 0; i < arrayOfFaces.length; i++) {
vertexaFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].a]
vertexbFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].b]
vertexcFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].c]
vertexaFace = buildingGeometry.vertices[face.a]
vertexbFace = buildingGeometry.vertices[face.b]
vertexcFace = buildingGeometry.vertices[face.c]
if (vertexaFaceFromArray.equals(vertexaFace) &&
vertexbFaceFromArray.equals(vertexbFace) &&
vertexcFaceFromArray.equals(vertexcFace)) {
return true;
}
}
return false;
}
但是,当然,这只会检查每个面的顶点是否与输入面完全相同。这取决于您将要使用的内容,但原则上,面(1,2,3)与面(2,3,1)和(3,1,2)相同。
此外,如果您的面是双面的,那么它也与顶点的任何顺序相同。即(3,2,1),(2,1,3)和(1,3,2)。因此,您可能希望扩展代码以另外检查这些情况。