You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
10 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. function Multiply(const m: __MAT): __MAT; inline;
  19. class function TryFromString(const s: String; out m: __MAT): Boolean; inline; static;
  20. class function FromString(const s: String): __MAT; inline; static;
  21. {$IF __SIZE = 3}
  22. function AxisX: __VEC3; inline;
  23. function AxisY: __VEC3; inline;
  24. function Position: __VEC3; inline;
  25. function Sub(const aCol, aRow: Integer): __IMPL.TMat2; inline;
  26. function Adjoint: __MAT; inline;
  27. function Translate(const v: __VEC2): __MAT; inline;
  28. function Translate(const x, y: __IMPL.TBaseType): __MAT; inline;
  29. function Scale(const v: __VEC2): __MAT; inline;
  30. function Scale(const v: __IMPL.TBaseType): __MAT; inline;
  31. function Shear(const v: __VEC2): __MAT; inline;
  32. function Shear(const x, y: __IMPL.TBaseType): __MAT; inline;
  33. function Rotate(const a: Double): __MAT; inline;
  34. class function Create(const x, y, z: __VEC3): __MAT; inline; static;
  35. class function CreateTranslate(const v: __VEC2): __MAT; inline; static;
  36. class function CreateTranslate(const x, y: __IMPL.TBaseType): __MAT; inline; static;
  37. class function CreateScale(const v: __VEC2): __MAT; inline; static;
  38. class function CreateScale(const v: __IMPL.TBaseType): __MAT; inline; static;
  39. class function CreateShear(const v: __VEC2): __MAT; inline; static;
  40. class function CreateShear(const x, y: __IMPL.TBaseType): __MAT; inline; static;
  41. class function CreateRotate(const a: Double): __MAT; inline; static;
  42. {$ELSEIF __SIZE = 4}
  43. function AxisX: __VEC4; inline;
  44. function AxisY: __VEC4; inline;
  45. function AxisZ: __VEC4; inline;
  46. function Position: __VEC4; inline;
  47. function Sub(const aCol, aRow: Integer): __IMPL.TMat3; inline;
  48. function Adjoint: __MAT; inline;
  49. function Translate(const v: __VEC3): __MAT; inline;
  50. function Translate(const x, y, z: __IMPL.TBaseType): __MAT; inline;
  51. function Scale(const v: __VEC3): __MAT; inline;
  52. function Scale(const v: __IMPL.TBaseType): __MAT; inline;
  53. function Rotate(const axis: __VEC3; const a: Double): __MAT; inline;
  54. class function Create(const x, y, z, w: __VEC4): __MAT; inline; static;
  55. class function CreateTranslate(const v: __VEC3): __MAT; inline; static;
  56. class function CreateTranslate(const x, y, z: __IMPL.TBaseType): __MAT; inline; static;
  57. class function CreateScale(const v: __VEC3): __MAT; inline; static;
  58. class function CreateScale(const v: __IMPL.TBaseType): __MAT; inline; static;
  59. class function CreateShear(const x, y, z: __VEC2): __MAT; inline; static;
  60. class function CreateShear(const xy, xz, yx, yz, zx, zy: __IMPL.TBaseType): __MAT; inline; static;
  61. class function CreateRotate(const axis: __VEC3; const a: Double): __MAT; inline; static;
  62. {$ENDIF}
  63. end;
  64. operator = (const m1, m2: __MAT): Boolean; inline;
  65. operator * (const m1, m2: __MAT): __MAT; inline;
  66. operator * (const m: __MAT; const v: __VEC): __VEC; inline;
  67. operator * (const s: __IMPL.TBaseType; const m: __MAT): __MAT; inline;
  68. operator * (const m: __MAT; const s: __IMPL.TBaseType): __MAT; inline;
  69. {$ELSEIF DEFINED (__MATRIX_HELPER_IMPL)}
  70. operator = (const m1, m2: __MAT): Boolean;
  71. begin
  72. result := __IMPL.Equals(m1, m2);
  73. end;
  74. operator * (const m1, m2: __MAT): __MAT;
  75. begin
  76. result := __IMPL.Multiply(m1, m2);
  77. end;
  78. operator * (const m: __MAT; const v: __VEC): __VEC;
  79. begin
  80. result := __IMPL.Multiply(m, v);
  81. end;
  82. operator * (const s: __IMPL.TBaseType; const m: __MAT): __MAT;
  83. begin
  84. result := __IMPL.Multiply(m, s);
  85. end;
  86. operator * (const m: __MAT; const s: __IMPL.TBaseType): __MAT;
  87. begin
  88. result := __IMPL.Multiply(m, s);
  89. end;
  90. function __HELPER.ToString(const aRound: Integer = -3): String;
  91. begin
  92. result := __IMPL.ToString(self, aRound);
  93. end;
  94. function __HELPER.Transpose: __MAT;
  95. begin
  96. result := __IMPL.Transpose(self);
  97. end;
  98. function __HELPER.Determinant: Double;
  99. begin
  100. result := __IMPL.Determinant(self);
  101. end;
  102. function __HELPER.Invert: __MAT;
  103. begin
  104. result := __IMPL.Invert(self);
  105. end;
  106. function __HELPER.Multiply(const m: __MAT): __MAT;
  107. begin
  108. result := __IMPL.Multiply(self, m);
  109. end;
  110. class function __HELPER.TryFromString(const s: String; out m: __MAT): Boolean;
  111. begin
  112. result := __IMPL.TryFromString(s, m);
  113. end;
  114. class function __HELPER.FromString(const s: String): __MAT;
  115. begin
  116. if not __IMPL.TryFromString(s, result) then
  117. result := Identity;
  118. end;
  119. {$IF __SIZE = 3}
  120. function __HELPER.AxisX: __VEC3;
  121. begin
  122. result := self[0];
  123. end;
  124. function __HELPER.AxisY: __VEC3;
  125. begin
  126. result := self[1];
  127. end;
  128. function __HELPER.Position: __VEC3;
  129. begin
  130. result := self[2];
  131. end;
  132. function __HELPER.Sub(const aCol, aRow: Integer): __IMPL.TMat2;
  133. begin
  134. result := __IMPL.Sub(self, aCol, aRow);
  135. end;
  136. function __HELPER.Adjoint: __MAT;
  137. begin
  138. result := __IMPL.Adjoint(self);
  139. end;
  140. function __HELPER.Translate(const v: __VEC2): __MAT;
  141. begin
  142. result := __IMPL.Multiply(self, __IMPL.CreateTranslate(v));
  143. end;
  144. function __HELPER.Translate(const x, y: __IMPL.TBaseType): __MAT;
  145. begin
  146. result := __IMPL.Multiply(self, __IMPL.CreateTranslate(__IMPL.TVectorHelper.Vector2(x, y)));
  147. end;
  148. function __HELPER.Scale(const v: __VEC2): __MAT;
  149. begin
  150. result := __IMPL.Multiply(self, __IMPL.CreateScale(v));
  151. end;
  152. function __HELPER.Scale(const v: __IMPL.TBaseType): __MAT;
  153. begin
  154. result := __IMPL.Multiply(self, __IMPL.CreateScale(__IMPL.TVectorHelper.Vector2(v, v)));
  155. end;
  156. function __HELPER.Shear(const v: __VEC2): __MAT;
  157. begin
  158. result := __IMPL.Multiply(self, __IMPL.CreateShear(v));
  159. end;
  160. function __HELPER.Shear(const x, y: __IMPL.TBaseType): __MAT;
  161. begin
  162. result := __IMPL.Multiply(self, __IMPL.CreateShear(__IMPL.TVectorHelper.Vector2(x, y)));
  163. end;
  164. function __HELPER.Rotate(const a: Double): __MAT;
  165. begin
  166. result := __IMPL.Multiply(self, __IMPL.CreateRotate(a));
  167. end;
  168. class function __HELPER.Create(const x, y, z: __VEC3): __MAT;
  169. begin
  170. result := __IMPL.Create(x, y, z);
  171. end;
  172. class function __HELPER.CreateTranslate(const v: __VEC2): __MAT;
  173. begin
  174. result := __IMPL.CreateTranslate(v);
  175. end;
  176. class function __HELPER.CreateTranslate(const x, y: __IMPL.TBaseType): __MAT;
  177. begin
  178. result := __IMPL.CreateTranslate(__IMPL.TVectorHelper.Vector2(x, y));
  179. end;
  180. class function __HELPER.CreateScale(const v: __VEC2): __MAT;
  181. begin
  182. result := __IMPL.CreateScale(v);
  183. end;
  184. class function __HELPER.CreateScale(const v: __IMPL.TBaseType): __MAT;
  185. begin
  186. result := __IMPL.CreateScale(__IMPL.TVectorHelper.Vector2(v, v));
  187. end;
  188. class function __HELPER.CreateShear(const v: __VEC2): __MAT;
  189. begin
  190. result := __IMPL.CreateShear(v);
  191. end;
  192. class function __HELPER.CreateShear(const x, y: __IMPL.TBaseType): __MAT;
  193. begin
  194. result := __IMPL.CreateShear(__IMPL.TVectorHelper.Vector2(x, y));
  195. end;
  196. class function __HELPER.CreateRotate(const a: Double): __MAT;
  197. begin
  198. result := __IMPL.CreateRotate(a);
  199. end;
  200. {$ELSEIF __SIZE = 4}
  201. function __HELPER.AxisX: __VEC4;
  202. begin
  203. result := self[0];
  204. end;
  205. function __HELPER.AxisY: __VEC4;
  206. begin
  207. result := self[1];
  208. end;
  209. function __HELPER.AxisZ: __VEC4;
  210. begin
  211. result := self[2];
  212. end;
  213. function __HELPER.Position: __VEC4;
  214. begin
  215. result := self[3];
  216. end;
  217. function __HELPER.Sub(const aCol, aRow: Integer): __IMPL.TMat3;
  218. begin
  219. result := __IMPL.Sub(self, aRow, aCol);
  220. end;
  221. function __HELPER.Adjoint: __MAT;
  222. begin
  223. result := __IMPL.Adjoint(self);
  224. end;
  225. function __HELPER.Translate(const v: __VEC3): __MAT;
  226. begin
  227. result := __IMPL.Multiply(self, __IMPL.CreateTranslate(v));
  228. end;
  229. function __HELPER.Translate(const x, y, z: __IMPL.TBaseType): __MAT;
  230. begin
  231. result := __IMPL.Multiply(self, __IMPL.CreateTranslate(__IMPL.TVectorHelper.Vector3(x, y, z)));
  232. end;
  233. function __HELPER.Scale(const v: __VEC3): __MAT;
  234. begin
  235. result := __IMPL.Multiply(self, __IMPL.CreateScale(v));
  236. end;
  237. function __HELPER.Scale(const v: __IMPL.TBaseType): __MAT;
  238. begin
  239. result := __IMPL.Multiply(self, __IMPL.CreateScale(__IMPL.TVectorHelper.Vector3(v, v, v)));
  240. end;
  241. function __HELPER.Rotate(const axis: __VEC3; const a: Double): __MAT;
  242. begin
  243. result := __IMPL.Multiply(self, __IMPL.CreateRotate(axis, a));
  244. end;
  245. class function __HELPER.Create(const x, y, z, w: __VEC4): __MAT;
  246. begin
  247. result := __IMPL.Create(x, y, z, w);
  248. end;
  249. class function __HELPER.CreateTranslate(const v: __VEC3): __MAT;
  250. begin
  251. result := __IMPL.CreateTranslate(v);
  252. end;
  253. class function __HELPER.CreateTranslate(const x, y, z: __IMPL.TBaseType): __MAT;
  254. begin
  255. result := __IMPL.CreateTranslate(__IMPL.TVectorHelper.Vector3(x, y, z));
  256. end;
  257. class function __HELPER.CreateScale(const v: __VEC3): __MAT;
  258. begin
  259. result := __IMPL.CreateScale(v);
  260. end;
  261. class function __HELPER.CreateScale(const v: __IMPL.TBaseType): __MAT;
  262. begin
  263. result := __IMPL.CreateScale(__IMPL.TVectorHelper.Vector3(v, v, v));
  264. end;
  265. class function __HELPER.CreateShear(const x, y, z: __VEC2): __MAT;
  266. begin
  267. result := __IMPL.CreateShear(x, y, z);
  268. end;
  269. class function __HELPER.CreateShear(const xy, xz, yx, yz, zx, zy: __IMPL.TBaseType): __MAT;
  270. begin
  271. result := __IMPL.CreateShear(
  272. __IMPL.TVectorHelper.Vector2(xy, xz),
  273. __IMPL.TVectorHelper.Vector2(yx, yz),
  274. __IMPL.TVectorHelper.Vector2(zx, zy));
  275. end;
  276. class function __HELPER.CreateRotate(const axis: __VEC3; const a: Double): __MAT;
  277. begin
  278. result := __IMPL.CreateRotate(axis, a);
  279. end;
  280. {$ENDIF}
  281. {$ENDIF}
  282. {$UNDEF __IMPL}
  283. {$UNDEF __SIZE}
  284. {$UNDEF __VEC}
  285. {$UNDEF __MAT}
  286. {$UNDEF __HELPER}