2

我想从一组面孔中删除重复的面孔 - 我尝试了下面的一些代码,但我不确定如何完成它。

首先,我惊讶地发现:

new THREE.Vector3(0,0,0) == new THREE.Vector3(0,0,0)

产生假(我希望它产生真),此外,下面的代码也产生假(我再次希望它产生真)。

    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 坐标吗?此外,顶点的顺序是否重要?

4

1 回答 1

3

这不起作用的原因new THREE.Vector3(0,0,0) == new THREE.Vector3(0,0,0) 是:==在这种情况下检查两个值是否是对同一对象的引用。但是您的向量是不同的对象,它们恰好具有相同的 x、y 和 z 值。您应该在 Vector3 上使用 three.jsequals函数:

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)。因此,您可能需要扩展代码以另外检查这些情况。

于 2017-09-18T08:29:39.597 回答