Skip to content

Commit

Permalink
#5773: Add copy constructors and assignment operators to TextureMatri…
Browse files Browse the repository at this point in the history
…x and TextureProjection. TextureProjection::Default() is now private.
  • Loading branch information
codereader committed Oct 9, 2021
1 parent 13a60a1 commit 35be717
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 66 deletions.
21 changes: 8 additions & 13 deletions radiantcore/brush/Face.cpp
Expand Up @@ -16,7 +16,7 @@
#include "BrushModule.h"

// The structure that is saved in the undostack
class Face::SavedState :
class Face::SavedState final :
public IUndoMemento
{
public:
Expand All @@ -29,15 +29,6 @@ class Face::SavedState :
_texdefState(face.getProjection()),
_materialName(face.getShader())
{}

virtual ~SavedState() {}

void exportState(Face& face) const
{
_planeState.exportState(face.getPlane());
face.setShader(_materialName);
face.getProjection().assign(_texdefState);
}
};

Face::Face(Brush& owner) :
Expand Down Expand Up @@ -184,14 +175,18 @@ void Face::undoSave()
// undoable
IUndoMementoPtr Face::exportState() const
{
return IUndoMementoPtr(new SavedState(*this));
return std::make_shared<SavedState>(*this);
}

void Face::importState(const IUndoMementoPtr& data)
{
undoSave();

std::static_pointer_cast<SavedState>(data)->exportState(*this);
auto state = std::static_pointer_cast<SavedState>(data);

state->_planeState.exportState(getPlane());
setShader(state->_materialName);
_texdef = state->_texdefState;

planeChanged();
_owner.onFaceConnectivityChanged();
Expand Down Expand Up @@ -464,7 +459,7 @@ void Face::GetTexdef(TextureProjection& projection) const
void Face::SetTexdef(const TextureProjection& projection)
{
undoSave();
_texdef.assign(projection);
_texdef = projection;
texdefChanged();
}

Expand Down
84 changes: 42 additions & 42 deletions radiantcore/brush/TextureMatrix.cpp
Expand Up @@ -7,22 +7,22 @@

TextureMatrix::TextureMatrix()
{
coords[0][0] = 2.0f;
coords[0][1] = 0.f;
coords[0][2] = 0.f;
coords[1][0] = 0.f;
coords[1][1] = 2.0f;
coords[1][2] = 0.f;
_coords[0][0] = 2.0f;
_coords[0][1] = 0.f;
_coords[0][2] = 0.f;
_coords[1][0] = 0.f;
_coords[1][1] = 2.0f;
_coords[1][2] = 0.f;
}

TextureMatrix::TextureMatrix(const Matrix3& transform)
{
coords[0][0] = transform.xx();
coords[0][1] = transform.yx();
coords[0][2] = transform.zx();
coords[1][0] = transform.xy();
coords[1][1] = transform.yy();
coords[1][2] = transform.zy();
_coords[0][0] = transform.xx();
_coords[0][1] = transform.yx();
_coords[0][2] = transform.zx();
_coords[1][0] = transform.xy();
_coords[1][1] = transform.yy();
_coords[1][2] = transform.zy();
}

TextureMatrix::TextureMatrix(const ShiftScaleRotation& ssr)
Expand All @@ -34,12 +34,12 @@ TextureMatrix::TextureMatrix(const ShiftScaleRotation& ssr)
auto x = 1.0 / ssr.scale[0];
auto y = 1.0 / ssr.scale[1];

coords[0][0] = x * c;
coords[1][0] = x * s;
coords[0][1] = y * -s;
coords[1][1] = y * c;
coords[0][2] = -ssr.shift[0];
coords[1][2] = ssr.shift[1];
_coords[0][0] = x * c;
_coords[1][0] = x * s;
_coords[0][1] = y * -s;
_coords[1][1] = y * c;
_coords[0][2] = -ssr.shift[0];
_coords[1][2] = ssr.shift[1];
}

void TextureMatrix::shift(double s, double t)
Expand All @@ -48,34 +48,34 @@ void TextureMatrix::shift(double s, double t)
// this depends on the texture size and the pixel/texel ratio
// as a ratio against texture size
// the scale of the texture is not relevant here (we work directly on a transformation from the base vectors)
coords[0][2] -= s;
coords[1][2] += t;
_coords[0][2] -= s;
_coords[1][2] += t;
}

void TextureMatrix::addScale(std::size_t width, std::size_t height)
{
coords[0][0] /= width;
coords[0][1] /= width;
coords[0][2] /= width;
coords[1][0] /= height;
coords[1][1] /= height;
coords[1][2] /= height;
_coords[0][0] /= width;
_coords[0][1] /= width;
_coords[0][2] /= width;
_coords[1][0] /= height;
_coords[1][1] /= height;
_coords[1][2] /= height;
}

ShiftScaleRotation TextureMatrix::getShiftScaleRotation() const
{
ShiftScaleRotation ssr;

ssr.scale[0] = 1.0 / Vector2(coords[0][0], coords[1][0]).getLength();
ssr.scale[1] = 1.0 / Vector2(coords[0][1], coords[1][1]).getLength();
ssr.scale[0] = 1.0 / Vector2(_coords[0][0], _coords[1][0]).getLength();
ssr.scale[1] = 1.0 / Vector2(_coords[0][1], _coords[1][1]).getLength();

ssr.rotate = -radians_to_degrees(arctangent_yx(coords[1][0], coords[0][0]));
ssr.rotate = -radians_to_degrees(arctangent_yx(_coords[1][0], _coords[0][0]));

ssr.shift[0] = -coords[0][2];
ssr.shift[1] = coords[1][2];
ssr.shift[0] = -_coords[0][2];
ssr.shift[1] = _coords[1][2];

// determine whether or not an axis is flipped using a 2d cross-product
auto cross = Vector2(coords[0][0], coords[0][1]).crossProduct(Vector2(coords[1][0], coords[1][1]));
auto cross = Vector2(_coords[0][0], _coords[0][1]).crossProduct(Vector2(_coords[1][0], _coords[1][1]));

if (cross < 0)
{
Expand All @@ -99,25 +99,25 @@ ShiftScaleRotation TextureMatrix::getShiftScaleRotation() const

void TextureMatrix::normalise(float width, float height)
{
coords[0][2] = float_mod(coords[0][2], width);
coords[1][2] = float_mod(coords[1][2], height);
_coords[0][2] = float_mod(_coords[0][2], width);
_coords[1][2] = float_mod(_coords[1][2], height);
}

Matrix3 TextureMatrix::getMatrix3() const
{
return Matrix3::byRows(
coords[0][0], coords[0][1], coords[0][2],
coords[1][0], coords[1][1], coords[1][2],
_coords[0][0], _coords[0][1], _coords[0][2],
_coords[1][0], _coords[1][1], _coords[1][2],
0, 0, 1
);
}

bool TextureMatrix::isSane() const
{
return !std::isnan(coords[0][0]) && !std::isinf(coords[0][0]) &&
!std::isnan(coords[0][1]) && !std::isinf(coords[0][1]) &&
!std::isnan(coords[0][2]) && !std::isinf(coords[0][2]) &&
!std::isnan(coords[1][0]) && !std::isinf(coords[1][0]) &&
!std::isnan(coords[1][1]) && !std::isinf(coords[1][1]) &&
!std::isnan(coords[1][2]) && !std::isinf(coords[1][2]);
return !std::isnan(_coords[0][0]) && !std::isinf(_coords[0][0]) &&
!std::isnan(_coords[0][1]) && !std::isinf(_coords[0][1]) &&
!std::isnan(_coords[0][2]) && !std::isinf(_coords[0][2]) &&
!std::isnan(_coords[1][0]) && !std::isinf(_coords[1][0]) &&
!std::isnan(_coords[1][1]) && !std::isinf(_coords[1][1]) &&
!std::isnan(_coords[1][2]) && !std::isinf(_coords[1][2]);
}
9 changes: 6 additions & 3 deletions radiantcore/brush/TextureMatrix.h
Expand Up @@ -35,11 +35,14 @@
class TextureMatrix final
{
private:
double coords[2][3];
double _coords[2][3];

public:
TextureMatrix();

TextureMatrix(const TextureMatrix& other) = default;
TextureMatrix& operator=(const TextureMatrix& other) = default;

// Copy-construct from the relevant components from the given transform
// (which is everything except the last row: xz() and yz() are 0, zz() is 1)
TextureMatrix(const Matrix3& transform);
Expand Down Expand Up @@ -75,7 +78,7 @@ class TextureMatrix final

inline std::ostream& operator<<(std::ostream& st, const TextureMatrix& texdef)
{
st << "<" << texdef.coords[0][0] << ", " << texdef.coords[0][1] << ", " << texdef.coords[0][2] << ">\n";
st << "<" << texdef.coords[1][0] << ", " << texdef.coords[1][1] << ", " << texdef.coords[1][2] << ">";
st << "<" << texdef._coords[0][0] << ", " << texdef._coords[0][1] << ", " << texdef._coords[0][2] << ">\n";
st << "<" << texdef._coords[1][0] << ", " << texdef._coords[1][1] << ", " << texdef._coords[1][2] << ">";
return st;
}
10 changes: 5 additions & 5 deletions radiantcore/brush/TextureProjection.cpp
Expand Up @@ -6,7 +6,7 @@
#include <limits>

TextureProjection::TextureProjection() :
TextureProjection(GetDefaultProjection())
TextureProjection(Default())
{}

TextureProjection::TextureProjection(const TextureProjection& other) :
Expand All @@ -27,7 +27,7 @@ TextureMatrix& TextureProjection::getTextureMatrix()
return _matrix;
}

TextureMatrix TextureProjection::GetDefaultProjection()
TextureMatrix TextureProjection::Default()
{
// Cache the registry key because this constructor is called a lot
static registry::CachedKey<float> scaleKey(
Expand All @@ -42,10 +42,10 @@ TextureMatrix TextureProjection::GetDefaultProjection()
return TextureMatrix(ssr);
}

// Assigns an <other> projection to this one
void TextureProjection::assign(const TextureProjection& other)
TextureProjection& TextureProjection::operator=(const TextureProjection& other)
{
_matrix = other._matrix;
return *this;
}

void TextureProjection::setTransform(const Matrix3& transform)
Expand Down Expand Up @@ -102,7 +102,7 @@ void TextureProjection::fitTexture(std::size_t width, std::size_t height,
}

// Sanity-check the matrix, if it contains any NaNs or INFs we fall back to the default projection (#5371)
Matrix4 st2tex = _matrix.isSane() ? getMatrix4() : getMatrix4FromTextureMatrix(GetDefaultProjection().getMatrix3());
Matrix4 st2tex = _matrix.isSane() ? getMatrix4() : getMatrix4FromTextureMatrix(Default().getMatrix3());

// the current texture transform
Matrix4 local2tex = st2tex;
Expand Down
6 changes: 3 additions & 3 deletions radiantcore/brush/TextureProjection.h
Expand Up @@ -38,9 +38,7 @@ class TextureProjection final
const TextureMatrix& getTextureMatrix() const;
TextureMatrix& getTextureMatrix();

static TextureMatrix GetDefaultProjection();

void assign(const TextureProjection& other);
TextureProjection& operator=(const TextureProjection& other);

void setTransform(const Matrix3& transform);
void setFromShiftScaleRotate(const ShiftScaleRotation& ssr);
Expand Down Expand Up @@ -69,6 +67,8 @@ class TextureProjection final
void calculateFromPoints(const Vector3 points[3], const Vector2 uvs[3], const Vector3& normal);

private:
static TextureMatrix Default();

Matrix4 getMatrix4() const;
void setTransformFromMatrix4(const Matrix4& transform);

Expand Down

0 comments on commit 35be717

Please sign in to comment.