JS 的线性变换和矩阵乘法失败 [英] Linear transformation and matrix multiplication fails with JS

查看:38
本文介绍了JS 的线性变换和矩阵乘法失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习线性代数并尝试使用基本的线性变换(旋转、缩放、平移)制作一个小程序.

I'm learning linear algebra and trying to make a little program with basic linear transformations (rotating, scaling, translating).

https://codesandbox.io/embed/determined-diffie-t2iy5?fontsize=14&hidenavigation=1&theme=dark

我编写了用于为每个变换生成每个矩阵的函数以及计算它们的函数(将矩阵与一个点相乘,将矩阵相乘).

I wrote functions for generating each matrix for each transformation and functions to calculate them (multiplying matrix with a point, multiplying matrices).

export const multiplyMatrixWithPoint = (matrix, point) => {
  return point.map((dimension, index) => {
    let result = 0;

    for (let i = 0; i < 4; i++) {
      const matrixIndex = index * 4 + i;
      result += dimension * matrix[matrixIndex];
    }

    return +result.toFixed(2);
  })
};

// Just creating 2D array to make it easy to calculate the matrix
export const matrixToPoints = matrix => {
  const result = [];

  for (let i = 0; i < 4; i++) {
    const onePoint = [];

    for (let j = 0; j < 4; j++) {
      onePoint.push(matrix[4 * i + j]);
    }
    
    result.push(onePoint);
  }

  return result;
};

// Just making 2D array to 1D
export const pointsToMatrix = points => points.reduce((acc, points) => [...acc, ...points], []);

// Transpose function to make the matrix multiplication correct
export const transposeCSSMatrixToTransform = matrix => matrix[0].map((column, index) => matrix.map(row => row[index]));

export const multiplyMatrices = (matrixA, matrixB) => {
  const separatePoints = transposeCSSMatrixToTransform(matrixToPoints(matrixB));

  return pointsToMatrix(transposeCSSMatrixToTransform(separatePoints.map(point => multiplyMatrixWithPoint(matrixA, point))));
};

export const rotationMatrixGenerator = (matrix, angle) => {
  const radians = degreeToRadian(angle);

  matrix[0] = Math.cos(radians);
  matrix[1] = -Math.sin(radians);
  matrix[4] = Math.sin(radians);
  matrix[5] = Math.cos(radians);

  return matrix; 
};

export const scaleMatrixGenerator = (matrix, { x, y }) => {
  matrix[0] = x;
  matrix[5] = y;

  return matrix;
};

export const translateMatrixGenerator = (matrix, { x, y }) => {
  matrix[12] = x;
  matrix[13] = y;

  return matrix;
};

为了显示转换,我使用了 CSS 的 matrix3d

For showing the transformations I used CSS's matrix3d

export const matrixToCSSMatrix = matrix => `matrix3d(${matrix.join(',')})`;

这里是代码的执行部分

  ...
  const [matrix, setMatrix] = useState(DEFAULT_MATRIX);
  const [rotationAngle, setRotationAngle] = useState(0);
  const [scale, setScale] = useState(DEFAULT_XY);
  const [translate, setTranslate] = useState(DEFAULT_XY);

  const classes = useStyles();
  const { app } = classes;

  // ---------------------- Rotate --------------------- //
  const rotate = useCallback(e => {
    const { value: angle } = e.target;

    const rotationMatrix = rotationMatrixGenerator(matrix, +angle);
    const updatedMatrix = multiplyMatrices(matrix, rotationMatrix);

    setMatrix(updatedMatrix);
    setRotationAngle(angle);
  }, [matrix]);

  // ---------------------- Scale --------------------- //
  const changeScale = useCallback((e, dimension) => {
    const { value } = e.target;
    const updatedScale = {
      ...scale,
      [dimension]: +value
    };

    const scaleMatrix = scaleMatrixGenerator(matrix, updatedScale);
    const updatedMatrix = multiplyMatrices(matrix, scaleMatrix);

    setMatrix(scaleMatrix);
    setScale(updatedScale);
  }, [matrix, scale]);

  // ------------------ Translate --------------------- //
  const changeTranslation = useCallback((e, position) => {
    const { value } = e.target;
    const updatedTranslation = {
      ...translate,
      [position]: +value
    };

    const translateMatrix = translateMatrixGenerator(matrix, updatedTranslation);
    const updatedMatrix = multiplyMatrices(matrix, translateMatrix);

    setMatrix(translateMatrix);
    setTranslate(updatedTranslation);
  }, [matrix, translate]);
  ...

首先我认为是因为我没有转置矩阵,我尝试转置它,它有一点帮助,但并没有修复所有错误.此外,我尝试收集所有转换并立即应用它们,但这是一个非常糟糕的解决方案,效果不佳.

Firstly I thought it's because I didn't transpose the matrix, I tried to transpose it, it helped a little but didn't fix all bugs. Also, I tried to collect all transformations and apply them at once, but it was a very bad solution and didn't work well.

我需要单独的旋转、缩放和过渡,但旋转和过渡效果不佳,因为它们使用相同的矩阵段.此外,我无法实现对 Z 轴的正常旋转.

I need to have a separate rotation, scaling and transition, but rotation and transition are didn't work well because they are using the same segments of matrix. Also, I can't achieve normal rotation against Z-axis.

推荐答案

这里我计算 3D 变换矩阵(4 x 4 矩阵)以进行 2D 平移、缩放和旋转.我还在样式转换中使用显式串联或通过预先乘以矩阵来组合它们.

Here I compute 3D transformation matrices (4 x 4 matrices) for 2D translation, scale, and rotation. I also compose them with explicit concatenation in the style transform, or by pre multiplying the matrices.

const resultEl = document.getElementById('result')
const angleEl = document.getElementById('angle')
const xEl = document.getElementById('x')
const yEl = document.getElementById('y')
const sxEl = document.getElementById('sx')
const syEl = document.getElementById('sy')
const premulEl = document.getElementById('premul')
/** Counter clock-wise rotation */
function getXRotation(angle){
  const c = Math.cos(angle)
  const s = Math.sin(angle)
  return [
  +c, -s,  0,  0,
  +s, +c,  0,  0,
   0,  0,  1,  0,
   0,  0,  0,  1];
}

function getTranslation(x, y){
return [
 1, 0, 0, 0,
 0, 1, 0, 0,
 0, 0, 1, 0,
 x, y, 0, 1]
}
function getScale(sx, sy){
return [
 sx, 0, 0, 0,
  0,sy, 0, 0,
  0, 0, 1, 0,
  0, 0, 0, 1]
}

function mat4mul(B, A){
  const C = [];
  for(let i = 0; i < 4; ++i){
    for(let j = 0; j < 4; ++j){
      let c = 0;
      for(let k = 0; k < 4; ++k){
        c += A[4*i+k] * B[4*k+j]
      }
      C.push(c);
    }
  }
  return C
}

const update = (e) => {
  const T1 = getTranslation(xEl.value, yEl.value)
  const T2 = getXRotation(angleEl.value * Math.PI / 180)
  const T3 = getScale(sxEl.value, syEl.value)
  let T = '';
  if(premul.checked){
    T = `matrix3d(${
      mat4mul(mat4mul(T1,T2),T3)
    })`
    resultEl.style.backgroundColor = 'yellow';
  }else{
    T = `
    matrix3d(${T1})
    matrix3d(${T2})
    matrix3d(${T3})
    `
    resultEl.style.backgroundColor = 'gray'
  }
  resultEl.style.transform = T;
}

angleEl.addEventListener('input', update)
xEl.addEventListener('input', update)
yEl.addEventListener('input', update)
sxEl.addEventListener('input', update)
syEl.addEventListener('input', update)
premulEl.addEventListener('input', update)

div#result {
 background-color: gray;
 border: 2px solid black;
 color: black;
 width: 3cm; 
 height: 2cm;
}

<label>Rotation angle <input type=range id=angle min=0 max=360 value=0>
</label><br>
<label>Translation X <input type=range id=x min=0 max=360 value=0></label><br>
<label>Translation Y <input type=range id=y min=0 max=360 value=0></label><br>
<label>Scale X <input type=range id=sx min=0.5 max=2 value=1 step=0.01></label><br>
<label>Scale Y <input type=range id=sy min=0.5 max=2 value=1 step=0.01></label><br>
<label><input type=checkbox id=premul> Multiply matrices

<br>
<br>
<br>
<br>
<div id=result>Result</div>

这篇关于JS 的线性变换和矩阵乘法失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆