微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将几何体与平面对齐选择 3 个相交点

如何解决将几何体与平面对齐选择 3 个相交点

我有几天时间尝试对齐 3D 对象,选择与 3D 对象相交的 3 个点。

我必须将 GEOMETRY 与平面对齐。

我开始从 3 个点获取 crossvectors。

我做的第一步是将选定的第一个点移动到 0,0 位置:

introducir la descripción de la imagen aquí

移动后,如您所见,点没有更新。

let matrixToApply = new Matrix4().makeTranslation(
            -this.aligningPoints[0].x,-this.aligningPoints[0].y,-this.aligningPoints[0].z
        );
 this.mesh.geometry.applyMatrix4(matrixToApply)

introducir la descripción de la imagen aquí

在这一步之后,接下来是旋转第一个点和第二个点,获得它们之间的角度,代码太愚蠢了,我需要一些帮助来获得更好的解决方案,例如在欧拉上应用四分法。我只需要旋转并匹配到平面。

if (crossvectors.x > 0 && crossvectors.y > 0 && crossvectors.z < 0) {

            console.log('+ + -');

            var rotateX = new THREE.Vector3(0,100).angleto(new THREE.Vector3(0,crossvectors.y,crossvectors.z));

            this.mesh.geometry.rotateX(rotateX);

        } else if (crossvectors.x > 0 && crossvectors.y < 0 && crossvectors.z < 0) {

            console.log('+ - -');

            var rotateX = new THREE.Vector3(0,-100).angleto(new THREE.Vector3(0,crossvectors.z));

            this.mesh.geometry.rotateX(rotateX);

        } else if (crossvectors.x > 0 && crossvectors.y < 0 && crossvectors.z > 0) {

            console.log(' + - +');

            var rotateX = new THREE.Vector3(0,100).normalize().angleto(new THREE.Vector3(crossvectors.x,crossvectors.z).normalize());

            this.mesh.geometry.rotateY(-rotateX);

        } else if (crossvectors.x < 0 && crossvectors.y < 0 && crossvectors.z > 0) {

            console.log('- - +');

            var rotateX = new THREE.Vector3(0,100).normalize().angleto(new THREE.Vector3(0,crossvectors.z).normalize());

            this.mesh.geometry.rotateX(-rotateX);

        } else if (crossvectors.x < 0 && crossvectors.y < 0 && crossvectors.z < 0) {

            console.log('- - -');

            var rotateX = new THREE.Vector3(0,-100).normalize().angleto(new THREE.Vector3(0,crossvectors.z).normalize());

            this.mesh.geometry.rotateX(rotateX);

        } else if (crossvectors.x > 0 && crossvectors.y > 0 && crossvectors.z > 0) {

            console.log('+ + +');

            var rotateX = new THREE.Vector3(0,crossvectors.z).normalize());

            this.mesh.geometry.rotateX(rotateX);

        } else if (crossvectors.x < 0 && crossvectors.y > 0 && crossvectors.z < 0) {

            console.log('- + -');

            var rotateX = new THREE.Vector3(0,crossvectors.z).normalize());

            this.mesh.geometry.rotateX(rotateX);

        } else if (crossvectors.x < 0 && crossvectors.y > 0 && crossvectors.z > 0) {

            console.log('- + +');

            var rotateX = new THREE.Vector3(0,crossvectors.z).normalize());

            this.mesh.geometry.rotateX(rotateX);

        }

        else {

            alert('IMPOSSIBLE TO ALIGN');

        }

introducir la descripción de la imagen aquí

最后一步对齐第 2 点和第 3 点,计算两者之间的角度。

我有什么解决办法吗?

真的谢谢大家,我有点疯了,我是新来的。

我想要一个更简单的代码,我 90% 确定它有更好的方法来做到这一点。

这个想法是让平面上的对象由 3 个点对齐:

introducir la descripción de la imagen aquí

所需的结果,如您所见,脚部与地板平面对齐。

enter image description here

真的很感谢!

解决方法

我已经通过@WestLangley 的回复解决了这个问题,非常感谢您的帮助。

有对齐网格以匹配平面的代码:

let subvector1,subvector2,crossVectors = new THREE.Vector3();

// Create the cross vector with the 3 points
subvector1.subVectors(this.aligningPoints[0],this.aligningPoints[2]);
subvector2.subVectors(this.aligningPoints[0],this.aligningPoints[1]);
crossVectors.crossVectors(subvector1,subvector2);

// Move the mesh (0,0) plane
this.mesh.geometry.applyMatrix4(new Matrix4().makeTranslation(-this.aligningPoints[0].x,-this.aligningPoints[0].y,-this.aligningPoints[0].z))

// Align the mesh to the plane with quaternion and matrix4
const quaternion = new THREE.Quaternion();
quaternion.setFromUnitVectors( crossVectors.normalize(),new THREE.Vector3(0,1).normalize() ); 
let matrix4 = new THREE.Matrix4()
matrix4.makeRotationFromQuaternion( quaternion ); 
this.mesh.geometry.applyMatrix4( matrix4 );

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?