Skip to content

Vector3 record #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmake/sac-core-ext.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ structures/ComplexScalarArith.sac Ext
structures/ComplexBasics.sac Ext
structures/Complex.sac Ext
structures/Quaternion.sac Ext
structures/Vector3.sac Ext
structures/Vector3f.sac Ext
structures/Char.sac Core
structures/Bits.sac Core
structures/String.sac Core
Expand Down
2 changes: 2 additions & 0 deletions src/structures/Structures.sac
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import String: all;
#ifdef EXT_STDLIB
import Complex: all;
import Quaternion: all;
import Vector3: all;
import Vector3f: all;
#ifndef SAC_BACKEND_MUTC
import Color8: all;
import List: all;
Expand Down
241 changes: 241 additions & 0 deletions src/structures/Vector3.mac
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
module VECTOR3;

export all;

struct VECTOR3 {
REAL x;
REAL y;
REAL z;
};

inline REAL
l2norm(struct VECTOR3 v)
{
x = _mul_SxS_(v.x, v.x);
y = _mul_SxS_(v.y, v.y);
z = _mul_SxS_(v.z, v.z);
return Math::sqrt(_add_SxS_(_add_SxS_(x, y), z));
}

/******************************************************************************
*
* Binary operations
*
******************************************************************************/

#define VEC3D_BIN_AxA(name, op) \
inline struct VECTOR3[d:shp] \
name(struct VECTOR3[d:shp] a, struct VECTOR3[d:shp] b) \
{ \
return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).x, _sel_VxA_(iv, b).x), \
op(_sel_VxA_(iv, a).y, _sel_VxA_(iv, b).y), \
op(_sel_VxA_(iv, a).z, _sel_VxA_(iv, b).z) } }; \
}

#define VEC3D_BIN_AxS(name, op) \
inline struct VECTOR3[d:shp] \
name(struct VECTOR3[d:shp] a, struct VECTOR3 b) \
{ \
return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).x, b.x), \
op(_sel_VxA_(iv, a).y, b.y), \
op(_sel_VxA_(iv, a).z, b.z) } }; \
}

#define VEC3D_BIN_SxA(name, op) \
inline struct VECTOR3[d:shp] \
name(struct VECTOR3 a, struct VECTOR3[d:shp] b) \
{ \
return { iv -> VECTOR3 { op(a.x, _sel_VxA_(iv, b).x), \
op(a.y, _sel_VxA_(iv, b).y), \
op(a.z, _sel_VxA_(iv, b).z) } }; \
}

#define VEC3D_BIN_AxD(name, op) \
inline struct VECTOR3[d:shp] \
name(struct VECTOR3[d:shp] a, REAL b) \
{ \
return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).x, b), \
op(_sel_VxA_(iv, a).y, b), \
op(_sel_VxA_(iv, a).z, b) } }; \
}

#define VEC3D_BIN_DxA(name, op) \
inline struct VECTOR3[d:shp] \
name(REAL a, struct VECTOR3[d:shp] b) \
{ \
return { iv -> VECTOR3 { op(a, _sel_VxA_(iv, b).x), \
op(a, _sel_VxA_(iv, b).y), \
op(a, _sel_VxA_(iv, b).z) } }; \
}

#define VEC3D_BIN_SxS(name, op) \
inline struct VECTOR3 \
name(struct VECTOR3 a, struct VECTOR3 b) \
{ \
return VECTOR3 { op(a.x, b.x), op(a.y, b.y), op(a.z, b.z) }; \
}

#define VEC3D_BIN_SxD(name, op) \
inline struct VECTOR3 \
name(struct VECTOR3 a, REAL b) \
{ \
return VECTOR3 { op(a.x, b), op(a.y, b), op(a.z, b) }; \
}


#define VEC3D_BIN_DxS(name, op) \
inline struct VECTOR3 \
name(REAL a, struct VECTOR3 b) \
{ \
return VECTOR3 { op(a, b.x), op(a, b.y), op(a, b.z) }; \
}


#define VEC3D_BIN(name, op) \
VEC3D_BIN_AxA(name, op) \
VEC3D_BIN_AxS(name, op) \
VEC3D_BIN_SxA(name, op) \
VEC3D_BIN_AxD(name, op) \
VEC3D_BIN_DxA(name, op) \
VEC3D_BIN_SxS(name, op) \
VEC3D_BIN_SxD(name, op) \
VEC3D_BIN_DxS(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:shp] \
name(struct VECTOR3[d:shp] a) \
{ \
return { iv -> VECTOR3 { op(_sel_VxA_(iv, a).x), \
op(_sel_VxA_(iv, a).y), \
op(_sel_VxA_(iv, a).z) } }; \
}

#define VEC3D_UNARY_S(name, op) \
inline struct VECTOR3 \
name(struct VECTOR3 a) \
{ \
return VECTOR3{ op(a.x), op(a.y), op(a.z) }; \
}

#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:shp] \
name(struct VECTOR3[d:shp] a, struct VECTOR3[d:shp] b) \
{ \
return with { \
(_mul_SxV_(0, shp) <= iv < shp) : \
and_or(op(_sel_VxA_(iv, a).x, _sel_VxA_(iv, b).x), \
and_or(op(_sel_VxA_(iv, a).y, _sel_VxA_(iv, b).y), \
op(_sel_VxA_(iv, a).z, _sel_VxA_(iv, b).z))); \
}: genarray(shp, true); \
}

#define VEC3D_EQ_AxS(name, op, and_or) \
inline bool[d:shp] \
name(struct VECTOR3[d:shp] a, struct VECTOR3 b) \
{ \
return with { \
(_mul_SxV_(0, shp) <= iv < shp) : \
and_or(op(_sel_VxA_(iv, a).x, b.x), \
and_or(op(_sel_VxA_(iv, a).y, b.y), \
op(_sel_VxA_(iv, a).z, b.z))); \
}: genarray(shp, true); \
}

#define VEC3D_EQ_SxA(name, op, and_or) \
inline bool[d:shp] \
name(struct VECTOR3 a, struct VECTOR3[d:shp] b) \
{ \
return with { \
(_mul_SxV_(0, shp) <= iv < shp) : \
and_or(op(a.x, _sel_VxA_(iv, b).x), \
and_or(op(a.y, _sel_VxA_(iv, b).y), \
op(a.z, _sel_VxA_(iv, b).z))); \
}: genarray(shp, true); \
}

#define VEC3D_EQ_AxD(name, op, and_or) \
inline bool[d:shp] \
name(struct VECTOR3[d:shp] a, REAL b) \
{ \
return with { \
(_mul_SxV_(0, shp) <= iv < shp) : \
and_or(op(_sel_VxA_(iv, a).x, b), \
and_or(op(_sel_VxA_(iv, a).y, b), \
op(_sel_VxA_(iv, a).z, b))); \
}: genarray(shp, true); \
}

#define VEC3D_EQ_DxA(name, op, and_or) \
inline bool[d:shp] \
name(REAL a, struct VECTOR3[d:shp] b) \
{ \
return with { \
(_mul_SxV_(0, shp) <= iv < shp) : \
and_or(op(a, _sel_VxA_(iv, b).x), \
and_or(op(a, _sel_VxA_(iv, b).y), \
op(a, _sel_VxA_(iv, b).z))); \
}: 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.x, b.x), \
and_or(op(a.y, b.y), \
op(a.z, b.z))); \
}

#define VEC3D_EQ_SxD(name, op, and_or) \
inline bool \
name(struct VECTOR3 a, REAL b) \
{ \
return and_or(op(a.x, b), \
and_or(op(a.y, b), \
op(a.z, b))); \
}

#define VEC3D_EQ_DxS(name, op, and_or) \
inline bool \
name(REAL a, struct VECTOR3 b) \
{ \
return and_or(op(a, b.x), \
and_or(op(a, b.y), \
op(a, b.z))); \
}

#define VEC3D_EQ(name, op, and_or) \
VEC3D_EQ_AxA(name, op, and_or) \
VEC3D_EQ_AxS(name, op, and_or) \
VEC3D_EQ_SxA(name, op, and_or) \
VEC3D_EQ_AxD(name, op, and_or) \
VEC3D_EQ_DxA(name, op, and_or) \
VEC3D_EQ_SxS(name, op, and_or) \
VEC3D_EQ_SxD(name, op, and_or) \
VEC3D_EQ_DxS(name, op, and_or)

VEC3D_EQ(==, _eq_SxS_, _and_SxS_)
VEC3D_EQ(!=, _neq_SxS_, _or_SxS_)
9 changes: 9 additions & 0 deletions src/structures/Vector3.sac
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#define VECTOR3 Vector3
#define REAL double
#define ZERO 0d

#include "Vector3.mac"

#undef VECTOR3
#undef REAL
#undef ZERO
9 changes: 9 additions & 0 deletions src/structures/Vector3f.sac
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#define VECTOR3 Vector3f
#define REAL float
#define ZERO 0f

#include "Vector3.mac"

#undef VECTOR3
#undef REAL
#undef ZERO
Loading