Skip to content

Commit

Permalink
#5969: Add unit test checking grid snapping of patch vertices
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jun 6, 2022
1 parent c1190bf commit 8487ddd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Expand Up @@ -30,6 +30,7 @@ add_executable(drtest
ModelScale.cpp
Models.cpp
Particles.cpp
Patch.cpp
PatchIterators.cpp
PatchWelding.cpp
PointTrace.cpp
Expand Down
100 changes: 100 additions & 0 deletions test/Patch.cpp
@@ -0,0 +1,100 @@
#include "RadiantTest.h"

#include "imap.h"
#include "ipatch.h"
#include "igrid.h"
#include "iselection.h"
#include "scenelib.h"
#include "algorithm/Primitives.h"
#include "algorithm/View.h"
#include "render/View.h"

namespace test
{

using PatchTest = RadiantTest;

namespace
{

void selectSinglePatchVertexAt(const Vector3& position)
{
render::View orthoView(false);

// Construct an orthoview to test-select the patch vertex
algorithm::constructCenteredOrthoview(orthoView, position);
auto centeredTest = algorithm::constructOrthoviewSelectionTest(orthoView);

auto previousComponentCount = GlobalSelectionSystem().countSelectedComponents();

GlobalSelectionSystem().selectPoint(centeredTest, selection::SelectionSystem::eToggle, false);

EXPECT_EQ(GlobalSelectionSystem().countSelectedComponents(), previousComponentCount + 1)
<< "1 additional vertex component should be selected now";
}

}

// Checks that snapping a single selected patch vertex is working
TEST_F(PatchTest, SnapVertexToGrid)
{
auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();

// Use some off-grid bounds to generate the patch
auto patchNode = algorithm::createPatchFromBounds(worldspawn, AABB({ 0,0,0 }, { 11.5, 13, 15 }));
Node_setSelected(patchNode, true);

GlobalGrid().setGridSize(GRID_4);

// Switch to vertex component mode and select a single patch vertex
GlobalSelectionSystem().SetMode(selection::SelectionSystem::eComponent);
GlobalSelectionSystem().SetComponentMode(selection::ComponentSelectionMode::Vertex);

// Pick a vertex from the patch
const auto& ctrl = Node_getIPatch(patchNode)->getTransformedCtrlAt(0, 2);

auto vertexAfterSnapping = ctrl.vertex.getSnapped(GlobalGrid().getGridSize());
EXPECT_FALSE(math::isNear(ctrl.vertex, vertexAfterSnapping, 0.01)) << "Vertex should be off-grid";

selectSinglePatchVertexAt(ctrl.vertex);

// Snap selection to grid
GlobalCommandSystem().executeCommand("SnapToGrid");

EXPECT_TRUE(math::isNear(ctrl.vertex, vertexAfterSnapping, 0.01)) << "Vertex should be snapped to grid now";
}

// Checks that snapping a single vertex to grid is undoable
TEST_F(PatchTest, VertexSnappingIsUndoable)
{
auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();

// Use some off-grid bounds to generate the patch
auto patchNode = algorithm::createPatchFromBounds(worldspawn, AABB({ 0,0,0 }, { 11.5, 13, 15 }));
Node_setSelected(patchNode, true);

GlobalGrid().setGridSize(GRID_4);

// Switch to vertex component mode and select a single patch vertex
GlobalSelectionSystem().SetMode(selection::SelectionSystem::eComponent);
GlobalSelectionSystem().SetComponentMode(selection::ComponentSelectionMode::Vertex);

// Pick a vertex from the patch
const auto& ctrl = Node_getIPatch(patchNode)->getTransformedCtrlAt(0, 2);

auto vertexBeforeSnapping = ctrl.vertex;
auto vertexAfterSnapping = ctrl.vertex.getSnapped(GlobalGrid().getGridSize());
EXPECT_FALSE(math::isNear(ctrl.vertex, vertexAfterSnapping, 0.01)) << "Vertex should be off-grid";

selectSinglePatchVertexAt(ctrl.vertex);

// Snap selection to grid
GlobalCommandSystem().executeCommand("SnapToGrid");

EXPECT_TRUE(math::isNear(ctrl.vertex, vertexAfterSnapping, 0.01)) << "Vertex should be snapped to grid now";

GlobalCommandSystem().executeCommand("Undo");
EXPECT_TRUE(math::isNear(ctrl.vertex, vertexBeforeSnapping, 0.01)) << "Vertex should be reverted and off-grid again";
}

}
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj
Expand Up @@ -112,6 +112,7 @@
<ClCompile Include="..\..\..\test\ModelScale.cpp" />
<ClCompile Include="..\..\..\test\Parsing.cpp" />
<ClCompile Include="..\..\..\test\Particles.cpp" />
<ClCompile Include="..\..\..\test\Patch.cpp" />
<ClCompile Include="..\..\..\test\PatchIterators.cpp" />
<ClCompile Include="..\..\..\test\PatchWelding.cpp" />
<ClCompile Include="..\..\..\test\PointTrace.cpp" />
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj.filters
Expand Up @@ -58,6 +58,7 @@
<ClCompile Include="..\..\..\test\Particles.cpp" />
<ClCompile Include="..\..\..\test\GeometryStore.cpp" />
<ClCompile Include="..\..\..\test\Settings.cpp" />
<ClCompile Include="..\..\..\test\Patch.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\test\HeadlessOpenGLContext.h" />
Expand Down

0 comments on commit 8487ddd

Please sign in to comment.