//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: Transform.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of Transform.cpp //---------------------------------------------------------------------------- #include "StdAfx.h" #include "Transform.h" #include "..\..\LgLib\Version.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif /************************************************************************** **************************************************************************/ CVector::CVector() { m_pElement[0] = 0.0; m_pElement[1] = 0.0; m_pElement[2] = 0.0; LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVector::CVector") ); } void CVector::Set( double a0, double a1, double a2 ) { m_pElement[0] = a0; m_pElement[1] = a1; m_pElement[2] = a2; } CVector& CVector::operator =( CVector& v ) { m_pElement[0] = v[0]; m_pElement[1] = v[1]; m_pElement[2] = v[2]; return *this; } CVector& CVector::operator -=( CVector& v ) { m_pElement[0] -= v[0]; m_pElement[1] -= v[1]; m_pElement[2] -= v[2]; return *this; } CVector& CVector::operator +=( CVector& v ) { m_pElement[0] += v[0]; m_pElement[1] += v[1]; m_pElement[2] += v[2]; return *this; } CVector& CVector::operator -() { m_pElement[0] = -m_pElement[0]; m_pElement[1] = -m_pElement[1]; m_pElement[2] = -m_pElement[2]; return *this; } CVector& CVector::operator *=( double d ) { m_pElement[0] *= d; m_pElement[1] *= d; m_pElement[2] *= d; return *this; } /************************************************************************** **************************************************************************/ CMatrix::CMatrix() { m_pElement[0][0] = 1.0; m_pElement[0][1] = 0.0; m_pElement[0][2] = 0.0; m_pElement[1][0] = 0.0; m_pElement[1][1] = 1.0; m_pElement[1][2] = 0.0; m_pElement[2][0] = 0.0; m_pElement[2][1] = 0.0; m_pElement[2][2] = 1.0; LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CMatrix::CMatrix") ); } CMatrix& CMatrix::operator =( CMatrix& m ) { m_pElement[0][0] = m[0][0]; m_pElement[0][1] = m[0][1]; m_pElement[0][2] = m[0][2]; m_pElement[1][0] = m[1][0]; m_pElement[1][1] = m[1][1]; m_pElement[1][2] = m[1][2]; m_pElement[2][0] = m[2][0]; m_pElement[2][1] = m[2][1]; m_pElement[2][2] = m[2][2]; return *this; } void CMatrix::Set( double a00, double a01, double a02, double a10, double a11, double a12, double a20, double a21, double a22 ) { m_pElement[0][0] = a00; m_pElement[0][1] = a01; m_pElement[0][2] = a02; m_pElement[1][0] = a10; m_pElement[1][1] = a11; m_pElement[1][2] = a12; m_pElement[2][0] = a20; m_pElement[2][1] = a21; m_pElement[2][2] = a22; } CMatrix operator *( CMatrix& m1, CMatrix& m2 ) { CMatrix m; for( int i = 0; i < 3; i++ ) { for( int j = 0; j < 3; j++ ) { m[i][j] = m1[i][0] * m2[0][j] + m1[i][1] * m2[1][j] + m1[i][2] * m2[2][j]; } } return m; } CVector operator *( CMatrix& m, CVector& v ) { CVector r; for( int i = 0; i < 3; i++ ) { r[i] = m[i][0] * v[0] + m[i][1] * v[1] + m[i][2] * v[2]; } return r; } /************************************************************************** **************************************************************************/ CTransform::CTransform() { ::ZeroMemory( &m_Values, sizeof(m_Values) ); m_bPrepared = false; LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CTransform::CTransform") ); } CTransform::~CTransform() { LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CTransform::~CTransform") ); } static double pi; static double d2r; static double r2d; static double r2b; void CTransform::Prepare( double Xc, double Yc, double Zc, double Psicd, double Thetacd, double Phicd ) { m_Values[0] = Xc; m_Values[1] = Yc; m_Values[2] = Zc; m_Values[3] = Psicd; m_Values[4] = Thetacd; m_Values[5] = Phicd; pi = asin( 1.0L ) * 2.0L; d2r = pi / 180.0L; r2d = 180.0L / pi; r2b = 128.0L / pi; // Camera translation vector m_Vector.Set( Xc, Yc, Zc ); // Change angles to rad double Psic = Psicd * d2r; double Thetac = Thetacd * d2r; double Phic = Phicd * d2r; // Calculate the sin() and cos() of the rotation angles double cPsic = cos( Psic ); double sPsic = sin( Psic ); double cThetac = cos( Thetac ); double sThetac = sin( Thetac ); double cPhic = cos( Phic ); double sPhic = sin( Phic ); // Rotation by Phic matrix CMatrix a1( 1, 0, 0, 0, cPhic, sPhic, 0, -sPhic, cPhic ); // Rotation by Thetac matrix CMatrix a2( cThetac, 0, -sThetac, 0, 1, 0, sThetac, 0, cThetac ); // Rotation by Psic matrix CMatrix a3( cPsic, sPsic, 0, -sPsic, cPsic, 0, 0, 0, 1 ); // Rotation matrix m_Matrix = a1 * a2 * a3; m_bPrepared = true; } bool CTransform::Evaluate( double angle, double range, double& pan, double& tilt, double& zoom ) { if( !m_bPrepared ) return false; // Target detected and measured by LGS sensor double psi_t_sd = angle; // Scan (psi) angle from LGS [deg] (LGS in 360 angles mode) double ro_t_s = range; // Target range from LGS [m], after conversion from bits to full scall of LRF double theta_t_s = 0; // Assume that the tilt ot the target is 0[deg]. // Latter we can put as the tilt of the LRF double psi_t_s = psi_t_sd*d2r; // Change angles to rad double cpsi_t_s = cos(psi_t_s); double spsi_t_s = sin(psi_t_s); // sin() and cos() double ctheta_t_s = cos(theta_t_s); double stheta_t_s = sin(theta_t_s); // sin() and cos() // Convert target coordinates from spherical to cartesian in [ ] a vector CVector r_t_s = CVector( cpsi_t_s * ctheta_t_s, spsi_t_s * ctheta_t_s, -stheta_t_s ) * ro_t_s; // Transformation to camera system CVector r_t_c = m_Matrix * ( r_t_s - m_Vector ); // r_t_c = Target vector in camera system [m] // Convert target coordinates from cartesian to spherical double psi_t_c = atan2( r_t_c[1], r_t_c[0] ); // Pan (psi) [rad] 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] 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] pan = psi_t_c * r2d; tilt = theta_t_c * r2d; zoom = ro_t_c; while( pan >= 360.0 ) pan -= 360.0; while( pan < 0.0 ) pan += 360.0; while( tilt >= 360.0 ) tilt -= 360.0; while( tilt < 0.0 ) tilt += 360.0; return true; }