My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

types.h 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include <math.h>
  24. #include <stddef.h>
  25. #include "millis_t.h"
  26. class __FlashStringHelper;
  27. typedef const __FlashStringHelper *progmem_str;
  28. //
  29. // Enumerated axis indices
  30. //
  31. // - X_AXIS, Y_AXIS, and Z_AXIS should be used for axes in Cartesian space
  32. // - A_AXIS, B_AXIS, and C_AXIS should be used for Steppers, corresponding to XYZ on Cartesians
  33. // - X_HEAD, Y_HEAD, and Z_HEAD should be used for Steppers on Core kinematics
  34. //
  35. enum AxisEnum : uint8_t {
  36. X_AXIS = 0, A_AXIS = 0,
  37. Y_AXIS = 1, B_AXIS = 1,
  38. Z_AXIS = 2, C_AXIS = 2,
  39. E_AXIS = 3,
  40. X_HEAD = 4, Y_HEAD = 5, Z_HEAD = 6,
  41. E0_AXIS = 3,
  42. E1_AXIS, E2_AXIS, E3_AXIS, E4_AXIS, E5_AXIS, E6_AXIS, E7_AXIS,
  43. ALL_AXES = 0xFE, NO_AXIS = 0xFF
  44. };
  45. //
  46. // Loop over XYZE axes
  47. //
  48. #define LOOP_S_LE_N(VAR, S, N) for (uint8_t VAR=(S); VAR<=(N); VAR++)
  49. #define LOOP_S_L_N(VAR, S, N) for (uint8_t VAR=(S); VAR<(N); VAR++)
  50. #define LOOP_LE_N(VAR, N) LOOP_S_LE_N(VAR, 0, N)
  51. #define LOOP_L_N(VAR, N) LOOP_S_L_N(VAR, 0, N)
  52. #define LOOP_XYZ(VAR) LOOP_S_LE_N(VAR, X_AXIS, Z_AXIS)
  53. #define LOOP_XYZE(VAR) LOOP_S_LE_N(VAR, X_AXIS, E_AXIS)
  54. #define LOOP_XYZE_N(VAR) LOOP_S_L_N(VAR, X_AXIS, XYZE_N)
  55. #define LOOP_ABC(VAR) LOOP_S_LE_N(VAR, A_AXIS, C_AXIS)
  56. #define LOOP_ABCE(VAR) LOOP_S_LE_N(VAR, A_AXIS, E_AXIS)
  57. #define LOOP_ABCE_N(VAR) LOOP_S_L_N(VAR, A_AXIS, XYZE_N)
  58. //
  59. // Conditional type assignment magic. For example...
  60. //
  61. // typename IF<(MYOPT==12), int, float>::type myvar;
  62. //
  63. template <bool, class L, class R>
  64. struct IF { typedef R type; };
  65. template <class L, class R>
  66. struct IF<true, L, R> { typedef L type; };
  67. //
  68. // feedRate_t is just a humble float
  69. //
  70. typedef float feedRate_t;
  71. // Conversion macros
  72. #define MMM_TO_MMS(MM_M) feedRate_t(float(MM_M) / 60.0f)
  73. #define MMS_TO_MMM(MM_S) (float(MM_S) * 60.0f)
  74. #define MMS_SCALED(V) ((V) * 0.01f * feedrate_percentage)
  75. //
  76. // Coordinates structures for XY, XYZ, XYZE...
  77. //
  78. // Helpers
  79. #define _RECIP(N) ((N) ? 1.0f / float(N) : 0.0f)
  80. #define _ABS(N) ((N) < 0 ? -(N) : (N))
  81. #define _LS(N) (N = (T)(uint32_t(N) << v))
  82. #define _RS(N) (N = (T)(uint32_t(N) >> v))
  83. #define FI FORCE_INLINE
  84. // Forward declarations
  85. template<typename T> struct XYval;
  86. template<typename T> struct XYZval;
  87. template<typename T> struct XYZEval;
  88. typedef struct XYval<bool> xy_bool_t;
  89. typedef struct XYZval<bool> xyz_bool_t;
  90. typedef struct XYZEval<bool> xyze_bool_t;
  91. typedef struct XYval<char> xy_char_t;
  92. typedef struct XYZval<char> xyz_char_t;
  93. typedef struct XYZEval<char> xyze_char_t;
  94. typedef struct XYval<unsigned char> xy_uchar_t;
  95. typedef struct XYZval<unsigned char> xyz_uchar_t;
  96. typedef struct XYZEval<unsigned char> xyze_uchar_t;
  97. typedef struct XYval<int8_t> xy_int8_t;
  98. typedef struct XYZval<int8_t> xyz_int8_t;
  99. typedef struct XYZEval<int8_t> xyze_int8_t;
  100. typedef struct XYval<uint8_t> xy_uint8_t;
  101. typedef struct XYZval<uint8_t> xyz_uint8_t;
  102. typedef struct XYZEval<uint8_t> xyze_uint8_t;
  103. typedef struct XYval<int16_t> xy_int_t;
  104. typedef struct XYZval<int16_t> xyz_int_t;
  105. typedef struct XYZEval<int16_t> xyze_int_t;
  106. typedef struct XYval<uint16_t> xy_uint_t;
  107. typedef struct XYZval<uint16_t> xyz_uint_t;
  108. typedef struct XYZEval<uint16_t> xyze_uint_t;
  109. typedef struct XYval<int32_t> xy_long_t;
  110. typedef struct XYZval<int32_t> xyz_long_t;
  111. typedef struct XYZEval<int32_t> xyze_long_t;
  112. typedef struct XYval<uint32_t> xy_ulong_t;
  113. typedef struct XYZval<uint32_t> xyz_ulong_t;
  114. typedef struct XYZEval<uint32_t> xyze_ulong_t;
  115. typedef struct XYZval<volatile int32_t> xyz_vlong_t;
  116. typedef struct XYZEval<volatile int32_t> xyze_vlong_t;
  117. typedef struct XYval<float> xy_float_t;
  118. typedef struct XYZval<float> xyz_float_t;
  119. typedef struct XYZEval<float> xyze_float_t;
  120. typedef struct XYval<feedRate_t> xy_feedrate_t;
  121. typedef struct XYZval<feedRate_t> xyz_feedrate_t;
  122. typedef struct XYZEval<feedRate_t> xyze_feedrate_t;
  123. typedef xy_uint8_t xy_byte_t;
  124. typedef xyz_uint8_t xyz_byte_t;
  125. typedef xyze_uint8_t xyze_byte_t;
  126. typedef xyz_long_t abc_long_t;
  127. typedef xyze_long_t abce_long_t;
  128. typedef xyz_ulong_t abc_ulong_t;
  129. typedef xyze_ulong_t abce_ulong_t;
  130. typedef xy_float_t xy_pos_t;
  131. typedef xyz_float_t xyz_pos_t;
  132. typedef xyze_float_t xyze_pos_t;
  133. typedef xy_float_t ab_float_t;
  134. typedef xyz_float_t abc_float_t;
  135. typedef xyze_float_t abce_float_t;
  136. typedef ab_float_t ab_pos_t;
  137. typedef abc_float_t abc_pos_t;
  138. typedef abce_float_t abce_pos_t;
  139. // External conversion methods
  140. void toLogical(xy_pos_t &raw);
  141. void toLogical(xyz_pos_t &raw);
  142. void toLogical(xyze_pos_t &raw);
  143. void toNative(xy_pos_t &raw);
  144. void toNative(xyz_pos_t &raw);
  145. void toNative(xyze_pos_t &raw);
  146. //
  147. // XY coordinates, counters, etc.
  148. //
  149. template<typename T>
  150. struct XYval {
  151. union {
  152. struct { T x, y; };
  153. struct { T a, b; };
  154. T pos[2];
  155. };
  156. FI void set(const T px) { x = px; }
  157. FI void set(const T px, const T py) { x = px; y = py; }
  158. FI void reset() { x = y = 0; }
  159. FI T magnitude() const { return (T)sqrtf(x*x + y*y); }
  160. FI operator T* () { return pos; }
  161. FI operator bool() { return x || y; }
  162. FI XYval<T> copy() const { return *this; }
  163. FI XYval<T> ABS() const { return { T(_ABS(x)), T(_ABS(y)) }; }
  164. FI XYval<int16_t> asInt() { return { int16_t(x), int16_t(y) }; }
  165. FI XYval<int16_t> asInt() const { return { int16_t(x), int16_t(y) }; }
  166. FI XYval<int32_t> asLong() { return { int32_t(x), int32_t(y) }; }
  167. FI XYval<int32_t> asLong() const { return { int32_t(x), int32_t(y) }; }
  168. FI XYval<float> asFloat() { return { float(x), float(y) }; }
  169. FI XYval<float> asFloat() const { return { float(x), float(y) }; }
  170. FI XYval<float> reciprocal() const { return { _RECIP(x), _RECIP(y) }; }
  171. FI XYval<float> asLogical() const { XYval<float> o = asFloat(); toLogical(o); return o; }
  172. FI XYval<float> asNative() const { XYval<float> o = asFloat(); toNative(o); return o; }
  173. FI operator XYZval<T>() { return { x, y }; }
  174. FI operator XYZval<T>() const { return { x, y }; }
  175. FI operator XYZEval<T>() { return { x, y }; }
  176. FI operator XYZEval<T>() const { return { x, y }; }
  177. FI T& operator[](const int i) { return pos[i]; }
  178. FI const T& operator[](const int i) const { return pos[i]; }
  179. FI XYval<T>& operator= (const T v) { set(v, v ); return *this; }
  180. FI XYval<T>& operator= (const XYZval<T> &rs) { set(rs.x, rs.y); return *this; }
  181. FI XYval<T>& operator= (const XYZEval<T> &rs) { set(rs.x, rs.y); return *this; }
  182. FI XYval<T> operator+ (const XYval<T> &rs) const { XYval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
  183. FI XYval<T> operator+ (const XYval<T> &rs) { XYval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
  184. FI XYval<T> operator- (const XYval<T> &rs) const { XYval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
  185. FI XYval<T> operator- (const XYval<T> &rs) { XYval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
  186. FI XYval<T> operator* (const XYval<T> &rs) const { XYval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
  187. FI XYval<T> operator* (const XYval<T> &rs) { XYval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
  188. FI XYval<T> operator/ (const XYval<T> &rs) const { XYval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
  189. FI XYval<T> operator/ (const XYval<T> &rs) { XYval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
  190. FI XYval<T> operator+ (const XYZval<T> &rs) const { XYval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
  191. FI XYval<T> operator+ (const XYZval<T> &rs) { XYval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
  192. FI XYval<T> operator- (const XYZval<T> &rs) const { XYval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
  193. FI XYval<T> operator- (const XYZval<T> &rs) { XYval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
  194. FI XYval<T> operator* (const XYZval<T> &rs) const { XYval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
  195. FI XYval<T> operator* (const XYZval<T> &rs) { XYval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
  196. FI XYval<T> operator/ (const XYZval<T> &rs) const { XYval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
  197. FI XYval<T> operator/ (const XYZval<T> &rs) { XYval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
  198. FI XYval<T> operator+ (const XYZEval<T> &rs) const { XYval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
  199. FI XYval<T> operator+ (const XYZEval<T> &rs) { XYval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
  200. FI XYval<T> operator- (const XYZEval<T> &rs) const { XYval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
  201. FI XYval<T> operator- (const XYZEval<T> &rs) { XYval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
  202. FI XYval<T> operator* (const XYZEval<T> &rs) const { XYval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
  203. FI XYval<T> operator* (const XYZEval<T> &rs) { XYval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
  204. FI XYval<T> operator/ (const XYZEval<T> &rs) const { XYval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
  205. FI XYval<T> operator/ (const XYZEval<T> &rs) { XYval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
  206. FI XYval<T> operator* (const float &v) const { XYval<T> ls = *this; ls.x *= v; ls.y *= v; return ls; }
  207. FI XYval<T> operator* (const float &v) { XYval<T> ls = *this; ls.x *= v; ls.y *= v; return ls; }
  208. FI XYval<T> operator* (const int &v) const { XYval<T> ls = *this; ls.x *= v; ls.y *= v; return ls; }
  209. FI XYval<T> operator* (const int &v) { XYval<T> ls = *this; ls.x *= v; ls.y *= v; return ls; }
  210. FI XYval<T> operator/ (const float &v) const { XYval<T> ls = *this; ls.x /= v; ls.y /= v; return ls; }
  211. FI XYval<T> operator/ (const float &v) { XYval<T> ls = *this; ls.x /= v; ls.y /= v; return ls; }
  212. FI XYval<T> operator/ (const int &v) const { XYval<T> ls = *this; ls.x /= v; ls.y /= v; return ls; }
  213. FI XYval<T> operator/ (const int &v) { XYval<T> ls = *this; ls.x /= v; ls.y /= v; return ls; }
  214. FI XYval<T> operator>>(const int &v) const { XYval<T> ls = *this; _RS(ls.x); _RS(ls.y); return ls; }
  215. FI XYval<T> operator>>(const int &v) { XYval<T> ls = *this; _RS(ls.x); _RS(ls.y); return ls; }
  216. FI XYval<T> operator<<(const int &v) const { XYval<T> ls = *this; _LS(ls.x); _LS(ls.y); return ls; }
  217. FI XYval<T> operator<<(const int &v) { XYval<T> ls = *this; _LS(ls.x); _LS(ls.y); return ls; }
  218. FI XYval<T>& operator+=(const XYval<T> &rs) { x += rs.x; y += rs.y; return *this; }
  219. FI XYval<T>& operator-=(const XYval<T> &rs) { x -= rs.x; y -= rs.y; return *this; }
  220. FI XYval<T>& operator*=(const XYval<T> &rs) { x *= rs.x; y *= rs.y; return *this; }
  221. FI XYval<T>& operator+=(const XYZval<T> &rs) { x += rs.x; y += rs.y; return *this; }
  222. FI XYval<T>& operator-=(const XYZval<T> &rs) { x -= rs.x; y -= rs.y; return *this; }
  223. FI XYval<T>& operator*=(const XYZval<T> &rs) { x *= rs.x; y *= rs.y; return *this; }
  224. FI XYval<T>& operator+=(const XYZEval<T> &rs) { x += rs.x; y += rs.y; return *this; }
  225. FI XYval<T>& operator-=(const XYZEval<T> &rs) { x -= rs.x; y -= rs.y; return *this; }
  226. FI XYval<T>& operator*=(const XYZEval<T> &rs) { x *= rs.x; y *= rs.y; return *this; }
  227. FI XYval<T>& operator*=(const float &v) { x *= v; y *= v; return *this; }
  228. FI XYval<T>& operator*=(const int &v) { x *= v; y *= v; return *this; }
  229. FI XYval<T>& operator>>=(const int &v) { _RS(x); _RS(y); return *this; }
  230. FI XYval<T>& operator<<=(const int &v) { _LS(x); _LS(y); return *this; }
  231. FI bool operator==(const XYval<T> &rs) { return x == rs.x && y == rs.y; }
  232. FI bool operator==(const XYZval<T> &rs) { return x == rs.x && y == rs.y; }
  233. FI bool operator==(const XYZEval<T> &rs) { return x == rs.x && y == rs.y; }
  234. FI bool operator==(const XYval<T> &rs) const { return x == rs.x && y == rs.y; }
  235. FI bool operator==(const XYZval<T> &rs) const { return x == rs.x && y == rs.y; }
  236. FI bool operator==(const XYZEval<T> &rs) const { return x == rs.x && y == rs.y; }
  237. FI bool operator!=(const XYval<T> &rs) { return !operator==(rs); }
  238. FI bool operator!=(const XYZval<T> &rs) { return !operator==(rs); }
  239. FI bool operator!=(const XYZEval<T> &rs) { return !operator==(rs); }
  240. FI bool operator!=(const XYval<T> &rs) const { return !operator==(rs); }
  241. FI bool operator!=(const XYZval<T> &rs) const { return !operator==(rs); }
  242. FI bool operator!=(const XYZEval<T> &rs) const { return !operator==(rs); }
  243. FI XYval<T> operator-() { XYval<T> o = *this; o.x = -x; o.y = -y; return o; }
  244. FI const XYval<T> operator-() const { XYval<T> o = *this; o.x = -x; o.y = -y; return o; }
  245. };
  246. //
  247. // XYZ coordinates, counters, etc.
  248. //
  249. template<typename T>
  250. struct XYZval {
  251. union {
  252. struct { T x, y, z; };
  253. struct { T a, b, c; };
  254. T pos[3];
  255. };
  256. FI void set(const T px) { x = px; }
  257. FI void set(const T px, const T py) { x = px; y = py; }
  258. FI void set(const T px, const T py, const T pz) { x = px; y = py; z = pz; }
  259. FI void set(const XYval<T> pxy, const T pz) { x = pxy.x; y = pxy.y; z = pz; }
  260. FI void reset() { x = y = z = 0; }
  261. FI T magnitude() const { return (T)sqrtf(x*x + y*y + z*z); }
  262. FI operator T* () { return pos; }
  263. FI operator bool() { return z || x || y; }
  264. FI XYZval<T> copy() const { XYZval<T> o = *this; return o; }
  265. FI XYZval<T> ABS() const { return { T(_ABS(x)), T(_ABS(y)), T(_ABS(z)) }; }
  266. FI XYZval<int16_t> asInt() { return { int16_t(x), int16_t(y), int16_t(z) }; }
  267. FI XYZval<int16_t> asInt() const { return { int16_t(x), int16_t(y), int16_t(z) }; }
  268. FI XYZval<int32_t> asLong() { return { int32_t(x), int32_t(y), int32_t(z) }; }
  269. FI XYZval<int32_t> asLong() const { return { int32_t(x), int32_t(y), int32_t(z) }; }
  270. FI XYZval<float> asFloat() { return { float(x), float(y), float(z) }; }
  271. FI XYZval<float> asFloat() const { return { float(x), float(y), float(z) }; }
  272. FI XYZval<float> reciprocal() const { return { _RECIP(x), _RECIP(y), _RECIP(z) }; }
  273. FI XYZval<float> asLogical() const { XYZval<float> o = asFloat(); toLogical(o); return o; }
  274. FI XYZval<float> asNative() const { XYZval<float> o = asFloat(); toNative(o); return o; }
  275. FI operator XYval<T>&() { return *(XYval<T>*)this; }
  276. FI operator const XYval<T>&() const { return *(const XYval<T>*)this; }
  277. FI operator XYZEval<T>() const { return { x, y, z }; }
  278. FI T& operator[](const int i) { return pos[i]; }
  279. FI const T& operator[](const int i) const { return pos[i]; }
  280. FI XYZval<T>& operator= (const T v) { set(v, v, v ); return *this; }
  281. FI XYZval<T>& operator= (const XYval<T> &rs) { set(rs.x, rs.y ); return *this; }
  282. FI XYZval<T>& operator= (const XYZEval<T> &rs) { set(rs.x, rs.y, rs.z); return *this; }
  283. FI XYZval<T> operator+ (const XYval<T> &rs) const { XYZval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
  284. FI XYZval<T> operator+ (const XYval<T> &rs) { XYZval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
  285. FI XYZval<T> operator- (const XYval<T> &rs) const { XYZval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
  286. FI XYZval<T> operator- (const XYval<T> &rs) { XYZval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
  287. FI XYZval<T> operator* (const XYval<T> &rs) const { XYZval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
  288. FI XYZval<T> operator* (const XYval<T> &rs) { XYZval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
  289. FI XYZval<T> operator/ (const XYval<T> &rs) const { XYZval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
  290. FI XYZval<T> operator/ (const XYval<T> &rs) { XYZval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
  291. FI XYZval<T> operator+ (const XYZval<T> &rs) const { XYZval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; ls.z += rs.z; return ls; }
  292. FI XYZval<T> operator+ (const XYZval<T> &rs) { XYZval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; ls.z += rs.z; return ls; }
  293. FI XYZval<T> operator- (const XYZval<T> &rs) const { XYZval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; ls.z -= rs.z; return ls; }
  294. FI XYZval<T> operator- (const XYZval<T> &rs) { XYZval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; ls.z -= rs.z; return ls; }
  295. FI XYZval<T> operator* (const XYZval<T> &rs) const { XYZval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; ls.z *= rs.z; return ls; }
  296. FI XYZval<T> operator* (const XYZval<T> &rs) { XYZval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; ls.z *= rs.z; return ls; }
  297. FI XYZval<T> operator/ (const XYZval<T> &rs) const { XYZval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; ls.z /= rs.z; return ls; }
  298. FI XYZval<T> operator/ (const XYZval<T> &rs) { XYZval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; ls.z /= rs.z; return ls; }
  299. FI XYZval<T> operator+ (const XYZEval<T> &rs) const { XYZval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; ls.z += rs.z; return ls; }
  300. FI XYZval<T> operator+ (const XYZEval<T> &rs) { XYZval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; ls.z += rs.z; return ls; }
  301. FI XYZval<T> operator- (const XYZEval<T> &rs) const { XYZval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; ls.z -= rs.z; return ls; }
  302. FI XYZval<T> operator- (const XYZEval<T> &rs) { XYZval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; ls.z -= rs.z; return ls; }
  303. FI XYZval<T> operator* (const XYZEval<T> &rs) const { XYZval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; ls.z *= rs.z; return ls; }
  304. FI XYZval<T> operator* (const XYZEval<T> &rs) { XYZval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; ls.z *= rs.z; return ls; }
  305. FI XYZval<T> operator/ (const XYZEval<T> &rs) const { XYZval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; ls.z /= rs.z; return ls; }
  306. FI XYZval<T> operator/ (const XYZEval<T> &rs) { XYZval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; ls.z /= rs.z; return ls; }
  307. FI XYZval<T> operator* (const float &v) const { XYZval<T> ls = *this; ls.x *= v; ls.y *= v; ls.z *= z; return ls; }
  308. FI XYZval<T> operator* (const float &v) { XYZval<T> ls = *this; ls.x *= v; ls.y *= v; ls.z *= z; return ls; }
  309. FI XYZval<T> operator* (const int &v) const { XYZval<T> ls = *this; ls.x *= v; ls.y *= v; ls.z *= z; return ls; }
  310. FI XYZval<T> operator* (const int &v) { XYZval<T> ls = *this; ls.x *= v; ls.y *= v; ls.z *= z; return ls; }
  311. FI XYZval<T> operator/ (const float &v) const { XYZval<T> ls = *this; ls.x /= v; ls.y /= v; ls.z /= z; return ls; }
  312. FI XYZval<T> operator/ (const float &v) { XYZval<T> ls = *this; ls.x /= v; ls.y /= v; ls.z /= z; return ls; }
  313. FI XYZval<T> operator/ (const int &v) const { XYZval<T> ls = *this; ls.x /= v; ls.y /= v; ls.z /= z; return ls; }
  314. FI XYZval<T> operator/ (const int &v) { XYZval<T> ls = *this; ls.x /= v; ls.y /= v; ls.z /= z; return ls; }
  315. FI XYZval<T> operator>>(const int &v) const { XYZval<T> ls = *this; _RS(ls.x); _RS(ls.y); _RS(ls.z); return ls; }
  316. FI XYZval<T> operator>>(const int &v) { XYZval<T> ls = *this; _RS(ls.x); _RS(ls.y); _RS(ls.z); return ls; }
  317. FI XYZval<T> operator<<(const int &v) const { XYZval<T> ls = *this; _LS(ls.x); _LS(ls.y); _LS(ls.z); return ls; }
  318. FI XYZval<T> operator<<(const int &v) { XYZval<T> ls = *this; _LS(ls.x); _LS(ls.y); _LS(ls.z); return ls; }
  319. FI XYZval<T>& operator+=(const XYval<T> &rs) { x += rs.x; y += rs.y; return *this; }
  320. FI XYZval<T>& operator-=(const XYval<T> &rs) { x -= rs.x; y -= rs.y; return *this; }
  321. FI XYZval<T>& operator*=(const XYval<T> &rs) { x *= rs.x; y *= rs.y; return *this; }
  322. FI XYZval<T>& operator/=(const XYval<T> &rs) { x /= rs.x; y /= rs.y; return *this; }
  323. FI XYZval<T>& operator+=(const XYZval<T> &rs) { x += rs.x; y += rs.y; z += rs.z; return *this; }
  324. FI XYZval<T>& operator-=(const XYZval<T> &rs) { x -= rs.x; y -= rs.y; z -= rs.z; return *this; }
  325. FI XYZval<T>& operator*=(const XYZval<T> &rs) { x *= rs.x; y *= rs.y; z *= rs.z; return *this; }
  326. FI XYZval<T>& operator/=(const XYZval<T> &rs) { x /= rs.x; y /= rs.y; z /= rs.z; return *this; }
  327. FI XYZval<T>& operator+=(const XYZEval<T> &rs) { x += rs.x; y += rs.y; z += rs.z; return *this; }
  328. FI XYZval<T>& operator-=(const XYZEval<T> &rs) { x -= rs.x; y -= rs.y; z -= rs.z; return *this; }
  329. FI XYZval<T>& operator*=(const XYZEval<T> &rs) { x *= rs.x; y *= rs.y; z *= rs.z; return *this; }
  330. FI XYZval<T>& operator/=(const XYZEval<T> &rs) { x /= rs.x; y /= rs.y; z /= rs.z; return *this; }
  331. FI XYZval<T>& operator*=(const float &v) { x *= v; y *= v; z *= v; return *this; }
  332. FI XYZval<T>& operator*=(const int &v) { x *= v; y *= v; z *= v; return *this; }
  333. FI XYZval<T>& operator>>=(const int &v) { _RS(x); _RS(y); _RS(z); return *this; }
  334. FI XYZval<T>& operator<<=(const int &v) { _LS(x); _LS(y); _LS(z); return *this; }
  335. FI bool operator==(const XYZEval<T> &rs) { return x == rs.x && y == rs.y && z == rs.z; }
  336. FI bool operator!=(const XYZEval<T> &rs) { return !operator==(rs); }
  337. FI bool operator==(const XYZEval<T> &rs) const { return x == rs.x && y == rs.y && z == rs.z; }
  338. FI bool operator!=(const XYZEval<T> &rs) const { return !operator==(rs); }
  339. FI XYZval<T> operator-() { XYZval<T> o = *this; o.x = -x; o.y = -y; o.z = -z; return o; }
  340. FI const XYZval<T> operator-() const { XYZval<T> o = *this; o.x = -x; o.y = -y; o.z = -z; return o; }
  341. };
  342. //
  343. // XYZE coordinates, counters, etc.
  344. //
  345. template<typename T>
  346. struct XYZEval {
  347. union {
  348. struct{ T x, y, z, e; };
  349. struct{ T a, b, c; };
  350. T pos[4];
  351. };
  352. FI void reset() { x = y = z = e = 0; }
  353. FI T magnitude() const { return (T)sqrtf(x*x + y*y + z*z + e*e); }
  354. FI operator T* () { return pos; }
  355. FI operator bool() { return e || z || x || y; }
  356. FI void set(const T px) { x = px; }
  357. FI void set(const T px, const T py) { x = px; y = py; }
  358. FI void set(const T px, const T py, const T pz) { x = px; y = py; z = pz; }
  359. FI void set(const T px, const T py, const T pz, const T pe) { x = px; y = py; z = pz; e = pe; }
  360. FI void set(const XYval<T> pxy) { x = pxy.x; y = pxy.y; }
  361. FI void set(const XYval<T> pxy, const T pz) { x = pxy.x; y = pxy.y; z = pz; }
  362. FI void set(const XYZval<T> pxyz) { x = pxyz.x; y = pxyz.y; z = pxyz.z; }
  363. FI void set(const XYval<T> pxy, const T pz, const T pe) { x = pxy.x; y = pxy.y; z = pz; e = pe; }
  364. FI void set(const XYval<T> pxy, const XYval<T> pze) { x = pxy.x; y = pxy.y; z = pze.z; e = pze.e; }
  365. FI void set(const XYZval<T> pxyz, const T pe) { x = pxyz.x; y = pxyz.y; z = pxyz.z; e = pe; }
  366. FI XYZEval<T> copy() const { return *this; }
  367. FI XYZEval<T> ABS() const { return { T(_ABS(x)), T(_ABS(y)), T(_ABS(z)), T(_ABS(e)) }; }
  368. FI XYZEval<int16_t> asInt() { return { int16_t(x), int16_t(y), int16_t(z), int16_t(e) }; }
  369. FI XYZEval<int16_t> asInt() const { return { int16_t(x), int16_t(y), int16_t(z), int16_t(e) }; }
  370. FI XYZEval<int32_t> asLong() const { return { int32_t(x), int32_t(y), int32_t(z), int32_t(e) }; }
  371. FI XYZEval<int32_t> asLong() { return { int32_t(x), int32_t(y), int32_t(z), int32_t(e) }; }
  372. FI XYZEval<float> asFloat() { return { float(x), float(y), float(z), float(e) }; }
  373. FI XYZEval<float> asFloat() const { return { float(x), float(y), float(z), float(e) }; }
  374. FI XYZEval<float> reciprocal() const { return { _RECIP(x), _RECIP(y), _RECIP(z), _RECIP(e) }; }
  375. FI XYZEval<float> asLogical() const { XYZEval<float> o = asFloat(); toLogical(o); return o; }
  376. FI XYZEval<float> asNative() const { XYZEval<float> o = asFloat(); toNative(o); return o; }
  377. FI operator XYval<T>&() { return *(XYval<T>*)this; }
  378. FI operator const XYval<T>&() const { return *(const XYval<T>*)this; }
  379. FI operator XYZval<T>&() { return *(XYZval<T>*)this; }
  380. FI operator const XYZval<T>&() const { return *(const XYZval<T>*)this; }
  381. FI T& operator[](const int i) { return pos[i]; }
  382. FI const T& operator[](const int i) const { return pos[i]; }
  383. FI XYZEval<T>& operator= (const T v) { set(v, v, v, v); return *this; }
  384. FI XYZEval<T>& operator= (const XYval<T> &rs) { set(rs.x, rs.y); return *this; }
  385. FI XYZEval<T>& operator= (const XYZval<T> &rs) { set(rs.x, rs.y, rs.z); return *this; }
  386. FI XYZEval<T> operator+ (const XYval<T> &rs) const { XYZEval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
  387. FI XYZEval<T> operator+ (const XYval<T> &rs) { XYZEval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
  388. FI XYZEval<T> operator- (const XYval<T> &rs) const { XYZEval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
  389. FI XYZEval<T> operator- (const XYval<T> &rs) { XYZEval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
  390. FI XYZEval<T> operator* (const XYval<T> &rs) const { XYZEval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
  391. FI XYZEval<T> operator* (const XYval<T> &rs) { XYZEval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
  392. FI XYZEval<T> operator/ (const XYval<T> &rs) const { XYZEval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
  393. FI XYZEval<T> operator/ (const XYval<T> &rs) { XYZEval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
  394. FI XYZEval<T> operator+ (const XYZval<T> &rs) const { XYZEval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; ls.z += rs.z; return ls; }
  395. FI XYZEval<T> operator+ (const XYZval<T> &rs) { XYZEval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; ls.z += rs.z; return ls; }
  396. FI XYZEval<T> operator- (const XYZval<T> &rs) const { XYZEval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; ls.z -= rs.z; return ls; }
  397. FI XYZEval<T> operator- (const XYZval<T> &rs) { XYZEval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; ls.z -= rs.z; return ls; }
  398. FI XYZEval<T> operator* (const XYZval<T> &rs) const { XYZEval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; ls.z *= rs.z; return ls; }
  399. FI XYZEval<T> operator* (const XYZval<T> &rs) { XYZEval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; ls.z *= rs.z; return ls; }
  400. FI XYZEval<T> operator/ (const XYZval<T> &rs) const { XYZEval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; ls.z /= rs.z; return ls; }
  401. FI XYZEval<T> operator/ (const XYZval<T> &rs) { XYZEval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; ls.z /= rs.z; return ls; }
  402. FI XYZEval<T> operator+ (const XYZEval<T> &rs) const { XYZEval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; ls.z += rs.z; ls.e += rs.e; return ls; }
  403. FI XYZEval<T> operator+ (const XYZEval<T> &rs) { XYZEval<T> ls = *this; ls.x += rs.x; ls.y += rs.y; ls.z += rs.z; ls.e += rs.e; return ls; }
  404. FI XYZEval<T> operator- (const XYZEval<T> &rs) const { XYZEval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; ls.z -= rs.z; ls.e -= rs.e; return ls; }
  405. FI XYZEval<T> operator- (const XYZEval<T> &rs) { XYZEval<T> ls = *this; ls.x -= rs.x; ls.y -= rs.y; ls.z -= rs.z; ls.e -= rs.e; return ls; }
  406. FI XYZEval<T> operator* (const XYZEval<T> &rs) const { XYZEval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; ls.z *= rs.z; ls.e *= rs.e; return ls; }
  407. FI XYZEval<T> operator* (const XYZEval<T> &rs) { XYZEval<T> ls = *this; ls.x *= rs.x; ls.y *= rs.y; ls.z *= rs.z; ls.e *= rs.e; return ls; }
  408. FI XYZEval<T> operator/ (const XYZEval<T> &rs) const { XYZEval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; ls.z /= rs.z; ls.e /= rs.e; return ls; }
  409. FI XYZEval<T> operator/ (const XYZEval<T> &rs) { XYZEval<T> ls = *this; ls.x /= rs.x; ls.y /= rs.y; ls.z /= rs.z; ls.e /= rs.e; return ls; }
  410. FI XYZEval<T> operator* (const float &v) const { XYZEval<T> ls = *this; ls.x *= v; ls.y *= v; ls.z *= v; ls.e *= v; return ls; }
  411. FI XYZEval<T> operator* (const float &v) { XYZEval<T> ls = *this; ls.x *= v; ls.y *= v; ls.z *= v; ls.e *= v; return ls; }
  412. FI XYZEval<T> operator* (const int &v) const { XYZEval<T> ls = *this; ls.x *= v; ls.y *= v; ls.z *= v; ls.e *= v; return ls; }
  413. FI XYZEval<T> operator* (const int &v) { XYZEval<T> ls = *this; ls.x *= v; ls.y *= v; ls.z *= v; ls.e *= v; return ls; }
  414. FI XYZEval<T> operator/ (const float &v) const { XYZEval<T> ls = *this; ls.x /= v; ls.y /= v; ls.z /= v; ls.e /= v; return ls; }
  415. FI XYZEval<T> operator/ (const float &v) { XYZEval<T> ls = *this; ls.x /= v; ls.y /= v; ls.z /= v; ls.e /= v; return ls; }
  416. FI XYZEval<T> operator/ (const int &v) const { XYZEval<T> ls = *this; ls.x /= v; ls.y /= v; ls.z /= v; ls.e /= v; return ls; }
  417. FI XYZEval<T> operator/ (const int &v) { XYZEval<T> ls = *this; ls.x /= v; ls.y /= v; ls.z /= v; ls.e /= v; return ls; }
  418. FI XYZEval<T> operator>>(const int &v) const { XYZEval<T> ls = *this; _RS(ls.x); _RS(ls.y); _RS(ls.z); _RS(ls.e); return ls; }
  419. FI XYZEval<T> operator>>(const int &v) { XYZEval<T> ls = *this; _RS(ls.x); _RS(ls.y); _RS(ls.z); _RS(ls.e); return ls; }
  420. FI XYZEval<T> operator<<(const int &v) const { XYZEval<T> ls = *this; _LS(ls.x); _LS(ls.y); _LS(ls.z); _LS(ls.e); return ls; }
  421. FI XYZEval<T> operator<<(const int &v) { XYZEval<T> ls = *this; _LS(ls.x); _LS(ls.y); _LS(ls.z); _LS(ls.e); return ls; }
  422. FI XYZEval<T>& operator+=(const XYval<T> &rs) { x += rs.x; y += rs.y; return *this; }
  423. FI XYZEval<T>& operator-=(const XYval<T> &rs) { x -= rs.x; y -= rs.y; return *this; }
  424. FI XYZEval<T>& operator*=(const XYval<T> &rs) { x *= rs.x; y *= rs.y; return *this; }
  425. FI XYZEval<T>& operator/=(const XYval<T> &rs) { x /= rs.x; y /= rs.y; return *this; }
  426. FI XYZEval<T>& operator+=(const XYZval<T> &rs) { x += rs.x; y += rs.y; z += rs.z; return *this; }
  427. FI XYZEval<T>& operator-=(const XYZval<T> &rs) { x -= rs.x; y -= rs.y; z -= rs.z; return *this; }
  428. FI XYZEval<T>& operator*=(const XYZval<T> &rs) { x *= rs.x; y *= rs.y; z *= rs.z; return *this; }
  429. FI XYZEval<T>& operator/=(const XYZval<T> &rs) { x /= rs.x; y /= rs.y; z /= rs.z; return *this; }
  430. FI XYZEval<T>& operator+=(const XYZEval<T> &rs) { x += rs.x; y += rs.y; z += rs.z; e += rs.e; return *this; }
  431. FI XYZEval<T>& operator-=(const XYZEval<T> &rs) { x -= rs.x; y -= rs.y; z -= rs.z; e -= rs.e; return *this; }
  432. FI XYZEval<T>& operator*=(const XYZEval<T> &rs) { x *= rs.x; y *= rs.y; z *= rs.z; e *= rs.e; return *this; }
  433. FI XYZEval<T>& operator/=(const XYZEval<T> &rs) { x /= rs.x; y /= rs.y; z /= rs.z; e /= rs.e; return *this; }
  434. FI XYZEval<T>& operator*=(const T &v) { x *= v; y *= v; z *= v; e *= v; return *this; }
  435. FI XYZEval<T>& operator>>=(const int &v) { _RS(x); _RS(y); _RS(z); _RS(e); return *this; }
  436. FI XYZEval<T>& operator<<=(const int &v) { _LS(x); _LS(y); _LS(z); _LS(e); return *this; }
  437. FI bool operator==(const XYZval<T> &rs) { return x == rs.x && y == rs.y && z == rs.z; }
  438. FI bool operator!=(const XYZval<T> &rs) { return !operator==(rs); }
  439. FI bool operator==(const XYZval<T> &rs) const { return x == rs.x && y == rs.y && z == rs.z; }
  440. FI bool operator!=(const XYZval<T> &rs) const { return !operator==(rs); }
  441. FI XYZEval<T> operator-() { return { -x, -y, -z, -e }; }
  442. FI const XYZEval<T> operator-() const { return { -x, -y, -z, -e }; }
  443. };
  444. #undef _RECIP
  445. #undef _ABS
  446. #undef _LS
  447. #undef _RS
  448. #undef FI
  449. const xyze_char_t axis_codes { 'X', 'Y', 'Z', 'E' };