
    ^j(:                         d Z ddlmZmZmZ ddlZddlmZ ddl	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZmZ ddlmZmZmZmZm Z  ddl!m"Z" ddl#m$Z$m#Z# ddl%m&Z& de
fdZ' G d d      Z(y)z
Surface skinning from curves.

This module provides functionality to create surfaces by skinning
a set of compatible B-spline curves.
    )ListOptionalTupleN)BSplCLib)Geom_BSplineCurveGeom_BSplineSurface
Geom_CurveGeom_TrimmedCurve)GeomAPI_Interpolate)GeomConvert)gp_Pnt)	Precision)TColgp_Array1OfPntTColgp_Array2OfPntTColgp_HArray1OfPntTColgp_HArray1OfPnt2d)TColStd_Array1OfIntegerTColStd_Array1OfRealTColStd_Array2OfRealTColStd_HArray1OfIntegerTColStd_HArray1OfReal   )BSplineAlgorithms)	ErrorCodeerror)PointsToBSplineInterpolationcurvec                     | j                         sy| j                          | j                         }| j                         }t	        | ||      }t        j                  |      }|S )zg
    Clamps a periodic B-spline curve by removing periodicity.
    Equivalent to C++ clampBSpline.
    N)
IsPeriodicSetNotPeriodicFirstParameterLastParameterr
   r   CurveToBSplineCurve_s)r   first_param
last_paramtrimmed_curve	new_curves        ^/opt/ringagent/.cad-venv/lib/python3.12/site-packages/ocp_gordon/internal/curves_to_surface.pyclamp_bspliner)   .   sg    
  
&&(K$$&J &e[*EM 11-@I     c            	       ~    e Zd ZdZ	 	 	 ddee   dee   dz  dedefdZde	fd	Z
d
ee   fdZd
efdZd Zd Zd Zy)CurvesToSurfacea  
    Creates a surface by skinning a set of compatible B-spline curves.

    The curves must have compatible parameterizations, degrees, and
    knot vectors for successful skinning. This implementation aims to
    mirror the C++ version's functionality.
    Ncurves
parameterscontinuous_if_closed	tolerancec                    || _         ||ng | _        || _        || _        d| _        d| _        d| _        g | _        g | _        | j                   D ]0  }| j                  j                  t        j                  |             2 t        j                  | j                         | j                  s| j                          | j                  s0t        j                  | j                  | j                        | _        yy)a  
        Initialize the surface skinner.

        Args:
            curves: List of curves to be interpolated. These will be converted to B-splines.
            parameters: Optional list of parameters for v-direction interpolation. If None, they will be calculated.
            continuous_if_closed: If True, attempts to make a C2 continuous surface at the start/end junction
                                  if the first and last curve are the same.
            tolerance: Construction tolerance.
        N   F)_input_curves_raw_parameters_continuous_if_closed
_tolerance_max_degree_has_performed_skinned_surface_input_curves_compatible_splinesappendr   r#   r   match_degree_calculate_parameters create_common_knots_vector_curve)selfr-   r.   r/   r0   r   s         r(   __init__zCurvesToSurface.__init__X   s    " "()3)?:R%9"# 	 $ $68<>  ++ 	PE%%k&G&G&NO	P 	&&t'9'9: &&( ''!BB&& $ (r*   degreec                 R    |dk  rt        d      || _        | j                          y)z
        Sets the maximum interpolation degree of the splines in skinning direction (v-direction).

        Args:
            degree: Maximum degree for v-direction interpolation.
        r   zDegree must be positive.N)
ValueErrorr7   
invalidate)r@   rB   s     r(   set_max_degreezCurvesToSurface.set_max_degree   s)     Q;788!r*   returnc                     | j                   S )z;Returns the parameters at the profile curves (v-direction).)r4   r@   s    r(   get_parameterszCurvesToSurface.get_parameters   s    r*   c                     | j                   s| j                          | j                  t        d      | j                  S )zReturns the skinned surface.zSurface could not be created.)r8   performr9   RuntimeErrorrI   s    r(   surfacezCurvesToSurface.surface   s8    ""LLN  (>??$$$r*   c                      d| _         d| _        y)zCInvalidates the cached surface, forcing recomputation on next call.FN)r8   r9   rI   s    r(   rE   zCurvesToSurface.invalidate   s    # $r*   c           	         | j                   sy| j                  s/t        j                  | j                   | j                        | _        | j                  d   }|j                         }t        | j                        }t        d|d|      }t        | j                  d      D ];  \  }}t        d|dz         D ]$  }|j                  |||j                  |             & = t        j                  |d      \  }}	|	| _        y)z
        Calculates parameters for the v-direction based on control points.
        Mirrors C++ CurvesToSurface::CalculateParameters.
        Nr   r   g      ?)alpha)r:   r;   r   r?   r6   NbPoleslenr   	enumeraterangeSetValuePolecompute_params_bspline_surfr4   )
r@   first_curvenum_poles_unum_splinescontrol_points_matrix
spline_idxsplinepole_idx_v_paramss
             r(   r>   z%CurvesToSurface._calculate_parameters   s   
 !! ''!BB&& $ ..q1!))+$223 !31k1k R"+D,D,Da"H 	J!![1_5 %..j&++h*?	 (CC!
8 $r*   c           
      8
   t        | j                        dk  ryt        | j                        t        | j                        k7  rt        dt        j
                        t        j                  | j                        t        j                  z  }| j                  xrF t        | j                        dkD  xr, | j                  d   j                  | j                  d   |      }t        | j                        }| j                  s/t        j                  | j                  | j                        | _        | j                  d   }|j                         }d}|j                         }d}d}	t!               }
d}t#        d|      }t%        d|dz         D ]^  }t%        d|dz         D ]5  }| j                  |dz
     j'                  |      }|j)                  ||       7 t+        || j                  | j,                  |      }|j/                         }|t1        d|       |rt3        |      }|r|}|dk(  r*|j                         }g }t%        d|j5                         dz         D ]"  }|j7                  |j9                  |             $ t;        dt        |            }t=        |d      D ]  \  }}|j)                  ||        g }t%        d|j5                         dz         D ]"  }|j7                  |j?                  |             $ tA        dt        |            }	t=        |d      D ]  \  }}|	j)                  ||        t!        d|d|j                               }
t%        d|j                         dz         D ]$  }|
j)                  |||j'                  |             & |dkD  s||j                         k7  s/t        d	| d
| d|j                          dt        j
                         g }t%        d|j5                         dz         D ]"  }|j7                  |j9                  |             $ t;        dt        |            }t=        |d      D ]  \  }}|j)                  ||        g }t%        d|j5                         dz         D ]"  }|j7                  |j?                  |             $ tA        dt        |            }t=        |d      D ]  \  }}|j)                  ||        ||	t1        d      tC        |
||||	||      | _"        d| _#        y)zi
        Build the surface by skinning the curves.
        Mirrors C++ CurvesToSurface::Perform.
           NzPThe amount of given parameters has to be equal to the amount of given B-splines!r   r   )pointsr.   
max_degreer/   z!Interpolation failed for U index z'Inconsistent degree_v found at U index z. Expected z, got .z6Failed to obtain V direction knots and multiplicities.T)$rS   r:   r4   r   r   
MATH_ERRORr   scaleREL_TOL_CLOSEDr5   IsEqualr;   r?   r6   rR   Degreer   r   rU   rW   rV   r   r7   r   rM   r)   NbKnotsr<   Knotr   rT   Multiplicityr   r   r9   r8   )r@   r0   make_closedn_curvesrY   num_control_points_udegree_vdegree_uknots_vmults_vcp_surfinterp_splineinterp_points_v_dircp_u_idxcp_v_idxpoleinterpolatorclamped_splineknots_v_listikmults_v_listmknots_u_listknots_umults_u_listmults_us                              r(   rL   zCurvesToSurface.perform   sL    t!!"Q&t C(:(:$;;b$$  ##D$6$67../ 	 && QD&&'!+Q""1%--d.@.@.DiP 	 t))* ''!BB&& $ ..q1*224%%'
 %& 2!X> a!5!9: B	H!!X\2 =//1=BB8L#,,Xt<= 8*++++%0	L )..0M$"%Fxj#QRR!.}!="$2M1}(//1  "q-"7"7"9A"=> ?A ''(:(:1(=>?.q#l2CD%lA6 +DAq$$Q*+  "q-"7"7"9A"=> GA ''(B(B1(EFG1!S5FG%lA6 +DAq$$Q*+ -+Q0E0E0G
 1m3359: E  1m.@.@.CDE
 !|M,@,@,B B=hZ{S[R\\bcpcwcwcybzz{|(( B	J q+--/!34 	5A 0 0 34	5&q#l*;<lA. 	#DAqQ"	# q+--/!34 	=A 8 8 ;<	=)!S->?lA. 	#DAqQ"	# ?goWXX
 !4!
 #r*   )NFg+=)__name__
__module____qualname____doc__listr	   floatboolrA   intrF   rJ   r   rN   rE   r>   rL    r*   r(   r,   r,   O   s     *.%* /Z / K$&/ #	/
 /b
S 
 U  %, %%
*$X`#r*   r,   ))r   typingr   r   r   numpynpOCP.BSplCLibr   OCP.Geomr   r   r	   r
   OCP.GeomAPIr   OCP.GeomConvertr   OCP.gpr   OCP.Precisionr   
OCP.TColgpr   r   r   r   OCP.TColStdr   r   r   r   r   bspline_algorithmsr   r   r   points_to_bspline_interpolationr   r)   r,   r   r*   r(   <module>r      sm    ) (  !  (  #   2 #* Bc# c#r*   