2626
2727from beartype import beartype as check_input_types
2828import numpy as np
29+ from scipy .integrate import quad
2930
3031from ansys .geometry .core .math import Matrix44 , Point3D
3132from ansys .geometry .core .math .vector import Vector3D
@@ -56,7 +57,7 @@ class NURBSCurve(Curve):
5657
5758 """
5859
59- def __init__ (self ):
60+ def __init__ (self , geomdl_object : "geomdl_nurbs.Curve" = None ):
6061 """Initialize ``NURBSCurve`` class."""
6162 try :
6263 import geomdl .NURBS as geomdl_nurbs # noqa: N811
@@ -66,7 +67,7 @@ def __init__(self):
6667 "Please install it using `pip install geomdl`."
6768 ) from e
6869
69- self ._nurbs_curve = geomdl_nurbs .Curve ()
70+ self ._nurbs_curve = geomdl_object if geomdl_object else geomdl_nurbs .Curve ()
7071
7172 @property
7273 def geomdl_nurbs_curve (self ) -> "geomdl_nurbs.Curve" :
@@ -99,6 +100,38 @@ def weights(self) -> list[Real]:
99100 """Get the weights of the control points."""
100101 return self ._nurbs_curve .weights
101102
103+ def length (self , num_points : int = None ) -> Real :
104+ """Calculate the length of the NURBS curve.
105+
106+ Parameters
107+ ----------
108+ num_points : int, default: None
109+ Number of points to sample along the curve for length calculation.
110+
111+ Returns
112+ -------
113+ Real
114+ Length of the NURBS curve.
115+ """
116+ if num_points is None :
117+ num_spans = len (self ._nurbs_curve .knotvector ) - (2 * self ._nurbs_curve .degree ) - 1
118+ num_points = max (
119+ num_spans * 10 , 50
120+ ) # 10 samples per span, floor of 50 ensures accuracy
121+
122+ self ._nurbs_curve .sample_size = num_points
123+
124+ def arc_length_func (u ):
125+ deriv = self ._nurbs_curve .derivatives (u , order = 1 )[1 ]
126+ return np .linalg .norm (deriv )
127+
128+ # Integrate over the curve's domain
129+ start_u = self ._nurbs_curve .knotvector [self ._nurbs_curve .degree ]
130+ end_u = self ._nurbs_curve .knotvector [- (self ._nurbs_curve .degree + 1 )]
131+
132+ length , _ = quad (arc_length_func , start_u , end_u )
133+ return length
134+
102135 @classmethod
103136 @check_input_types
104137 def from_control_points (
0 commit comments