LinearR3.h 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027
  1. /*
  2. *
  3. * Mathematics Subpackage (VrMath)
  4. *
  5. *
  6. * Author: Samuel R. Buss, [email protected].
  7. * Web page: http://math.ucsd.edu/~sbuss/MathCG
  8. *
  9. *
  10. This software is provided 'as-is', without any express or implied warranty.
  11. In no event will the authors be held liable for any damages arising from the use of this software.
  12. Permission is granted to anyone to use this software for any purpose,
  13. including commercial applications, and to alter it and redistribute it freely,
  14. subject to the following restrictions:
  15. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  16. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. *
  19. *
  20. */
  21. //
  22. // Linear Algebra Classes over R3
  23. //
  24. //
  25. // A. Vector and Position classes
  26. //
  27. // A.1. VectorR3: a real column vector of length 3.
  28. //
  29. // A.2. VectorHgR3: a column vector of length 4 which is
  30. // the homogenous representation of a vector in 3-space
  31. //
  32. // B. Matrix Classes
  33. //
  34. // B.1 LinearMapR3 - arbitrary linear map; 3x3 real matrix
  35. //
  36. // B.2 AffineMapR3 - arbitrary affine map; a 3x4 real matrix
  37. //
  38. // B.3 RotationMapR3 - orthonormal 3x3 matrix
  39. //
  40. // B.4 RigidMapR3 - RotationMapR3 plus displacement
  41. //
  42. #ifndef LINEAR_R3_H
  43. #define LINEAR_R3_H
  44. #include <math.h>
  45. #include <assert.h>
  46. #include <iostream>
  47. #include "MathMisc.h"
  48. using namespace std;
  49. class VectorR3; // Space Vector (length 3)
  50. class VectorHgR3; // Homogenous Space Vector
  51. class VectorR4; // Space Vector (length 4)
  52. class LinearMapR3; // Linear Map (3x3 Matrix)
  53. class AffineMapR3; // Affine Map (3x4 Matrix)
  54. class RotationMapR3; // Rotation (3x3 orthonormal matrix)
  55. class RigidMapR3; // 3x4 matrix, first 3 columns orthonormal
  56. // Most for internal use:
  57. class Matrix3x3;
  58. class Matrix3x4;
  59. class Quaternion;
  60. // **************************************
  61. // VectorR3 class *
  62. // * * * * * * * * * * * * * * * * * * **
  63. class VectorR3 {
  64. public:
  65. double x, y, z; // The x & y & z coordinates.
  66. static const VectorR3 Zero;
  67. static const VectorR3 UnitX;
  68. static const VectorR3 UnitY;
  69. static const VectorR3 UnitZ;
  70. static const VectorR3 NegUnitX;
  71. static const VectorR3 NegUnitY;
  72. static const VectorR3 NegUnitZ;
  73. public:
  74. VectorR3( ) : x(0.0), y(0.0), z(0.0) {}
  75. VectorR3( double xVal, double yVal, double zVal )
  76. : x(xVal), y(yVal), z(zVal) {}
  77. VectorR3( const VectorHgR3& uH );
  78. VectorR3& Set( const Quaternion& ); // Convert quat to rotation vector
  79. VectorR3& Set( double xx, double yy, double zz )
  80. { x=xx; y=yy; z=zz; return *this; }
  81. VectorR3& SetFromHg( const VectorR4& ); // Convert homogeneous VectorR4 to VectorR3
  82. VectorR3& SetZero() { x=0.0; y=0.0; z=0.0; return *this;}
  83. VectorR3& Load( const double* v );
  84. VectorR3& Load( const float* v );
  85. void Dump( double* v ) const;
  86. void Dump( float* v ) const;
  87. inline double operator[]( int i );
  88. VectorR3& operator= ( const VectorR3& v )
  89. { x=v.x; y=v.y; z=v.z; return(*this);}
  90. VectorR3& operator+= ( const VectorR3& v )
  91. { x+=v.x; y+=v.y; z+=v.z; return(*this); }
  92. VectorR3& operator-= ( const VectorR3& v )
  93. { x-=v.x; y-=v.y; z-=v.z; return(*this); }
  94. VectorR3& operator*= ( double m )
  95. { x*=m; y*=m; z*=m; return(*this); }
  96. VectorR3& operator/= ( double m )
  97. { register double mInv = 1.0/m;
  98. x*=mInv; y*=mInv; z*=mInv;
  99. return(*this); }
  100. VectorR3 operator- () const { return ( VectorR3(-x, -y, -z) ); }
  101. VectorR3& operator*= (const VectorR3& v); // Cross Product
  102. VectorR3& ArrayProd(const VectorR3&); // Component-wise product
  103. VectorR3& AddScaled( const VectorR3& u, double s );
  104. bool IsZero() const { return ( x==0.0 && y==0.0 && z==0.0 ); }
  105. double Norm() const { return ( (double)sqrt( x*x + y*y + z*z ) ); }
  106. double NormSq() const { return ( x*x + y*y + z*z ); }
  107. double MaxAbs() const;
  108. double Dist( const VectorR3& u ) const; // Distance from u
  109. double DistSq( const VectorR3& u ) const; // Distance from u squared
  110. VectorR3& Negate() { x = -x; y = -y; z = -z; return *this;}
  111. VectorR3& Normalize () { *this /= Norm(); return *this;} // No error checking
  112. inline VectorR3& MakeUnit(); // Normalize() with error checking
  113. inline VectorR3& ReNormalize();
  114. bool IsUnit( ) const
  115. { register double norm = Norm();
  116. return ( 1.000001>=norm && norm>=0.999999 ); }
  117. bool IsUnit( double tolerance ) const
  118. { register double norm = Norm();
  119. return ( 1.0+tolerance>=norm && norm>=1.0-tolerance ); }
  120. bool NearZero(double tolerance) const { return( MaxAbs()<=tolerance );}
  121. // tolerance should be non-negative
  122. double YaxisDistSq() const { return (x*x+z*z); }
  123. double YaxisDist() const { return sqrt(x*x+z*z); }
  124. VectorR3& Rotate( double theta, const VectorR3& u); // rotate around u.
  125. VectorR3& RotateUnitInDirection ( const VectorR3& dir); // rotate in direction dir
  126. VectorR3& Rotate( const Quaternion& ); // Rotate according to quaternion
  127. friend ostream& operator<< ( ostream& os, const VectorR3& u );
  128. };
  129. inline VectorR3 operator+( const VectorR3& u, const VectorR3& v );
  130. inline VectorR3 operator-( const VectorR3& u, const VectorR3& v );
  131. inline VectorR3 operator*( const VectorR3& u, double m);
  132. inline VectorR3 operator*( double m, const VectorR3& u);
  133. inline VectorR3 operator/( const VectorR3& u, double m);
  134. inline int operator==( const VectorR3& u, const VectorR3& v );
  135. inline double operator^ (const VectorR3& u, const VectorR3& v ); // Dot Product
  136. inline VectorR3 operator* (const VectorR3& u, const VectorR3& v); // Cross Product
  137. inline VectorR3 ArrayProd ( const VectorR3& u, const VectorR3& v );
  138. inline double Mag(const VectorR3& u) { return u.Norm(); }
  139. inline double Dist(const VectorR3& u, const VectorR3& v) { return u.Dist(v); }
  140. inline double DistSq(const VectorR3& u, const VectorR3& v) { return u.DistSq(v); }
  141. inline double NormalizeError (const VectorR3& u);
  142. extern const VectorR3 UnitVecIR3;
  143. extern const VectorR3 UnitVecJR3;
  144. extern const VectorR3 UnitVecKR3;
  145. inline VectorR3 ToVectorR3( const Quaternion& q )
  146. {return VectorR3().Set(q);}
  147. // ****************************************
  148. // VectorHgR3 class *
  149. // * * * * * * * * * * * * * * * * * * * **
  150. class VectorHgR3 {
  151. public:
  152. double x, y, z, w; // The x & y & z & w coordinates.
  153. public:
  154. VectorHgR3( ) : x(0.0), y(0.0), z(0.0), w(1.0) {}
  155. VectorHgR3( double xVal, double yVal, double zVal )
  156. : x(xVal), y(yVal), z(zVal), w(1.0) {}
  157. VectorHgR3( double xVal, double yVal, double zVal, double wVal )
  158. : x(xVal), y(yVal), z(zVal), w(wVal) {}
  159. VectorHgR3 ( const VectorR3& u ) : x(u.x), y(u.y), z(u.z), w(1.0) {}
  160. };
  161. //
  162. // Advanced vector and position functions (prototypes)
  163. //
  164. VectorR3 Interpolate( const VectorR3& start, const VectorR3& end, double a);
  165. // *****************************************
  166. // Matrix3x3 class *
  167. // * * * * * * * * * * * * * * * * * * * * *
  168. class Matrix3x3 {
  169. public:
  170. double m11, m12, m13, m21, m22, m23, m31, m32, m33;
  171. // Implements a 3x3 matrix: m_i_j - row-i and column-j entry
  172. static const Matrix3x3 Identity;
  173. public:
  174. inline Matrix3x3();
  175. inline Matrix3x3(const VectorR3&, const VectorR3&, const VectorR3&); // Sets by columns!
  176. inline Matrix3x3(double, double, double, double, double, double,
  177. double, double, double ); // Sets by columns
  178. inline void SetIdentity (); // Set to the identity map
  179. inline void Set ( const Matrix3x3& ); // Set to the matrix.
  180. inline void Set3x3 ( const Matrix3x4& ); // Set to the 3x3 part of the matrix.
  181. inline void Set( const VectorR3&, const VectorR3&, const VectorR3& );
  182. inline void Set( double, double, double,
  183. double, double, double,
  184. double, double, double );
  185. inline void SetByRows( double, double, double, double, double, double,
  186. double, double, double );
  187. inline void SetByRows( const VectorR3&, const VectorR3&, const VectorR3& );
  188. inline void SetColumn1 ( double, double, double );
  189. inline void SetColumn2 ( double, double, double );
  190. inline void SetColumn3 ( double, double, double );
  191. inline void SetColumn1 ( const VectorR3& );
  192. inline void SetColumn2 ( const VectorR3& );
  193. inline void SetColumn3 ( const VectorR3& );
  194. inline VectorR3 Column1() const;
  195. inline VectorR3 Column2() const;
  196. inline VectorR3 Column3() const;
  197. inline void SetRow1 ( double, double, double );
  198. inline void SetRow2 ( double, double, double );
  199. inline void SetRow3 ( double, double, double );
  200. inline void SetRow1 ( const VectorR3& );
  201. inline void SetRow2 ( const VectorR3& );
  202. inline void SetRow3 ( const VectorR3& );
  203. inline VectorR3 Row1() const;
  204. inline VectorR3 Row2() const;
  205. inline VectorR3 Row3() const;
  206. inline void SetDiagonal( double, double, double );
  207. inline void SetDiagonal( const VectorR3& );
  208. inline double Diagonal( int );
  209. inline void MakeTranspose(); // Transposes it.
  210. Matrix3x3& ReNormalize();
  211. VectorR3 Solve(const VectorR3&) const; // Returns solution
  212. inline void Transform( VectorR3* ) const;
  213. inline void Transform( const VectorR3& src, VectorR3* dest) const;
  214. protected:
  215. void OperatorTimesEquals( const Matrix3x3& ); // Internal use only
  216. void SetZero (); // Set to the zero map
  217. };
  218. inline VectorR3 operator* ( const Matrix3x3&, const VectorR3& );
  219. ostream& operator<< ( ostream& os, const Matrix3x3& A );
  220. // *****************************************
  221. // Matrix3x4 class *
  222. // * * * * * * * * * * * * * * * * * * * * *
  223. class Matrix3x4
  224. {
  225. public:
  226. double m11, m12, m13, m21, m22, m23, m31, m32, m33;
  227. double m14;
  228. double m24;
  229. double m34;
  230. static const Matrix3x4 Identity;
  231. public:
  232. // Constructors set by columns!
  233. Matrix3x4() {}
  234. Matrix3x4(const VectorR3&, const VectorR3&, const VectorR3&, const VectorR3& );
  235. Matrix3x4(double, double, double, double, double, double,
  236. double, double, double, double, double, double ); // Sets by columns
  237. Matrix3x4( const Matrix3x3&, const VectorR3& );
  238. void SetIdentity (); // Set to the identity map
  239. void Set ( const Matrix3x4& ); // Set to the matrix.
  240. void Set3x3 ( const Matrix3x3& ); // Set linear part to the matrix.
  241. void Set ( const Matrix3x3&, const VectorR3& ); // Set to the matrix plus 4th column
  242. void Set( const VectorR3&, const VectorR3&, const VectorR3&, const VectorR3& );
  243. void Set( double, double, double,
  244. double, double, double,
  245. double, double, double,
  246. double, double, double ); // Sets by columns
  247. void Set3x3( double, double, double,
  248. double, double, double,
  249. double, double, double ); // Sets by columns
  250. void SetByRows( double, double, double, double, double, double,
  251. double, double, double, double, double, double );
  252. void SetColumn1 ( double, double, double );
  253. void SetColumn2 ( double, double, double );
  254. void SetColumn3 ( double, double, double );
  255. void SetColumn4 ( double, double, double );
  256. void SetColumn1 ( const VectorR3& );
  257. void SetColumn2 ( const VectorR3& );
  258. void SetColumn3 ( const VectorR3& );
  259. void SetColumn4 ( const VectorR3& );
  260. VectorR3 Column1() const;
  261. VectorR3 Column2() const;
  262. VectorR3 Column3() const;
  263. VectorR3 Column4() const;
  264. void SetRow1 ( double x, double y, double z, double w );
  265. void SetRow2 ( double x, double y, double z, double w );
  266. void SetRow3 ( double x, double y, double z, double w );
  267. void SetRow4 ( double x, double y, double z, double w );
  268. Matrix3x4& ApplyTranslationLeft( const VectorR3& u );
  269. Matrix3x4& ApplyTranslationRight( const VectorR3& u );
  270. Matrix3x4& ApplyYRotationLeft( double theta );
  271. Matrix3x4& ApplyYRotationLeft( double costheta, double sintheta );
  272. Matrix3x4& ReNormalize();
  273. VectorR3 Solve(const VectorR3&) const; // Returns solution
  274. inline void Transform( VectorR3* ) const;
  275. inline void Transform3x3( VectorR3* ) const;
  276. inline void Transform( const VectorR3& src, VectorR3* dest ) const;
  277. inline void Transform3x3( const VectorR3& src, VectorR3* dest ) const;
  278. inline void Transform3x3Transpose( VectorR3* dest ) const;
  279. inline void Transform3x3Transpose( const VectorR3& src, VectorR3* dest ) const;
  280. protected:
  281. void SetZero (); // Set to the zero map
  282. void OperatorTimesEquals( const Matrix3x3& ); // Internal use only
  283. void OperatorTimesEquals( const Matrix3x4& ); // Internal use only
  284. };
  285. inline VectorR3 operator* ( const Matrix3x4&, const VectorR3& );
  286. ostream& operator<< ( ostream& os, const Matrix3x4& A );
  287. // *****************************************
  288. // LinearMapR3 class *
  289. // * * * * * * * * * * * * * * * * * * * * *
  290. class LinearMapR3 : public Matrix3x3 {
  291. public:
  292. LinearMapR3();
  293. LinearMapR3( const VectorR3&, const VectorR3&, const VectorR3& );
  294. LinearMapR3( double, double, double, double, double, double,
  295. double, double, double ); // Sets by columns
  296. LinearMapR3 ( const Matrix3x3& );
  297. void SetZero (); // Set to the zero map
  298. inline void Negate();
  299. inline LinearMapR3& operator+= (const Matrix3x3& );
  300. inline LinearMapR3& operator-= (const Matrix3x3& );
  301. inline LinearMapR3& operator*= (double);
  302. inline LinearMapR3& operator/= (double);
  303. LinearMapR3& operator*= (const Matrix3x3& ); // Matrix product
  304. inline LinearMapR3 Transpose() const; // Returns the transpose
  305. double Determinant () const; // Returns the determinant
  306. LinearMapR3 Inverse() const; // Returns inverse
  307. LinearMapR3& Invert(); // Converts into inverse.
  308. VectorR3 Solve(const VectorR3&) const; // Returns solution
  309. LinearMapR3 PseudoInverse() const; // Returns pseudo-inverse TO DO
  310. VectorR3 PseudoSolve(const VectorR3&); // Finds least squares solution TO DO
  311. };
  312. inline LinearMapR3 operator+ (const LinearMapR3&, const Matrix3x3&);
  313. inline LinearMapR3 operator+ (const Matrix3x3&, const LinearMapR3&);
  314. inline LinearMapR3 operator- (const LinearMapR3&);
  315. inline LinearMapR3 operator- (const LinearMapR3&, const Matrix3x3&);
  316. inline LinearMapR3 operator- (const Matrix3x3&, const LinearMapR3&);
  317. inline LinearMapR3 operator* ( const LinearMapR3&, double);
  318. inline LinearMapR3 operator* ( double, const LinearMapR3& );
  319. inline LinearMapR3 operator/ ( const LinearMapR3&, double );
  320. LinearMapR3 operator* ( const LinearMapR3&, const LinearMapR3& );
  321. // Matrix product (composition)
  322. // *****************************************************
  323. // * AffineMapR3 class *
  324. // * * * * * * * * * * * * * * * * * * * * * * * * * * *
  325. class AffineMapR3 : public Matrix3x4 {
  326. public:
  327. AffineMapR3();
  328. AffineMapR3( double, double, double, double, double, double,
  329. double, double, double, double, double, double ); // Sets by columns
  330. AffineMapR3 ( const VectorR3&, const VectorR3&, const VectorR3&, const VectorR3&);
  331. AffineMapR3 ( const LinearMapR3&, const VectorR3& );
  332. void SetIdentity (); // Set to the identity map
  333. void SetZero (); // Set to the zero map
  334. AffineMapR3& operator+= (const Matrix3x4& );
  335. AffineMapR3& operator-= (const Matrix3x4& );
  336. AffineMapR3& operator*= (double);
  337. AffineMapR3& operator/= (double);
  338. AffineMapR3& operator*= (const Matrix3x3& ); // Composition
  339. AffineMapR3& operator*= (const Matrix3x4& ); // Composition
  340. AffineMapR3& ApplyTranslationLeft( const VectorR3& u )
  341. { Matrix3x4::ApplyTranslationLeft( u ); return *this; }
  342. AffineMapR3& ApplyTranslationRight( const VectorR3& u )
  343. { Matrix3x4::ApplyTranslationRight( u ); return *this; }
  344. AffineMapR3& ApplyYRotationLeft( double theta )
  345. { Matrix3x4::ApplyYRotationLeft( theta ); return *this; }
  346. AffineMapR3& ApplyYRotationLeft( double costheta, double sintheta )
  347. { Matrix3x4::ApplyYRotationLeft( costheta, sintheta ); return *this; }
  348. AffineMapR3 Inverse() const; // Returns inverse
  349. AffineMapR3& Invert(); // Converts into inverse.
  350. VectorR3 Solve(const VectorR3&) const; // Returns solution
  351. AffineMapR3 PseudoInverse() const; // Returns pseudo-inverse // TO DO
  352. VectorR3 PseudoSolve(const VectorR3&); // Least squares solution // TO DO
  353. };
  354. inline AffineMapR3 operator+ (const AffineMapR3&, const Matrix3x4&);
  355. inline AffineMapR3 operator+ (const Matrix3x4&, const AffineMapR3&);
  356. inline AffineMapR3 operator+ (const AffineMapR3&, const Matrix3x3&);
  357. inline AffineMapR3 operator+ (const Matrix3x3&, const AffineMapR3&);
  358. inline AffineMapR3 operator- (const AffineMapR3&, const Matrix3x4&);
  359. inline AffineMapR3 operator- (const Matrix3x4&, const AffineMapR3&);
  360. inline AffineMapR3 operator- (const AffineMapR3&, const Matrix3x3&);
  361. inline AffineMapR3 operator- (const Matrix3x3&, const AffineMapR3&);
  362. inline AffineMapR3 operator* (const AffineMapR3&, double);
  363. inline AffineMapR3 operator* (double, const AffineMapR3& );
  364. inline AffineMapR3 operator/ (const AffineMapR3&, double );
  365. // Composition operators
  366. AffineMapR3 operator* ( const AffineMapR3&, const AffineMapR3& );
  367. AffineMapR3 operator* ( const LinearMapR3&, const AffineMapR3& );
  368. AffineMapR3 operator* ( const AffineMapR3&, const LinearMapR3& );
  369. // *******************************************
  370. // RotationMapR3 class *
  371. // * * * * * * * * * * * * * * * * * * * * * *
  372. class RotationMapR3 : public Matrix3x3 {
  373. public:
  374. RotationMapR3();
  375. RotationMapR3( const VectorR3&, const VectorR3&, const VectorR3& );
  376. RotationMapR3( double, double, double, double, double, double,
  377. double, double, double );
  378. RotationMapR3& Set( const Quaternion& );
  379. RotationMapR3& Set( const VectorR3&, double theta ); // Set rotation axis and angle
  380. RotationMapR3& Set( const VectorR3&, double sintheta, double costheta );
  381. RotationMapR3& operator*= (const RotationMapR3& ); // Matrix product
  382. RotationMapR3 Transpose() const { return Inverse(); }; // Returns the transpose
  383. RotationMapR3 Inverse() const; // Returns inverse
  384. RotationMapR3& Invert(); // Converts into inverse.
  385. VectorR3 Solve(const VectorR3&) const; // Returns solution // Was named Invert
  386. bool ToAxisAndAngle( VectorR3* u, double* theta ) const; // returns unit vector u and angle
  387. };
  388. RotationMapR3 operator* ( const RotationMapR3&, const RotationMapR3& );
  389. // Matrix product (composition)
  390. inline RotationMapR3 ToRotationMapR3( const Quaternion& q )
  391. { return( RotationMapR3().Set(q) ); }
  392. ostream& operator<< ( ostream& os, const RotationMapR3& A );
  393. // ***************************************************************
  394. // * RigidMapR3 class - prototypes. * *
  395. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  396. class RigidMapR3 : public Matrix3x4
  397. {
  398. public:
  399. RigidMapR3();
  400. RigidMapR3( const VectorR3&, const VectorR3&, const VectorR3&, const VectorR3& );
  401. RigidMapR3( double, double, double, double, double, double,
  402. double, double, double, double, double, double );
  403. RigidMapR3( const Matrix3x3&, const VectorR3& );
  404. RigidMapR3& Set( const Matrix3x3&, const VectorR3& ); // Set to RotationMap & Vector
  405. RigidMapR3& SetTranslationPart( const VectorR3& ); // Set the translation part
  406. RigidMapR3& SetTranslationPart( double, double, double ); // Set the translation part
  407. RigidMapR3& SetRotationPart( const Matrix3x3& ); // Set the rotation part
  408. RigidMapR3& SetRotationPart( const Quaternion& );
  409. RigidMapR3& SetRotationPart( const VectorR3&, double theta ); // Set rotation axis and angle
  410. RigidMapR3& SetRotationPart( const VectorR3&, double sintheta, double costheta );
  411. RigidMapR3& ApplyTranslationLeft( const VectorR3& u )
  412. {Matrix3x4::ApplyTranslationLeft( u ); return *this;}
  413. RigidMapR3& ApplyTranslationRight( const VectorR3& u )
  414. {Matrix3x4::ApplyTranslationRight( u ); return *this;}
  415. RigidMapR3& ApplyYRotationLeft( double theta )
  416. { Matrix3x4::ApplyYRotationLeft( theta ); return *this; }
  417. RigidMapR3& ApplyYRotationLeft( double costheta, double sintheta )
  418. { Matrix3x4::ApplyYRotationLeft( costheta, sintheta ); return *this; }
  419. RigidMapR3& operator*=(const RotationMapR3& ); // Composition
  420. RigidMapR3& operator*=(const RigidMapR3& ); // Composition
  421. RigidMapR3 Inverse() const; // Returns inverse
  422. RigidMapR3& Invert(); // Converts into inverse.
  423. bool CalcGlideRotation( VectorR3* u, VectorR3* v,
  424. double *glideDist, double *rotation ) const;
  425. void Transform3x3Inverse( VectorR3* dest ) const
  426. { Matrix3x4::Transform3x3Transpose( dest ); }
  427. void Transform3x3Inverse( const VectorR3& src, VectorR3* dest ) const
  428. { Matrix3x4::Transform3x3Transpose( src, dest ); }
  429. };
  430. // ***************************************************************
  431. // * 3-space vector and matrix utilities (prototypes) *
  432. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  433. // Returns the solid angle between vectors v and w.
  434. inline double SolidAngle( const VectorR3& v, const VectorR3& w);
  435. // Returns a righthanded orthonormal basis to complement unit vector x
  436. void GetOrtho( const VectorR3& x, VectorR3& y, VectorR3& z);
  437. // Returns a vector v orthonormal to unit vector x
  438. void GetOrtho( const VectorR3& x, VectorR3& y );
  439. // Projections
  440. // The next three functions are templated below.
  441. //inline VectorR3 ProjectToUnit ( const VectorR3& u, const VectorR3& v); // Project u onto v
  442. //inline VectorR3 ProjectPerpUnit ( const VectorR3& u, const VectorR3 & v); // Project perp to v
  443. //inline VectorR3 ProjectPerpUnitDiff ( const VectorR3& u, const VectorR3& v)
  444. // v must be a unit vector.
  445. // Projection maps (LinearMapR3s)
  446. inline LinearMapR3 VectorProjectMap( const VectorR3& u );
  447. inline LinearMapR3 PlaneProjectMap ( const VectorR3& w );
  448. inline LinearMapR3 PlaneProjectMap ( const VectorR3& u, const VectorR3 &v );
  449. // u,v,w - must be unit vector. u and v must be orthonormal and
  450. // specify the plane they are parallel to. w specifies the plane
  451. // it is orthogonal to.
  452. // VrRotate is similar to glRotate. Returns a matrix (RotationMapR3)
  453. // that will perform the rotation. u should be a unit vector.
  454. RotationMapR3 VrRotate( double theta, const VectorR3& u );
  455. RotationMapR3 VrRotate( double costheta, double sintheta, const VectorR3& u );
  456. RotationMapR3 VrRotateAlign( const VectorR3& fromVec, const VectorR3& toVec);
  457. RotationMapR3 RotateToMap( const VectorR3& fromVec, const VectorR3& toVec);
  458. // fromVec and toVec should be unit vectors for RotateToMap
  459. // ***************************************************************
  460. // * Stream Output Routines (Prototypes) *
  461. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  462. ostream& operator<< ( ostream& os, const VectorR3& u );
  463. // *****************************************************
  464. // * VectorR3 class - inlined functions *
  465. // * * * * * * * * * * * * * * * * * * * * * * * * * * *
  466. inline VectorR3& VectorR3::Load( const double* v )
  467. {
  468. x = *v;
  469. y = *(v+1);
  470. z = *(v+2);
  471. return *this;
  472. }
  473. inline VectorR3& VectorR3::Load( const float* v )
  474. {
  475. x = *v;
  476. y = *(v+1);
  477. z = *(v+2);
  478. return *this;
  479. }
  480. inline void VectorR3::Dump( double* v ) const
  481. {
  482. *v = x;
  483. *(v+1) = y;
  484. *(v+2) = z;
  485. }
  486. inline void VectorR3::Dump( float* v ) const
  487. {
  488. *v = (float)x;
  489. *(v+1) = (float)y;
  490. *(v+2) = (float)z;
  491. }
  492. inline double VectorR3::operator[]( int i )
  493. {
  494. switch (i) {
  495. case 0:
  496. return x;
  497. case 1:
  498. return y;
  499. case 2:
  500. return z;
  501. default:
  502. assert(0);
  503. return 0.0;
  504. }
  505. }
  506. inline VectorR3& VectorR3::MakeUnit () // Convert to unit vector (or leave zero).
  507. {
  508. double nSq = NormSq();
  509. if (nSq != 0.0) {
  510. *this /= sqrt(nSq);
  511. }
  512. return *this;
  513. }
  514. inline VectorR3 operator+( const VectorR3& u, const VectorR3& v )
  515. {
  516. return VectorR3(u.x+v.x, u.y+v.y, u.z+v.z);
  517. }
  518. inline VectorR3 operator-( const VectorR3& u, const VectorR3& v )
  519. {
  520. return VectorR3(u.x-v.x, u.y-v.y, u.z-v.z);
  521. }
  522. inline VectorR3 operator*( const VectorR3& u, register double m)
  523. {
  524. return VectorR3( u.x*m, u.y*m, u.z*m);
  525. }
  526. inline VectorR3 operator*( register double m, const VectorR3& u)
  527. {
  528. return VectorR3( u.x*m, u.y*m, u.z*m);
  529. }
  530. inline VectorR3 operator/( const VectorR3& u, double m)
  531. {
  532. register double mInv = 1.0/m;
  533. return VectorR3( u.x*mInv, u.y*mInv, u.z*mInv);
  534. }
  535. inline int operator==( const VectorR3& u, const VectorR3& v )
  536. {
  537. return ( u.x==v.x && u.y==v.y && u.z==v.z );
  538. }
  539. inline double operator^ ( const VectorR3& u, const VectorR3& v ) // Dot Product
  540. {
  541. return ( u.x*v.x + u.y*v.y + u.z*v.z );
  542. }
  543. inline VectorR3 operator* (const VectorR3& u, const VectorR3& v) // Cross Product
  544. {
  545. return (VectorR3( u.y*v.z - u.z*v.y,
  546. u.z*v.x - u.x*v.z,
  547. u.x*v.y - u.y*v.x ) );
  548. }
  549. inline VectorR3 ArrayProd ( const VectorR3& u, const VectorR3& v )
  550. {
  551. return ( VectorR3( u.x*v.x, u.y*v.y, u.z*v.z ) );
  552. }
  553. inline VectorR3& VectorR3::operator*= (const VectorR3& v) // Cross Product
  554. {
  555. double tx=x, ty=y;
  556. x = y*v.z - z*v.y;
  557. y = z*v.x - tx*v.z;
  558. z = tx*v.y - ty*v.x;
  559. return ( *this );
  560. }
  561. inline VectorR3& VectorR3::ArrayProd (const VectorR3& v) // Component-wise Product
  562. {
  563. x *= v.x;
  564. y *= v.y;
  565. z *= v.z;
  566. return ( *this );
  567. }
  568. inline VectorR3& VectorR3::AddScaled( const VectorR3& u, double s )
  569. {
  570. x += s*u.x;
  571. y += s*u.y;
  572. z += s*u.z;
  573. return(*this);
  574. }
  575. inline VectorR3::VectorR3( const VectorHgR3& uH )
  576. : x(uH.x), y(uH.y), z(uH.z)
  577. {
  578. *this /= uH.w;
  579. }
  580. inline VectorR3& VectorR3::ReNormalize() // Convert near unit back to unit
  581. {
  582. double nSq = NormSq();
  583. register double mFact = 1.0-0.5*(nSq-1.0); // Multiplicative factor
  584. *this *= mFact;
  585. return *this;
  586. }
  587. inline double NormalizeError (const VectorR3& u)
  588. {
  589. register double discrepancy;
  590. discrepancy = u.x*u.x + u.y*u.y + u.z*u.z - 1.0;
  591. if ( discrepancy < 0.0 ) {
  592. discrepancy = -discrepancy;
  593. }
  594. return discrepancy;
  595. }
  596. inline double VectorR3::Dist( const VectorR3& u ) const // Distance from u
  597. {
  598. return sqrt( DistSq(u) );
  599. }
  600. inline double VectorR3::DistSq( const VectorR3& u ) const // Distance from u
  601. {
  602. return ( (x-u.x)*(x-u.x) + (y-u.y)*(y-u.y) + (z-u.z)*(z-u.z) );
  603. }
  604. //
  605. // Interpolation routines (not just Spherical Interpolation)
  606. //
  607. // Interpolate(start,end,frac) - linear interpolation
  608. // - allows overshooting the end points
  609. inline VectorR3 Interpolate( const VectorR3& start, const VectorR3& end, double a)
  610. {
  611. VectorR3 ret;
  612. Lerp( start, end, a, ret );
  613. return ret;
  614. }
  615. // ******************************************************
  616. // * Matrix3x3 class - inlined functions *
  617. // * * * * * * * * * * * * * * * * * * * * * * * * * * **
  618. inline Matrix3x3::Matrix3x3() {}
  619. inline Matrix3x3::Matrix3x3( const VectorR3& u, const VectorR3& v,
  620. const VectorR3& s )
  621. {
  622. m11 = u.x; // Column 1
  623. m21 = u.y;
  624. m31 = u.z;
  625. m12 = v.x; // Column 2
  626. m22 = v.y;
  627. m32 = v.z;
  628. m13 = s.x; // Column 3
  629. m23 = s.y;
  630. m33 = s.z;
  631. }
  632. inline Matrix3x3::Matrix3x3( double a11, double a21, double a31,
  633. double a12, double a22, double a32,
  634. double a13, double a23, double a33)
  635. // Values specified in column order!!!
  636. {
  637. m11 = a11; // Row 1
  638. m12 = a12;
  639. m13 = a13;
  640. m21 = a21; // Row 2
  641. m22 = a22;
  642. m23 = a23;
  643. m31 = a31; // Row 3
  644. m32 = a32;
  645. m33 = a33;
  646. }
  647. inline void Matrix3x3::SetIdentity ( )
  648. {
  649. m11 = m22 = m33 = 1.0;
  650. m12 = m13 = m21 = m23 = m31 = m32 = 0.0;
  651. }
  652. inline void Matrix3x3::SetZero( )
  653. {
  654. m11 = m12 = m13 = m21 = m22 = m23 = m31 = m32 = m33 = 0.0;
  655. }
  656. inline void Matrix3x3::Set ( const Matrix3x3& A ) // Set to the matrix.
  657. {
  658. m11 = A.m11;
  659. m21 = A.m21;
  660. m31 = A.m31;
  661. m12 = A.m12;
  662. m22 = A.m22;
  663. m32 = A.m32;
  664. m13 = A.m13;
  665. m23 = A.m23;
  666. m33 = A.m33;
  667. }
  668. inline void Matrix3x3::Set3x3 ( const Matrix3x4& A ) // Set to the 3x3 part of the matrix.
  669. {
  670. m11 = A.m11;
  671. m21 = A.m21;
  672. m31 = A.m31;
  673. m12 = A.m12;
  674. m22 = A.m22;
  675. m32 = A.m32;
  676. m13 = A.m13;
  677. m23 = A.m23;
  678. m33 = A.m33;
  679. }
  680. inline void Matrix3x3::Set( const VectorR3& u, const VectorR3& v,
  681. const VectorR3& w)
  682. {
  683. m11 = u.x; // Column 1
  684. m21 = u.y;
  685. m31 = u.z;
  686. m12 = v.x; // Column 2
  687. m22 = v.y;
  688. m32 = v.z;
  689. m13 = w.x; // Column 3
  690. m23 = w.y;
  691. m33 = w.z;
  692. }
  693. inline void Matrix3x3::Set( double a11, double a21, double a31,
  694. double a12, double a22, double a32,
  695. double a13, double a23, double a33)
  696. // Values specified in column order!!!
  697. {
  698. m11 = a11; // Row 1
  699. m12 = a12;
  700. m13 = a13;
  701. m21 = a21; // Row 2
  702. m22 = a22;
  703. m23 = a23;
  704. m31 = a31; // Row 3
  705. m32 = a32;
  706. m33 = a33;
  707. }
  708. inline void Matrix3x3::SetByRows( double a11, double a12, double a13,
  709. double a21, double a22, double a23,
  710. double a31, double a32, double a33)
  711. // Values specified in row order!!!
  712. {
  713. m11 = a11; // Row 1
  714. m12 = a12;
  715. m13 = a13;
  716. m21 = a21; // Row 2
  717. m22 = a22;
  718. m23 = a23;
  719. m31 = a31; // Row 3
  720. m32 = a32;
  721. m33 = a33;
  722. }
  723. inline void Matrix3x3::SetByRows( const VectorR3& u, const VectorR3& v,
  724. const VectorR3& s )
  725. {
  726. m11 = u.x; // Row 1
  727. m12 = u.y;
  728. m13 = u.z;
  729. m21 = v.x; // Row 2
  730. m22 = v.y;
  731. m23 = v.z;
  732. m31 = s.x; // Row 3
  733. m32 = s.y;
  734. m33 = s.z;
  735. }
  736. inline void Matrix3x3::SetColumn1 ( double x, double y, double z)
  737. {
  738. m11 = x; m21 = y; m31= z;
  739. }
  740. inline void Matrix3x3::SetColumn2 ( double x, double y, double z)
  741. {
  742. m12 = x; m22 = y; m32= z;
  743. }
  744. inline void Matrix3x3::SetColumn3 ( double x, double y, double z)
  745. {
  746. m13 = x; m23 = y; m33= z;
  747. }
  748. inline void Matrix3x3::SetColumn1 ( const VectorR3& u )
  749. {
  750. m11 = u.x; m21 = u.y; m31 = u.z;
  751. }
  752. inline void Matrix3x3::SetColumn2 ( const VectorR3& u )
  753. {
  754. m12 = u.x; m22 = u.y; m32 = u.z;
  755. }
  756. inline void Matrix3x3::SetColumn3 ( const VectorR3& u )
  757. {
  758. m13 = u.x; m23 = u.y; m33 = u.z;
  759. }
  760. inline void Matrix3x3::SetRow1 ( double x, double y, double z )
  761. {
  762. m11 = x;
  763. m12 = y;
  764. m13 = z;
  765. }
  766. inline void Matrix3x3::SetRow2 ( double x, double y, double z )
  767. {
  768. m21 = x;
  769. m22 = y;
  770. m23 = z;
  771. }
  772. inline void Matrix3x3::SetRow3 ( double x, double y, double z )
  773. {
  774. m31 = x;
  775. m32 = y;
  776. m33 = z;
  777. }
  778. inline VectorR3 Matrix3x3::Column1() const
  779. {
  780. return ( VectorR3(m11, m21, m31) );
  781. }
  782. inline VectorR3 Matrix3x3::Column2() const
  783. {
  784. return ( VectorR3(m12, m22, m32) );
  785. }
  786. inline VectorR3 Matrix3x3::Column3() const
  787. {
  788. return ( VectorR3(m13, m23, m33) );
  789. }
  790. inline VectorR3 Matrix3x3::Row1() const
  791. {
  792. return ( VectorR3(m11, m12, m13) );
  793. }
  794. inline VectorR3 Matrix3x3::Row2() const
  795. {
  796. return ( VectorR3(m21, m22, m23) );
  797. }
  798. inline VectorR3 Matrix3x3::Row3() const
  799. {
  800. return ( VectorR3(m31, m32, m33) );
  801. }
  802. inline void Matrix3x3::SetDiagonal( double x, double y, double z )
  803. {
  804. m11 = x;
  805. m22 = y;
  806. m33 = z;
  807. }
  808. inline void Matrix3x3::SetDiagonal( const VectorR3& u )
  809. {
  810. SetDiagonal ( u.x, u.y, u.z );
  811. }
  812. inline double Matrix3x3::Diagonal( int i )
  813. {
  814. switch (i) {
  815. case 0:
  816. return m11;
  817. case 1:
  818. return m22;
  819. case 2:
  820. return m33;
  821. default:
  822. assert(0);
  823. return 0.0;
  824. }
  825. }
  826. inline void Matrix3x3::MakeTranspose() // Transposes it.
  827. {
  828. register double temp;
  829. temp = m12;
  830. m12 = m21;
  831. m21=temp;
  832. temp = m13;
  833. m13 = m31;
  834. m31 = temp;
  835. temp = m23;
  836. m23 = m32;
  837. m32 = temp;
  838. }
  839. inline VectorR3 operator* ( const Matrix3x3& A, const VectorR3& u)
  840. {
  841. return( VectorR3( A.m11*u.x + A.m12*u.y + A.m13*u.z,
  842. A.m21*u.x + A.m22*u.y + A.m23*u.z,
  843. A.m31*u.x + A.m32*u.y + A.m33*u.z ) );
  844. }
  845. inline void Matrix3x3::Transform( VectorR3* u ) const {
  846. double newX, newY;
  847. newX = m11*u->x + m12*u->y + m13*u->z;
  848. newY = m21*u->x + m22*u->y + m23*u->z;
  849. u->z = m31*u->x + m32*u->y + m33*u->z;
  850. u->x = newX;
  851. u->y = newY;
  852. }
  853. inline void Matrix3x3::Transform( const VectorR3& src, VectorR3* dest ) const {
  854. dest->x = m11*src.x + m12*src.y + m13*src.z;
  855. dest->y = m21*src.x + m22*src.y + m23*src.z;
  856. dest->z = m31*src.x + m32*src.y + m33*src.z;
  857. }
  858. // ******************************************************
  859. // * Matrix3x4 class - inlined functions *
  860. // * * * * * * * * * * * * * * * * * * * * * * * * * * **
  861. inline Matrix3x4::Matrix3x4(const VectorR3& u, const VectorR3& v,
  862. const VectorR3& s, const VectorR3& t)
  863. {
  864. m11 = u.x; // Column 1
  865. m21 = u.y;
  866. m31 = u.z;
  867. m12 = v.x; // Column 2
  868. m22 = v.y;
  869. m32 = v.z;
  870. m13 = s.x; // Column 3
  871. m23 = s.y;
  872. m33 = s.z;
  873. m14 = t.x;
  874. m24 = t.y;
  875. m34 = t.z;
  876. }
  877. inline Matrix3x4::Matrix3x4(double a11, double a21, double a31,
  878. double a12, double a22, double a32,
  879. double a13, double a23, double a33,
  880. double a14, double a24, double a34)
  881. { // Values in COLUMN order!
  882. m11 = a11; // Row 1
  883. m12 = a12;
  884. m13 = a13;
  885. m14 = a14;
  886. m21 = a21; // Row 2
  887. m22 = a22;
  888. m23 = a23;
  889. m24 = a24;
  890. m31 = a31; // Row 3
  891. m32 = a32;
  892. m33 = a33;
  893. m34 = a34;
  894. }
  895. inline Matrix3x4::Matrix3x4( const Matrix3x3& A, const VectorR3& u )
  896. {
  897. Set(A, u);
  898. }
  899. inline void Matrix3x4::SetIdentity () // Set to the identity map
  900. {
  901. m11 = m22 = m33 = 1.0;
  902. m12 = m13 = m21 = m23 = m31 = m32 = 0.0;
  903. m14 = m24 = m34 = 0.0;
  904. }
  905. inline void Matrix3x4::SetZero () // Set to the zero map
  906. {
  907. m11 = m22 = m33 = 0.0;
  908. m12 = m13 = m21 = m23 = m31 = m32 = 0.0;
  909. m14 = m24 = m34 = 0.0;
  910. }
  911. inline void Matrix3x4::Set ( const Matrix3x4& A ) // Set to the matrix.
  912. {
  913. m11 = A.m11;
  914. m21 = A.m21;
  915. m31 = A.m31;
  916. m12 = A.m12;
  917. m22 = A.m22;
  918. m32 = A.m32;
  919. m13 = A.m13;
  920. m23 = A.m23;
  921. m33 = A.m33;
  922. m14 = A.m14;
  923. m24 = A.m24;
  924. m34 = A.m34;
  925. }
  926. inline void Matrix3x4::Set ( const Matrix3x3& A, const VectorR3& t ) // Set to the matrix plus 4th column
  927. {
  928. m11 = A.m11;
  929. m21 = A.m21;
  930. m31 = A.m31;
  931. m12 = A.m12;
  932. m22 = A.m22;
  933. m32 = A.m32;
  934. m13 = A.m13;
  935. m23 = A.m23;
  936. m33 = A.m33;
  937. m14 = t.x; // Column 4
  938. m24 = t.y;
  939. m34 = t.z;
  940. }
  941. // Set linear part to the matrix
  942. inline void Matrix3x4::Set3x3 ( const Matrix3x3& A )
  943. {
  944. m11 = A.m11;
  945. m21 = A.m21;
  946. m31 = A.m31;
  947. m12 = A.m12;
  948. m22 = A.m22;
  949. m32 = A.m32;
  950. m13 = A.m13;
  951. m23 = A.m23;
  952. m33 = A.m33;
  953. }
  954. inline void Matrix3x4::Set( const VectorR3& u, const VectorR3& v,
  955. const VectorR3& w, const VectorR3& t )
  956. {
  957. m11 = u.x; // Column 1
  958. m21 = u.y;
  959. m31 = u.z;
  960. m12 = v.x; // Column 2
  961. m22 = v.y;
  962. m32 = v.z;
  963. m13 = w.x; // Column 3
  964. m23 = w.y;
  965. m33 = w.z;
  966. m14 = t.x; // Column 4
  967. m24 = t.y;
  968. m34 = t.z;
  969. }
  970. inline void Matrix3x4::Set( double a11, double a21, double a31,
  971. double a12, double a22, double a32,
  972. double a13, double a23, double a33,
  973. double a14, double a24, double a34 )
  974. // Values specified in column order!!!
  975. {
  976. m11 = a11; // Row 1
  977. m12 = a12;
  978. m13 = a13;
  979. m14 = a14;
  980. m21 = a21; // Row 2
  981. m22 = a22;
  982. m23 = a23;
  983. m24 = a24;
  984. m31 = a31; // Row 3
  985. m32 = a32;
  986. m33 = a33;
  987. m34 = a34;
  988. }
  989. inline void Matrix3x4::Set3x3( double a11, double a21, double a31,
  990. double a12, double a22, double a32,
  991. double a13, double a23, double a33 )
  992. // Values specified in column order!!!
  993. {
  994. m11 = a11; // Row 1
  995. m12 = a12;
  996. m13 = a13;
  997. m21 = a21; // Row 2
  998. m22 = a22;
  999. m23 = a23;
  1000. m31 = a31; // Row 3
  1001. m32 = a32;
  1002. m33 = a33;
  1003. }
  1004. inline void Matrix3x4::SetByRows( double a11, double a12, double a13, double a14,
  1005. double a21, double a22, double a23, double a24,
  1006. double a31, double a32, double a33, double a34 )
  1007. // Values specified in row order!!!
  1008. {
  1009. m11 = a11; // Row 1
  1010. m12 = a12;
  1011. m13 = a13;
  1012. m14 = a14;
  1013. m21 = a21; // Row 2
  1014. m22 = a22;
  1015. m23 = a23;
  1016. m24 = a24;
  1017. m31 = a31; // Row 3
  1018. m32 = a32;
  1019. m33 = a33;
  1020. m34 = a34;
  1021. }
  1022. inline void Matrix3x4::SetColumn1 ( double x, double y, double z)
  1023. {
  1024. m11 = x; m21 = y; m31= z;
  1025. }
  1026. inline void Matrix3x4::SetColumn2 ( double x, double y, double z)
  1027. {
  1028. m12 = x; m22 = y; m32= z;
  1029. }
  1030. inline void Matrix3x4::SetColumn3 ( double x, double y, double z)
  1031. {
  1032. m13 = x; m23 = y; m33= z;
  1033. }
  1034. inline void Matrix3x4::SetColumn4 ( double x, double y, double z )
  1035. {
  1036. m14 = x; m24 = y; m34= z;
  1037. }
  1038. inline void Matrix3x4::SetColumn1 ( const VectorR3& u )
  1039. {
  1040. m11 = u.x; m21 = u.y; m31 = u.z;
  1041. }
  1042. inline void Matrix3x4::SetColumn2 ( const VectorR3& u )
  1043. {
  1044. m12 = u.x; m22 = u.y; m32 = u.z;
  1045. }
  1046. inline void Matrix3x4::SetColumn3 ( const VectorR3& u )
  1047. {
  1048. m13 = u.x; m23 = u.y; m33 = u.z;
  1049. }
  1050. inline void Matrix3x4::SetColumn4 ( const VectorR3& u )
  1051. {
  1052. m14 = u.x; m24 = u.y; m34 = u.z;
  1053. }
  1054. inline VectorR3 Matrix3x4::Column1() const
  1055. {
  1056. return ( VectorR3(m11, m21, m31) );
  1057. }
  1058. inline VectorR3 Matrix3x4::Column2() const
  1059. {
  1060. return ( VectorR3(m12, m22, m32) );
  1061. }
  1062. inline VectorR3 Matrix3x4::Column3() const
  1063. {
  1064. return ( VectorR3(m13, m23, m33) );
  1065. }
  1066. inline VectorR3 Matrix3x4::Column4() const
  1067. {
  1068. return ( VectorR3(m14, m24, m34) );
  1069. }
  1070. inline void Matrix3x4::SetRow1 ( double x, double y, double z, double w )
  1071. {
  1072. m11 = x;
  1073. m12 = y;
  1074. m13 = z;
  1075. m14 = w;
  1076. }
  1077. inline void Matrix3x4::SetRow2 ( double x, double y, double z, double w )
  1078. {
  1079. m21 = x;
  1080. m22 = y;
  1081. m23 = z;
  1082. m24 = w;
  1083. }
  1084. inline void Matrix3x4::SetRow3 ( double x, double y, double z, double w )
  1085. {
  1086. m31 = x;
  1087. m32 = y;
  1088. m33 = z;
  1089. m34 = w;
  1090. }
  1091. // Left multiply with a translation (so the translation is applied afterwards).
  1092. inline Matrix3x4& Matrix3x4::ApplyTranslationLeft( const VectorR3& u )
  1093. {
  1094. m14 += u.x;
  1095. m24 += u.y;
  1096. m34 += u.z;
  1097. return *this;
  1098. }
  1099. // Right multiply with a translation (so the translation is applied first).
  1100. inline Matrix3x4& Matrix3x4::ApplyTranslationRight( const VectorR3& u )
  1101. {
  1102. double new14 = m14 + m11*u.x + m12*u.y + m13*u.z;
  1103. double new24 = m24 + m21*u.x + m22*u.y + m23*u.z;
  1104. m34 = m34 + m31*u.x + m32*u.y + m33*u.z;
  1105. m14 = new14;
  1106. m24 = new24;
  1107. return *this;
  1108. }
  1109. // Left-multiply with a rotation around the y-axis.
  1110. inline Matrix3x4& Matrix3x4::ApplyYRotationLeft( double theta )
  1111. {
  1112. double costheta = cos(theta);
  1113. double sintheta = sin(theta);
  1114. return ApplyYRotationLeft( costheta, sintheta );
  1115. }
  1116. inline Matrix3x4& Matrix3x4::ApplyYRotationLeft( double costheta, double sintheta )
  1117. {
  1118. double tmp;
  1119. tmp = costheta*m11+sintheta*m31;
  1120. m31 = costheta*m31-sintheta*m11;
  1121. m11 = tmp;
  1122. tmp = costheta*m12+sintheta*m32;
  1123. m32 = costheta*m32-sintheta*m12;
  1124. m12 = tmp;
  1125. tmp = costheta*m13+sintheta*m33;
  1126. m33 = costheta*m33-sintheta*m13;
  1127. m13 = tmp;
  1128. tmp = costheta*m14+sintheta*m34;
  1129. m34 = costheta*m34-sintheta*m14;
  1130. m14 = tmp;
  1131. return *this;
  1132. }
  1133. inline VectorR3 Matrix3x4::Solve(const VectorR3& u) const // Returns solution
  1134. {
  1135. Matrix3x3 A;
  1136. A.Set3x3(*this);
  1137. return ( A.Solve( VectorR3(m14-u.x, m24-u.y, m34-u.z) ) );
  1138. }
  1139. inline void Matrix3x4::Transform( VectorR3* u ) const {
  1140. double newX, newY;
  1141. newX = m11*u->x + m12*u->y + m13*u->z + m14;
  1142. newY = m21*u->x + m22*u->y + m23*u->z + m24;
  1143. u->z = m31*u->x + m32*u->y + m33*u->z + m34;
  1144. u->x = newX;
  1145. u->y = newY;
  1146. }
  1147. inline void Matrix3x4::Transform3x3( VectorR3* u ) const {
  1148. double newX, newY;
  1149. newX = m11*u->x + m12*u->y + m13*u->z;
  1150. newY = m21*u->x + m22*u->y + m23*u->z;
  1151. u->z = m31*u->x + m32*u->y + m33*u->z;
  1152. u->x = newX;
  1153. u->y = newY;
  1154. }
  1155. inline void Matrix3x4::Transform( const VectorR3& src, VectorR3* dest ) const {
  1156. dest->x = m11*src.x + m12*src.y + m13*src.z + m14;
  1157. dest->y = m21*src.x + m22*src.y + m23*src.z + m24;
  1158. dest->z = m31*src.x + m32*src.y + m33*src.z + m34;
  1159. }
  1160. inline void Matrix3x4::Transform3x3( const VectorR3& src, VectorR3* dest ) const {
  1161. dest->x = m11*src.x + m12*src.y + m13*src.z;
  1162. dest->y = m21*src.x + m22*src.y + m23*src.z;
  1163. dest->z = m31*src.x + m32*src.y + m33*src.z;
  1164. }
  1165. inline void Matrix3x4::Transform3x3Transpose( VectorR3* u ) const {
  1166. double newX, newY;
  1167. newX = m11*u->x + m21*u->y + m31*u->z;
  1168. newY = m12*u->x + m22*u->y + m32*u->z;
  1169. u->z = m13*u->x + m23*u->y + m33*u->z;
  1170. u->x = newX;
  1171. u->y = newY;
  1172. }
  1173. inline void Matrix3x4::Transform3x3Transpose( const VectorR3& src, VectorR3* dest ) const {
  1174. dest->x = m11*src.x + m21*src.y + m31*src.z;
  1175. dest->y = m12*src.x + m22*src.y + m32*src.z;
  1176. dest->z = m13*src.x + m23*src.y + m33*src.z;
  1177. }
  1178. inline VectorR3 operator* ( const Matrix3x4& A, const VectorR3& u )
  1179. {
  1180. return( VectorR3( A.m11*u.x + A.m12*u.y + A.m13*u.z + A.m14,
  1181. A.m21*u.x + A.m22*u.y + A.m23*u.z + A.m24,
  1182. A.m31*u.x + A.m32*u.y + A.m33*u.z + A.m34) );
  1183. }
  1184. // ******************************************************
  1185. // * LinearMapR3 class - inlined functions *
  1186. // * * * * * * * * * * * * * * * * * * * * * * * * * * **
  1187. inline LinearMapR3::LinearMapR3()
  1188. {
  1189. SetZero();
  1190. return;
  1191. }
  1192. inline LinearMapR3::LinearMapR3( const VectorR3& u, const VectorR3& v,
  1193. const VectorR3& s )
  1194. :Matrix3x3 ( u, v, s )
  1195. { }
  1196. inline LinearMapR3::LinearMapR3(
  1197. double a11, double a21, double a31,
  1198. double a12, double a22, double a32,
  1199. double a13, double a23, double a33)
  1200. // Values specified in column order!!!
  1201. :Matrix3x3 ( a11, a21, a31, a12, a22, a32, a13, a23, a33)
  1202. { }
  1203. inline LinearMapR3::LinearMapR3 ( const Matrix3x3& A )
  1204. : Matrix3x3 (A)
  1205. {}
  1206. inline void LinearMapR3::SetZero( )
  1207. {
  1208. Matrix3x3::SetZero();
  1209. }
  1210. inline void LinearMapR3::Negate()
  1211. {
  1212. m11 = -m11; // Row 1
  1213. m12 = -m12;
  1214. m13 = -m13;
  1215. m21 = -m21; // Row 2
  1216. m22 = -m22;
  1217. m23 = -m23;
  1218. m31 = -m31; // Row 3
  1219. m32 = -m32;
  1220. m33 = -m33;
  1221. }
  1222. inline LinearMapR3& LinearMapR3::operator+= (const Matrix3x3& B)
  1223. {
  1224. m11 += B.m11;
  1225. m12 += B.m12;
  1226. m13 += B.m13;
  1227. m21 += B.m21;
  1228. m22 += B.m22;
  1229. m23 += B.m23;
  1230. m31 += B.m31;
  1231. m32 += B.m32;
  1232. m33 += B.m33;
  1233. return ( *this );
  1234. }
  1235. inline LinearMapR3& LinearMapR3::operator-= (const Matrix3x3& B)
  1236. {
  1237. m11 -= B.m11;
  1238. m12 -= B.m12;
  1239. m13 -= B.m13;
  1240. m21 -= B.m21;
  1241. m22 -= B.m22;
  1242. m23 -= B.m23;
  1243. m31 -= B.m31;
  1244. m32 -= B.m32;
  1245. m33 -= B.m33;
  1246. return( *this );
  1247. }
  1248. inline LinearMapR3 operator+ (const LinearMapR3& A, const Matrix3x3& B)
  1249. {
  1250. return (LinearMapR3( A.m11+B.m11, A.m21+B.m21, A.m31+B.m31,
  1251. A.m12+B.m12, A.m22+B.m22, A.m32+B.m32,
  1252. A.m13+B.m13, A.m23+B.m23, A.m33+B.m33 ) );
  1253. }
  1254. inline LinearMapR3 operator+ (const Matrix3x3& A, const LinearMapR3& B)
  1255. {
  1256. return (LinearMapR3( A.m11+B.m11, A.m21+B.m21, A.m31+B.m31,
  1257. A.m12+B.m12, A.m22+B.m22, A.m32+B.m32,
  1258. A.m13+B.m13, A.m23+B.m23, A.m33+B.m33 ) );
  1259. }
  1260. inline LinearMapR3 operator- (const LinearMapR3& A)
  1261. {
  1262. return( LinearMapR3( -A.m11, -A.m21, -A.m31,
  1263. -A.m12, -A.m22, -A.m32,
  1264. -A.m13, -A.m23, -A.m33 ) );
  1265. }
  1266. inline LinearMapR3 operator- (const Matrix3x3& A, const LinearMapR3& B)
  1267. {
  1268. return( LinearMapR3( A.m11-B.m11, A.m21-B.m21, A.m31-B.m31,
  1269. A.m12-B.m12, A.m22-B.m22, A.m32-B.m32,
  1270. A.m13-B.m13, A.m23-B.m23, A.m33-B.m33 ) );
  1271. }
  1272. inline LinearMapR3 operator- (const LinearMapR3& A, const Matrix3x3& B)
  1273. {
  1274. return( LinearMapR3( A.m11-B.m11, A.m21-B.m21, A.m31-B.m31,
  1275. A.m12-B.m12, A.m22-B.m22, A.m32-B.m32,
  1276. A.m13-B.m13, A.m23-B.m23, A.m33-B.m33 ) );
  1277. }
  1278. inline LinearMapR3& LinearMapR3::operator*= (register double b)
  1279. {
  1280. m11 *= b;
  1281. m12 *= b;
  1282. m13 *= b;
  1283. m21 *= b;
  1284. m22 *= b;
  1285. m23 *= b;
  1286. m31 *= b;
  1287. m32 *= b;
  1288. m33 *= b;
  1289. return ( *this);
  1290. }
  1291. inline LinearMapR3 operator* ( const LinearMapR3& A, register double b)
  1292. {
  1293. return( LinearMapR3( A.m11*b, A.m21*b, A.m31*b,
  1294. A.m12*b, A.m22*b, A.m32*b,
  1295. A.m13*b, A.m23*b, A.m33*b ) );
  1296. }
  1297. inline LinearMapR3 operator* ( register double b, const LinearMapR3& A)
  1298. {
  1299. return( LinearMapR3( A.m11*b, A.m21*b, A.m31*b,
  1300. A.m12*b, A.m22*b, A.m32*b,
  1301. A.m13*b, A.m23*b, A.m33*b ) );
  1302. }
  1303. inline LinearMapR3 operator/ ( const LinearMapR3& A, double b)
  1304. {
  1305. register double bInv = 1.0/b;
  1306. return( LinearMapR3( A.m11*bInv, A.m21*bInv, A.m31*bInv,
  1307. A.m12*bInv, A.m22*bInv, A.m32*bInv,
  1308. A.m13*bInv, A.m23*bInv, A.m33*bInv ) );
  1309. }
  1310. inline LinearMapR3& LinearMapR3::operator/= (register double b)
  1311. {
  1312. register double bInv = 1.0/b;
  1313. return ( *this *= bInv );
  1314. }
  1315. inline LinearMapR3& LinearMapR3::operator*= (const Matrix3x3& B) // Matrix product
  1316. {
  1317. OperatorTimesEquals( B );
  1318. return( *this );
  1319. }
  1320. inline VectorR3 LinearMapR3::Solve(const VectorR3& u) const // Returns solution
  1321. {
  1322. return ( Matrix3x3::Solve( u ) );
  1323. }
  1324. inline LinearMapR3 LinearMapR3::Transpose() const // Returns the transpose
  1325. {
  1326. return ( LinearMapR3 ( m11, m12, m13, m21, m22, m23, m31, m32, m33) );
  1327. }
  1328. // ******************************************************
  1329. // * AffineMapR3 class - inlined functions *
  1330. // * * * * * * * * * * * * * * * * * * * * * * * * * * **
  1331. inline AffineMapR3::AffineMapR3( double a11, double a21, double a31,
  1332. double a12, double a22, double a32,
  1333. double a13, double a23, double a33,
  1334. double a14, double a24, double a34)
  1335. { // Values in COLUMN order!
  1336. m11 = a11; // Row 1
  1337. m12 = a12;
  1338. m13 = a13;
  1339. m14 = a14;
  1340. m21 = a21; // Row 2
  1341. m22 = a22;
  1342. m23 = a23;
  1343. m24 = a24;
  1344. m31 = a31; // Row 3
  1345. m32 = a32;
  1346. m33 = a33;
  1347. m34 = a34;
  1348. }
  1349. inline AffineMapR3::AffineMapR3 (const VectorR3& u, const VectorR3& v,
  1350. const VectorR3& w, const VectorR3& t)
  1351. {
  1352. m11 = u.x; // Column 1
  1353. m21 = u.y;
  1354. m31 = u.z;
  1355. m12 = v.x; // Column 2
  1356. m22 = v.y;
  1357. m32 = v.z;
  1358. m13 = w.x; // Column 3
  1359. m23 = w.y;
  1360. m33 = w.z;
  1361. m14 = t.x; // Column 4
  1362. m24 = t.y;
  1363. m34 = t.z;
  1364. }
  1365. inline AffineMapR3::AffineMapR3 (const LinearMapR3& A, const VectorR3& t)
  1366. {
  1367. m11 = A.m11;
  1368. m12 = A.m12;
  1369. m13 = A.m13;
  1370. m14 = t.x;
  1371. m21 = A.m21;
  1372. m22 = A.m22;
  1373. m23 = A.m23;
  1374. m24 = t.y;
  1375. m31 = A.m31;
  1376. m32 = A.m32;
  1377. m33 = A.m33;
  1378. m34 = t.z;
  1379. }
  1380. inline void AffineMapR3::SetIdentity ( )
  1381. {
  1382. Matrix3x4::SetIdentity();
  1383. }
  1384. inline void AffineMapR3::SetZero( )
  1385. {
  1386. Matrix3x4::SetZero();
  1387. }
  1388. inline VectorR3 AffineMapR3::Solve(const VectorR3& u) const // Returns solution
  1389. {
  1390. return ( Matrix3x4::Solve( u ) );
  1391. }
  1392. inline AffineMapR3& AffineMapR3::operator+= (const Matrix3x4& B)
  1393. {
  1394. m11 += B.m11;
  1395. m21 += B.m21;
  1396. m31 += B.m31;
  1397. m12 += B.m12;
  1398. m22 += B.m22;
  1399. m32 += B.m32;
  1400. m13 += B.m13;
  1401. m23 += B.m23;
  1402. m33 += B.m33;
  1403. m14 += B.m14;
  1404. m24 += B.m24;
  1405. m34 += B.m34;
  1406. return (*this);
  1407. }
  1408. inline AffineMapR3& AffineMapR3::operator-= (const Matrix3x4& B)
  1409. {
  1410. m11 -= B.m11;
  1411. m21 -= B.m21;
  1412. m31 -= B.m31;
  1413. m12 -= B.m12;
  1414. m22 -= B.m22;
  1415. m32 -= B.m32;
  1416. m13 -= B.m13;
  1417. m23 -= B.m23;
  1418. m33 -= B.m33;
  1419. m14 -= B.m14;
  1420. m24 -= B.m24;
  1421. m34 -= B.m34;
  1422. return (*this);
  1423. }
  1424. inline AffineMapR3 operator+ (const AffineMapR3& A, const AffineMapR3& B)
  1425. {
  1426. return( AffineMapR3( A.m11+B.m11, A.m21+B.m21, A.m31+B.m31,
  1427. A.m12+B.m12, A.m22+B.m22, A.m32+B.m32,
  1428. A.m13+B.m13, A.m23+B.m23, A.m33+B.m33,
  1429. A.m14+B.m14, A.m23+B.m24, A.m34+B.m34) );
  1430. }
  1431. inline AffineMapR3 operator+ (const AffineMapR3& A, const Matrix3x3& B)
  1432. {
  1433. return( AffineMapR3( A.m11+B.m11, A.m21+B.m21, A.m31+B.m31,
  1434. A.m12+B.m12, A.m22+B.m22, A.m32+B.m32,
  1435. A.m13+B.m13, A.m23+B.m23, A.m33+B.m33,
  1436. A.m14, A.m23, A.m34 ) );
  1437. }
  1438. inline AffineMapR3 operator+ (const Matrix3x3& B, const AffineMapR3& A)
  1439. {
  1440. return( AffineMapR3( A.m11+B.m11, A.m21+B.m21, A.m31+B.m31,
  1441. A.m12+B.m12, A.m22+B.m22, A.m32+B.m32,
  1442. A.m13+B.m13, A.m23+B.m23, A.m33+B.m33,
  1443. A.m14, A.m23, A.m34 ) );
  1444. }
  1445. inline AffineMapR3 operator- (const AffineMapR3& A, const AffineMapR3& B)
  1446. {
  1447. return( AffineMapR3( A.m11-B.m11, A.m21-B.m21, A.m31-B.m31,
  1448. A.m12-B.m12, A.m22-B.m22, A.m32-B.m32,
  1449. A.m13-B.m13, A.m23-B.m23, A.m33-B.m33,
  1450. A.m14-B.m14, A.m23-B.m24, A.m34-B.m34) );
  1451. }
  1452. inline AffineMapR3 operator- (const AffineMapR3& A, const LinearMapR3& B)
  1453. {
  1454. return ( AffineMapR3( A.m11-B.m11, A.m21-B.m21, A.m31-B.m31,
  1455. A.m12-B.m12, A.m22-B.m22, A.m32-B.m32,
  1456. A.m13-B.m13, A.m23-B.m23, A.m33-B.m33,
  1457. A.m14, A.m23, A.m34 ) );
  1458. }
  1459. inline AffineMapR3 operator- (const LinearMapR3& B, const AffineMapR3& A)
  1460. {
  1461. return( AffineMapR3( A.m11-B.m11, A.m21-B.m21, A.m31-B.m31,
  1462. A.m12-B.m12, A.m22-B.m22, A.m32-B.m32,
  1463. A.m13-B.m13, A.m23-B.m23, A.m33-B.m33,
  1464. A.m14, A.m23, A.m34 ) );
  1465. }
  1466. inline AffineMapR3& AffineMapR3::operator*= (register double b)
  1467. {
  1468. m11 *= b;
  1469. m12 *= b;
  1470. m13 *= b;
  1471. m21 *= b;
  1472. m22 *= b;
  1473. m23 *= b;
  1474. m31 *= b;
  1475. m32 *= b;
  1476. m33 *= b;
  1477. m14 *= b;
  1478. m24 *= b;
  1479. m34 *= b;
  1480. return (*this);
  1481. }
  1482. inline AffineMapR3 operator* (const AffineMapR3& A, register double b)
  1483. {
  1484. return(AffineMapR3( A.m11*b, A.m21*b, A.m31*b,
  1485. A.m12*b, A.m22*b, A.m32*b,
  1486. A.m13*b, A.m23*b, A.m33*b,
  1487. A.m14*b, A.m24*b, A.m34*b ) );
  1488. }
  1489. inline AffineMapR3 operator* (register double b, const AffineMapR3& A)
  1490. {
  1491. return(AffineMapR3( A.m11*b, A.m21*b, A.m31*b,
  1492. A.m12*b, A.m22*b, A.m32*b,
  1493. A.m13*b, A.m23*b, A.m33*b,
  1494. A.m14*b, A.m24*b, A.m34*b ) );
  1495. }
  1496. inline AffineMapR3& AffineMapR3::operator/= (double b)
  1497. {
  1498. register double bInv = 1.0/b;
  1499. *this *= bInv;
  1500. return( *this );
  1501. }
  1502. inline AffineMapR3 operator/ (const AffineMapR3& A, double b)
  1503. {
  1504. register double bInv = 1.0/b;
  1505. return(AffineMapR3( A.m11*bInv, A.m21*bInv, A.m31*bInv,
  1506. A.m12*bInv, A.m22*bInv, A.m32*bInv,
  1507. A.m13*bInv, A.m23*bInv, A.m33*bInv,
  1508. A.m14*bInv, A.m24*bInv, A.m34*bInv ) );
  1509. }
  1510. inline AffineMapR3& AffineMapR3::operator*= (const Matrix3x3& B) // Composition
  1511. {
  1512. OperatorTimesEquals( B );
  1513. return( *this );
  1514. }
  1515. inline AffineMapR3& AffineMapR3::operator*= (const Matrix3x4& B) // Composition
  1516. {
  1517. OperatorTimesEquals( B );
  1518. return( *this );
  1519. }
  1520. // **************************************************************
  1521. // RotationMapR3 class (inlined functions) *
  1522. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **
  1523. inline RotationMapR3::RotationMapR3()
  1524. {
  1525. SetIdentity();
  1526. return;
  1527. }
  1528. inline RotationMapR3::RotationMapR3( const VectorR3& u, const VectorR3& v,
  1529. const VectorR3& s )
  1530. :Matrix3x3 ( u, v, s )
  1531. { }
  1532. inline RotationMapR3::RotationMapR3(
  1533. double a11, double a21, double a31,
  1534. double a12, double a22, double a32,
  1535. double a13, double a23, double a33)
  1536. // Values specified in column order!!!
  1537. :Matrix3x3 ( a11, a21, a31, a12, a22, a32, a13, a23, a33)
  1538. { }
  1539. inline RotationMapR3 RotationMapR3::Inverse() const // Returns inverse
  1540. {
  1541. return ( RotationMapR3 ( m11, m12, m13, // In column order!
  1542. m21, m22, m23,
  1543. m31, m32, m33 ) );
  1544. }
  1545. inline RotationMapR3& RotationMapR3::Invert() // Converts into inverse.
  1546. {
  1547. register double temp;
  1548. temp = m12;
  1549. m12 = m21;
  1550. m21 = temp;
  1551. temp = m13;
  1552. m13 = m31;
  1553. m31 = temp;
  1554. temp = m23;
  1555. m23 = m32;
  1556. m32 = temp;
  1557. return ( *this );
  1558. }
  1559. inline VectorR3 RotationMapR3::Solve(const VectorR3& u) const // Returns solution
  1560. {
  1561. return( VectorR3( m11*u.x + m21*u.y + m31*u.z,
  1562. m12*u.x + m22*u.y + m32*u.z,
  1563. m13*u.x + m23*u.y + m33*u.z ) );
  1564. }
  1565. inline RotationMapR3& RotationMapR3::operator*= (const RotationMapR3& B) // Matrix product
  1566. {
  1567. OperatorTimesEquals( B );
  1568. return( *this );
  1569. }
  1570. // **************************************************************
  1571. // RigidMapR3 class (inlined functions) *
  1572. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **
  1573. inline RigidMapR3::RigidMapR3()
  1574. {
  1575. SetIdentity();
  1576. return;
  1577. }
  1578. inline RigidMapR3::RigidMapR3( const VectorR3& u, const VectorR3& v,
  1579. const VectorR3& s, const VectorR3& t )
  1580. :Matrix3x4 ( u, v, s, t )
  1581. {}
  1582. inline RigidMapR3::RigidMapR3(
  1583. double a11, double a21, double a31,
  1584. double a12, double a22, double a32,
  1585. double a13, double a23, double a33,
  1586. double a14, double a24, double a34)
  1587. // Values specified in column order!!!
  1588. :Matrix3x4 ( a11, a21, a31, a12, a22, a32, a13, a23, a33, a14, a24, a34 )
  1589. {}
  1590. inline RigidMapR3::RigidMapR3( const Matrix3x3& A, const VectorR3& u ) // Set to RotationMap & Vector
  1591. : Matrix3x4( A, u )
  1592. {}
  1593. inline RigidMapR3& RigidMapR3::Set( const Matrix3x3& A, const VectorR3& u ) // Set to RotationMap & Vector
  1594. {
  1595. Matrix3x4::Set( A, u );
  1596. return *this;
  1597. }
  1598. inline RigidMapR3& RigidMapR3::SetTranslationPart( const VectorR3& u) // Set the translation part
  1599. {
  1600. SetColumn4( u );
  1601. return *this;
  1602. }
  1603. inline RigidMapR3& RigidMapR3::SetTranslationPart( double x, double y, double z) // Set the translation part
  1604. {
  1605. SetColumn4( x, y, z );
  1606. return *this;
  1607. }
  1608. inline RigidMapR3& RigidMapR3::SetRotationPart( const Matrix3x3& A) // Set the rotation part
  1609. {
  1610. Matrix3x4::Set3x3( A );
  1611. return *this;
  1612. }
  1613. inline RigidMapR3& RigidMapR3::operator*= (const RotationMapR3& B) // Composition
  1614. {
  1615. OperatorTimesEquals( B );
  1616. return( *this );
  1617. }
  1618. inline RigidMapR3& RigidMapR3::operator*= (const RigidMapR3& B) // Composition
  1619. {
  1620. OperatorTimesEquals( B );
  1621. return( *this );
  1622. }
  1623. inline RigidMapR3 RigidMapR3::Inverse() const // Returns inverse
  1624. {
  1625. double new14 = -(m11*m14 + m21*m24 + m31*m34);
  1626. double new24 = -(m12*m14 + m22*m24 + m32*m34);
  1627. double new34 = -(m13*m14 + m23*m24 + m33*m34);
  1628. return ( RigidMapR3 ( m11, m12, m13, // In column order!
  1629. m21, m22, m23,
  1630. m31, m32, m33,
  1631. new14, new24, new34 ) );
  1632. }
  1633. inline RigidMapR3& RigidMapR3::Invert() // Converts into inverse.
  1634. {
  1635. double new14 = -(m11*m14 + m21*m24 + m31*m34);
  1636. double new24 = -(m12*m14 + m22*m24 + m32*m34);
  1637. m34 = -(m13*m14 + m23*m24 + m33*m34);
  1638. m14 = new14;
  1639. m24 = new24;
  1640. register double temp;
  1641. temp = m12;
  1642. m12 = m21;
  1643. m21 = temp;
  1644. temp = m13;
  1645. m13 = m31;
  1646. m31 = temp;
  1647. temp = m23;
  1648. m23 = m32;
  1649. m32 = temp;
  1650. return ( *this );
  1651. }
  1652. // ***************************************************************
  1653. // * 3-space vector and matrix utilities (inlined functions) *
  1654. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1655. // Returns the projection of u onto unit v
  1656. inline VectorR3 ProjectToUnit ( const VectorR3& u, const VectorR3& v)
  1657. {
  1658. return (u^v)*v;
  1659. }
  1660. // Returns the projection of u onto the plane perpindicular to the unit vector v
  1661. inline VectorR3 ProjectPerpUnit ( const VectorR3& u, const VectorR3& v)
  1662. {
  1663. return ( u - ((u^v)*v) );
  1664. }
  1665. // Returns the projection of u onto the plane perpindicular to the unit vector v
  1666. // This one is more stable when u and v are nearly equal.
  1667. inline VectorR3 ProjectPerpUnitDiff ( const VectorR3& u, const VectorR3& v)
  1668. {
  1669. VectorR3 ans = u;
  1670. ans -= v;
  1671. ans -= ((ans^v)*v);
  1672. return ans; // ans = (u-v) - ((u-v)^v)*v
  1673. }
  1674. // VectorProjectMap returns map projecting onto a given vector u.
  1675. // u should be a unit vector (otherwise the returned map is
  1676. // scaled according to the magnitude of u.
  1677. inline LinearMapR3 VectorProjectMap( const VectorR3& u )
  1678. {
  1679. double a = u.x*u.y;
  1680. double b = u.x*u.z;
  1681. double c = u.y*u.z;
  1682. return( LinearMapR3( u.x*u.x, a, b,
  1683. a, u.y*u.y, c,
  1684. b, c, u.z*u.z ) );
  1685. }
  1686. // PlaneProjectMap returns map projecting onto a given plane.
  1687. // The plane is the plane orthognal to w.
  1688. // w must be a unit vector (otherwise the returned map is
  1689. // garbage).
  1690. inline LinearMapR3 PlaneProjectMap ( const VectorR3& w )
  1691. {
  1692. double a = -w.x*w.y;
  1693. double b = -w.x*w.z;
  1694. double c = -w.y*w.z;
  1695. return( LinearMapR3( 1.0-w.x*w.x, a, b,
  1696. a, 1.0-w.y*w.y, c,
  1697. b, c, 1.0-w.z*w.z ) );
  1698. }
  1699. // PlaneProjectMap returns map projecting onto a given plane.
  1700. // The plane is the plane containing the two orthonormal vectors u,v.
  1701. // If u, v are orthonormal, this is a projection with scaling.
  1702. // If they are not orthonormal, the results are more difficult
  1703. // to interpret.
  1704. inline LinearMapR3 PlaneProjectMap ( const VectorR3& u, const VectorR3 &v )
  1705. {
  1706. double a = u.x*u.y + v.x*v.y;
  1707. double b = u.x*u.z + v.x*v.z;
  1708. double c = u.y*u.z + v.y*v.z;
  1709. return( LinearMapR3( u.x*u.x+v.x*v.x, a, b,
  1710. a, u.y*u.y+u.y*u.y, c,
  1711. b, c, u.z*u.z+v.z*v.z ) );
  1712. }
  1713. // Returns the solid angle between unit vectors v and w.
  1714. inline double SolidAngle( const VectorR3& v, const VectorR3& w)
  1715. {
  1716. return atan2 ( (v*w).Norm(), v^w );
  1717. }
  1718. #endif
  1719. // ******************* End of header material ********************