Skip to content

Commit

Permalink
#5547: Use the TextureNormaliser algorithm in Patch::normaliseTexture()
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Oct 5, 2021
1 parent ce0a9df commit 2eb9926
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 49 deletions.
43 changes: 3 additions & 40 deletions radiantcore/patch/Patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2634,46 +2634,9 @@ void Patch::stitchTextureFrom(Patch& sourcePatch) {
controlPointsChanged();
}

void Patch::normaliseTexture() {
// Find the nearest control vertex

// Initialise the compare value
PatchControlIter nearestControl = _ctrl.begin();

// Cycle through all the control points with an iterator
for (PatchControlIter i = _ctrl.begin(); i != _ctrl.end(); ++i) {
// Take the according value (e.g. s = x, t = y, depending on the nAxis argument)
// and apply the appropriate texture coordinate
if (i->texcoord.getLength() < nearestControl->texcoord.getLength()) {
nearestControl = i;
}
}

// Get the nearest texcoord
Vector2 texcoord = nearestControl->texcoord;

// The floored values
Vector2 floored(floor(fabs(texcoord[0])), floor(fabs(texcoord[1])));

// Compute the shift applicable to all vertices
Vector2 shift;
shift[0] = (fabs(texcoord[0])>1.0E-4) ? -floored[0] * texcoord[0]/fabs(texcoord[0]) : 0.0f;
shift[1] = (fabs(texcoord[1])>1.0E-4) ? -floored[1] * texcoord[1]/fabs(texcoord[1]) : 0.0f;

// Is there anything to do at all?
if (shift.getLength() > 0.0f) {
// Save the undo memento
undoSave();

// Now shift all the texture vertices in the right direction, so that this patch
// is getting as close as possible to the origin in texture space.
for (PatchControlIter i = _ctrl.begin(); i != _ctrl.end(); ++i) {
i->texcoord += shift;
}

// Notify the patch about the change
controlPointsChanged();
}
void Patch::normaliseTexture()
{
selection::algorithm::TextureNormaliser::NormalisePatch(*this);
}

const Subdivisions& Patch::getSubdivisions() const
Expand Down
18 changes: 9 additions & 9 deletions test/TextureManipulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,15 +526,15 @@ TEST_F(TextureManipulationTest, NormalisePatch)

patch->normaliseTexture();

EXPECT_TRUE(math::isNear(patch->ctrlAt(0, 0).texcoord,{ 3.2263, 6.4018}, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(1, 0).texcoord,{ 1.855, 3.9462}, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(2, 0).texcoord,{ 0.4838, 1.4907}, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(0, 1).texcoord,{ 4.8088, 5.5181}, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(1, 1).texcoord,{ 3.4375, 3.0625}, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(2, 1).texcoord,{ 2.0662, 0.6069}, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(0, 2).texcoord,{ 6.3912, 4.6343}, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(1, 2).texcoord,{ 5.02, 2.1788}, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(2, 2).texcoord,{ 3.6487, 0.2768}, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(0, 0).texcoord, { 0.226303, 3.40177 }, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(1, 0).texcoord, { -1.14497, 0.946209 }, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(2, 0).texcoord, { -2.51625, -1.50935 }, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(0, 1).texcoord, { 1.80877, 2.51806 }, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(1, 1).texcoord, { 0.4375, 0.0625 }, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(2, 1).texcoord, { -0.933773, -2.39306 }, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(0, 2).texcoord, { 3.39125, 1.63435 }, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(1, 2).texcoord, { 2.01997, -0.821211 }, 0.01));
EXPECT_TRUE(math::isNear(patch->ctrlAt(2, 2).texcoord, { 0.648697, -3.27677 }, 0.01));
}

}

0 comments on commit 2eb9926

Please sign in to comment.