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.

257 lines
6.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 5/04/2001
  5. // Last modification: 08/10/2003
  6. // Project Intruder Position Indicator
  7. // SUBSYSTEM: Receiver/Transmitter information from/to LGS
  8. // FILE: Transform.h
  9. // AUTHOR: Belkovsky Joseph
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Header file of classes vector_3, matrix_3 and transform_3
  13. //----------------------------------------------------------------------------
  14. #if ! defined(__transform_h_guard__)
  15. #define __transform_h_guard__
  16. #include <stdint.h>
  17. #ifndef M_E
  18. #include <math.h>
  19. #endif
  20. #ifndef C3D_PRECISE_CALC
  21. #define C3D_PI 3.1415926535897932384626433832795f
  22. #define C3D_2PI 6.283185307179586476925286766559f
  23. #define C3D_PI2 1.5707963267948966192313216916398f
  24. #define C3D_FLOAT float
  25. #define C3D_SIN(X) sinf(X)
  26. #define C3D_COS(X) cosf(X)
  27. #define C3D_ACOS(X) acosf(X)
  28. #define C3D_POW(X, Y) powf(X, Y)
  29. #define C3D_SQRT(X) sqrtf(X)
  30. #define C3D_ATAN(X) atanf(X)
  31. #define C3D_ATAN2(X, Y) atan2f(X, Y)
  32. #else
  33. #ifdef C3D_VERY_PRECISE_CALC
  34. #define C3D_PI 3.1415926535897932384626433832795
  35. #define C3D_2PI 6.283185307179586476925286766559
  36. #define C3D_PI2 1.5707963267948966192313216916398
  37. #define C3D_FLOAT long double
  38. #define C3D_SIN(X) sinl(X)
  39. #define C3D_COS(X) cosl(X)
  40. #define C3D_ACOS(X) acosl(X)
  41. #define C3D_POW(X, Y) powl(X, Y)
  42. #define C3D_SQRT(X) sqrtl(X)
  43. #define C3D_ATAN(X) atanl(X)
  44. #define C3D_ATAN2(X, Y) atan2l(X, Y)
  45. #else
  46. #define C3D_PI 3.1415926535897932384626433832795
  47. #define C3D_2PI 6.283185307179586476925286766559
  48. #define C3D_PI2 1.5707963267948966192313216916398
  49. #define C3D_FLOAT double
  50. #define C3D_SIN(X) sin(X)
  51. #define C3D_COS(X) cos(X)
  52. #define C3D_ACOS(X) acos(X)
  53. #define C3D_POW(X, Y) pow(X, Y)
  54. #define C3D_SQRT(X) sqrt(X)
  55. #define C3D_ATAN(X) atan(X)
  56. #define C3D_ATAN2(X, Y) atan2(X, Y)
  57. #endif
  58. #endif
  59. typedef struct sTransMatrix
  60. {
  61. C3D_FLOAT AA;
  62. C3D_FLOAT AB;
  63. C3D_FLOAT AC;
  64. C3D_FLOAT AD;
  65. C3D_FLOAT BA;
  66. C3D_FLOAT BB;
  67. C3D_FLOAT BC;
  68. C3D_FLOAT BD;
  69. C3D_FLOAT CA;
  70. C3D_FLOAT CB;
  71. C3D_FLOAT CC;
  72. C3D_FLOAT CD;
  73. C3D_FLOAT DA;
  74. C3D_FLOAT DB;
  75. C3D_FLOAT DC;
  76. C3D_FLOAT DD;
  77. } S_TRANS_MATRIX;
  78. typedef struct S_C3D_DEC
  79. {
  80. C3D_FLOAT x;
  81. C3D_FLOAT y;
  82. C3D_FLOAT z;
  83. } C3D_DECART;
  84. typedef struct S_C3D_LOC
  85. {
  86. C3D_FLOAT lat;
  87. C3D_FLOAT lon;
  88. C3D_FLOAT alt;
  89. } C3D_WGS84_LOC;
  90. typedef struct S_C3D_SPH_RAD
  91. {
  92. C3D_FLOAT r;
  93. C3D_FLOAT fi; // XY - planar
  94. C3D_FLOAT tetta; // Z planar -PI/2 .. 0=XY .. +PI/2
  95. } C3D_SPH_RADIANS;
  96. typedef struct S_C3D_ORIENT_RAD
  97. {
  98. C3D_FLOAT rot_x;
  99. C3D_FLOAT rot_y;
  100. C3D_FLOAT rot_z;
  101. } C3D_ORIENT_RAD;
  102. typedef struct S_C3D_ORIENT_DEG
  103. {
  104. C3D_FLOAT rot_x;
  105. C3D_FLOAT rot_y;
  106. C3D_FLOAT rot_z;
  107. } C3D_ORIENT_DEG;
  108. typedef struct S_C3D_ORIENT_ONVIF
  109. {
  110. C3D_FLOAT pan;
  111. C3D_FLOAT tilt;
  112. C3D_FLOAT zoom;
  113. } C3D_ORIENT_ONVIF;
  114. typedef struct S_C3D_SPH_DEG
  115. {
  116. C3D_FLOAT r;
  117. C3D_FLOAT fi; // XY - planar
  118. C3D_FLOAT tetta; // Z planar -90 .. 0=XY .. +90
  119. } C3D_SPH_DEGREES;
  120. inline C3D_FLOAT deg2rad(C3D_FLOAT deg)
  121. {
  122. return (deg / 180.0f) * C3D_PI;
  123. }
  124. inline C3D_FLOAT rad2deg(C3D_FLOAT rad)
  125. {
  126. return (rad / C3D_PI) * 180.0f;
  127. }
  128. void decart2spherical(C3D_DECART in, C3D_SPH_RADIANS & out);
  129. void decart2spherical(C3D_DECART in, C3D_SPH_DEGREES & out);
  130. void spherical2decart(C3D_SPH_RADIANS in, C3D_DECART & out);
  131. void spherical2decart(C3D_SPH_DEGREES in, C3D_DECART & out);
  132. void spherical2onvif(C3D_SPH_DEGREES in, C3D_ORIENT_ONVIF & out, float maxR, uint8_t panMode = 0, float panAngles = 360.0f, uint8_t tiltMode = 0, float tiltAngles = 90.0f);
  133. void spherical2onvif(C3D_SPH_RADIANS in, C3D_ORIENT_ONVIF & out, float maxR, uint8_t panMode = 0, float panAngles = 360.0f, uint8_t tiltMode = 0, float tiltAngles = 90.0f);
  134. void spherical2spherical(C3D_SPH_DEGREES in, C3D_SPH_RADIANS & out);
  135. void spherical2spherical(C3D_SPH_RADIANS in, C3D_SPH_DEGREES & out);
  136. void translate2local(S_TRANS_MATRIX mat, C3D_DECART world, C3D_DECART & local);
  137. void translate2world(S_TRANS_MATRIX mat, C3D_DECART local, C3D_DECART & world);
  138. void transponeMatrix(S_TRANS_MATRIX in, S_TRANS_MATRIX & out);
  139. C3D_FLOAT getDistance(C3D_DECART A, C3D_DECART B);
  140. struct ptz_params
  141. {
  142. float pan;
  143. float tilt;
  144. float dist; // Transformed to zoom
  145. };
  146. class vector_3
  147. {
  148. public:
  149. explicit vector_3(C3D_FLOAT a0 = 0, C3D_FLOAT a1 = 0, C3D_FLOAT a2 = 0);
  150. C3D_FLOAT & operator[](uint8_t) const;
  151. vector_3 operator+(const vector_3 &);
  152. vector_3 operator-(const vector_3 &);
  153. C3D_FLOAT operator*(const vector_3 &) const;
  154. friend vector_3 operator*(C3D_FLOAT, const vector_3 &);
  155. private:
  156. C3D_FLOAT elem_[3];
  157. };
  158. inline C3D_FLOAT & vector_3::operator[](uint8_t idx) const
  159. {
  160. return const_cast<C3D_FLOAT &>(elem_[idx]);
  161. }
  162. inline vector_3 vector_3::operator+(const vector_3 & vectorP)
  163. {
  164. return vector_3(elem_[0] + vectorP.elem_[0], elem_[1] + vectorP.elem_[1], elem_[2] + vectorP.elem_[2]);
  165. }
  166. inline vector_3 vector_3::operator-(const vector_3 & vectorP)
  167. {
  168. return vector_3(elem_[0] - vectorP.elem_[0], elem_[1] - vectorP.elem_[1], elem_[2] - vectorP.elem_[2]);
  169. }
  170. inline C3D_FLOAT vector_3::operator*(const vector_3 & vectorP) const
  171. {
  172. return elem_[0] * vectorP.elem_[0] + elem_[1] * vectorP.elem_[1] + elem_[2] * vectorP.elem_[2];
  173. }
  174. inline vector_3 operator*(C3D_FLOAT scalar, const vector_3 & vectorP)
  175. {
  176. return vector_3(vectorP.elem_[0] * scalar, vectorP.elem_[1] * scalar, vectorP.elem_[2] * scalar);
  177. }
  178. class matrix_3
  179. {
  180. public:
  181. explicit matrix_3(C3D_FLOAT a00 = 0, C3D_FLOAT a01 = 0, C3D_FLOAT a02 = 0, C3D_FLOAT a10 = 0, C3D_FLOAT a11 = 0, C3D_FLOAT a12 = 0, C3D_FLOAT a20 = 0, C3D_FLOAT a21 = 0, C3D_FLOAT a22 = 0);
  182. matrix_3 operator*(const matrix_3 &) const;
  183. matrix_3 operator~() const;
  184. vector_3 operator*(const vector_3 &) const;
  185. matrix_3 & operator*=(const matrix_3 &);
  186. friend matrix_3 operator*(C3D_FLOAT, const matrix_3 &);
  187. friend vector_3 operator*(const vector_3 &, const matrix_3 &);
  188. private:
  189. double elem_[3][3];
  190. };
  191. inline matrix_3 & matrix_3::operator*=(const matrix_3 & matrix)
  192. {
  193. return *this = *this * matrix;
  194. }
  195. class transform_3
  196. {
  197. public:
  198. transform_3(C3D_FLOAT Xc, C3D_FLOAT Yc, C3D_FLOAT Zc, C3D_FLOAT Psicd, C3D_FLOAT Thetacd, C3D_FLOAT Phicd);
  199. void get_ptz_params(C3D_FLOAT angle, C3D_FLOAT theta_t_s, C3D_FLOAT range, ptz_params & params);
  200. void get_vector_world(vector_3 target, vector_3 & param);
  201. void get_vector_local(vector_3 target, vector_3 & param);
  202. private:
  203. static const C3D_FLOAT PI_M;
  204. static const C3D_FLOAT DTOR;
  205. static const C3D_FLOAT RTOD;
  206. private:
  207. matrix_3 core_matrix_; // Camera rotation matrix
  208. vector_3 core_vector_; // Camera translation vector
  209. };
  210. #endif // __transform_h_guard__