Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/orbweaver/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	libs/math/Vector3.h
#	test/math/Matrix4.cpp
  • Loading branch information
codereader committed Apr 10, 2021
2 parents 356dfb4 + 639f8ea commit fdee766
Show file tree
Hide file tree
Showing 45 changed files with 965 additions and 882 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
@@ -0,0 +1,4 @@
[submodule "external/eigen"]
path = external/eigen
url = https://gitlab.com/libeigen/eigen.git
shallow = true
5 changes: 4 additions & 1 deletion CMakeLists.txt
Expand Up @@ -7,6 +7,9 @@ project(darkradiant VERSION 2.11.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# Global compiler options and warnings (TODO: may need updating for CLang)
add_compile_options(-Werror=return-local-addr)

# GCC 8 and earlier require explicit linking against stdc++fs
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
link_libraries(stdc++fs)
Expand Down Expand Up @@ -75,7 +78,7 @@ include(${wxWidgets_USE_FILE})
find_package(Python REQUIRED COMPONENTS Development)

# Global includes and flags
include_directories(libs libs/libfmt include)
include_directories(libs libs/libfmt external/eigen include)
add_compile_definitions(POSIX
WXINTL_NO_GETTEXT_MACRO
FMT_HEADER_ONLY
Expand Down
1 change: 1 addition & 0 deletions external/eigen
Submodule eigen added at 0fd6b4
2 changes: 1 addition & 1 deletion include/itransformnode.h
Expand Up @@ -11,7 +11,7 @@ class ITransformNode
virtual ~ITransformNode() {}

/// \brief Returns the transform which maps the node's local-space into the local-space of its parent node.
virtual const Matrix4& localToParent() const = 0;
virtual Matrix4 localToParent() const = 0;
};
typedef std::shared_ptr<ITransformNode> ITransformNodePtr;

Expand Down
Binary file modified install/bitmaps/select_mouserotate.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified install/bitmaps/select_mousetranslate.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified install/bitmaps/view_clipper.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions install/user.xml
Expand Up @@ -263,8 +263,8 @@
<toggletoolbutton name="translate" action="MouseTranslate" tooltip="Translate" icon="select_mousetranslate.png"/>
<toggletoolbutton name="rotate" action="MouseRotate" tooltip="Rotate" icon="select_mouserotate.png"/>
<toggletoolbutton name="resize" action="MouseDrag" tooltip="Resize" icon="select_mouseresize.png"/>
<toggletoolbutton name="clipper" action="ToggleClipper" tooltip="Clipper" icon="view_clipper.png"/>
<toggletoolbutton name="modelscaler" action="ToggleModelScaleManipulator" tooltip="Model Scaler" icon="select_mousescale.png"/>
<toggletoolbutton name="clipper" action="ToggleClipper" tooltip="Clip or divide brushes" icon="view_clipper.png"/>
<toggletoolbutton name="modelscaler" action="ToggleModelScaleManipulator" tooltip="Model Scaler" icon="select_mousescale.png"/>
<separator/>
<toolbutton name="csgsubstract" action="CSGSubtract" tooltip="CSG Subtract" icon="selection_csgsubtract.png"/>
<toolbutton name="csgmerge" action="CSGMerge" tooltip="CSG Merge" icon="selection_csgmerge.png"/>
Expand Down
173 changes: 1 addition & 172 deletions libs/math/Matrix4.cpp
Expand Up @@ -48,34 +48,11 @@ Matrix4::Matrix4(double xx_, double xy_, double xz_, double xw_,

// Named constructors

// Identity matrix
const Matrix4& Matrix4::getIdentity()
{
static const Matrix4 _identity(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
return _identity;
}

// Get a translation matrix for the given vector
Matrix4 Matrix4::getTranslation(const Vector3& translation)
{
return Matrix4::byColumns(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
translation.x(), translation.y(), translation.z(), 1
);
}

// Get a rotation from 2 vectors (named constructor)
Matrix4 Matrix4::getRotation(const Vector3& a, const Vector3& b)
{
double angle = a.angle(b);
Vector3 axis = b.crossProduct(a).getNormalised();
Vector3 axis = b.cross(a).getNormalised();

return getRotation(axis, angle);
}
Expand Down Expand Up @@ -238,154 +215,6 @@ Matrix4 Matrix4::getRotationForEulerXYZDegrees(const Vector3& euler)
return getRotationForEulerXYZ(euler_degrees_to_radians(euler));
}

// Get a scale matrix
Matrix4 Matrix4::getScale(const Vector3& scale)
{
return Matrix4::byColumns(
scale[0], 0, 0, 0,
0, scale[1], 0, 0,
0, 0, scale[2], 0,
0, 0, 0, 1
);
}

// Transpose the matrix in-place
void Matrix4::transpose()
{
std::swap(_m[1], _m[4]); // xy <=> yx
std::swap(_m[2], _m[8]); // xz <=> zx
std::swap(_m[3], _m[12]); // xw <=> tx
std::swap(_m[6], _m[9]); // yz <=> zy
std::swap(_m[7], _m[13]); // yw <=> ty
std::swap(_m[11], _m[14]); // zw <=> tz
}

// Return transposed copy
Matrix4 Matrix4::getTransposed() const
{
return Matrix4(
xx(), yx(), zx(), tx(),
xy(), yy(), zy(), ty(),
xz(), yz(), zz(), tz(),
xw(), yw(), zw(), tw()
);
}

// Return affine inverse
Matrix4 Matrix4::getInverse() const
{
Matrix4 result;

// determinant of rotation submatrix
double det
= _m[0] * ( _m[5]*_m[10] - _m[9]*_m[6] )
- _m[1] * ( _m[4]*_m[10] - _m[8]*_m[6] )
+ _m[2] * ( _m[4]*_m[9] - _m[8]*_m[5] );

// throw exception here if (det*det < 1e-25)

// invert rotation submatrix
det = 1.0f / det;

result[0] = ( (_m[5]*_m[10]- _m[6]*_m[9] )*det);
result[1] = (- (_m[1]*_m[10]- _m[2]*_m[9] )*det);
result[2] = ( (_m[1]*_m[6] - _m[2]*_m[5] )*det);
result[3] = 0;
result[4] = (- (_m[4]*_m[10]- _m[6]*_m[8] )*det);
result[5] = ( (_m[0]*_m[10]- _m[2]*_m[8] )*det);
result[6] = (- (_m[0]*_m[6] - _m[2]*_m[4] )*det);
result[7] = 0;
result[8] = ( (_m[4]*_m[9] - _m[5]*_m[8] )*det);
result[9] = (- (_m[0]*_m[9] - _m[1]*_m[8] )*det);
result[10]= ( (_m[0]*_m[5] - _m[1]*_m[4] )*det);
result[11] = 0;

// multiply translation part by rotation
result[12] = - (_m[12] * result[0] +
_m[13] * result[4] +
_m[14] * result[8]);
result[13] = - (_m[12] * result[1] +
_m[13] * result[5] +
_m[14] * result[9]);
result[14] = - (_m[12] * result[2] +
_m[13] * result[6] +
_m[14] * result[10]);
result[15] = 1;

return result;
}

Matrix4 Matrix4::getFullInverse() const
{
// The inverse is generated through the adjugate matrix

// 2x2 minors (re-usable for the determinant)
double minor01 = zz() * tw() - zw() * tz();
double minor02 = zy() * tw() - zw() * ty();
double minor03 = zx() * tw() - zw() * tx();
double minor04 = zy() * tz() - zz() * ty();
double minor05 = zx() * tz() - zz() * tx();
double minor06 = zx() * ty() - zy() * tx();

// 2x2 minors (not usable for the determinant)
double minor07 = yz() * tw() - yw() * tz();
double minor08 = yy() * tw() - yw() * ty();
double minor09 = yy() * tz() - yz() * ty();
double minor10 = yx() * tw() - yw() * tx();
double minor11 = yx() * tz() - yz() * tx();
double minor12 = yx() * ty() - yy() * tx();
double minor13 = yz() * zw() - yw() * zz();
double minor14 = yy() * zw() - yw() * zy();
double minor15 = yy() * zz() - yz() * zy();
double minor16 = yx() * zw() - yw() * zx();
double minor17 = yx() * zz() - yz() * zx();
double minor18 = yx() * zy() - yy() * zx();

// 3x3 minors (re-usable for the determinant)
double minor3x3_11 = yy() * minor01 - yz() * minor02 + yw() * minor04;
double minor3x3_21 = yx() * minor01 - yz() * minor03 + yw() * minor05;
double minor3x3_31 = yx() * minor02 - yy() * minor03 + yw() * minor06;
double minor3x3_41 = yx() * minor04 - yy() * minor05 + yz() * minor06;

// 3x3 minors (not usable for the determinant)
double minor3x3_12 = xy() * minor01 - xz() * minor02 + xw() * minor04;
double minor3x3_22 = xx() * minor01 - xz() * minor03 + xw() * minor05;
double minor3x3_32 = xx() * minor02 - xy() * minor03 + xw() * minor06;
double minor3x3_42 = xx() * minor04 - xy() * minor05 + xz() * minor06;

double minor3x3_13 = xy() * minor07 - xz() * minor08 + xw() * minor09;
double minor3x3_23 = xx() * minor07 - xz() * minor10 + xw() * minor11;
double minor3x3_33 = xx() * minor08 - xy() * minor10 + xw() * minor12;
double minor3x3_43 = xx() * minor09 - xy() * minor11 + xz() * minor12;

double minor3x3_14 = xy() * minor13 - xz() * minor14 + xw() * minor15;
double minor3x3_24 = xx() * minor13 - xz() * minor16 + xw() * minor17;
double minor3x3_34 = xx() * minor14 - xy() * minor16 + xw() * minor18;
double minor3x3_44 = xx() * minor15 - xy() * minor17 + xz() * minor18;

double determinant = xx() * minor3x3_11 - xy() * minor3x3_21 + xz() * minor3x3_31 - xw() * minor3x3_41;
double invDet = 1.0f / determinant;

return Matrix4::byColumns(
+minor3x3_11 * invDet, -minor3x3_12 * invDet, +minor3x3_13 * invDet, -minor3x3_14 * invDet,
-minor3x3_21 * invDet, +minor3x3_22 * invDet, -minor3x3_23 * invDet, +minor3x3_24 * invDet,
+minor3x3_31 * invDet, -minor3x3_32 * invDet, +minor3x3_33 * invDet, -minor3x3_34 * invDet,
-minor3x3_41 * invDet, +minor3x3_42 * invDet, -minor3x3_43 * invDet, +minor3x3_44 * invDet
);
}

// Multiply by another matrix, in-place
void Matrix4::multiplyBy(const Matrix4& other)
{
*this = getMultipliedBy(other);
}

// Add a translation component
void Matrix4::translateBy(const Vector3& translation)
{
multiplyBy(getTranslation(translation));
}

// Add a scale component
void Matrix4::scaleBy(const Vector3& scale)
{
Expand Down

0 comments on commit fdee766

Please sign in to comment.