Transform.pde 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. class Transform {
  2. float[] inv_rot; // inverse of rotation matrix
  3. PVector inv_mov; // inverse of movement vector
  4. float focal; // the focal distacne of real camera
  5. int w, h; // the width and height of the frame
  6. float normalier; // nomalization factor of depth
  7. Transform(float tx, float ty, float tz, float qx, float qy, float qz,
  8. float qw, float fov, int w, int h, float normalier) {
  9. // currently, we did not use the info of real camera's position and
  10. // quaternion maybe we will use it in the future when combine all frames
  11. float[] rot = quaternion2Mat3x3(qx, qy, qz, qw);
  12. inv_rot = transpose3x3(rot);
  13. inv_mov = new PVector(-tx, -ty, -tz);
  14. this.focal = 0.5f * h / tan(fov / 2.0);
  15. this.w = w;
  16. this.h = h;
  17. this.normalier = normalier;
  18. }
  19. PVector transform(int i, int j, float d) {
  20. // transfer from camera view to world view
  21. float z = d / normalier;
  22. float x = (i - w / 2.0f) * z / focal;
  23. float y = (j - h / 2.0f) * z / focal;
  24. return new PVector(x, y, z);
  25. }
  26. }
  27. // get rotation matrix by using rotation axis and angle
  28. float[] getRotationMat3x3(float angle, float ax, float ay, float az) {
  29. float[] mat = new float[9];
  30. float c = cos(angle);
  31. float s = sin(angle);
  32. mat[0] = c + ax * ax * (1 - c);
  33. mat[1] = ax * ay * (1 - c) - az * s;
  34. mat[2] = ax * az * (1 - c) + ay * s;
  35. mat[3] = ay * ax * (1 - c) + az * s;
  36. mat[4] = c + ay * ay * (1 - c);
  37. mat[5] = ay * az * (1 - c) - ax * s;
  38. mat[6] = az * ax * (1 - c) - ay * s;
  39. mat[7] = az * ay * (1 - c) + ax * s;
  40. mat[8] = c + az * az * (1 - c);
  41. return mat;
  42. }
  43. // get rotation matrix by using quaternion
  44. float[] quaternion2Mat3x3(float qx, float qy, float qz, float qw) {
  45. float[] mat = new float[9];
  46. mat[0] = 1 - 2 * qy * qy - 2 * qz * qz;
  47. mat[1] = 2 * qx * qy - 2 * qz * qw;
  48. mat[2] = 2 * qx * qz + 2 * qy * qw;
  49. mat[3] = 2 * qx * qy + 2 * qz * qw;
  50. mat[4] = 1 - 2 * qx * qx - 2 * qz * qz;
  51. mat[5] = 2 * qy * qz - 2 * qx * qw;
  52. mat[6] = 2 * qx * qz - 2 * qy * qw;
  53. mat[7] = 2 * qy * qz + 2 * qx * qw;
  54. mat[8] = 1 - 2 * qx * qx - 2 * qy * qy;
  55. return mat;
  56. }
  57. // tranpose a 3x3 matrix
  58. float[] transpose3x3(float[] mat) {
  59. float[] Tmat = new float[9];
  60. for (int i = 0; i < 3; i++)
  61. for (int j = 0; j < 3; j++) {
  62. Tmat[i * 3 + j] = mat[j * 3 + i];
  63. }
  64. return Tmat;
  65. }
  66. // multiply a matrix with vector
  67. PVector MatxVec3(float[] mat, PVector v) {
  68. float[] vec = v.array();
  69. float[] res = new float[3];
  70. for (int i = 0; i < 3; i++) {
  71. res[i] = 0.0f;
  72. for (int j = 0; j < 3; j++) {
  73. res[i] += mat[i * 3 + j] * vec[j];
  74. }
  75. }
  76. return new PVector(res[0], res[1], res[2]);
  77. }