2
0

affinemat4.monkey2 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. Namespace std.geom
  2. #rem monkeydoc Convenience type alias for AffineMat4\<Float\>
  3. #end
  4. Alias AffineMat4f:AffineMat4<Float>
  5. #rem monkeydoc The generic AffineMat4f class provides support for affine 4x4 matrices.
  6. An affine 4x4 matrix is a 4x4 matrix whose right hand column is always 0,0,0,1.
  7. Affine 4x4 matrices are often used for 3d transformations such as scaling, rotation and translation.
  8. Unless otherwise noted, methods and operators always return a new vec3 containing the result, without modifying any parameters or 'self'.
  9. This allows you to chain operators together easily just like 'real' expressions.
  10. #end
  11. Struct AffineMat4<T>
  12. #rem monkeydoc The matrix component of the matrix.
  13. #end
  14. Field m:Mat3<T>
  15. #rem monkeydoc The translation component of the matrix.
  16. #end
  17. Field t:Vec3<T>
  18. #rem monkeydoc Creates a new matrix.
  19. #end
  20. Method New()
  21. m.i.x=1; m.j.y=1; m.k.z=1
  22. End
  23. Method New( m:Mat3<T> )
  24. Self.m=m
  25. End
  26. Method New( t:Vec3<T> )
  27. m.i.x=1; m.j.y=1; m.k.z=1 ; Self.t=t
  28. End
  29. Method New( m:Mat3<T>,t:Vec3<T> )
  30. Self.m=m; Self.t=t
  31. End
  32. Method New( i:Vec3<T>,j:Vec3<T>,k:Vec3<T>,t:Vec3<T> )
  33. m.i=i; m.j=j; m.k=k; Self.t=t
  34. End
  35. Method New( ix:T,iy:T,iz:T,jx:T,jy:T,jz:T,kx:T,ky:T,kz:T,vx:T,vy:T,vz:T )
  36. m.i.x=ix; m.i.y=iy; m.i.z=iz
  37. m.j.x=jx; m.j.y=jy; m.j.z=jz
  38. m.k.x=kx; m.k.y=ky; m.k.z=kz
  39. t.x=vx; t.y=vy; t.z=vz
  40. End
  41. Method new( m:AffineMat3<T> )
  42. Self.m.i=New Vec3<T>( m.i,0 )
  43. Self.m.j=New Vec3<T>( m.j,0 )
  44. Self.m.k=New Vec3<T>( 0,0,1 )
  45. Self.t= New Vec3<T>( m.t,1 )
  46. End
  47. Method New( m:Mat4<T> )
  48. Self.m.i=m.i.XYZ
  49. Self.m.j=m.j.XYZ
  50. Self.m.k=m.k.XYZ
  51. Self.t=m.t.XYZ
  52. End
  53. #rem monkeydoc Converts the matrix to a matrix of a different type, or a printable string.
  54. #end
  55. Operator To<C>:AffineMat4<C>()
  56. Return New AffineMat4<C>( m,t )
  57. End
  58. Operator To:String()
  59. Return "AffineMat4("+m+","+t+")"
  60. End
  61. #rem monkeydoc Transposes the matrix.
  62. Transposing a matrix swaps rows and columns.
  63. #End
  64. Operator~:AffineMat4()
  65. Local i:=~m
  66. Return New AffineMat4( i,i*-t )
  67. End
  68. #rem monkeydoc Inverts the matrix.
  69. #end
  70. Operator-:AffineMat4()
  71. Local i:=-m
  72. Return New AffineMat4( i,i*-t )
  73. End
  74. #rem monkeydoc Multiplies the matrix by another matrix.
  75. #end
  76. Operator*:AffineMat4( q:AffineMat4 )
  77. Return New AffineMat4( m*q.m,m*q.t+t )
  78. End
  79. #rem monkeydoc Multiplies a vector by the matrix.
  80. #end
  81. Operator*:Vec3<T>( v:Vec3<T> )
  82. Return New Vec3<T>(
  83. m.i.x*v.x+m.j.x*v.y+m.k.x*v.z+t.x,
  84. m.i.y*v.x+m.j.y*v.y+m.k.y*v.z+t.y,
  85. m.i.z*v.x+m.j.z*v.y+m.k.z*v.z+t.z )
  86. End
  87. #rem monkeydoc Multiplies a box by the matrix.
  88. #end
  89. Operator*:Box<T>( box:Box<T> )
  90. Local r:=Box<T>.EmptyBounds
  91. For Local i:=0 Until 8
  92. r|=Self * box.Corner( i )
  93. Next
  94. Return r
  95. End
  96. #rem monkeydoc Applies a translation transformation to the matrix.
  97. #end
  98. Method Translate:AffineMat4( tx:T,ty:T,tz:T )
  99. Return Self * Translation( tx,ty,tz )
  100. End
  101. Method Translate:AffineMat4( tv:Vec3<T> )
  102. Return Self * Translation( tv )
  103. End
  104. #rem monkeydoc Applies a rotation transformation to the matrix.
  105. #end
  106. Method Rotate:AffineMat4( rx:Double,ry:Double,rz:Double )
  107. Return Self * Rotation( rx,ry,rz )
  108. End
  109. Method Rotate:AffineMat4( rv:Vec3<Double> )
  110. Return Self * Rotation( rv )
  111. End
  112. Method Rotate:AffineMat4( q:Quat<T> )
  113. Return Self * Rotation( q )
  114. End
  115. #rem monkeydoc Applies a scaling transformation to the matrix.
  116. #end
  117. Method Scale:AffineMat4( sx:T,sy:T,sz:T )
  118. Return Self * Scaling( sx,sy,sz )
  119. End
  120. Method Scale:AffineMat4( sv:Vec3<T> )
  121. Return Self * Scaling( sv )
  122. End
  123. Method Scale:AffineMat4f( scaling:T )
  124. Return Self * Scaling( scaling )
  125. End
  126. #rem monkeydoc Creates a matrix representing a translation.
  127. #end
  128. Function Translation:AffineMat4( tv:Vec3<T> )
  129. Return New AffineMat4( tv )
  130. End
  131. Function Translation:AffineMat4( tx:T,ty:T,tz:T )
  132. Return New AffineMat4( New Vec3<T>( tx,ty,tz ) )
  133. End
  134. #rem monkeydoc Creates a matrix repsenting a rotation form eular angles or a quat.
  135. Order of rotation is Yaw * Pitch * Roll.
  136. The `rotation` angle is in radians.
  137. #end
  138. Function Rotation:AffineMat4( rv:Vec3<Double> )
  139. Return New AffineMat4( Mat3<T>.Rotation( rv ) )
  140. End
  141. Function Rotation:AffineMat4( rx:Double,ry:Double,rz:Double )
  142. Return New AffineMat4( Mat3<T>.Rotation( rx,ry,rz ) )
  143. End
  144. Function Rotation:AffineMat4( q:Quat<T> )
  145. Return New AffineMat4( q )
  146. End
  147. #rem monkeydoc Creates a matrix representing a scaling.
  148. #end
  149. Function Scaling:AffineMat4( sv:Vec3<T> )
  150. Return New AffineMat4( Mat3<T>.Scaling( sv ) )
  151. End
  152. Function Scaling:AffineMat4( sx:T,sy:T,sz:T )
  153. Return New AffineMat4( Mat3<T>.Scaling( sx,sy,sz ) )
  154. End
  155. Function Scaling:AffineMat4( t:T )
  156. Return Scaling( t,t,t )
  157. End
  158. End