scirs2-interpolate 0.4.1

Interpolation module for SciRS2 (scirs2-interpolate)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! Core NURBS constructors and basic operations
//!
//! This module contains the fundamental constructors and accessor methods
//! for NURBS curves and surfaces, including parameter validation and
//! basic geometric queries.

use crate::bspline::{BSpline, ExtrapolateMode};
use crate::error::{InterpolateError, InterpolateResult};
use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2};
use super::types::{NurbsCurve, NurbsSurface, NurbsFloat, NurbsValidator, ValidationResult};

impl<T: NurbsFloat> NurbsCurve<T> {
    /// Create a new NURBS curve from control points, weights, knots, and degree
    ///
    /// # Arguments
    ///
    /// * `control_points` - Control points in n-dimensional space
    /// * `weights` - Weights for each control point (must have the same length as `control_points.shape()[0]`)
    /// * `knots` - Knot vector
    /// * `degree` - Degree of the NURBS curve
    /// * `extrapolate` - Extrapolation mode
    ///
    /// # Returns
    ///
    /// A new `NurbsCurve` object
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Control points and weights have different lengths
    /// - Any weight is non-positive
    /// - Knot vector is invalid
    /// - Degree is too high for the number of control points
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scirs2_core::ndarray::{array, Array1, Array2};
    /// use scirs2_interpolate::nurbs::NurbsCurve;
    /// use scirs2_interpolate::bspline::ExtrapolateMode;
    ///
    /// // Create a simple NURBS curve
    /// let control_points = array![
    ///     [0.0, 0.0],
    ///     [1.0, 1.0],
    ///     [2.0, 0.0]
    /// ];
    /// let weights = array![1.0, 1.0, 1.0];
    /// let knots = array![0.0, 0.0, 0.0, 1.0, 1.0, 1.0];
    /// let degree = 2;
    ///
    /// let curve = NurbsCurve::new(
    ///     &control_points.view(),
    ///     &weights.view(),
    ///     &knots.view(),
    ///     degree,
    ///     ExtrapolateMode::Extrapolate
    /// ).expect("Operation failed");
    /// ```
    pub fn new(
        control_points: &ArrayView2<T>,
        weights: &ArrayView1<T>,
        knots: &ArrayView1<T>,
        degree: usize,
        extrapolate: ExtrapolateMode,
    ) -> InterpolateResult<Self> {
        // Validate input parameters
        let validation = NurbsValidator::validate_curve_inputs(&control_points.to_owned(), &weights.to_owned(), &knots.to_owned(), degree);
        if !validation.is_valid() {
            return Err(InterpolateError::invalid_input(
                validation.to_error_message().to_string(),
            ));
        }

        // Create homogeneous coordinates by multiplying control points by weights
        let n = control_points.shape()[0];
        let dim = control_points.shape()[1];
        let mut homogeneous_coords = Array1::zeros(n);

        // We set the coefficient array to just the weights for now
        // Later in evaluate() we'll compute the full homogeneous coordinates
        for i in 0..n {
            homogeneous_coords[i] = weights[i];
        }

        // Create the underlying B-spline
        let bspline = BSpline::new(knots, &homogeneous_coords.view(), degree, extrapolate)?;

        Ok(NurbsCurve {
            control_points: control_points.to_owned(),
            weights: weights.to_owned(),
            bspline,
            dimension: dim,
        })
    }

    /// Create a NURBS curve from arrays (convenience constructor)
    ///
    /// This is a convenience method that takes owned arrays instead of views.
    ///
    /// # Arguments
    ///
    /// * `control_points` - Control points in n-dimensional space
    /// * `weights` - Weights for each control point
    /// * `knots` - Knot vector
    /// * `degree` - Degree of the NURBS curve
    /// * `extrapolate` - Extrapolation mode
    ///
    /// # Returns
    ///
    /// A new `NurbsCurve` object
    pub fn from_arrays(
        control_points: Array2<T>,
        weights: Array1<T>,
        knots: Array1<T>,
        degree: usize,
        extrapolate: ExtrapolateMode,
    ) -> InterpolateResult<Self> {
        Self::new(
            &control_points.view(),
            &weights.view(),
            &knots.view(),
            degree,
            extrapolate,
        )
    }

    /// Get the control points of the NURBS curve
    ///
    /// # Returns
    ///
    /// Reference to the 2D array of control points
    pub fn control_points(&self) -> &Array2<T> {
        &self.control_points
    }

    /// Get the weights of the NURBS curve
    ///
    /// # Returns
    ///
    /// Reference to the 1D array of weights
    pub fn weights(&self) -> &Array1<T> {
        &self.weights
    }

    /// Get the knot vector of the NURBS curve
    ///
    /// # Returns
    ///
    /// Reference to the knot vector
    pub fn knots(&self) -> &Array1<T> {
        self.bspline.knot_vector()
    }

    /// Get the degree of the NURBS curve
    ///
    /// # Returns
    ///
    /// The degree of the curve
    pub fn degree(&self) -> usize {
        self.bspline.degree()
    }

    /// Get the dimension of the control points
    ///
    /// # Returns
    ///
    /// The spatial dimension of the control points
    pub fn dimension(&self) -> usize {
        self.dimension
    }

    /// Get the number of control points
    ///
    /// # Returns
    ///
    /// The number of control points
    pub fn len(&self) -> usize {
        self.control_points.nrows()
    }

    /// Check if the curve has no control points
    ///
    /// # Returns
    ///
    /// True if the curve has no control points
    pub fn is_empty(&self) -> bool {
        self.control_points.is_empty()
    }

    /// Get the parameter domain of the curve
    ///
    /// # Returns
    ///
    /// A tuple (t_min, t_max) representing the parameter domain
    pub fn domain(&self) -> (T, T) {
        let knots = self.bspline.knot_vector();
        let degree = self.bspline.degree();
        let t_min = knots[degree];
        let t_max = knots[knots.len() - degree - 1];
        (t_min, t_max)
    }

    /// Clone the curve with modified weights
    ///
    /// Creates a new curve with the same control points and knots but different weights.
    ///
    /// # Arguments
    ///
    /// * `new_weights` - New weights for the control points
    ///
    /// # Returns
    ///
    /// A new `NurbsCurve` with the modified weights
    ///
    /// # Errors
    ///
    /// Returns an error if the new weights array has incorrect length or contains non-positive values.
    pub fn with_weights(&self, new_weights: &ArrayView1<T>) -> InterpolateResult<Self> {
        Self::new(
            &self.control_points.view(),
            new_weights,
            &self.knots().view(),
            self.degree(),
            self.bspline.extrapolate_mode(),
        )
    }

    /// Clone the curve with modified control points
    ///
    /// Creates a new curve with the same weights and knots but different control points.
    ///
    /// # Arguments
    ///
    /// * `new_control_points` - New control points
    ///
    /// # Returns
    ///
    /// A new `NurbsCurve` with the modified control points
    ///
    /// # Errors
    ///
    /// Returns an error if the new control points have incorrect dimensions.
    pub fn with_control_points(&self, new_control_points: &ArrayView2<T>) -> InterpolateResult<Self> {
        Self::new(
            new_control_points,
            &self.weights.view(),
            &self.knots().view(),
            self.degree(),
            self.bspline.extrapolate_mode(),
        )
    }
}

impl<T: NurbsFloat> NurbsSurface<T> {
    /// Create a new NURBS surface from control points, weights, knot vectors, and degrees
    ///
    /// # Arguments
    ///
    /// * `control_points` - Control points arranged as (nu * nv x dim)
    /// * `weights` - Weights for each control point (nu * nv)
    /// * `nu` - Number of control points in the u direction
    /// * `nv` - Number of control points in the v direction
    /// * `knotsu` - Knot vector in the u direction
    /// * `knotsv` - Knot vector in the v direction
    /// * `degreeu` - Degree in the u direction
    /// * `degreev` - Degree in the v direction
    /// * `extrapolate` - Extrapolation mode
    ///
    /// # Returns
    ///
    /// A new `NurbsSurface` object
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Control points and weights have inconsistent dimensions
    /// - Any weight is non-positive
    /// - Knot vectors are invalid
    /// - Degrees are too high for the number of control points
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scirs2_core::ndarray::{array, Array1, Array2};
    /// use scirs2_interpolate::nurbs::NurbsSurface;
    /// use scirs2_interpolate::bspline::ExtrapolateMode;
    ///
    /// // Create a simple NURBS surface
    /// let control_points = array![
    ///     [0.0, 0.0, 0.0], [1.0, 0.0, 0.0],
    ///     [0.0, 1.0, 0.0], [1.0, 1.0, 1.0]
    /// ];
    /// let weights = array![1.0, 1.0, 1.0, 1.0];
    /// let knotsu = array![0.0, 0.0, 1.0, 1.0];
    /// let knotsv = array![0.0, 0.0, 1.0, 1.0];
    ///
    /// let surface = NurbsSurface::new(
    ///     &control_points.view(),
    ///     &weights.view(),
    ///     2, 2,  // nu, nv
    ///     &knotsu.view(),
    ///     &knotsv.view(),
    ///     1, 1,  // degreeu, degreev
    ///     ExtrapolateMode::Extrapolate
    /// ).expect("Operation failed");
    /// ```
    pub fn new(
        control_points: &ArrayView2<T>,
        weights: &ArrayView1<T>,
        nu: usize,
        nv: usize,
        knotsu: &ArrayView1<T>,
        knotsv: &ArrayView1<T>,
        degreeu: usize,
        degreev: usize,
        extrapolate: ExtrapolateMode,
    ) -> InterpolateResult<Self> {
        // Validate input parameters
        let validation = NurbsValidator::validate_surface_inputs(
            &control_points.to_owned(), &weights.to_owned(), nu, nv, &knotsu.to_owned(), &knotsv.to_owned(), degreeu, degreev,
        );
        if !validation.is_valid() {
            return Err(InterpolateError::invalid_input(
                validation.to_error_message().to_string(),
            ));
        }

        let dimension = control_points.shape()[1];

        Ok(NurbsSurface {
            control_points: control_points.to_owned(),
            weights: weights.to_owned(),
            nu,
            nv,
            knotsu: knotsu.to_owned(),
            knotsv: knotsv.to_owned(),
            degreeu,
            degreev,
            dimension,
            extrapolate,
        })
    }

    /// Create a NURBS surface from arrays (convenience constructor)
    ///
    /// This is a convenience method that takes owned arrays instead of views.
    ///
    /// # Arguments
    ///
    /// * `control_points` - Control points arranged as (nu * nv x dim)
    /// * `weights` - Weights for each control point
    /// * `nu` - Number of control points in the u direction
    /// * `nv` - Number of control points in the v direction
    /// * `knotsu` - Knot vector in the u direction
    /// * `knotsv` - Knot vector in the v direction
    /// * `degreeu` - Degree in the u direction
    /// * `degreev` - Degree in the v direction
    /// * `extrapolate` - Extrapolation mode
    ///
    /// # Returns
    ///
    /// A new `NurbsSurface` object
    pub fn from_arrays(
        control_points: Array2<T>,
        weights: Array1<T>,
        nu: usize,
        nv: usize,
        knotsu: Array1<T>,
        knotsv: Array1<T>,
        degreeu: usize,
        degreev: usize,
        extrapolate: ExtrapolateMode,
    ) -> InterpolateResult<Self> {
        Self::new(
            &control_points.view(),
            &weights.view(),
            nu,
            nv,
            &knotsu.view(),
            &knotsv.view(),
            degreeu,
            degreev,
            extrapolate,
        )
    }

    /// Get the control points of the NURBS surface
    ///
    /// # Returns
    ///
    /// Reference to the 2D array of control points
    pub fn control_points(&self) -> &Array2<T> {
        &self.control_points
    }

    /// Get the weights of the NURBS surface
    ///
    /// # Returns
    ///
    /// Reference to the 1D array of weights
    pub fn weights(&self) -> &Array1<T> {
        &self.weights
    }

    /// Get the knot vector in the u direction
    ///
    /// # Returns
    ///
    /// Reference to the u-direction knot vector
    pub fn knotsu(&self) -> &Array1<T> {
        &self.knotsu
    }

    /// Get the knot vector in the v direction
    ///
    /// # Returns
    ///
    /// Reference to the v-direction knot vector
    pub fn knotsv(&self) -> &Array1<T> {
        &self.knotsv
    }

    /// Get the degree in the u direction
    ///
    /// # Returns
    ///
    /// The degree in the u direction
    pub fn degreeu(&self) -> usize {
        self.degreeu
    }

    /// Get the degree in the v direction
    ///
    /// # Returns
    ///
    /// The degree in the v direction
    pub fn degreev(&self) -> usize {
        self.degreev
    }

    /// Get the number of control points in the u direction
    ///
    /// # Returns
    ///
    /// Number of control points in u direction
    pub fn nu(&self) -> usize {
        self.nu
    }

    /// Get the number of control points in the v direction
    ///
    /// # Returns
    ///
    /// Number of control points in v direction
    pub fn nv(&self) -> usize {
        self.nv
    }

    /// Get the dimension of the control points
    ///
    /// # Returns
    ///
    /// The spatial dimension of the control points
    pub fn dimension(&self) -> usize {
        self.dimension
    }

    /// Get the total number of control points
    ///
    /// # Returns
    ///
    /// The total number of control points (nu * nv)
    pub fn len(&self) -> usize {
        self.nu * self.nv
    }

    /// Check if the surface has no control points
    ///
    /// # Returns
    ///
    /// True if the surface has no control points
    pub fn is_empty(&self) -> bool {
        self.nu == 0 || self.nv == 0
    }

    /// Get the parameter domain of the surface
    ///
    /// # Returns
    ///
    /// A tuple ((u_min, u_max), (v_min, v_max)) representing the parameter domain
    pub fn domain(&self) -> ((T, T), (T, T)) {
        let u_min = self.knotsu[self.degreeu];
        let u_max = self.knotsu[self.nu];
        let v_min = self.knotsv[self.degreev];
        let v_max = self.knotsv[self.nv];

        ((u_min, u_max), (v_min, v_max))
    }

    /// Get the extrapolation mode
    ///
    /// # Returns
    ///
    /// The extrapolation mode used by the surface
    pub fn extrapolate_mode(&self) -> ExtrapolateMode {
        self.extrapolate
    }
}