00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __VECTOR__
00024 #define __VECTOR__
00025
00026 #include <stdio.h>
00027
00028
00029 #include "jed_defs.h"
00030
00031 #define DISTANCIA_V(a, b) (DISTANCIA( (a).x, (a).y, (a).z,\
00032 (b).x, (b).y, (b).z ))
00033 #define MAGNITUD(a) (DISTANCIA( (a).x, (a).y, (a).z,\
00034 0.0, 0.0 ,0.0 ))
00035
00036 class VECTOR {
00037 public:
00038 double x;
00039 double y;
00040 double z;
00041
00042 VECTOR ();
00043 VECTOR (double ax, double ay, double az);
00044 inline VECTOR producto_cruz(VECTOR v2);
00045 inline double producto_punto(VECTOR v2);
00046 inline void punto_medio(VECTOR a, VECTOR b);
00047 inline void normalizar(void);
00048 inline double norma(void);
00049 inline void direccion_euler(double *yaw, double *pitch);
00050 inline VECTOR operator + (VECTOR a);
00051 inline VECTOR operator - (VECTOR a);
00052 inline VECTOR operator * (double c);
00053 inline void operator *= (double c);
00054 inline VECTOR operator / (double c);
00055 inline void operator /= (double c);
00056 inline void rotar_x(double);
00057 inline void rotar_y(double);
00058 inline void rotar_z(double);
00059 void imprimir(void);
00060 };
00061
00062
00063
00064
00065
00066 inline double
00067 VECTOR::norma(void)
00070 {
00071 return DISTANCIA(0.0, 0.0, 0.0, x, y, z);
00072 }
00073
00074 inline VECTOR
00075 VECTOR::producto_cruz(VECTOR v2)
00079 {
00080 return VECTOR ( y*v2.z - v2.y*z,
00081 -(x*v2.z - v2.x*z),
00082 x*v2.y - v2.x*y );
00083 }
00084
00085 inline double
00086 VECTOR::producto_punto(VECTOR v2)
00089 {
00090 return ( (x*v2.x) + (y*v2.y) + (z*v2.z) );
00091 }
00092
00093 inline VECTOR
00094 VECTOR::operator * (double c)
00098 {
00099 VECTOR v;
00100
00101 v.x = c*this->x;
00102 v.y = c*this->y;
00103 v.z = c*this->z;
00104
00105 return v;
00106 }
00107
00108 inline void
00109 VECTOR::operator *= (double c)
00113 {
00114 x *= c;
00115 y *= c;
00116 z *= c;
00117 }
00118
00119 inline void
00120 VECTOR::operator /= (double c)
00124 {
00125 x /= c;
00126 y /= c;
00127 z /= c;
00128 }
00129
00130 inline VECTOR
00131 VECTOR ::operator / (double c)
00135 {
00136 VECTOR v;
00137
00138 if ( IGUAL(c, 0.0) ){
00139 fprintf(stderr,"Error: Division por cero\n");
00140 return v;
00141 }
00142 else{
00143 v.x = (this->x)/c;
00144 v.y = (this->y)/c;
00145 v.z = (this->z)/c;
00146 }
00147
00148 return v;
00149 }
00150
00151 inline VECTOR
00152 VECTOR ::operator + (VECTOR a)
00156 {
00157 VECTOR v;
00158
00159 v.x = this->x + a.x;
00160 v.y = this->y + a.y;
00161 v.z = this->z + a.z;
00162
00163 return v;
00164 }
00165
00166 inline VECTOR
00167 VECTOR ::operator - (VECTOR a)
00171 {
00172 VECTOR v;
00173
00174 v.x = this->x - a.x;
00175 v.y = this->y - a.y;
00176 v.z = this->z - a.z;
00177
00178 return v;
00179 }
00180
00181 inline void
00182 VECTOR::normalizar(void)
00185 {
00186 double norma = DISTANCIA(0.0, 0.0, 0.0, x, y, z);
00187 norma = 1/norma;
00188 (*this) = (*this) * norma;
00189 }
00190
00191 inline void
00192 VECTOR::direccion_euler(double *yaw, double *pitch)
00197 {
00198 VECTOR yo_normal = (*this);
00199 double sp;
00200
00201 yo_normal.normalizar();
00202
00203 (*pitch) = acos(yo_normal.z);
00204 sp = sin(*pitch);
00205 if ( fabs(sp) < EPSILON ) {
00206 (*yaw) = 0;
00207 }
00208 else {
00209 yo_normal.z = 0;
00210 yo_normal.normalizar();
00211 if ( yo_normal.y > 0) {
00212 (*yaw) = acos(yo_normal.x);
00213 }
00214 else {
00215 (*yaw) = -acos(yo_normal.x);
00216 }
00217 }
00218 (*pitch) -= DEG2RAD(90);
00219 (*pitch) *= -1;
00220 }
00221
00222 inline void
00223 VECTOR::punto_medio(VECTOR a, VECTOR b)
00224
00226 {
00227 x = (a.x + b.x)/2.0;
00228 y = (a.y + b.y)/2.0;
00229 z = (a.z + b.z)/2.0;
00230 }
00231
00232
00233
00234 inline void
00235 VECTOR::rotar_y(double angulo)
00238 {
00239 double z_ant;
00240
00241 z_ant=z;
00242 z= z_ant*cos(angulo) - x*sin(angulo);
00243 x= z_ant*sin(angulo) + x*cos(angulo);
00244 }
00245
00246 inline void
00247 VECTOR::rotar_x(double angulo)
00250 {
00251 double y_ant;
00252
00253 y_ant=y;
00254 y= y_ant*cos(angulo) - z*sin(angulo);
00255 z= y_ant*sin(angulo) + z*cos(angulo);
00256 }
00257
00258 inline void
00259 VECTOR::rotar_z(double angulo)
00262 {
00263 double x_ant;
00264
00265 x_ant=x;
00266 x= x_ant*cos(angulo) - y*sin(angulo);
00267 y= x_ant*sin(angulo) + y*cos(angulo);
00268 }
00269
00270 #endif
00271
00272
00273
00274
00275