You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
8.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: Transform.cpp
  9. // AUTHOR: Belkovsky Joseph
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of Transform.cpp
  13. //----------------------------------------------------------------------------
  14. // Transform.cpp : Defines the entry point for the console application.
  15. //
  16. #include "Transform.h"
  17. void spherical2decart(C3D_SPH_DEGREES in, C3D_DECART &out){
  18. C3D_FLOAT tT, tF;
  19. tT = deg2rad(in.tetta);
  20. tF = deg2rad(in.fi);
  21. out.x = in.r * C3D_SIN(tT) * C3D_COS(tF);
  22. out.y = in.r * C3D_SIN(tT) * C3D_SIN(tF);
  23. out.z = in.r * C3D_COS(tT);
  24. };
  25. void decart2spherical(C3D_DECART in, C3D_SPH_DEGREES &out){
  26. unsigned char oct;
  27. out.r = C3D_SQRT(C3D_POW(in.x, 2.f) + C3D_POW(in.y, 2.f) + C3D_POW(in.z, 2.f));
  28. out.fi = rad2deg(C3D_ATAN(in.y / in.x));
  29. out.tetta = rad2deg(C3D_ACOS(in.z / out.r));
  30. oct = ((in.x < 0.f) ? 1 : 0) | ((in.y < 0.f) ? 2 : 0) | ((in.z < 0.f) ? 4 : 0);
  31. switch (oct){
  32. case 0:
  33. case 4: break;
  34. case 1:
  35. case 3:
  36. case 5:
  37. case 7:
  38. out.fi += 180.f;
  39. break;
  40. case 2:
  41. case 6:
  42. out.fi += 360.f;
  43. break;
  44. };
  45. };
  46. void spherical2decart(C3D_SPH_RADIANS in, C3D_DECART &out){
  47. out.x = in.r * C3D_SIN(in.tetta) * C3D_COS(in.fi);
  48. out.y = in.r * C3D_SIN(in.tetta) * C3D_SIN(in.fi);
  49. out.z = in.r * C3D_COS(in.tetta);
  50. };
  51. void decart2spherical(C3D_DECART in, C3D_SPH_RADIANS &out){
  52. unsigned char oct;
  53. out.r = C3D_SQRT(C3D_POW(in.x, 2.f) + C3D_POW(in.y, 2.f) + C3D_POW(in.z, 2.f));
  54. out.fi = C3D_ATAN(in.y / in.x);
  55. out.tetta = C3D_PI2 - C3D_ACOS(in.z / out.r);
  56. oct = ((in.x < 0.f) ? 1 : 0) | ((in.y < 0.f) ? 2 : 0) | ((in.z < 0.f) ? 4 : 0);
  57. switch (oct){
  58. case 0:
  59. case 4: break;
  60. case 1:
  61. case 3:
  62. case 5:
  63. case 7:
  64. out.fi += C3D_PI;
  65. break;
  66. case 2:
  67. case 6:
  68. out.fi += C3D_2PI;
  69. break;
  70. };
  71. };
  72. /*
  73. void translate2local(S_TRANS_MATRIX mat, C3D_DECART world, C3D_DECART &local){
  74. S_TRANS_MATRIX tMat;
  75. transponeMatrix(mat, tMat);
  76. local.x = world.x * tMat.AA + world.y * tMat.AB + world.z * tMat.AC + tMat.AD;
  77. local.y = world.x * tMat.BA + world.y * tMat.BB + world.z * tMat.BC + tMat.BD;
  78. local.z = world.x * tMat.CA + world.y * tMat.CB + world.z * tMat.CC + tMat.CD;
  79. };
  80. void translate2world(S_TRANS_MATRIX tMat, C3D_DECART local, C3D_DECART &world){
  81. world.x = local.x * tMat.AA + local.y * tMat.AB + local.z * tMat.AC + tMat.AD;
  82. world.y = local.x * tMat.BA + local.y * tMat.BB + local.z * tMat.BC + tMat.BD;
  83. world.z = local.x * tMat.CA + local.y * tMat.CB + local.z * tMat.CC + tMat.CD;
  84. };
  85. */
  86. void translate2local(S_TRANS_MATRIX mat, C3D_DECART world, C3D_DECART &local){
  87. S_TRANS_MATRIX tMat;
  88. transform_3 *trHelper;
  89. transponeMatrix(mat, tMat);
  90. local.x = world.x * tMat.AA + world.y * tMat.AB + world.z * tMat.AC + tMat.AD;
  91. local.y = world.x * tMat.BA + world.y * tMat.BB + world.z * tMat.BC + tMat.BD;
  92. local.z = world.x * tMat.CA + world.y * tMat.CB + world.z * tMat.CC + tMat.CD;
  93. };
  94. void translate2world(S_TRANS_MATRIX tMat, C3D_DECART local, C3D_DECART &world){
  95. world.x = local.x * tMat.AA + local.y * tMat.AB + local.z * tMat.AC + tMat.AD;
  96. world.y = local.x * tMat.BA + local.y * tMat.BB + local.z * tMat.BC + tMat.BD;
  97. world.z = local.x * tMat.CA + local.y * tMat.CB + local.z * tMat.CC + tMat.CD;
  98. };
  99. void transponeMatrix(S_TRANS_MATRIX in, S_TRANS_MATRIX &out){
  100. out.AA = in.AA; out.AB = in.BA; out.AC = in.CA; out.AD = in.DA;
  101. out.BA = in.AB; out.BB = in.BB; out.BC = in.CB; out.BD = in.DB;
  102. out.CA = in.AC; out.CB = in.BC; out.CC = in.CC; out.CD = in.DC;
  103. out.DA = in.AD; out.DB = in.BD; out.DC = in.CD; out.DD = in.DD;
  104. };
  105. const C3D_FLOAT transform_3::PI_M = 3.141592653589793;
  106. const C3D_FLOAT transform_3::DTOR = 0.017453292519943;
  107. const C3D_FLOAT transform_3::RTOD = 57.29577951308232;
  108. vector_3::vector_3(C3D_FLOAT a0, C3D_FLOAT a1, C3D_FLOAT a2)
  109. {
  110. elem_[0] = a0;
  111. elem_[1] = a1;
  112. elem_[2] = a2;
  113. }
  114. matrix_3::matrix_3(C3D_FLOAT a00, C3D_FLOAT a01, C3D_FLOAT a02,
  115. C3D_FLOAT a10, C3D_FLOAT a11, C3D_FLOAT a12,
  116. C3D_FLOAT a20, C3D_FLOAT a21, C3D_FLOAT a22)
  117. {
  118. elem_[0][0] = a00; elem_[0][1] = a01; elem_[0][2] = a02;
  119. elem_[1][0] = a10; elem_[1][1] = a11; elem_[1][2] = a12;
  120. elem_[2][0] = a20; elem_[2][1] = a21; elem_[2][2] = a22;
  121. }
  122. matrix_3 matrix_3::operator * (const matrix_3& matrix) const
  123. {
  124. matrix_3 result;
  125. for(uint8_t ii = 0; ii < 3; ++ii)
  126. for(uint8_t jj = 0; jj < 3; ++jj)
  127. for(uint8_t kk = 0; kk < 3; ++kk)
  128. result.elem_[ii][jj] += elem_[ii][kk] * matrix.elem_[kk][jj];
  129. return result;
  130. }
  131. matrix_3 matrix_3::operator ~ () const
  132. {
  133. matrix_3 result;
  134. for (uint8_t ii = 0; ii < 3; ++ii)
  135. for (uint8_t jj = 0; jj < 3; ++jj)
  136. result.elem_[ii][jj] = elem_[jj][ii];
  137. return result;
  138. }
  139. vector_3 matrix_3::operator * (const vector_3& vector) const
  140. {
  141. vector_3 result;
  142. for(uint8_t ii = 0; ii < 3; ++ii)
  143. for(uint8_t jj = 0; jj < 3; ++jj)
  144. result[ii] += elem_[ii][jj] * vector[jj];
  145. return result;
  146. }
  147. vector_3 operator * (const vector_3& vector, const matrix_3& matrix)
  148. {
  149. vector_3 result;
  150. for(uint8_t ii = 0; ii < 3; ++ii)
  151. for(uint8_t jj = 0; jj < 3; ++jj)
  152. result[ii] += matrix.elem_[jj][ii] * vector[jj];
  153. return result;
  154. }
  155. matrix_3 operator * (C3D_FLOAT scalar, const matrix_3& matrix)
  156. {
  157. matrix_3 result = matrix;
  158. for(uint8_t ii = 0; ii < 3; ++ii)
  159. for(uint8_t jj = 0; jj < 3; ++jj)
  160. result.elem_[ii][jj] *= scalar;
  161. return result;
  162. }
  163. /*
  164. Cannonical Transform
  165. transform_3::transform_3(C3D_FLOAT Xc, C3D_FLOAT Yc, C3D_FLOAT Zc,
  166. C3D_FLOAT Psicd, C3D_FLOAT Thetacd, C3D_FLOAT Phicd) :
  167. // Rotation by Phic matrix (X)
  168. core_matrix_(
  169. 1, 0, 0,
  170. 0, C3D_COS(Phicd), -C3D_SIN(Phicd),
  171. 0, C3D_SIN(Phicd), C3D_COS(Phicd)
  172. ),
  173. core_vector_( Xc, Yc, Zc ) {
  174. // Rotation by Thetac matrix (Y)
  175. core_matrix_ *= matrix_3(C3D_COS(Thetacd), 0, C3D_SIN(Thetacd),
  176. 0, 1, 0,
  177. -C3D_SIN(Thetacd), 0, C3D_COS(Thetacd));
  178. // Rotation by Psic matrix (Z)
  179. core_matrix_ *= matrix_3(C3D_COS(Psicd), -C3D_SIN(Psicd), 0,
  180. C3D_SIN(Psicd), C3D_COS(Psicd), 0,
  181. 0, 0, 1);
  182. }
  183. */
  184. transform_3::transform_3(C3D_FLOAT Xc, C3D_FLOAT Yc, C3D_FLOAT Zc,
  185. C3D_FLOAT Psicd, C3D_FLOAT Thetacd, C3D_FLOAT Phicd) :
  186. // Rotation by Phic matrix (X)
  187. core_matrix_(
  188. 1, 0, 0,
  189. 0, C3D_COS(Thetacd), -C3D_SIN(Thetacd),
  190. 0, C3D_SIN(Thetacd), C3D_COS(Thetacd)
  191. ),
  192. core_vector_( Xc, Yc, Zc ) {
  193. // Rotation by Thetac matrix (Y)
  194. core_matrix_ *= matrix_3(C3D_COS(Psicd), 0, C3D_SIN(Psicd),
  195. 0, 1, 0,
  196. -C3D_SIN(Psicd), 0, C3D_COS(Psicd));
  197. // Rotation by Psic matrix (Z)
  198. core_matrix_ *= matrix_3(C3D_COS(Phicd), -C3D_SIN(Phicd), 0,
  199. C3D_SIN(Phicd), C3D_COS(Phicd), 0,
  200. 0, 0, 1);
  201. }
  202. void transform_3::get_ptz_params(C3D_FLOAT angle, C3D_FLOAT theta_t_s, C3D_FLOAT range, ptz_params& params){
  203. //double theta_t_s = .0; // Assume that the tilt on the target is 0[deg].
  204. // Latter we can put as the tilt of the LRF
  205. // Transformation to camera system, r_t_c = Target vector in camera system [m]
  206. // Convert from Spherical to Decart
  207. vector_3 r_t_c = core_matrix_ * (range * vector_3( C3D_COS(DTOR * angle) * C3D_COS(DTOR * theta_t_s),
  208. C3D_SIN(DTOR * angle) * C3D_COS(DTOR * theta_t_s),
  209. // then Move basis by core_vector_
  210. -C3D_SIN(DTOR * theta_t_s) ) - core_vector_ );
  211. // Convert back Decart to Spherical
  212. params.pan = static_cast<float> (RTOD * C3D_ATAN2(r_t_c[1], r_t_c[0])); // Pan, in degree
  213. params.tilt = static_cast<float> (RTOD * C3D_ATAN2(-r_t_c[2], sqrt(r_t_c[0] * r_t_c[0] + r_t_c[1] * r_t_c[1]))); // Tilt, in degree
  214. params.dist = static_cast<float> (sqrt(r_t_c * r_t_c)); // Range, in meters
  215. }
  216. void transform_3::get_vector_world( vector_3 target, vector_3 &param ){
  217. param = ( core_matrix_ * target ) + core_vector_ ;
  218. };
  219. void transform_3::get_vector_local( vector_3 target, vector_3 &param ){
  220. param = ( ~core_matrix_ ) * ( target - core_vector_ );
  221. };