Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

282 строки
9.7 KiB

  1. {$IF DEFINED(__MATRIX_HELPER_INTERFACE)}
  2. type __HELPER = type helper for __MAT
  3. public const
  4. {$IF __SIZE = 2}
  5. Identity: __MAT = ((1, 0), (0, 1));
  6. {$ELSEIF __SIZE = 3}
  7. Identity: __MAT = ((1, 0, 0), (0, 1, 0), (0, 0, 1));
  8. {$ELSEIF __SIZE = 4}
  9. Identity: __MAT = ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1));
  10. {$ELSE}
  11. {$ERROR only vectors of size 2, 3 or 4 are supported}
  12. {$ENDIF}
  13. public
  14. function ToString(const aRound: Integer = -3): String; inline;
  15. function Transpose: __MAT; inline;
  16. function Determinant: Double; inline;
  17. function Invert: __MAT; inline;
  18. class function TryFromString(const s: String; out m: __MAT): Boolean; inline; static;
  19. class function FromString(const s: String): __MAT; inline; static;
  20. {$IF __SIZE = 3}
  21. function Sub(const aCol, aRow: Integer): __IMPL.TMat2; inline;
  22. function Adjoint: __MAT; inline;
  23. function Translate(const v: __IMPL.TVectorHelper.TVector2): __MAT; inline;
  24. function Translate(const x, y: __IMPL.TBaseType): __MAT; inline;
  25. function Scale(const v: __IMPL.TVectorHelper.TVector2): __MAT; inline;
  26. function Scale(const v: __IMPL.TBaseType): __MAT; inline;
  27. function Shear(const v: __IMPL.TVectorHelper.TVector2): __MAT; inline;
  28. function Shear(const x, y: __IMPL.TBaseType): __MAT; inline;
  29. function Rotate(const a: Double): __MAT; inline;
  30. class function CreateTranslate(const v: __IMPL.TVectorHelper.TVector2): __MAT; inline; static;
  31. class function CreateTranslate(const x, y: __IMPL.TBaseType): __MAT; inline; static;
  32. class function CreateScale(const v: __IMPL.TVectorHelper.TVector2): __MAT; inline; static;
  33. class function CreateScale(const v: __IMPL.TBaseType): __MAT; inline; static;
  34. class function CreateShear(const v: __IMPL.TVectorHelper.TVector2): __MAT; inline; static;
  35. class function CreateShear(const x, y: __IMPL.TBaseType): __MAT; inline; static;
  36. class function CreateRotate(const a: Double): __MAT; inline; static;
  37. {$ELSEIF __SIZE = 4}
  38. function Sub(const aCol, aRow: Integer): __IMPL.TMat3; inline;
  39. function Adjoint: __MAT; inline;
  40. function Translate(const v: __IMPL.TVectorHelper.TVector3): __MAT; inline;
  41. function Translate(const x, y, z: __IMPL.TBaseType): __MAT; inline;
  42. function Scale(const v: __IMPL.TVectorHelper.TVector3): __MAT; inline;
  43. function Scale(const v: __IMPL.TBaseType): __MAT; inline;
  44. function Rotate(const axis: __IMPL.TVectorHelper.TVector3; const a: Double): __MAT; inline;
  45. class function CreateTranslate(const v: __IMPL.TVectorHelper.TVector3): __MAT; inline; static;
  46. class function CreateTranslate(const x, y, z: __IMPL.TBaseType): __MAT; inline; static;
  47. class function CreateScale(const v: __IMPL.TVectorHelper.TVector3): __MAT; inline; static;
  48. class function CreateScale(const v: __IMPL.TBaseType): __MAT; inline; static;
  49. class function CreateShear(const x, y, z: __IMPL.TVectorHelper.TVector2): __MAT; inline; static;
  50. class function CreateShear(const xy, xz, yx, yz, zx, zy: __IMPL.TBaseType): __MAT; inline; static;
  51. class function CreateRotate(const axis: __IMPL.TVectorHelper.TVector3; const a: Double): __MAT; inline; static;
  52. {$ENDIF}
  53. end;
  54. operator = (const m1, m2: __MAT): Boolean; inline;
  55. operator * (const m1, m2: __MAT): __MAT; inline;
  56. operator * (const m: __MAT; const v: __VEC): __VEC; inline;
  57. operator * (const s: __IMPL.TBaseType; const m: __MAT): __MAT; inline;
  58. operator * (const m: __MAT; const s: __IMPL.TBaseType): __MAT; inline;
  59. {$ELSEIF DEFINED (__MATRIX_HELPER_IMPL)}
  60. operator = (const m1, m2: __MAT): Boolean;
  61. begin
  62. result := __IMPL.Equals(m1, m2);
  63. end;
  64. operator * (const m1, m2: __MAT): __MAT;
  65. begin
  66. result := __IMPL.Multiply(m1, m2);
  67. end;
  68. operator * (const m: __MAT; const v: __VEC): __VEC;
  69. begin
  70. result := __IMPL.Multiply(m, v);
  71. end;
  72. operator * (const s: __IMPL.TBaseType; const m: __MAT): __MAT;
  73. begin
  74. result := __IMPL.Multiply(m, s);
  75. end;
  76. operator * (const m: __MAT; const s: __IMPL.TBaseType): __MAT;
  77. begin
  78. result := __IMPL.Multiply(m, s);
  79. end;
  80. function __HELPER.ToString(const aRound: Integer = -3): String;
  81. begin
  82. result := __IMPL.ToString(self, aRound);
  83. end;
  84. function __HELPER.Transpose: __MAT;
  85. begin
  86. result := __IMPL.Transpose(self);
  87. end;
  88. function __HELPER.Determinant: Double;
  89. begin
  90. result := __IMPL.Determinant(self);
  91. end;
  92. function __HELPER.Invert: __MAT;
  93. begin
  94. result := __IMPL.Invert(self);
  95. end;
  96. class function __HELPER.TryFromString(const s: String; out m: __MAT): Boolean;
  97. begin
  98. result := __IMPL.TryFromString(s, m);
  99. end;
  100. class function __HELPER.FromString(const s: String): __MAT;
  101. begin
  102. if not __IMPL.TryFromString(s, result) then
  103. result := Identity;
  104. end;
  105. {$IF __SIZE = 3}
  106. function __HELPER.Sub(const aCol, aRow: Integer): __IMPL.TMat2;
  107. begin
  108. result := __IMPL.Sub(self, aCol, aRow);
  109. end;
  110. function __HELPER.Adjoint: __MAT;
  111. begin
  112. result := __IMPL.Adjoint(self);
  113. end;
  114. function __HELPER.Translate(const v: __IMPL.TVectorHelper.TVector2): __MAT;
  115. begin
  116. result := __IMPL.Multiply(self, __IMPL.CreateTranslate(v));
  117. end;
  118. function __HELPER.Translate(const x, y: __IMPL.TBaseType): __MAT;
  119. begin
  120. result := __IMPL.Multiply(self, __IMPL.CreateTranslate(__IMPL.TVectorHelper.Vector2(x, y)));
  121. end;
  122. function __HELPER.Scale(const v: __IMPL.TVectorHelper.TVector2): __MAT;
  123. begin
  124. result := __IMPL.Multiply(self, __IMPL.CreateScale(v));
  125. end;
  126. function __HELPER.Scale(const v: __IMPL.TBaseType): __MAT;
  127. begin
  128. result := __IMPL.Multiply(self, __IMPL.CreateScale(__IMPL.TVectorHelper.Vector2(v, v)));
  129. end;
  130. function __HELPER.Shear(const v: __IMPL.TVectorHelper.TVector2): __MAT;
  131. begin
  132. result := __IMPL.Multiply(self, __IMPL.CreateShear(v));
  133. end;
  134. function __HELPER.Shear(const x, y: __IMPL.TBaseType): __MAT;
  135. begin
  136. result := __IMPL.Multiply(self, __IMPL.CreateShear(__IMPL.TVectorHelper.Vector2(x, y)));
  137. end;
  138. function __HELPER.Rotate(const a: Double): __MAT;
  139. begin
  140. result := __IMPL.Multiply(self, __IMPL.CreateRotate(a));
  141. end;
  142. class function __HELPER.CreateTranslate(const v: __IMPL.TVectorHelper.TVector2): __MAT;
  143. begin
  144. result := __IMPL.CreateTranslate(v);
  145. end;
  146. class function __HELPER.CreateTranslate(const x, y: __IMPL.TBaseType): __MAT;
  147. begin
  148. result := __IMPL.CreateTranslate(__IMPL.TVectorHelper.Vector2(x, y));
  149. end;
  150. class function __HELPER.CreateScale(const v: __IMPL.TVectorHelper.TVector2): __MAT;
  151. begin
  152. result := __IMPL.CreateScale(v);
  153. end;
  154. class function __HELPER.CreateScale(const v: __IMPL.TBaseType): __MAT;
  155. begin
  156. result := __IMPL.CreateScale(__IMPL.TVectorHelper.Vector2(v, v));
  157. end;
  158. class function __HELPER.CreateShear(const v: __IMPL.TVectorHelper.TVector2): __MAT;
  159. begin
  160. result := __IMPL.CreateShear(v);
  161. end;
  162. class function __HELPER.CreateShear(const x, y: __IMPL.TBaseType): __MAT;
  163. begin
  164. result := __IMPL.CreateShear(__IMPL.TVectorHelper.Vector2(x, y));
  165. end;
  166. class function __HELPER.CreateRotate(const a: Double): __MAT;
  167. begin
  168. result := __IMPL.CreateRotate(a);
  169. end;
  170. {$ELSEIF __SIZE = 4}
  171. function __HELPER.Sub(const aCol, aRow: Integer): __IMPL.TMat3;
  172. begin
  173. result := __IMPL.Sub(self, aRow, aCol);
  174. end;
  175. function __HELPER.Adjoint: __MAT;
  176. begin
  177. result := __IMPL.Adjoint(self);
  178. end;
  179. function __HELPER.Translate(const v: __IMPL.TVectorHelper.TVector3): __MAT;
  180. begin
  181. result := __IMPL.Multiply(self, __IMPL.CreateTranslate(v));
  182. end;
  183. function __HELPER.Translate(const x, y, z: __IMPL.TBaseType): __MAT;
  184. begin
  185. result := __IMPL.Multiply(self, __IMPL.CreateTranslate(__IMPL.TVectorHelper.Vector3(x, y, z)));
  186. end;
  187. function __HELPER.Scale(const v: __IMPL.TVectorHelper.TVector3): __MAT;
  188. begin
  189. result := __IMPL.Multiply(self, __IMPL.CreateScale(v));
  190. end;
  191. function __HELPER.Scale(const v: __IMPL.TBaseType): __MAT;
  192. begin
  193. result := __IMPL.Multiply(self, __IMPL.CreateScale(__IMPL.TVectorHelper.Vector3(v, v, v)));
  194. end;
  195. function __HELPER.Rotate(const axis: __IMPL.TVectorHelper.TVector3; const a: Double): __MAT;
  196. begin
  197. result := __IMPL.Multiply(self, __IMPL.CreateRotate(axis, a));
  198. end;
  199. class function __HELPER.CreateTranslate(const v: __IMPL.TVectorHelper.TVector3): __MAT;
  200. begin
  201. result := __IMPL.CreateTranslate(v);
  202. end;
  203. class function __HELPER.CreateTranslate(const x, y, z: __IMPL.TBaseType): __MAT;
  204. begin
  205. result := __IMPL.CreateTranslate(__IMPL.TVectorHelper.Vector3(x, y, z));
  206. end;
  207. class function __HELPER.CreateScale(const v: __IMPL.TVectorHelper.TVector3): __MAT;
  208. begin
  209. result := __IMPL.CreateScale(v);
  210. end;
  211. class function __HELPER.CreateScale(const v: __IMPL.TBaseType): __MAT;
  212. begin
  213. result := __IMPL.CreateScale(__IMPL.TVectorHelper.Vector3(v, v, v));
  214. end;
  215. class function __HELPER.CreateShear(const x, y, z: __IMPL.TVectorHelper.TVector2): __MAT;
  216. begin
  217. result := __IMPL.CreateShear(x, y, z);
  218. end;
  219. class function __HELPER.CreateShear(const xy, xz, yx, yz, zx, zy: __IMPL.TBaseType): __MAT;
  220. begin
  221. result := __IMPL.CreateShear(
  222. __IMPL.TVectorHelper.Vector2(xy, xz),
  223. __IMPL.TVectorHelper.Vector2(yx, yz),
  224. __IMPL.TVectorHelper.Vector2(zx, zy));
  225. end;
  226. class function __HELPER.CreateRotate(const axis: __IMPL.TVectorHelper.TVector3; const a: Double): __MAT;
  227. begin
  228. result := __IMPL.CreateRotate(axis, a);
  229. end;
  230. {$ENDIF}
  231. {$ENDIF}
  232. {$UNDEF __IMPL}
  233. {$UNDEF __SIZE}
  234. {$UNDEF __VEC}
  235. {$UNDEF __MAT}
  236. {$UNDEF __HELPER}