Loading CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ target_sources(csg PRIVATE src/csg/impl/levelset_3d.cpp src/csg/impl/levelset_2d.cpp src/csg/impl/csg.cpp src/csg/impl/matrix_functions.cpp ) target_include_directories(csg PRIVATE Loading src/csg/impl/csg_types.hpp +36 −2 Original line number Diff line number Diff line #ifndef CSG_TYPES_H_ #define CSG_TYPES_H_ #include "matrix_functions.hpp" #include <array> #include <memory> #include <optional> Loading Loading @@ -86,15 +88,47 @@ template <Dimension dim> struct Difference { }; template <> struct Mulmatrix<Dimension::D3> { std::array<std::array<double, 3>, 3> rotation; private: matrix::Mat3d m_rotation; matrix::Mat3d m_rotation_inv; public: std::array<double, 3> translation; Union<Dimension::D3> group; Mulmatrix(const std::array<double, 3> &rot_row0, const std::array<double, 3> &rot_row1, const std::array<double, 3> &rot_row2) { m_rotation[0] = rot_row0; m_rotation[1] = rot_row1; m_rotation[2] = rot_row2; m_rotation_inv = matrix::inverse(m_rotation); } const matrix::Mat3d &rotation() const { return m_rotation; } const matrix::Mat3d &rotation_inv() const { return m_rotation_inv; } }; template <> struct Mulmatrix<Dimension::D2> { std::array<std::array<double, 2>, 2> rotation; private: matrix::Mat2d m_rotation; matrix::Mat2d m_rotation_inv; public: std::array<double, 2> translation; Union<Dimension::D2> group; Mulmatrix(const std::array<double, 2> &rot_row0, const std::array<double, 2> &rot_row1) { m_rotation[0] = rot_row0; m_rotation[1] = rot_row1; m_rotation_inv = matrix::inverse(m_rotation); } const matrix::Mat2d &rotation() const { return m_rotation; } const matrix::Mat2d &rotation_inv() const { return m_rotation_inv; } }; struct LinearExtrude { Loading src/csg/impl/levelset_2d.cpp +3 −4 Original line number Diff line number Diff line Loading @@ -55,12 +55,11 @@ double signed_distance_2d(const Difference2D &group, double xx, double yy) { } double signed_distance_2d(const Mulmatrix2D &mm, double xx, double yy) { // TODO: Invert non-orthogonal matrices auto XX = xx - mm.translation[0]; auto YY = yy - mm.translation[1]; return signed_distance_2d(mm.group, mm.rotation[0][0] * XX + mm.rotation[1][0] * YY, mm.rotation[0][1] * XX + mm.rotation[1][1] * YY); auto ri = mm.rotation_inv(); return signed_distance_2d(mm.group, ri[0][0] * XX + ri[0][1] * YY, ri[1][0] * XX + ri[1][1] * YY); } double signed_distance_2d(const Square &sq, double xx, double yy) { Loading src/csg/impl/levelset_3d.cpp +5 −6 Original line number Diff line number Diff line Loading @@ -64,15 +64,14 @@ double signed_distance_3d(const Difference3D &group, double xx, double yy, double signed_distance_3d(const Mulmatrix3D &mm, double xx, double yy, double zz) { // TODO: Invert non-orthogonal matrices auto XX = xx - mm.translation[0]; auto YY = yy - mm.translation[1]; auto ZZ = zz - mm.translation[2]; return signed_distance_3d( mm.group, mm.rotation[0][0] * XX + mm.rotation[1][0] * YY + mm.rotation[2][0] * ZZ, mm.rotation[0][1] * XX + mm.rotation[1][1] * YY + mm.rotation[2][1] * ZZ, mm.rotation[0][2] * XX + mm.rotation[1][2] * YY + mm.rotation[2][2] * ZZ); auto ri = mm.rotation_inv(); return signed_distance_3d(mm.group, ri[0][0] * XX + ri[0][1] * YY + ri[0][2] * ZZ, ri[1][0] * XX + ri[1][1] * YY + ri[1][2] * ZZ, ri[2][0] * XX + ri[2][1] * YY + ri[2][2] * ZZ); } double signed_distance_3d(const Cone &cone, double xx, double yy, double zz) { Loading src/csg/impl/matrix_functions.cpp 0 → 100644 +47 −0 Original line number Diff line number Diff line #include "matrix_functions.hpp" namespace { double determinant(const matrix::Mat2d &m) { return m[0][0] * m[1][1] - m[1][0] * m[1][1]; } double determinant(const matrix::Mat3d &m) { return m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]) + m[0][1] * (m[1][2] * m[2][0] - m[2][2] * m[1][0]) + m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]); } } // namespace namespace matrix { Mat2d inverse(const Mat2d &m) { Mat2d res; double d = 1 / determinant(m); res[0][0] = d * m[1][1]; res[0][1] = -1.0 * d * m[0][1]; res[1][0] = -1.0 * d * m[1][0]; res[1][1] = d * m[0][0]; return res; } Mat3d inverse(const Mat3d &m) { Mat3d res; double d = 1 / determinant(m); res[0][0] = d * (m[1][1] * m[2][2] - m[2][1] * m[1][2]); res[0][1] = d * (m[0][2] * m[2][1] - m[0][1] * m[2][2]); res[0][2] = d * (m[0][1] * m[1][2] - m[0][2] * m[1][1]); res[1][0] = d * (m[1][2] * m[2][0] - m[1][0] * m[2][2]); res[1][1] = d * (m[0][0] * m[2][2] - m[0][2] * m[2][0]); res[1][2] = d * (m[1][0] * m[0][2] - m[0][0] * m[1][2]); res[2][0] = d * (m[1][0] * m[2][1] - m[2][0] * m[1][1]); res[2][1] = d * (m[2][0] * m[0][1] - m[0][0] * m[2][1]); res[2][2] = d * (m[0][0] * m[1][1] - m[1][0] * m[0][1]); return res; } } // namespace matrix Loading
CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ target_sources(csg PRIVATE src/csg/impl/levelset_3d.cpp src/csg/impl/levelset_2d.cpp src/csg/impl/csg.cpp src/csg/impl/matrix_functions.cpp ) target_include_directories(csg PRIVATE Loading
src/csg/impl/csg_types.hpp +36 −2 Original line number Diff line number Diff line #ifndef CSG_TYPES_H_ #define CSG_TYPES_H_ #include "matrix_functions.hpp" #include <array> #include <memory> #include <optional> Loading Loading @@ -86,15 +88,47 @@ template <Dimension dim> struct Difference { }; template <> struct Mulmatrix<Dimension::D3> { std::array<std::array<double, 3>, 3> rotation; private: matrix::Mat3d m_rotation; matrix::Mat3d m_rotation_inv; public: std::array<double, 3> translation; Union<Dimension::D3> group; Mulmatrix(const std::array<double, 3> &rot_row0, const std::array<double, 3> &rot_row1, const std::array<double, 3> &rot_row2) { m_rotation[0] = rot_row0; m_rotation[1] = rot_row1; m_rotation[2] = rot_row2; m_rotation_inv = matrix::inverse(m_rotation); } const matrix::Mat3d &rotation() const { return m_rotation; } const matrix::Mat3d &rotation_inv() const { return m_rotation_inv; } }; template <> struct Mulmatrix<Dimension::D2> { std::array<std::array<double, 2>, 2> rotation; private: matrix::Mat2d m_rotation; matrix::Mat2d m_rotation_inv; public: std::array<double, 2> translation; Union<Dimension::D2> group; Mulmatrix(const std::array<double, 2> &rot_row0, const std::array<double, 2> &rot_row1) { m_rotation[0] = rot_row0; m_rotation[1] = rot_row1; m_rotation_inv = matrix::inverse(m_rotation); } const matrix::Mat2d &rotation() const { return m_rotation; } const matrix::Mat2d &rotation_inv() const { return m_rotation_inv; } }; struct LinearExtrude { Loading
src/csg/impl/levelset_2d.cpp +3 −4 Original line number Diff line number Diff line Loading @@ -55,12 +55,11 @@ double signed_distance_2d(const Difference2D &group, double xx, double yy) { } double signed_distance_2d(const Mulmatrix2D &mm, double xx, double yy) { // TODO: Invert non-orthogonal matrices auto XX = xx - mm.translation[0]; auto YY = yy - mm.translation[1]; return signed_distance_2d(mm.group, mm.rotation[0][0] * XX + mm.rotation[1][0] * YY, mm.rotation[0][1] * XX + mm.rotation[1][1] * YY); auto ri = mm.rotation_inv(); return signed_distance_2d(mm.group, ri[0][0] * XX + ri[0][1] * YY, ri[1][0] * XX + ri[1][1] * YY); } double signed_distance_2d(const Square &sq, double xx, double yy) { Loading
src/csg/impl/levelset_3d.cpp +5 −6 Original line number Diff line number Diff line Loading @@ -64,15 +64,14 @@ double signed_distance_3d(const Difference3D &group, double xx, double yy, double signed_distance_3d(const Mulmatrix3D &mm, double xx, double yy, double zz) { // TODO: Invert non-orthogonal matrices auto XX = xx - mm.translation[0]; auto YY = yy - mm.translation[1]; auto ZZ = zz - mm.translation[2]; return signed_distance_3d( mm.group, mm.rotation[0][0] * XX + mm.rotation[1][0] * YY + mm.rotation[2][0] * ZZ, mm.rotation[0][1] * XX + mm.rotation[1][1] * YY + mm.rotation[2][1] * ZZ, mm.rotation[0][2] * XX + mm.rotation[1][2] * YY + mm.rotation[2][2] * ZZ); auto ri = mm.rotation_inv(); return signed_distance_3d(mm.group, ri[0][0] * XX + ri[0][1] * YY + ri[0][2] * ZZ, ri[1][0] * XX + ri[1][1] * YY + ri[1][2] * ZZ, ri[2][0] * XX + ri[2][1] * YY + ri[2][2] * ZZ); } double signed_distance_3d(const Cone &cone, double xx, double yy, double zz) { Loading
src/csg/impl/matrix_functions.cpp 0 → 100644 +47 −0 Original line number Diff line number Diff line #include "matrix_functions.hpp" namespace { double determinant(const matrix::Mat2d &m) { return m[0][0] * m[1][1] - m[1][0] * m[1][1]; } double determinant(const matrix::Mat3d &m) { return m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]) + m[0][1] * (m[1][2] * m[2][0] - m[2][2] * m[1][0]) + m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]); } } // namespace namespace matrix { Mat2d inverse(const Mat2d &m) { Mat2d res; double d = 1 / determinant(m); res[0][0] = d * m[1][1]; res[0][1] = -1.0 * d * m[0][1]; res[1][0] = -1.0 * d * m[1][0]; res[1][1] = d * m[0][0]; return res; } Mat3d inverse(const Mat3d &m) { Mat3d res; double d = 1 / determinant(m); res[0][0] = d * (m[1][1] * m[2][2] - m[2][1] * m[1][2]); res[0][1] = d * (m[0][2] * m[2][1] - m[0][1] * m[2][2]); res[0][2] = d * (m[0][1] * m[1][2] - m[0][2] * m[1][1]); res[1][0] = d * (m[1][2] * m[2][0] - m[1][0] * m[2][2]); res[1][1] = d * (m[0][0] * m[2][2] - m[0][2] * m[2][0]); res[1][2] = d * (m[1][0] * m[0][2] - m[0][0] * m[1][2]); res[2][0] = d * (m[1][0] * m[2][1] - m[2][0] * m[1][1]); res[2][1] = d * (m[2][0] * m[0][1] - m[0][0] * m[2][1]); res[2][2] = d * (m[0][0] * m[1][1] - m[1][0] * m[0][1]); return res; } } // namespace matrix