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.

403 lines
11 KiB

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