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.

351 lines
11 KiB

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