diff --git a/cmake/sac-core-ext.txt b/cmake/sac-core-ext.txt index d4887c83..4e927d32 100644 --- a/cmake/sac-core-ext.txt +++ b/cmake/sac-core-ext.txt @@ -56,6 +56,8 @@ structures/ScalarArith.xsac Core structures/String.sac Core structures/StringArray.sac Ext structures/Structures.sac Core +structures/Vector3d.xsac Ext +structures/Vector3f.xsac Ext # System system/Clock.sac Core diff --git a/include/Vector3.mac b/include/Vector3.mac new file mode 100644 index 00000000..c2fc44fc --- /dev/null +++ b/include/Vector3.mac @@ -0,0 +1,171 @@ +#pragma safe + +module VECTOR3; + +export all; + +struct VECTOR3 { + REAL VX; + REAL VY; + REAL VZ; +}; + +inline REAL +l2norm(struct VECTOR3 v) +{ + x = _mul_SxS_(v.VX, v.VX); + y = _mul_SxS_(v.VY, v.VY); + z = _mul_SxS_(v.VZ, v.VZ); + return Math::sqrt(_add_SxS_(_add_SxS_(x, y), z)); +} + +/****************************************************************************** + * + * Binary operations + * + ******************************************************************************/ + +#define VEC3D_BIN_AxA(name, op) \ +inline struct VECTOR3[d>0:shp] \ +name(struct VECTOR3[d>0:shp] a, struct VECTOR3[d>0:shp] b) \ +{ \ + return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).VX, _sel_VxA_(iv, b).VX), \ + op(_sel_VxA_(iv, a).VY, _sel_VxA_(iv, b).VY), \ + op(_sel_VxA_(iv, a).VZ, _sel_VxA_(iv, b).VZ) } \ + | iv < shp }; \ +} + +#define VEC3D_BIN_AxS(name, op) \ +inline struct VECTOR3[d>0:shp] \ +name(struct VECTOR3[d>0:shp] a, struct VECTOR3 b) \ +{ \ + return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).VX, b.VX), \ + op(_sel_VxA_(iv, a).VY, b.VY), \ + op(_sel_VxA_(iv, a).VZ, b.VZ) } \ + | iv < shp }; \ +} + +#define VEC3D_BIN_AxX(name, op) \ +inline struct VECTOR3[d>0:shp] name(struct VECTOR3[d>0:shp] a, REAL b) \ +{ \ + return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).VX, b), \ + op(_sel_VxA_(iv, a).VY, b), \ + op(_sel_VxA_(iv, a).VZ, b) } \ + | iv < shp }; \ +} + +#define VEC3D_BIN_SxS(name, op) \ +inline struct VECTOR3 name(struct VECTOR3 a, struct VECTOR3 b) \ +{ \ + return VECTOR3 { op(a.VX, b.VX), op(a.VY, b.VY), op(a.VZ, b.VZ) }; \ +} + +#define VEC3D_BIN_SxX(name, op) \ +inline struct VECTOR3 name(struct VECTOR3 a, REAL b) \ +{ \ + return VECTOR3 { op(a.VX, b), op(a.VY, b), op(a.VZ, b) }; \ +} + +#define VEC3D_BIN(name, op) \ +VEC3D_BIN_AxA(name, op) \ +VEC3D_BIN_AxS(name, op) \ +VEC3D_BIN_AxX(name, op) \ +VEC3D_BIN_SxS(name, op) \ +VEC3D_BIN_SxX(name, op) + +VEC3D_BIN(+, _add_SxS_) +VEC3D_BIN(-, _sub_SxS_) +VEC3D_BIN(*, _mul_SxS_) +VEC3D_BIN(/, _div_SxS_) + +/****************************************************************************** + * + * Unary operations + * + ******************************************************************************/ + +#define VEC3D_UNARY_A(name, op) \ +inline struct VECTOR3[d>0:shp] name(struct VECTOR3[d>0:shp] a) \ +{ \ + return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).VX), \ + op(_sel_VxA_(iv, a).VY), \ + op(_sel_VxA_(iv, a).VZ) } \ + | iv < shp }; \ +} + +#define VEC3D_UNARY_S(name, op) \ +inline struct VECTOR3 name(struct VECTOR3 a) \ +{ \ + return VECTOR3{ op(a.VX), op(a.VY), op(a.VZ) }; \ +} + +#define VEC3D_UNARY(name, op) \ +VEC3D_UNARY_A(name, op) \ +VEC3D_UNARY_S(name, op) + +VEC3D_UNARY(-, _neg_S_) + +/****************************************************************************** + * + * Equality operations + * + ******************************************************************************/ + +#define VEC3D_EQ_AxA(name, op, and_or) \ +inline bool[d>0:shp] name(struct VECTOR3[d>0:shp] a, struct VECTOR3[d>0:shp] b)\ +{ \ + return with { \ + (_mul_SxV_(0, shp) <= iv < shp) : \ + and_or(op(_sel_VxA_(iv, a).VX, _sel_VxA_(iv, b).VX), \ + and_or(op(_sel_VxA_(iv, a).VY, _sel_VxA_(iv, b).VY), \ + op(_sel_VxA_(iv, a).VZ, _sel_VxA_(iv, b).VZ))); \ + }: genarray(shp, true); \ +} + +#define VEC3D_EQ_AxS(name, op, and_or) \ +inline bool[d>0:shp] name(struct VECTOR3[d>0:shp] a, struct VECTOR3 b) \ +{ \ + return with { \ + (_mul_SxV_(0, shp) <= iv < shp) : \ + and_or(op(_sel_VxA_(iv, a).VX, b.VX), \ + and_or(op(_sel_VxA_(iv, a).VY, b.VY), \ + op(_sel_VxA_(iv, a).VZ, b.VZ))); \ + }: genarray(shp, true); \ +} + +#define VEC3D_EQ_AxX(name, op, and_or) \ +inline bool[d>0:shp] name(struct VECTOR3[d>0:shp] a, REAL b) \ +{ \ + return with { \ + (_mul_SxV_(0, shp) <= iv < shp) : \ + and_or(op(_sel_VxA_(iv, a).VX, b), \ + and_or(op(_sel_VxA_(iv, a).VY, b), \ + op(_sel_VxA_(iv, a).VZ, b))); \ + }: genarray(shp, true); \ +} + +#define VEC3D_EQ_SxS(name, op, and_or) \ +inline bool name(struct VECTOR3 a, struct VECTOR3 b) \ +{ \ + return and_or(op(a.VX, b.VX), \ + and_or(op(a.VY, b.VY), \ + op(a.VZ, b.VZ))); \ +} + +#define VEC3D_EQ_SxX(name, op, and_or) \ +inline bool name(struct VECTOR3 a, REAL b) \ +{ \ + return and_or(op(a.VX, b), \ + and_or(op(a.VY, b), \ + op(a.VZ, b))); \ +} + +#define VEC3D_EQ(name, op, and_or) \ +VEC3D_EQ_AxA(name, op, and_or) \ +VEC3D_EQ_AxS(name, op, and_or) \ +VEC3D_EQ_AxX(name, op, and_or) \ +VEC3D_EQ_SxS(name, op, and_or) \ +VEC3D_EQ_SxX(name, op, and_or) + +VEC3D_EQ(==, _eq_SxS_, _and_SxS_) +VEC3D_EQ(!=, _neq_SxS_, _or_SxS_) diff --git a/src/structures/Structures.sac b/src/structures/Structures.sac index dfa0c289..f7e139ba 100644 --- a/src/structures/Structures.sac +++ b/src/structures/Structures.sac @@ -11,6 +11,11 @@ import Color8: all; import Complex: all; import List: all; import Quaternion: all; +/* Importing both currently causes a name clash, because the same C function + * name is being generated for both the double and float variants. + * Until this has been fixed, we must exclude them from this module. */ +//import Vector3d: all; +//import Vector3f: all; #endif export all; diff --git a/src/structures/Vector3d.xsac b/src/structures/Vector3d.xsac new file mode 100644 index 00000000..569a8c75 --- /dev/null +++ b/src/structures/Vector3d.xsac @@ -0,0 +1,7 @@ +#define REAL double +#define VECTOR3 Vector3d +#define VX xd +#define VY yd +#define VZ zd + +#include "Vector3.mac" diff --git a/src/structures/Vector3f.xsac b/src/structures/Vector3f.xsac new file mode 100644 index 00000000..e10563a7 --- /dev/null +++ b/src/structures/Vector3f.xsac @@ -0,0 +1,7 @@ +#define REAL float +#define VECTOR3 Vector3f +#define VX xf +#define VY yf +#define VZ zf + +#include "Vector3.mac"