Skip to content

Commit

Permalink
Merge pull request #5158 from nortikin/curve_to_nurbs
Browse files Browse the repository at this point in the history
"Curve to NURBS" node
  • Loading branch information
portnov authored Oct 26, 2024
2 parents acb5a10 + 81da894 commit 2b817c6
Show file tree
Hide file tree
Showing 20 changed files with 842 additions and 5 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/nodes/curve/curve_to_nurbs_nodes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
142 changes: 142 additions & 0 deletions docs/nodes/curve/curve_to_nurbs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
Curve to NURBS
==============

Functionality
-------------

This node allows to:

* Convert arbitrary (non-NURBS) curve to NURBS curve;
* Reparametrize any curve: given one curve, make a NURBS curve with "natural"
(uniform) parametrization, or with arbitrary parametrization.

More specifically, what this node does is:

* Evaluates initial curve at some number of points; these node can be defined
by uniform distribution of curve T parameter, or by dividing curve into
segments of equal length. If initial curve is already NURBS or NURBS-like,
this node will always use curve's "fracture" (crisp) points as such base
points, in order to preserve fracture points at same places.
* Interpolates a NURBS curve through these points, using one of criteria to
define T parameters of new curve at these points - this will define
parametrization of the curve.
* In the end, resulting NURBS curve is simplified by removing as much
unnesessary knots as possible, while trying to change it not too much.

Inputs
------

This node has the following inputs:

* **Curve**. The curve to be converted to NURBS. This input is mandatory.
* **Degree**. Degree of resulting NURBS curve. The default value is 3.
* **Samples**. Number of base points on the curve, which will be used for
interpolation. More samples will give NURBS curve which follows initial curve
more precisely, but have more control points (and will take more time to
calculate). The default value is 50.
* **LengthResolution**. This input is available only when **Parametrization**
parameter is set to **Natural (Curve Length)**. Resolution which is used to
calculate curve length. Bigger value will give more precise calculation, but
will take more time to calculate. The default value is 50.
* **Parametrization**. This input is available and mandatory only when
**Parametrization** parameter is seto to **Arbitrary**. The curve in this
input defines wanted parametrization of NURBS curve. The node expects a curve
which lies in XOY plane and has [0; 1] as it's domain, and it's expected that
Y coordinate of point on the curve is growing with growth of T parameter,
while remaining in [0; 1] bounds.
* **Tolerance**. This input is available only when **Simplify** parameter is
checked. This defines tolerance value used when removing excessive knots of
interpolated NURBS curve. Lower values will give curves which follow initial
curve more precisely, but have more control points. The default value is 1e-6
(one per million).

Parameters
----------

This node has the following parameters:

* **Parametrization**. This defines parametrization of resulting NURBS curve.
The available options are:

* **Natural (Curve Length)**. Curve T parameter of some point on the curve
will be equal (approximately) to length of the curve segment from start to
that point.
* **Keep Original**. Parametrization of resulting curve will be the same as
parametrization of input curve.
* **Arbitrary**. This allows to specify arbitrary curve parametrization by
providing a curve in **Parametrization** input.

The default option is **Natural (Curve Length)**.
* **Simplify**. If checked, the node will try to simplify the curve after
interpolation. With big enough values of **Samples** input, interpolated
curve will have a lot of control points, some of which are not really
necessary if you do not need too much precision. Checked by default.

Outputs
-------

This node has the following output:

* **Curve**. The resulting NURBS curve.

Examples of Usage
-----------------

General nodes setup:

.. image:: ../../../docs/assets/nodes/curve/curve_to_nurbs_nodes.png
:target: ../../../docs/assets/nodes/curve/curve_to_nurbs_nodes.png

If we specify Parametrization = "Keep Original", then for NURBS curves node
will basically return the same curve (slightly modified due to algorithms
involved):

.. image:: ../../../docs/assets/nodes/curve/curve_to_nurbs_original.png
:target: ../../../docs/assets/nodes/curve/curve_to_nurbs_original.png

So if you already have a NURBS curve, it does not make sense to use original
parametrization. It may, however, make sense for generic curves, see below.

In the following case we already have Bezier curve, but we want to have a NURBS
curve with uniform parametrization:

.. image:: ../../../docs/assets/nodes/curve/curve_to_nurbs_bezier_degree_3.png
:target: ../../../docs/assets/nodes/curve/curve_to_nurbs_bezier_degree_3.png

Note that while the curve is smooth, it's curvature comb is not smooth. If we
want it to be smooth, we can use NURBS curves of higher degree:

.. image:: ../../../docs/assets/nodes/curve/curve_to_nurbs_bezier_degree_4.png
:target: ../../../docs/assets/nodes/curve/curve_to_nurbs_bezier_degree_4.png

It is possible to create NURBS curves with arbitrary parametrization. Nodes setup:

.. image:: ../../../docs/assets/nodes/curve/curve_to_nurbs_arbitrary_nodes.png
:target: ../../../docs/assets/nodes/curve/curve_to_nurbs_arbitrary_nodes.png

"Formula interpolate" node defines the following reparametrization curve in this case:

.. image:: ../../../docs/assets/nodes/curve/curve_to_nurbs_arbitrary_parametrization.png
:target: ../../../docs/assets/nodes/curve/curve_to_nurbs_arbitrary_parametrization.png

This gives the following result:

.. image:: ../../../docs/assets/nodes/curve/curve_to_nurbs_arbitrary_result.png
:target: ../../../docs/assets/nodes/curve/curve_to_nurbs_arbitrary_result.png

Let's check with arbitrary curve; in this case it's a straight line segment
deformed by vector noise field:

.. image:: ../../../docs/assets/nodes/curve/curve_to_nurbs_noise_nodes.png
:target: ../../../docs/assets/nodes/curve/curve_to_nurbs_noise_nodes.png

With Parametrization = "Keep Original" this gives:

.. image:: ../../../docs/assets/nodes/curve/curve_to_nurbs_noise_keep.png
:target: ../../../docs/assets/nodes/curve/curve_to_nurbs_noise_keep.png

With Parametrization = "Natural" this gives:

.. image:: ../../../docs/assets/nodes/curve/curve_to_nurbs_noise_natural.png
:target: ../../../docs/assets/nodes/curve/curve_to_nurbs_noise_natural.png

1 change: 1 addition & 0 deletions index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
- SvExNurbsCurveNode
- SvApproxNurbsCurveMk3Node
- SvExInterpolateNurbsCurveNodeMK2
- SvCurveToNurbsNode
- SvCurveDiscontinuityNode
- SvFilletCurveNode
- SvDeconstructCurveNode
Expand Down
1 change: 1 addition & 0 deletions menus/full_by_data_type.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
- SvExNurbsCurveNode
- SvApproxNurbsCurveMk3Node
- SvExInterpolateNurbsCurveNodeMK2
- SvCurveToNurbsNode
- ---
- SvGenerateKnotvectorNode
- Modify Curve:
Expand Down
1 change: 1 addition & 0 deletions menus/full_nortikin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
- SvExNurbsCurveNode
- SvApproxNurbsCurveMk3Node
- SvExInterpolateNurbsCurveNodeMK2
- SvCurveToNurbsNode
- GC SCRIPTED:
- icon_name: PLUGIN
- SvExCurveFormulaNode
Expand Down
138 changes: 138 additions & 0 deletions nodes/curve/curve_to_nurbs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# This file is part of project Sverchok. It's copyrighted by the contributors
# recorded in the version control history of the file, available from
# its original location https://github.com/nortikin/sverchok/commit/master
#
# SPDX-License-Identifier: GPL3
# License-Filename: LICENSE

import numpy as np

import bpy
from bpy.props import FloatProperty, EnumProperty, BoolProperty, IntProperty
from mathutils import Vector

from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode, zip_long_repeat, repeat_last_for_length, ensure_nesting_level, get_data_nesting_level
from sverchok.utils.curve.core import SvCurve
from sverchok.utils.curve.nurbs_algorithms import remove_excessive_knots
from sverchok.utils.curve.nurbs_solver_applications import curve_to_nurbs, CURVE_PARAMETER, CURVE_LENGTH, CURVE_CURVATURE, CURVE_ARBITRARY

class SvCurveToNurbsNode(SverchCustomTreeNode, bpy.types.Node):
"""
Triggers: Curve Rebuild NURBS
Tooltip: Convert arbitrary curve to NURBS
"""
bl_idname = 'SvCurveToNurbsNode'
bl_label = 'Curve to NURBS'
bl_icon = 'OUTLINER_OB_EMPTY'
sv_icon = 'SV_CURVE_TO_NURBS'

degree : IntProperty(
name = "Degree",
min = 1,
default = 3,
update = updateNode)

samples : IntProperty(
name = "Samples",
min = 3,
default = 50,
update = updateNode)

resolution : IntProperty(
name = "Length Resolution",
min = 10,
default = 50,
update = updateNode)

parametrizations = [
(CURVE_LENGTH, "Natural (Curve Length)", "Natural curve parametrization (according to curve length)", 0),
#(CURVE_CURVATURE, "Curvature", "Parametrization according to curvature", 1),
(CURVE_PARAMETER, "Keep Original", "Keep original curve parametrization", 2),
(CURVE_ARBITRARY, "Arbitrary", "Specify arbitrary curve parametrization", 3)
]

def update_sockets(self, context):
self.inputs['LengthResolution'].hide_safe = self.parametrization not in {CURVE_LENGTH,CURVE_CURVATURE}
self.inputs['Parametrization'].hide_safe = self.parametrization != CURVE_ARBITRARY
self.inputs['Tolerance'].hide_safe = self.simplify != True
updateNode(self, context)

parametrization : EnumProperty(
name = "Parametrization",
items = parametrizations,
update = update_sockets)

simplify : BoolProperty(
name = "Simplify",
default = True,
update = update_sockets)

tolerance : FloatProperty(
name = "Tolerance",
default = 1e-6,
min = 0.0,
precision = 8,
update = updateNode)

def draw_buttons(self, context, layout):
layout.label(text='Parametrization:')
layout.prop(self, 'parametrization', text='')
layout.prop(self, 'simplify')

def sv_init(self, context):
self.inputs.new('SvCurveSocket', "Curve")
self.inputs.new('SvStringsSocket', "Degree").prop_name = 'degree'
self.inputs.new('SvStringsSocket', "Samples").prop_name = 'samples'
self.inputs.new('SvStringsSocket', "LengthResolution").prop_name = 'resolution'
self.inputs.new('SvCurveSocket', "Parametrization")
self.inputs.new('SvStringsSocket', "Tolerance").prop_name = 'tolerance'
self.outputs.new('SvCurveSocket', "Curve")
self.update_sockets(context)

def process(self):
if not any(socket.is_linked for socket in self.outputs):
return

curves_s = self.inputs['Curve'].sv_get()
input_level = get_data_nesting_level(curves_s, data_types=(SvCurve,))
curves_s = ensure_nesting_level(curves_s, 2, data_types=(SvCurve,))
nested_output = input_level > 1
degree_s = self.inputs['Degree'].sv_get()
degree_s = ensure_nesting_level(degree_s, 2)
samples_s = self.inputs['Samples'].sv_get()
samples_s = ensure_nesting_level(samples_s, 2)
resolution_s = self.inputs['LengthResolution'].sv_get()
resolution_s = ensure_nesting_level(resolution_s, 2)
tolerance_s = self.inputs['Tolerance'].sv_get()
tolerance_s = ensure_nesting_level(tolerance_s, 2)
if self.parametrization == CURVE_ARBITRARY:
parametrization_s = self.inputs['Parametrization'].sv_get()
parametrization_s = ensure_nesting_level(parametrization_s, 2, data_types=(SvCurve,))
else:
parametrization_s = [[None]]

curves_out = []
for params in zip_long_repeat(curves_s, degree_s, samples_s, resolution_s, tolerance_s, parametrization_s):
new_curves = []
for curve, degree, samples, resolution, tolerance, parametrization in zip_long_repeat(*params):
curve = curve_to_nurbs(degree, curve, samples,
method = self.parametrization,
parametrization = parametrization,
resolution = resolution,
logger = self.sv_logger)
if self.simplify:
curve = remove_excessive_knots(curve, tolerance = tolerance)
new_curves.append(curve)
if nested_output:
curves_out.append(new_curves)
else:
curves_out.extend(new_curves)
self.outputs['Curve'].sv_set(curves_out)

def register():
bpy.utils.register_class(SvCurveToNurbsNode)

def unregister():
bpy.utils.unregister_class(SvCurveToNurbsNode)

Binary file added ui/icons/sv_curve_to_nurbs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2b817c6

Please sign in to comment.