affinemat4.monkey2 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. Namespace std.geom
  2. #rem monkeydoc @hidden Convenience type alias for AffineMat4\<Float\>
  3. #end
  4. Alias AffineMat4f:AffineMat4<Float>
  5. #rem monkeydoc @hidden Affine 4x4 matrix class.
  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. #end
  9. Struct AffineMat4<T>
  10. Field m:Mat3<T>
  11. Field t:Vec3<T>
  12. Method New()
  13. m.i.x=1; m.j.y=1; m.k.z=1
  14. End
  15. Method New( m:Mat3<T>,t:Vec3<T> )
  16. Self.m=m; Self.t=t
  17. End
  18. Method New( m:Mat3<T> )
  19. Self.m=m
  20. End
  21. Method New( t:Vec3<T> )
  22. m.i.x=1; m.j.y=1; m.k.z=1 ; Self.t=t
  23. End
  24. Method New( i:Vec3<T>,j:Vec3<T>,k:Vec3<T>,t:Vec3<T> )
  25. m.i=i; m.j=j; m.k=k; Self.t=t
  26. End
  27. 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 )
  28. m.i.x=ix; m.i.y=iy; m.i.z=iz
  29. m.j.x=jx; m.j.y=jy; m.j.z=jz
  30. m.k.x=kx; m.k.y=ky; m.k.z=kz
  31. t.x=vx; t.y=vy; t.z=vz
  32. End
  33. #rem monkeydoc Converts the matrix to a matrix of a different type.
  34. #end
  35. Operator To<C>:AffineMat4<C>()
  36. Return New AffineMat4<C>( m,t )
  37. End
  38. #rem monkeydoc Converts the matrix to a printable string.
  39. #end
  40. Operator To:String()
  41. Return "AffineMat4("+m+","+t+")"
  42. End
  43. #rem monkeydoc Returns the transpose of the matrix.
  44. #End
  45. Operator~:AffineMat4()
  46. Local i:=~m
  47. Return New AffineMat4( i,i*-t )
  48. End
  49. #rem monkeydoc Returns the inverse of the matrix.
  50. #end
  51. Operator-:AffineMat4()
  52. Local i:=-m
  53. Return New AffineMat4( i,i*-t )
  54. End
  55. #rem monkeydoc Multiplies the matrix by another matrix and returns the result.
  56. #end
  57. Operator*:AffineMat4( q:AffineMat4 )
  58. Return New AffineMat4( m*q.m,m*q.t+t )
  59. End
  60. #rem monkeydoc Multiplies a vector by the matrix and returns the result.
  61. #end
  62. Operator*:Vec3<T>( v:Vec3<T> )
  63. Return New Vec3<T>(
  64. m.i.x*v.x+m.j.x*v.y+m.k.x*v.z+t.x,
  65. m.i.y*v.x+m.j.y*v.y+m.k.y*v.z+t.y,
  66. m.i.z*v.x+m.j.z*v.y+m.k.z*v.z+t.z )
  67. End
  68. #rem monkeydoc Applies a translation transformation to the matrix and returns the result.
  69. #end
  70. Method Translate:AffineMat4( tx:T,ty:T,tz:T )
  71. Return Self * Translation( tx,ty,tz )
  72. End
  73. Method Translate:AffineMat4( tv:Vec3<T> )
  74. Return Self * Translation( tv )
  75. End
  76. #rem monkeydoc Applies a rotation transformation to the matrix and returns the result.
  77. #end
  78. Method Rotate:AffineMat4( rx:Double,ry:Double,rz:Double )
  79. Return Self * Rotation( rx,ry,rz )
  80. End
  81. Method Rotate:AffineMat4( rv:Vec3<Double> )
  82. Return Self * Rotation( rv )
  83. End
  84. #rem monkeydoc Applies a scaling transformation to the matrix and returns the result.
  85. #end
  86. Method Scale:AffineMat4( sx:T,sy:T,sz:T )
  87. Return Self * Scaling( sx,sy,sz )
  88. End
  89. Method Scale:AffineMat4( sv:Vec3<T> )
  90. Return Self * Scaling( sv )
  91. End
  92. Method Scale:AffineMat4f( scaling:T )
  93. Return Self * Scaling( scaling )
  94. End
  95. #rem monkeydoc Creates a translation matrix.
  96. #end
  97. Function Translation:AffineMat4( tv:Vec3<T> )
  98. Return New AffineMat4( tv )
  99. End
  100. Function Translation:AffineMat4( tx:T,ty:T,tz:T )
  101. Return New AffineMat4( New Vec3<T>( tx,ty,tz ) )
  102. End
  103. #rem monkeydoc Creates a rotation matrix from a quaternion.
  104. #end
  105. Function Rotation:AffineMat4( quat:Quat<T> )
  106. Return New AffineMat4( Mat3<T>.Rotation( quat ) )
  107. End
  108. #rem monkeydoc Creates a rotation matrix from euler angles.
  109. Order of rotation is Yaw * Pitch * Roll.
  110. #end
  111. Function Rotation:AffineMat4( rv:Vec3<Double> )
  112. Return New AffineMat4( Mat3<T>.Rotation( rv ) )
  113. End
  114. Function Rotation:AffineMat4( rx:Double,ry:Double,rz:Double )
  115. Return New AffineMat4( Mat3<T>.Rotation( rx,ry,rz ) )
  116. End
  117. #rem monkeydoc Creates a scaling matrix.
  118. #end
  119. Function Scaling:AffineMat4( sv:Vec3<T> )
  120. Return New AffineMat4( Mat3<T>.Scaling( sv ) )
  121. End
  122. Function Scaling:AffineMat4( sx:T,sy:T,sz:T )
  123. Return New AffineMat4( Mat3<T>.Scaling( sx,sy,sz ) )
  124. End
  125. Function Scaling:AffineMat4( t:T )
  126. Return Scaling( t,t,t )
  127. End
  128. End