00001 // Filename: lvector3_src.I 00002 // Created by: drose (24Sep99) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 00020 //////////////////////////////////////////////////////////////////// 00021 // Function: LVector3::Default Constructor 00022 // Access: Public 00023 // Description: 00024 //////////////////////////////////////////////////////////////////// 00025 INLINE_LINMATH FLOATNAME(LVector3):: 00026 FLOATNAME(LVector3)() { 00027 } 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Function: LVector3::Copy Constructor 00031 // Access: Public 00032 // Description: 00033 //////////////////////////////////////////////////////////////////// 00034 INLINE_LINMATH FLOATNAME(LVector3):: 00035 FLOATNAME(LVector3)(const FLOATNAME(LVecBase3) ©) : FLOATNAME(LVecBase3)(copy) { 00036 } 00037 00038 //////////////////////////////////////////////////////////////////// 00039 // Function: LVector3::Copy Assignment Operator 00040 // Access: Public 00041 // Description: 00042 //////////////////////////////////////////////////////////////////// 00043 INLINE_LINMATH FLOATNAME(LVector3) &FLOATNAME(LVector3):: 00044 operator = (const FLOATNAME(LVecBase3) ©) { 00045 FLOATNAME(LVecBase3)::operator = (copy); 00046 return *this; 00047 } 00048 00049 //////////////////////////////////////////////////////////////////// 00050 // Function: LVector3::Copy Fill Operator 00051 // Access: Public 00052 // Description: 00053 //////////////////////////////////////////////////////////////////// 00054 INLINE_LINMATH FLOATNAME(LVector3) &FLOATNAME(LVector3):: 00055 operator = (FLOATTYPE fill_value) { 00056 FLOATNAME(LVecBase3)::operator = (fill_value); 00057 return *this; 00058 } 00059 00060 //////////////////////////////////////////////////////////////////// 00061 // Function: LVector3::Constructor 00062 // Access: Public 00063 // Description: 00064 //////////////////////////////////////////////////////////////////// 00065 INLINE_LINMATH FLOATNAME(LVector3):: 00066 FLOATNAME(LVector3)(FLOATTYPE fill_value) : 00067 FLOATNAME(LVecBase3)(fill_value) 00068 { 00069 } 00070 00071 //////////////////////////////////////////////////////////////////// 00072 // Function: LVector3::Constructor 00073 // Access: Public 00074 // Description: 00075 //////////////////////////////////////////////////////////////////// 00076 INLINE_LINMATH FLOATNAME(LVector3):: 00077 FLOATNAME(LVector3)(FLOATTYPE x, FLOATTYPE y, FLOATTYPE z) : 00078 FLOATNAME(LVecBase3)(x, y, z) 00079 { 00080 } 00081 00082 //////////////////////////////////////////////////////////////////// 00083 // Function: LVector3::zero Named Constructor 00084 // Access: Public 00085 // Description: Returns a zero-length vector. 00086 //////////////////////////////////////////////////////////////////// 00087 INLINE_LINMATH const FLOATNAME(LVector3) &FLOATNAME(LVector3):: 00088 zero() { 00089 return (const FLOATNAME(LVector3) &)FLOATNAME(LVecBase3)::zero(); 00090 } 00091 00092 //////////////////////////////////////////////////////////////////// 00093 // Function: LVector3::unit_x Named Constructor 00094 // Access: Public 00095 // Description: Returns a unit X vector. 00096 //////////////////////////////////////////////////////////////////// 00097 INLINE_LINMATH const FLOATNAME(LVector3) &FLOATNAME(LVector3):: 00098 unit_x() { 00099 return (const FLOATNAME(LVector3) &)FLOATNAME(LVecBase3)::unit_x(); 00100 } 00101 00102 //////////////////////////////////////////////////////////////////// 00103 // Function: LVector3::unit_y Named Constructor 00104 // Access: Public 00105 // Description: Returns a unit Y vector. 00106 //////////////////////////////////////////////////////////////////// 00107 INLINE_LINMATH const FLOATNAME(LVector3) &FLOATNAME(LVector3):: 00108 unit_y() { 00109 return (const FLOATNAME(LVector3) &)FLOATNAME(LVecBase3)::unit_y(); 00110 } 00111 00112 //////////////////////////////////////////////////////////////////// 00113 // Function: LVector3::unit_z Named Constructor 00114 // Access: Public 00115 // Description: Returns a unit Z vector. 00116 //////////////////////////////////////////////////////////////////// 00117 INLINE_LINMATH const FLOATNAME(LVector3) &FLOATNAME(LVector3):: 00118 unit_z() { 00119 return (const FLOATNAME(LVector3) &)FLOATNAME(LVecBase3)::unit_z(); 00120 } 00121 00122 //////////////////////////////////////////////////////////////////// 00123 // Function: LVector3::unary - 00124 // Access: Public 00125 // Description: 00126 //////////////////////////////////////////////////////////////////// 00127 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00128 operator - () const { 00129 return FLOATNAME(LVecBase3)::operator - (); 00130 } 00131 00132 //////////////////////////////////////////////////////////////////// 00133 // Function: LVector3::vector + vecbase 00134 // Access: Public 00135 // Description: 00136 //////////////////////////////////////////////////////////////////// 00137 INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LVector3):: 00138 operator + (const FLOATNAME(LVecBase3) &other) const { 00139 return FLOATNAME(LVecBase3)::operator + (other); 00140 } 00141 00142 //////////////////////////////////////////////////////////////////// 00143 // Function: LVector3::vector + vector 00144 // Access: Public 00145 // Description: 00146 //////////////////////////////////////////////////////////////////// 00147 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00148 operator + (const FLOATNAME(LVector3) &other) const { 00149 return FLOATNAME(LVecBase3)::operator + (other); 00150 } 00151 00152 //////////////////////////////////////////////////////////////////// 00153 // Function: LVector3::vector - vecbase 00154 // Access: Public 00155 // Description: 00156 //////////////////////////////////////////////////////////////////// 00157 INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LVector3):: 00158 operator - (const FLOATNAME(LVecBase3) &other) const { 00159 return FLOATNAME(LVecBase3)::operator - (other); 00160 } 00161 00162 //////////////////////////////////////////////////////////////////// 00163 // Function: LVector3::vector - vector 00164 // Access: Public 00165 // Description: 00166 //////////////////////////////////////////////////////////////////// 00167 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00168 operator - (const FLOATNAME(LVector3) &other) const { 00169 return FLOATNAME(LVecBase3)::operator - (other); 00170 } 00171 00172 //////////////////////////////////////////////////////////////////// 00173 // Function: LVector3::length 00174 // Access: Public 00175 // Description: Returns the length of the vector, by the Pythagorean 00176 // theorem. 00177 //////////////////////////////////////////////////////////////////// 00178 INLINE_LINMATH FLOATTYPE FLOATNAME(LVector3):: 00179 length() const { 00180 return csqrt((*this).dot(*this)); 00181 } 00182 00183 //////////////////////////////////////////////////////////////////// 00184 // Function: LVector3::length_squared 00185 // Access: Public 00186 // Description: Returns the square of the vector's length, cheap and 00187 // easy. 00188 //////////////////////////////////////////////////////////////////// 00189 INLINE_LINMATH FLOATTYPE FLOATNAME(LVector3):: 00190 length_squared() const { 00191 return (*this).dot(*this); 00192 } 00193 00194 //////////////////////////////////////////////////////////////////// 00195 // Function: LVector3::normalize 00196 // Access: Public 00197 // Description: Normalizes the vector in place. Returns true if the 00198 // vector was normalized, false if it was a zero-length 00199 // vector. 00200 //////////////////////////////////////////////////////////////////// 00201 INLINE_LINMATH bool FLOATNAME(LVector3):: 00202 normalize() { 00203 FLOATTYPE l2 = length_squared(); 00204 if (l2 == (FLOATTYPE)0.0f) { 00205 set(0.0f, 0.0f, 0.0f); 00206 return false; 00207 00208 } else if (!IS_THRESHOLD_EQUAL(l2, 1.0f, (NEARLY_ZERO(FLOATTYPE) * NEARLY_ZERO(FLOATTYPE)))) { 00209 (*this) /= csqrt(l2); 00210 } 00211 00212 return true; 00213 } 00214 00215 //////////////////////////////////////////////////////////////////// 00216 // Function: LVector3::cross 00217 // Access: Public 00218 // Description: 00219 //////////////////////////////////////////////////////////////////// 00220 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00221 cross(const FLOATNAME(LVecBase3) &other) const { 00222 return FLOATNAME(LVecBase3)::cross(other); 00223 } 00224 00225 //////////////////////////////////////////////////////////////////// 00226 // Function: LVector3::operator * scalar 00227 // Access: Public 00228 // Description: 00229 //////////////////////////////////////////////////////////////////// 00230 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00231 operator * (FLOATTYPE scalar) const { 00232 return FLOATNAME(LVector3)(FLOATNAME(LVecBase3)::operator * (scalar)); 00233 } 00234 00235 //////////////////////////////////////////////////////////////////// 00236 // Function: LVector3::operator / scalar 00237 // Access: Public 00238 // Description: 00239 //////////////////////////////////////////////////////////////////// 00240 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00241 operator / (FLOATTYPE scalar) const { 00242 FLOATTYPE recip_scalar = 1.0f/scalar; 00243 return FLOATNAME(LVector3)(FLOATNAME(LVecBase3)::operator * (recip_scalar)); 00244 } 00245 00246 //////////////////////////////////////////////////////////////////// 00247 // Function: LVector3::up 00248 // Access: Public, Static 00249 // Description: Returns the up vector for the given coordinate 00250 // system. 00251 //////////////////////////////////////////////////////////////////// 00252 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00253 up(CoordinateSystem cs) { 00254 if (cs == CS_default) { 00255 cs = default_coordinate_system; 00256 } 00257 switch (cs) { 00258 case CS_zup_right: 00259 case CS_zup_left: 00260 return FLOATNAME(LVector3)(0.0f, 0.0f, 1.0f); 00261 00262 case CS_yup_right: 00263 case CS_yup_left: 00264 return FLOATNAME(LVector3)(0.0f, 1.0f, 0.0f); 00265 00266 default: 00267 linmath_cat.error() 00268 << "Invalid coordinate system!\n"; 00269 return FLOATNAME(LVector3)(0.0f, 0.0f, 0.0f); 00270 } 00271 } 00272 00273 //////////////////////////////////////////////////////////////////// 00274 // Function: LVector3::right 00275 // Access: Public, Static 00276 // Description: Returns the right vector for the given coordinate 00277 // system. 00278 //////////////////////////////////////////////////////////////////// 00279 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00280 right(CoordinateSystem) { 00281 return FLOATNAME(LVector3)(1.0f, 0.0f, 0.0f); 00282 } 00283 00284 //////////////////////////////////////////////////////////////////// 00285 // Function: LVector3::forward 00286 // Access: Public, Static 00287 // Description: Returns the forward vector for the given coordinate 00288 // system. 00289 //////////////////////////////////////////////////////////////////// 00290 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00291 forward(CoordinateSystem cs) { 00292 if (cs == CS_default) { 00293 cs = default_coordinate_system; 00294 } 00295 switch (cs) { 00296 case CS_zup_right: 00297 return FLOATNAME(LVector3)(0.0f, 1.0f, 0.0f); 00298 00299 case CS_zup_left: 00300 return FLOATNAME(LVector3)(0.0f, -1.0f, 0.0f); 00301 00302 case CS_yup_right: 00303 return FLOATNAME(LVector3)(0.0f, 0.0f, -1.0f); 00304 00305 case CS_yup_left: 00306 return FLOATNAME(LVector3)(0.0f, 0.0f, 1.0f); 00307 00308 default: 00309 linmath_cat.error() 00310 << "Invalid coordinate system!\n"; 00311 return FLOATNAME(LVector3)(0.0f, 0.0f, 0.0f); 00312 } 00313 } 00314 00315 //////////////////////////////////////////////////////////////////// 00316 // Function: LVector3::down 00317 // Access: Public, Static 00318 // Description: Returns the down vector for the given coordinate 00319 // system. 00320 //////////////////////////////////////////////////////////////////// 00321 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00322 down(CoordinateSystem cs) { 00323 return -up(cs); 00324 } 00325 00326 //////////////////////////////////////////////////////////////////// 00327 // Function: LVector3::left 00328 // Access: Public, Static 00329 // Description: Returns the left vector for the given coordinate 00330 // system. 00331 //////////////////////////////////////////////////////////////////// 00332 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00333 left(CoordinateSystem cs) { 00334 return -right(cs); 00335 } 00336 00337 //////////////////////////////////////////////////////////////////// 00338 // Function: LVector3::back 00339 // Access: Public, Static 00340 // Description: Returns the back vector for the given coordinate 00341 // system. 00342 //////////////////////////////////////////////////////////////////// 00343 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00344 back(CoordinateSystem cs) { 00345 return -forward(cs); 00346 } 00347 00348 //////////////////////////////////////////////////////////////////// 00349 // Function: LVector3::rfu 00350 // Access: Public, Static 00351 // Description: Returns a vector that is described by its right, 00352 // forward, and up components, in whatever way the 00353 // coordinate system represents that vector. 00354 //////////////////////////////////////////////////////////////////// 00355 00356 //INLINE_LINMATH FLOATNAME(LVector3) & FLOATNAME(LVector3):: 00357 INLINE_LINMATH FLOATNAME(LVector3) FLOATNAME(LVector3):: 00358 rfu(FLOATTYPE right_v, FLOATTYPE fwd_v, FLOATTYPE up_v, 00359 CoordinateSystem cs) { 00360 00361 /* return forward(cs) * fwd_v + up(cs) * up_v + right(cs) * right_v; */ 00362 00363 // fast implementation of above for axis-aligned coordinate systems 00364 00365 00366 if (cs == CS_default) { 00367 cs = default_coordinate_system; 00368 } 00369 FLOATTYPE vy,vz; 00370 switch(cs) { 00371 case CS_yup_right: 00372 vz = -fwd_v; 00373 vy = up_v; 00374 break; 00375 00376 case CS_yup_left: 00377 vz = fwd_v; 00378 vy = up_v; 00379 break; 00380 00381 case CS_zup_right: 00382 vy = fwd_v; 00383 vz = up_v; 00384 break; 00385 00386 case CS_zup_left: 00387 vy = -fwd_v; 00388 vz = up_v; 00389 00390 default: 00391 linmath_cat.error() 00392 << "Invalid coordinate system!\n"; 00393 return FLOATNAME(LVector3)(0.0f, 0.0f, 0.0f); 00394 } 00395 00396 return FLOATNAME(LVector3)(right_v,vy,vz); 00397 } 00398