├── curve2d ├── ex_curve02.cpt ├── ex_curve03.cpt ├── ex_curve01.cpt ├── ex_curve04.cptw ├── ex_curve05_clamped.py ├── ex_curve05_unclamped.py ├── ex_curve01.py ├── ex_curve04.py ├── ex_curve05_periodic.py ├── ex_curve01_knot_insert.py ├── ex_curve01_decompose.py ├── ex_curve04_degree.py ├── ex_curve04_convert3d.py ├── ex_curve04_decompose.py ├── ex_curve03_decompose.py ├── ex_curve01_split.py ├── ex_curve02_split.py ├── ex_curve02.py ├── ex_curve02_decompose.py ├── ex_curve03.py └── ex_curve01_refinement.py ├── bezier ├── images │ ├── Bezier01.png │ ├── Bezier02.png │ ├── Bezier03.png │ └── Bezier04.png ├── README.md ├── ex_bezier_curve3d.py ├── ex_bezier_surface.py └── ex_bezier_curve2d.py ├── surface ├── ex_cylinder_quarter.cptw ├── ex_cylinder_half.cptw ├── ex_surface02.cpt ├── ex_surface01.cpt ├── ex_cylinder.cptw ├── ex_cylinder_tmpl.cptw ├── ex_cylinder_quarter.py ├── ex_cylinder_half.py ├── ex_surface01.py ├── ex_torus.py ├── ex_surface01_decompose.py ├── ex_surface02.py ├── ex_surface01_split.py ├── ex_cylinder_decompose.py ├── ex_cylinder.py ├── ex_cylinder_tmpl.py ├── trimming │ ├── trim_single.py │ ├── trim_multi_sense1.py │ ├── trim_advanced.py │ ├── trim_multi_sense2.py │ ├── trim_multi_sense3.py │ ├── trim_separate_surfaces.py │ └── trim_multi.py ├── ex_surface01_split_multi.py ├── ex_surface01_rotation.py ├── ex_torus.cptw ├── ex_curved.py └── refinement │ └── ex_density.py ├── curve3d ├── ex_curve3d02.cpt ├── ex_curve3d01.cpt ├── ex_curve3d02.py ├── ex_curve3d01.py └── ex_curve3d01_split.py ├── isu ├── README.md └── ex_smesh.py ├── .gitignore ├── octave ├── README.md └── ex_curve02.m ├── exchange ├── ex_obj.py ├── ex_off.py ├── ex_stl.py ├── ex_vtk.py └── ex_vtk_multisurf.py ├── objects ├── duck1.weights ├── duck3.ctrlpts ├── duck2.ctrlpts ├── ducky.py └── duck1.ctrlpts ├── shapes ├── ex_cylinder.py ├── ex_circle_9cp.py └── ex_circle_7cp.py ├── grid ├── ex_grid01.py ├── ex_grid02.py ├── ex_surfgen02.py ├── ex_surfgen03.py └── ex_surfgen01.py ├── fitting ├── approximation │ ├── global_curve2d.py │ ├── global_curve3d.py │ ├── ex_curve3d.cpt │ └── global_surface.py └── interpolation │ ├── global_curve2d.py │ ├── global_curve3d.py │ ├── ex_curve3d.cpt │ └── global_surface.py ├── LICENSE ├── compat └── ex_convert01.py ├── README.rst ├── volume ├── ex_knot_removal.py ├── ex_knot_insertion2.py ├── ex_knot_insertion1.py ├── ex_planar_bspline.py └── ex_knot_refinement.py └── visualization ├── mpl_curve2d_tangents.py ├── mpl_curve3d_tangents.py ├── mpl_trisurf_vectors.py └── mpl_curve3d_vectors.py /curve2d/ex_curve02.cpt: -------------------------------------------------------------------------------- 1 | 05,05 2 | 10,20 3 | 20,35 4 | 35,35 5 | 45,20 6 | 50,05 7 | -------------------------------------------------------------------------------- /bezier/images/Bezier01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbingol/geomdl-examples/HEAD/bezier/images/Bezier01.png -------------------------------------------------------------------------------- /bezier/images/Bezier02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbingol/geomdl-examples/HEAD/bezier/images/Bezier02.png -------------------------------------------------------------------------------- /bezier/images/Bezier03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbingol/geomdl-examples/HEAD/bezier/images/Bezier03.png -------------------------------------------------------------------------------- /bezier/images/Bezier04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orbingol/geomdl-examples/HEAD/bezier/images/Bezier04.png -------------------------------------------------------------------------------- /curve2d/ex_curve03.cpt: -------------------------------------------------------------------------------- 1 | 0,0 2 | 1,5 3 | 2,10 4 | 5,12 5 | 8,10 6 | 14,-10 7 | 16,-12 8 | 18,-10 9 | 22,0 10 | -------------------------------------------------------------------------------- /curve2d/ex_curve01.cpt: -------------------------------------------------------------------------------- 1 | 05,10 2 | 15,25 3 | 30,30 4 | 45,05 5 | 55,05 6 | 70,40 7 | 60,60 8 | 35,60 9 | 20,40 10 | -------------------------------------------------------------------------------- /surface/ex_cylinder_quarter.cptw: -------------------------------------------------------------------------------- 1 | 1.0,0.0,0.0,1.0;0.7071,0.7071,0.0,0.7071;0.0,1.0,0.0,1.0 2 | 1.0,0.0,1.0,1.0;0.7071,0.7071,0.7071,0.7071;0.0,1.0,1.0,1.0 3 | -------------------------------------------------------------------------------- /curve3d/ex_curve3d02.cpt: -------------------------------------------------------------------------------- 1 | 5,15,0 2 | 10,25,5 3 | 20,20,10 4 | 15,-5,15 5 | 7.5,10,20 6 | 12.5,15,25 7 | 15,0,30 8 | 5,-10,35 9 | 10,15,40 10 | 5,15,30 11 | -------------------------------------------------------------------------------- /curve2d/ex_curve04.cptw: -------------------------------------------------------------------------------- 1 | 0,-1,1 2 | -0.707,-0.707,0.707 3 | -1,0,1 4 | -0.707,0.707,0.707 5 | 0,1,1 6 | 0.707,0.707,0.707 7 | 1,0,1 8 | 0.707,-0.707,0.707 9 | 0,-1,1 10 | -------------------------------------------------------------------------------- /isu/README.md: -------------------------------------------------------------------------------- 1 | This directory contains some scripts which require some data in a special format that is used in Department of Mechanical Engineering at Iowa State University. 2 | -------------------------------------------------------------------------------- /curve3d/ex_curve3d01.cpt: -------------------------------------------------------------------------------- 1 | 5,5,0 2 | 5,10,0 3 | 10,10,5 4 | 10,5,5 5 | 5,5,5 6 | 5,10,10 7 | 10,10,10 8 | 10,5,10 9 | 5,5,15 10 | 5,10,15 11 | 10,10,15 12 | 10,5,20 13 | 5,5,20 14 | -------------------------------------------------------------------------------- /surface/ex_cylinder_half.cptw: -------------------------------------------------------------------------------- 1 | 1.0,0.0,0.0,1.0;0.7071,0.7071,0.0,0.7071;0.0,1.0,0.0,1.0;-0.7071,0.7071,0.0,0.7071;-1.0,0.0,0.0,1.0 2 | 1.0,0.0,1.0,1.0;0.7071,0.7071,0.7071,0.7071;0.0,1.0,1.0,1.0;-0.7071,0.7071,0.7071,0.7071;-1.0,0.0,1.0,1.0 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | .vscode/* 3 | examples/* 4 | .ipynb* 5 | *.pyc 6 | *.txt 7 | *.csv 8 | *.obj 9 | *.stl 10 | *.off 11 | *.html 12 | *.dat 13 | *.pdf 14 | *.cfg 15 | *.json 16 | *.smesh 17 | *.vmesh 18 | *.vtk 19 | *.yaml 20 | *.yml 21 | *.3dm 22 | -------------------------------------------------------------------------------- /octave/README.md: -------------------------------------------------------------------------------- 1 | This directory contains [GNU Octave](https://www.gnu.org/software/octave/) scripts which may be used to validate NURBS-Python results. 2 | 3 | Requirements: 4 | 5 | * GNU Octave v4.2.2 6 | * [nurbs 1.3.13](https://octave.sourceforge.io/nurbs/) 7 | -------------------------------------------------------------------------------- /surface/ex_surface02.cpt: -------------------------------------------------------------------------------- 1 | -25,-25,-5;-25,-15,0;-25,-5,0;-25,5,0;-25,15,0;-25,25,5 2 | -15,-25,0;-15,-15,0;-15,-5,0;-15,5,0;-15,15,1;-15,25,1 3 | -5,-25,5;-5,-15,5;-5,-5,5;-5,5,0;-5,15,0;-5,25,0 4 | 5,-25,0;5,-15,0;5,-5,0;5,5,-5;5,15,-5;5,25,-5 5 | 15,-25,0;15,-15,0;15,-5,0;15,5,0;15,15,-10;15,25,-10 6 | 25,-25,0;25,-15,0;25,-5,0;25,5,0;25,15,0;25,25,-5 7 | -------------------------------------------------------------------------------- /surface/ex_surface01.cpt: -------------------------------------------------------------------------------- 1 | -25,-25,-10;-25,-15,-5;-25,-5,0;-25,5,0;-25,15,-5;-25,25,-10 2 | -15,-25,-8;-15,-15,-4;-15,-5,-4;-15,5,-4;-15,15,-4;-15,25,-8 3 | -5,-25,-5;-5,-15,-3;-5,-5,-8;-5,5,-8;-5,15,-3;-5,25,-5 4 | 5,-25,-3;5,-15,-2;5,-5,-8;5,5,-8;5,15,-2;5,25,-3 5 | 15,-25,-8;15,-15,-4;15,-5,-4;15,5,-4;15,15,-4;15,25,-8 6 | 25,-25,-10;25,-15,-5;25,-5,2;25,5,2;25,15,-5;25,25,-10 7 | -------------------------------------------------------------------------------- /surface/ex_cylinder.cptw: -------------------------------------------------------------------------------- 1 | 1.0,0.0,0.0,1.0;0.7071,0.7071,0.0,0.7071;0.0,1.0,0.0,1.0;-0.7071,0.7071,0.0,0.7071;-1.0,0.0,0.0,1.0;-0.7071,-0.7071,0.0,0.7071;0.0,-1.0,0.0,1.0;0.7071,-0.7071,0.0,0.7071;1.0,0.0,0.0,1.0 2 | 1.0,0.0,1.0,1.0;0.7071,0.7071,0.7071,0.7071;0.0,1.0,1.0,1.0;-0.7071,0.7071,0.7071,0.7071;-1.0,0.0,1.0,1.0;-0.7071,-0.7071,0.7071,0.7071;0.0,-1.0,1.0,1.0;0.7071,-0.7071,0.7071,0.7071;1.0,0.0,1.0,1.0 3 | -------------------------------------------------------------------------------- /exchange/ex_obj.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | Exporting a NURBS surface as .obj file 10 | """ 11 | 12 | from geomdl.shapes import surface 13 | from geomdl import exchange 14 | 15 | cylinder = surface.cylinder(radius=5.0, height=22.5) 16 | 17 | # Export the surface as a .obj file 18 | exchange.export_obj(cylinder, "cylindrical_surface.obj") 19 | 20 | # Good to have something here to put a breakpoint 21 | pass 22 | -------------------------------------------------------------------------------- /exchange/ex_off.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | Exporting a NURBS surface as .off file 10 | """ 11 | 12 | from geomdl.shapes import surface 13 | from geomdl import exchange 14 | 15 | cylinder = surface.cylinder(radius=5.0, height=22.5) 16 | 17 | # Export the surface as a .off file 18 | exchange.export_off(cylinder, "cylindrical_surface.off") 19 | 20 | # Good to have something here to put a breakpoint 21 | pass 22 | -------------------------------------------------------------------------------- /exchange/ex_stl.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | Exporting a NURBS surface as .stl file 10 | """ 11 | 12 | from geomdl.shapes import surface 13 | from geomdl import exchange 14 | 15 | cylinder = surface.cylinder(radius=5.0, height=22.5) 16 | 17 | # Export the surface as a .stl file 18 | exchange.export_stl(cylinder, "cylindrical_surface.stl") 19 | 20 | # Good to have something here to put a breakpoint 21 | pass 22 | -------------------------------------------------------------------------------- /objects/duck1.weights: -------------------------------------------------------------------------------- 1 | 1,1,1,1, 2 | 1,1,1,1, 3 | 1,1,1,1, 4 | 1,1,1,1, 5 | 1,1,1,1, 6 | 1,1,1,1, 7 | 1,1,1,1, 8 | 1,1,1,1, 9 | 1,1,1,1, 10 | 1,1,1,1, 11 | 1,1,1,1, 12 | 1,1,1,1, 13 | 1,1,1,1, 14 | 1,1,1,1, 15 | 1,1,1,1, 16 | 1,1,1,1, 17 | 1,1,1,1, 18 | 0.5,1,1,1, 19 | 1,1,0.5,1, 20 | 1,1,1,1, 21 | 1,1,1,1, 22 | 1,1,1,1, 23 | 1,1,1,1, 24 | 1,1,1,1, 25 | 1,1,1,1, 26 | 1,1,1,1, 27 | 1,1,1,1, 28 | 1,1,1,1, 29 | 1,1,1,1, 30 | 1,1,1,1, 31 | 1,1,1,1, 32 | 1,1,1,1, 33 | 1,1,1,1, 34 | 1,1,1,1, 35 | 1,1,1,1, 36 | 1,1,1,1, 37 | 1,1,1,1, 38 | 1,1,1,1, 39 | 1,1,1,1, 40 | 1,1,1,1, 41 | 1,1,1,1, 42 | 1,1,1,1, 43 | 1,1,1,1, 44 | 1,1,1,1, 45 | 1,1,1,1, 46 | 1,1 47 | -------------------------------------------------------------------------------- /shapes/ex_cylinder.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | from geomdl.shapes import surface 10 | from geomdl.visualization import VisMPL 11 | 12 | 13 | # Generate a cylindrical surface using the shapes component 14 | cylinder = surface.cylinder(radius=5.0, height=22.5) 15 | cylinder.sample_size = 20 16 | 17 | # Render the surface 18 | vis_config = VisMPL.VisConfig(ctrlpts=True) 19 | vis_comp = VisMPL.VisSurface(config=vis_config) 20 | cylinder.vis = vis_comp 21 | cylinder.render() 22 | 23 | # Good to have something here to put a breakpoint 24 | pass 25 | -------------------------------------------------------------------------------- /shapes/ex_circle_9cp.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | from geomdl.shapes import curve2d 11 | from geomdl.visualization import VisMPL 12 | 13 | 14 | # Generate a NURBS full circle from 9 control points 15 | circle = curve2d.full_circle(radius=5.0) 16 | circle.sample_size = 50 17 | 18 | # Render the circle and the control points polygon 19 | vis_config = VisMPL.VisConfig(ctrlpts=True, figure_size=[8, 8]) 20 | vis_comp = VisMPL.VisCurve2D(config=vis_config) 21 | circle.vis = vis_comp 22 | circle.render() 23 | 24 | # Good to have something here to put a breakpoint 25 | pass 26 | -------------------------------------------------------------------------------- /surface/ex_cylinder_tmpl.cptw: -------------------------------------------------------------------------------- 1 | 1.0,0.0,0.0,1.0;{{ 1 / sqrt(2) }},{{ 1.0 / sqrt(2) }},0.0,{{ 1 / sqrt(2) }};0.0,1.0,0.0,1.0;{{ -1.0 / sqrt(2) }},{{ 1.0 / sqrt(2) }},0.0,{{ 1.0 / sqrt(2) }};-1.0,0.0,0.0,1.0;{{ -1.0 / sqrt(2) }},{{ -1.0 / sqrt(2) }},0.0,{{ 1.0 / sqrt(2) }};0.0,-1.0,0.0,1.0;{{ 1.0 / sqrt(2) }},{{ -1.0 / sqrt(2) }},0.0,{{ 1.0 / sqrt(2) }};1.0,0.0,0.0,1.0 2 | 1.0,0.0,1.0,1.0;{{ 1.0 / sqrt(2) }},{{ 1.0 / sqrt(2) }},{{ 1.0 / sqrt(2) }},{{ 1.0 / sqrt(2) }};0.0,1.0,1.0,1.0;{{ -1.0 / sqrt(2) }},{{ 1.0 / sqrt(2) }},{{ 1.0 / sqrt(2) }},{{ 1.0 / sqrt(2) }};-1.0,0.0,1.0,1.0;{{ -1.0 / sqrt(2) }},{{ -1.0 / sqrt(2) }},{{ 1.0 / sqrt(2) }},{{ 1.0 / sqrt(2) }};0.0,-1.0,1.0,1.0;{{ 1.0 / sqrt(2) }},{{ -1.0 / sqrt(2) }},{{ 1.0 / sqrt(2) }},{{ 1.0 / sqrt(2) }};1.0,0.0,1.0,1.0 3 | -------------------------------------------------------------------------------- /grid/ex_grid01.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2017 8 | """ 9 | import os 10 | from geomdl import CPGen 11 | 12 | # Fix file path 13 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 14 | 15 | # Generate a 50x100 rectangle 16 | mygrid = CPGen.Grid(50, 100) 17 | 18 | # Split the width into 5 equal pieces and the height into 10 equal pieces 19 | mygrid.generate(15, 20) 20 | 21 | # Generate 4 bumps on the grid 22 | mygrid.bumps(num_bumps=4, bump_height=50) 23 | 24 | # Get the grid points for plotting 25 | grid_data = mygrid.grid 26 | 27 | # Good to have something here to put a breakpoint 28 | pass 29 | -------------------------------------------------------------------------------- /grid/ex_grid02.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2017 8 | """ 9 | import os 10 | from geomdl import CPGen 11 | 12 | # Fix file path 13 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 14 | 15 | # Generate a 50x100 rectangle 16 | mygrid = CPGen.GridWeighted(50, 100) 17 | 18 | # Split the width into 5 equal pieces and the height into 10 equal pieces 19 | mygrid.generate(15, 20) 20 | 21 | # Generate 4 bumps on the grid 22 | mygrid.bumps(num_bumps=4, bump_height=50) 23 | 24 | # Add weight 25 | mygrid.weight = 2.3 26 | 27 | # Modify weight 28 | mygrid.weight = 1.0 29 | 30 | # Get the grid points for plotting 31 | grid_data = mygrid.grid 32 | 33 | # Good to have something here to put a breakpoint 34 | pass 35 | -------------------------------------------------------------------------------- /exchange/ex_vtk.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | 9 | Exporting a NURBS surface in VTK Polydata format 10 | """ 11 | 12 | from geomdl.shapes import surface 13 | from geomdl import exchange_vtk 14 | 15 | # Create a cylindrical surface 16 | cylinder = surface.cylinder(radius=5.0, height=22.5) 17 | 18 | # Export evaluated points as a .vtk file 19 | exchange_vtk.export_polydata(cylinder, "cylindrical_surface_evalpts.vtk", tessellate=True) 20 | 21 | # Export control points as a .vtk file (optionally tessellated as quads) 22 | exchange_vtk.export_polydata(cylinder, "cylindrical_surface_ctrlpts.vtk", point_type="ctrlpts", tessellate=False) 23 | 24 | # Good to have something here to put a breakpoint 25 | pass 26 | -------------------------------------------------------------------------------- /octave/ex_curve02.m: -------------------------------------------------------------------------------- 1 | # ex_curve02.m 2 | # Tested on Octave 4.2.2 with nurbs 1.3.13 3 | 4 | # Housekeeping 5 | clear all; 6 | 7 | # Load "nurbs" package (install it via "pkg install -forge nurbs") 8 | pkg load nurbs 9 | 10 | # Control points and knot vectors for a 3rd degree curve 11 | ctrlpts = [05 10 20 35 45 50; 05 20 35 35 20 05]; 12 | kv = [0.0, 0.0, 0.0, 0.0, 0.333333, 0.666667, 1.0, 1.0, 1.0, 1.0]; 13 | 14 | # Generate the NURBS curve 15 | crv = nrbmak(ctrlpts, kv); 16 | 17 | # Compute the 1st and 2nd derivative curves 18 | [ders1, ders2] = nrbderiv(crv); 19 | 20 | # Evaluate the derivatives 21 | t = 0.2; 22 | [p1, dp1] = nrbdeval(crv, ders1, t); 23 | [p2, dp2] = nrbdeval(crv, ders2, t); 24 | 25 | # Normalize derivative vectors 26 | dp1n = vecnorm(dp1); 27 | dp2n = vecnorm(dp2); 28 | 29 | # Display output 30 | fprintf('1st derivative vector: %s\n', sprintf('%f ', dp1n)) 31 | fprintf('2nd derivative vector: %s\n', sprintf('%f ', dp2n)) 32 | -------------------------------------------------------------------------------- /bezier/README.md: -------------------------------------------------------------------------------- 1 | # Bézier Curves and Surfaces 2 | 3 | This directory contains some Bézier surface and 2D/3D curve examples for NURBS-Python. Bézier curves and surfaces are 4 | considered as a subset of NURBS and therefore, they can be generated using NURBS algorithms. 5 | 6 | Please check the scripts in this directory on details of how to generate Bézier curves and surfaces. 7 | 8 | ## 2D Curve Examples 9 | 10 | The following examples are generated using `ex_bezier_curve2d.py` script. 11 | 12 | ![2D curve with degree 2](images/Bezier01.png) 13 | 14 | ![2D curve with degree 3](images/Bezier02.png) 15 | 16 | ## 3D Curve Example 17 | 18 | The following example is generated using `ex_bezier_curve3d.py` script. 19 | 20 | ![3D curve with degree 3](images/Bezier03.png) 21 | 22 | ## Surface Example 23 | 24 | The following example is generated using `ex_bezier_surface.py` script. 25 | 26 | ![Surface](images/Bezier04.png) 27 | 28 | ## License 29 | 30 | [MIT](../LICENSE) 31 | -------------------------------------------------------------------------------- /exchange/ex_vtk_multisurf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | 9 | Exporting multiple NURBS surfaces in VTK Polydata format 10 | """ 11 | 12 | from geomdl.shapes import surface 13 | from geomdl import operations 14 | from geomdl import multi 15 | from geomdl import exchange_vtk 16 | 17 | # Create a cylindrical surface 18 | cylinder = surface.cylinder(radius=5.0, height=22.5) 19 | 20 | # Decompose to generate multiple surfaces 21 | cylinder_decomposed = operations.decompose_surface(cylinder) 22 | 23 | # Add decomposed surfaces to a surface container 24 | surf_container = multi.SurfaceContainer(cylinder_decomposed) 25 | 26 | # Export evaluated points as a .vtk file 27 | exchange_vtk.export_polydata(surf_container, "cylindrical_surface_decomposed_evalpts.vtk", tessellate=True) 28 | 29 | # Good to have something here to put a breakpoint 30 | pass 31 | -------------------------------------------------------------------------------- /fitting/approximation/global_curve2d.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | 2-dimensional curve fitting by global approximation 10 | """ 11 | 12 | from geomdl import fitting 13 | from geomdl.visualization import VisMPL as vis 14 | 15 | 16 | # The NURBS Book Ex9.1 17 | points = ((0, 0), (3, 4), (-1, 4), (-4, 0), (-4, -3)) 18 | degree = 3 # cubic curve 19 | 20 | # Do global curve approximation 21 | curve = fitting.approximate_curve(points, degree) 22 | 23 | # Plot the interpolated curve 24 | curve.delta = 0.01 25 | curve.vis = vis.VisCurve2D() 26 | curve.render() 27 | 28 | # # Visualize data and evaluated points together 29 | # import numpy as np 30 | # import matplotlib.pyplot as plt 31 | # evalpts = np.array(curve.evalpts) 32 | # pts = np.array(points) 33 | # plt.plot(evalpts[:, 0], evalpts[:, 1]) 34 | # plt.scatter(pts[:, 0], pts[:, 1], color="red") 35 | # plt.show() 36 | -------------------------------------------------------------------------------- /fitting/interpolation/global_curve2d.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | 2-dimensional curve fitting by global interpolation 10 | """ 11 | 12 | from geomdl import fitting 13 | from geomdl.visualization import VisMPL as vis 14 | 15 | 16 | # The NURBS Book Ex9.1 17 | points = ((0, 0), (3, 4), (-1, 4), (-4, 0), (-4, -3)) 18 | degree = 3 # cubic curve 19 | 20 | # Do global curve interpolation 21 | curve = fitting.interpolate_curve(points, degree) 22 | 23 | # Plot the interpolated curve 24 | curve.delta = 0.01 25 | curve.vis = vis.VisCurve2D() 26 | curve.render() 27 | 28 | # # Visualize data and evaluated points together 29 | # import numpy as np 30 | # import matplotlib.pyplot as plt 31 | # evalpts = np.array(curve.evalpts) 32 | # pts = np.array(points) 33 | # plt.plot(evalpts[:, 0], evalpts[:, 1]) 34 | # plt.scatter(pts[:, 0], pts[:, 1], color="red") 35 | # plt.show() 36 | -------------------------------------------------------------------------------- /isu/ex_smesh.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import exchange 12 | from geomdl import multi 13 | from geomdl.visualization import VisPlotly as vis 14 | 15 | 16 | # Fix file path 17 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 18 | 19 | # Read a directory containing mesh files 20 | data = exchange.import_smesh('data') 21 | 22 | # Generate a surface container for visualization 23 | multi_mesh = multi.SurfaceContainer(data) 24 | multi_mesh.sample_size = 5 25 | 26 | # Draw the control point grid and the evaluated surface 27 | vis_config = vis.VisConfig(ctrlpts=False, legend=False) 28 | vis_comp = vis.VisSurface(vis_config) 29 | multi_mesh.vis = vis_comp 30 | multi_mesh.render() 31 | 32 | # Save surfaces as a .obj file 33 | exchange.export_obj(multi_mesh, "multisurface01.obj") 34 | 35 | # Good to have something here to put a breakpoint 36 | pass 37 | -------------------------------------------------------------------------------- /curve2d/ex_curve05_clamped.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl.visualization import VisMPL 13 | 14 | 15 | # Fix file path 16 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 17 | 18 | # Create a BSpline (NUBS) curve instance 19 | curve = BSpline.Curve() 20 | 21 | # Set degree 22 | curve.degree = 2 23 | 24 | # Set control points for a periodic curve 25 | curve.ctrlpts = [[-1, 0], [-0.5, 0.5], [0.5, -0.5], [1, 0]] 26 | 27 | # Knot vector for clamped curve 28 | curve.knotvector = [2, 2, 2, 3, 4, 4, 4] 29 | 30 | # Set evaluation delta 31 | curve.delta = 0.01 32 | 33 | # Evaulate curve 34 | curve.evaluate() 35 | 36 | # Plot the control point polygon and the evaluated curve 37 | vis_comp = VisMPL.VisCurve2D() 38 | curve.vis = vis_comp 39 | curve.render() 40 | 41 | # Good to have something here to put a breakpoint 42 | pass 43 | -------------------------------------------------------------------------------- /curve2d/ex_curve05_unclamped.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import utilities 13 | from geomdl.visualization import VisMPL 14 | 15 | 16 | # Fix file path 17 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 18 | 19 | # Create a BSpline (NUBS) curve instance 20 | curve = BSpline.Curve() 21 | 22 | # Set degree 23 | curve.degree = 2 24 | 25 | # Set control points for a periodic curve 26 | curve.ctrlpts = [[-1, 0], [-0.5, 0.5], [0.5, -0.5], [1, 0]] 27 | 28 | # Knot vector for unclamped curve 29 | curve.knotvector = [0, 1, 2, 3, 4, 5, 6] 30 | 31 | # Set evaluation delta 32 | curve.delta = 0.01 33 | 34 | # Evaulate curve 35 | curve.evaluate() 36 | 37 | # Plot the control point polygon and the evaluated curve 38 | vis_comp = VisMPL.VisCurve2D() 39 | curve.vis = vis_comp 40 | curve.render() 41 | 42 | # Good to have something here to put a breakpoint 43 | pass 44 | -------------------------------------------------------------------------------- /curve2d/ex_curve01.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2016-2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import utilities 13 | from geomdl import exchange 14 | from geomdl.visualization import VisMPL 15 | 16 | 17 | # Fix file path 18 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 19 | 20 | # Create a B-Spline curve instance 21 | curve = BSpline.Curve() 22 | 23 | # Set up the curve 24 | curve.degree = 4 25 | curve.ctrlpts = exchange.import_txt("ex_curve01.cpt") 26 | 27 | # Auto-generate knot vector 28 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 29 | 30 | # Set evaluation delta 31 | curve.delta = 0.01 32 | 33 | # Evaluate curve 34 | curve.evaluate() 35 | 36 | # Plot the control point polygon and the evaluated curve 37 | vis_comp = VisMPL.VisCurve2D() 38 | curve.vis = vis_comp 39 | curve.render() 40 | 41 | # Good to have something here to put a breakpoint 42 | pass 43 | -------------------------------------------------------------------------------- /curve2d/ex_curve04.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2017 8 | """ 9 | 10 | import os 11 | from geomdl import NURBS 12 | from geomdl import exchange 13 | # from geomdl.visualization import VisMPL 14 | from geomdl.visualization import VisPlotly 15 | 16 | 17 | # Fix file path 18 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 19 | 20 | # Create a NURBS curve instance (full circle) 21 | curve = NURBS.Curve() 22 | 23 | # Set up curve 24 | curve.degree = 2 25 | curve.ctrlptsw = exchange.import_txt("ex_curve04.cptw") 26 | 27 | # Use a specialized knot vector 28 | curve.knotvector = [0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1] 29 | 30 | # Set evaluation delta 31 | curve.delta = 0.01 32 | 33 | # Evaluate curve 34 | curve.evaluate() 35 | 36 | # Plot the control point polygon and the evaluated curve 37 | vis_comp = VisPlotly.VisCurve2D() 38 | curve.vis = vis_comp 39 | curve.render() 40 | 41 | # Good to have something here to put a breakpoint 42 | pass 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016-2019 Onur Rauf Bingol 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /surface/ex_cylinder_quarter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import NURBS 12 | from geomdl import exchange 13 | from geomdl.visualization import VisMPL as vis 14 | 15 | 16 | # Fix file path 17 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 18 | 19 | # Create a NURBS surface instance 20 | surf = NURBS.Surface() 21 | 22 | # Set degrees 23 | surf.degree_u = 1 24 | surf.degree_v = 2 25 | 26 | # Set control points 27 | surf.set_ctrlpts(*exchange.import_txt("ex_cylinder_quarter.cptw", two_dimensional=True)) 28 | 29 | # Set knot vectors 30 | surf.knotvector_u = [0, 0, 1, 1] 31 | surf.knotvector_v = [0, 0, 0, 1, 1, 1] 32 | 33 | # Set evaluation delta 34 | surf.delta = 0.05 35 | 36 | # Evaluate surface 37 | surf.evaluate() 38 | 39 | # Plot the control point grid and the evaluated surface 40 | vis_comp = vis.VisSurface() 41 | surf.vis = vis_comp 42 | surf.render() 43 | 44 | # Good to have something here to put a breakpoint 45 | pass 46 | -------------------------------------------------------------------------------- /objects/duck3.ctrlpts: -------------------------------------------------------------------------------- 1 | 0.697749 -0.482282 1.72233 2 | 0.697749 -0.551961 1.72629 3 | 0.497736 -0.357714 1.40084 4 | 0.497736 0.429409 1.40084 5 | 0.697749 0.623656 1.72629 6 | 0.697749 0.553978 1.72233 7 | 0.275414 -0.310712 1.69077 8 | 0.275414 -0.350082 1.66688 9 | 0.25645 -0.222548 1.48535 10 | 0.25645 0.294244 1.48535 11 | 0.275414 0.421778 1.66688 12 | 0.275414 0.382408 1.69077 13 | 0.146088 -0.237558 1.70237 14 | 0.120839 -0.266714 1.66702 15 | 0.0189557 -0.269827 1.5816 16 | 0.0189557 0.341523 1.5816 17 | 0.120839 0.33841 1.66702 18 | 0.146088 0.309254 1.70237 19 | -0.0870671 -0.223076 1.68462 20 | -0.117868 -0.254292 1.66378 21 | -0.315615 -0.227541 1.69583 22 | -0.315615 0.299237 1.69583 23 | -0.117868 0.325988 1.66378 24 | -0.0870671 0.294772 1.68462 25 | -0.231993 -0.185237 1.74138 26 | -0.280097 -0.23448 1.73119 27 | -0.380927 -0.0267278 1.75395 28 | -0.380927 0.0984238 1.75395 29 | -0.280097 0.306176 1.73119 30 | -0.231993 0.256933 1.74138 31 | -0.274698 -0.102977 1.76242 32 | -0.299816 -0.0724353 1.77409 33 | -0.307722 -0.000246432 1.77549 34 | -0.307722 0.0719424 1.77549 35 | -0.299816 0.144131 1.77409 36 | -0.274698 0.174673 1.76242 37 | -------------------------------------------------------------------------------- /surface/ex_cylinder_half.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import NURBS 12 | from geomdl import exchange 13 | from geomdl.visualization import VisMPL as vis 14 | 15 | 16 | # Fix file path 17 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 18 | 19 | # Create a NURBS surface instance 20 | surf = NURBS.Surface() 21 | 22 | # Set degrees 23 | surf.degree_u = 1 24 | surf.degree_v = 2 25 | 26 | # Set control points 27 | surf.set_ctrlpts(*exchange.import_txt("ex_cylinder_half.cptw", two_dimensional=True)) 28 | 29 | # Set knot vectors 30 | surf.knotvector_u = [0, 0, 1, 1] 31 | surf.knotvector_v = [0, 0, 0, 0.5, 0.5, 1, 1, 1] 32 | 33 | # Set evaluation delta 34 | surf.delta = 0.05 35 | 36 | # Evaluate surface 37 | surf.evaluate() 38 | 39 | # Plot the control point grid and the evaluated surface 40 | vis_comp = vis.VisSurface() 41 | surf.vis = vis_comp 42 | surf.render() 43 | 44 | # Good to have something here to put a breakpoint 45 | pass 46 | -------------------------------------------------------------------------------- /curve2d/ex_curve05_periodic.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import utilities 13 | from geomdl.visualization import VisMPL 14 | 15 | 16 | # Fix file path 17 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 18 | 19 | # Create a BSpline (NUBS) curve instance 20 | curve = BSpline.Curve() 21 | 22 | # Set degree 23 | curve.degree = 2 24 | 25 | # Set control points for a periodic curve 26 | curve.ctrlpts = [[1, 0], [-1, 0], [-1.5, 1.5], [1.5, -1.5], [1, 0], [-1, 0]] 27 | 28 | # Knot vector 29 | curve.knotvector = [0, 1, 2, 3, 4, 5, 6, 7, 8] 30 | 31 | # Set evaluation delta 32 | curve.sample_size = 100 33 | 34 | # Evaluate curve 35 | curve.evaluate() 36 | 37 | # Draw the control point polygon and the evaluated curve 38 | vis_config = VisMPL.VisConfig(ctrlpts=False, legend=False) 39 | vis_comp = VisMPL.VisCurve2D(vis_config) 40 | curve.vis = vis_comp 41 | curve.render() 42 | 43 | # Good to have something here to put a breakpoint 44 | pass 45 | -------------------------------------------------------------------------------- /curve3d/ex_curve3d02.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | 3-dimensional B-Spline curve 10 | """ 11 | 12 | import os 13 | from geomdl import BSpline 14 | from geomdl import utilities 15 | from geomdl import exchange 16 | # from geomdl.visualization import VisMPL 17 | from geomdl.visualization import VisPlotly 18 | 19 | 20 | # Fix file path 21 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 22 | 23 | # Create a B-Spline curve instance 24 | curve = BSpline.Curve() 25 | 26 | # Set up curve 27 | curve.degree = 3 28 | curve.ctrlpts = exchange.import_txt("ex_curve3d02.cpt") 29 | 30 | # Auto-generate knot vector 31 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 32 | 33 | # Set evaluation delta 34 | curve.delta = 0.01 35 | 36 | # Evaluate curve 37 | curve.evaluate() 38 | 39 | # Plot the control point polygon and the evaluated curve 40 | vis_comp = VisPlotly.VisCurve3D() 41 | curve.vis = vis_comp 42 | curve.render() 43 | 44 | # Good to have something here to put a breakpoint 45 | pass 46 | -------------------------------------------------------------------------------- /fitting/interpolation/global_curve3d.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | 3-dimensional curve fitting by global interpolation 10 | """ 11 | 12 | from geomdl import fitting 13 | from geomdl import exchange 14 | from geomdl.visualization import VisMPL as vis 15 | 16 | # Read data points from a .csv file (ref: https://github.com/idealab-isu/de-la-mo) 17 | points = exchange.import_csv("ex_curve3d.cpt") 18 | 19 | # Do global curve interpolation 20 | curve = fitting.interpolate_curve(points, degree=3) 21 | 22 | # Plot the interpolated curve 23 | curve.delta = 0.005 24 | curve.vis = vis.VisCurve3D(vis.VisConfig(ctrlpts=False)) 25 | curve.render() 26 | 27 | # # Visualize data and evaluated points together 28 | # import numpy as np 29 | # import matplotlib.pyplot as plt 30 | # evalpts = np.array(curve.evalpts) 31 | # datapts = np.array(points) 32 | # fig = plt.figure() 33 | # ax = plt.axes(projection='3d') 34 | # ax.plot(evalpts[:, 0], evalpts[:, 1], evalpts[:, 2]) 35 | # ax.scatter(datapts[:, 0], datapts[:, 1], datapts[:, 2], color="red") 36 | # plt.show() 37 | -------------------------------------------------------------------------------- /bezier/ex_bezier_curve3d.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | 3rd degree 3-dimensional Bezier curve 10 | """ 11 | 12 | import os 13 | from geomdl import BSpline 14 | from geomdl import utilities 15 | from geomdl.visualization import VisMPL 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a 3D B-Spline curve instance (Bezier Curve) 22 | curve = BSpline.Curve() 23 | 24 | # Set up the Bezier curve 25 | curve.degree = 3 26 | curve.ctrlpts = [[10, 5, 10], [10, 20, -30], [40, 10, 25], [-10, 5, 0]] 27 | 28 | # Auto-generate knot vector 29 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 30 | 31 | # Set evaluation delta 32 | curve.sample_size = 40 33 | 34 | # Evaluate curve 35 | curve.evaluate() 36 | 37 | # Draw the control point polygon and the evaluated curve 38 | vis_comp = VisMPL.VisCurve3D() 39 | curve.vis = vis_comp 40 | 41 | # Don't pop up the plot window, instead save it as a PDF file 42 | curve.render(filename="bezier.pdf", plot=False) 43 | 44 | pass 45 | -------------------------------------------------------------------------------- /curve2d/ex_curve01_knot_insert.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import utilities 13 | from geomdl import exchange 14 | from geomdl.visualization import VisMPL 15 | 16 | 17 | # Fix file path 18 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 19 | 20 | # Create a B-Spline curve instance 21 | curve = BSpline.Curve() 22 | 23 | # Set up the curve 24 | curve.degree = 4 25 | curve.ctrlpts = exchange.import_txt("ex_curve01.cpt") 26 | 27 | # Auto-generate knot vector 28 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 29 | 30 | # Set evaluation delta 31 | curve.delta = 0.01 32 | 33 | # Evaluate curve 34 | curve.evaluate() 35 | 36 | # Plot the control point polygon and the evaluated curve 37 | vis_comp = VisMPL.VisCurve2D() 38 | curve.vis = vis_comp 39 | curve.render() 40 | 41 | # Insert knot 42 | curve.insert_knot(0.1) 43 | 44 | # Plot the curve after knot insertion 45 | curve.render() 46 | 47 | # Good to have something here to put a breakpoint 48 | pass 49 | -------------------------------------------------------------------------------- /curve2d/ex_curve01_decompose.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | import os 10 | from geomdl import BSpline 11 | from geomdl import utilities 12 | from geomdl import exchange 13 | from geomdl import operations 14 | from geomdl import multi 15 | from geomdl.visualization import VisMPL 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a B-Spline curve instance 22 | curve = BSpline.Curve() 23 | 24 | # Set up the curve 25 | curve.degree = 4 26 | curve.ctrlpts = exchange.import_txt("ex_curve01.cpt") 27 | 28 | # Auto-generate knot vector 29 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 30 | 31 | # Decompose the curve into Bezier curve segments 32 | bezier_curves = operations.decompose_curve(curve) 33 | bezier = multi.CurveContainer(bezier_curves) 34 | 35 | # Plot the curves using the curve container 36 | bezier.sample_size = 40 37 | vis_comp = VisMPL.VisCurve2D() 38 | bezier.vis = vis_comp 39 | bezier.render() 40 | 41 | # Good to have something here to put a breakpoint 42 | pass 43 | -------------------------------------------------------------------------------- /fitting/approximation/global_curve3d.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | 3-dimensional curve fitting by global approximation using least squares method 10 | """ 11 | 12 | from geomdl import fitting 13 | from geomdl import exchange 14 | from geomdl.visualization import VisMPL as vis 15 | 16 | # Read data points from a .csv file (ref: https://github.com/idealab-isu/de-la-mo) 17 | points = exchange.import_csv("ex_curve3d.cpt") 18 | 19 | # Do global curve interpolation 20 | curve = fitting.approximate_curve(points, degree=2, ctrlpts_size=30) 21 | 22 | # Plot the interpolated curve 23 | curve.delta = 0.005 24 | curve.vis = vis.VisCurve3D(vis.VisConfig(ctrlpts=False)) 25 | curve.render() 26 | 27 | # # Visualize data and evaluated points together 28 | # import numpy as np 29 | # import matplotlib.pyplot as plt 30 | # evalpts = np.array(curve.evalpts) 31 | # datapts = np.array(points) 32 | # fig = plt.figure() 33 | # ax = plt.axes(projection='3d') 34 | # ax.plot(evalpts[:, 0], evalpts[:, 1], evalpts[:, 2]) 35 | # ax.scatter(datapts[:, 0], datapts[:, 1], datapts[:, 2], color="red") 36 | # plt.show() 37 | -------------------------------------------------------------------------------- /curve2d/ex_curve04_degree.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2017-2019 8 | """ 9 | 10 | import os 11 | from geomdl import NURBS 12 | from geomdl import exchange 13 | from geomdl import operations 14 | from geomdl.visualization import VisMPL 15 | 16 | 17 | # Fix file path 18 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 19 | 20 | # Create a NURBS curve instance (full circle) 21 | curve = NURBS.Curve() 22 | 23 | # Set up curve 24 | curve.degree = 2 25 | curve.ctrlptsw = exchange.import_txt("ex_curve04.cptw") 26 | 27 | # Use a specialized knot vector 28 | curve.knotvector = [0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1] 29 | 30 | # Set evaluation delta 31 | curve.delta = 0.01 32 | 33 | # Evaluate curve 34 | curve.evaluate() 35 | 36 | # Plot the control point polygon and the evaluated curve 37 | curve.vis = VisMPL.VisCurve2D() 38 | curve.render() 39 | 40 | # Degree elevation 41 | operations.degree_operations(curve, [1]) 42 | curve.render() 43 | 44 | # Degree reduction 45 | operations.degree_operations(curve, [-1]) 46 | curve.render() 47 | 48 | # Good to have something here to put a breakpoint 49 | pass 50 | -------------------------------------------------------------------------------- /curve2d/ex_curve04_convert3d.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import NURBS 12 | from geomdl import exchange 13 | from geomdl import operations 14 | from geomdl.visualization import VisMPL 15 | 16 | 17 | # Fix file path 18 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 19 | 20 | # Create a NURBS curve instance (full circle) 21 | curve = NURBS.Curve() 22 | 23 | # Set up curve 24 | curve.degree = 2 25 | curve.ctrlptsw = exchange.import_txt("ex_curve04.cptw") 26 | # Use a specialized knot vector 27 | curve.knotvector = [0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1] 28 | 29 | # Convert to a 3D curve 30 | curve3d = operations.add_dimension(curve) 31 | 32 | # Translate curve to z = 5 33 | operations.translate(curve3d, (0, 0, 5)) 34 | 35 | # Set evaluation delta 36 | curve3d.delta = 0.01 37 | 38 | # Plot the control point polygon and the evaluated curve 39 | vis_config = VisMPL.VisConfig(ctrlpts=True) 40 | vis_comp = VisMPL.VisCurve3D(vis_config) 41 | curve3d.vis = vis_comp 42 | curve3d.render() 43 | 44 | # Good to have something here to put a breakpoint 45 | pass 46 | -------------------------------------------------------------------------------- /curve2d/ex_curve04_decompose.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2017 8 | """ 9 | 10 | import os 11 | from geomdl import NURBS 12 | from geomdl import exchange 13 | from geomdl import operations 14 | from geomdl import multi 15 | from geomdl.visualization import VisMPL 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a NURBS curve instance (full circle) 22 | curve = NURBS.Curve() 23 | 24 | # Set up the curve 25 | curve.degree = 2 26 | curve.ctrlptsw = exchange.import_txt("ex_curve04.cptw") 27 | 28 | # Use a specialized knot vector 29 | curve.knotvector = [0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1] 30 | 31 | # Decompose the curve into Bezier curve segments 32 | curve_list = operations.decompose_curve(curve) 33 | bezier = multi.CurveContainer(curve_list) 34 | 35 | # Set sample size of the Bezier curves 36 | bezier.sample_size = 25 37 | 38 | # Plot the Bezier curves 39 | vis_config = VisMPL.VisConfig(figure_size=[8, 8]) 40 | vis_comp = VisMPL.VisCurve2D(vis_config) 41 | bezier.vis = vis_comp 42 | bezier.render() 43 | 44 | # Good to have something here to put a breakpoint 45 | pass 46 | -------------------------------------------------------------------------------- /shapes/ex_circle_7cp.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | from geomdl.shapes import curve2d 11 | from geomdl import operations 12 | from geomdl import multi 13 | from geomdl.visualization import VisMPL 14 | 15 | 16 | # Generate a NURBS full circle from 7 control points 17 | circle = curve2d.full_circle2(radius=5.0) 18 | circle.sample_size = 75 19 | 20 | # Render the circle and the control points polygon 21 | vis_config = VisMPL.VisConfig(ctrlpts=True, figure_size=[9, 8]) 22 | vis_comp = VisMPL.VisCurve2D(config=vis_config) 23 | circle.vis = vis_comp 24 | circle.render() 25 | 26 | # Decompose the circle into Bezier curve segments 27 | segments = operations.decompose_curve(circle) 28 | bezier_segments = multi.CurveContainer(segments) 29 | 30 | # Set sample size (delta) 31 | bezier_segments.sample_size = 25 32 | 33 | # Render the Bezier curve segments and their control points polygons 34 | vis_config = VisMPL.VisConfig(ctrlpts=True, figure_size=[9, 8]) 35 | vis_comp = VisMPL.VisCurve2D(config=vis_config) 36 | bezier_segments.vis = vis_comp 37 | bezier_segments.render() 38 | 39 | # Good to have something here to put a breakpoint 40 | pass 41 | -------------------------------------------------------------------------------- /surface/ex_surface01.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2016-2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import exchange 13 | from geomdl.visualization import VisVTK as vis 14 | 15 | 16 | # Fix file path 17 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 18 | 19 | # Create a BSpline surface instance 20 | surf = BSpline.Surface() 21 | 22 | # Set degrees 23 | surf.degree_u = 3 24 | surf.degree_v = 3 25 | 26 | # Set control points 27 | surf.set_ctrlpts(*exchange.import_txt("ex_surface01.cpt", two_dimensional=True)) 28 | 29 | # Set knot vectors 30 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 31 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 32 | 33 | # Set evaluation delta 34 | surf.delta = 0.025 35 | 36 | # Evaluate surface points 37 | surf.evaluate() 38 | 39 | # Import and use Matplotlib's colormaps 40 | from matplotlib import cm 41 | 42 | # Plot the control point grid and the evaluated surface 43 | vis_comp = vis.VisSurface() 44 | surf.vis = vis_comp 45 | surf.render(colormap=cm.cool) 46 | 47 | # Good to have something here to put a breakpoint 48 | pass 49 | -------------------------------------------------------------------------------- /curve3d/ex_curve3d01.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | 3-dimensional B-Spline curve 10 | """ 11 | 12 | import os 13 | from geomdl import BSpline 14 | from geomdl import utilities 15 | from geomdl import exchange 16 | 17 | from geomdl.visualization import VisMPL 18 | 19 | 20 | # Fix file path 21 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 22 | 23 | # Create a B-Spline curve instance 24 | curve = BSpline.Curve() 25 | 26 | # Set up curve 27 | curve.degree = 4 28 | curve.ctrlpts = exchange.import_txt("ex_curve3d01.cpt") 29 | 30 | # Auto-generate knot vector 31 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 32 | 33 | # Set evaluation delta 34 | curve.delta = 0.001 35 | 36 | # Evaluate curve 37 | curve.evaluate() 38 | 39 | # Plot the control point polygon and the evaluated curve 40 | vis_comp = VisMPL.VisCurve3D() 41 | curve.vis = vis_comp 42 | curve.render() 43 | 44 | # Insert a knot 45 | u = 0.2 46 | curve.insert_knot(u) 47 | 48 | # Draw the control point polygon and the evaluated curve after knot insertion 49 | curve.render() 50 | 51 | # Good to have something here to put a breakpoint 52 | pass 53 | -------------------------------------------------------------------------------- /curve2d/ex_curve03_decompose.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2016-2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import exchange 13 | from geomdl import operations 14 | from geomdl import multi 15 | # from geomdl.visualization import VisMPL as vis 16 | from geomdl.visualization import VisPlotly as vis 17 | 18 | 19 | # Fix file path 20 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 21 | 22 | # Create a BSpline curve instance 23 | curve = BSpline.Curve() 24 | 25 | # Set degree 26 | curve.degree = 3 27 | 28 | # Read control points from a file 29 | curve.ctrlpts = exchange.import_txt("ex_curve03.cpt") 30 | 31 | # Set knot vector 32 | curve.knotvector = [0, 0, 0, 0, 0.25, 0.25, 0.5, 0.75, 0.75, 1, 1, 1, 1] 33 | 34 | # Decompose the curve 35 | curve_list = operations.decompose_curve(curve) 36 | curve_decomposed = multi.CurveContainer(curve_list) 37 | 38 | # Set sample size for the decomposed curve 39 | curve_decomposed.sample_size = 25 40 | 41 | # Plot the decomposed curve 42 | vis_comp = vis.VisCurve2D() 43 | curve_decomposed.vis = vis_comp 44 | curve_decomposed.render() 45 | 46 | # Good to have something here to put a breakpoint 47 | pass 48 | -------------------------------------------------------------------------------- /bezier/ex_bezier_surface.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | A cubic-quadratic Bezier surface 10 | """ 11 | 12 | import os 13 | from geomdl import BSpline 14 | from geomdl import utilities 15 | from geomdl.visualization import VisMPL 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a BSpline surface instance (Bezier surface) 22 | surf = BSpline.Surface() 23 | 24 | # Set up the Bezier surface 25 | surf.degree_u = 3 26 | surf.degree_v = 2 27 | control_points = [[0, 0, 0], [0, 4, 0], [0, 8, -3], 28 | [2, 0, 6], [2, 4, 0], [2, 8, 0], 29 | [4, 0, 0], [4, 4, 0], [4, 8, 3], 30 | [6, 0, 0], [6, 4, -3], [6, 8, 0]] 31 | surf.set_ctrlpts(control_points, 4, 3) 32 | surf.knotvector_u = utilities.generate_knot_vector(surf.degree_u, surf.ctrlpts_size_u) 33 | surf.knotvector_v = utilities.generate_knot_vector(surf.degree_v, surf.ctrlpts_size_v) 34 | 35 | # Set sample size 36 | surf.sample_size = 25 37 | 38 | # Evaluate surface 39 | surf.evaluate() 40 | 41 | # Plot the control point grid and the evaluated surface 42 | vis_comp = VisMPL.VisSurface() 43 | surf.vis = vis_comp 44 | surf.render() 45 | 46 | pass 47 | -------------------------------------------------------------------------------- /surface/ex_torus.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | Toroidal surface is contributed by Harshil Shah (@harshilsofeshah) 10 | """ 11 | 12 | import os 13 | from geomdl import NURBS 14 | from geomdl import exchange 15 | from geomdl.visualization import VisMPL 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a NURBS surface instance 22 | surf = NURBS.Surface() 23 | 24 | # Set degress 25 | surf.degree_u = 2 26 | surf.degree_v = 2 27 | 28 | # Set control points 29 | surf.set_ctrlpts(*exchange.import_txt("ex_torus.cptw", two_dimensional=True)) 30 | 31 | # Set knot vectors 32 | surf.knotvector_u = [0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1] 33 | surf.knotvector_v = [0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1] 34 | 35 | # Set sample size and evaluate surface 36 | surf.sample_size = 25 37 | surf.evaluate() 38 | 39 | # Import colormaps 40 | from matplotlib import cm 41 | 42 | # Plot the surface 43 | vis_config = VisMPL.VisConfig(ctrlpts=True, axes=True, legend=False) 44 | vis_comp = VisMPL.VisSurfTriangle(vis_config) 45 | surf.vis = vis_comp 46 | surf.render(colormap=cm.coolwarm) 47 | 48 | # Good to have something here to put a breakpoint 49 | pass 50 | -------------------------------------------------------------------------------- /surface/ex_surface01_decompose.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import exchange 13 | from geomdl import operations 14 | from geomdl import multi 15 | from geomdl.visualization import VisMPL 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a BSpline surface instance 22 | surf = BSpline.Surface() 23 | 24 | # Set degrees 25 | surf.degree_u = 3 26 | surf.degree_v = 3 27 | 28 | # Set control points 29 | surf.set_ctrlpts(*exchange.import_txt("ex_surface01.cpt", two_dimensional=True)) 30 | 31 | # Set knot vectors 32 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 33 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 34 | 35 | # Decompose the surface 36 | surf_list = operations.decompose_surface(surf) 37 | surfaces = multi.SurfaceContainer(surf_list) 38 | 39 | # Set number of samples for all split surfaces 40 | surfaces.sample_size = 20 41 | 42 | # Plot the control point grid and the evaluated surface 43 | vis_config = VisMPL.VisConfig(legend=False) 44 | vis_comp = VisMPL.VisSurface(vis_config) 45 | surfaces.vis = vis_comp 46 | surfaces.render() 47 | 48 | # Good to have something here to put a breakpoint 49 | pass 50 | -------------------------------------------------------------------------------- /surface/ex_surface02.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2016-2017 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import utilities 13 | from geomdl import exchange 14 | from geomdl import operations 15 | from geomdl.visualization import VisPlotly 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a BSpline surface instance 22 | surf = BSpline.Surface() 23 | 24 | # Set degrees 25 | surf.degree_u = 3 26 | surf.degree_v = 3 27 | 28 | # Set control points 29 | surf.set_ctrlpts(*exchange.import_txt("ex_surface02.cpt", two_dimensional=True)) 30 | 31 | # Set knot vectors 32 | surf.knotvector_u = utilities.generate_knot_vector(surf.degree_u, 6) 33 | surf.knotvector_v = utilities.generate_knot_vector(surf.degree_v, 6) 34 | 35 | # Set evaluation delta 36 | surf.delta = 0.025 37 | 38 | # Evaluate surface 39 | surf.evaluate() 40 | 41 | # Plot the control point grid and the evaluated surface 42 | vis_comp = VisPlotly.VisSurface() 43 | surf.vis = vis_comp 44 | surf.render() 45 | 46 | # Evaluate surface tangent and normal at the given u and v 47 | uv = [0.2, 0.9] 48 | surf_tangent = operations.tangent(surf, uv) 49 | surf_normal = operations.normal(surf, uv) 50 | 51 | # Good to have something here to put a breakpoint 52 | pass 53 | -------------------------------------------------------------------------------- /curve2d/ex_curve01_split.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import utilities 13 | from geomdl import exchange 14 | from geomdl import operations 15 | from geomdl import multi 16 | from geomdl.visualization import VisMPL 17 | 18 | 19 | # Fix file path 20 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 21 | 22 | # Create a B-Spline curve instance 23 | curve = BSpline.Curve() 24 | 25 | # Set up the curve 26 | curve.degree = 4 27 | curve.ctrlpts = exchange.import_txt("ex_curve01.cpt") 28 | 29 | # Auto-generate knot vector 30 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 31 | 32 | # Split the curve 33 | curve_list = operations.split_curve(curve, 0.6) 34 | curves = multi.CurveContainer(curve_list) 35 | 36 | # Move the 1st curve a little bit far away from the 2nd curve 37 | c2tan = curves[1].tangent(0.0, normalize=True) 38 | c2tanvec = [-3 * p for p in c2tan[1]] 39 | operations.translate(curves[0], c2tanvec, inplace=True) 40 | 41 | # Plot the curves using the curve container 42 | curves.sample_size = 40 43 | 44 | # Plot the curve 45 | vis_comp = VisMPL.VisCurve2D() 46 | curves.vis = vis_comp 47 | curves.render() 48 | 49 | # Good to have something here to put a breakpoint 50 | pass 51 | -------------------------------------------------------------------------------- /curve2d/ex_curve02_split.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import utilities 13 | from geomdl import multi 14 | from geomdl import exchange 15 | from geomdl import operations 16 | from geomdl.visualization import VisMPL 17 | 18 | 19 | # Fix file path 20 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 21 | 22 | # Create a BSpline (NUBS) curve instance 23 | curve = BSpline.Curve() 24 | 25 | # Set up the curve 26 | curve.degree = 3 27 | curve.ctrlpts = exchange.import_txt("ex_curve02.cpt") 28 | 29 | # Auto-generate knot vector 30 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 31 | 32 | # Split the curve 33 | curve1, curve2 = operations.split_curve(curve, 0.2) 34 | 35 | # Move the 1st curve a little bit far away from the 2nd curve 36 | c2tan = curve2.tangent(0.0, normalize=True) 37 | c2tanvec = [-1 * p for p in c2tan[1]] 38 | operations.translate(curve1, c2tanvec, inplace=True) 39 | 40 | # Plot the curves using the curve container 41 | curves = multi.CurveContainer() 42 | curves.sample_size = 40 43 | curves.add(curve1) 44 | curves.add(curve2) 45 | 46 | vis_comp = VisMPL.VisCurve2D() 47 | curves.vis = vis_comp 48 | curves.render() 49 | 50 | # Good to have something here to put a breakpoint 51 | pass 52 | -------------------------------------------------------------------------------- /surface/ex_surface01_split.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import exchange 13 | from geomdl import operations 14 | from geomdl import multi 15 | from geomdl.visualization import VisMPL 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a BSpline surface instance 22 | surf = BSpline.Surface() 23 | 24 | # Set degrees 25 | surf.degree_u = 3 26 | surf.degree_v = 3 27 | 28 | # Set control points 29 | surf.set_ctrlpts(*exchange.import_txt("ex_surface01.cpt", two_dimensional=True)) 30 | 31 | # Set knot vectors 32 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 33 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 34 | 35 | # Split the surface on the v-direction 36 | surf_list = operations.split_surface_v(surf, param=0.5) 37 | surfaces = multi.SurfaceContainer(surf_list) 38 | 39 | # Translate one of the surfaces by a vector 40 | operations.translate(surfaces[0], (0, -2.5, 0), inplace=True) 41 | 42 | # Set number of samples for all split surfaces 43 | surfaces.sample_size = 20 44 | 45 | # Plot the control point grid and the evaluated surface 46 | vis_comp = VisMPL.VisSurface() 47 | surfaces.vis = vis_comp 48 | surfaces.render() 49 | 50 | # Good to have something here to put a breakpoint 51 | pass 52 | -------------------------------------------------------------------------------- /curve2d/ex_curve02.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2016-2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import utilities 13 | from geomdl import exchange 14 | from geomdl import operations 15 | from geomdl import evaluators 16 | from geomdl.visualization import VisMPL 17 | 18 | 19 | # Fix file path 20 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 21 | 22 | # Create a BSpline (NUBS) curve instance 23 | curve = BSpline.Curve() 24 | 25 | # Set up the curve 26 | curve.degree = 3 27 | curve.ctrlpts = exchange.import_txt("ex_curve02.cpt") 28 | 29 | # Auto-generate knot vector 30 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 31 | 32 | # Set evaluation delta 33 | curve.delta = 0.01 34 | 35 | # Evaluate curve 36 | curve.evaluate() 37 | 38 | # Plot the control point polygon and the evaluated curve 39 | vis_comp = VisMPL.VisCurve2D() 40 | curve.vis = vis_comp 41 | curve.render() 42 | 43 | # Evaluate derivatives at u = 0.6 44 | ders1 = curve.derivatives(0.6, 4) 45 | 46 | # Change evaluator 47 | curve.evaluator = evaluators.CurveEvaluator2() 48 | 49 | # Evaluate derivatives at u = 0.6 using the new evaluator 50 | ders2 = curve.derivatives(0.6, 4) 51 | 52 | # Compute curve tangent at u = 0.6 53 | curvetan = operations.tangent(curve, 0.6) 54 | 55 | # Good to have something here to put a breakpoint 56 | pass 57 | -------------------------------------------------------------------------------- /fitting/approximation/ex_curve3d.cpt: -------------------------------------------------------------------------------- 1 | "Points:0","Points:1","Points:2" 2 | -22.9223,22.989,2.84126 3 | -12.3606,22.892,2.84496 4 | -6.99361,19.845,2.95333 5 | -4.55742,14.2306,3.11251 6 | -1.90278,15.2378,3.08782 7 | -1.80021,11.9152,3.16287 8 | -1.87746,7.65356,3.23224 9 | 2.84614,5.64227,3.25449 10 | 4.83652,1.33384,3.27948 11 | 6.07966,-1.49949,3.27909 12 | 9.63138,-0.275984,3.2809 13 | 11.6077,-2.43306,3.27604 14 | 15.1146,-4.7473,3.26222 15 | 18.1099,-9.18591,3.21078 16 | 18.3256,-12.7528,3.14569 17 | 21.7079,-13.021,3.13994 18 | 22.2407,-16.3942,3.05739 19 | 32.261,-16.0727,3.06607 20 | 38.9495,-19.0839,2.97799 21 | 37.266,-22.8816,2.84536 22 | 32.9259,-24.0227,2.80081 23 | 27.0851,-29.8883,2.53756 24 | 23.3137,-32.4214,2.40611 25 | 11.7315,-31.8796,2.43513 26 | 12.0121,-27.3814,2.65709 27 | 7.55334,-26.6465,2.69015 28 | 5.89992,-22.5151,2.85921 29 | 4.9932,-19.318,2.9705 30 | 2.69479,-16.2207,3.06209 31 | -0.755061,-15.004,3.0937 32 | -1.68063,-11.6558,3.16796 33 | -4.43081,-10.358,3.19173 34 | -5.6034,-7.4919,3.23428 35 | -8.92014,-6.12537,3.24976 36 | -12.8208,-9.51628,3.20564 37 | -13.3003,-7.06653,3.23943 38 | -13.8029,-3.46478,3.27098 39 | -15.6857,-0.474408,3.28078 40 | -16.6273,2.46917,3.27589 41 | -24.1171,3.43908,3.27113 42 | -28.8149,4.1352,3.26674 43 | -34.2631,5.1975,3.2585 44 | -39.4616,4.84693,3.26143 45 | -42.495,4.94682,3.26061 46 | -44.5188,5.81611,3.25283 47 | -45.6507,13.4231,3.13109 48 | -42.5386,13.4031,3.13154 49 | -39.1331,13.018,3.14 50 | -33.0238,13.9859,3.11826 51 | -26.6314,19.2328,2.97324 52 | -22.9223,22.989,2.84126 53 | -------------------------------------------------------------------------------- /fitting/interpolation/ex_curve3d.cpt: -------------------------------------------------------------------------------- 1 | "Points:0","Points:1","Points:2" 2 | -22.9223,22.989,2.84126 3 | -12.3606,22.892,2.84496 4 | -6.99361,19.845,2.95333 5 | -4.55742,14.2306,3.11251 6 | -1.90278,15.2378,3.08782 7 | -1.80021,11.9152,3.16287 8 | -1.87746,7.65356,3.23224 9 | 2.84614,5.64227,3.25449 10 | 4.83652,1.33384,3.27948 11 | 6.07966,-1.49949,3.27909 12 | 9.63138,-0.275984,3.2809 13 | 11.6077,-2.43306,3.27604 14 | 15.1146,-4.7473,3.26222 15 | 18.1099,-9.18591,3.21078 16 | 18.3256,-12.7528,3.14569 17 | 21.7079,-13.021,3.13994 18 | 22.2407,-16.3942,3.05739 19 | 32.261,-16.0727,3.06607 20 | 38.9495,-19.0839,2.97799 21 | 37.266,-22.8816,2.84536 22 | 32.9259,-24.0227,2.80081 23 | 27.0851,-29.8883,2.53756 24 | 23.3137,-32.4214,2.40611 25 | 11.7315,-31.8796,2.43513 26 | 12.0121,-27.3814,2.65709 27 | 7.55334,-26.6465,2.69015 28 | 5.89992,-22.5151,2.85921 29 | 4.9932,-19.318,2.9705 30 | 2.69479,-16.2207,3.06209 31 | -0.755061,-15.004,3.0937 32 | -1.68063,-11.6558,3.16796 33 | -4.43081,-10.358,3.19173 34 | -5.6034,-7.4919,3.23428 35 | -8.92014,-6.12537,3.24976 36 | -12.8208,-9.51628,3.20564 37 | -13.3003,-7.06653,3.23943 38 | -13.8029,-3.46478,3.27098 39 | -15.6857,-0.474408,3.28078 40 | -16.6273,2.46917,3.27589 41 | -24.1171,3.43908,3.27113 42 | -28.8149,4.1352,3.26674 43 | -34.2631,5.1975,3.2585 44 | -39.4616,4.84693,3.26143 45 | -42.495,4.94682,3.26061 46 | -44.5188,5.81611,3.25283 47 | -45.6507,13.4231,3.13109 48 | -42.5386,13.4031,3.13154 49 | -39.1331,13.018,3.14 50 | -33.0238,13.9859,3.11826 51 | -26.6314,19.2328,2.97324 52 | -22.9223,22.989,2.84126 53 | -------------------------------------------------------------------------------- /surface/ex_cylinder_decompose.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | The surface example is kindly contributed by John-Eric Dufour (@jedufour) 10 | """ 11 | 12 | import os 13 | from geomdl import NURBS 14 | from geomdl import exchange 15 | from geomdl import operations 16 | from geomdl import multi 17 | from geomdl.visualization import VisMPL 18 | 19 | 20 | # Fix file path 21 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 22 | 23 | # Create a NURBS surface instance 24 | surf = NURBS.Surface() 25 | 26 | # Set degrees 27 | surf.degree_u = 1 28 | surf.degree_v = 2 29 | 30 | # Set control points 31 | surf.set_ctrlpts(*exchange.import_txt("ex_cylinder.cptw", two_dimensional=True)) 32 | 33 | # Set knot vector 34 | surf.knotvector_u = [0, 0, 1, 1] 35 | surf.knotvector_v = [0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1] 36 | 37 | # Decompose the surface 38 | surf_list = operations.decompose_surface(surf) 39 | surfaces = multi.SurfaceContainer(surf_list) 40 | 41 | # Translate one of the surface patch 42 | operations.translate(surfaces[1], (-0.25, 0.25, 0), inplace=True) 43 | 44 | # Set number of samples for all split surfaces 45 | surfaces.sample_size = 50 46 | 47 | # Plot the control point grid and the evaluated surface 48 | vis_comp = VisMPL.VisSurfWireframe() 49 | surfaces.vis = vis_comp 50 | surfaces.render() 51 | 52 | # Good to have something here to put a breakpoint 53 | pass 54 | -------------------------------------------------------------------------------- /surface/ex_cylinder.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2016-2017 8 | 9 | This example is contributed by John-Eric Dufour (@jedufour) 10 | """ 11 | 12 | import os 13 | from geomdl import NURBS 14 | from geomdl import construct 15 | from geomdl import exchange 16 | from geomdl.visualization import VisMPL as vis 17 | 18 | 19 | # Fix file path 20 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 21 | 22 | # Create a NURBS surface instance 23 | surf = NURBS.Surface() 24 | 25 | # Set degrees 26 | surf.degree_u = 1 27 | surf.degree_v = 2 28 | 29 | # Set control points 30 | surf.set_ctrlpts(*exchange.import_txt("ex_cylinder.cptw", two_dimensional=True)) 31 | 32 | # Set knot vectors 33 | surf.knotvector_u = [0, 0, 1, 1] 34 | surf.knotvector_v = [0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1] 35 | 36 | # Set evaluation delta 37 | surf.delta = 0.05 38 | 39 | surf_curves = construct.extract_curves(surf) 40 | plot_extras = [ 41 | dict( 42 | points=surf_curves['u'][0].evalpts, 43 | name="u", 44 | color="cyan", 45 | size=15 46 | ), 47 | dict( 48 | points=surf_curves['v'][0].evalpts, 49 | name="v", 50 | color="magenta", 51 | size=5 52 | ) 53 | ] 54 | 55 | # Plot the control point grid and the evaluated surface 56 | surf.vis = vis.VisSurface() 57 | surf.render(extras=plot_extras) 58 | 59 | # Good to have something here to put a breakpoint 60 | pass 61 | -------------------------------------------------------------------------------- /surface/ex_cylinder_tmpl.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | Read control points from a template file 10 | """ 11 | 12 | import os 13 | from geomdl import NURBS 14 | from geomdl import construct 15 | from geomdl import exchange 16 | from geomdl.visualization import VisMPL as vis 17 | 18 | 19 | # Fix file path 20 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 21 | 22 | # Create a NURBS surface instance 23 | surf = NURBS.Surface() 24 | 25 | # Set degrees 26 | surf.degree_u = 1 27 | surf.degree_v = 2 28 | 29 | # Set control points from a template file 30 | surf.set_ctrlpts(*exchange.import_txt("ex_cylinder_tmpl.cptw", two_dimensional=True, jinja2=True)) 31 | 32 | # Set knot vectors 33 | surf.knotvector_u = [0, 0, 1, 1] 34 | surf.knotvector_v = [0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1] 35 | 36 | # Set evaluation delta 37 | surf.delta = 0.05 38 | 39 | surf_curves = construct.extract_curves(surf) 40 | plot_extras = [ 41 | dict( 42 | points=surf_curves['u'][0].evalpts, 43 | name="u", 44 | color="cyan", 45 | size=15 46 | ), 47 | dict( 48 | points=surf_curves['v'][0].evalpts, 49 | name="v", 50 | color="magenta", 51 | size=5 52 | ) 53 | ] 54 | 55 | # Plot the control point grid and the evaluated surface 56 | surf.vis = vis.VisSurface() 57 | surf.render(extras=plot_extras) 58 | 59 | # Good to have something here to put a breakpoint 60 | pass 61 | -------------------------------------------------------------------------------- /fitting/interpolation/global_surface.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | Surface fitting by global interpolation 10 | """ 11 | 12 | from geomdl import fitting 13 | from geomdl.visualization import VisMPL as vis 14 | 15 | 16 | # Data set 17 | points = ((-5, -5, 0), (-2.5, -5, 0), (0, -5, 0), (2.5, -5, 0), (5, -5, 0), (7.5, -5, 0), (10, -5, 0), 18 | (-5, 0, 3), (-2.5, 0, 3), (0, 0, 3), (2.5, 0, 3), (5, 0, 3), (7.5, 0, 3), (10, 0, 3), 19 | (-5, 5, 0), (-2.5, 5, 0), (0, 5, 0), (2.5, 5, 0), (5, 5, 0), (7.5, 5, 0), (10, 5, 0), 20 | (-5, 7.5, -3), (-2.5, 7.5, -3), (0, 7.5, -3), (2.5, 7.5, -3), (5, 7.5, -3), (7.5, 7.5, -3), (10, 7.5, -3), 21 | (-5, 10, 0), (-2.5, 10, 0), (0, 10, 0), (2.5, 10, 0), (5, 10, 0), (7.5, 10, 0), (10, 10, 0)) 22 | size_u = 5 23 | size_v = 7 24 | degree_u = 2 25 | degree_v = 3 26 | 27 | # Do global surface interpolation 28 | surf = fitting.interpolate_surface(points, size_u, size_v, degree_u, degree_v) 29 | 30 | # Plot the interpolated surface 31 | surf.delta = 0.05 32 | surf.vis = vis.VisSurface() 33 | surf.render() 34 | 35 | # # Visualize data and evaluated points together 36 | # import numpy as np 37 | # import matplotlib.pyplot as plt 38 | # evalpts = np.array(surf.evalpts) 39 | # pts = np.array(points) 40 | # fig = plt.figure() 41 | # ax = plt.axes(projection='3d') 42 | # ax.scatter(evalpts[:, 0], evalpts[:, 1], evalpts[:, 2]) 43 | # ax.scatter(pts[:, 0], pts[:, 1], pts[:, 2], color="red") 44 | # plt.show() 45 | -------------------------------------------------------------------------------- /curve3d/ex_curve3d01_split.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | A 3-dimensional B-Spline curve is split at u=0.3 and one of the split curves is translated using the tangent vector 10 | """ 11 | 12 | import os 13 | from geomdl import BSpline 14 | from geomdl import utilities 15 | from geomdl import exchange 16 | from geomdl import operations 17 | from geomdl import multi 18 | from geomdl.visualization import VisPlotly 19 | 20 | 21 | # Fix file path 22 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 23 | 24 | # Create a B-Spline curve instance 25 | curve = BSpline.Curve() 26 | 27 | # Set up curve 28 | curve.degree = 4 29 | curve.ctrlpts = exchange.import_txt("ex_curve3d01.cpt") 30 | 31 | # Auto-generate knot vector 32 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 33 | 34 | # Split the curve 35 | split_curves = operations.split_curve(curve, 0.3) 36 | curves = multi.CurveContainer(split_curves) 37 | 38 | # Move the 1st curve a little bit far away from the 2nd curve 39 | c2tan = operations.tangent(curves[1], 0.0, normalize=True) 40 | c2tanvec = [-1 * p for p in c2tan[1]] 41 | operations.translate(curves[0], c2tanvec, inplace=True) 42 | 43 | # Plot the curves using the curve container 44 | curves.sample_size = 100 45 | 46 | # Plot the control point polygon and the evaluated curve 47 | vis_comp = VisPlotly.VisCurve3D() 48 | curves.vis = vis_comp 49 | curves.render() 50 | 51 | # Good to have something here to put a breakpoint 52 | pass 53 | -------------------------------------------------------------------------------- /curve2d/ex_curve02_decompose.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | import os 10 | from geomdl import BSpline 11 | from geomdl import utilities 12 | from geomdl import exchange 13 | from geomdl import operations 14 | from geomdl import multi 15 | from geomdl.visualization import VisMPL 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a BSpline (NUBS) curve instance 22 | curve = BSpline.Curve() 23 | 24 | # Set up the curve 25 | curve.degree = 3 26 | curve.ctrlpts = exchange.import_txt("ex_curve02.cpt") 27 | 28 | # Auto-generate knot vector 29 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 30 | 31 | # Decompose the curve into Bezier curve segments 32 | curve_list = operations.decompose_curve(curve) 33 | bezier = multi.CurveContainer(curve_list) 34 | 35 | # Move the curves a little bit away from each other (for display purposes only) 36 | c2tan1 = bezier[1].tangent(0.0, normalize=True) 37 | c2tanvec1 = [-1 * p for p in c2tan1[1]] 38 | operations.translate(bezier[0], c2tanvec1, inplace=True) 39 | 40 | c2tan2 = bezier[1].tangent(1.0, normalize=True) 41 | c2tanvec2 = [1 * p for p in c2tan2[1]] 42 | operations.translate(bezier[2], c2tanvec2, inplace=True) 43 | 44 | # Set sample size of the Bezier curves 45 | bezier.sample_size = 20 46 | 47 | # Plot the Bezier curves 48 | vis_comp = VisMPL.VisCurve2D() 49 | bezier.vis = vis_comp 50 | bezier.render() 51 | 52 | # Good to have something here to put a breakpoint 53 | pass 54 | -------------------------------------------------------------------------------- /surface/trimming/trim_single.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import exchange 13 | from geomdl import tessellate 14 | from geomdl.visualization import VisVTK as vis 15 | from geomdl.shapes import analytic 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a BSpline surface instance 22 | surf = BSpline.Surface() 23 | 24 | # Set degrees 25 | surf.degree_u = 3 26 | surf.degree_v = 3 27 | 28 | # Set control points 29 | surf.set_ctrlpts(*exchange.import_txt("../ex_surface01.cpt", two_dimensional=True)) 30 | 31 | # Set knot vectors 32 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 33 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 34 | 35 | # Set sample size 36 | surf.sample_size = 35 37 | 38 | # Set surface tessellation component 39 | surf.tessellator = tessellate.TrimTessellate() 40 | 41 | # Set visualization component 42 | visconf = vis.VisConfig(ctrlpts=False, legend=False) 43 | surf.vis = vis.VisSurface(config=visconf) 44 | 45 | # Generate a circular trim curve 46 | tcrv = analytic.Circle(origin=(0.5, 0.5), radius=0.25) 47 | tcrv.opt = ['reversed', 1] 48 | trim_curves = [tcrv] 49 | 50 | # Set trim curves (as a list) 51 | surf.trims = trim_curves 52 | 53 | # Visualize surface 54 | # surf.render() 55 | 56 | # # Save as Wavefront OBJ file 57 | exchange.export_obj(surf, "trim_single.obj") 58 | 59 | # Good to have something here to put a breakpoint 60 | pass 61 | -------------------------------------------------------------------------------- /surface/ex_surface01_split_multi.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import exchange 13 | from geomdl import operations 14 | from geomdl import multi 15 | from geomdl.visualization import VisMPL 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a BSpline surface instance 22 | surf = BSpline.Surface() 23 | 24 | # Set degrees 25 | surf.degree_u = 3 26 | surf.degree_v = 3 27 | 28 | # Set control points 29 | surf.set_ctrlpts(*exchange.import_txt("ex_surface01.cpt", two_dimensional=True)) 30 | 31 | # Set knot vectors 32 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 33 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 34 | 35 | # Split the surface 36 | surface_list1 = operations.split_surface_u(surf, param=0.25) 37 | surfaces1 = multi.SurfaceContainer(surface_list1) 38 | 39 | # Translate one of the split surfaces 40 | operations.translate(surfaces1[0], (0, 0, 5), inplace=True) 41 | 42 | # Split the other split surface 43 | surface_list2 = operations.split_surface_v(surfaces1[1], param=0.75) 44 | surfaces = multi.SurfaceContainer(surface_list2) 45 | surfaces.add(surfaces1[0]) 46 | 47 | # Set number of samples for all split surfaces 48 | surfaces.sample_size = 20 49 | 50 | # Plot the control point grid and the evaluated surface 51 | vis_comp = VisMPL.VisSurfTriangle() 52 | surfaces.vis = vis_comp 53 | surfaces.render() 54 | 55 | # Good to have something here to put a breakpoint 56 | pass 57 | -------------------------------------------------------------------------------- /surface/ex_surface01_rotation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import multi 13 | from geomdl import operations 14 | from geomdl import exchange 15 | from geomdl.visualization import VisMPL as vis 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a BSpline surface instance 22 | surf1 = BSpline.Surface() 23 | 24 | # Set degrees 25 | surf1.degree_u = 3 26 | surf1.degree_v = 3 27 | 28 | # Set control points 29 | surf1.set_ctrlpts(*exchange.import_txt("ex_surface01.cpt", two_dimensional=True)) 30 | 31 | # Set knot vectors 32 | surf1.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 33 | surf1.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 34 | 35 | # Set evaluation delta 36 | surf1.delta = 0.025 37 | 38 | # Rotate surf1 and generate a copy in surf2 39 | surf2 = operations.rotate(surf1, 45, axis=2) 40 | 41 | # Visualize surfaces 42 | msurf1 = multi.SurfaceContainer(surf1, surf2) 43 | msurf1.vis = vis.VisSurface() 44 | msurf1.render() 45 | 46 | # Decompose surf1 47 | surf1_decomposed = operations.decompose_surface(surf1) 48 | msurf2 = multi.SurfaceContainer(surf1_decomposed) 49 | 50 | # Rotate decomposed surface 51 | msurf2_rotated = operations.rotate(msurf2, 90, axis=1) 52 | 53 | # Visualize decomposed and decomposed-rotated surfaces 54 | msurf3 = multi.SurfaceContainer(msurf2, msurf2_rotated) 55 | msurf3.vis = vis.VisSurface() 56 | msurf3.render() 57 | 58 | # Good to have something here to put a breakpoint 59 | pass 60 | -------------------------------------------------------------------------------- /bezier/ex_bezier_curve2d.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | 2nd degree and 3rd degree 2-dimensional Bezier curves 10 | """ 11 | 12 | import os 13 | from geomdl import BSpline 14 | from geomdl import utilities 15 | from geomdl.visualization import VisMPL 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # 22 | # 2D CURVE 1 23 | # 24 | 25 | # Create a B-Spline curve instance (Bezier Curve) 26 | curve1 = BSpline.Curve() 27 | 28 | # Set up the Bezier curve 29 | curve1.degree = 2 30 | curve1.ctrlpts = [[10, 0], [20, 15], [30, 0]] 31 | 32 | 33 | # Auto-generate knot vector 34 | curve1.knotvector = utilities.generate_knot_vector(curve1.degree, len(curve1.ctrlpts)) 35 | 36 | # Set evaluation delta 37 | curve1.sample_size = 40 38 | 39 | # Evaluate curve 40 | curve1.evaluate() 41 | 42 | # Draw the control point polygon and the evaluated curve 43 | vis_comp1 = VisMPL.VisCurve2D() 44 | curve1.vis = vis_comp1 45 | curve1.render() 46 | 47 | # 48 | # 2D CURVE 2 49 | # 50 | 51 | # Create another B-Spline curve instance (Bezier Curve) 52 | curve2 = BSpline.Curve() 53 | 54 | # Set up the Bezier curve 55 | curve2.degree = 3 56 | curve2.ctrlpts = [[10, 0], [20, 15], [20, 15], [30, 0]] 57 | 58 | # Auto-generate knot vector 59 | curve2.knotvector = utilities.generate_knot_vector(curve2.degree, len(curve2.ctrlpts)) 60 | 61 | # Set evaluation delta 62 | curve2.sample_size = 40 63 | 64 | # Evaluate curve 65 | curve2.evaluate() 66 | 67 | # Draw the control point polygon and the evaluated curve 68 | vis_comp2 = VisMPL.VisCurve2D() 69 | curve2.vis = vis_comp2 70 | curve2.render() 71 | 72 | pass 73 | -------------------------------------------------------------------------------- /curve2d/ex_curve03.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2016-2018 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import utilities 13 | from geomdl import exchange 14 | from geomdl import operations 15 | # from geomdl.visualization import VisMPL 16 | from geomdl.visualization import VisPlotly 17 | 18 | 19 | # Fix file path 20 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 21 | 22 | # Create a BSpline curve instance 23 | curve = BSpline.Curve() 24 | 25 | # Set up the curve 26 | curve.degree = 3 27 | curve.ctrlpts = exchange.import_txt("ex_curve03.cpt") 28 | 29 | # Auto-generate knot vector 30 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 31 | 32 | # Set evaluation delta 33 | curve.delta = 0.01 34 | 35 | # Evaulate curve 36 | curve.evaluate() 37 | 38 | # Draw the control point polygon and the evaluated curve 39 | vis_comp = VisPlotly.VisCurve2D() 40 | curve.vis = vis_comp 41 | curve.render() 42 | 43 | # Evaluate curve tangent at u = 0.0 44 | curvetan1 = operations.tangent(curve, 0.0, normalize=True) 45 | 46 | # Evaluate curve tangent at u = 0.2 47 | curvetan2 = operations.tangent(curve, 0.2, normalize=True) 48 | 49 | # Evaluate curve tangent at u = 0.5 50 | curvetan3 = operations.tangent(curve, 0.5, normalize=True) 51 | 52 | # Evaluate curve tangent at u = 0.6 53 | curvetan4 = operations.tangent(curve, 0.6, normalize=True) 54 | 55 | # Evaluate curve tangent at u = 0.8 56 | curvetan5 = operations.tangent(curve, 0.8, normalize=True) 57 | 58 | # Evaluate curve tangent at u = 1.0 59 | curvetan6 = operations.tangent(curve, 1.0, normalize=True) 60 | 61 | # Good to have something here to put a breakpoint 62 | pass 63 | -------------------------------------------------------------------------------- /surface/trimming/trim_multi_sense1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import exchange 13 | from geomdl import tessellate 14 | from geomdl.visualization import VisVTK as vis 15 | from geomdl.shapes import analytic 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a BSpline surface instance 22 | surf = BSpline.Surface() 23 | 24 | # Set degrees 25 | surf.degree_u = 3 26 | surf.degree_v = 3 27 | 28 | # Set control points 29 | surf.set_ctrlpts(*exchange.import_txt("../ex_surface01.cpt", two_dimensional=True)) 30 | 31 | # Set knot vectors 32 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 33 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 34 | 35 | # Set sample size 36 | surf.sample_size = 40 37 | 38 | # Set surface tessellation component 39 | surf.tessellator = tessellate.TrimTessellate() 40 | 41 | # Set visualization component 42 | visconf = vis.VisConfig(ctrlpts=False, legend=False, trims=False) 43 | surf.vis = vis.VisSurface(config=visconf) 44 | 45 | # Generate circular trim curves with different sense 46 | tcrv1 = analytic.Circle(origin=(0.25, 0.25), radius=0.20) 47 | tcrv1.opt = ['reversed', 0] 48 | tcrv2 = analytic.Circle(origin=(0.75, 0.75), radius=0.20) 49 | tcrv2.opt = ['reversed', 0] 50 | trim_curves = [tcrv1, tcrv2] 51 | 52 | # Set trim curves (as a list) 53 | surf.trims = trim_curves 54 | 55 | # Visualize surface 56 | surf.render() 57 | 58 | # # Save as Wavefront OBJ file 59 | # exchange.export_obj(surf, "trim_multi_sense1.obj") 60 | 61 | # Good to have something here to put a breakpoint 62 | pass 63 | -------------------------------------------------------------------------------- /surface/ex_torus.cptw: -------------------------------------------------------------------------------- 1 | -125.0,0.0,0.0,1.0;-125.0,0.0,125.0,1.0;0.0,0.0,125.0,1.0;125.0,0.0,125.0,1.0;125.0,0.0,0.0,1.0;125.0,0.0,-125.0,1.0;0.0,0.0,-125.0,1.0;-125.0,0.0,-125.0,1.0;-125.0,0.0,0.0,1.0 2 | -88.375,70.7,0.0,0.707;-88.375,70.7,88.375,0.707;0.0,70.7,88.375,0.707;88.375,70.7,88.375,0.707;88.375,70.7,0.0,0.707;88.375,70.7,-88.375,0.707;0.0,70.7,-88.375,0.707;-88.375,70.7,-88.375,0.707;-88.375,70.7,0.0,0.707 3 | -225.0,100.0,0.0,1.0;-225.0,100.0,225.0,1.0;0.0,100.0,225.0,1.0;225.0,100.0,225.0,1.0;225.0,100.0,0.0,1.0;225.0,100.0,-225.0,1.0;0.0,100.0,-225.0,1.0;-225.0,100.0,-225.0,1.0;-225.0,100.0,0.0,1.0 4 | -229.775,70.7,0.0,0.707;-229.775,70.7,229.775,0.707;0.0,70.7,229.775,0.707;229.775,70.7,229.775,0.707;229.775,70.7,0.0,0.707;229.775,70.7,-229.775,0.707;0.0,70.7,-229.775,0.707;-229.775,70.7,-229.775,0.707;-229.775,70.7,0.0,0.707 5 | -325.0,0.0,0.0,1.0;-325.0,0.0,325.0,1.0;0.0,0.0,325.0,1.0;325.0,0.0,325.0,1.0;325.0,0.0,0.0,1.0;325.0,0.0,-325.0,1.0;0.0,0.0,-325.0,1.0;-325.0,0.0,-325.0,1.0;-325.0,0.0,0.0,1.0 6 | -229.775,-70.7,0.0,0.707;-229.775,-70.7,229.775,0.707;0.0,-70.7,229.775,0.707;229.775,-70.7,229.775,0.707;229.775,-70.7,0.0,0.707;229.775,-70.7,-229.775,0.707;0.0,-70.7,-229.775,0.707;-229.775,-70.7,-229.775,0.707;-229.775,-70.7,0.0,0.707 7 | -225.0,-100.0,0.0,1.0;-225.0,-100.0,225.0,1.0;0.0,-100.0,225.0,1.0;225.0,-100.0,225.0,1.0;225.0,-100.0,0.0,1.0;225.0,-100.0,-225.0,1.0;0.0,-100.0,-225.0,1.0;-225.0,-100.0,-225.0,1.0;-225.0,-100.0,0.0,1.0 8 | -88.375,-70.7,0.0,0.707;-88.375,-70.7,88.375,0.707;0.0,-70.7,88.375,0.707;88.375,-70.7,88.375,0.707;88.375,-70.7,0.0,0.707;88.375,-70.7,-88.375,0.707;0.0,-70.7,-88.375,0.707;-88.375,-70.7,-88.375,0.707;-88.375,-70.7,0.0,0.707 9 | -125.0,0.0,0.0,1.0;-125.0,0.0,125.0,1.0;0.0,0.0,125.0,1.0;125.0,0.0,125.0,1.0;125.0,0.0,0.0,1.0;125.0,0.0,-125.0,1.0;0.0,0.0,-125.0,1.0;-125.0,0.0,-125.0,1.0;-125.0,0.0,0.0,1.0 10 | -------------------------------------------------------------------------------- /surface/trimming/trim_advanced.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | import os 11 | from matplotlib import cm 12 | from geomdl import BSpline 13 | from geomdl import exchange 14 | from geomdl import tessellate 15 | from geomdl.visualization import VisMPL as vis 16 | from geomdl import knotvector 17 | from geomdl import trimming 18 | 19 | 20 | # Fix file path 21 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 22 | 23 | # Create a BSpline surface instance 24 | surf = BSpline.Surface() 25 | 26 | # Set degrees 27 | surf.degree_u = 3 28 | surf.degree_v = 3 29 | 30 | # Set control points 31 | surf.set_ctrlpts(*exchange.import_txt("../ex_surface01.cpt", two_dimensional=True)) 32 | 33 | # Set knot vectors 34 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 35 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 36 | 37 | # Set sample size 38 | surf.sample_size = 50 39 | 40 | # Set surface tessellation component 41 | surf.tessellator = tessellate.TrimTessellate() 42 | 43 | # Set visualization component 44 | visconf = vis.VisConfig(ctrlpts=False, legend=False) 45 | surf.vis = vis.VisSurface(config=visconf) 46 | 47 | # Generate a trim curve 48 | tcrv = BSpline.Curve() 49 | tcrv.degree = 1 50 | tcrv.ctrlpts = [[0.5, 0.0], [0.5, 0.5], [0.0, 0.5]] 51 | tcrv.knotvector = knotvector.generate(1, 3) 52 | trim_curves = [tcrv] 53 | 54 | # Set trim curves (as a list) 55 | surf.trims = trim_curves 56 | 57 | # Fix trimming curves 58 | # trimming.fix_trim_curves(surf) 59 | 60 | # Visualize surface 61 | surf.render(colormap=cm.copper) 62 | 63 | # # Save as Wavefront OBJ file 64 | # exchange.export_obj(surf, "trim_advanced.obj") 65 | 66 | # Good to have something here to put a breakpoint 67 | pass 68 | -------------------------------------------------------------------------------- /compat/ex_convert01.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | Demonstration of the compatibility module 10 | """ 11 | 12 | from geomdl import NURBS 13 | from geomdl import utilities as utils 14 | from geomdl import compatibility as compat 15 | 16 | from geomdl.visualization import VisMPL 17 | 18 | 19 | # 20 | # The initial surface exported from your CAD software 21 | # 22 | 23 | # Control points in u-row order 24 | p_size_u = 4 25 | p_size_v = 3 26 | p_ctrlpts = [[0, 0, 0], [1, 0, 6], [2, 0, 0], [3, 0, 0], 27 | [0, 1, 0], [1, 1, 0], [2, 1, 0], [3, 1, -3], 28 | [0, 2, -3], [1, 2, 0], [2, 2, 3], [3, 2, 0]] 29 | 30 | # Weights of the control points 31 | p_weights = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] 32 | 33 | # Degrees 34 | p_degree_u = 3 35 | p_degree_v = 2 36 | 37 | # 38 | # Prepare data for import 39 | # 40 | 41 | t_ctrlptsw = compat.combine_ctrlpts_weights(p_ctrlpts, p_weights) 42 | n_ctrlptsw = compat.flip_ctrlpts_u(t_ctrlptsw, p_size_u, p_size_v) 43 | 44 | # Since we have no information on knot vectors, let's auto-generate them 45 | n_knotvector_u = utils.generate_knot_vector(p_degree_u, p_size_u) 46 | n_knotvector_v = utils.generate_knot_vector(p_degree_v, p_size_v) 47 | 48 | 49 | # 50 | # Import surface to NURBS-Python 51 | # 52 | 53 | surf = NURBS.Surface() 54 | surf.degree_u = p_degree_u 55 | surf.degree_v = p_degree_v 56 | surf.ctrlpts_size_u = p_size_u 57 | surf.ctrlpts_size_v = p_size_v 58 | surf.ctrlptsw = n_ctrlptsw 59 | surf.knotvector_u = n_knotvector_u 60 | surf.knotvector_v = n_knotvector_v 61 | 62 | # Set evaluation delta 63 | surf.delta = 0.05 64 | 65 | # Set visualization component 66 | vis_comp = VisMPL.VisSurfTriangle() 67 | surf.vis = vis_comp 68 | 69 | # Render the surface 70 | surf.render() 71 | -------------------------------------------------------------------------------- /curve2d/ex_curve01_refinement.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import exchange 13 | from geomdl import operations 14 | from geomdl import knotvector 15 | from geomdl.visualization import VisMPL 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a B-Spline curve instance 22 | curve = BSpline.Curve() 23 | 24 | # Set degree 25 | curve.degree = 4 26 | 27 | # Set control points 28 | curve.ctrlpts = exchange.import_txt("ex_curve01.cpt") 29 | 30 | # Auto-generate knot vector 31 | curve.knotvector = knotvector.generate(curve.degree, curve.ctrlpts_size) 32 | 33 | # Set visualization component 34 | curve.vis = VisMPL.VisCurve2D() 35 | 36 | # Plot the control point polygon and the evaluated curve 37 | curve.render() 38 | 39 | # Apply knot refinement w/ density = 0 (no refinement) 40 | operations.refine_knotvector(curve, [0]) 41 | 42 | # Plot the control point polygon and the evaluated curve (should be same as the previous one) 43 | curve.render() 44 | 45 | # Apply knot refinement w/ density = 1 (default) 46 | operations.refine_knotvector(curve, [1]) 47 | 48 | # Plot the control point polygon and the evaluated curve after knot refinement 49 | curve.render() 50 | 51 | # Apply knot refinement w/ density = 2 52 | operations.refine_knotvector(curve, [2]) 53 | 54 | # Plot the control point polygon and the evaluated curve after knot refinement 55 | curve.render() 56 | 57 | # Apply knot refinement w/ density = 3 58 | operations.refine_knotvector(curve, [3]) 59 | 60 | # Plot the control point polygon and the evaluated curve after knot refinement 61 | curve.render() 62 | 63 | # Good to have something here to put a breakpoint 64 | pass 65 | -------------------------------------------------------------------------------- /surface/trimming/trim_multi_sense2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import exchange 13 | from geomdl import tessellate 14 | from geomdl.visualization import VisVTK as vis 15 | from geomdl.shapes import analytic 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a BSpline surface instance 22 | surf = BSpline.Surface() 23 | 24 | # Set degrees 25 | surf.degree_u = 3 26 | surf.degree_v = 3 27 | 28 | # Set control points 29 | surf.set_ctrlpts(*exchange.import_txt("../ex_surface01.cpt", two_dimensional=True)) 30 | 31 | # Set knot vectors 32 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 33 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 34 | 35 | # Set sample size 36 | surf.sample_size = 60 37 | 38 | # Set surface tessellation component 39 | surf.tessellator = tessellate.TrimTessellate() 40 | 41 | # Set visualization component 42 | visconf = vis.VisConfig(ctrlpts=False, legend=False, trims=False) 43 | surf.vis = vis.VisSurface(config=visconf) 44 | 45 | # Generate circular trim curves with different sense 46 | tcrv1 = analytic.Circle(origin=(0.35, 0.35), radius=0.3) 47 | tcrv1.opt = ['reversed', 1] 48 | tcrv2 = analytic.Circle(origin=(0.65, 0.65), radius=0.3) 49 | tcrv2.opt = ['reversed', 1] 50 | tcrv3 = analytic.Circle(origin=(0.5, 0.5), radius=0.05) 51 | tcrv3.opt = ['reversed', 0] 52 | trim_curves = [tcrv1, tcrv3, tcrv2] 53 | 54 | # Set trim curves (as a list) 55 | surf.trims = trim_curves 56 | 57 | # Visualize surface 58 | surf.render() 59 | 60 | # # Save as Wavefront OBJ file 61 | # exchange.export_obj(surf, "trim_multi_sense2.obj") 62 | 63 | # Good to have something here to put a breakpoint 64 | pass 65 | -------------------------------------------------------------------------------- /surface/trimming/trim_multi_sense3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import exchange 13 | from geomdl import tessellate 14 | from geomdl.visualization import VisVTK as vis 15 | from geomdl.shapes import analytic 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Create a BSpline surface instance 22 | surf = BSpline.Surface() 23 | 24 | # Set degrees 25 | surf.degree_u = 3 26 | surf.degree_v = 3 27 | 28 | # Set control points 29 | surf.set_ctrlpts(*exchange.import_txt("../ex_surface01.cpt", two_dimensional=True)) 30 | 31 | # Set knot vectors 32 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 33 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 34 | 35 | # Set surface tessellation component 36 | surf.tessellator = tessellate.TrimTessellate() 37 | 38 | # Set visualization component 39 | visconf = vis.VisConfig(ctrlpts=False, legend=False, trims=False) 40 | surf.vis = vis.VisSurface(config=visconf) 41 | 42 | # Generate circular trim curves with different sense 43 | tcrv1 = analytic.Circle(origin=(0.5, 0.0), radius=0.2) 44 | tcrv1.opt = ['reversed', 0] 45 | tcrv2 = analytic.Circle(origin=(0.0, 0.5), radius=0.2) 46 | tcrv2.opt = ['reversed', 0] 47 | tcrv3 = analytic.Circle(origin=(1.0, 0.5), radius=0.2) 48 | tcrv3.opt = ['reversed', 0] 49 | trim_curves = [tcrv1, tcrv2, tcrv3] 50 | # trim_curves = [tcrv3] 51 | 52 | # Set trim curves (as a list) 53 | surf.trims = trim_curves 54 | 55 | # Set sample size 56 | surf.sample_size = 50 57 | 58 | # Visualize surface 59 | surf.render() 60 | 61 | # # Save as Wavefront OBJ file 62 | # exchange.export_obj(surf, "trim_multi_sense3.obj") 63 | 64 | # Good to have something here to put a breakpoint 65 | pass 66 | -------------------------------------------------------------------------------- /grid/ex_surfgen02.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under the MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | This example illustrates the following operations: 10 | 11 | * Generating a control points grid 12 | * Rotating the control points grid about some chosen axes 13 | * Generating a B-Spline surface and using the generated grid as its control points input 14 | * Plotting the surface 15 | """ 16 | 17 | 18 | from geomdl import BSpline 19 | from geomdl import CPGen 20 | from geomdl import utilities 21 | from geomdl import operations 22 | from geomdl.visualization import VisMPL as vis 23 | 24 | # Generate a control points grid 25 | surfgrid = CPGen.Grid(50, 100) 26 | 27 | # Grid size 25x30 28 | surfgrid.generate(25, 30) 29 | 30 | # Generate bumps on the grid 31 | surfgrid.bumps(num_bumps=5, bump_height=20, base_extent=4) 32 | 33 | # Create a BSpline surface instance 34 | surf = BSpline.Surface() 35 | 36 | # Set degrees 37 | surf.degree_u = 3 38 | surf.degree_v = 3 39 | 40 | # Get the control points from the generated grid 41 | surf.ctrlpts2d = surfgrid.grid 42 | 43 | # Set knot vectors 44 | surf.knotvector_u = utilities.generate_knot_vector(surf.degree_u, surf.ctrlpts_size_u) 45 | surf.knotvector_v = utilities.generate_knot_vector(surf.degree_v, surf.ctrlpts_size_v) 46 | 47 | # Set sample size of the surface 48 | surf.sample_size = 50 49 | 50 | # Rotate the surface about y and z axes 51 | operations.rotate(surf, 12.5, axis=1) 52 | operations.rotate(surf, 45, axis=2) 53 | 54 | # Generate the visualization component and its configuration 55 | vis_config = vis.VisConfig(ctrlpts=False, legend=False) 56 | vis_comp = vis.VisSurface(vis_config) 57 | 58 | # Set visualization component of the surface 59 | surf.vis = vis_comp 60 | 61 | # Plot the surface 62 | surf.render() 63 | 64 | # Good to have something here to put a breakpoint 65 | pass 66 | -------------------------------------------------------------------------------- /grid/ex_surfgen03.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under the MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | This example illustrates the following operations: 10 | 11 | * Generating a control points grid 12 | * Generating a B-Spline surface and using the generated grid as its control points input 13 | * Plotting the surface using Matplotlib 14 | * Exporting the surface as a .stl file 15 | """ 16 | 17 | 18 | from geomdl import BSpline 19 | from geomdl import CPGen 20 | from geomdl import utilities 21 | from geomdl.visualization import VisMPL as vis 22 | from geomdl import exchange 23 | 24 | # Generate a control points grid 25 | surfgrid = CPGen.Grid(100, 100) 26 | 27 | # This will generate a 8x8 grid 28 | surfgrid.generate(8, 8) 29 | 30 | # Generate 1 bump at the center of the grid 31 | surfgrid.bumps(num_bumps=1, bump_height=20, base_extent=4, base_adjust=-1) 32 | 33 | # Create a BSpline surface instance 34 | surf = BSpline.Surface() 35 | 36 | # Set degrees 37 | surf.degree_u = 3 38 | surf.degree_v = 3 39 | 40 | # Get the control points from the generated grid 41 | surf.ctrlpts2d = surfgrid.grid 42 | 43 | # Set knot vectors 44 | surf.knotvector_u = utilities.generate_knot_vector(surf.degree_u, surf.ctrlpts_size_u) 45 | surf.knotvector_v = utilities.generate_knot_vector(surf.degree_v, surf.ctrlpts_size_v) 46 | 47 | # Set sample size of the surface 48 | surf.sample_size = 50 49 | 50 | # Generate the visualization component and its configuration 51 | vis_config = vis.VisConfig(ctrlpts=True, legend=False) 52 | vis_comp = vis.VisSurface(vis_config) 53 | 54 | # Set visualization component of the surface 55 | surf.vis = vis_comp 56 | 57 | # Plot the surface 58 | surf.render() 59 | 60 | # Export the surface as a .stl file 61 | exchange.export_stl(surf, "bump_smoother_1pt-padding.stl") 62 | 63 | # Good to have something here to put a breakpoint 64 | pass 65 | -------------------------------------------------------------------------------- /grid/ex_surfgen01.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under the MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | This example illustrates the following operations: 10 | 11 | * Generating a control points grid 12 | * Generating a B-Spline surface and using the generated grid as its control points input 13 | * Splitting the B-Spline surface 14 | * Plotting the split surface using Plotly 15 | """ 16 | 17 | 18 | from geomdl import BSpline 19 | from geomdl import CPGen 20 | from geomdl import utilities 21 | from geomdl import operations 22 | from geomdl.visualization import VisPlotly 23 | 24 | # Generate a control points grid 25 | surfgrid = CPGen.Grid(50, 100) 26 | 27 | # This will generate a 32x32 grid 28 | surfgrid.generate(32, 32) 29 | 30 | # Generate bumps on the grid 31 | surfgrid.bumps(num_bumps=4, all_positive=True, bump_height=45, base_extent=4, base_adjust=1) 32 | 33 | # Create a BSpline surface instance 34 | surf = BSpline.Surface() 35 | 36 | # Set degrees 37 | surf.degree_u = 3 38 | surf.degree_v = 3 39 | 40 | # Get the control points from the generated grid 41 | surf.ctrlpts2d = surfgrid.grid 42 | 43 | # Set knot vectors 44 | surf.knotvector_u = utilities.generate_knot_vector(surf.degree_u, surf.ctrlpts_size_u) 45 | surf.knotvector_v = utilities.generate_knot_vector(surf.degree_v, surf.ctrlpts_size_v) 46 | 47 | # Split the surface at v = 0.35 48 | split_surf = operations.split_surface_v(surf, 0.35) 49 | 50 | # Set sample size of the split surface 51 | split_surf.sample_size = 25 52 | 53 | # Generate the visualization component and its configuration 54 | vis_config = VisPlotly.VisConfig(ctrlpts=False, legend=False) 55 | vis_comp = VisPlotly.VisSurface(vis_config) 56 | 57 | # Set visualization component of the split surface 58 | split_surf.vis = vis_comp 59 | 60 | # Plot the split surface 61 | split_surf.render() 62 | 63 | # Good to have something here to put a breakpoint 64 | pass 65 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | NURBS-Python (geomdl) Examples 2 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3 | 4 | |DOI|_ |RTD|_ |PYPI|_ |ANACONDA|_ 5 | 6 | This repository contains API demonstration scripts for the NURBS-Python (geomdl) package. 7 | Please refer to our `SoftwareX article `_ 8 | for more details. 9 | 10 | More on NURBS-Python (geomdl) 11 | ============================= 12 | 13 | * `Documentation `_ 14 | * `Downloads `_ 15 | 16 | Directory Structure 17 | =================== 18 | 19 | * ``curve2d/`` contains 2-dimensional curve examples 20 | * ``curve3d/`` contains 3-dimensional curve examples 21 | * ``surface/`` contains surface examples 22 | * ``volume/`` contains volume examples 23 | * ``grid/`` contains examples for surface grid generator 24 | * ``visualization/`` contains advanced visualization examples. 25 | * ``bezier/`` contains Bezier curve and surface examples 26 | * ``shapes/`` contains examples demonstrating ``geomdl.shapes`` component 27 | * ``exchange/`` contains examples demonstrating ``geomdl.exchange`` module 28 | * ``compat/`` contains examples for data conversion 29 | * ``objects/`` contains examples of the real world objects 30 | * ``fitting/`` contains examples for curve and surface fitting 31 | 32 | Author 33 | ====== 34 | 35 | * Onur Rauf Bingol (`@orbingol `_) 36 | 37 | License 38 | ======= 39 | 40 | `MIT `_ 41 | 42 | 43 | .. |DOI| image:: https://zenodo.org/badge/DOI/10.5281/zenodo.1346376.svg 44 | .. _DOI: https://doi.org/10.5281/zenodo.1346376 45 | 46 | .. |RTD| image:: https://readthedocs.org/projects/nurbs-python/badge/?version=latest 47 | .. _RTD: http://nurbs-python.readthedocs.io/en/latest/?badge=latest 48 | 49 | .. |PYPI| image:: https://img.shields.io/pypi/v/geomdl.svg 50 | .. _PYPI: https://pypi.org/project/geomdl/ 51 | 52 | .. |ANACONDA| image:: https://anaconda.org/orbingol/geomdl/badges/version.svg 53 | .. _ANACONDA: https://anaconda.org/orbingol/geomdl 54 | -------------------------------------------------------------------------------- /surface/ex_curved.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | This example is contributed by Dr. Adarsh Krishnamurthy (@k-adarsh) 10 | """ 11 | 12 | import os 13 | from geomdl import NURBS 14 | from geomdl.visualization import VisMPL 15 | from matplotlib import cm 16 | 17 | 18 | # Fix file path 19 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 20 | 21 | # Control points 22 | ctrlpts = [[[25.0, -25.0, 0.0, 1.0], [15.0, -25.0, 0.0, 1.0], [5.0, -25.0, 0.0, 1.0], 23 | [-5.0, -25.0, 0.0, 1.0], [-15.0, -25.0, 0.0, 1.0], [-25.0, -25.0, 0.0, 1.0]], 24 | [[25.0, -15.0, 0.0, 1.0], [15.0, -15.0, 0.0, 1.0], [5.0, -15.0, 0.0, 1.0], 25 | [-5.0, -15.0, 0.0, 1.0], [-15.0, -15.0, 0.0, 1.0], [-25.0, -15.0, 0.0, 1.0]], 26 | [[25.0, -5.0, 5.0, 1.0], [15.0, -5.0, 5.0, 1.0], [5.0, -5.0, 5.0, 1.0], 27 | [-5.0, -5.0, 5.0, 1.0], [-15.0, -5.0, 5.0, 1.0], [-25.0, -5.0, 5.0, 1.0]], 28 | [[25.0, 5.0, 5.0, 1.0], [15.0, 5.0, 5.0, 1.0], [5.0, 5.0, 5.0, 1.0], 29 | [-5.0, 5.0, 5.0, 1.0], [-15.0, 5.0, 5.0, 1.0], [-25.0, 5.0, 5.0, 1.0]], 30 | [[25.0, 15.0, 0.0, 1.0], [15.0, 15.0, 0.0, 1.0], [5.0, 15.0, 5.0, 1.0], 31 | [-5.0, 15.0, 5.0, 1.0], [-15.0, 15.0, 0.0, 1.0], [-25.0, 15.0, 0.0, 1.0]], 32 | [[25.0, 25.0, 0.0, 1.0], [15.0, 25.0, 0.0, 1.0], [5.0, 25.0, 5.0, 1.0], 33 | [-5.0, 25.0, 5.0, 1.0], [-15.0, 25.0, 0.0, 1.0], [-25.0, 25.0, 0.0, 1.0]]] 34 | 35 | # Generate surface 36 | surf = NURBS.Surface() 37 | surf.degree_u = 3 38 | surf.degree_v = 3 39 | surf.ctrlpts2d = ctrlpts 40 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 41 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 42 | surf.sample_size = 30 43 | 44 | # Visualize surface 45 | surf.vis = VisMPL.VisSurfTriangle() 46 | surf.render(colormap=cm.summer) 47 | 48 | # Good to have something here to put a breakpoint 49 | pass 50 | -------------------------------------------------------------------------------- /fitting/approximation/global_surface.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | 9 | Surface fitting by global approximation 10 | """ 11 | 12 | from geomdl import construct 13 | from geomdl import fitting 14 | from geomdl.visualization import VisMPL as vis 15 | 16 | 17 | # Data set 18 | points = ((-5, -5, 0), (-2.5, -5, 0), (0, -5, 0), (2.5, -5, 0), (5, -5, 0), (7.5, -5, 0), (10, -5, 0), 19 | (-5, 0, 3), (-2.5, 0, 3), (0, 0, 3), (2.5, 0, 3), (5, 0, 3), (7.5, 0, 3), (10, 0, 3), 20 | (-5, 5, 0), (-2.5, 5, 0), (0, 5, 0), (2.5, 5, 0), (5, 5, 0), (7.5, 5, 0), (10, 5, 0), 21 | (-5, 7.5, -3), (-2.5, 7.5, -3), (0, 7.5, -3), (2.5, 7.5, -3), (5, 7.5, -3), (7.5, 7.5, -3), (10, 7.5, -3), 22 | (-5, 10, 0), (-2.5, 10, 0), (0, 10, 0), (2.5, 10, 0), (5, 10, 0), (7.5, 10, 0), (10, 10, 0)) 23 | size_u = 5 24 | size_v = 7 25 | degree_u = 2 26 | degree_v = 3 27 | 28 | # Do global surface approximation 29 | surf = fitting.approximate_surface(points, size_u, size_v, degree_u, degree_v) 30 | # surf = fitting.approximate_surface(points, size_u, size_v, degree_u, degree_v, ctrlpts_size_u=3, ctrlpts_size_v=4) 31 | 32 | # Extract curves from the approximated surface 33 | surf_curves = construct.extract_curves(surf) 34 | plot_extras = [ 35 | dict( 36 | points=surf_curves['u'][0].evalpts, 37 | name="u", 38 | color="cyan", 39 | size=5 40 | ), 41 | dict( 42 | points=surf_curves['v'][0].evalpts, 43 | name="v", 44 | color="magenta", 45 | size=5 46 | ) 47 | ] 48 | 49 | # Plot the interpolated surface 50 | surf.delta = 0.05 51 | surf.vis = vis.VisSurface() 52 | surf.render(extras=plot_extras) 53 | 54 | # # Visualize data and evaluated points together 55 | # import numpy as np 56 | # import matplotlib.pyplot as plt 57 | # evalpts = np.array(surf.evalpts) 58 | # pts = np.array(points) 59 | # fig = plt.figure() 60 | # ax = plt.axes(projection='3d') 61 | # ax.scatter(evalpts[:, 0], evalpts[:, 1], evalpts[:, 2]) 62 | # ax.scatter(pts[:, 0], pts[:, 1], pts[:, 2], color="red") 63 | # plt.show() 64 | -------------------------------------------------------------------------------- /surface/trimming/trim_separate_surfaces.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | import os 11 | from copy import deepcopy 12 | from geomdl import BSpline 13 | from geomdl import operations 14 | from geomdl import multi 15 | from geomdl import tessellate 16 | from geomdl.visualization import VisVTK as vis 17 | from geomdl import knotvector 18 | 19 | 20 | # Fix file path 21 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 22 | 23 | # Create a planar BSpline surface (surface 1) 24 | surf1 = BSpline.Surface() 25 | surf1.degree = (1, 1) 26 | surf1.ctrlpts_size_u = 2 27 | surf1.ctrlpts_size_v = 2 28 | surf1.ctrlpts = ( 29 | (0, 0, 0), (0, 1, 0), 30 | (1, 0, 0), (1, 1, 0) 31 | ) 32 | surf1.knotvector = ((0, 0, 1, 1), (0, 0, 1, 1)) 33 | 34 | # Create another surface from the initial one (surface 2) 35 | surf2 = operations.rotate(surf1, 90, axis=1) 36 | operations.translate(surf2, (0.5, 0, 1), inplace=True) 37 | 38 | # Create another surface from the initial one (surface 3) 39 | surf3 = operations.rotate(surf1, 45, axis=0) 40 | operations.translate(surf3, (1, 0.25, 0.5), inplace=True) 41 | 42 | # Create trim curves 43 | trim1 = BSpline.Curve() 44 | trim1.degree = 1 45 | trim1.ctrlpts = ( 46 | (1, 0), (0.95, 0.5), (1, 1), (0, 1), (0.05, 0.5), (0, 0), (1, 0) 47 | ) 48 | trim1.knotvector = knotvector.generate(trim1.degree, trim1.ctrlpts_size) 49 | trim1.delta = 0.001 50 | trim1.opt = ['reversed', 1] 51 | # operations.scale(trim1, 0.5, inplace=True) 52 | 53 | trim2 = deepcopy(trim1) 54 | trim2.opt = ['reversed', 0] 55 | 56 | # Add trim to surface 1 57 | surf1.trims = [trim1] 58 | 59 | # Add trim to surface 3 60 | surf3.trims = [trim2] 61 | 62 | # Visualize all surfaces 63 | mult = multi.SurfaceContainer(surf1, surf2, surf3) 64 | mult.sample_size = 30 65 | mult.tessellator = tessellate.TrimTessellate() 66 | 67 | # Show trim curves but don't show control points 68 | vis_conf = vis.VisConfig(trims=True, ctrlpts=False) 69 | mult.vis = vis.VisSurface(vis_conf) 70 | 71 | # Render with colors 72 | # mult.render(evalcolor=["steelblue", "red", "green"]) 73 | 74 | from geomdl import exchange 75 | exchange.export_json(mult, "mult.json") 76 | -------------------------------------------------------------------------------- /volume/ex_knot_removal.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | from geomdl import BSpline 11 | from geomdl import CPGen 12 | from geomdl import utilities 13 | from geomdl import construct 14 | from geomdl import operations 15 | from geomdl.visualization import VisMPL as vis 16 | 17 | 18 | # Generate control points grid for Surface #1 19 | sg01 = CPGen.Grid(15, 10, z_value=0.0) 20 | sg01.generate(8, 8) 21 | 22 | # Create a BSpline surface instance 23 | surf01 = BSpline.Surface() 24 | 25 | # Set degrees 26 | surf01.degree_u = 1 27 | surf01.degree_v = 1 28 | 29 | # Get the control points from the generated grid 30 | surf01.ctrlpts2d = sg01.grid 31 | 32 | # Set knot vectors 33 | surf01.knotvector_u = utilities.generate_knot_vector(surf01.degree_u, surf01.ctrlpts_size_u) 34 | surf01.knotvector_v = utilities.generate_knot_vector(surf01.degree_v, surf01.ctrlpts_size_v) 35 | 36 | # Generate control points grid for Surface #2 37 | sg02 = CPGen.Grid(15, 10, z_value=1.0) 38 | sg02.generate(8, 8) 39 | 40 | # Create a BSpline surface instance 41 | surf02 = BSpline.Surface() 42 | 43 | # Set degrees 44 | surf02.degree_u = 1 45 | surf02.degree_v = 1 46 | 47 | # Get the control points from the generated grid 48 | surf02.ctrlpts2d = sg02.grid 49 | 50 | # Set knot vectors 51 | surf02.knotvector_u = utilities.generate_knot_vector(surf02.degree_u, surf02.ctrlpts_size_u) 52 | surf02.knotvector_v = utilities.generate_knot_vector(surf02.degree_v, surf02.ctrlpts_size_v) 53 | 54 | # Generate control points grid for Surface #3 55 | sg03 = CPGen.Grid(15, 10, z_value=2.0) 56 | sg03.generate(8, 8) 57 | 58 | # Create a BSpline surface instance 59 | surf03 = BSpline.Surface() 60 | 61 | # Set degrees 62 | surf03.degree_u = 1 63 | surf03.degree_v = 1 64 | 65 | # Get the control points from the generated grid 66 | surf03.ctrlpts2d = sg03.grid 67 | 68 | # Set knot vectors 69 | surf03.knotvector_u = utilities.generate_knot_vector(surf03.degree_u, surf03.ctrlpts_size_u) 70 | surf03.knotvector_v = utilities.generate_knot_vector(surf03.degree_v, surf03.ctrlpts_size_v) 71 | 72 | # Construct the parametric volume with a uniform knot vector 73 | pvolume = construct.construct_volume('w', surf01, surf02, surf03, degree=1) 74 | 75 | # Visualize volume 76 | pvolume.vis = vis.VisVolume(vis.VisConfig(ctrlpts=True, evalpts=False)) 77 | pvolume.render() 78 | 79 | # Remove knots 80 | pvolume.remove_knot(w=0.5) 81 | 82 | # Visualize volume after knot removal 83 | pvolume.render() 84 | 85 | # Good to have something here to put a breakpoint 86 | pass 87 | -------------------------------------------------------------------------------- /volume/ex_knot_insertion2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | from geomdl import BSpline 11 | from geomdl import CPGen 12 | from geomdl import utilities 13 | from geomdl import construct 14 | from geomdl import operations 15 | from geomdl.visualization import VisMPL as vis 16 | 17 | 18 | # Generate control points grid for Surface #1 19 | sg01 = CPGen.Grid(15, 10, z_value=0.0) 20 | sg01.generate(4, 3) 21 | 22 | # Create a BSpline surface instance 23 | surf01 = BSpline.Surface() 24 | 25 | # Set degrees 26 | surf01.degree_u = 2 27 | surf01.degree_v = 1 28 | 29 | # Get the control points from the generated grid 30 | surf01.ctrlpts2d = sg01.grid 31 | 32 | # Set knot vectors 33 | surf01.knotvector_u = utilities.generate_knot_vector(surf01.degree_u, surf01.ctrlpts_size_u) 34 | surf01.knotvector_v = utilities.generate_knot_vector(surf01.degree_v, surf01.ctrlpts_size_v) 35 | 36 | # Generate control points grid for Surface #2 37 | sg02 = CPGen.Grid(15, 10, z_value=1.0) 38 | sg02.generate(4, 3) 39 | 40 | # Create a BSpline surface instance 41 | surf02 = BSpline.Surface() 42 | 43 | # Set degrees 44 | surf02.degree_u = 2 45 | surf02.degree_v = 1 46 | 47 | # Get the control points from the generated grid 48 | surf02.ctrlpts2d = sg02.grid 49 | 50 | # Set knot vectors 51 | surf02.knotvector_u = utilities.generate_knot_vector(surf02.degree_u, surf02.ctrlpts_size_u) 52 | surf02.knotvector_v = utilities.generate_knot_vector(surf02.degree_v, surf02.ctrlpts_size_v) 53 | 54 | # Generate control points grid for Surface #3 55 | sg03 = CPGen.Grid(15, 10, z_value=2.0) 56 | sg03.generate(4, 3) 57 | 58 | # Create a BSpline surface instance 59 | surf03 = BSpline.Surface() 60 | 61 | # Set degrees 62 | surf03.degree_u = 2 63 | surf03.degree_v = 1 64 | 65 | # Get the control points from the generated grid 66 | surf03.ctrlpts2d = sg03.grid 67 | 68 | # Set knot vectors 69 | surf03.knotvector_u = utilities.generate_knot_vector(surf03.degree_u, surf03.ctrlpts_size_u) 70 | surf03.knotvector_v = utilities.generate_knot_vector(surf03.degree_v, surf03.ctrlpts_size_v) 71 | 72 | # Construct the parametric volume with a uniform knot vector 73 | pvolume = construct.construct_volume('w', surf01, surf02, surf03, degree=1) 74 | 75 | # Visualize volume 76 | pvolume.vis = vis.VisVolume(vis.VisConfig(ctrlpts=True, evalpts=False)) 77 | pvolume.render() 78 | 79 | # Insert knots 80 | operations.insert_knot(pvolume, [0.9, 0.1, None], [2, 1, 0]) 81 | 82 | # Visualize volume after knot insertions 83 | pvolume.render() 84 | 85 | # Good to have something here to put a breakpoint 86 | pass 87 | -------------------------------------------------------------------------------- /volume/ex_knot_insertion1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | from geomdl import BSpline 11 | from geomdl import CPGen 12 | from geomdl import utilities 13 | from geomdl import construct 14 | from geomdl import operations 15 | from geomdl.visualization import VisMPL as vis 16 | 17 | 18 | # Generate control points grid for Surface #1 19 | sg01 = CPGen.Grid(15, 10, z_value=0.0) 20 | sg01.generate(8, 8) 21 | 22 | # Create a BSpline surface instance 23 | surf01 = BSpline.Surface() 24 | 25 | # Set degrees 26 | surf01.degree_u = 1 27 | surf01.degree_v = 1 28 | 29 | # Get the control points from the generated grid 30 | surf01.ctrlpts2d = sg01.grid 31 | 32 | # Set knot vectors 33 | surf01.knotvector_u = utilities.generate_knot_vector(surf01.degree_u, surf01.ctrlpts_size_u) 34 | surf01.knotvector_v = utilities.generate_knot_vector(surf01.degree_v, surf01.ctrlpts_size_v) 35 | 36 | # Generate control points grid for Surface #2 37 | sg02 = CPGen.Grid(15, 10, z_value=1.0) 38 | sg02.generate(8, 8) 39 | 40 | # Create a BSpline surface instance 41 | surf02 = BSpline.Surface() 42 | 43 | # Set degrees 44 | surf02.degree_u = 1 45 | surf02.degree_v = 1 46 | 47 | # Get the control points from the generated grid 48 | surf02.ctrlpts2d = sg02.grid 49 | 50 | # Set knot vectors 51 | surf02.knotvector_u = utilities.generate_knot_vector(surf02.degree_u, surf02.ctrlpts_size_u) 52 | surf02.knotvector_v = utilities.generate_knot_vector(surf02.degree_v, surf02.ctrlpts_size_v) 53 | 54 | # Generate control points grid for Surface #3 55 | sg03 = CPGen.Grid(15, 10, z_value=2.0) 56 | sg03.generate(8, 8) 57 | 58 | # Create a BSpline surface instance 59 | surf03 = BSpline.Surface() 60 | 61 | # Set degrees 62 | surf03.degree_u = 1 63 | surf03.degree_v = 1 64 | 65 | # Get the control points from the generated grid 66 | surf03.ctrlpts2d = sg03.grid 67 | 68 | # Set knot vectors 69 | surf03.knotvector_u = utilities.generate_knot_vector(surf03.degree_u, surf03.ctrlpts_size_u) 70 | surf03.knotvector_v = utilities.generate_knot_vector(surf03.degree_v, surf03.ctrlpts_size_v) 71 | 72 | # Construct the parametric volume with a uniform knot vector 73 | pvolume = construct.construct_volume('w', surf01, surf02, surf03, degree=1) 74 | 75 | # Visualize volume 76 | pvolume.vis = vis.VisVolume(vis.VisConfig(ctrlpts=True, evalpts=False)) 77 | pvolume.render() 78 | 79 | # Insert knots 80 | operations.insert_knot(pvolume, [None, None, 0.25], [0, 0, 1]) 81 | operations.insert_knot(pvolume, [None, None, 0.75], [0, 0, 1]) 82 | 83 | # Visualize volume after knot insertions 84 | pvolume.render() 85 | 86 | # Good to have something here to put a breakpoint 87 | pass 88 | -------------------------------------------------------------------------------- /surface/trimming/trim_multi.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import exchange 13 | from geomdl import knotvector 14 | from geomdl import operations 15 | from geomdl import tessellate 16 | from geomdl.visualization import VisVTK as vis 17 | from geomdl.shapes import curve2d 18 | 19 | 20 | # Fix file path 21 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 22 | 23 | ######################## 24 | # GENERATE TRIM CURVES # 25 | ######################## 26 | 27 | # Create a B-Spline curve instance 28 | curve = BSpline.Curve() 29 | 30 | # Set degree 31 | curve.degree = 1 32 | 33 | # Set control points 34 | curve.ctrlpts = [[1.1, 1.1], [0.24, 1.1], [0.24, 1.1], [0.24, 0.49], [0.24, 0.49], [1.1, 0.49], [1.1, 0.49], [1.1, 1.1]] 35 | 36 | # Auto-generate knot vector 37 | curve.knotvector = knotvector.generate(curve.degree, curve.ctrlpts_size) 38 | 39 | # Translate original curve to generate 1st trim curve 40 | trim_curve1 = operations.translate(curve, (0.25, 0.25)) 41 | 42 | # Translate original curve to generate 2nd trim curve 43 | trim_curve2 = operations.translate(curve, (-0.75, -0.75)) 44 | 45 | # Generate a NURBS full circle from 9 control points 46 | circle = curve2d.full_circle(radius=0.15) 47 | 48 | # Translate circle 49 | operations.translate(circle, (0.5, 0.5), inplace=True) 50 | 51 | # Set evaluation deltas 52 | trim_curve1.delta = 0.01 53 | trim_curve2.delta = 0.01 54 | circle.delta = 0.01 55 | 56 | # List of trim curves 57 | trim_curves = [trim_curve1, trim_curve2, circle] 58 | 59 | #################### 60 | # GENERATE SURFACE # 61 | #################### 62 | 63 | # Create a BSpline surface instance 64 | surf = BSpline.Surface() 65 | 66 | # Set degrees 67 | surf.degree_u = 3 68 | surf.degree_v = 3 69 | 70 | # Set control points 71 | surf.set_ctrlpts(*exchange.import_txt("../ex_surface01.cpt", two_dimensional=True)) 72 | 73 | # Set knot vectors 74 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 75 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 76 | 77 | # Set evaluation delta 78 | surf.delta = 0.015 79 | 80 | # Set surface tessellation component 81 | surf.tessellator = tessellate.TrimTessellate() 82 | 83 | # Set trim curves 84 | surf.trims = trim_curves 85 | 86 | # Set visualization component 87 | visconf = vis.VisConfig(ctrlpts=False, legend=False, trims=False) 88 | surf.vis = vis.VisSurface(config=visconf) 89 | 90 | # Visualize surface 91 | surf.render() 92 | 93 | # Save as Wavefront OBJ file 94 | # exchange.export_obj(surf, "trim_multi.obj") 95 | 96 | # Good to have something here to put a breakpoint 97 | pass 98 | -------------------------------------------------------------------------------- /objects/duck2.ctrlpts: -------------------------------------------------------------------------------- 1 | -0.279652 0.207397 1.75598 2 | -0.291276 0.195828 1.75911 3 | -0.305767 0.156964 1.76536 4 | -0.323195 0.0976051 1.77348 5 | -0.324811 0.0575112 1.7749 6 | -0.324878 0.00965657 1.77496 7 | -0.323096 -0.0303954 1.7734 8 | -0.305497 -0.0905963 1.76504 9 | -0.291682 -0.12612 1.75909 10 | -0.279652 -0.138572 1.75598 11 | -0.269113 0.222966 1.74866 12 | -0.263871 0.258906 1.77657 13 | -0.3196 0.232048 1.81233 14 | -0.398219 0.161683 1.84252 15 | -0.413466 0.081587 1.84607 16 | -0.41368 -0.0156633 1.84616 17 | -0.398615 -0.0957014 1.84248 18 | -0.319645 -0.166603 1.81134 19 | -0.262126 -0.192803 1.77911 20 | -0.269113 -0.154139 1.74866 21 | -0.242427 0.248091 1.74002 22 | -0.250205 0.313952 1.78585 23 | -0.206038 0.298954 1.83128 24 | -0.288104 0.183633 1.93874 25 | -0.310232 0.0882907 1.94062 26 | -0.310299 -0.023674 1.94074 27 | -0.288436 -0.119573 1.93833 28 | -0.206498 -0.233814 1.82973 29 | -0.246528 -0.250884 1.7902 30 | -0.242427 -0.179251 1.74002 31 | -0.179498 0.282063 1.72166 32 | -0.17679 0.326377 1.75992 33 | -0.110471 0.312635 1.79867 34 | -0.254015 0.182324 1.82418 35 | -0.251274 0.0829962 1.82146 36 | -0.251233 -0.0178177 1.82168 37 | -0.255262 -0.119254 1.8228 38 | -0.111415 -0.246282 1.797 39 | -0.173192 -0.26317 1.76401 40 | -0.179498 -0.21315 1.72166 41 | -0.0461346 0.30226 1.6761 42 | -0.0309577 0.346416 1.72013 43 | -0.0164148 0.32483 1.77109 44 | -0.0589178 0.175862 1.80591 45 | -0.0531642 0.0769225 1.81173 46 | -0.0531119 -0.0109558 1.8121 47 | -0.0603414 -0.11356 1.80335 48 | -0.017706 -0.257577 1.76904 49 | -0.0288649 -0.282457 1.72533 50 | -0.0461346 -0.233217 1.6761 51 | 0.223016 0.355831 1.68021 52 | 0.220378 0.396637 1.72855 53 | 0.177912 0.348447 1.78214 54 | 0.111397 0.163111 1.8289 55 | 0.114733 0.0725392 1.86208 56 | 0.114717 -0.00559361 1.86264 57 | 0.111044 -0.101223 1.82465 58 | 0.176651 -0.280222 1.77884 59 | 0.219996 -0.331394 1.73441 60 | 0.223016 -0.286637 1.68021 61 | 0.477456 0.492685 1.69775 62 | 0.478012 0.525066 1.7464 63 | 0.389698 0.419772 1.79903 64 | 0.241845 0.157044 1.86838 65 | 0.248091 0.082689 1.948 66 | 0.248091 -0.0151387 1.94871 67 | 0.243166 -0.0948644 1.86227 68 | 0.388089 -0.350312 1.79395 69 | 0.478027 -0.458802 1.7521 70 | 0.477456 -0.423169 1.69775 71 | 0.625729 0.586266 1.7479 72 | 0.624419 0.607445 1.79189 73 | 0.485322 0.455259 1.82632 74 | 0.257618 0.145814 1.89966 75 | 0.271645 0.105257 2.03203 76 | 0.271645 -0.038398 2.03279 77 | 0.261609 -0.082252 1.89132 78 | 0.484169 -0.385149 1.81866 79 | 0.624385 -0.541398 1.79651 80 | 0.625729 -0.517056 1.7479 81 | 0.657067 0.586266 1.7479 82 | 0.646467 0.575674 1.7919 83 | 0.502994 0.41679 1.82461 84 | 0.283455 0.142402 1.89403 85 | 0.303728 0.110488 2.02461 86 | 0.303728 -0.0442053 2.02538 87 | 0.286523 -0.077615 1.88597 88 | 0.502318 -0.349657 1.81709 89 | 0.646195 -0.50881 1.79653 90 | 0.657067 -0.517056 1.7479 91 | -------------------------------------------------------------------------------- /visualization/mpl_curve2d_tangents.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | Visualization Examples for the NURBS-Python Package 5 | Released under The MIT License 6 | Developed by Onur Rauf Bingol (c) 2018 7 | 8 | Creates a 2-dimensional curve and plots tangent vectors 9 | """ 10 | 11 | import os 12 | from geomdl import BSpline 13 | from geomdl import utilities 14 | from geomdl import exchange 15 | from geomdl import operations 16 | 17 | import numpy as np 18 | import matplotlib.pyplot as plt 19 | 20 | 21 | # Fix file path 22 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 23 | 24 | # 25 | # Curve Evaluation 26 | # 27 | 28 | # Create a BSpline curve instance 29 | curve = BSpline.Curve() 30 | 31 | # Set degree 32 | curve.degree = 3 33 | 34 | # Set control points 35 | curve.ctrlpts = exchange.import_txt("../curve2d/ex_curve03.cpt") 36 | 37 | # Auto-generate knot vector 38 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 39 | 40 | # Set evaluation delta 41 | curve.delta = 0.01 42 | 43 | # Evaulate curve 44 | curve.evaluate() 45 | 46 | # 47 | # Tangent Vector Evaluation 48 | # 49 | 50 | # Store tangent vectors in a list for plotting 51 | curvetan = [] 52 | 53 | # Evaluate curve tangent at u = 0.0 54 | ct1 = operations.tangent(curve, 0.0, normalize=True) 55 | curvetan.append(ct1) 56 | 57 | # Evaluate curve tangent at u = 0.2 58 | ct2 = operations.tangent(curve, 0.2, normalize=True) 59 | curvetan.append(ct2) 60 | 61 | # Evaluate curve tangent at u = 0.5 62 | ct3 = operations.tangent(curve, 0.5, normalize=True) 63 | curvetan.append(ct3) 64 | 65 | # Evaluate curve tangent at u = 0.6 66 | ct4 = operations.tangent(curve, 0.6, normalize=True) 67 | curvetan.append(ct4) 68 | 69 | # Evaluate curve tangent at u = 0.8 70 | ct5 = operations.tangent(curve, 0.8, normalize=True) 71 | curvetan.append(ct5) 72 | 73 | # Evaluate curve tangent at u = 1.0 74 | ct6 = operations.tangent(curve, 1.0, normalize=True) 75 | curvetan.append(ct6) 76 | 77 | # 78 | # Control Points, Curve and Tangent Vector Plotting 79 | # 80 | 81 | # Arrange control points and evaluated curve points for plotting 82 | ctrlpts = np.array(curve.ctrlpts) 83 | curvepts = np.array(curve.evalpts) 84 | 85 | # Convert tangent list into a NumPy array 86 | ctarr = np.array(curvetan) 87 | 88 | # Plot using Matplotlib 89 | plt.figure(figsize=(10.67, 8), dpi=96) 90 | yaxis = plt.plot((-1, 25), (0, 0), "k-") # y-axis line 91 | cppolygon, = plt.plot(ctrlpts[:, 0], ctrlpts[:, 1], color='black', linestyle='-.', marker='o', markersize='3') # control points polygon 92 | curveplt, = plt.plot(curvepts[:, 0], curvepts[:, 1], color='green', linestyle='-') # evaluated curve points 93 | tanline = plt.quiver(ctarr[:, 0, 0], ctarr[:, 0, 1], ctarr[:, 1, 0], ctarr[:, 1, 1], color='blue', angles='xy', scale_units='xy', scale=1, width=0.003) # tangents 94 | tanlinekey = plt.quiverkey(tanline, 23.75, -14.5, 35, "Tangent Vectors", coordinates='data', labelpos='W') 95 | plt.legend([cppolygon, curveplt], ["Control Points", "Evaluated Curve"]) 96 | plt.axis([-1, 25, -15, 15]) 97 | plt.show() 98 | -------------------------------------------------------------------------------- /volume/ex_planar_bspline.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2018 8 | """ 9 | 10 | from geomdl import BSpline 11 | from geomdl import multi 12 | from geomdl import CPGen 13 | from geomdl import utilities 14 | from geomdl import construct 15 | from geomdl.visualization import VisMPL as vis 16 | 17 | 18 | # Required for multiprocessing module 19 | if __name__ == "__main__": 20 | # Generate control points grid for Surface #1 21 | sg01 = CPGen.Grid(15, 10, z_value=0.0) 22 | sg01.generate(8, 8) 23 | 24 | # Create a BSpline surface instance 25 | surf01 = BSpline.Surface() 26 | 27 | # Set degrees 28 | surf01.degree_u = 1 29 | surf01.degree_v = 1 30 | 31 | # Get the control points from the generated grid 32 | surf01.ctrlpts2d = sg01.grid 33 | 34 | # Set knot vectors 35 | surf01.knotvector_u = utilities.generate_knot_vector(surf01.degree_u, surf01.ctrlpts_size_u) 36 | surf01.knotvector_v = utilities.generate_knot_vector(surf01.degree_v, surf01.ctrlpts_size_v) 37 | 38 | # Generate control points grid for Surface #2 39 | sg02 = CPGen.Grid(15, 10, z_value=1.0) 40 | sg02.generate(8, 8) 41 | 42 | # Create a BSpline surface instance 43 | surf02 = BSpline.Surface() 44 | 45 | # Set degrees 46 | surf02.degree_u = 1 47 | surf02.degree_v = 1 48 | 49 | # Get the control points from the generated grid 50 | surf02.ctrlpts2d = sg02.grid 51 | 52 | # Set knot vectors 53 | surf02.knotvector_u = utilities.generate_knot_vector(surf02.degree_u, surf02.ctrlpts_size_u) 54 | surf02.knotvector_v = utilities.generate_knot_vector(surf02.degree_v, surf02.ctrlpts_size_v) 55 | 56 | # Generate control points grid for Surface #3 57 | sg03 = CPGen.Grid(15, 10, z_value=2.0) 58 | sg03.generate(8, 8) 59 | 60 | # Create a BSpline surface instance 61 | surf03 = BSpline.Surface() 62 | 63 | # Set degrees 64 | surf03.degree_u = 1 65 | surf03.degree_v = 1 66 | 67 | # Get the control points from the generated grid 68 | surf03.ctrlpts2d = sg03.grid 69 | 70 | # Set knot vectors 71 | surf03.knotvector_u = utilities.generate_knot_vector(surf03.degree_u, surf03.ctrlpts_size_u) 72 | surf03.knotvector_v = utilities.generate_knot_vector(surf03.degree_v, surf03.ctrlpts_size_v) 73 | 74 | # Construct the volume 75 | pvolume = construct.construct_volume('w', surf01, surf02, surf03, degree=1) 76 | 77 | # Voxelize the volume 78 | pvolume.vis = vis.VisVoxel(vis.VisConfig(ctrlpts=False)) 79 | pvolume.delta_u = pvolume.delta_v = 0.025 80 | pvolume.delta_w = 0.05 81 | pvolume.render(evalcolor="firebrick", num_procs=16) 82 | 83 | # Extract the isosurface 84 | surfvol = construct.extract_isosurface(pvolume) 85 | msurf = multi.SurfaceContainer(surfvol) 86 | 87 | # Visualize the isosurface 88 | msurf.vis = vis.VisSurface(vis.VisConfig(ctrlpts=False)) 89 | msurf.delta = 0.05 90 | msurf.render(evalcolor=["skyblue", "cadetblue", "crimson", "crimson", "crimson", "crimson"]) 91 | 92 | # Good to have something here to put a breakpoint 93 | pass 94 | -------------------------------------------------------------------------------- /volume/ex_knot_refinement.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | from geomdl import BSpline 11 | from geomdl import CPGen 12 | from geomdl import utilities 13 | from geomdl import construct 14 | from geomdl import operations 15 | from geomdl.visualization import VisMPL as vis 16 | 17 | 18 | # Generate control points grid for Surface #1 19 | sg01 = CPGen.Grid(15, 10, z_value=0.0) 20 | sg01.generate(8, 10) 21 | 22 | # Create a BSpline surface instance 23 | surf01 = BSpline.Surface() 24 | 25 | # Set degrees 26 | surf01.degree_u = 2 27 | surf01.degree_v = 3 28 | 29 | # Get the control points from the generated grid 30 | surf01.ctrlpts2d = sg01.grid 31 | 32 | # Set knot vectors 33 | surf01.knotvector_u = utilities.generate_knot_vector(surf01.degree_u, surf01.ctrlpts_size_u) 34 | surf01.knotvector_v = utilities.generate_knot_vector(surf01.degree_v, surf01.ctrlpts_size_v) 35 | 36 | # Generate control points grid for Surface #2 37 | sg02 = CPGen.Grid(15, 10, z_value=1.0) 38 | sg02.generate(8, 10) 39 | 40 | # Create a BSpline surface instance 41 | surf02 = BSpline.Surface() 42 | 43 | # Set degrees 44 | surf02.degree_u = 2 45 | surf02.degree_v = 3 46 | 47 | # Get the control points from the generated grid 48 | surf02.ctrlpts2d = sg02.grid 49 | 50 | # Set knot vectors 51 | surf02.knotvector_u = utilities.generate_knot_vector(surf02.degree_u, surf02.ctrlpts_size_u) 52 | surf02.knotvector_v = utilities.generate_knot_vector(surf02.degree_v, surf02.ctrlpts_size_v) 53 | 54 | # Generate control points grid for Surface #3 55 | sg03 = CPGen.Grid(15, 10, z_value=2.0) 56 | sg03.generate(8, 10) 57 | 58 | # Create a BSpline surface instance 59 | surf03 = BSpline.Surface() 60 | 61 | # Set degrees 62 | surf03.degree_u = 2 63 | surf03.degree_v = 3 64 | 65 | # Get the control points from the generated grid 66 | surf03.ctrlpts2d = sg03.grid 67 | 68 | # Set knot vectors 69 | surf03.knotvector_u = utilities.generate_knot_vector(surf03.degree_u, surf03.ctrlpts_size_u) 70 | surf03.knotvector_v = utilities.generate_knot_vector(surf03.degree_v, surf03.ctrlpts_size_v) 71 | 72 | # Generate control points grid for Surface #4 73 | sg04 = CPGen.Grid(15, 10, z_value=3.0) 74 | sg04.generate(8, 10) 75 | 76 | # Create a BSpline surface instance 77 | surf04 = BSpline.Surface() 78 | 79 | # Set degrees 80 | surf04.degree_u = 2 81 | surf04.degree_v = 3 82 | 83 | # Get the control points from the generated grid 84 | surf04.ctrlpts2d = sg04.grid 85 | 86 | # Set knot vectors 87 | surf04.knotvector_u = utilities.generate_knot_vector(surf04.degree_u, surf04.ctrlpts_size_u) 88 | surf04.knotvector_v = utilities.generate_knot_vector(surf04.degree_v, surf04.ctrlpts_size_v) 89 | 90 | 91 | # Construct the parametric volume with a uniform knot vector 92 | pvolume = construct.construct_volume('w', surf01, surf02, surf03, surf04, degree=2) 93 | 94 | # Visualize volume 95 | pvolume.vis = vis.VisVolume(vis.VisConfig(ctrlpts=True, evalpts=False)) 96 | pvolume.render() 97 | 98 | # Knot vector refinement 99 | operations.refine_knotvector(pvolume, [1, 1, 1]) 100 | 101 | # Visualize volume after knot insertions 102 | pvolume.render() 103 | 104 | # Good to have something here to put a breakpoint 105 | pass 106 | -------------------------------------------------------------------------------- /visualization/mpl_curve3d_tangents.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | Visualization Examples for the NURBS-Python Package 5 | Released under The MIT License 6 | Developed by Onur Rauf Bingol (c) 2018 7 | 8 | Creates a 3-dimensional curve and plots tangent vectors 9 | """ 10 | import os 11 | from geomdl import BSpline 12 | from geomdl import utilities 13 | from geomdl import exchange 14 | from geomdl import operations 15 | 16 | import numpy as np 17 | import matplotlib 18 | from mpl_toolkits.mplot3d import Axes3D 19 | import matplotlib.pyplot as plt 20 | 21 | 22 | # Fix file path 23 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 24 | 25 | # 26 | # Curve Evaluation 27 | # 28 | 29 | # Create a BSpline curve instance 30 | curve = BSpline.Curve() 31 | 32 | # Set degree 33 | curve.degree = 3 34 | 35 | # Set control points 36 | curve.ctrlpts = exchange.import_txt("../curve3d/ex_curve3d01.cpt") 37 | 38 | # Auto-generate knot vector 39 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 40 | 41 | # Set evaluation delta 42 | curve.delta = 0.001 43 | 44 | # Evaulate curve 45 | curve.evaluate() 46 | 47 | # 48 | # Tangent Vector Evaluation 49 | # 50 | 51 | # Store tangent vectors in a list for plotting 52 | curvetan = [] 53 | 54 | # Evaluate curve tangent at u = 0.0175 55 | ct1 = operations.tangent(curve, 0.0175, normalize=True) 56 | curvetan.append(ct1) 57 | 58 | # Evaluate curve tangent at u = 0.075 59 | ct2 = operations.tangent(curve, 0.075, normalize=True) 60 | curvetan.append(ct2) 61 | 62 | # Evaluate curve tangent at u = 0.375 63 | ct3 = operations.tangent(curve, 0.375, normalize=True) 64 | curvetan.append(ct3) 65 | 66 | # Evaluate curve tangent at u = 0.535 67 | ct4 = operations.tangent(curve, 0.535, normalize=True) 68 | curvetan.append(ct4) 69 | 70 | # Evaluate curve tangent at u = 0.65 71 | ct5 = operations.tangent(curve, 0.65, normalize=True) 72 | curvetan.append(ct5) 73 | 74 | # Evaluate curve tangent at u = 0.85 75 | ct6 = operations.tangent(curve, 0.85, normalize=True) 76 | curvetan.append(ct6) 77 | 78 | # Evaluate curve tangent at u = 0.975 79 | ct7 = operations.tangent(curve, 0.975, normalize=True) 80 | curvetan.append(ct7) 81 | 82 | # 83 | # Control Points, Curve and Tangent Vector Plotting using Matplotlib 84 | # 85 | 86 | # Arrange control points and evaluated curve points for plotting 87 | ctrlpts = np.array(curve.ctrlpts) 88 | curvepts = np.array(curve.evalpts) 89 | 90 | # Convert tangent list into a NumPy array 91 | ctarr = np.array(curvetan) 92 | 93 | # Draw the control points polygon, the 3D curve and the tangent vectors 94 | fig = plt.figure(figsize=(10.67, 8), dpi=96) 95 | ax = Axes3D(fig) 96 | 97 | # Plot 3D lines 98 | ax.plot(ctrlpts[:, 0], ctrlpts[:, 1], ctrlpts[:, 2], color='black', linestyle='-.', marker='o') 99 | ax.plot(curvepts[:, 0], curvepts[:, 1], curvepts[:, 2], color='green', linestyle='-') 100 | 101 | # Plot tangent vectors 102 | ax.quiver(ctarr[:, 0, 0], ctarr[:, 0, 1], ctarr[:, 0, 2], ctarr[:, 1, 0], ctarr[:, 1, 1], ctarr[:, 1, 2], color='blue') 103 | 104 | # Add legend to 3D plot, @ref: https://stackoverflow.com/a/20505720 105 | ctrlpts_proxy = matplotlib.lines.Line2D([0], [0], linestyle='-.', color='black', marker='o') 106 | curvepts_proxy = matplotlib.lines.Line2D([0], [0], linestyle='none', color='green', marker='o') 107 | tangent_proxy = matplotlib.lines.Line2D([0], [0], linestyle='none', color='blue', marker='>') 108 | ax.legend([ctrlpts_proxy, curvepts_proxy, tangent_proxy], ['Control Points', 'Curve', 'Tangents'], numpoints=1) 109 | 110 | # Display the 3D plot 111 | plt.show() 112 | -------------------------------------------------------------------------------- /visualization/mpl_trisurf_vectors.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | Visualization Examples for the NURBS-Python Package 5 | Released under The MIT License 6 | Developed by Onur Rauf Bingol (c) 2018 7 | 8 | Creates a triangulated surface and plots tangent and normal vectors 9 | 10 | Colors: https://xkcd.com/color/rgb/ 11 | """ 12 | 13 | import os 14 | from geomdl import BSpline 15 | from geomdl import utilities 16 | from geomdl import exchange 17 | from geomdl import operations 18 | 19 | import numpy as np 20 | import matplotlib 21 | from matplotlib import cm 22 | from mpl_toolkits.mplot3d import Axes3D 23 | import matplotlib.pyplot as plt 24 | 25 | 26 | # Fix file path 27 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 28 | 29 | # Create a BSpline surface instance 30 | surf = BSpline.Surface() 31 | 32 | # Set degrees 33 | surf.degree_u = 3 34 | surf.degree_v = 3 35 | 36 | # Set control points 37 | surf.set_ctrlpts(*exchange.import_txt("../surface/ex_surface02.cpt", two_dimensional=True)) 38 | 39 | surf.knotvector_u = utilities.generate_knot_vector(surf.degree_u, 6) 40 | surf.knotvector_v = utilities.generate_knot_vector(surf.degree_v, 6) 41 | 42 | # Set evaluation delta 43 | surf.delta = 0.05 44 | 45 | # Evaluate surface 46 | surf.evaluate() 47 | 48 | # Evaluate surface tangent and normal vectors 49 | uv_vals = [[0.1, 0.1], [0.65, 0.25], [0.9, 0.7], [0.2, 0.9]] 50 | surftans = [[] for _ in range(len(uv_vals))] 51 | surfnorms = [[] for _ in range(len(uv_vals))] 52 | 53 | for idx, uv in enumerate(uv_vals): 54 | surftans[idx] = operations.tangent(surf, uv, normalize=True) 55 | surfnorms[idx] = operations.normal(surf, uv, normalize=True) 56 | 57 | # Prepare points for plotting 58 | surfpts = np.array(surf.evalpts) 59 | tangent_vectors = np.array(surftans) 60 | normal_vectors = np.array(surfnorms) 61 | 62 | # Start plotting of the surface and the control points grid 63 | fig = plt.figure(figsize=(10.67, 8), dpi=96) 64 | ax = Axes3D(fig) 65 | 66 | # Plot surface points 67 | ax.plot_trisurf(surfpts[:, 0], surfpts[:, 1], surfpts[:, 2], color='xkcd:gold', alpha=0.5) 68 | 69 | # Plot tangent vectors (u-dir) 70 | ax.quiver(tangent_vectors[:, 0, 0], tangent_vectors[:, 0, 1], tangent_vectors[:, 0, 2], 71 | tangent_vectors[:, 1, 0], tangent_vectors[:, 1, 1], tangent_vectors[:, 1, 2], 72 | color='xkcd:bright blue', length=5) 73 | 74 | # Plot tangent vectors (v-dir) 75 | ax.quiver(tangent_vectors[:, 0, 0], tangent_vectors[:, 0, 1], tangent_vectors[:, 0, 2], 76 | tangent_vectors[:, 2, 0], tangent_vectors[:, 2, 1], tangent_vectors[:, 2, 2], 77 | color='xkcd:neon green', length=5) 78 | 79 | # Plot normal vectors 80 | ax.quiver(normal_vectors[:, 0, 0], normal_vectors[:, 0, 1], normal_vectors[:, 0, 2], 81 | normal_vectors[:, 1, 0], normal_vectors[:, 1, 1], normal_vectors[:, 1, 2], 82 | color='xkcd:bright red', length=3) 83 | 84 | # Add legend to 3D plot, @ref: https://stackoverflow.com/a/20505720 85 | surface_prx = matplotlib.lines.Line2D([0], [0], linestyle='none', color='xkcd:gold', marker='^') 86 | tanu_prx = matplotlib.lines.Line2D([0], [0], linestyle='none', color='xkcd:bright blue', marker='>') 87 | tanv_prx = matplotlib.lines.Line2D([0], [0], linestyle='none', color='xkcd:neon green', marker='>') 88 | normal_prx = matplotlib.lines.Line2D([0], [0], linestyle='none', color='xkcd:bright red', marker='>') 89 | ax.legend([surface_prx, tanu_prx, tanv_prx, normal_prx], 90 | ['Surface Plot', 'Tangent Vectors (u-dir)', 'Tangent Vectors (v-dir)', 'Normal Vectors'], 91 | numpoints=1) 92 | 93 | # Rotate the axes and update the plot 94 | for angle in range(0, 360, 10): 95 | ax.view_init(30, angle) 96 | plt.draw() 97 | plt.pause(.001) 98 | 99 | # Display the final 3D plot 100 | plt.show() 101 | -------------------------------------------------------------------------------- /visualization/mpl_curve3d_vectors.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | Visualization Examples for the NURBS-Python Package 5 | Released under The MIT License 6 | Developed by Onur Rauf Bingol (c) 2018 7 | 8 | Creates a 3-dimensional curve and plots tangent, normal and binormal vectors 9 | """ 10 | 11 | import os 12 | from geomdl import BSpline 13 | from geomdl import utilities 14 | from geomdl import exchange 15 | from geomdl import operations 16 | 17 | import numpy as np 18 | import matplotlib 19 | from mpl_toolkits.mplot3d import Axes3D 20 | import matplotlib.pyplot as plt 21 | 22 | 23 | # Fix file path 24 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 25 | 26 | # 27 | # Curve Evaluation 28 | # 29 | 30 | # Create a BSpline curve instance 31 | curve = BSpline.Curve() 32 | 33 | # Set degree 34 | curve.degree = 3 35 | 36 | # Set control points 37 | curve.ctrlpts = exchange.import_txt("../curve3d/ex_curve3d02.cpt") 38 | 39 | # Auto-generate knot vector 40 | curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) 41 | 42 | # Set evaluation delta 43 | curve.delta = 0.001 44 | 45 | # Evaulate curve 46 | curve.evaluate() 47 | 48 | # 49 | # Multiple vector evaluation after v3.0.7 50 | # 51 | 52 | # List of parametric coordinates to be evaluated 53 | u_list = (0.0175, 0.075, 0.375, 0.535, 0.65, 0.85, 0.975) 54 | 55 | # Evaluate tangents, normals and binormals, respectively 56 | curvetans = [[] for _ in range(len(u_list))] 57 | curvenorms = [[] for _ in range(len(u_list))] 58 | curvebinorms = [[] for _ in range(len(u_list))] 59 | for idx, u in enumerate(u_list): 60 | curvetans[idx] = operations.tangent(curve, u, normalize=True) 61 | curvenorms[idx] = operations.normal(curve, u, normalize=True) 62 | curvebinorms[idx] = operations.binormal(curve, u, normalize=True) 63 | 64 | # 65 | # Control Points, Curve and Tangent Vector Plotting using Matplotlib 66 | # 67 | 68 | # Arrange control points and evaluated curve points for plotting 69 | ctrlpts = np.array(curve.ctrlpts) 70 | curvepts = np.array(curve.evalpts) 71 | 72 | # Convert tangent, normal and binormal vector lists into NumPy arrays 73 | ctarr = np.array(curvetans) 74 | cnarr = np.array(curvenorms) 75 | cbnarr = np.array(curvebinorms) 76 | 77 | # Draw the control points polygon, the 3D curve and the vectors 78 | fig = plt.figure(figsize=(10.67, 8), dpi=96) 79 | ax = Axes3D(fig) 80 | 81 | # Plot 3D lines 82 | ax.plot(ctrlpts[:, 0], ctrlpts[:, 1], ctrlpts[:, 2], color='black', linestyle='-.', marker='o', linewidth=1) 83 | ax.plot(curvepts[:, 0], curvepts[:, 1], curvepts[:, 2], color='brown', linestyle='-', linewidth=2) 84 | 85 | # Plot tangent vectors 86 | ax.quiver(ctarr[:, 0, 0], ctarr[:, 0, 1], ctarr[:, 0, 2], ctarr[:, 1, 0], ctarr[:, 1, 1], ctarr[:, 1, 2], 87 | color='blue', length=2.5) 88 | 89 | # Plot normal vectors 90 | ax.quiver(cnarr[:, 0, 0], cnarr[:, 0, 1], cnarr[:, 0, 2], cnarr[:, 1, 0], cnarr[:, 1, 1], cnarr[:, 1, 2], 91 | color='red', length=2.5) 92 | 93 | # Plot binormal vectors 94 | ax.quiver(cbnarr[:, 0, 0], cbnarr[:, 0, 1], cbnarr[:, 0, 2], cbnarr[:, 1, 0], cbnarr[:, 1, 1], cbnarr[:, 1, 2], 95 | color='green', length=2.5) 96 | 97 | # Add legend to 3D plot, @ref: https://stackoverflow.com/a/20505720 98 | ctrlpts_proxy = matplotlib.lines.Line2D([0], [0], linestyle='-.', color='black', marker='o') 99 | curvepts_proxy = matplotlib.lines.Line2D([0], [0], linestyle='none', color='brown', marker='o') 100 | tangent_proxy = matplotlib.lines.Line2D([0], [0], linestyle='none', color='blue', marker='>') 101 | normal_proxy = matplotlib.lines.Line2D([0], [0], linestyle='none', color='red', marker='>') 102 | binormal_proxy = matplotlib.lines.Line2D([0], [0], linestyle='none', color='green', marker='>') 103 | ax.legend([ctrlpts_proxy, curvepts_proxy, tangent_proxy, normal_proxy, binormal_proxy], 104 | ['Control Points', 'Curve', 'Tangents', 'Normals', 'Binormals'], numpoints=1) 105 | 106 | # Display the 3D plot 107 | plt.show() 108 | -------------------------------------------------------------------------------- /surface/refinement/ex_density.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Examples for the NURBS-Python Package 6 | Released under MIT License 7 | Developed by Onur Rauf Bingol (c) 2019 8 | """ 9 | 10 | import os 11 | from copy import deepcopy 12 | from geomdl import NURBS 13 | from geomdl import operations 14 | from geomdl.visualization import VisMPL 15 | 16 | 17 | # Fix file path 18 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 19 | 20 | # Control points 21 | ctrlpts = [[[25.0, -25.0, 0.0, 1.0], [15.0, -25.0, 0.0, 1.0], [5.0, -25.0, 0.0, 1.0], 22 | [-5.0, -25.0, 0.0, 1.0], [-15.0, -25.0, 0.0, 1.0], [-25.0, -25.0, 0.0, 1.0]], 23 | [[25.0, -15.0, 0.0, 1.0], [15.0, -15.0, 0.0, 1.0], [5.0, -15.0, 0.0, 1.0], 24 | [-5.0, -15.0, 0.0, 1.0], [-15.0, -15.0, 0.0, 1.0], [-25.0, -15.0, 0.0, 1.0]], 25 | [[25.0, -5.0, 5.0, 1.0], [15.0, -5.0, 5.0, 1.0], [5.0, -5.0, 5.0, 1.0], 26 | [-5.0, -5.0, 5.0, 1.0], [-15.0, -5.0, 5.0, 1.0], [-25.0, -5.0, 5.0, 1.0]], 27 | [[25.0, 5.0, 5.0, 1.0], [15.0, 5.0, 5.0, 1.0], [5.0, 5.0, 5.0, 1.0], 28 | [-5.0, 5.0, 5.0, 1.0], [-15.0, 5.0, 5.0, 1.0], [-25.0, 5.0, 5.0, 1.0]], 29 | [[25.0, 15.0, 0.0, 1.0], [15.0, 15.0, 0.0, 1.0], [5.0, 15.0, 5.0, 1.0], 30 | [-5.0, 15.0, 5.0, 1.0], [-15.0, 15.0, 0.0, 1.0], [-25.0, 15.0, 0.0, 1.0]], 31 | [[25.0, 25.0, 0.0, 1.0], [15.0, 25.0, 0.0, 1.0], [5.0, 25.0, 5.0, 1.0], 32 | [-5.0, 25.0, 5.0, 1.0], [-15.0, 25.0, 0.0, 1.0], [-25.0, 25.0, 0.0, 1.0]]] 33 | 34 | # Generate surface 35 | surf = NURBS.Surface() 36 | surf.degree_u = 3 37 | surf.degree_v = 3 38 | surf.ctrlpts2d = ctrlpts 39 | surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 40 | surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] 41 | surf.sample_size = 30 42 | 43 | # Set visualization component 44 | surf.vis = VisMPL.VisSurface(VisMPL.VisConfig(alpha=0.75, figure_size=[10.66, 8])) 45 | 46 | # Visualize 47 | surf.render(filename="surf01.png", plot=False) 48 | 49 | # Create a deep copy of the surface 50 | surf_refined = deepcopy(surf) 51 | 52 | # Refine knot vectors 53 | operations.refine_knotvector(surf_refined, [1, 1]) 54 | 55 | # Visualize after knot vector refinement 56 | surf_refined.render(filename="surf02.png", plot=False) 57 | 58 | # Create a deep copy of the surface 59 | surf_refined = deepcopy(surf) 60 | 61 | # Refine knot vectors 62 | operations.refine_knotvector(surf_refined, [2, 1]) 63 | 64 | # Visualize after knot vector refinement 65 | surf_refined.render(filename="surf03.png", plot=False) 66 | 67 | # Create a deep copy of the surface 68 | surf_refined = deepcopy(surf) 69 | 70 | # Refine knot vectors 71 | operations.refine_knotvector(surf_refined, [1, 2]) 72 | 73 | # Visualize after knot vector refinement 74 | surf_refined.render(filename="surf04.png", plot=False) 75 | 76 | # Create a deep copy of the surface 77 | surf_refined = deepcopy(surf) 78 | 79 | # Refine knot vectors 80 | operations.refine_knotvector(surf_refined, [2, 2]) 81 | 82 | # Visualize after knot vector refinement 83 | surf_refined.render(filename="surf05.png", plot=False) 84 | 85 | # Create a deep copy of the surface 86 | surf_refined = deepcopy(surf) 87 | 88 | # Refine knot vectors 89 | operations.refine_knotvector(surf_refined, [3, 2]) 90 | 91 | # Visualize after knot vector refinement 92 | surf_refined.render(filename="surf06.png", plot=False) 93 | 94 | # Create a deep copy of the surface 95 | surf_refined = deepcopy(surf) 96 | 97 | # Refine knot vectors 98 | operations.refine_knotvector(surf_refined, [2, 3]) 99 | 100 | # Visualize after knot vector refinement 101 | surf_refined.render(filename="surf07.png", plot=False) 102 | 103 | # Create a deep copy of the surface 104 | surf_refined = deepcopy(surf) 105 | 106 | # Refine knot vectors 107 | operations.refine_knotvector(surf_refined, [3, 3]) 108 | 109 | # Visualize after knot vector refinement 110 | surf_refined.render(filename="surf08.png", plot=False) 111 | -------------------------------------------------------------------------------- /objects/ducky.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Plotting the Duck Example from GLScene with NURBS-Python 6 | 7 | GLScene: http://wiki.freepascal.org/GLScene 8 | GLScene on GitHub: https://github.com/cutec-chris/GLScene 9 | 10 | The "Ducky" example contains 3 surfaces. The surface data is accessible via the "GLScene on GitHub" link above in 11 | the directory "Demos/media/" with the file names "duck1.nurbs", "duck2.nurbs" and "duck3.nurbs". The contents of 12 | these files are extracted into NURBS-Python format. 13 | 14 | The main difference between the surface formats is the row order. Most of the time it affects the evaluation result. 15 | It is easy to understand the issues caused by the row order: Using a 1-dimensional array of control points in the 16 | wrong order may cause a "hole" or leave some regions unevaluated. For the 2-dimensional array of control points, 17 | most of the time you will get an IndexError exception. 18 | 19 | NURBS-Python uses v-row order but GLScene uses u-row order (please see docs for more details on the row order). 20 | This means that u- and v-directions of the *.nurbs files must be transposed (or flipped) in order to get the 21 | correct shapes. This example fixes the issues caused by the row order difference. 22 | """ 23 | 24 | import os 25 | from geomdl import NURBS 26 | from geomdl import multi 27 | from geomdl import exchange 28 | from geomdl import compatibility 29 | from geomdl.visualization import VisVTK 30 | 31 | 32 | def read_weights(filename, sep=","): 33 | try: 34 | with open(filename, "r") as fp: 35 | content = fp.read() 36 | content_arr = [float(w) for w in (''.join(content.split())).split(sep)] 37 | return content_arr 38 | except IOError as e: 39 | print("An error occurred: {}".format(e.args[-1])) 40 | raise e 41 | 42 | 43 | # Fix file path 44 | os.chdir(os.path.dirname(os.path.realpath(__file__))) 45 | 46 | # duck1.nurbs 47 | 48 | # Process control points and weights 49 | d2_ctrlpts = exchange.import_txt("duck1.ctrlpts", separator=" ") 50 | d1_weights = read_weights("duck1.weights") 51 | d1_ctrlptsw = compatibility.combine_ctrlpts_weights(d2_ctrlpts, d1_weights) 52 | 53 | # Create a NURBS surface 54 | duck1 = NURBS.Surface() 55 | duck1.name = "body" 56 | duck1.order_u = 4 57 | duck1.order_v = 4 58 | duck1.ctrlpts_size_u = 14 59 | duck1.ctrlpts_size_v = 13 60 | duck1.ctrlptsw = d1_ctrlptsw 61 | duck1.knotvector_u = [-1.5708, -1.5708, -1.5708, -1.5708, -1.0472, -0.523599, 0, 0.523599, 0.808217, 62 | 1.04015, 1.0472, 1.24824, 1.29714, 1.46148, 1.5708, 1.5708, 1.5708, 1.5708] 63 | duck1.knotvector_v = [-3.14159, -3.14159, -3.14159, -3.14159, -2.61799, -2.0944, -1.0472, -0.523599, 64 | 6.66134e-016, 0.523599, 1.0472, 2.0944, 2.61799, 3.14159, 3.14159, 3.14159, 3.14159] 65 | 66 | 67 | # duck2.nurbs 68 | 69 | # Process control points and weights 70 | d2_ctrlpts = exchange.import_txt("duck2.ctrlpts", separator=" ") 71 | 72 | # Create a NURBS surface 73 | duck2 = NURBS.Surface() 74 | duck2.name = "beak top" 75 | duck2.order_u = 4 76 | duck2.order_v = 4 77 | duck2.ctrlpts_size_u = 9 78 | duck2.ctrlpts_size_v = 10 79 | duck2.ctrlpts = d2_ctrlpts 80 | duck2.knotvector_u = [0, 0, 0, 0, 0.145456, 0.265731, 0.436096, 0.583258, 0.847704, 1, 1, 1, 1] 81 | duck2.knotvector_v = [0, 0, 0, 0, 0.179541, 0.317924, 0.485586, 0.507528, 0.709398, 0.813231, 1, 1, 1, 1] 82 | 83 | 84 | # duck3.nurbs 85 | 86 | # Process control points and weights 87 | d3_ctrlpts = exchange.import_txt("duck3.ctrlpts", separator=" ") 88 | 89 | # Create a NURBS surface 90 | duck3 = NURBS.Surface() 91 | duck3.name = "beak bottom" 92 | duck3.order_u = 4 93 | duck3.order_v = 4 94 | duck3.ctrlpts_size_u = 6 95 | duck3.ctrlpts_size_v = 6 96 | duck3.ctrlpts = d3_ctrlpts 97 | duck3.knotvector_u = [0, 0, 0, 0, 0.333333, 0.666667, 1, 1, 1, 1] 98 | duck3.knotvector_v = [0, 0, 0, 0, 0.333333, 0.666667, 1, 1, 1, 1] 99 | 100 | # Add all surfaces to a surface container 101 | ducky = multi.SurfaceContainer(duck1, duck2, duck3) 102 | ducky.sample_size = 50 103 | 104 | # Visualization configuration 105 | ducky.vis = VisVTK.VisSurface(ctrlpts=True, legend=False, figure_size=[940, 940]) 106 | 107 | # Render the ducky 108 | ducky.render(evalcolor=["green", "yellow", "yellow"], cpcolor="wheat") 109 | -------------------------------------------------------------------------------- /objects/duck1.ctrlpts: -------------------------------------------------------------------------------- 1 | 1.49662 0.0131926 -0.535702 2 | 1.49662 0.0131926 -0.535702 3 | 1.49662 0.0131926 -0.535702 4 | 1.49662 0.0131926 -0.535702 5 | 1.49662 0.0131926 -0.535702 6 | 1.49662 0.0131926 -0.535702 7 | 1.49662 0.0131926 -0.535702 8 | 1.49662 0.0131926 -0.535702 9 | 1.49662 0.0131926 -0.535702 10 | 1.49662 0.0131926 -0.535702 11 | 1.49662 0.0131926 -0.535702 12 | 1.49662 0.0131926 -0.535702 13 | 1.49662 0.0131926 -0.535702 14 | 1.92379 0.0131926 -0.535702 15 | 1.92379 0.0465361 -0.535702 16 | 1.90747 0.121179 -0.535702 17 | 1.57358 0.236742 -0.535702 18 | 1.41966 0.237449 -0.535702 19 | 1.30075 0.120118 -0.535702 20 | 1.27627 0.0131926 -0.535702 21 | 1.30075 -0.093733 -0.535702 22 | 1.41966 -0.211064 -0.535702 23 | 1.57358 -0.210357 -0.535702 24 | 1.90747 -0.0947938 -0.535702 25 | 1.92379 -0.0201509 -0.535702 26 | 1.92379 0.0131926 -0.535702 27 | 2.73528 0.0131926 -0.516333 28 | 2.73528 0.118624 -0.516333 29 | 2.6288 0.354652 -0.516333 30 | 1.80454 0.720067 -0.516333 31 | 1.00825 1.13359 -0.516333 32 | 0.455347 0.351297 -0.516333 33 | 0.295617 0.0131926 -0.516333 34 | 0.455347 -0.324912 -0.516333 35 | 1.00825 -1.1072 -0.516333 36 | 1.80454 -0.693681 -0.516333 37 | 2.6288 -0.328267 -0.516333 38 | 2.73528 -0.092239 -0.516333 39 | 2.73528 0.0131926 -0.516333 40 | 3.43266 0.0131926 0.450235 41 | 3.43266 0.194338 0.450235 42 | 3.06333 1.15022 0.450235 43 | 2.5118 0.943504 -0.0916114 44 | 1.21331 1.45996 -0.0657666 45 | -0.0331817 0.756754 -0.0657666 46 | -0.225645 0.0131926 -0.0657666 47 | -0.0331817 -0.730369 -0.0657666 48 | 1.21331 -1.43358 -0.0657666 49 | 2.5118 -0.917119 -0.0916115 50 | 3.06333 -1.12383 0.450235 51 | 3.43266 -0.167953 0.450235 52 | 3.43266 0.0131926 0.450235 53 | 3.31158 0.0131926 2.00763 54 | 3.31158 0.222706 2.00763 55 | 3.18953 0.265519 2.00763 56 | 2.66054 1.27398 0.409614 57 | 1.21288 1.56324 0.566692 58 | -0.0613496 0.873195 0.530222 59 | -0.244426 0.0131926 0.530222 60 | -0.0613496 -0.84681 0.530222 61 | 1.21288 -1.53686 0.566692 62 | 2.66054 -1.24759 0.409614 63 | 3.18953 -0.239134 2.00763 64 | 3.31158 -0.19632 2.00763 65 | 3.31158 0.0131926 2.00763 66 | 2.82922 0.0131926 1.43919 67 | 2.82922 0.0634254 1.43919 68 | 2.88211 0.124312 1.39263 69 | 1.12024 0.632184 0.332866 70 | 1.19494 1.26056 0.851303 71 | 0.0960279 0.60794 0.857131 72 | -0.0446044 0.0131926 0.857131 73 | 0.0960279 -0.581555 0.857131 74 | 1.19494 -1.23418 0.851303 75 | 1.12024 -0.618992 0.332866 76 | 2.88211 -0.0979265 1.39263 77 | 2.82922 -0.0370402 1.43919 78 | 2.82922 0.0131926 1.43919 79 | 2.53537 0.0131927 1.10909 80 | 2.53537 0.194424 1.10909 81 | 2.42558 0.620334 1.04763 82 | 2.01157 0.801663 0.908186 83 | 1.15419 0.971482 0.98254 84 | 0.350506 0.470106 1.03237 85 | 0.245852 0.0131927 1.03238 86 | 0.350506 -0.44372 1.03238 87 | 1.15419 -0.945097 0.98254 88 | 2.01157 -0.775277 0.908186 89 | 2.42558 -0.593949 1.04763 90 | 2.53537 -0.168039 1.10909 91 | 2.53537 0.0131927 1.10909 92 | 1.78151 0.0131927 0.94063 93 | 1.78151 0.0883495 0.94063 94 | 1.77363 0.260454 0.948305 95 | 1.5189 0.69632 1.10191 96 | 1.12675 0.782575 1.08713 97 | 0.541466 0.380035 1.13032 98 | 0.457467 0.0131927 1.13032 99 | 0.541466 -0.353649 1.13032 100 | 1.12675 -0.75619 1.08713 101 | 1.5189 -0.669934 1.10191 102 | 1.77363 -0.234068 0.948305 103 | 1.78151 -0.0619642 0.94063 104 | 1.78151 0.0131927 0.94063 105 | 1.59703 0.0131926 1.26531 106 | 1.59703 0.0853004 1.26531 107 | 1.57395 0.248356 1.26856 108 | 1.37314 0.572456 1.20861 109 | 1.04665 0.609796 1.17838 110 | 0.619437 0.297653 1.27202 111 | 0.554301 0.0131927 1.27202 112 | 0.619437 -0.271268 1.27202 113 | 1.04665 -0.58341 1.17838 114 | 1.37314 -0.54607 1.20861 115 | 1.57395 -0.22197 1.26856 116 | 1.59703 -0.0589152 1.26531 117 | 1.59703 0.0131926 1.26531 118 | 1.51978 0.0131926 1.37211 119 | 1.51978 0.111102 1.37211 120 | 1.47815 0.331128 1.37378 121 | 1.37119 0.708727 1.34617 122 | 0.953251 0.729271 1.36483 123 | 0.53756 0.354619 1.37425 124 | 0.45938 0.0131926 1.37425 125 | 0.53756 -0.328234 1.37425 126 | 0.953251 -0.702886 1.36483 127 | 1.37119 -0.682342 1.34617 128 | 1.47815 -0.304743 1.37378 129 | 1.51978 -0.0847171 1.37211 130 | 1.51978 0.0131926 1.37211 131 | 1.90802 0.0131926 1.79656 132 | 1.90802 0.127865 1.79656 133 | 1.8532 0.384752 1.79691 134 | 1.3569 0.790178 1.82875 135 | 0.867368 0.796466 1.83264 136 | 0.249556 0.401946 1.83461 137 | 0.16404 0.0131926 1.83461 138 | 0.249556 -0.375561 1.83461 139 | 0.867368 -0.770081 1.83264 140 | 1.3569 -0.763793 1.82875 141 | 1.8532 -0.358367 1.79691 142 | 1.90802 -0.10148 1.79656 143 | 1.90802 0.0131926 1.79656 144 | 1.70755 0.0131926 2.42201 145 | 1.70755 0.111781 2.42201 146 | 1.65928 0.332482 2.42201 147 | 1.29612 0.674173 2.42201 148 | 0.868507 0.676264 2.42201 149 | 0.55633 0.430641 2.42201 150 | 0.293367 0.0131926 2.42201 151 | 0.55633 -0.404256 2.42201 152 | 0.868507 -0.649879 2.42201 153 | 1.29612 -0.647788 2.42201 154 | 1.65928 -0.306097 2.42201 155 | 1.70755 -0.0853954 2.42201 156 | 1.70755 0.0131926 2.42201 157 | 1.0955 0.0131926 2.51111 158 | 1.0955 0.0201538 2.51111 159 | 1.09209 0.0357371 2.51111 160 | 1.06727 0.0598634 2.51111 161 | 1.03513 0.060011 2.51111 162 | 1.01031 0.0355157 2.51111 163 | 1.00519 0.0131926 2.51111 164 | 1.01031 -0.00913046 2.51111 165 | 1.03513 -0.0336258 2.51111 166 | 1.06727 -0.0334782 2.51111 167 | 1.09209 -0.00935192 2.51111 168 | 1.0955 0.00623142 2.51111 169 | 1.0955 0.0131926 2.51111 170 | 1.0512 0.0131926 2.5017 171 | 1.0512 0.0131926 2.5017 172 | 1.0512 0.0131926 2.5017 173 | 1.0512 0.0131926 2.5017 174 | 1.0512 0.0131926 2.5017 175 | 1.0512 0.0131926 2.5017 176 | 1.0512 0.0131926 2.5017 177 | 1.0512 0.0131926 2.5017 178 | 1.0512 0.0131926 2.5017 179 | 1.0512 0.0131926 2.5017 180 | 1.0512 0.0131926 2.5017 181 | 1.0512 0.0131926 2.5017 182 | 1.0512 0.0131926 2.5017 183 | --------------------------------------------------------------------------------