|
|
//----------------------------------------------------------------------------
// 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: Belkovsky Joseph
// OVERVIEW
// ~~~~~~~~
// Source file of Transform.cpp
//----------------------------------------------------------------------------
// Transform.cpp : Defines the entry point for the console application.
//
#include "Transform.h"
void spherical2decart(C3D_SPH_DEGREES in, C3D_DECART &out){ C3D_FLOAT tT, tF; tT = deg2rad(in.tetta); tF = deg2rad(in.fi); out.x = in.r * C3D_SIN(tT) * C3D_COS(tF); out.y = in.r * C3D_SIN(tT) * C3D_SIN(tF); out.z = in.r * C3D_COS(tT);};
void decart2spherical(C3D_DECART in, C3D_SPH_DEGREES &out){ unsigned char oct; out.r = C3D_SQRT(C3D_POW(in.x, 2.f) + C3D_POW(in.y, 2.f) + C3D_POW(in.z, 2.f)); out.fi = rad2deg(C3D_ATAN(in.y / in.x)); out.tetta = rad2deg(C3D_ACOS(in.z / out.r)); oct = ((in.x < 0.f) ? 1 : 0) | ((in.y < 0.f) ? 2 : 0) | ((in.z < 0.f) ? 4 : 0); switch (oct){ case 0: case 4: break; case 1: case 3: case 5: case 7: out.fi += 180.f; break; case 2: case 6: out.fi += 360.f; break; };};
void spherical2decart(C3D_SPH_RADIANS in, C3D_DECART &out){ out.x = in.r * C3D_SIN(in.tetta) * C3D_COS(in.fi); out.y = in.r * C3D_SIN(in.tetta) * C3D_SIN(in.fi); out.z = in.r * C3D_COS(in.tetta);};
void decart2spherical(C3D_DECART in, C3D_SPH_RADIANS &out){ unsigned char oct; out.r = C3D_SQRT(C3D_POW(in.x, 2.f) + C3D_POW(in.y, 2.f) + C3D_POW(in.z, 2.f)); out.fi = C3D_ATAN(in.y / in.x); out.tetta = C3D_PI2 - C3D_ACOS(in.z / out.r); oct = ((in.x < 0.f) ? 1 : 0) | ((in.y < 0.f) ? 2 : 0) | ((in.z < 0.f) ? 4 : 0); switch (oct){ case 0: case 4: break; case 1: case 3: case 5: case 7: out.fi += C3D_PI; break; case 2: case 6: out.fi += C3D_2PI; break; };};
/*
void translate2local(S_TRANS_MATRIX mat, C3D_DECART world, C3D_DECART &local){ S_TRANS_MATRIX tMat; transponeMatrix(mat, tMat); local.x = world.x * tMat.AA + world.y * tMat.AB + world.z * tMat.AC + tMat.AD; local.y = world.x * tMat.BA + world.y * tMat.BB + world.z * tMat.BC + tMat.BD; local.z = world.x * tMat.CA + world.y * tMat.CB + world.z * tMat.CC + tMat.CD;};
void translate2world(S_TRANS_MATRIX tMat, C3D_DECART local, C3D_DECART &world){ world.x = local.x * tMat.AA + local.y * tMat.AB + local.z * tMat.AC + tMat.AD; world.y = local.x * tMat.BA + local.y * tMat.BB + local.z * tMat.BC + tMat.BD; world.z = local.x * tMat.CA + local.y * tMat.CB + local.z * tMat.CC + tMat.CD;};*/
void translate2local(S_TRANS_MATRIX mat, C3D_DECART world, C3D_DECART &local){ S_TRANS_MATRIX tMat; transform_3 *trHelper; transponeMatrix(mat, tMat); local.x = world.x * tMat.AA + world.y * tMat.AB + world.z * tMat.AC + tMat.AD; local.y = world.x * tMat.BA + world.y * tMat.BB + world.z * tMat.BC + tMat.BD; local.z = world.x * tMat.CA + world.y * tMat.CB + world.z * tMat.CC + tMat.CD;};
void translate2world(S_TRANS_MATRIX tMat, C3D_DECART local, C3D_DECART &world){ world.x = local.x * tMat.AA + local.y * tMat.AB + local.z * tMat.AC + tMat.AD; world.y = local.x * tMat.BA + local.y * tMat.BB + local.z * tMat.BC + tMat.BD; world.z = local.x * tMat.CA + local.y * tMat.CB + local.z * tMat.CC + tMat.CD;};
void transponeMatrix(S_TRANS_MATRIX in, S_TRANS_MATRIX &out){ out.AA = in.AA; out.AB = in.BA; out.AC = in.CA; out.AD = in.DA; out.BA = in.AB; out.BB = in.BB; out.BC = in.CB; out.BD = in.DB; out.CA = in.AC; out.CB = in.BC; out.CC = in.CC; out.CD = in.DC; out.DA = in.AD; out.DB = in.BD; out.DC = in.CD; out.DD = in.DD;};
const C3D_FLOAT transform_3::PI_M = 3.141592653589793;const C3D_FLOAT transform_3::DTOR = 0.017453292519943;const C3D_FLOAT transform_3::RTOD = 57.29577951308232;
vector_3::vector_3(C3D_FLOAT a0, C3D_FLOAT a1, C3D_FLOAT a2){ elem_[0] = a0; elem_[1] = a1; elem_[2] = a2;}
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){ elem_[0][0] = a00; elem_[0][1] = a01; elem_[0][2] = a02; elem_[1][0] = a10; elem_[1][1] = a11; elem_[1][2] = a12; elem_[2][0] = a20; elem_[2][1] = a21; elem_[2][2] = a22;}
matrix_3 matrix_3::operator * (const matrix_3& matrix) const{ matrix_3 result;
for(uint8_t ii = 0; ii < 3; ++ii) for(uint8_t jj = 0; jj < 3; ++jj) for(uint8_t kk = 0; kk < 3; ++kk) result.elem_[ii][jj] += elem_[ii][kk] * matrix.elem_[kk][jj];
return result;}
matrix_3 matrix_3::operator ~ () const{ matrix_3 result; for (uint8_t ii = 0; ii < 3; ++ii) for (uint8_t jj = 0; jj < 3; ++jj) result.elem_[ii][jj] = elem_[jj][ii]; return result;}
vector_3 matrix_3::operator * (const vector_3& vector) const{ vector_3 result;
for(uint8_t ii = 0; ii < 3; ++ii) for(uint8_t jj = 0; jj < 3; ++jj) result[ii] += elem_[ii][jj] * vector[jj];
return result;}
vector_3 operator * (const vector_3& vector, const matrix_3& matrix){ vector_3 result;
for(uint8_t ii = 0; ii < 3; ++ii) for(uint8_t jj = 0; jj < 3; ++jj) result[ii] += matrix.elem_[jj][ii] * vector[jj];
return result;}
matrix_3 operator * (C3D_FLOAT scalar, const matrix_3& matrix){ matrix_3 result = matrix;
for(uint8_t ii = 0; ii < 3; ++ii) for(uint8_t jj = 0; jj < 3; ++jj) result.elem_[ii][jj] *= scalar;
return result;}/*
Cannonical Transformtransform_3::transform_3(C3D_FLOAT Xc, C3D_FLOAT Yc, C3D_FLOAT Zc, C3D_FLOAT Psicd, C3D_FLOAT Thetacd, C3D_FLOAT Phicd) : // Rotation by Phic matrix (X)
core_matrix_( 1, 0, 0, 0, C3D_COS(Phicd), -C3D_SIN(Phicd), 0, C3D_SIN(Phicd), C3D_COS(Phicd) ), core_vector_( Xc, Yc, Zc ) {
// Rotation by Thetac matrix (Y)
core_matrix_ *= matrix_3(C3D_COS(Thetacd), 0, C3D_SIN(Thetacd), 0, 1, 0, -C3D_SIN(Thetacd), 0, C3D_COS(Thetacd)); // Rotation by Psic matrix (Z)
core_matrix_ *= matrix_3(C3D_COS(Psicd), -C3D_SIN(Psicd), 0, C3D_SIN(Psicd), C3D_COS(Psicd), 0, 0, 0, 1);}*/transform_3::transform_3(C3D_FLOAT Xc, C3D_FLOAT Yc, C3D_FLOAT Zc, C3D_FLOAT Psicd, C3D_FLOAT Thetacd, C3D_FLOAT Phicd) : // Rotation by Phic matrix (X)
core_matrix_( 1, 0, 0, 0, C3D_COS(Thetacd), -C3D_SIN(Thetacd), 0, C3D_SIN(Thetacd), C3D_COS(Thetacd) ), core_vector_( Xc, Yc, Zc ) {
// Rotation by Thetac matrix (Y)
core_matrix_ *= matrix_3(C3D_COS(Psicd), 0, C3D_SIN(Psicd), 0, 1, 0, -C3D_SIN(Psicd), 0, C3D_COS(Psicd)); // Rotation by Psic matrix (Z)
core_matrix_ *= matrix_3(C3D_COS(Phicd), -C3D_SIN(Phicd), 0, C3D_SIN(Phicd), C3D_COS(Phicd), 0, 0, 0, 1);}void transform_3::get_ptz_params(C3D_FLOAT angle, C3D_FLOAT theta_t_s, C3D_FLOAT range, ptz_params& params){ //double theta_t_s = .0; // Assume that the tilt on the target is 0[deg].
// Latter we can put as the tilt of the LRF
// Transformation to camera system, r_t_c = Target vector in camera system [m]
// Convert from Spherical to Decart
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), // then Move basis by core_vector_
-C3D_SIN(DTOR * theta_t_s) ) - core_vector_ ); // Convert back Decart to Spherical
params.pan = static_cast<float> (RTOD * C3D_ATAN2(r_t_c[1], r_t_c[0])); // Pan, in degree
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
params.dist = static_cast<float> (sqrt(r_t_c * r_t_c)); // Range, in meters
}
void transform_3::get_vector_world( vector_3 target, vector_3 ¶m ){ param = ( core_matrix_ * target ) + core_vector_ ;};
void transform_3::get_vector_local( vector_3 target, vector_3 ¶m ){ param = ( ~core_matrix_ ) * ( target - core_vector_ );};
|