maj*_*rif 0 javascript python math matrix
我正在尝试计算Three.JS中的变换,老实说,我是3D数学的新手.
在Three.JS中有3x3和4x4矩阵类,但没有4x3.
目标:我想了解如何在4x3*4x3矩阵上进行乘法运算.
我有一组输入和正确的输出.我通过使用python得到它,因为有一个名为Noesis
4x3矩阵的乘法库.
问题是库调用python二进制文件,所以我看不到源代码.
python中的代码很简单: modelSpaceMat = boneMat * frameMat
以下是正确数据集的示例:
boneMat= (
(0.0, 0.0, 1.0),
(0.0, 1.0, 0.0),
(-1.0, 0.0, 0.0),
(0.0, 32.29199981689453, -3.2665998935699463)
)
frameMat= (
(0.6425124406814575, -0.06018795818090439, 0.7639083862304688),
(-0.003379624802619219, 0.9966778755187988, 0.08136376738548279),
(-0.7662678360939026, -0.05486864596605301, 0.640174925327301),
(4.438972473144531, -1.4769394397735596, 3.863013744354248)
)
modelSpaceMat=(
(-0.7639083862304688, -0.06018795818090439, 0.6425124406814575),
(-0.08136376738548279, 0.9966778755187988, -0.003379624802619219),
(-0.640174925327301, -0.05486864596605301, -0.7662678360939026),
(-1.1457012760729413e-07, 30.441999435424805, 9.592490357590577e-08)
)
boneMat= (
(1.0, 0.0, 0.0),
(0.0, 1.0, 0.0),
(0.0, 0.0, 1.0),
(0.0, 0.007000000216066837, -3.2665998935699463)
)
frameMat= (
(1.0000004768371582, 2.3320662876358256e-06, -1.4419805665966123e-05),
(2.817355607476202e-06, 0.9999991059303284, -3.3202520626218757e-06),
(1.5038006495160516e-05, -2.133270072590676e-06, 1.0000003576278687),
(-0.0015083501348271966, 0.02300064079463482, 3.266691207885742)
)
modelSpaceMat= (
(1.0000004768371582, 2.3320662876358256e-06, -1.4419805665966123e-05),
(2.817355607476202e-06, 0.9999991059303284, -3.3202520626218757e-06),
(1.5038006495160516e-05, -2.133270072590676e-06, 1.0000003576278687),
(-0.0014612301019951701, 0.030011480674147606, 9.013115777634084e-05)
)
Run Code Online (Sandbox Code Playgroud)
现在我希望能够用javascript做到这一点.
问题:
python模块Noesis有这一行:
def __mul__(self, other):
if isinstance(other, (NoeMat43, list, tuple)):
return noesis.mat43Mul(self, other)
elif isinstance(other, NoeVec3):
return noesis.mat43TransformPoint(self, other)
elif isinstance(other, NoeVec4):
return noesis.mat43TransformVec4(self, other)
else:
return NoeMat43([self.mat43[0]*other, self.mat43[1]*other, self.mat43[2]*other, self.mat43[3]*other])
Run Code Online (Sandbox Code Playgroud)
我找不到源代码noesis.mat43Mul
.
测试Tamas Hegedus回答:
var boneMat = new THREE.Matrix4().copy(_bone.userData.modelMatrix);
var frameMat = new THREE.Matrix4();
frameMat.set(
bone.rotationMatrix[0], bone.rotationMatrix[1], bone.rotationMatrix[2], bone.positionVector[0],
bone.rotationMatrix[3], bone.rotationMatrix[4], bone.rotationMatrix[5], bone.positionVector[1],
bone.rotationMatrix[6], bone.rotationMatrix[7], bone.rotationMatrix[8], bone.positionVector[2],
0.0, 0.0, 0.0, 1.0
);
var modelSpaceMat = new THREE.Matrix4();
modelSpaceMat.multiplyMatrices(boneMat, frameMatrix);
console.log(boneMat, frameMat, modelSpaceMat);
Run Code Online (Sandbox Code Playgroud)
但结果与python库modelSpaceMat
有点不同modelSpaceMat
.就像(-1.1457012760729413e-07, 30.441999435424805, 9.592490357590577e-08)
在python中的位置部分,但是3.863013744354248, 30.815059661865234, -7.705572128295898
在javascript中.
这是为什么?
从数学的角度来看,你不能乘以4x3矩阵.
当它关于仿射变换时,图形库在同构空间中使用特殊的矩形矩阵子集来描述变换:
a b c 0
d e f 0
g h i 0
x y z 1
Run Code Online (Sandbox Code Playgroud)
选择最后一列是为了0 0 0 1
使变换不是透视投影.
有些图形库只存储该变换的有趣4x3部分,但乘法必须按照4x4格式进行.
Threejs有一个内置Matrix4
类,它实现了乘法.