Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use splines in pythonOCC?

I have a two-part question about how to use splines in pythonOCC.

Firstly, I know that I can create a spline with

array = []
array.append(gp_Pnt2d (0,0))
array.append(gp_Pnt2d (1,2))
array.append(gp_Pnt2d (2,3))
array.append(gp_Pnt2d (4,3))
array.append(gp_Pnt2d (5,5))

pt2d_list = point2d_list_to_TColgp_Array1OfPnt2d(array)
SPL1      = Geom2dAPI_PointsToBSpline(pt2d_list).Curve()
display.DisplayShape(make_edge2d(SPL1) , update=True)

And I expect that the bspline can be calculated by

BSPL1      = Geom2dAPI_PointsToBSpline(pt2d_list)

But how do I get:

  1. The derivative of the bspline?
  2. The knots of the bspline?
  3. Is the knots the pt2d_list?
  4. The control points of the bspline?
  5. The coefficients of the spline?

And how do I remove or add knots to the bspline?

Secondly, when loading a CAD drawing .stp file in pythonOCC like this:

from OCC import TopoDS, StlAPI
shape = TopoDS.TopoDS_Shape()
stl_reader = StlAPI.StlAPI_Reader()
stl_reader.Read(shape,str(filename))
display.DisplayShape(shape)

How do I get the data out of the shape like knot, bspline, and coefficients.

like image 846
Steenstrup Avatar asked Oct 14 '13 08:10

Steenstrup


1 Answers

I've used the python-boost do achieve this. Check out this function: http://letslearncomputing.blogspot.com/2013/04/c-program-for-cubic-spline-interpolation.html You can get here some of your desired 5 values.

You just need to modify the code to be C++ (not C)

BOOST_PYTHON_MODULE(Spline)
{
    import_array();
    boost::python::numeric::array::set_module_and_type("numpy", "ndarray");
    class_<Spline, Spline*>("Spline", init<>())
        .def("spline", &Spline::spline)
        ;
}

So in Python you can use:

from Spline.Spline import *
operation = Spline()
value, error_ = operation.spline(np.array(your_x_array), np.array(your_y_array), 0.01)

Cpp class:

#define NUMBER_OF_SAMPLES 14
class Spline
{
public:
    boost::python::list spline(numeric::array& x_val, numeric::array& y_val, double look_up_val);
};

Then in boost::python::list Spline::spline(numeric::array& x_val, numeric::array& y_val, double p) function you get:

PyArrayObject* x_pyArr = (PyArrayObject*)PyArray_FROM_O(x_val.ptr());

PyArrayObject* y_pyArr = (PyArrayObject*)PyArray_FROM_O(y_val.ptr());
int size = *(x_pyArr->dimensions), i , j;    
double* data_x = (double*)x_pyArr->data;
double* data_y = (double*)y_pyArr->data;
double h[NUMBER_OF_SAMPLES], a, b, c, d, s[NUMBER_OF_SAMPLES] = { 0 }, F[NUMBER_OF_SAMPLES], f[NUMBER_OF_SAMPLES], x[NUMBER_OF_SAMPLES], m[NUMBER_OF_SAMPLES][NUMBER_OF_SAMPLES] = { 0 }, temp;

for (int i = 0; i < size; i++)
{
    x[i] = *(data_x + i);
    f[i] = *(data_y + i);
}

And so on according to code in link. I return a python list in my Spline::spline function:

boost::python::list return_val;
// ....
return_val.append(sum);
return_val.append(result);
return return_val;
like image 173
Jagoda Gorus Avatar answered Oct 24 '22 13:10

Jagoda Gorus