gpt4 book ai didi

javascript - 从人脸数组中删除重复的人脸 - Three.js

转载 作者:行者123 更新时间:2023-11-29 19:02:02 24 4
gpt4 key购买 nike

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

首先我惊讶地发现:

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

产生 false(我希望它产生 True)而且下面的代码也产生 false(我再次希望它产生 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 坐标吗?此外,顶点的顺序重要吗?

最佳答案

这不起作用的原因: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) 相同。

此外,如果你的脸是两个面的,那么它也与顶点的任何顺序相同。 IE。 (3, 2, 1), (2, 1, 3) 和 (1, 3, 2)。因此,您可能希望扩展代码以额外检查这些情况。

关于javascript - 从人脸数组中删除重复的人脸 - Three.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46259217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com