File size: 6,142 Bytes
a5ffdcd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | /****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2025 librecad.org
** Copyright (C) 2025 Dongxu Li (github.com/dxli)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**********************************************************************/
// File: rs_spline_tests.cpp
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_approx.hpp>
#include "rs_debug.h"
#include "rs_spline.h"
#include "lc_splinehelper.h"
#include "rs_vector.h"
#include "rs_math.h"
using Catch::Approx;
namespace {
bool compareVector(const RS_Vector& va, const RS_Vector& vb, double tol = 1e-4) {
return va.distanceTo(vb) <= tol;
}
}
TEST_CASE("RS_Spline Basic Functionality", "[RS_Spline]")
{
RS_SplineData splineData(3, false);
RS_Spline spline(nullptr, splineData);
SECTION("Construction and Getters")
{
REQUIRE(spline.getDegree() == 3);
REQUIRE(!spline.isClosed());
REQUIRE(spline.getNumberOfControlPoints() == 0);
REQUIRE(spline.getNumberOfKnots() == 0);
}
SECTION("Set Degree")
{
spline.setDegree(2);
REQUIRE(spline.getDegree() == 2);
REQUIRE_THROWS_AS(spline.setDegree(0), std::invalid_argument);
REQUIRE_THROWS_AS(spline.setDegree(4), std::invalid_argument);
}
}
TEST_CASE("Non-uniform knot vectors - validation and type handling", "[RS_Spline][nonuniform]")
{
SECTION("ClampedOpen non-uniform knots - valid")
{
RS_SplineData d(3, false);
d.type = RS_SplineData::SplineType::ClampedOpen;
d.controlPoints = {
RS_Vector(0,0), RS_Vector(10,20), RS_Vector(30,30), RS_Vector(50,20), RS_Vector(60,0), RS_Vector(70,10), RS_Vector(80,0)
};
d.weights = {1.0, 2.0, 1.5, 1.0, 1.0, 1.2, 1.0};
d.knotslist = {0.0, 0.0, 0.0, 0.0, 8.0, 25.0, 55.0, 100.0, 100.0, 100.0, 100.0};
RS_Spline s(nullptr, d);
REQUIRE(s.validate() == true);
REQUIRE(s.isClosed() == false);
REQUIRE(s.getDegree() == 3);
REQUIRE(s.getNumberOfControlPoints() == 7);
}
SECTION("ClampedOpen non-uniform - invalid (wrong end multiplicity)")
{
RS_SplineData d(3, false);
d.type = RS_SplineData::SplineType::ClampedOpen;
d.controlPoints = {
RS_Vector(0,0), RS_Vector(10,20), RS_Vector(30,30), RS_Vector(50,20), RS_Vector(60,0), RS_Vector(70,10), RS_Vector(80,0)
};
d.weights = {1.0, 2.0, 1.5, 1.0, 1.0, 1.2, 1.0};
// Note: end multiplicity is only 3 instead of 4 → invalid for ClampedOpen
d.knotslist = {0.0, 0.0, 0.0, 0.0, 8.0, 25.0, 55.0, 90.0, 100.0, 100.0, 100.0};
RS_Spline s(nullptr, d);
REQUIRE(s.validate() == false);
}
SECTION("Standard (open non-clamped non-uniform) - valid")
{
RS_SplineData d(3, false);
d.type = RS_SplineData::SplineType::Standard;
d.controlPoints = {
RS_Vector(0,0), RS_Vector(15,25), RS_Vector(40,35), RS_Vector(80,0)
};
d.weights.assign(4, 1.0);
d.knotslist = {0.0, 12.0, 35.0, 60.0, 100.0, 140.0, 180.0, 220.0};
RS_Spline s(nullptr, d);
REQUIRE(s.validate() == true);
}
SECTION("Standard non-uniform - invalid (accidental clamping at start)")
{
RS_SplineData d(3, false);
d.type = RS_SplineData::SplineType::Standard;
d.controlPoints = {
RS_Vector(0,0), RS_Vector(15,25), RS_Vector(40,35), RS_Vector(80,0)
};
d.weights.assign(4, 1.0);
d.knotslist = {0.0, 0.0, 0.0, 0.0, 20.0, 50.0, 100.0, 150.0};
RS_Spline s(nullptr, d);
REQUIRE(s.validate() == false);
}
SECTION("WrappedClosed non-uniform knots - valid")
{
RS_SplineData d(3, true);
d.type = RS_SplineData::SplineType::WrappedClosed;
d.controlPoints = {
RS_Vector(0,0), RS_Vector(15,25), RS_Vector(40,35), RS_Vector(70,20), RS_Vector(80,0),
RS_Vector(0,0), RS_Vector(15,25), RS_Vector(40,35)
};
d.weights = {1.0, 1.5, 2.0, 1.5, 1.0, 1.0, 1.5, 2.0};
d.knotslist = {0.0, 12.0, 35.0, 60.0, 100.0, 140.0, 180.0, 220.0, 260.0, 290.0, 320.0, 350.0};
RS_Spline s(nullptr, d);
REQUIRE(s.validate() == true);
REQUIRE(s.isClosed() == true);
REQUIRE(s.hasWrappedControlPoints() == true);
}
SECTION("WrappedClosed non-uniform - invalid (clamped-style ends)")
{
RS_SplineData d(3, true);
d.type = RS_SplineData::SplineType::WrappedClosed;
d.controlPoints.resize(8);
d.weights.assign(8, 1.0);
d.knotslist = {0.0, 0.0, 0.0, 0.0, 20.0, 50.0, 100.0, 150.0, 150.0, 150.0, 150.0};
RS_Spline s(nullptr, d);
REQUIRE(s.validate() == false);
}
SECTION("WrappedClosed non-uniform - valid (missing wrapped control points)")
{
RS_SplineData d(3, true);
d.type = RS_SplineData::SplineType::WrappedClosed;
d.controlPoints = {
RS_Vector(0,0), RS_Vector(10,10), RS_Vector(20,10), RS_Vector(30,10), RS_Vector(40,0),
RS_Vector(99,99), RS_Vector(99,99), RS_Vector(99,99)
};
d.weights.assign(8, 1.0);
d.knotslist = {0.0, 10.0, 20.0, 35.0, 55.0, 80.0, 110.0, 140.0, 170.0, 200.0, 230.0, 260.0};
// contor will add control point wrapping
RS_Spline s(nullptr, d);
REQUIRE(s.validate());
}
}
|