Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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