从 2 组点计算仿射变换

obe*_*tet 4 javascript 3d geometry three.js

我使用的是Threejs,并且通过两组THREE.Vector3' 定义了 2 个矩形,每个矩形有 4 个顶点。

如何计算将第一个矩形变换为第二个矩形的仿射变换?

我想通过将计算的仿射变换应用到第三个矩形.applyMatrix(matrix)。

在此输入图像描述

解决了:

/**
 * Transform a THREE.CSS3DObject object so that it aligns to a given rectangle.
 *
 * @param object: A THREE.CSS3DObject object.
 * @param v: A list of the 4 vertices of the rectangle (clockwise order) on which to align the object.
 */
function alignObject(object, v) {

   // width of DOM object wrapped via CSS3DObject
   var width = parseInt(object.element.style.width, 10);

   // compute rect vectors from rect vertices
   var v10 = v[1].clone().sub(v[0]);
   var v30 = v[3].clone().sub(v[0]);

   // compute (uniform) scaling
   var scale = v10.length() / width;

   // compute translation / new mid-point
   var position = new THREE.Vector3().addVectors(v10, v30).multiplyScalar(0.5).add(v[0]);

   // compute rotations
   var rotX = -v30.angleTo(new THREE.Vector3(0, -1, 0));
   // FIXME: rotY, rotZ

   // apply transformation
   object.scale.set(scale, scale, scale);
   object.position = position;
   object.rotateX(rotX);
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*ael 5

我正在寻找同样的东西。发现这个:https: //github.com/epistemex/transformation-matrix-js

还没有尝试过,但 fromTriangles() 函数看起来很有希望。

Matrix.fromTriangles( t1, t2 ); // returns matrix needed to produce t2 from t1

编辑:哎呀,我以为我将此作为评论发布。这成了一个答案。那好吧..

  • 图书馆不再存在,但这是一个很好的选择:https://github.com/chrvadala/transformation-matrix (2认同)