LeastSquaresFitting::InitialGuessForLeastSquaresFitting LeastSquaresFitting::findInitialGuess(const Handle_TColgp_HArray1OfPnt& points) { //Compute center of gravity for point set double cx=0.0; double cy=0.0; for(Standard_Integer i=points->Lower();i<=points->Upper();i++) { cx += points->Value(i).X(); cy += points->Value(i).Y(); } cx /= points->Length(); cy /= points->Length(); gp_Pnt centerPoint(cx,cy,0.0); //Compute average radius double averageRadius = 0.0; for(Standard_Integer i=points->Lower();i<=points->Upper();i++) { averageRadius += centerPoint.Distance(points->Value(i)); } averageRadius /= points->Length(); return InitialGuessForLeastSquaresFitting(centerPoint,averageRadius); }
PyObject* BSplineCurvePy::interpolate(PyObject *args) { PyObject* obj; double tol3d = Precision::Approximation(); PyObject* periodic = Py_False; PyObject* t1=0; PyObject* t2=0; if (!PyArg_ParseTuple(args, "O|O!dO!O!",&obj, &PyBool_Type, &periodic, &tol3d, &Base::VectorPy::Type, &t1, &Base::VectorPy::Type, &t2)) return 0; try { Py::Sequence list(obj); Handle_TColgp_HArray1OfPnt interpolationPoints = new TColgp_HArray1OfPnt(1, list.size()); Standard_Integer index = 1; for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { Py::Vector v(*it); Base::Vector3d pnt = v.toVector(); interpolationPoints->SetValue(index++, gp_Pnt(pnt.x,pnt.y,pnt.z)); } if (interpolationPoints->Length() < 2) { Standard_Failure::Raise("not enough points given"); } GeomAPI_Interpolate aBSplineInterpolation(interpolationPoints, PyObject_IsTrue(periodic) ? Standard_True : Standard_False, tol3d); if (t1 && t2) { Base::Vector3d v1 = Py::Vector(t1,false).toVector(); Base::Vector3d v2 = Py::Vector(t2,false).toVector(); gp_Vec initTangent(v1.x,v1.y,v1.z), finalTangent(v2.x,v2.y,v2.z); aBSplineInterpolation.Load(initTangent, finalTangent); } aBSplineInterpolation.Perform(); if (aBSplineInterpolation.IsDone()) { Handle_Geom_BSplineCurve aBSplineCurve(aBSplineInterpolation.Curve()); this->getGeomBSplineCurvePtr()->setHandle(aBSplineCurve); Py_Return; } else { Standard_Failure::Raise("failed to interpolate points"); return 0; // goes to the catch block } } catch (Standard_Failure) { Handle_Standard_Failure e = Standard_Failure::Caught(); std::string err = e->GetMessageString(); if (err.empty()) err = e->DynamicType()->Name(); PyErr_SetString(PartExceptionOCCError, err.c_str()); return 0; } }
PyObject* BSplineCurvePy::interpolate(PyObject *args, PyObject *kwds) { PyObject* obj; PyObject* par = 0; double tol3d = Precision::Approximation(); PyObject* periodic = Py_False; PyObject* t1 = 0; PyObject* t2 = 0; PyObject* ts = 0; PyObject* fl = 0; PyObject* scale = Py_True; static char* kwds_interp[] = {"Points", "PeriodicFlag", "Tolerance", "InitialTangent", "FinalTangent", "Tangents", "TangentFlags", "Parameters", "Scale", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O!dO!O!OOOO!",kwds_interp, &obj, &PyBool_Type, &periodic, &tol3d, &Base::VectorPy::Type, &t1, &Base::VectorPy::Type, &t2, &ts, &fl, &par, &PyBool_Type, &scale)) return 0; try { Py::Sequence list(obj); Handle_TColgp_HArray1OfPnt interpolationPoints = new TColgp_HArray1OfPnt(1, list.size()); Standard_Integer index = 1; for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { Py::Vector v(*it); Base::Vector3d pnt = v.toVector(); interpolationPoints->SetValue(index++, gp_Pnt(pnt.x,pnt.y,pnt.z)); } if (interpolationPoints->Length() < 2) { Standard_Failure::Raise("not enough points given"); } Handle_TColStd_HArray1OfReal parameters; if (par) { Py::Sequence plist(par); parameters = new TColStd_HArray1OfReal(1, plist.size()); Standard_Integer pindex = 1; for (Py::Sequence::iterator it = plist.begin(); it != plist.end(); ++it) { Py::Float f(*it); parameters->SetValue(pindex++, static_cast<double>(f)); } } std::unique_ptr<GeomAPI_Interpolate> aBSplineInterpolation; if (parameters.IsNull()) { aBSplineInterpolation.reset(new GeomAPI_Interpolate(interpolationPoints, PyObject_IsTrue(periodic) ? Standard_True : Standard_False, tol3d)); } else { aBSplineInterpolation.reset(new GeomAPI_Interpolate(interpolationPoints, parameters, PyObject_IsTrue(periodic) ? Standard_True : Standard_False, tol3d)); } if (t1 && t2) { Base::Vector3d v1 = Py::Vector(t1,false).toVector(); Base::Vector3d v2 = Py::Vector(t2,false).toVector(); gp_Vec initTangent(v1.x,v1.y,v1.z), finalTangent(v2.x,v2.y,v2.z); aBSplineInterpolation->Load(initTangent, finalTangent, PyObject_IsTrue(scale) ? Standard_True : Standard_False); } else if (ts && fl) { Py::Sequence tlist(ts); TColgp_Array1OfVec tangents(1, tlist.size()); Standard_Integer index = 1; for (Py::Sequence::iterator it = tlist.begin(); it != tlist.end(); ++it) { Py::Vector v(*it); Base::Vector3d vec = v.toVector(); tangents.SetValue(index++, gp_Vec(vec.x,vec.y,vec.z)); } Py::Sequence flist(fl); Handle_TColStd_HArray1OfBoolean tangentFlags = new TColStd_HArray1OfBoolean(1, flist.size()); Standard_Integer findex = 1; for (Py::Sequence::iterator it = flist.begin(); it != flist.end(); ++it) { Py::Boolean flag(*it); tangentFlags->SetValue(findex++, static_cast<bool>(flag) ? Standard_True : Standard_False); } aBSplineInterpolation->Load(tangents, tangentFlags, PyObject_IsTrue(scale) ? Standard_True : Standard_False); } aBSplineInterpolation->Perform(); if (aBSplineInterpolation->IsDone()) { Handle_Geom_BSplineCurve aBSplineCurve(aBSplineInterpolation->Curve()); this->getGeomBSplineCurvePtr()->setHandle(aBSplineCurve); Py_Return; } else { Standard_Failure::Raise("failed to interpolate points"); return 0; // goes to the catch block } } catch (Standard_Failure) { Handle_Standard_Failure e = Standard_Failure::Caught(); std::string err = e->GetMessageString(); if (err.empty()) err = e->DynamicType()->Name(); PyErr_SetString(PartExceptionOCCError, err.c_str()); return 0; } }