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.

420 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. // Endless pan: the azimuth may come as -180..180 or above 360, bring it into 0..360
  131. if (panAngles >= 360.0f)
  132. {
  133. in.fi = fmodf(in.fi, 360.0f);
  134. if (in.fi < 0.0f)
  135. {
  136. in.fi += 360.0f;
  137. };
  138. };
  139. switch (panMode)
  140. {
  141. case 1:
  142. {
  143. out.pan = 1.0f - ((2.0f / panAngles) * in.fi);
  144. break;
  145. };
  146. case 2:
  147. default:
  148. {
  149. out.pan = (2.0f / panAngles) * in.fi - 1.0f;
  150. };
  151. };
  152. switch (tiltMode)
  153. {
  154. case 0:
  155. {
  156. out.tilt = -2.0f * ((tiltAngles - in.tetta) / tiltAngles) + 1.0f;
  157. break;
  158. };
  159. case 1:
  160. {
  161. out.tilt = -1.0f * (-2.0f * ((tiltAngles - in.tetta) / tiltAngles) + 1.0f);
  162. break;
  163. };
  164. case 2:
  165. default:
  166. {
  167. out.tilt = (2.0f / tiltAngles) * (tiltAngles - in.tetta) - 1.0f;
  168. break;
  169. };
  170. };
  171. // A target is accepted up to half a field of view outside the mechanical range (isInRange), and a camera
  172. // that can tilt 90 degrees accepts any elevation: the camera must then go to its end position, where the
  173. // target is still in the picture. A value outside -1..1 (generic ONVIF space) is rejected by the camera
  174. // with ter:InvalidPosition ("The requested destination is out of bounds").
  175. out.pan = (out.pan < -1.0f) ? -1.0f : ((out.pan > 1.0f) ? 1.0f : out.pan);
  176. out.tilt = (out.tilt < -1.0f) ? -1.0f : ((out.tilt > 1.0f) ? 1.0f : out.tilt);
  177. out.zoom = (out.zoom < 0.0f) ? 0.0f : ((out.zoom > 1.0f) ? 1.0f : out.zoom);
  178. }
  179. void spherical2onvif(C3D_SPH_RADIANS inr, C3D_ORIENT_ONVIF & out, float maxR, uint8_t panMode, float panAngles, uint8_t tiltMode, float tiltAngles)
  180. {
  181. C3D_SPH_DEGREES in;
  182. spherical2spherical(inr, in);
  183. spherical2onvif(in, out, maxR, panMode, panAngles, tiltMode, tiltAngles);
  184. }
  185. /*
  186. void translate2local(S_TRANS_MATRIX mat, C3D_DECART world, C3D_DECART &local){
  187. S_TRANS_MATRIX tMat;
  188. transponeMatrix(mat, tMat);
  189. local.x = world.x * tMat.AA + world.y * tMat.AB + world.z * tMat.AC + tMat.AD;
  190. local.y = world.x * tMat.BA + world.y * tMat.BB + world.z * tMat.BC + tMat.BD;
  191. local.z = world.x * tMat.CA + world.y * tMat.CB + world.z * tMat.CC + tMat.CD;
  192. };
  193. void translate2world(S_TRANS_MATRIX tMat, C3D_DECART local, C3D_DECART &world){
  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. */
  199. void translate2local(S_TRANS_MATRIX mat, C3D_DECART world, C3D_DECART & local)
  200. {
  201. S_TRANS_MATRIX tMat;
  202. // transform_3 *trHelper;
  203. transponeMatrix(mat, tMat);
  204. local.x = world.x * tMat.AA + world.y * tMat.AB + world.z * tMat.AC + tMat.AD;
  205. local.y = world.x * tMat.BA + world.y * tMat.BB + world.z * tMat.BC + tMat.BD;
  206. local.z = world.x * tMat.CA + world.y * tMat.CB + world.z * tMat.CC + tMat.CD;
  207. }
  208. void translate2world(S_TRANS_MATRIX tMat, C3D_DECART local, C3D_DECART & world)
  209. {
  210. world.x = local.x * tMat.AA + local.y * tMat.AB + local.z * tMat.AC + tMat.AD;
  211. world.y = local.x * tMat.BA + local.y * tMat.BB + local.z * tMat.BC + tMat.BD;
  212. world.z = local.x * tMat.CA + local.y * tMat.CB + local.z * tMat.CC + tMat.CD;
  213. }
  214. void transponeMatrix(S_TRANS_MATRIX in, S_TRANS_MATRIX & out)
  215. {
  216. out.AA = in.AA;
  217. out.AB = in.BA;
  218. out.AC = in.CA;
  219. out.AD = in.DA;
  220. out.BA = in.AB;
  221. out.BB = in.BB;
  222. out.BC = in.CB;
  223. out.BD = in.DB;
  224. out.CA = in.AC;
  225. out.CB = in.BC;
  226. out.CC = in.CC;
  227. out.CD = in.DC;
  228. out.DA = in.AD;
  229. out.DB = in.BD;
  230. out.DC = in.CD;
  231. out.DD = in.DD;
  232. }
  233. const C3D_FLOAT transform_3::PI_M = 3.141592653589793f;
  234. const C3D_FLOAT transform_3::DTOR = 0.017453292519943f;
  235. const C3D_FLOAT transform_3::RTOD = 57.29577951308232f;
  236. vector_3::vector_3(C3D_FLOAT a0, C3D_FLOAT a1, C3D_FLOAT a2)
  237. {
  238. elem_[0] = a0;
  239. elem_[1] = a1;
  240. elem_[2] = a2;
  241. }
  242. 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)
  243. {
  244. elem_[0][0] = a00;
  245. elem_[0][1] = a01;
  246. elem_[0][2] = a02;
  247. elem_[1][0] = a10;
  248. elem_[1][1] = a11;
  249. elem_[1][2] = a12;
  250. elem_[2][0] = a20;
  251. elem_[2][1] = a21;
  252. elem_[2][2] = a22;
  253. }
  254. matrix_3 matrix_3::operator*(const matrix_3 & matrix) const
  255. {
  256. matrix_3 result;
  257. for (uint8_t ii = 0; ii < 3; ++ii)
  258. {
  259. for (uint8_t jj = 0; jj < 3; ++jj)
  260. {
  261. for (uint8_t kk = 0; kk < 3; ++kk)
  262. {
  263. result.elem_[ii][jj] += elem_[ii][kk] * matrix.elem_[kk][jj];
  264. }
  265. }
  266. }
  267. return result;
  268. }
  269. matrix_3 matrix_3::operator~() const
  270. {
  271. matrix_3 result;
  272. for (uint8_t ii = 0; ii < 3; ++ii)
  273. {
  274. for (uint8_t jj = 0; jj < 3; ++jj)
  275. {
  276. result.elem_[ii][jj] = elem_[jj][ii];
  277. }
  278. }
  279. return result;
  280. }
  281. vector_3 matrix_3::operator*(const vector_3 & vector) const
  282. {
  283. vector_3 result;
  284. for (uint8_t ii = 0; ii < 3; ++ii)
  285. {
  286. for (uint8_t jj = 0; jj < 3; ++jj)
  287. {
  288. result[ii] += (float) elem_[ii][jj] * vector[jj];
  289. }
  290. }
  291. return result;
  292. }
  293. vector_3 operator*(const vector_3 & vector, const matrix_3 & matrix)
  294. {
  295. vector_3 result;
  296. for (uint8_t ii = 0; ii < 3; ++ii)
  297. {
  298. for (uint8_t jj = 0; jj < 3; ++jj)
  299. {
  300. result[ii] += (float) matrix.elem_[jj][ii] * vector[jj];
  301. }
  302. }
  303. return result;
  304. }
  305. matrix_3 operator*(C3D_FLOAT scalar, const matrix_3 & matrix)
  306. {
  307. matrix_3 result = matrix;
  308. for (uint8_t ii = 0; ii < 3; ++ii)
  309. {
  310. for (uint8_t jj = 0; jj < 3; ++jj)
  311. {
  312. result.elem_[ii][jj] *= scalar;
  313. }
  314. }
  315. return result;
  316. }
  317. /*
  318. Cannonical Transform
  319. transform_3::transform_3(C3D_FLOAT Xc, C3D_FLOAT Yc, C3D_FLOAT Zc,
  320. C3D_FLOAT Psicd, C3D_FLOAT Thetacd, C3D_FLOAT Phicd) :
  321. // Rotation by Phic matrix (X)
  322. core_matrix_(
  323. 1, 0, 0,
  324. 0, C3D_COS(Phicd), -C3D_SIN(Phicd),
  325. 0, C3D_SIN(Phicd), C3D_COS(Phicd)
  326. ),
  327. core_vector_( Xc, Yc, Zc ) {
  328. // Rotation by Thetac matrix (Y)
  329. core_matrix_ *= matrix_3(C3D_COS(Thetacd), 0, C3D_SIN(Thetacd),
  330. 0, 1, 0,
  331. -C3D_SIN(Thetacd), 0, C3D_COS(Thetacd));
  332. // Rotation by Psic matrix (Z)
  333. core_matrix_ *= matrix_3(C3D_COS(Psicd), -C3D_SIN(Psicd), 0,
  334. C3D_SIN(Psicd), C3D_COS(Psicd), 0,
  335. 0, 0, 1);
  336. }
  337. */
  338. transform_3::transform_3(C3D_FLOAT Xc, C3D_FLOAT Yc, C3D_FLOAT Zc, C3D_FLOAT Psicd, C3D_FLOAT Thetacd, C3D_FLOAT Phicd) :
  339. // Rotation by Phic matrix (X)
  340. core_matrix_(1, 0, 0, 0, C3D_COS(Thetacd), -C3D_SIN(Thetacd), 0, C3D_SIN(Thetacd), C3D_COS(Thetacd)),
  341. core_vector_(Xc, Yc, Zc)
  342. {
  343. // Rotation by Thetac matrix (Y)
  344. core_matrix_ *= matrix_3(C3D_COS(Psicd), 0, C3D_SIN(Psicd), 0, 1, 0, -C3D_SIN(Psicd), 0, C3D_COS(Psicd));
  345. // Rotation by Psic matrix (Z)
  346. core_matrix_ *= matrix_3(C3D_COS(Phicd), -C3D_SIN(Phicd), 0, C3D_SIN(Phicd), C3D_COS(Phicd), 0, 0, 0, 1);
  347. }
  348. void transform_3::get_ptz_params(C3D_FLOAT angle, C3D_FLOAT theta_t_s, C3D_FLOAT range, ptz_params & params)
  349. {
  350. // double theta_t_s = .0; // Assume that the tilt on the target is 0[deg].
  351. // Latter we can put as the tilt of the LRF
  352. // Transformation to camera system, r_t_c = Target vector in camera system [m]
  353. // Convert from Spherical to Decart
  354. 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),
  355. // then Move basis by core_vector_
  356. -C3D_SIN(DTOR * theta_t_s)) -
  357. core_vector_);
  358. // Convert back Decart to Spherical
  359. params.pan = static_cast<float>(RTOD * C3D_ATAN2(r_t_c[1], r_t_c[0])); // Pan, in degree
  360. 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
  361. params.dist = static_cast<float>(sqrt(r_t_c * r_t_c)); // Range, in meters
  362. }
  363. void transform_3::get_vector_world(vector_3 target, vector_3 & param)
  364. {
  365. param = (core_matrix_ * target) + core_vector_;
  366. }
  367. void transform_3::get_vector_local(vector_3 target, vector_3 & param)
  368. {
  369. param = (~core_matrix_) * (target - core_vector_);
  370. }
  371. #ifdef DEBUG
  372. #undef LOG
  373. #undef LOG_ERROR
  374. #endif