Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

m_vector4.cpp

Go to the documentation of this file.
00001 // OMICRON ENGINE HEADER FILE
00002 //
00003 // --------------------------------------------------------------------------
00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de>
00005 //
00006 // This file is part of the Omicron Engine.
00007 //
00008 // The Omicron Engine is free software; you can redistribute it and/or modify
00009 // it under the terms of the  GNU General Public License  as published by the
00010 // Free Software Foundation;  either version  2  of the License,  or (at your
00011 // option) any later version.
00012 //
00013 // The Omicron Engine  is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00015 // or  FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
00016 // for more details.
00017 //
00018 // You should have  received a copy of the  GNU General Public License  along
00019 // with The Omicron Engine;  if not,  write to the  Free Software Foundation,
00020 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00021 //
00022 // --------------------------------------------------------------------------
00023 // Last modified:       $Date: 2002/12/07 19:02:06 $
00024 // By           :       $Author: kolrabi $
00025 // $Id: m_vector4.cpp,v 1.1.1.1 2002/12/07 19:02:06 kolrabi Exp $ 
00026 
00027 /*
00028 
00029   $Log: m_vector4.cpp,v $
00030   Revision 1.1.1.1  2002/12/07 19:02:06  kolrabi
00031   initial release
00032 
00033 
00034 */
00035  
00040 #include            "omicron/internal.h"
00041 #include            <math.h>
00042 
00043 const vec4_t vec4_c::vec4x   = { 1, 0, 0, 0 };
00044 const vec4_t vec4_c::vec4y   = { 0, 1, 0, 0 };
00045 const vec4_t vec4_c::vec4z   = { 0, 0, 1, 0 };
00046 const vec4_t vec4_c::vec4w   = { 0, 0, 0, 1 };
00047 const vec4_t vec4_c::vec40   = { 0, 0, 0, 0 };
00048 
00049 /**************************************************************************** 
00050  **************************************************************************** 
00051  * VECTOR ROUTINES ********************************************************** 
00052  **************************************************************************** 
00053  ****************************************************************************/ 
00054 
00055 vec4_c::vec4_c()
00056 {
00057     set(0,0,0,0);
00058 }
00059 
00060 vec4_c::vec4_c(const vec4_t v)
00061 {
00062     set(v[0], v[1], v[2], v[3]);
00063 }
00064 
00065 vec4_c::vec4_c(const vec4_c &v)
00066 {
00067     set(v.vec[0], v.vec[1], v.vec[2], v.vec[3]);
00068 }
00069 
00070 vec4_c::vec4_c(float x, float y, float z, float w)
00071 {
00072     set(x,y,z,w);
00073 }
00074 
00075 vec4_c::~vec4_c()
00076 {
00077 }
00078 
00079 vec4_c::operator const float*()
00080 {
00081     return vec;
00082 }
00083 
00084 // -- 
00085 
00086 void vec4_c::set(float x, float y, float z, float w)
00087 {
00088     vec[0] = x;
00089     vec[1] = y;
00090     vec[2] = z;
00091     vec[3] = w;
00092 }
00093 
00094 void vec4_c::set_component(int n, float x)
00095 {
00096     vec[n] = x;
00097 }
00098 
00099 void vec4_c::clear()
00100 {
00101     set(0,0,0,0);
00102 }
00103 
00104 float vec4_c::length()
00105 {
00106     return (float)sqrt(
00107         vec[VECT_X]*vec[VECT_X] +
00108         vec[VECT_Y]*vec[VECT_Y] +
00109         vec[VECT_Z]*vec[VECT_Z] +
00110         vec[VECT_W]*vec[VECT_W]
00111         );       // sqrts are slow :( 
00112 }
00113 
00114 float vec4_c::length_squared()
00115 {
00116     return vec[VECT_X]*vec[VECT_X] + vec[VECT_Y]*vec[VECT_Y] + vec[VECT_Z]*vec[VECT_Z] + vec[VECT_W]*vec[VECT_W];
00117 }
00118 
00119 bool vec4_c::operator ==(const vec4_t v)
00120 {
00121     ulong     i; 
00122 
00123     AssertReturnValue1(v, false);
00124 
00125     for (i=0 ; ++i<4;) 
00126         if (fabs(vec[i]-v[i]) > EQUAL_EPSILON) 
00127             return false; 
00128  
00129     return true; 
00130 }
00131 
00132 vec4_c vec4_c::operator +(const vec4_t v)
00133 {
00134     return vec4_c(v[0]+vec[0], v[1]+vec[1], v[2]+vec[2], v[3]+vec[3]);
00135 }
00136 
00137 vec4_c vec4_c::operator -(const vec4_t v)
00138 {
00139     return vec4_c(vec[0]-v[0], vec[1]-v[1], vec[2]-v[2], vec[3]-v[3]);
00140 }
00141 
00142 vec4_c vec4_c::operator *(const float f)
00143 {
00144     return vec4_c(f*vec[0], f*vec[1], f*vec[2], f*vec[3]);
00145 }
00146 
00147 vec4_c vec4_c::operator *(const vec4_t v)
00148 {
00149     return vec4_c(vec[0]*v[0], vec[1]*v[1], vec[2]*v[2], vec[3]*v[3]);
00150 }
00151 
00152 /**************************************************************************** 
00153  * vector_dot                                        calculate dot product * 
00154  ****************************************************************************/
00155 float vec4_c::dot
00156 ( 
00157     const vec4_t v
00158 ) 
00159 { 
00160     AssertReturnValue1(v, 0);
00161 
00162     return  v[VECT_X]*vec[VECT_X] + 
00163             v[VECT_Y]*vec[VECT_Y] +
00164             v[VECT_Z]*vec[VECT_Z] +
00165             v[VECT_W]*vec[VECT_W]
00166             ; 
00167 } // vector_dot 
00168 
00169 vec4_c vec4_c::ma(const vec_t s, const vec4_t v)
00170 {
00171     return *this+vec4_c(v)*s;
00172 }
00173 
00174 vec4_c vec4_c::ma_vec(const vec4_t s, const vec4_t v)
00175 {
00176     return *this+vec4_c(v)*s;
00177 }
00178 
00179 vec4_c vec4_c::operator =(const vec4_t v)
00180 {
00181     vec[0] = v[0];
00182     vec[1] = v[1];
00183     vec[2] = v[2];
00184     vec[3] = v[3];
00185 
00186     return *this;
00187 }
00188 
00189 vec4_c vec4_c::normalize()
00190 {
00191     return *this * (1/length());
00192 }
00193 
00194 /****************************************************************************
00195  * vector_linear                              interpolation of two vectors *
00196  ****************************************************************************/
00197 vec4_c vec4_c::linear
00198 (
00199     const vec4_t p1, 
00200     const vec4_t p2, 
00201     float frac
00202 ) 
00203 { 
00204     AssertReturnValue2(p1,p2, vec4_c());
00205 
00206     vec4_c vec;
00207 
00208     for (ulong i=0; i<4; i++) 
00209         vec.set_component(i, _interpolate_float_linear(p1[i], p2[i], frac));
00210 
00211     return vec;
00212 } 
00213 
00214 /****************************************************************************
00215  * vector_hermite                     hermite interpolation of two vectors *
00216  ****************************************************************************/
00217 vec4_c vec4_c::hermite
00218 (
00219     const vec4_t p1, 
00220     const vec4_t t1, 
00221     const vec4_t p2, 
00222     const vec4_t t2, 
00223     float   frac
00224 ) 
00225 { 
00226     AssertReturnValue4(p1,p2,t1,t2, vec4_c());
00227 
00228     vec4_c vec;
00229 
00230     float tsquared  = frac*frac; 
00231     float tsquared3 = tsquared*3; 
00232     float tcubed    = frac*tsquared; 
00233     float tcubed2   = tcubed*2; 
00234     float t         = frac; 
00235  
00236     float a = (tcubed2-tsquared3+1); 
00237     float b = (tcubed-(2*tsquared)+t); 
00238     float c = (tcubed-tsquared); 
00239     float d = (-tcubed2+tsquared3); 
00240  
00241     for (ulong i=0; i<4; i++) 
00242     { 
00243         float   pa = p1[i]; 
00244         float   pb = p2[i]; 
00245         float   ma = t1[i]; 
00246         float   mb = t2[i]; 
00247  
00248         vec.set_component(i, a*pa+b*ma+c*mb+d*pb);
00249     } 
00250 
00251     return vec;
00252 } 

Generated on Wed Dec 18 15:48:47 2002 for omicron engine by doxygen1.2.18