00001 /* 00002 * matrix_4.h 00003 * 00004 * Copyright (C) 2009,2010 Thomas A. Vaughan 00005 * All rights reserved. 00006 * 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * * Neither the name of the <organization> nor the 00016 * names of its contributors may be used to endorse or promote products 00017 * derived from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY 00020 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00022 * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY 00023 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00026 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 * 00030 * 00031 * 4x4 matrices, especially for 3D transformations. 00032 */ 00033 00034 #ifndef WAVEPACKET_MATRIX_4_H__ 00035 #define WAVEPACKET_MATRIX_4_H__ 00036 00037 // includes -------------------------------------------------------------------- 00038 #include "geometry_3d.h" 00039 00040 00041 ///\ingroup geometry 00042 /*@{*/ 00043 00044 //////////////////////////////////////////////////////////////////////////////// 00045 // 00046 // basic 4x4 matrix 00047 // 00048 //////////////////////////////////////////////////////////////////////////////// 00049 00050 /// 4x4 matrix object. Note that this matrix is especially optimized for 3D 00051 /// transformations. 00052 struct matrix4_t { 00053 00054 // manipulators 00055 void clear(void) throw() { this->setZero(); } 00056 void setZero(void) throw(); 00057 void setIdentity(void) throw() { this->setXYZScale(1.0); } 00058 00059 void addTranslation(IN const point3d_t& t) throw(); 00060 void operator += (IN const point3d_t& t) throw() { 00061 this->addTranslation(t); 00062 } 00063 00064 void setTranslation(IN const point3d_t& t) throw() { 00065 this->setIdentity(); 00066 this->addTranslation(t); 00067 } 00068 void operator = (IN const point3d_t& t) throw() { 00069 this->setTranslation(t); 00070 } 00071 00072 const point3d_t getTranslation(void) const throw() { 00073 return point3d_t(m[3], m[7], m[11]); 00074 } 00075 00076 void setZRotation(IN float radians) throw(); 00077 void setXRotation(IN float radians) throw(); 00078 void setYRotation(IN float radians) throw(); 00079 void setXYZScale(IN float r) throw(); 00080 00081 void transpose(void) throw(); 00082 00083 void dump(IN const char * txt) const throw(); 00084 00085 /// transforms 3D point p. \b NOTE: assumes this 4x4 matrix is the 00086 /// standard form for 3D transformations! 00087 point3d_t transform(IN const point3d_t& p) const throw(); 00088 00089 /// sets M = AB (note: order is important for matrix multiplication!) 00090 void setToProductOf(IN const matrix4_t& A, 00091 IN const matrix4_t& B) throw(); 00092 00093 /// M = M + A 00094 /// \b CAREFUL: do you really want to do this? Not legal for 3D xforms 00095 void addMatrix(IN const matrix4_t& A) throw(); 00096 00097 /// M = M - A 00098 /// \b CAREFUL: do you really want to do this? Not legal for 3D xforms 00099 void subtractMatrix(IN const matrix4_t& B) throw(); 00100 00101 /// M = r * M 00102 /// \b CAREFUL: do you really want to do this? Not legal for 3D xforms 00103 void scale(IN float r) throw(); 00104 00105 // raw data 00106 float m[16]; // 4x4 raw data 00107 }; 00108 00109 00110 00111 inline matrix4_t operator * (IN const matrix4_t& A, IN const matrix4_t & B) throw() 00112 { 00113 matrix4_t M; 00114 M.setToProductOf(A, B); 00115 return M; 00116 } 00117 00118 00119 inline point3d_t operator * (IN const matrix4_t& M, IN const point3d_t& p) throw() 00120 { 00121 return M.transform(p); 00122 } 00123 00124 00125 inline matrix4_t operator + (IN const matrix4_t& M, IN const point3d_t t) throw() 00126 { 00127 matrix4_t M2 = M; 00128 M2.addTranslation(t); 00129 return M2; 00130 } 00131 00132 // NOTE: other operators aren't provided because they are invalid for 3D transforms 00133 00134 00135 00136 /// helper method to transform an array of points. Caller must provide valid 00137 /// input and output arrays, each of which must contain at least N points. 00138 void transformPoints(IN const matrix4_t& T, 00139 IN int N, 00140 IN const point3d_t * inputPoints, 00141 OUT point3d_t * outputPoints); 00142 00143 00144 #endif // WAVEPACKET_MATRIX_4_H__ 00145