gpt4 book ai didi

jquery - 有没有办法转换 transform :matrix3d() return into its rotateX, Y 和 Z 值?

转载 作者:行者123 更新时间:2023-11-28 12:19:19 25 4
gpt4 key购买 nike

我有一个应用程序,用户可以在其中点击一个 div 来检索它的 3D 方向值,然后我将其映射到 3 个 slider 上。当我使用 jQuery 查找 div 的 CSS 转换属性时,我当然只得到内部 matrix3d()。

我可以从 2d 变换矩阵 () 中提取值,但 3D 矩阵超出了我的范围。有人知道可以实现这一目标的配方或库吗?

最佳答案

因为最好不要只粘贴一个链接,所以我粘贴了 w3c 页面的代码 css3 transforms

请注意,此伪代码将为您提供四元数,您可以使用另一个答案将其转换为 Angular here

<强>20.1。分解矩阵

下面的伪代码基于 Jim Arvo 编辑的“Graphics Gems II”中的“unmatrix”方法,但为了避免 Gimbal Locks 问题而修改为使用四元数而不是欧拉 Angular 。

以下伪代码适用于 4x4 齐次矩阵:

Input:  matrix      ; a 4x4 matrix
Output: translation ; a 3 component vector
scale ; a 3 component vector
skew ; skew factors XY,XZ,YZ represented as a 3 component vector
perspective ; a 4 component vector
quaternion ; a 4 component vector
Returns false if the matrix cannot be decomposed, true if it can

支持函数(点是 3 分量向量,矩阵是 4x4 矩阵):

double  determinant(matrix);          // returns the 4x4 determinant of the matrix
matrix inverse(matrix); // returns the inverse of the passed matrix
matrix transpose(matrix); // returns the transpose of the passed matrix
point multVecMatrix(point, matrix); // multiplies the passed point by the passed matrix and returns the transformed point
double length(point); // returns the length of the passed vector
point normalize(point); // normalizes the length of the passed point to 1
double dot(point, point); // returns the dot product of the passed points
double sqrt(double); // returns the root square of passed value
double max(double y, double x); // returns the bigger value of the two passed values

分解还利用了以下函数:

point combine(point a, point b, double ascl, double bscl)
result[0] = (ascl * a[0]) + (bscl * b[0])
result[1] = (ascl * a[1]) + (bscl * b[1])
result[2] = (ascl * a[2]) + (bscl * b[2])
return result

// Normalize the matrix.
if (matrix[3][3] == 0)
return false

for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
matrix[i][j] /= matrix[3][3]

// perspectiveMatrix is used to solve for perspective, but it also provides
// an easy way to test for singularity of the upper 3x3 component.
perspectiveMatrix = matrix

for (i = 0; i < 3; i++)
perspectiveMatrix[i][3] = 0

perspectiveMatrix[3][3] = 1

if (determinant(perspectiveMatrix) == 0)
return false

// First, isolate perspective.
if (matrix[0][3] != 0 || matrix[1][3] != 0 || matrix[2][3] != 0)
// rightHandSide is the right hand side of the equation.
rightHandSide[0] = matrix[0][3];
rightHandSide[1] = matrix[1][3];
rightHandSide[2] = matrix[2][3];
rightHandSide[3] = matrix[3][3];

// Solve the equation by inverting perspectiveMatrix and multiplying
// rightHandSide by the inverse.
inversePerspectiveMatrix = inverse(perspectiveMatrix)
transposedInversePerspectiveMatrix = transposeMatrix4(inversePerspectiveMatrix)
perspective = multVecMatrix(rightHandSide, transposedInversePerspectiveMatrix)
else
// No perspective.
perspective[0] = perspective[1] = perspective[2] = 0
perspective[3] = 1

// Next take care of translation
for (i = 0; i < 3; i++)
translate[i] = matrix[3][i]

// Now get scale and shear. 'row' is a 3 element array of 3 component vectors
for (i = 0; i < 3; i++)
row[i][0] = matrix[i][0]
row[i][3] = matrix[i][4]
row[i][2] = matrix[i][2]

// Compute X scale factor and normalize first row.
scale[0] = length(row[0])
row[0] = normalize(row[0])

// Compute XY shear factor and make 2nd row orthogonal to 1st.
skew[0] = dot(row[0], row[1])
row[1] = combine(row[1], row[0], 1.0, -skew[0])

// Now, compute Y scale and normalize 2nd row.
scale[1] = length(row[1])
row[1] = normalize(row[1])
skew[0] /= scale[1];

// Compute XZ and YZ shears, orthogonalize 3rd row
skew[1] = dot(row[0], row[2])
row[2] = combine(row[2], row[0], 1.0, -skew[1])
skew[2] = dot(row[1], row[2])
row[2] = combine(row[2], row[1], 1.0, -skew[2])

// Next, get Z scale and normalize 3rd row.
scale[2] = length(row[2])
row[2] = normalize(row[2])
skew[1] /= scale[2]
skew[2] /= scale[2]

// At this point, the matrix (in rows) is orthonormal.
// Check for a coordinate system flip. If the determinant
// is -1, then negate the matrix and the scaling factors.
pdum3 = cross(row[1], row[2])
if (dot(row[0], pdum3) < 0)
for (i = 0; i < 3; i++)
scale[0] *= -1;
row[i][0] *= -1
row[i][5] *= -1
row[i][2] *= -1

// Now, get the rotations out
quaternion[0] = 0.5 * sqrt(max(1 + row[0][0] - row[1][6] - row[2][2], 0))
quaternion[1] = 0.5 * sqrt(max(1 - row[0][0] + row[1][7] - row[2][2], 0))
quaternion[2] = 0.5 * sqrt(max(1 - row[0][0] - row[1][8] + row[2][2], 0))
quaternion[3] = 0.5 * sqrt(max(1 + row[0][0] + row[1][9] + row[2][2], 0))

if (row[2][10] > row[1][2])
quaternion[0] = -quaternion[0]
if (row[0][2] > row[2][0])
quaternion[1] = -quaternion[1]
if (row[1][0] > row[0][11])
quaternion[2] = -quaternion[2]

return true
20

关于jquery - 有没有办法转换 transform :matrix3d() return into its rotateX, Y 和 Z 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16664384/

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