Old fashion design for PC-104
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.

271 lines
6.8 KiB

4 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: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of Transform.cpp
  13. //----------------------------------------------------------------------------
  14. #include "StdAfx.h"
  15. #include "Transform.h"
  16. #include "..\..\LgLib\Version.h"
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[]=__FILE__;
  20. #define new DEBUG_NEW
  21. #endif
  22. /**************************************************************************
  23. **************************************************************************/
  24. CVector::CVector()
  25. {
  26. m_pElement[0] = 0.0;
  27. m_pElement[1] = 0.0;
  28. m_pElement[2] = 0.0;
  29. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVector::CVector") );
  30. }
  31. void CVector::Set( double a0, double a1, double a2 )
  32. {
  33. m_pElement[0] = a0;
  34. m_pElement[1] = a1;
  35. m_pElement[2] = a2;
  36. }
  37. CVector& CVector::operator =( CVector& v )
  38. {
  39. m_pElement[0] = v[0];
  40. m_pElement[1] = v[1];
  41. m_pElement[2] = v[2];
  42. return *this;
  43. }
  44. CVector& CVector::operator -=( CVector& v )
  45. {
  46. m_pElement[0] -= v[0];
  47. m_pElement[1] -= v[1];
  48. m_pElement[2] -= v[2];
  49. return *this;
  50. }
  51. CVector& CVector::operator +=( CVector& v )
  52. {
  53. m_pElement[0] += v[0];
  54. m_pElement[1] += v[1];
  55. m_pElement[2] += v[2];
  56. return *this;
  57. }
  58. CVector& CVector::operator -()
  59. {
  60. m_pElement[0] = -m_pElement[0];
  61. m_pElement[1] = -m_pElement[1];
  62. m_pElement[2] = -m_pElement[2];
  63. return *this;
  64. }
  65. CVector& CVector::operator *=( double d )
  66. {
  67. m_pElement[0] *= d;
  68. m_pElement[1] *= d;
  69. m_pElement[2] *= d;
  70. return *this;
  71. }
  72. /**************************************************************************
  73. **************************************************************************/
  74. CMatrix::CMatrix()
  75. {
  76. m_pElement[0][0] = 1.0;
  77. m_pElement[0][1] = 0.0;
  78. m_pElement[0][2] = 0.0;
  79. m_pElement[1][0] = 0.0;
  80. m_pElement[1][1] = 1.0;
  81. m_pElement[1][2] = 0.0;
  82. m_pElement[2][0] = 0.0;
  83. m_pElement[2][1] = 0.0;
  84. m_pElement[2][2] = 1.0;
  85. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CMatrix::CMatrix") );
  86. }
  87. CMatrix& CMatrix::operator =( CMatrix& m )
  88. {
  89. m_pElement[0][0] = m[0][0];
  90. m_pElement[0][1] = m[0][1];
  91. m_pElement[0][2] = m[0][2];
  92. m_pElement[1][0] = m[1][0];
  93. m_pElement[1][1] = m[1][1];
  94. m_pElement[1][2] = m[1][2];
  95. m_pElement[2][0] = m[2][0];
  96. m_pElement[2][1] = m[2][1];
  97. m_pElement[2][2] = m[2][2];
  98. return *this;
  99. }
  100. void CMatrix::Set( double a00, double a01, double a02,
  101. double a10, double a11, double a12,
  102. double a20, double a21, double a22 )
  103. {
  104. m_pElement[0][0] = a00;
  105. m_pElement[0][1] = a01;
  106. m_pElement[0][2] = a02;
  107. m_pElement[1][0] = a10;
  108. m_pElement[1][1] = a11;
  109. m_pElement[1][2] = a12;
  110. m_pElement[2][0] = a20;
  111. m_pElement[2][1] = a21;
  112. m_pElement[2][2] = a22;
  113. }
  114. CMatrix operator *( CMatrix& m1, CMatrix& m2 )
  115. {
  116. CMatrix m;
  117. for( int i = 0; i < 3; i++ )
  118. {
  119. for( int j = 0; j < 3; j++ )
  120. {
  121. m[i][j] = m1[i][0] * m2[0][j] +
  122. m1[i][1] * m2[1][j] +
  123. m1[i][2] * m2[2][j];
  124. }
  125. }
  126. return m;
  127. }
  128. CVector operator *( CMatrix& m, CVector& v )
  129. {
  130. CVector r;
  131. for( int i = 0; i < 3; i++ )
  132. {
  133. r[i] = m[i][0] * v[0] +
  134. m[i][1] * v[1] +
  135. m[i][2] * v[2];
  136. }
  137. return r;
  138. }
  139. /**************************************************************************
  140. **************************************************************************/
  141. CTransform::CTransform()
  142. {
  143. ::ZeroMemory( &m_Values, sizeof(m_Values) );
  144. m_bPrepared = false;
  145. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CTransform::CTransform") );
  146. }
  147. CTransform::~CTransform()
  148. {
  149. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CTransform::~CTransform") );
  150. }
  151. static double pi;
  152. static double d2r;
  153. static double r2d;
  154. static double r2b;
  155. void CTransform::Prepare( double Xc, double Yc, double Zc, double Psicd, double Thetacd, double Phicd )
  156. {
  157. m_Values[0] = Xc;
  158. m_Values[1] = Yc;
  159. m_Values[2] = Zc;
  160. m_Values[3] = Psicd;
  161. m_Values[4] = Thetacd;
  162. m_Values[5] = Phicd;
  163. pi = asin( 1.0L ) * 2.0L;
  164. d2r = pi / 180.0L;
  165. r2d = 180.0L / pi;
  166. r2b = 128.0L / pi;
  167. // Camera translation vector
  168. m_Vector.Set( Xc, Yc, Zc );
  169. // Change angles to rad
  170. double Psic = Psicd * d2r;
  171. double Thetac = Thetacd * d2r;
  172. double Phic = Phicd * d2r;
  173. // Calculate the sin() and cos() of the rotation angles
  174. double cPsic = cos( Psic );
  175. double sPsic = sin( Psic );
  176. double cThetac = cos( Thetac );
  177. double sThetac = sin( Thetac );
  178. double cPhic = cos( Phic );
  179. double sPhic = sin( Phic );
  180. // Rotation by Phic matrix
  181. CMatrix a1( 1, 0, 0,
  182. 0, cPhic, sPhic,
  183. 0, -sPhic, cPhic );
  184. // Rotation by Thetac matrix
  185. CMatrix a2( cThetac, 0, -sThetac,
  186. 0, 1, 0,
  187. sThetac, 0, cThetac );
  188. // Rotation by Psic matrix
  189. CMatrix a3( cPsic, sPsic, 0,
  190. -sPsic, cPsic, 0,
  191. 0, 0, 1 );
  192. // Rotation matrix
  193. m_Matrix = a1 * a2 * a3;
  194. m_bPrepared = true;
  195. }
  196. bool CTransform::Evaluate( double angle, double range, double& pan, double& tilt, double& zoom )
  197. {
  198. if( !m_bPrepared ) return false;
  199. // Target detected and measured by LGS sensor
  200. double psi_t_sd = angle; // Scan (psi) angle from LGS [deg] (LGS in 360 angles mode)
  201. double ro_t_s = range; // Target range from LGS [m], after conversion from bits to full scall of LRF
  202. double theta_t_s = 0; // Assume that the tilt ot the target is 0[deg].
  203. // Latter we can put as the tilt of the LRF
  204. double psi_t_s = psi_t_sd*d2r; // Change angles to rad
  205. double cpsi_t_s = cos(psi_t_s);
  206. double spsi_t_s = sin(psi_t_s); // sin() and cos()
  207. double ctheta_t_s = cos(theta_t_s);
  208. double stheta_t_s = sin(theta_t_s); // sin() and cos()
  209. // Convert target coordinates from spherical to cartesian in [ ] a vector
  210. CVector r_t_s = CVector( cpsi_t_s * ctheta_t_s,
  211. spsi_t_s * ctheta_t_s,
  212. -stheta_t_s ) * ro_t_s;
  213. // Transformation to camera system
  214. CVector r_t_c = m_Matrix * ( r_t_s - m_Vector ); // r_t_c = Target vector in camera system [m]
  215. // Convert target coordinates from cartesian to spherical
  216. double psi_t_c = atan2( r_t_c[1], r_t_c[0] ); // Pan (psi) [rad]
  217. double theta_t_c = atan2( -r_t_c[2], sqrt( pow( r_t_c[0], 2 ) + pow( r_t_c[1], 2 ) ) ); // Tilt (theta) [rad]
  218. double ro_t_c = sqrt( pow( r_t_c[0], 2 ) + pow( r_t_c[1], 2 ) + pow( r_t_c[2], 2 ) ); // Range [m]
  219. pan = psi_t_c * r2d;
  220. tilt = theta_t_c * r2d;
  221. zoom = ro_t_c;
  222. while( pan >= 360.0 ) pan -= 360.0;
  223. while( pan < 0.0 ) pan += 360.0;
  224. while( tilt >= 360.0 ) tilt -= 360.0;
  225. while( tilt < 0.0 ) tilt += 360.0;
  226. return true;
  227. }