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.
 
 
 
 
 
 

403 lines
11 KiB

//----------------------------------------------------------------------------
// 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"
#ifdef DEBUG
#include "../../components/base/basicLogCollection.h"
#define LOG(X) basicLogCollection::getInstance()->Log("TRANS", NULL, X)
#define LOG_ERROR(X) \
{ \
char _tBfrErrMSG2w34[256]; \
sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
basicLogCollection::getInstance()->Log("TRANS", NULL, _LOG_ERROR, _tBfrErrMSG2w34); \
}
#endif
C3D_FLOAT getDistance(C3D_DECART A, C3D_DECART B)
{
C3D_FLOAT dx2 = C3D_POW(A.x - B.x, 2.0f);
C3D_FLOAT dy2 = C3D_POW(A.y - B.y, 2.0f);
C3D_FLOAT dz2 = C3D_POW(A.z - B.z, 2.0f);
return C3D_SQRT(dx2 + dy2 + dz2);
}
void spherical2decart(C3D_SPH_DEGREES in, C3D_DECART & out)
{
C3D_FLOAT tT, tF;
tT = deg2rad(in.tetta);
tF = deg2rad(in.fi);
#ifdef DEBUG_9908
char tB[128];
sprintf(tB, "IN D> R:%0.5f T:%0.5f F:%0.5f", in.r, in.tetta, in.fi);
LOG(tB);
sprintf(tB, "IN R> R:%0.5f T:%0.5f F:%0.5f", in.r, tT, tF);
LOG(tB);
#endif
out.x = in.r * C3D_COS(tF) * C3D_SIN(tT);
out.y = in.r * C3D_SIN(tF) * C3D_SIN(tT);
out.z = in.r * C3D_COS(tT);
#ifdef DEBUG_8898
sprintf(tB, "OUT> X:%0.5f Y:%0.5f Z:%0.5f", out.x, out.y, out.z);
LOG(tB);
#endif
}
void decart2spherical(C3D_DECART in, C3D_SPH_DEGREES & out)
{
unsigned char oct;
out.r = C3D_SQRT(C3D_POW(in.x, 2.0f) + C3D_POW(in.y, 2.0f) + C3D_POW(in.z, 2.0f));
out.fi = rad2deg(C3D_ATAN(in.y / in.x));
out.tetta = rad2deg(C3D_ACOS(in.z / out.r));
oct = ((in.x < 0.0f) ? 1 : 0) | ((in.y < 0.0f) ? 2 : 0) | ((in.z < 0.0f) ? 4 : 0);
switch (oct)
{
case 0:
case 4: break;
case 1:
case 3:
case 5:
case 7: out.fi += 180.0f; break;
case 2:
case 6: out.fi += 360.0f; break;
};
}
void decart2spherical(C3D_DECART in, C3D_SPH_RADIANS & out)
{
unsigned char oct;
out.r = C3D_SQRT(C3D_POW(in.x, 2.0f) + C3D_POW(in.y, 2.0f) + C3D_POW(in.z, 2.0f));
out.fi = C3D_ATAN(in.y / in.x);
out.tetta = C3D_PI2 - C3D_ACOS(in.z / out.r);
oct = ((in.x < 0.0f) ? 1 : 0) | ((in.y < 0.0f) ? 2 : 0) | ((in.z < 0.0f) ? 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 spherical2decart(C3D_SPH_RADIANS in, C3D_DECART & out)
{
out.x = in.r * C3D_COS(in.fi) * C3D_SIN(in.tetta);
out.y = in.r * C3D_SIN(in.fi) * C3D_SIN(in.tetta);
out.z = in.r * C3D_COS(in.tetta);
}
void spherical2spherical(C3D_SPH_DEGREES in, C3D_SPH_RADIANS & out)
{
out.fi = deg2rad(in.fi);
out.tetta = deg2rad(in.tetta);
out.r = in.r;
}
void spherical2spherical(C3D_SPH_RADIANS in, C3D_SPH_DEGREES & out)
{
out.fi = rad2deg(in.fi);
out.tetta = rad2deg(in.tetta);
out.r = in.r;
}
void spherical2onvif(C3D_SPH_DEGREES in, C3D_ORIENT_ONVIF & out, float maxR, uint8_t panMode, float panAngles, uint8_t tiltMode, float tiltAngles)
{
out.zoom = .0f;
out.tilt = .0f;
out.pan = .0f;
if (maxR > 0.0f)
{
if (in.r > maxR)
{
out.zoom = 1.0f;
}
else
{
out.zoom = in.r / maxR;
};
}
else
{
out.zoom = .0f;
}
switch (panMode)
{
case 1:
{
out.pan = 1.0f - ((2.0f / panAngles) * in.fi);
break;
};
case 2:
default:
{
out.pan = (2.0f / panAngles) * in.fi - 1.0f;
};
};
switch (tiltMode)
{
case 0:
{
out.tilt = -2.0f * ((tiltAngles - in.tetta) / tiltAngles) + 1.0f;
break;
};
case 1:
{
out.tilt = -1.0f * (-2.0f * ((tiltAngles - in.tetta) / tiltAngles) + 1.0f);
break;
};
case 2:
default:
{
out.tilt = (2.0f / tiltAngles) * (tiltAngles - in.tetta) - 1.0f;
break;
};
};
}
void spherical2onvif(C3D_SPH_RADIANS inr, C3D_ORIENT_ONVIF & out, float maxR, uint8_t panMode, float panAngles, uint8_t tiltMode, float tiltAngles)
{
C3D_SPH_DEGREES in;
spherical2spherical(inr, in);
spherical2onvif(in, out, maxR, panMode, panAngles, tiltMode, tiltAngles);
}
/*
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.141592653589793f;
const C3D_FLOAT transform_3::DTOR = 0.017453292519943f;
const C3D_FLOAT transform_3::RTOD = 57.29577951308232f;
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] += (float) 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] += (float) 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 Transform
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(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 & param)
{
param = (core_matrix_ * target) + core_vector_;
}
void transform_3::get_vector_local(vector_3 target, vector_3 & param)
{
param = (~core_matrix_) * (target - core_vector_);
}
#ifdef DEBUG
#undef LOG
#undef LOG_ERROR
#endif