00001 00011 /*************************************************************************** 00012 * Copyright (C) 2007 by Jan Koci * 00013 * honza.koci@email.cz * 00014 * http://kengine.sourceforge.net/tutorial/ 00015 * * 00016 * This program is free software; you can redistribute it and/or modify * 00017 * it under the terms of the GNU General Public License as published by * 00018 * the Free Software Foundation; either version 2 of the License, or * 00019 * (at your option) any later version. * 00020 * * 00021 * This program is distributed in the hope that it will be useful, * 00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00024 * GNU General Public License for more details. * 00025 * * 00026 * You should have received a copy of the GNU General Public License * 00027 * along with this program; if not, write to the * 00028 * Free Software Foundation, Inc., * 00029 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00030 ***************************************************************************/ 00031 00032 #include "K3dVector4.h" 00033 00034 00035 K3dVector4::K3dVector4() 00036 { 00037 } 00038 00039 // Constructor set private vector 00040 K3dVector4::K3dVector4(const K3dVector4& rkV) 00041 { 00042 for(int i=0; i<4; i++) 00043 { 00044 m_afVector[i] = rkV.m_afVector[i]; 00045 } 00046 } 00047 00048 // Constructor set vector from array 00049 K3dVector4::K3dVector4( const float afVector[4] ) 00050 { 00051 m_afVector[0] = afVector[0]; // Set x 00052 m_afVector[1] = afVector[1]; // Set y 00053 m_afVector[2] = afVector[2]; // Set z 00054 m_afVector[3] = afVector[3]; // Set w 00055 } 00056 00057 00058 00059 // Constructor to set vector from unsigned char array 00060 K3dVector4::K3dVector4(const unsigned char aucVector[4]) 00061 { 00062 m_afVector[0] = (float) aucVector[0]; // Set x 00063 m_afVector[1] = (float) aucVector[1]; // Set y 00064 m_afVector[2] = (float) aucVector[2]; // Set z 00065 m_afVector[3] = (float) aucVector[3]; // Set w 00066 } 00067 00068 00069 00070 // Constructor set vector from x, y, z, w values 00071 K3dVector4::K3dVector4(const float fX, const float fY, const float fZ, const float fW ) 00072 { 00073 m_afVector[0] = fX; // Set x 00074 m_afVector[1] = fY; // Set y 00075 m_afVector[2] = fZ; // Set z 00076 m_afVector[3] = fW; // Set w 00077 } 00078 00079 00080 00081 // Descructor 00082 K3dVector4::~K3dVector4() 00083 { 00084 } 00085 00086 00087 00088 // Assignment operator 00089 K3dVector4& K3dVector4::operator= ( K3dVector4& rkV) 00090 { 00091 for(int i=0; i<4; i++) 00092 { 00093 m_afVector[i] = rkV.m_afVector[i]; 00094 } 00095 return *this; 00096 } 00097 00098 00099 00100 // Overloaded operator+ allows add two vectors 00101 K3dVector4 K3dVector4::operator+(K3dVector4& rkV ) 00102 { 00103 K3dVector4 kSum; 00104 for (int i = 0; i < 4; i++) 00105 kSum.m_afVector[i] = m_afVector[i] + rkV.m_afVector[i]; 00106 return kSum; 00107 } 00108 00109 00110 00111 // Overloaded operator+ allows add vector and float value 00112 K3dVector4 K3dVector4::operator+( float fScalar ) 00113 { 00114 00115 float afValue[4]; // Slave array 00116 for (int i=0; i<4; i++) // Go through x, y, z values 00117 { 00118 afValue[i] = m_afVector[i] + fScalar; // Hold to slave array 00119 } 00120 00121 return K3dVector4(afValue); // Return slave array 00122 00123 } 00124 00125 00126 00127 // Overloaded operator+= 00128 K3dVector4& K3dVector4::operator+=(const K3dVector4 &rkVector) 00129 { 00130 for (int i=0; i<4; i++) // Go through x, y, z values 00131 { 00132 this->m_afVector[i] += rkVector.m_afVector[i]; 00133 } 00134 00135 return *this; 00136 } 00137 00138 00139 00140 // Overloaded scalar operator+= 00141 K3dVector4& K3dVector4::operator+=(float fScalar ) 00142 { 00143 for (int i=0; i<4; i++) 00144 { 00145 this->m_afVector[i] += fScalar; 00146 } 00147 00148 return *this; 00149 } 00150 00151 00152 00153 // Overloaded scalar operator-= 00154 K3dVector4& K3dVector4::operator-=(float fScalar ) 00155 { 00156 for (int i=0; i<4; i++) 00157 { 00158 this->m_afVector[i] -= fScalar; 00159 } 00160 00161 return *this; 00162 } 00163 00164 00165 00166 // Overloaded operator-= 00167 K3dVector4& K3dVector4::operator-=(const K3dVector4 &rkVector) 00168 { 00169 for (int i=0; i<4; i++) 00170 { 00171 this->m_afVector[i] = this->m_afVector[i] - rkVector.m_afVector[i]; 00172 } 00173 00174 return *this; 00175 } 00176 00177 00178 00179 // Overloaded operator- allows subtract two vectors 00180 K3dVector4 K3dVector4::operator-(K3dVector4 &rkVector ) 00181 { 00182 float f_value[4]; 00183 for (int i=0; i<4; i++) 00184 { 00185 f_value[i] = m_afVector[i] - rkVector[i]; // Hold to slave array 00186 } 00187 00188 return K3dVector4(f_value); // Return slave array 00189 } 00190 00191 00192 00193 // Overloaded operator- allows add vector and float value 00194 K3dVector4 K3dVector4::operator-( float f ) 00195 { 00196 float afValue[4]; // Slave array 00197 for (int i=0; i<4; i++) // Go through x, y, z values 00198 { 00199 afValue[i] = m_afVector[i] - f; // Hold to slave array 00200 } 00201 00202 return K3dVector4(afValue); // Return slave array 00203 } 00204 00205 00206 00207 K3dVector4 K3dVector4::operator- () 00208 { 00209 K3dVector4 kNeg; 00210 for (int i = 0; i < 4; i++) 00211 kNeg.m_afVector[i] = -m_afVector[i]; 00212 return kNeg; 00213 } 00214 00215 00216 00217 // Overloaded operator* allows multiply vector and float value 00218 K3dVector4 K3dVector4::operator*( float num ) 00219 { 00220 float f_value[4]; // Slave array 00221 for (int i=0; i<4; i++) // Go through x, y, z values 00222 { 00223 f_value[i] = m_afVector[i] * num; // Hold to slave array 00224 00225 } 00226 00227 return K3dVector4(f_value); // Return slave array 00228 } 00229 00230 00231 00232 // Overloaded operator*= allows multiply two vectors 00233 K3dVector4 K3dVector4::operator*=( const K3dVector4 &rkVector ) 00234 { 00235 for (int i=0; i<4; i++) // Go through x, y, z values 00236 { 00237 m_afVector[i] *= rkVector.m_afVector[i]; // Hold to slave array 00238 } 00239 00240 return *this; 00241 } 00242 00243 00244 00245 // Overloaded operator*= allows multiply vector and float value 00246 K3dVector4 K3dVector4::operator*=( float fScalar ) 00247 { 00248 for (int i=0; i<4; i++) // Go through x, y, z values 00249 { 00250 m_afVector[i] *= fScalar; // Hold to slave array 00251 } 00252 00253 return *this; 00254 } 00255 00256 00257 00258 // Overloaded operator/= allows divide two vectors 00259 K3dVector4 K3dVector4::operator/=( const K3dVector4 &rkVector ) 00260 { 00261 for (int i=0; i<4; i++) // Go through x, y, z values 00262 { 00263 if(rkVector.m_afVector[i]) 00264 { 00265 m_afVector[i] /= rkVector.m_afVector[i]; // Hold to slave array 00266 } 00267 else 00268 { 00269 m_afVector[i] = 0; 00270 } 00271 } 00272 00273 return *this; 00274 } 00275 00276 00277 // Overloaded operator* allows multiply two vectors 00278 K3dVector4 K3dVector4::operator*( K3dVector4 &rkVector ) 00279 { 00280 float af[4]; 00281 00282 for (int i=0; i<4; i++) // Go through x, y, z values 00283 { 00284 af[i] = m_afVector[i] * rkVector.m_afVector[i]; 00285 } 00286 00287 return K3dVector4(af); 00288 } 00289 00290 00291 00292 // Overloaded operator* allows divide two vectors 00293 K3dVector4 K3dVector4::operator/( K3dVector4 &rkVector ) 00294 { 00295 K3dVector4 kV(0,0,0,0); 00296 00297 00298 for (int i=0; i<4; i++) // Go through x, y, z values 00299 { 00300 if(rkVector.m_afVector[i]) 00301 { 00302 kV.m_afVector[i] = m_afVector[i] / rkVector.m_afVector[i]; 00303 } 00304 } 00305 00306 return kV; 00307 00308 00309 } 00310 00311 00312 00313 // Compare arrays for comparison operators 00314 int K3dVector4::CompareArrays(K3dVector4& rkV) 00315 { 00316 unsigned int uiTest0; 00317 unsigned int uiTest1; 00318 00319 for (int i = 0; i < 4; i++) 00320 { 00321 uiTest0 = *(unsigned int*)&m_afVector[i]; 00322 uiTest1 = *(unsigned int*)&rkV.m_afVector[i]; 00323 if ( uiTest0 < uiTest1 ) 00324 return -1; 00325 if ( uiTest0 > uiTest1 ) 00326 return +1; 00327 } 00328 return 0; 00329 } 00330 00331 00332 00333 00334 00335 00336 00337 00338 00339