Skip to content

Commit 0dfe953

Browse files
authored
Merge pull request #11 from rainman110/fix_scale_sensitivity
Fix scale sensitivity
2 parents b012fa7 + 6eabde4 commit 0dfe953

5 files changed

Lines changed: 18 additions & 15 deletions

File tree

src/internal/BSplineAlgorithms.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,8 @@ ApproxResult BSplineAlgorithms::reparametrizeBSplineContinuouslyApprox(const Han
754754
approximationObj.InterpolatePoint(idx, true);
755755
}
756756
#endif
757-
758-
ApproxResult result = approximationObj.FitCurveOptimal(parameters);
757+
758+
ApproxResult result = approximationObj.FitCurveOptimal(parameters, 1);
759759

760760
assert(!result.curve.IsNull());
761761

src/internal/BSplineApproxInterp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ ApproxResult BSplineApproxInterp::FitCurveOptimal(const std::vector<double>& ini
254254
ApproxResult result = solve(parms, occKnots->Array1(), occMults->Array1());
255255
double old_error = result.error * 2.;
256256

257-
while(result.error > 0 && (old_error - result.error) / std::max(result.error, 1e-6) > 1e-3 && iteration < maxIter) {
257+
while(result.error > 0 && (old_error - result.error) / result.error > 1e-3 && iteration < maxIter) {
258258
old_error = result.error;
259259

260260
optimizeParameters(result.curve, parms);

src/internal/IntersectBSplines.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ namespace
232232
class CurveCurveDistanceObjective : public math_MultipleVarFunctionWithGradient
233233
{
234234
public:
235-
CurveCurveDistanceObjective(const Handle(Geom_Curve)& c1, const Handle(Geom_Curve)& c2)
236-
: m_c1(c1), m_c2(c2)
235+
CurveCurveDistanceObjective(const Handle(Geom_Curve)& c1, const Handle(Geom_Curve)& c2, double scale)
236+
: m_c1(c1), m_c2(c2), m_invScaleSqr(1. / sqr(std::max(scale, std::numeric_limits<double>::epsilon())))
237237
{}
238238

239239
virtual Standard_Integer NbVariables() const override
@@ -310,18 +310,18 @@ namespace
310310
m_c2->D1(v, p2, d2);
311311

312312
gp_Vec diff = p1.XYZ() - p2.XYZ();
313-
F = diff.SquareMagnitude();
314-
G(1) = 2. * diff.Dot(d1) * d_getUParam(X.Value(1));
315-
G(2) = -2. * diff.Dot(d2) * d_getVParam(X.Value(2));
313+
F = diff.SquareMagnitude() * m_invScaleSqr;
314+
G(1) = 2. * diff.Dot(d1) * d_getUParam(X.Value(1)) * m_invScaleSqr;
315+
G(2) = -2. * diff.Dot(d2) * d_getVParam(X.Value(2)) * m_invScaleSqr;
316316

317317
return true;
318318
}
319319

320320
private:
321321
const Handle(Geom_Curve) m_c1, m_c2;
322+
const double m_invScaleSqr;
322323
};
323324

324-
325325
void CheckGradient(math_MultipleVarFunctionWithGradient& func, const math_Vector& X, double step)
326326
{
327327
int nvars = func.NbVariables();
@@ -373,6 +373,7 @@ namespace occ_gordon_internal
373373

374374
std::vector<CurveIntersectionResult> IntersectBSplines(const Handle(Geom_BSplineCurve) curve1, const Handle(Geom_BSplineCurve) curve2, double tolerance)
375375
{
376+
const double optimizerScale = (BSplineAlgorithms::scale(curve1) + BSplineAlgorithms::scale(curve2)) / 2.;
376377
auto hulls = getRangesOfIntersection(curve1, curve2, tolerance);
377378

378379
std::list<BoundingBox> curve1_ints, curve2_ints;
@@ -425,7 +426,7 @@ std::vector<CurveIntersectionResult> IntersectBSplines(const Handle(Geom_BSpline
425426
auto c1 = BSplineAlgorithms::trimCurve(curve1, boxes.b1.range.min,boxes.b1.range.max);
426427
auto c2 = BSplineAlgorithms::trimCurve(curve2, boxes.b2.range.min,boxes.b2.range.max);
427428

428-
CurveCurveDistanceObjective obj(c1, c2);
429+
CurveCurveDistanceObjective obj(c1, c2, optimizerScale);
429430

430431
// The objective is designed such that x=[0, 0] is in the middle of the parameter space of both curves
431432
math_Vector guess(1, 2);

src/internal/IntersectBSplines.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ struct CurveIntersectionResult
3333
* intersect each other. If so, this process is repeated until the curve segment is almost a line segment.
3434
* This result is used to locally optimize into a true minimum.
3535
*/
36-
std::vector<CurveIntersectionResult> IntersectBSplines(const Handle(Geom_BSplineCurve) curve1, const Handle(Geom_BSplineCurve) curve2, double tolerance=1e-5);
36+
std::vector<CurveIntersectionResult> IntersectBSplines(const Handle(Geom_BSplineCurve) curve1,
37+
const Handle(Geom_BSplineCurve) curve2,
38+
double absTolerance=1e-5);
3739

3840
} // namespace occ_gordon_internal
3941

tests/unittests/testctiglbsplinealgorithms.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ TEST(BSplineAlgorithms, testIntersectionFinder)
10041004
std::vector<std::pair<double, double> > intersection_vector = BSplineAlgorithms::intersections(spline_u, spline_v);
10051005

10061006
// splines should intersect at u = 0.5 + std::sqrt(0.1) and v = 4. / 5
1007-
ASSERT_NEAR(intersection_vector[0].first, 0.5 + std::sqrt(0.1), 1e-13);
1007+
ASSERT_NEAR(intersection_vector[0].first, 0.5 + std::sqrt(0.1), 1e-12);
10081008
ASSERT_NEAR(intersection_vector[0].second, 4. / 5, 1e-13);
10091009
}
10101010
/*
@@ -1223,9 +1223,9 @@ TEST(BSplineAlgorithms, testCreateGordonSurfaceGeneral)
12231223
gp_Pnt point_curve2 = spline_u4->Value(u_value); // represents y(z) = (z - 0.5)^2 with offset 2 in x-direction
12241224
gp_Pnt right_point(point_curve1.X() * (1. - v_value) + point_curve2.X() * v_value, point_curve1.Y() * (1. - v_value) + point_curve2.Y() * v_value, point_curve1.Z() * (1. - v_value) + point_curve2.Z() * v_value);
12251225

1226-
ASSERT_NEAR(surface_point.X(), right_point.X(), 1e-13);
1227-
ASSERT_NEAR(surface_point.Y(), right_point.Y(), 1e-13);
1228-
ASSERT_NEAR(surface_point.Z(), right_point.Z(), 1e-13);
1226+
ASSERT_NEAR(surface_point.X(), right_point.X(), 1e-12);
1227+
ASSERT_NEAR(surface_point.Y(), right_point.Y(), 1e-12);
1228+
ASSERT_NEAR(surface_point.Z(), right_point.Z(), 1e-12);
12291229
}
12301230
}
12311231
}

0 commit comments

Comments
 (0)