Part: [skip ci] add method to approximate a B-spline curve with a given max. degree

This commit is contained in:
wmayer
2020-10-23 00:06:31 +02:00
parent 09c72214e8
commit 4fbc693014
3 changed files with 70 additions and 8 deletions

View File

@@ -801,6 +801,7 @@ PyObject* BSplineCurvePy::approximate(PyObject *args, PyObject *kwds)
PyObject* obj;
Standard_Integer degMin=3;
Standard_Integer degMax=8;
Standard_Integer segMax=8;
char* continuity = "C2";
double tol3d = 1e-3;
char* parType = "ChordLength";
@@ -809,15 +810,46 @@ PyObject* BSplineCurvePy::approximate(PyObject *args, PyObject *kwds)
double weight2 = 0;
double weight3 = 0;
static char* kwds_interp[] = {"Points", "DegMax", "Continuity", "Tolerance", "DegMin", "ParamType", "Parameters",
"LengthWeight", "CurvatureWeight", "TorsionWeight", NULL};
// Approximate this curve with a given continuity and degree
static char* kwds_reapprox[] = {"MaxDegree", "MaxSegments", "Continuity", "Tolerance", nullptr};
if (PyArg_ParseTupleAndKeywords(args, kwds, "i|isd", kwds_reapprox,
&tol3d, &degMax, &segMax, &continuity)) {
GeomAbs_Shape c;
std::string str = continuity;
if (str == "C0")
c = GeomAbs_C0;
else if (str == "G1")
c = GeomAbs_G1;
else if (str == "C1")
c = GeomAbs_C1;
else if (str == "G2")
c = GeomAbs_G2;
else if (str == "C2")
c = GeomAbs_C2;
else if (str == "C3")
c = GeomAbs_C3;
else if (str == "CN")
c = GeomAbs_CN;
else
c = GeomAbs_C2;
bool ok = this->getGeomBSplineCurvePtr()->approximate(tol3d, segMax, degMax, c);
return Py_BuildValue("O", (ok ? Py_True : Py_False));
}
// Approximate a list of points
//
static char* kwds_interp[] = {"Points", "DegMax", "Continuity", "Tolerance", "DegMin", "ParamType", "Parameters",
"LengthWeight", "CurvatureWeight", "TorsionWeight", nullptr};
PyErr_Clear();
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|isdisOddd",kwds_interp,
&obj, &degMax,
&continuity, &tol3d, &degMin,
&parType, &par,
&weight1, &weight2, &weight3))
return 0;
return nullptr;
try {
Py::Sequence list(obj);

View File

@@ -23,6 +23,7 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <Approx_Curve3d.hxx>
# include <BRepBuilderAPI_MakeEdge.hxx>
# include <BRepBuilderAPI_MakeFace.hxx>
# include <BRepBuilderAPI_MakeVertex.hxx>
@@ -50,6 +51,7 @@
# include <Geom_RectangularTrimmedSurface.hxx>
# include <Geom_SurfaceOfRevolution.hxx>
# include <Geom_SurfaceOfLinearExtrusion.hxx>
# include <GeomAdaptor_HCurve.hxx>
# include <GeomAPI_Interpolate.hxx>
# include <GeomConvert.hxx>
# include <GeomConvert_CompCurveToBSplineCurve.hxx>
@@ -1338,19 +1340,47 @@ void GeomBSplineCurve::makeC1Continuous(double tol, double ang_tol)
GeomConvert::C0BSplineToC1BSplineCurve(this->myCurve, tol, ang_tol);
}
void GeomBSplineCurve::increaseDegree(double degree)
void GeomBSplineCurve::increaseDegree(int degree)
{
try {
Handle(Geom_BSplineCurve) curve = Handle(Geom_BSplineCurve)::DownCast(this->handle());
curve->IncreaseDegree(degree);
return;
}
catch (Standard_Failure& e) {
THROWM(Base::CADKernelError,e.GetMessageString())
}
}
/*!
* \brief GeomBSplineCurve::approximate
* \param tol3d
* \param maxSegments
* \param maxDegree
* \param continuity
* \return true if the approximation succeeded, false otherwise
*/
bool GeomBSplineCurve::approximate(double tol3d, int maxSegments, int maxDegree, int continuity)
{
try {
GeomAbs_Shape cont = GeomAbs_C0;
if (continuity >= 0 && continuity <= 6)
cont = static_cast<GeomAbs_Shape>(continuity);
GeomAdaptor_Curve adapt(myCurve);
Handle(GeomAdaptor_HCurve) hCurve = new GeomAdaptor_HCurve(adapt);
Approx_Curve3d approx(hCurve, tol3d, cont, maxSegments, maxDegree);
if (approx.IsDone() && approx.HasResult()) {
this->setHandle(approx.Curve());
return true;
}
}
catch (Standard_Failure& e) {
THROWM(Base::CADKernelError,e.GetMessageString())
}
return false;
}
void GeomBSplineCurve::increaseMultiplicity(int index, int multiplicity)
{
try {
@@ -1359,7 +1389,6 @@ void GeomBSplineCurve::increaseMultiplicity(int index, int multiplicity)
return;
}
catch (Standard_Failure& e) {
THROWM(Base::CADKernelError,e.GetMessageString())
}
}

View File

@@ -291,7 +291,8 @@ public:
void makeC1Continuous(double, double);
std::list<Geometry*> toBiArcs(double tolerance) const;
void increaseDegree(double degree);
void increaseDegree(int degree);
bool approximate(double tol3d, int maxSegments, int maxDegree, int continuity);
void increaseMultiplicity(int index, int multiplicity);
bool removeKnot(int index, int multiplicity, double tolerance = Precision::PConfusion());