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.

345 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
5 years ago
​
7 years ago
​
​
​
​
​
​
5 years ago
7 years ago
​
​
5 years ago
7 years ago
​
​
​
​
​
5 years ago
​
7 years ago
​
​
​
​
​
​
5 years ago
7 years ago
​
​
5 years ago
7 years ago
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
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. #ifdef DEBUG
  18. #include "../../components/base/basicLogCollection.h"
  19. #define LOG( X ) basicLogCollection::getInstance()->Log( "TRANS", NULL, X )
  20. #define LOG_ERROR( X ) {\
  21. char _tBfrErrMSG2w34[256];\
  22. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  23. basicLogCollection::getInstance()->Log( "TRANS", NULL, _LOG_ERROR, _tBfrErrMSG2w34 );\
  24. }
  25. #endif
  26. void spherical2decart(C3D_SPH_DEGREES in, C3D_DECART &out){
  27. C3D_FLOAT tT, tF;
  28. tT = deg2rad(in.tetta);
  29. tF = deg2rad(in.fi);
  30. #ifdef DEBUG_9908
  31. char tB[128];
  32. sprintf( tB, "IN D> R:%0.5f T:%0.5f F:%0.5f", in.r, in.tetta, in.fi );
  33. LOG( tB );
  34. sprintf( tB, "IN R> R:%0.5f T:%0.5f F:%0.5f", in.r, tT, tF );
  35. LOG( tB );
  36. #endif
  37. out.x = in.r * C3D_COS(tF) * C3D_SIN(tT);
  38. out.y = in.r * C3D_SIN(tF) * C3D_SIN(tT);
  39. out.z = in.r * C3D_COS(tT);
  40. #ifdef DEBUG_8898
  41. sprintf( tB, "OUT> X:%0.5f Y:%0.5f Z:%0.5f", out.x, out.y, out.z );
  42. LOG( tB );
  43. #endif
  44. };
  45. void decart2spherical(C3D_DECART in, C3D_SPH_DEGREES &out){
  46. unsigned char oct;
  47. out.r = C3D_SQRT(C3D_POW(in.x, 2.0f) + C3D_POW(in.y, 2.0f) + C3D_POW(in.z, 2.0f));
  48. out.fi = rad2deg(C3D_ATAN(in.y / in.x));
  49. out.tetta = rad2deg(C3D_ACOS(in.z / out.r));
  50. oct = ((in.x < 0.0f) ? 1 : 0) | ((in.y < 0.0f) ? 2 : 0) | ((in.z < 0.0f) ? 4 : 0);
  51. switch (oct){
  52. case 0:
  53. case 4: break;
  54. case 1:
  55. case 3:
  56. case 5:
  57. case 7:
  58. out.fi += 180.0f;
  59. break;
  60. case 2:
  61. case 6:
  62. out.fi += 360.0f;
  63. break;
  64. };
  65. };
  66. void decart2spherical(C3D_DECART in, C3D_SPH_RADIANS &out){
  67. unsigned char oct;
  68. out.r = C3D_SQRT(C3D_POW(in.x, 2.0f) + C3D_POW(in.y, 2.0f) + C3D_POW(in.z, 2.0f));
  69. out.fi = C3D_ATAN(in.y / in.x);
  70. out.tetta = C3D_PI2 - C3D_ACOS(in.z / out.r);
  71. oct = ((in.x < 0.0f) ? 1 : 0) | ((in.y < 0.0f) ? 2 : 0) | ((in.z < 0.0f) ? 4 : 0);
  72. switch (oct){
  73. case 0:
  74. case 4: break;
  75. case 1:
  76. case 3:
  77. case 5:
  78. case 7:
  79. out.fi += C3D_PI;
  80. break;
  81. case 2:
  82. case 6:
  83. out.fi += C3D_2PI;
  84. break;
  85. };
  86. };
  87. void spherical2decart(C3D_SPH_RADIANS in, C3D_DECART &out){
  88. out.x = in.r * C3D_COS(in.fi) * C3D_SIN(in.tetta);
  89. out.y = in.r * C3D_SIN(in.fi) * C3D_SIN(in.tetta);
  90. out.z = in.r * C3D_COS(in.tetta);
  91. };
  92. void spherical2spherical(C3D_SPH_DEGREES in, C3D_SPH_RADIANS &out){
  93. out.fi = deg2rad(in.fi);
  94. out.tetta = deg2rad(in.tetta);
  95. out.r = in.r;
  96. };
  97. void spherical2spherical(C3D_SPH_RADIANS in, C3D_SPH_DEGREES &out){
  98. out.fi = rad2deg(in.fi);
  99. out.tetta = rad2deg(in.tetta);
  100. out.r = in.r;
  101. };
  102. void spherical2onvif(C3D_SPH_DEGREES in, C3D_ORIENT_ONVIF &out, float maxR, uint8_t panMode, float panAngles, uint8_t tiltMode, float tiltAngles){
  103. out.zoom = .0f;
  104. out.tilt = .0f;
  105. out.pan = .0f;
  106. if( maxR > 0.0f ) {
  107. if( in.r > maxR ) {
  108. out.zoom = 1.0f;
  109. } else {
  110. out.zoom = in.r / maxR;
  111. };
  112. } else out.zoom = .0f;
  113. switch( panMode ){
  114. case 1:{
  115. out.pan = 1.0f - (( 2.0f / panAngles ) * in.fi );
  116. break;
  117. };
  118. case 2:
  119. default:{
  120. out.pan = ( 2.0f / panAngles ) * in.fi - 1.0f;
  121. };
  122. };
  123. switch( tiltMode ) {
  124. case 0:{
  125. out.tilt = -2.0f * ( ( tiltAngles - in.tetta ) / tiltAngles ) + 1.0f;
  126. break;
  127. };
  128. case 1:{
  129. out.tilt = -2.0f * ( ( tiltAngles - in.tetta ) / tiltAngles ) + 1.0f;
  130. break;
  131. };
  132. case 2:
  133. default:{
  134. out.tilt = ( 2.0f / tiltAngles ) * ( tiltAngles - in.tetta ) - 1.0f;
  135. break;
  136. };
  137. };
  138. };
  139. void spherical2onvif( C3D_SPH_RADIANS inr, C3D_ORIENT_ONVIF &out, float maxR, uint8_t panMode, float panAngles, uint8_t tiltMode, float tiltAngles ){
  140. C3D_SPH_DEGREES in;
  141. spherical2spherical(inr, in);
  142. spherical2onvif( in, out, maxR, panMode, panAngles, tiltMode, tiltAngles);
  143. };
  144. /*
  145. void translate2local(S_TRANS_MATRIX mat, C3D_DECART world, C3D_DECART &local){
  146. S_TRANS_MATRIX tMat;
  147. transponeMatrix(mat, tMat);
  148. local.x = world.x * tMat.AA + world.y * tMat.AB + world.z * tMat.AC + tMat.AD;
  149. local.y = world.x * tMat.BA + world.y * tMat.BB + world.z * tMat.BC + tMat.BD;
  150. local.z = world.x * tMat.CA + world.y * tMat.CB + world.z * tMat.CC + tMat.CD;
  151. };
  152. void translate2world(S_TRANS_MATRIX tMat, C3D_DECART local, C3D_DECART &world){
  153. world.x = local.x * tMat.AA + local.y * tMat.AB + local.z * tMat.AC + tMat.AD;
  154. world.y = local.x * tMat.BA + local.y * tMat.BB + local.z * tMat.BC + tMat.BD;
  155. world.z = local.x * tMat.CA + local.y * tMat.CB + local.z * tMat.CC + tMat.CD;
  156. };
  157. */
  158. void translate2local(S_TRANS_MATRIX mat, C3D_DECART world, C3D_DECART &local){
  159. S_TRANS_MATRIX tMat;
  160. //transform_3 *trHelper;
  161. transponeMatrix(mat, tMat);
  162. local.x = world.x * tMat.AA + world.y * tMat.AB + world.z * tMat.AC + tMat.AD;
  163. local.y = world.x * tMat.BA + world.y * tMat.BB + world.z * tMat.BC + tMat.BD;
  164. local.z = world.x * tMat.CA + world.y * tMat.CB + world.z * tMat.CC + tMat.CD;
  165. };
  166. void translate2world(S_TRANS_MATRIX tMat, C3D_DECART local, C3D_DECART &world){
  167. world.x = local.x * tMat.AA + local.y * tMat.AB + local.z * tMat.AC + tMat.AD;
  168. world.y = local.x * tMat.BA + local.y * tMat.BB + local.z * tMat.BC + tMat.BD;
  169. world.z = local.x * tMat.CA + local.y * tMat.CB + local.z * tMat.CC + tMat.CD;
  170. };
  171. void transponeMatrix(S_TRANS_MATRIX in, S_TRANS_MATRIX &out){
  172. out.AA = in.AA; out.AB = in.BA; out.AC = in.CA; out.AD = in.DA;
  173. out.BA = in.AB; out.BB = in.BB; out.BC = in.CB; out.BD = in.DB;
  174. out.CA = in.AC; out.CB = in.BC; out.CC = in.CC; out.CD = in.DC;
  175. out.DA = in.AD; out.DB = in.BD; out.DC = in.CD; out.DD = in.DD;
  176. };
  177. const C3D_FLOAT transform_3::PI_M = 3.141592653589793f;
  178. const C3D_FLOAT transform_3::DTOR = 0.017453292519943f;
  179. const C3D_FLOAT transform_3::RTOD = 57.29577951308232f;
  180. vector_3::vector_3(C3D_FLOAT a0, C3D_FLOAT a1, C3D_FLOAT a2)
  181. {
  182. elem_[0] = a0;
  183. elem_[1] = a1;
  184. elem_[2] = a2;
  185. }
  186. matrix_3::matrix_3(C3D_FLOAT a00, C3D_FLOAT a01, C3D_FLOAT a02,
  187. C3D_FLOAT a10, C3D_FLOAT a11, C3D_FLOAT a12,
  188. C3D_FLOAT a20, C3D_FLOAT a21, C3D_FLOAT a22)
  189. {
  190. elem_[0][0] = a00; elem_[0][1] = a01; elem_[0][2] = a02;
  191. elem_[1][0] = a10; elem_[1][1] = a11; elem_[1][2] = a12;
  192. elem_[2][0] = a20; elem_[2][1] = a21; elem_[2][2] = a22;
  193. }
  194. matrix_3 matrix_3::operator * (const matrix_3& matrix) const
  195. {
  196. matrix_3 result;
  197. for(uint8_t ii = 0; ii < 3; ++ii)
  198. for(uint8_t jj = 0; jj < 3; ++jj)
  199. for(uint8_t kk = 0; kk < 3; ++kk)
  200. result.elem_[ii][jj] += elem_[ii][kk] * matrix.elem_[kk][jj];
  201. return result;
  202. }
  203. matrix_3 matrix_3::operator ~ () const
  204. {
  205. matrix_3 result;
  206. for (uint8_t ii = 0; ii < 3; ++ii)
  207. for (uint8_t jj = 0; jj < 3; ++jj)
  208. result.elem_[ii][jj] = elem_[jj][ii];
  209. return result;
  210. }
  211. vector_3 matrix_3::operator * (const vector_3& vector) const
  212. {
  213. vector_3 result;
  214. for(uint8_t ii = 0; ii < 3; ++ii)
  215. for(uint8_t jj = 0; jj < 3; ++jj)
  216. result[ii] += (float)elem_[ii][jj] * vector[jj];
  217. return result;
  218. }
  219. vector_3 operator * (const vector_3& vector, const matrix_3& matrix)
  220. {
  221. vector_3 result;
  222. for(uint8_t ii = 0; ii < 3; ++ii)
  223. for(uint8_t jj = 0; jj < 3; ++jj)
  224. result[ii] += (float)matrix.elem_[jj][ii] * vector[jj];
  225. return result;
  226. }
  227. matrix_3 operator * (C3D_FLOAT scalar, const matrix_3& matrix)
  228. {
  229. matrix_3 result = matrix;
  230. for(uint8_t ii = 0; ii < 3; ++ii)
  231. for(uint8_t jj = 0; jj < 3; ++jj)
  232. result.elem_[ii][jj] *= scalar;
  233. return result;
  234. }
  235. /*
  236. Cannonical Transform
  237. transform_3::transform_3(C3D_FLOAT Xc, C3D_FLOAT Yc, C3D_FLOAT Zc,
  238. C3D_FLOAT Psicd, C3D_FLOAT Thetacd, C3D_FLOAT Phicd) :
  239. // Rotation by Phic matrix (X)
  240. core_matrix_(
  241. 1, 0, 0,
  242. 0, C3D_COS(Phicd), -C3D_SIN(Phicd),
  243. 0, C3D_SIN(Phicd), C3D_COS(Phicd)
  244. ),
  245. core_vector_( Xc, Yc, Zc ) {
  246. // Rotation by Thetac matrix (Y)
  247. core_matrix_ *= matrix_3(C3D_COS(Thetacd), 0, C3D_SIN(Thetacd),
  248. 0, 1, 0,
  249. -C3D_SIN(Thetacd), 0, C3D_COS(Thetacd));
  250. // Rotation by Psic matrix (Z)
  251. core_matrix_ *= matrix_3(C3D_COS(Psicd), -C3D_SIN(Psicd), 0,
  252. C3D_SIN(Psicd), C3D_COS(Psicd), 0,
  253. 0, 0, 1);
  254. }
  255. */
  256. transform_3::transform_3(C3D_FLOAT Xc, C3D_FLOAT Yc, C3D_FLOAT Zc,
  257. C3D_FLOAT Psicd, C3D_FLOAT Thetacd, C3D_FLOAT Phicd) :
  258. // Rotation by Phic matrix (X)
  259. core_matrix_(
  260. 1, 0, 0,
  261. 0, C3D_COS(Thetacd), -C3D_SIN(Thetacd),
  262. 0, C3D_SIN(Thetacd), C3D_COS(Thetacd)
  263. ),
  264. core_vector_( Xc, Yc, Zc ) {
  265. // Rotation by Thetac matrix (Y)
  266. core_matrix_ *= matrix_3(C3D_COS(Psicd), 0, C3D_SIN(Psicd),
  267. 0, 1, 0,
  268. -C3D_SIN(Psicd), 0, C3D_COS(Psicd));
  269. // Rotation by Psic matrix (Z)
  270. core_matrix_ *= matrix_3(C3D_COS(Phicd), -C3D_SIN(Phicd), 0,
  271. C3D_SIN(Phicd), C3D_COS(Phicd), 0,
  272. 0, 0, 1);
  273. }
  274. void transform_3::get_ptz_params(C3D_FLOAT angle, C3D_FLOAT theta_t_s, C3D_FLOAT range, ptz_params& params){
  275. //double theta_t_s = .0; // Assume that the tilt on the target is 0[deg].
  276. // Latter we can put as the tilt of the LRF
  277. // Transformation to camera system, r_t_c = Target vector in camera system [m]
  278. // Convert from Spherical to Decart
  279. vector_3 r_t_c = core_matrix_ * (range * vector_3( C3D_COS(DTOR * angle) * C3D_COS(DTOR * theta_t_s),
  280. C3D_SIN(DTOR * angle) * C3D_COS(DTOR * theta_t_s),
  281. // then Move basis by core_vector_
  282. -C3D_SIN(DTOR * theta_t_s) ) - core_vector_ );
  283. // Convert back Decart to Spherical
  284. params.pan = static_cast<float> (RTOD * C3D_ATAN2(r_t_c[1], r_t_c[0])); // Pan, in degree
  285. 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
  286. params.dist = static_cast<float> (sqrt(r_t_c * r_t_c)); // Range, in meters
  287. }
  288. void transform_3::get_vector_world( vector_3 target, vector_3 &param ){
  289. param = ( core_matrix_ * target ) + core_vector_ ;
  290. };
  291. void transform_3::get_vector_local( vector_3 target, vector_3 &param ){
  292. param = ( ~core_matrix_ ) * ( target - core_vector_ );
  293. };
  294. #ifdef DEBUG
  295. #undef LOG
  296. #undef LOG_ERROR
  297. #endif