Changeset 1954 in orxonox.OLD for orxonox/branches/chris
- Timestamp:
- Jun 15, 2004, 4:52:33 PM (20 years ago)
- Location:
- orxonox/branches/chris/core
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/chris/core/vector.cc
r1939 r1954 168 168 return f * 180 / PI; 169 169 } 170 171 /** 172 \brief create a rotation from a vector 173 \param v: a vector 174 */ 175 Rotation::Rotation (const Vector& v) 176 { 177 Vector x = Vector( 1, 0, 0); 178 Vector axis = x.cross( v); 179 axis.normalize(); 180 float angle = angle_rad( x, v); 181 float ca = cos(angle); 182 float sa = sin(angle); 183 m[0] = 1.0f+(1.0f-ca)*(axis.x*axis.x-1.0f); 184 m[1] = -axis.z*sa+(1.0f-ca)*axis.x*axis.y; 185 m[2] = axis.y*sa+(1.0f-ca)*axis.x*axis.z; 186 m[3] = axis.z*sa+(1.0f-ca)*axis.x*axis.y; 187 m[4] = 1.0f+(1.0f-ca)*(axis.y*axis.y-1.0f); 188 m[5] = -axis.x*sa+(1.0f-ca)*axis.y*axis.z; 189 m[6] = -axis.y*sa+(1.0f-ca)*axis.x*axis.z; 190 m[7] = axis.x*sa+(1.0f-ca)*axis.y*axis.z; 191 m[8] = 1.0f+(1.0f-ca)*(axis.z*axis.z-1.0f); 192 } 193 194 /** 195 \brief creates a rotation from an axis and an angle (radians!) 196 \param axis: the rotational axis 197 \param angle: the angle in radians 198 */ 199 Rotation::Rotation (const Vector& axis, float angle) 200 { 201 float ca, sa; 202 ca = cos(angle); 203 sa = sin(angle); 204 m[0] = 1.0f+(1.0f-ca)*(axis.x*axis.x-1.0f); 205 m[1] = -axis.z*sa+(1.0f-ca)*axis.x*axis.y; 206 m[2] = axis.y*sa+(1.0f-ca)*axis.x*axis.z; 207 m[3] = axis.z*sa+(1.0f-ca)*axis.x*axis.y; 208 m[4] = 1.0f+(1.0f-ca)*(axis.y*axis.y-1.0f); 209 m[5] = -axis.x*sa+(1.0f-ca)*axis.y*axis.z; 210 m[6] = -axis.y*sa+(1.0f-ca)*axis.x*axis.z; 211 m[7] = axis.x*sa+(1.0f-ca)*axis.y*axis.z; 212 m[8] = 1.0f+(1.0f-ca)*(axis.z*axis.z-1.0f); 213 } 214 215 /** 216 \brief creates a rotation from euler angles (pitch/yaw/roll) 217 \param pitch: rotation around z (in radians) 218 \param yaw: rotation around y (in radians) 219 \param roll: rotation around x (in radians) 220 */ 221 Rotation::Rotation ( float pitch, float yaw, float roll) 222 { 223 float cy, sy, cr, sr, cp, sp; 224 cy = cos(yaw); 225 sy = sin(yaw); 226 cr = cos(roll); 227 sr = sin(roll); 228 cp = cos(pitch); 229 sp = sin(pitch); 230 m[0] = cy*cr; 231 m[1] = -cy*sr; 232 m[2] = sy; 233 m[3] = cp*sr+sp*sy*cr; 234 m[4] = cp*cr-sp*sr*sy; 235 m[5] = -sp*cy; 236 m[6] = sp*sr-cp*sy*cr; 237 m[7] = sp*cr+cp*sy*sr; 238 m[8] = cp*cy; 239 } 240 241 /** 242 \brief creates a nullrotation 243 */ 244 Rotation::Rotation () 245 { 246 m[0] = 1.0f; 247 m[1] = 0.0f; 248 m[2] = 0.0f; 249 m[3] = 0.0f; 250 m[4] = 1.0f; 251 m[5] = 0.0f; 252 m[6] = 0.0f; 253 m[7] = 0.0f; 254 m[8] = 1.0f; 255 } 256 257 /** 258 \brief rotates the vector by the given rotation 259 \param v: a vector 260 \param r: a rotation 261 */ 262 Vector rotate_vector( const Vector& v, const Rotation& r) 263 { 264 Vector t; 265 266 t.x = v.x * r.m[0] + v.y * r.m[1] + v.z * r.m[2]; 267 t.y = v.x * r.m[3] + v.y * r.m[4] + v.z * r.m[5]; 268 t.z = v.x * r.m[6] + v.y * r.m[7] + v.z * r.m[8]; 269 270 return t; 271 } 272 273 /** 274 \brief calculate the distance between two lines 275 \param l: the other line 276 */ 277 float Line::distance (const Line& l) const 278 { 279 float q, d; 280 Vector n = a.cross(l.a); 281 q = n.dot(r-l.r); 282 d = n.len(); 283 if( d == 0.0) return 0.0; 284 return q/d; 285 } 286 287 /** 288 \brief calculate the distance between a line and a point 289 \param v: the point 290 */ 291 float Line::distance_point (const Vector& v) const 292 { 293 Vector d = v-r; 294 Vector u = a * d.dot( a); 295 return (d - u).len(); 296 } 297 298 /** 299 \brief calculate the two points of minimal distance of two lines 300 \param l: the other line 301 */ 302 Vector* Line::footpoints (const Line& l) const 303 { 304 Vector* fp = new Vector[2]; 305 Plane p = Plane (r + a.cross(l.a), r, r + a); 306 fp[1] = p.intersect_line (l); 307 p = Plane (fp[1], l.a); 308 fp[0] = p.intersect_line (*this); 309 return fp; 310 } 311 312 /** 313 \brief calculate the length of a line 314 */ 315 float Line::len() const 316 { 317 return a.len(); 318 } 319 320 /** 321 \brief rotate the line by given rotation 322 \param rot: a rotation 323 */ 324 void Line::rotate (const Rotation& rot) 325 { 326 Vector t = a + r; 327 t = rotate_vector( t, rot); 328 r = rotate_vector( r, rot), 329 a = t - r; 330 } 331 332 /** 333 \brief create a plane from three lines 334 \param a: first point 335 \param b: second point 336 \param c: third point 337 */ 338 Plane::Plane (Vector a, Vector b, Vector c) 339 { 340 n = (a-b).cross(c-b); 341 k = -(n.x*b.x+n.y*b.y+n.z*b.z); 342 } 343 344 /** 345 \brief create a plane from anchor point and normal 346 \param n: normal vector 347 \param p: anchor point 348 */ 349 Plane::Plane (Vector norm, Vector p) 350 { 351 n = norm; 352 k = -(n.x*p.x+n.y*p.y+n.z*p.z); 353 } 354 355 /** 356 \brief returns the intersection point between the plane and a line 357 \param l: a line 358 */ 359 Vector Plane::intersect_line (const Line& l) const 360 { 361 if (n.x*l.a.x+n.y*l.a.y+n.z*l.a.z == 0.0) return Vector(0,0,0); 362 float t = (n.x*l.r.x+n.y*l.r.y+n.z*l.r.z+k) / (n.x*l.a.x+n.y*l.a.y+n.z*l.a.z); 363 return l.r + (l.a * t); 364 } 365 366 /** 367 \brief returns the distance between the plane and a point 368 \param p: a Point 369 */ 370 float Plane::distance_point (const Vector& p) const 371 { 372 float l = n.len(); 373 if( l == 0.0) return 0.0; 374 return (n.dot(p) + k) / n.len(); 375 } 376 377 /** 378 \brief returns the side a point is located relative to a plane 379 \param p: a Point 380 */ 381 float Plane::locate_point (const Vector& p) const 382 { 383 return (n.dot(p) + k); 384 } -
orxonox/branches/chris/core/vector.h
r1939 r1954 3 3 #define VECTOR_H 4 4 5 #include "data_tank.h"6 # include "stdincl.h"5 #include <math.h> 6 #define PI 3.14159265359f 7 7 8 8 class Vector { … … 30 30 float angle_rad (const Vector& v1, const Vector& v2); 31 31 32 float line_distance (Vector* l1, Vector* l2); 33 34 class Rotation { 35 public: 36 37 float m[9]; 38 39 Rotation ( const Vector& v); 40 Rotation ( const Vector& axis, float angle); 41 Rotation ( float pitch, float yaw, float roll); 42 Rotation (); 43 ~Rotation () {} 44 45 }; 46 47 Vector rotate_vector( const Vector& v, const Rotation& r); 48 49 class Line 50 { 51 public: 52 53 Vector r, a; 54 55 Line ( Vector r, Vector a) : r(r), a(a) {} 56 Line () : r(Vector(0,0,0)), a(Vector (1,1,1)) {} 57 ~Line () {} 58 59 float distance (const Line& l) const; 60 float distance_point (const Vector& v) const; 61 Vector* footpoints (const Line& l) const; 62 float len () const; 63 64 void rotate(const Rotation& rot); 65 }; 66 67 class Plane 68 { 69 public: 70 71 Vector n; 72 float k; 73 74 Plane (Vector a, Vector b, Vector c); 75 Plane (Vector norm, Vector p); 76 Plane (Vector n, float k) : n(n), k(k) {} 77 Plane () : n(Vector(1,1,1)), k(0) {} 78 ~Plane () {} 79 80 Vector intersect_line (const Line& l) const; 81 float distance_point (const Vector& p) const; 82 float locate_point (const Vector& p) const; 83 }; 32 84 33 85 #endif
Note: See TracChangeset
for help on using the changeset viewer.