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.

341 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 = ( 2.0f / panAngles ) * in.fi - 1.0f;
  116. break;
  117. };
  118. case 2:
  119. default:{
  120. out.pan = ( 2.0f / 360.0f ) * in.fi - 1.0f;
  121. };
  122. };
  123. switch( tiltMode ) {
  124. case 1:{
  125. out.tilt = -2.0f * ( ( tiltAngles - in.tetta ) / tiltAngles ) + 1.0f;
  126. break;
  127. };
  128. case 2:
  129. default:{
  130. out.tilt = ( 2.0f / tiltAngles ) * ( tiltAngles - in.tetta ) - 1.0f;
  131. break;
  132. };
  133. };
  134. };
  135. void spherical2onvif( C3D_SPH_RADIANS inr, C3D_ORIENT_ONVIF &out, float maxR, uint8_t panMode, float panAngles, uint8_t tiltMode, float tiltAngles ){
  136. C3D_SPH_DEGREES in;
  137. spherical2spherical(inr, in);
  138. spherical2onvif( in, out, maxR, panMode, panAngles, tiltMode, tiltAngles);
  139. };
  140. /*
  141. void translate2local(S_TRANS_MATRIX mat, C3D_DECART world, C3D_DECART &local){
  142. S_TRANS_MATRIX tMat;
  143. transponeMatrix(mat, tMat);
  144. local.x = world.x * tMat.AA + world.y * tMat.AB + world.z * tMat.AC + tMat.AD;
  145. local.y = world.x * tMat.BA + world.y * tMat.BB + world.z * tMat.BC + tMat.BD;
  146. local.z = world.x * tMat.CA + world.y * tMat.CB + world.z * tMat.CC + tMat.CD;
  147. };
  148. void translate2world(S_TRANS_MATRIX tMat, C3D_DECART local, C3D_DECART &world){
  149. world.x = local.x * tMat.AA + local.y * tMat.AB + local.z * tMat.AC + tMat.AD;
  150. world.y = local.x * tMat.BA + local.y * tMat.BB + local.z * tMat.BC + tMat.BD;
  151. world.z = local.x * tMat.CA + local.y * tMat.CB + local.z * tMat.CC + tMat.CD;
  152. };
  153. */
  154. void translate2local(S_TRANS_MATRIX mat, C3D_DECART world, C3D_DECART &local){
  155. S_TRANS_MATRIX tMat;
  156. //transform_3 *trHelper;
  157. transponeMatrix(mat, tMat);
  158. local.x = world.x * tMat.AA + world.y * tMat.AB + world.z * tMat.AC + tMat.AD;
  159. local.y = world.x * tMat.BA + world.y * tMat.BB + world.z * tMat.BC + tMat.BD;
  160. local.z = world.x * tMat.CA + world.y * tMat.CB + world.z * tMat.CC + tMat.CD;
  161. };
  162. void translate2world(S_TRANS_MATRIX tMat, C3D_DECART local, C3D_DECART &world){
  163. world.x = local.x * tMat.AA + local.y * tMat.AB + local.z * tMat.AC + tMat.AD;
  164. world.y = local.x * tMat.BA + local.y * tMat.BB + local.z * tMat.BC + tMat.BD;
  165. world.z = local.x * tMat.CA + local.y * tMat.CB + local.z * tMat.CC + tMat.CD;
  166. };
  167. void transponeMatrix(S_TRANS_MATRIX in, S_TRANS_MATRIX &out){
  168. out.AA = in.AA; out.AB = in.BA; out.AC = in.CA; out.AD = in.DA;
  169. out.BA = in.AB; out.BB = in.BB; out.BC = in.CB; out.BD = in.DB;
  170. out.CA = in.AC; out.CB = in.BC; out.CC = in.CC; out.CD = in.DC;
  171. out.DA = in.AD; out.DB = in.BD; out.DC = in.CD; out.DD = in.DD;
  172. };
  173. const C3D_FLOAT transform_3::PI_M = 3.141592653589793f;
  174. const C3D_FLOAT transform_3::DTOR = 0.017453292519943f;
  175. const C3D_FLOAT transform_3::RTOD = 57.29577951308232f;
  176. vector_3::vector_3(C3D_FLOAT a0, C3D_FLOAT a1, C3D_FLOAT a2)
  177. {
  178. elem_[0] = a0;
  179. elem_[1] = a1;
  180. elem_[2] = a2;
  181. }
  182. matrix_3::matrix_3(C3D_FLOAT a00, C3D_FLOAT a01, C3D_FLOAT a02,
  183. C3D_FLOAT a10, C3D_FLOAT a11, C3D_FLOAT a12,
  184. C3D_FLOAT a20, C3D_FLOAT a21, C3D_FLOAT a22)
  185. {
  186. elem_[0][0] = a00; elem_[0][1] = a01; elem_[0][2] = a02;
  187. elem_[1][0] = a10; elem_[1][1] = a11; elem_[1][2] = a12;
  188. elem_[2][0] = a20; elem_[2][1] = a21; elem_[2][2] = a22;
  189. }
  190. matrix_3 matrix_3::operator * (const matrix_3& matrix) const
  191. {
  192. matrix_3 result;
  193. for(uint8_t ii = 0; ii < 3; ++ii)
  194. for(uint8_t jj = 0; jj < 3; ++jj)
  195. for(uint8_t kk = 0; kk < 3; ++kk)
  196. result.elem_[ii][jj] += elem_[ii][kk] * matrix.elem_[kk][jj];
  197. return result;
  198. }
  199. matrix_3 matrix_3::operator ~ () const
  200. {
  201. matrix_3 result;
  202. for (uint8_t ii = 0; ii < 3; ++ii)
  203. for (uint8_t jj = 0; jj < 3; ++jj)
  204. result.elem_[ii][jj] = elem_[jj][ii];
  205. return result;
  206. }
  207. vector_3 matrix_3::operator * (const vector_3& vector) const
  208. {
  209. vector_3 result;
  210. for(uint8_t ii = 0; ii < 3; ++ii)
  211. for(uint8_t jj = 0; jj < 3; ++jj)
  212. result[ii] += (float)elem_[ii][jj] * vector[jj];
  213. return result;
  214. }
  215. vector_3 operator * (const vector_3& vector, const matrix_3& matrix)
  216. {
  217. vector_3 result;
  218. for(uint8_t ii = 0; ii < 3; ++ii)
  219. for(uint8_t jj = 0; jj < 3; ++jj)
  220. result[ii] += (float)matrix.elem_[jj][ii] * vector[jj];
  221. return result;
  222. }
  223. matrix_3 operator * (C3D_FLOAT scalar, const matrix_3& matrix)
  224. {
  225. matrix_3 result = matrix;
  226. for(uint8_t ii = 0; ii < 3; ++ii)
  227. for(uint8_t jj = 0; jj < 3; ++jj)
  228. result.elem_[ii][jj] *= scalar;
  229. return result;
  230. }
  231. /*
  232. Cannonical Transform
  233. transform_3::transform_3(C3D_FLOAT Xc, C3D_FLOAT Yc, C3D_FLOAT Zc,
  234. C3D_FLOAT Psicd, C3D_FLOAT Thetacd, C3D_FLOAT Phicd) :
  235. // Rotation by Phic matrix (X)
  236. core_matrix_(
  237. 1, 0, 0,
  238. 0, C3D_COS(Phicd), -C3D_SIN(Phicd),
  239. 0, C3D_SIN(Phicd), C3D_COS(Phicd)
  240. ),
  241. core_vector_( Xc, Yc, Zc ) {
  242. // Rotation by Thetac matrix (Y)
  243. core_matrix_ *= matrix_3(C3D_COS(Thetacd), 0, C3D_SIN(Thetacd),
  244. 0, 1, 0,
  245. -C3D_SIN(Thetacd), 0, C3D_COS(Thetacd));
  246. // Rotation by Psic matrix (Z)
  247. core_matrix_ *= matrix_3(C3D_COS(Psicd), -C3D_SIN(Psicd), 0,
  248. C3D_SIN(Psicd), C3D_COS(Psicd), 0,
  249. 0, 0, 1);
  250. }
  251. */
  252. transform_3::transform_3(C3D_FLOAT Xc, C3D_FLOAT Yc, C3D_FLOAT Zc,
  253. C3D_FLOAT Psicd, C3D_FLOAT Thetacd, C3D_FLOAT Phicd) :
  254. // Rotation by Phic matrix (X)
  255. core_matrix_(
  256. 1, 0, 0,
  257. 0, C3D_COS(Thetacd), -C3D_SIN(Thetacd),
  258. 0, C3D_SIN(Thetacd), C3D_COS(Thetacd)
  259. ),
  260. core_vector_( Xc, Yc, Zc ) {
  261. // Rotation by Thetac matrix (Y)
  262. core_matrix_ *= matrix_3(C3D_COS(Psicd), 0, C3D_SIN(Psicd),
  263. 0, 1, 0,
  264. -C3D_SIN(Psicd), 0, C3D_COS(Psicd));
  265. // Rotation by Psic matrix (Z)
  266. core_matrix_ *= matrix_3(C3D_COS(Phicd), -C3D_SIN(Phicd), 0,
  267. C3D_SIN(Phicd), C3D_COS(Phicd), 0,
  268. 0, 0, 1);
  269. }
  270. void transform_3::get_ptz_params(C3D_FLOAT angle, C3D_FLOAT theta_t_s, C3D_FLOAT range, ptz_params& params){
  271. //double theta_t_s = .0; // Assume that the tilt on the target is 0[deg].
  272. // Latter we can put as the tilt of the LRF
  273. // Transformation to camera system, r_t_c = Target vector in camera system [m]
  274. // Convert from Spherical to Decart
  275. vector_3 r_t_c = core_matrix_ * (range * vector_3( C3D_COS(DTOR * angle) * C3D_COS(DTOR * theta_t_s),
  276. C3D_SIN(DTOR * angle) * C3D_COS(DTOR * theta_t_s),
  277. // then Move basis by core_vector_
  278. -C3D_SIN(DTOR * theta_t_s) ) - core_vector_ );
  279. // Convert back Decart to Spherical
  280. params.pan = static_cast<float> (RTOD * C3D_ATAN2(r_t_c[1], r_t_c[0])); // Pan, in degree
  281. 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
  282. params.dist = static_cast<float> (sqrt(r_t_c * r_t_c)); // Range, in meters
  283. }
  284. void transform_3::get_vector_world( vector_3 target, vector_3 &param ){
  285. param = ( core_matrix_ * target ) + core_vector_ ;
  286. };
  287. void transform_3::get_vector_local( vector_3 target, vector_3 &param ){
  288. param = ( ~core_matrix_ ) * ( target - core_vector_ );
  289. };
  290. #ifdef DEBUG
  291. #undef LOG
  292. #undef LOG_ERROR
  293. #endif