[PYTHON] 3D or D in Py Thon de ~ Spines r Fase ~

Create a Spline Surface with pyhonocc-core (Open CASCADE). Then spit it out in STEP.

Surface generation code

The code is this.

The red Surface and the blue Surface have the same shape, but the location is changed using gp_Ax3.

qiita_002-1.png

Only the important part is excerpted.

def spl_face(px, py, pz, axs=gp_Ax3()):
    nx, ny = px.shape
    pnt_2d = TColgp_Array2OfPnt(1, nx, 1, ny)
    for row in range(pnt_2d.LowerRow(), pnt_2d.UpperRow() + 1):
        for col in range(pnt_2d.LowerCol(), pnt_2d.UpperCol() + 1):
            i, j = row - 1, col - 1
            pnt = gp_Pnt(px[i, j], py[i, j], pz[i, j])
            pnt_2d.SetValue(row, col, pnt)
            #print (i, j, px[i, j], py[i, j], pz[i, j])

    api = GeomAPI_PointsToBSplineSurface(pnt_2d, 3, 8, GeomAbs_G2, 0.001)
    api.Interpolate(pnt_2d)
    face = BRepBuilderAPI_MakeFace(api.Surface(), 1e-6).Face()
    face.Location(set_loc(gp_Ax3(), axs))
    return face

For px, py, pz, np.array of (n x n) is generated by np.meshgrid, and Grid data of an appropriate curved surface is input as pz.

After that, assign it to the 2D array of gp_Pnt of OpenCASCADE called TColgp_Array2OfPnt (for loop part) and feed it to GeomAPI_PointsToBSplineSurface.

GeomAPI_PointsToBSplineSurface is a function that actually generates a Spline Surface, but for details, see here Please.

The two numbers (3,8) after TColgp_Array2OfPnt are the minimum and maximum degrees of the approximate polynomial that produces the Spline. GeomAbs_G2 defines smoothness. There are also GeomAbs_C0, GeomAbs_C1, GeomAbs_C2, GeomAbs_G1 and so on. G2 is second-order differentiable. The final 0.001 is the maximum error in the approximation, and the Surface is generated so that the error between the input point cloud and the Surface is less than this value.

Try to enter an abnormal value for only one point

It is not interesting to put in clean point cloud data, so let's put an abnormal value in the generated data.

The code is here.

Only the data generation part is excerpted.

px = np.linspace(-1, 1, 100) * 100 + 50
py = np.linspace(-1, 1, 200) * 100 - 50
mesh = np.meshgrid(px, py)
data = mesh[0]**2 / 1000 + mesh[1]**2 / 2000
data[100, 50] = 100.0

Make the (200 x 100) 2D array a protruding shape with (100,50) roughly in the middle as 100.0.

qiita_002-2.png

The shape is abnormally raised around the center. As long as you use Spline, the shape of the surrounding area is deformed by being pulled by the protruding part. It is also the strength of Open CASCADE that even such an abnormal shape can be generated (whether it is correct or not).

Recommended Posts

3D or D in Py Thon de ~ Spines r Fase ~
3D or D with Py