Skip to content

Commit

Permalink
Matrix4 column accessors return Vector3 not Vector4
Browse files Browse the repository at this point in the history
Every single use of xCol(), yCol() or zCol() was immediately followed by
getVector3() to retrieve the first three components. These methods are now
renamed xCol3(), yCol3() and zCol3(), and return a Vector3 directly.

tCol() is unchanged since there is calling code which uses getProjected()
instead of getVector3().
  • Loading branch information
Matthew Mott committed Sep 22, 2021
1 parent b0d562b commit 1a00a72
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 88 deletions.
6 changes: 3 additions & 3 deletions libs/Transformable.h
Expand Up @@ -195,9 +195,9 @@ class Transformable :
static Matrix4 getMatrixForComponents(const Vector3& translation, const Quaternion& rotation, const Vector3& scale)
{
Matrix4 result(Matrix4::getRotationQuantised(rotation));
result.xCol().getVector3() *= scale.x();
result.yCol().getVector3() *= scale.y();
result.zCol().getVector3() *= scale.z();
result.xCol3() *= scale.x();
result.yCol3() *= scale.y();
result.zCol3() *= scale.z();
result.tx() = translation.x();
result.ty() = translation.y();
result.tz() = translation.z();
Expand Down
32 changes: 16 additions & 16 deletions libs/math/AABB.cpp
Expand Up @@ -121,9 +121,9 @@ unsigned int AABB::classifyOrientedPlane(const Matrix4& transform, const Plane3&
{
double distance_origin = plane.normal().dot(origin) + plane.dist();

if (fabs(distance_origin) < (fabs(extents[0] * plane.normal().dot(transform.xCol().getVector3())) +
fabs(extents[1] * plane.normal().dot(transform.yCol().getVector3())) +
fabs(extents[2] * plane.normal().dot(transform.zCol().getVector3()))))
if (fabs(distance_origin) < (fabs(extents[0] * plane.normal().dot(transform.xCol3())) +
fabs(extents[1] * plane.normal().dot(transform.yCol3())) +
fabs(extents[2] * plane.normal().dot(transform.zCol3()))))
{
return 1; // partially inside
}
Expand All @@ -137,9 +137,9 @@ unsigned int AABB::classifyOrientedPlane(const Matrix4& transform, const Plane3&

void AABB::getCorners(Vector3 corners[8], const Matrix4& rotation) const
{
Vector3 x = rotation.xCol().getVector3() * extents.x();
Vector3 y = rotation.yCol().getVector3() * extents.y();
Vector3 z = rotation.zCol().getVector3() * extents.z();
Vector3 x = rotation.xCol3() * extents.x();
Vector3 y = rotation.yCol3() * extents.y();
Vector3 z = rotation.zCol3() * extents.z();

corners[0] = origin - x + y + z;
corners[1] = origin + x + y + z;
Expand All @@ -153,16 +153,16 @@ void AABB::getCorners(Vector3 corners[8], const Matrix4& rotation) const

void AABB::getPlanes(Plane3 planes[6], const Matrix4& rotation) const
{
double x = rotation.xCol().getVector3().dot(origin);
double y = rotation.yCol().getVector3().dot(origin);
double z = rotation.zCol().getVector3().dot(origin);

planes[0] = Plane3( rotation.xCol().getVector3(), x + extents[0]);
planes[1] = Plane3(-rotation.xCol().getVector3(), -(x - extents[0]));
planes[2] = Plane3( rotation.yCol().getVector3(), y + extents[1]);
planes[3] = Plane3(-rotation.yCol().getVector3(), -(y - extents[1]));
planes[4] = Plane3( rotation.zCol().getVector3(), z + extents[2]);
planes[5] = Plane3(-rotation.zCol().getVector3(), -(z - extents[2]));
double x = rotation.xCol3().dot(origin);
double y = rotation.yCol3().dot(origin);
double z = rotation.zCol3().dot(origin);

planes[0] = Plane3( rotation.xCol3(), x + extents[0]);
planes[1] = Plane3(-rotation.xCol3(), -(x - extents[0]));
planes[2] = Plane3( rotation.yCol3(), y + extents[1]);
planes[3] = Plane3(-rotation.yCol3(), -(y - extents[1]));
planes[4] = Plane3( rotation.zCol3(), z + extents[2]);
planes[5] = Plane3(-rotation.zCol3(), -(z - extents[2]));
}

AABB AABB::createFromOrientedAABB(const AABB& aabb, const Matrix4& transform)
Expand Down
32 changes: 16 additions & 16 deletions libs/math/Matrix4.h
Expand Up @@ -211,29 +211,29 @@ class alignas(16) Matrix4
* Return columns of the matrix as vectors.
* \{
*/
Vector4& xCol()
Vector3& xCol3()
{
return reinterpret_cast<Vector4&>(xx());
return reinterpret_cast<Vector3&>(xx());
}
const Vector4& xCol() const
const Vector3& xCol3() const
{
return reinterpret_cast<const Vector4&>(xx());
return reinterpret_cast<const Vector3&>(xx());
}
Vector4& yCol()
Vector3& yCol3()
{
return reinterpret_cast<Vector4&>(yx());
return reinterpret_cast<Vector3&>(yx());
}
const Vector4& yCol() const
const Vector3& yCol3() const
{
return reinterpret_cast<const Vector4&>(yx());
return reinterpret_cast<const Vector3&>(yx());
}
Vector4& zCol()
Vector3& zCol3()
{
return reinterpret_cast<Vector4&>(zx());
return reinterpret_cast<Vector3&>(zx());
}
const Vector4& zCol() const
const Vector3& zCol3() const
{
return reinterpret_cast<const Vector4&>(zx());
return reinterpret_cast<const Vector3&>(zx());
}
Vector4& tCol()
{
Expand Down Expand Up @@ -526,7 +526,7 @@ inline bool operator!=(const Matrix4& l, const Matrix4& r)

inline Matrix4::Handedness Matrix4::getHandedness() const
{
return (xCol().getVector3().cross(yCol().getVector3()).dot(zCol().getVector3()) < 0.0f) ? LEFTHANDED : RIGHTHANDED;
return (xCol3().cross(yCol3()).dot(zCol3()) < 0.0f) ? LEFTHANDED : RIGHTHANDED;
}

template<typename T>
Expand Down Expand Up @@ -569,9 +569,9 @@ inline Vector3 Matrix4::getEulerAnglesXYZDegrees() const
inline Vector3 Matrix4::getScale() const
{
return Vector3(
xCol().getVector3().getLength(),
yCol().getVector3().getLength(),
zCol().getVector3().getLength()
xCol3().getLength(),
yCol3().getLength(),
zCol3().getLength()
);
}

Expand Down
6 changes: 3 additions & 3 deletions libs/math/Plane3.cpp
Expand Up @@ -5,9 +5,9 @@

double Plane3::distanceToOrientedExtents(const Vector3& extents, const Matrix4& orientation) const
{
return fabs(extents[0] * normal().dot(orientation.xCol().getVector3())) +
fabs(extents[1] * normal().dot(orientation.yCol().getVector3())) +
fabs(extents[2] * normal().dot(orientation.zCol().getVector3()));
return fabs(extents[0] * normal().dot(orientation.xCol3())) +
fabs(extents[1] * normal().dot(orientation.yCol3())) +
fabs(extents[2] * normal().dot(orientation.zCol3()));
}

bool Plane3::containsAABB(const AABB& aabb, const Matrix4& orientation) const
Expand Down
22 changes: 11 additions & 11 deletions libs/pivot.h
Expand Up @@ -9,13 +9,13 @@ inline void billboard_viewplaneOriented(Matrix4& rotation, const Matrix4& world2
{
#if 1
rotation = Matrix4::getIdentity();
Vector3 x(world2screen.xCol().getVector3().getNormalised());
Vector3 y(world2screen.yCol().getVector3().getNormalised());
Vector3 z(world2screen.zCol().getVector3().getNormalised());
rotation.yCol().getVector3() = Vector3(x.y(), y.y(), z.y());
rotation.zCol().getVector3() = -Vector3(x.z(), y.z(), z.z());
rotation.xCol().getVector3() = rotation.yCol().getVector3().cross(rotation.zCol().getVector3()).getNormalised();
rotation.yCol().getVector3() = rotation.zCol().getVector3().cross(rotation.xCol().getVector3());
Vector3 x(world2screen.xCol3().getNormalised());
Vector3 y(world2screen.yCol3().getNormalised());
Vector3 z(world2screen.zCol3().getNormalised());
rotation.yCol3() = Vector3(x.y(), y.y(), z.y());
rotation.zCol3() = -Vector3(x.z(), y.z(), z.z());
rotation.xCol3() = rotation.yCol3().cross(rotation.zCol3()).getNormalised();
rotation.yCol3() = rotation.zCol3().cross(rotation.xCol3());
#else
Matrix4 screen2world(matrix4_full_inverse(world2screen));

Expand Down Expand Up @@ -54,10 +54,10 @@ inline void billboard_viewpointOriented(Matrix4& rotation, const Matrix4& world2

#if 1
rotation = Matrix4::getIdentity();
rotation.yCol().getVector3() = screen2world.yCol().getVector3().getNormalised();
rotation.zCol().getVector3() = -screen2world.zCol().getVector3().getNormalised();
rotation.xCol().getVector3() = rotation.yCol().getVector3().cross(rotation.zCol().getVector3()).getNormalised();
rotation.yCol().getVector3() = rotation.zCol().getVector3().cross(rotation.xCol().getVector3());
rotation.yCol3() = screen2world.yCol3().getNormalised();
rotation.zCol3() = -screen2world.zCol3().getNormalised();
rotation.xCol3() = rotation.yCol3().cross(rotation.zCol3()).getNormalised();
rotation.yCol3() = rotation.zCol3().cross(rotation.xCol3());
#else
Vector3 near_(
matrix4_transformed_vector4(
Expand Down
2 changes: 1 addition & 1 deletion libs/selection/Pivot2World.h
Expand Up @@ -45,7 +45,7 @@ class Pivot2World
_viewpointSpace.multiplyBy(scale);

billboard_viewpointOriented(scale, pivot2screen);
_axisScreen = scale.zCol().getVector3();
_axisScreen = scale.zCol3();
_viewpointSpace.multiplyBy(scale);

scale = getPerspectiveScale(pivot2screen);
Expand Down
12 changes: 6 additions & 6 deletions radiantcore/brush/TextureProjection.cpp
Expand Up @@ -90,8 +90,8 @@ Matrix4 TextureProjection::getBasisForNormal(const Vector3& normal) const {
Matrix4 basis;

basis = Matrix4::getIdentity();
ComputeAxisBase(normal, basis.xCol().getVector3(), basis.yCol().getVector3());
basis.zCol().getVector3() = normal;
ComputeAxisBase(normal, basis.xCol3(), basis.yCol3());
basis.zCol3() = normal;

// At this point the basis matrix contains three lines that are
// perpendicular to each other.
Expand Down Expand Up @@ -132,10 +132,10 @@ void TextureProjection::transformLocked(std::size_t width, std::size_t height, c
);

Vector3 originalProjectionAxis(
identity2stIdentity.getInverse().zCol().getVector3()
identity2stIdentity.getInverse().zCol3()
);

Vector3 transformedProjectionAxis(stTransformed2identity.zCol().getVector3());
Vector3 transformedProjectionAxis(stTransformed2identity.zCol3());

Matrix4 stIdentity2stOriginal = getTransform();
Matrix4 identity2stOriginal = stIdentity2stOriginal.getMultipliedBy(identity2stIdentity);
Expand Down Expand Up @@ -375,8 +375,8 @@ void TextureProjection::emitTextureCoordinates(Winding& w, const Vector3& normal
}

// Calculate the tangent and bitangent vectors to allow the correct openGL transformations
Vector3 tangent(local2tex.getTransposed().xCol().getVector3().getNormalised());
Vector3 bitangent(local2tex.getTransposed().yCol().getVector3().getNormalised());
Vector3 tangent(local2tex.getTransposed().xCol3().getNormalised());
Vector3 bitangent(local2tex.getTransposed().yCol3().getNormalised());

// Transform the texture basis vectors into the "BrushFace space"
// usually the localToWorld matrix is identity, so this doesn't do anything.
Expand Down
6 changes: 3 additions & 3 deletions radiantcore/particles/RenderableParticleBunch.cpp
Expand Up @@ -197,7 +197,7 @@ Matrix4 RenderableParticleBunch::getAimedMatrix(const Vector3& particleVelocity)
Vector3 viewProj = view - vel * view.dot(vel);

// This is the particle normal in object space (after being oriented such that y || velocity)
Vector3 z = object2Vel.zCol().getVector3();
Vector3 z = object2Vel.zCol3();

// The particle needs to be rotated by this angle around the velocity axis
double aimedAngle = z.angle(-viewProj);
Expand Down Expand Up @@ -563,7 +563,7 @@ void RenderableParticleBunch::pushQuad(ParticleRenderInfo& particle, const Vecto
// greebo: Create a (rotated) quad facing the z axis
// then rotate it to fit the requested orientation
// finally translate it to its position.
const Vector3& normal = _viewRotation.zCol().getVector3();
const Vector3& normal = _viewRotation.zCol3();

_quads.push_back(ParticleQuad(particle.size, particle.aspect, particle.angle, colour, normal, s0, sWidth));
_quads.back().transform(_viewRotation);
Expand Down Expand Up @@ -625,7 +625,7 @@ void RenderableParticleBunch::pushAimedParticles(ParticleRenderInfo& particle, s
Matrix4 local2aimed = getAimedMatrix(velocity);

{
const Vector3& normal = local2aimed.zCol().getVector3();
const Vector3& normal = local2aimed.zCol3();

// Ignore the angle for aimed orientation
ParticleQuad curQuad(aimedParticle.size, aimedParticle.aspect, 0,
Expand Down
22 changes: 11 additions & 11 deletions radiantcore/selection/manipulators/RotateManipulator.cpp
Expand Up @@ -93,36 +93,36 @@ void RotateManipulator::UpdateColours()
void RotateManipulator::updateCircleTransforms()
{
Vector3 localViewpoint(
_pivot2World._worldSpace.getTransposed().transformDirection(_pivot2World._viewpointSpace.zCol().getVector3())
_pivot2World._worldSpace.getTransposed().transformDirection(_pivot2World._viewpointSpace.zCol3())
);

_circleX_visible = !math::isNear(g_vector3_axis_x, localViewpoint, 1e-6);
if(_circleX_visible)
{
_local2worldX = Matrix4::getIdentity();
_local2worldX.yCol().getVector3() = g_vector3_axis_x.cross(localViewpoint).getNormalised();
_local2worldX.zCol().getVector3() = _local2worldX.xCol().getVector3().cross(
_local2worldX.yCol().getVector3()).getNormalised();
_local2worldX.yCol3() = g_vector3_axis_x.cross(localViewpoint).getNormalised();
_local2worldX.zCol3() = _local2worldX.xCol3().cross(
_local2worldX.yCol3()).getNormalised();
_local2worldX.premultiplyBy(_pivot2World._worldSpace);
}

_circleY_visible = !math::isNear(g_vector3_axis_y, localViewpoint, 1e-6);
if(_circleY_visible)
{
_local2worldY = Matrix4::getIdentity();
_local2worldY.zCol().getVector3() = g_vector3_axis_y.cross(localViewpoint).getNormalised();
_local2worldY.xCol().getVector3() = _local2worldY.yCol().getVector3().cross(
_local2worldY.zCol().getVector3()).getNormalised();
_local2worldY.zCol3() = g_vector3_axis_y.cross(localViewpoint).getNormalised();
_local2worldY.xCol3() = _local2worldY.yCol3().cross(
_local2worldY.zCol3()).getNormalised();
_local2worldY.premultiplyBy(_pivot2World._worldSpace);
}

_circleZ_visible = !math::isNear(g_vector3_axis_z, localViewpoint, 1e-6);
if(_circleZ_visible)
{
_local2worldZ = Matrix4::getIdentity();
_local2worldZ.xCol().getVector3() = g_vector3_axis_z.cross(localViewpoint).getNormalised();
_local2worldZ.yCol().getVector3() = _local2worldZ.zCol().getVector3().cross(
_local2worldZ.xCol().getVector3()).getNormalised();
_local2worldZ.xCol3() = g_vector3_axis_z.cross(localViewpoint).getNormalised();
_local2worldZ.yCol3() = _local2worldZ.zCol3().cross(
_local2worldZ.xCol3()).getNormalised();
_local2worldZ.premultiplyBy(_pivot2World._worldSpace);
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ std::string RotateManipulator::getRotationAxisName() const
if (_selectableX.isSelected()) return "X";
if (_selectableY.isSelected()) return "Y";
if (_selectableZ.isSelected()) return "Z";

return std::string();
}

Expand Down
12 changes: 6 additions & 6 deletions radiantcore/selection/manipulators/TranslateManipulator.cpp
Expand Up @@ -52,13 +52,13 @@ void TranslateManipulator::render(RenderableCollector& collector, const VolumeTe
// temp hack
UpdateColours();

Vector3 x = _pivot2World._worldSpace.xCol().getVector3().getNormalised();
Vector3 x = _pivot2World._worldSpace.xCol3().getNormalised();
bool show_x = manipulator_show_axis(_pivot2World, x);

Vector3 y = _pivot2World._worldSpace.yCol().getVector3().getNormalised();
Vector3 y = _pivot2World._worldSpace.yCol3().getNormalised();
bool show_y = manipulator_show_axis(_pivot2World, y);

Vector3 z = _pivot2World._worldSpace.zCol().getVector3().getNormalised();
Vector3 z = _pivot2World._worldSpace.zCol3().getNormalised();
bool show_z = manipulator_show_axis(_pivot2World, z);

if(show_x)
Expand Down Expand Up @@ -97,13 +97,13 @@ void TranslateManipulator::testSelect(SelectionTest& test, const Matrix4& pivot2

SelectionPool selector;

Vector3 x = _pivot2World._worldSpace.xCol().getVector3().getNormalised();
Vector3 x = _pivot2World._worldSpace.xCol3().getNormalised();
bool show_x = manipulator_show_axis(_pivot2World, x);

Vector3 y = _pivot2World._worldSpace.yCol().getVector3().getNormalised();
Vector3 y = _pivot2World._worldSpace.yCol3().getNormalised();
bool show_y = manipulator_show_axis(_pivot2World, y);

Vector3 z = _pivot2World._worldSpace.zCol().getVector3().getNormalised();
Vector3 z = _pivot2World._worldSpace.zCol3().getNormalised();
bool show_z = manipulator_show_axis(_pivot2World, z);

{
Expand Down
24 changes: 12 additions & 12 deletions test/math/Matrix4.cpp
Expand Up @@ -121,20 +121,20 @@ TEST(MatrixTest, AccessMatrixColumnVectors)
26, -100, 0.5, 3);

// Read column values
EXPECT_EQ(m.xCol(), Vector4(1, 2, 11, 26));
EXPECT_EQ(m.yCol(), Vector4(4, 9, -2, -100));
EXPECT_EQ(m.zCol(), Vector4(8, 7, 10, 0.5));
EXPECT_EQ(m.xCol3(), Vector3(1, 2, 11));
EXPECT_EQ(m.yCol3(), Vector3(4, 9, -2));
EXPECT_EQ(m.zCol3(), Vector3(8, 7, 10));
EXPECT_EQ(m.tCol(), Vector4(-5, 13, 14, 3));
EXPECT_EQ(m.translation(), Vector3(-5, 13, 14));

// Write column values
m.xCol() = Vector4(0.1, 0.2, 0.3, 0.4);
m.yCol() = Vector4(0.5, 0.6, 0.7, 0.8);
m.zCol() = Vector4(0.9, 1.0, 1.1, 1.2);
m.xCol3() = Vector3(0.1, 0.2, 0.3);
m.yCol3() = Vector3(0.5, 0.6, 0.7);
m.zCol3() = Vector3(0.9, 1.0, 1.1);
m.tCol() = Vector4(1.3, 1.4, 1.5, 1.6);
EXPECT_EQ(m, Matrix4::byColumns(0.1, 0.2, 0.3, 0.4,
0.5, 0.6, 0.7, 0.8,
0.9, 1.0, 1.1, 1.2,
EXPECT_EQ(m, Matrix4::byColumns(0.1, 0.2, 0.3, 26,
0.5, 0.6, 0.7, -100,
0.9, 1.0, 1.1, 0.5,
1.3, 1.4, 1.5, 1.6));
}

Expand Down Expand Up @@ -536,9 +536,9 @@ TEST(MatrixTest, GetInverseScale)
Matrix4 invSc = getInverseScale(m);

// Result should be a diagonal matrix containing only a scale
EXPECT_TRUE(math::isNear(invSc.xCol(), Vector4(1.0 / 3, 0, 0, 0), 1E-6));
EXPECT_TRUE(math::isNear(invSc.yCol(), Vector4(0, 1.0 / 2.5, 0, 0), 1E-6));
EXPECT_TRUE(math::isNear(invSc.zCol(), Vector4(0, 0, 1.0 / 8.2, 0), 1E-6));
EXPECT_TRUE(math::isNear(invSc.xCol3(), Vector3(1.0 / 3, 0, 0), 1E-6));
EXPECT_TRUE(math::isNear(invSc.yCol3(), Vector3(0, 1.0 / 2.5, 0), 1E-6));
EXPECT_TRUE(math::isNear(invSc.zCol3(), Vector3(0, 0, 1.0 / 8.2), 1E-6));
EXPECT_TRUE(math::isNear(invSc.tCol(), Vector4(0, 0, 0, 1), 1E-6));
}

Expand Down

0 comments on commit 1a00a72

Please sign in to comment.