Skip to content

Commit

Permalink
more matrix constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
elalish committed Oct 12, 2024
1 parent 69b808a commit 71c8c20
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
44 changes: 32 additions & 12 deletions include/manifold/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,11 @@ struct vec<T, 2> {
constexpr explicit vec(const T *p) : vec(p[0], p[1]) {}
template <class U, int N>
constexpr explicit vec(const vec<U, N> &v)
: vec(static_cast<T>(v.x), N < 2 ? 0 : static_cast<T>(v.y)) {}
: vec(static_cast<T>(v.x), static_cast<T>(v.y)) {
static_assert(
N >= 2,
"You must give extra arguments if your input vector is shorter.");
}
constexpr const T &operator[](int i) const { return i == 0 ? x : y; }
LINALG_CONSTEXPR14 T &operator[](int i) { return i == 0 ? x : y; }

Expand All @@ -809,8 +813,11 @@ struct vec<T, 3> {
constexpr explicit vec(const T *p) : vec(p[0], p[1], p[2]) {}
template <class U, int N>
constexpr explicit vec(const vec<U, N> &v)
: vec(static_cast<T>(v.x), N < 2 ? 0 : static_cast<T>(v.y),
N < 3 ? 0 : static_cast<T>(v.z)) {}
: vec(static_cast<T>(v.x), static_cast<T>(v.y), static_cast<T>(v.z)) {
static_assert(
N >= 3,
"You must give extra arguments if your input vector is shorter.");
}
constexpr const T &operator[](int i) const {
return i == 0 ? x : i == 1 ? y : z;
}
Expand Down Expand Up @@ -843,8 +850,12 @@ struct vec<T, 4> {
constexpr explicit vec(const T *p) : vec(p[0], p[1], p[2], p[3]) {}
template <class U, int N>
constexpr explicit vec(const vec<U, N> &v)
: vec(static_cast<T>(v.x), N < 2 ? 0 : static_cast<T>(v.y),
N < 3 ? 0 : static_cast<T>(v.z), N < 4 ? 0 : static_cast<T>(v.w)) {}
: vec(static_cast<T>(v.x), static_cast<T>(v.y), static_cast<T>(v.z),
static_cast<T>(v.w)) {
static_assert(
N >= 4,
"You must give extra arguments if your input vector is shorter.");
}
constexpr const T &operator[](int i) const {
return i == 0 ? x : i == 1 ? y : i == 2 ? z : w;
}
Expand Down Expand Up @@ -899,8 +910,10 @@ struct mat<T, M, 2> {
constexpr mat(const V &x_, const V &y_) : x(x_), y(y_) {}
constexpr explicit mat(const T &s) : x(s), y(s) {}
constexpr explicit mat(const T *p) : x(p + M * 0), y(p + M * 1) {}
template <class U>
constexpr explicit mat(const mat<U, M, 2> &m) : mat(V(m.x), V(m.y)) {}
template <class U, int N, int P>
constexpr explicit mat(const mat<U, N, P> &m) : mat(V(m.x), V(m.y)) {
static_assert(P >= 2, "Input matrix dimensions must be at least as big.");
}
constexpr vec<T, 2> row(int i) const { return {x[i], y[i]}; }
constexpr const V &operator[](int j) const { return j == 0 ? x : y; }
LINALG_CONSTEXPR14 V &operator[](int j) { return j == 0 ? x : y; }
Expand All @@ -921,8 +934,10 @@ struct mat<T, M, 3> {
constexpr explicit mat(const T &s) : x(s), y(s), z(s) {}
constexpr explicit mat(const T *p)
: x(p + M * 0), y(p + M * 1), z(p + M * 2) {}
template <class U>
constexpr explicit mat(const mat<U, M, 3> &m) : mat(V(m.x), V(m.y), V(m.z)) {}
template <class U, int N, int P>
constexpr explicit mat(const mat<U, N, P> &m) : mat(V(m.x), V(m.y), V(m.z)) {
static_assert(P >= 3, "Input matrix dimensions must be at least as big.");
}
constexpr vec<T, 3> row(int i) const { return {x[i], y[i], z[i]}; }
constexpr const V &operator[](int j) const {
return j == 0 ? x : j == 1 ? y : z;
Expand All @@ -945,12 +960,17 @@ struct mat<T, M, 4> {
constexpr mat() : x(), y(), z(), w() {}
constexpr mat(const V &x_, const V &y_, const V &z_, const V &w_)
: x(x_), y(y_), z(z_), w(w_) {}
constexpr mat(const mat<T, M, 3> &m_, const V &w_)
: x(m_.x), y(m_.y), z(m_.z), w(w_) {}
constexpr explicit mat(const T &s) : x(s), y(s), z(s), w(s) {}
constexpr explicit mat(const T *p)
: x(p + M * 0), y(p + M * 1), z(p + M * 2), w(p + M * 3) {}
template <class U>
constexpr explicit mat(const mat<U, M, 4> &m)
: mat(V(m.x), V(m.y), V(m.z), V(m.w)) {}
template <class U, int N, int P>
constexpr explicit mat(const mat<U, N, P> &m)
: mat(V(m.x), V(m.y), V(m.z), V(m.w)) {
static_assert(P >= 4, "Input matrix dimensions must be at least as big.");
}

constexpr vec<T, 4> row(int i) const { return {x[i], y[i], z[i], w[i]}; }
constexpr const V &operator[](int j) const {
return j == 0 ? x : j == 1 ? y : j == 2 ? z : w;
Expand Down
6 changes: 3 additions & 3 deletions src/csg_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ std::shared_ptr<CsgNode> CsgNode::Rotate(double xDegrees, double yDegrees,
mat3 rZ({cosd(zDegrees), sind(zDegrees), 0.0}, //
{-sind(zDegrees), cosd(zDegrees), 0.0}, //
{0.0, 0.0, 1.0});
mat4x3 transform(la::mul(rZ, rY, rX));
mat4x3 transform(rZ * rY * rX, vec3());
return Transform(transform);
}

Expand Down Expand Up @@ -161,7 +161,7 @@ std::shared_ptr<CsgLeafNode> CsgLeafNode::ToLeafNode() const {
}

std::shared_ptr<CsgNode> CsgLeafNode::Transform(const mat4x3 &m) const {
return std::make_shared<CsgLeafNode>(pImpl_, la::mul(m, mat4(transform_)));
return std::make_shared<CsgLeafNode>(pImpl_, m * Mat4(transform_));
}

CsgNodeType CsgLeafNode::GetNodeType() const { return CsgNodeType::Leaf; }
Expand Down Expand Up @@ -392,7 +392,7 @@ std::shared_ptr<CsgNode> CsgOpNode::Boolean(
std::shared_ptr<CsgNode> CsgOpNode::Transform(const mat4x3 &m) const {
auto node = std::make_shared<CsgOpNode>();
node->impl_ = impl_;
node->transform_ = la::mul(m, mat4(transform_));
node->transform_ = m * Mat4(transform_);
node->op_ = op_;
return node;
}
Expand Down
2 changes: 1 addition & 1 deletion src/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ Manifold::Impl Manifold::Impl::Transform(const mat4x3& transform_) const {

result.meshRelation_.originalID = -1;
for (auto& m : result.meshRelation_.meshIDtransform) {
m.second.transform = transform_ * mat4(m.second.transform);
m.second.transform = transform_ * Mat4(m.second.transform);
}

result.vertPos_.resize(NumVert());
Expand Down
10 changes: 5 additions & 5 deletions src/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ struct Manifold::Impl {
meshRelation_.meshIDtransform[meshID] = {originalID};
} else {
const Precision* m = meshGL.runTransform.data() + 12 * i;
meshRelation_.meshIDtransform[meshID] = {
originalID,
{{m[0], m[1], m[2], m[3]},
{m[4], m[5], m[6], m[7]},
{m[8], m[9], m[10], m[11]}}};
meshRelation_.meshIDtransform[meshID] = {originalID,
{{m[0], m[1], m[2]},
{m[3], m[4], m[5]},
{m[6], m[7], m[8]},
{m[9], m[10], m[11]}}};
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/svd.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ struct SVDSet {
* @param A The matrix to decompose.
*/
inline SVDSet SVD(mat3 A) {
mat3 V = JacobiEigenAnalysis(la::mul(la::transpose(A), A));
auto B = la::mul(A, V);
mat3 V = JacobiEigenAnalysis(la::transpose(A) * A);
auto B = A * V;
SortSingularValues(B, V);
QR qr = QRDecomposition(B);
return SVDSet{qr.Q, qr.R, V};
Expand Down
4 changes: 4 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ inline mat4x3 RotateUp(vec3 up) {
return mat4x3(la::rotate(mat4(1), angle, axis));
}

inline mat4 Mat4(mat4x3 a) {
return mat4({a[0], 0}, {a[1], 0}, {a[2], 0}, {a[3], 1});
}

/** @} */

/** @defgroup Debug
Expand Down

0 comments on commit 71c8c20

Please sign in to comment.