opensubdiv-petite 0.3.1

Wrapper around parts of Pixar’s OpenSubdiv
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! # Patch Table
//!
//! A `PatchTable` is a representation of the refined surface topology that can
//! be used for efficient evaluation of primvar data at arbitrary locations.
//!
//! The patches in a `PatchTable` are organized into patch arrays, where all
//! patches in a patch array have the same patch type. Each patch has a
//! `PatchDescriptor` that describes the number and arrangement of control
//! points, and a `PatchParam` that provides additional information about the
//! patch's parameterization.

use super::StencilTableRef;
use crate::{Error, Index};
use opensubdiv_petite_sys as sys;
use std::marker::PhantomData;
use std::pin::Pin;

/// Options for creating a patch table
pub struct PatchTableOptions {
    inner: Pin<Box<sys::far::PatchTableFactoryOptions>>,
}

impl Default for PatchTableOptions {
    fn default() -> Self {
        Self::new()
    }
}

impl PatchTableOptions {
    /// Create a new PatchTableOptions with default settings
    pub fn new() -> Self {
        unsafe {
            let ptr = sys::far::PatchTableFactory_Options_new();
            assert!(!ptr.is_null());
            Self {
                inner: Pin::new(Box::from_raw(ptr)),
            }
        }
    }

    /// Set the end cap type
    pub fn end_cap_type(mut self, end_cap_type: EndCapType) -> Self {
        unsafe {
            sys::far::PatchTableFactory_Options_SetEndCapType(
                self.inner.as_mut().get_unchecked_mut(),
                end_cap_type as i32,
            );
        }
        self
    }

    /// Get the end cap type
    pub fn get_end_cap_type(&self) -> EndCapType {
        unsafe {
            let end_cap = sys::far::PatchTableFactory_Options_GetEndCapType(
                self.inner.as_ref().get_ref() as *const _,
            );
            match end_cap {
                0 => EndCapType::None,
                1 => EndCapType::BSplineBasis,
                2 => EndCapType::GregoryBasis,
                3 => EndCapType::LegacyGregory,
                _ => EndCapType::None,
            }
        }
    }

    /// Set the triangle subdivision type
    pub fn triangle_subdivision(mut self, triangle_subdivision: TriangleSubdivision) -> Self {
        unsafe {
            sys::far::PatchTableFactory_Options_SetTriangleSubdivision(
                self.inner.as_mut().get_unchecked_mut(),
                triangle_subdivision as i32,
            );
        }
        self
    }

    /// Set whether to use infinitely sharp patches
    pub fn use_inf_sharp_patch(mut self, use_inf_sharp_patch: bool) -> Self {
        unsafe {
            sys::far::PatchTableFactory_Options_SetUseInfSharpPatch(
                self.inner.as_mut().get_unchecked_mut(),
                use_inf_sharp_patch,
            );
        }
        self
    }

    /// Set the number of legacy Gregory patches
    pub fn num_legacy_gregory_patches(mut self, num_patches: i32) -> Self {
        unsafe {
            sys::far::PatchTableFactory_Options_SetNumLegacyGregoryPatches(
                self.inner.as_mut().get_unchecked_mut(),
                num_patches,
            );
        }
        self
    }

    pub(crate) fn as_ptr(&self) -> *const sys::far::PatchTableFactoryOptions {
        self.inner.as_ref().get_ref() as *const _
    }
}

impl Drop for PatchTableOptions {
    fn drop(&mut self) {
        unsafe {
            sys::far::PatchTableFactory_Options_delete(self.inner.as_mut().get_unchecked_mut());
        }
    }
}

/// End cap types for patch generation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EndCapType {
    /// No end caps
    None,
    /// B-spline basis end caps
    BSplineBasis,
    /// Gregory basis end caps
    GregoryBasis,
    /// Legacy Gregory end caps
    LegacyGregory,
}

// Re-export TriangleSubdivision from topology_refiner
pub use crate::far::topology_refiner::TriangleSubdivision;

/// A patch table containing refined surface patches.
///
/// The [`PatchTable`] stores a collection of surface patches generated from
/// a refined [`TopologyRefiner`](crate::far::TopologyRefiner). It represents
/// the limit surface as a collection of parametric patches (B-spline, Bezier,
/// or Gregory patches).
///
/// ## Local Points
///
/// For patches near extraordinary vertices or boundaries that cannot be
/// represented with regular B-spline or Bezier patches, OpenSubdiv creates
/// additional "local points". These are extra control vertices needed for
/// Gregory patches and other irregular patch types.
///
/// The local points are computed from the refined vertices using stencil
/// tables. When [`local_point_stencil_table`](Self::local_point_stencil_table)
/// returns a stencil table with `control_vertex_count() == 0`, this indicates
/// the stencil table is specifically for computing local points from refined
/// vertices, not from base vertices.
///
/// ## Example
///
/// See the `patch_table_local_points` example for a complete demonstration
/// of working with local points and their stencil tables.
pub struct PatchTable {
    ptr: *mut sys::far::PatchTable,
    _phantom: PhantomData<sys::far::PatchTable>,
}

impl PatchTable {
    /// Create a new patch table from a topology refiner
    pub fn new(
        refiner: &crate::far::TopologyRefiner,
        options: Option<PatchTableOptions>,
    ) -> Result<Self, Error> {
        unsafe {
            let options_ptr = options
                .as_ref()
                .map(|o| o.as_ptr())
                .unwrap_or(std::ptr::null());

            let ptr = sys::far::PatchTableFactory_Create(refiner.as_ptr(), options_ptr);

            if ptr.is_null() {
                Err(Error::PatchTableCreation)
            } else {
                Ok(Self {
                    ptr,
                    _phantom: PhantomData,
                })
            }
        }
    }

    /// Get the number of patch arrays
    pub fn patch_array_count(&self) -> usize {
        unsafe { sys::far::PatchTable_GetNumPatchArrays(self.ptr) as usize }
    }

    /// Get the number of patch arrays
    #[deprecated(since = "0.3.0", note = "Use `patch_array_count` instead")]
    #[inline]
    pub fn patch_arrays_len(&self) -> usize {
        self.patch_array_count()
    }

    /// Get the total number of patches
    pub fn patch_count(&self) -> usize {
        unsafe { sys::far::PatchTable_GetNumPatches(self.ptr) as usize }
    }

    /// Get the total number of patches
    #[deprecated(since = "0.3.0", note = "Use `patch_count` instead")]
    #[inline]
    pub fn patches_len(&self) -> usize {
        self.patch_count()
    }

    /// Get the number of control vertices
    pub fn control_vertex_count(&self) -> usize {
        unsafe { sys::far::PatchTable_GetNumControlVertices(self.ptr) as usize }
    }

    /// Get the number of control vertices
    #[deprecated(since = "0.3.0", note = "Use `control_vertex_count` instead")]
    #[inline]
    pub fn control_vertices_len(&self) -> usize {
        self.control_vertex_count()
    }

    /// Get the maximum valence
    pub fn max_valence(&self) -> usize {
        unsafe { sys::far::PatchTable_GetMaxValence(self.ptr) as usize }
    }

    /// Get the number of local points.
    ///
    /// Local points are additional vertices created for patches that cannot
    /// be represented with regular B-spline or Bezier patches, such as
    /// Gregory patches near extraordinary vertices.
    pub fn local_point_count(&self) -> usize {
        unsafe { sys::far::PatchTable_GetNumLocalPoints(self.ptr) as usize }
    }

    /// Get the stencil table for computing local points.
    ///
    /// The returned stencil table can be used to compute local point positions
    /// from refined vertices. When the stencil table's `control_vertex_count()`
    /// returns 0, it indicates the stencils operate on refined vertices rather
    /// than base vertices.
    ///
    /// # Returns
    ///
    /// - `Some(stencil_table)` if local points exist and need to be computed.
    /// - `None` if there are no local points (all patches are regular).
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use opensubdiv_petite::far::{PatchTable, TopologyRefiner};
    /// # fn example(patch_table: &PatchTable, refined_vertices: &[f32]) {
    /// if let Some(stencil_table) = patch_table.local_point_stencil_table() {
    ///     // When control_vertex_count is 0, use refined vertices as input
    ///     if stencil_table.control_vertex_count() == 0 {
    ///         let local_points = stencil_table.update_values(refined_vertices, None, None);
    ///         // local_points now contains the computed local point positions
    ///     }
    /// }
    /// # }
    /// ```
    pub fn local_point_stencil_table(&self) -> Option<StencilTableRef<'_>> {
        unsafe {
            let stencil_ptr = sys::far::PatchTable_GetLocalPointStencilTable(self.ptr);
            if stencil_ptr.is_null() {
                None
            } else {
                Some(StencilTableRef {
                    ptr: stencil_ptr as *mut _,
                    _marker: std::marker::PhantomData,
                })
            }
        }
    }

    /// Get the number of patches in a specific patch array
    pub fn patch_array_patch_count(&self, array_index: usize) -> usize {
        unsafe {
            sys::far::PatchTable_GetNumPatches_PatchArray(self.ptr, array_index as i32) as usize
        }
    }

    /// Get the number of patches in a specific patch array
    #[deprecated(since = "0.3.0", note = "Use `patch_array_patch_count` instead")]
    #[inline]
    pub fn patch_array_patches_len(&self, array_index: usize) -> usize {
        self.patch_array_patch_count(array_index)
    }

    /// Get the descriptor for a patch array
    pub fn patch_array_descriptor(&self, array_index: usize) -> Option<PatchDescriptor> {
        if array_index >= self.patch_array_count() {
            return None;
        }

        unsafe {
            let mut desc = std::mem::zeroed::<sys::far::PatchDescriptor>();
            sys::far::PatchTable_GetPatchArrayDescriptor(self.ptr, array_index as i32, &mut desc);
            Some(PatchDescriptor { inner: desc })
        }
    }

    /// Get the control vertex indices for a patch array
    pub fn patch_array_vertices(&self, array_index: usize) -> Option<&[Index]> {
        if array_index >= self.patch_array_count() {
            return None;
        }

        unsafe {
            let ptr = sys::far::PatchTable_GetPatchArrayVertices(self.ptr, array_index as i32);
            if ptr.is_null() {
                None
            } else {
                let len = self.patch_array_patch_count(array_index);
                let desc = self.patch_array_descriptor(array_index)?;
                let num_cvs = desc.control_vertex_count();
                let total_len = len * num_cvs;

                // Cast from i32 to Index (u32)
                Some(std::slice::from_raw_parts(ptr as *const Index, total_len))
            }
        }
    }

    /// Get the patch parameter for a specific patch
    pub fn patch_param(&self, array_index: usize, patch_index: usize) -> Option<PatchParam> {
        if array_index >= self.patch_array_count() {
            return None;
        }

        if patch_index >= self.patch_array_patch_count(array_index) {
            return None;
        }

        unsafe {
            let mut param = std::mem::zeroed::<sys::far::PatchParam>();
            sys::far::PatchTable_GetPatchParam(
                self.ptr,
                array_index as i32,
                patch_index as i32,
                &mut param,
            );
            Some(PatchParam { inner: param })
        }
    }

    /// Get all patch control vertex indices
    pub fn control_vertices_table(&self) -> Option<&[Index]> {
        unsafe {
            let ptr = sys::far::PatchTable_GetPatchControlVerticesTable(self.ptr);
            if ptr.is_null() {
                None
            } else {
                let len = self.control_vertex_count();
                // Cast from i32 to Index (u32)
                Some(std::slice::from_raw_parts(ptr as *const Index, len))
            }
        }
    }

    pub(crate) fn as_ptr(&self) -> *const sys::far::PatchTable {
        self.ptr
    }
}

impl Drop for PatchTable {
    fn drop(&mut self) {
        unsafe {
            sys::far::PatchTable_delete(self.ptr);
        }
    }
}

unsafe impl Send for PatchTable {}
unsafe impl Sync for PatchTable {}

/// Describes a patch type and its control point arrangement
#[derive(Clone, Copy)]
pub struct PatchDescriptor {
    inner: sys::far::PatchDescriptor,
}

impl PatchDescriptor {
    /// Get the patch type
    pub fn patch_type(&self) -> PatchType {
        unsafe {
            let patch_type = sys::far::PatchDescriptor_GetType(&self.inner);
            match patch_type {
                0 => PatchType::NonPatch,
                1 => PatchType::Points,
                2 => PatchType::Lines,
                3 => PatchType::Quads,
                4 => PatchType::Triangles,
                5 => PatchType::Loop,
                6 => PatchType::Regular,
                7 => PatchType::BoundaryPattern0,
                8 => PatchType::BoundaryPattern1,
                9 => PatchType::BoundaryPattern2,
                10 => PatchType::BoundaryPattern3,
                11 => PatchType::BoundaryPattern4,
                12 => PatchType::CornerPattern0,
                13 => PatchType::CornerPattern1,
                14 => PatchType::CornerPattern2,
                15 => PatchType::CornerPattern3,
                16 => PatchType::CornerPattern4,
                17 => PatchType::Gregory,
                18 => PatchType::GregoryBoundary,
                19 => PatchType::GregoryCorner,
                20 => PatchType::GregoryBasis,
                21 => PatchType::GregoryTriangle,
                _ => PatchType::NonPatch,
            }
        }
    }

    /// Get the number of control vertices for this patch type
    pub fn control_vertex_count(&self) -> usize {
        unsafe { sys::far::PatchDescriptor_GetNumControlVertices(&self.inner) as usize }
    }

    /// Get the number of control vertices for this patch type
    #[deprecated(since = "0.3.0", note = "Use `control_vertex_count` instead")]
    #[inline]
    pub fn control_vertices_len(&self) -> usize {
        self.control_vertex_count()
    }

    /// Check if this is a regular patch
    pub fn is_regular(&self) -> bool {
        unsafe { sys::far::PatchDescriptor_IsRegular(&self.inner) }
    }
}

/// Patch types supported by OpenSubdiv
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PatchType {
    /// Not a patch
    NonPatch,
    /// Point patches (1 control vertex)
    Points,
    /// Line patches (2 control vertices)
    Lines,
    /// Quad patches (4 control vertices)
    Quads,
    /// Triangle patches (3 control vertices)
    Triangles,
    /// Loop patches (12 control vertices)
    Loop,
    /// Regular patches (16 control vertices, bi-cubic B-spline)
    Regular,
    /// Boundary pattern patches
    BoundaryPattern0,
    BoundaryPattern1,
    BoundaryPattern2,
    BoundaryPattern3,
    BoundaryPattern4,
    /// Corner pattern patches
    CornerPattern0,
    CornerPattern1,
    CornerPattern2,
    CornerPattern3,
    CornerPattern4,
    /// Gregory patches
    Gregory,
    GregoryBoundary,
    GregoryCorner,
    GregoryBasis,
    GregoryTriangle,
}

/// Parameters for a patch
#[derive(Clone, Copy)]
pub struct PatchParam {
    inner: sys::far::PatchParam,
}

impl PatchParam {
    /// Get the UV coordinates of the patch
    pub fn uv(&self) -> (f32, f32) {
        unsafe {
            let mut u = 0.0;
            let mut v = 0.0;
            sys::far::PatchParam_GetUV(&self.inner, &mut u, &mut v);
            (u, v)
        }
    }

    /// Get the subdivision depth of the patch
    pub fn depth(&self) -> usize {
        unsafe { sys::far::PatchParam_GetDepth(&self.inner) as usize }
    }

    /// Check if this is a regular patch
    pub fn is_regular(&self) -> bool {
        unsafe { sys::far::PatchParam_IsRegular(&self.inner) }
    }

    /// Get the boundary mask
    pub fn boundary(&self) -> i32 {
        unsafe { sys::far::PatchParam_GetBoundary(&self.inner) }
    }

    /// Get the transition mask
    pub fn transition(&self) -> i32 {
        unsafe { sys::far::PatchParam_GetTransition(&self.inner) }
    }
}

/// Result of patch evaluation containing point and derivatives
#[derive(Clone, Copy)]
pub struct PatchEvalResult {
    /// Evaluated point position
    pub point: [f32; 3],
    /// First derivative with respect to u
    pub du: [f32; 3],
    /// First derivative with respect to v
    pub dv: [f32; 3],
    /// Second derivative with respect to u
    pub duu: [f32; 3],
    /// Mixed second derivative
    pub duv: [f32; 3],
    /// Second derivative with respect to v
    pub dvv: [f32; 3],
}

impl From<sys::far::PatchEvalResult> for PatchEvalResult {
    fn from(result: sys::far::PatchEvalResult) -> Self {
        Self {
            point: result.point,
            du: result.du,
            dv: result.dv,
            duu: result.duu,
            duv: result.duv,
            dvv: result.dvv,
        }
    }
}

/// Result of basis evaluation containing weights for position and derivatives
pub type BasisWeights = (Vec<f32>, Vec<f32>, Vec<f32>, Vec<f32>, Vec<f32>, Vec<f32>);

impl PatchTable {
    /// Evaluate basis functions for a patch at given parametric coordinates
    pub fn evaluate_basis(&self, patch_index: usize, u: f32, v: f32) -> Option<BasisWeights> {
        if patch_index >= self.patch_count() {
            return None;
        }

        // Find which patch array this patch belongs to
        let mut array_index = 0;
        let mut local_patch_index = patch_index;

        for i in 0..self.patch_array_count() {
            let num_patches = self.patch_array_patch_count(i);
            if local_patch_index < num_patches {
                array_index = i;
                break;
            }
            local_patch_index -= num_patches;
        }

        // Get the number of control vertices for this patch
        let desc = self.patch_array_descriptor(array_index)?;
        let num_cvs = desc.control_vertex_count();

        // Allocate vectors for weights
        let mut w_p = vec![0.0f32; num_cvs];
        let mut w_du = vec![0.0f32; num_cvs];
        let mut w_dv = vec![0.0f32; num_cvs];
        let mut w_duu = vec![0.0f32; num_cvs];
        let mut w_duv = vec![0.0f32; num_cvs];
        let mut w_dvv = vec![0.0f32; num_cvs];

        unsafe {
            let success = sys::far::PatchTable_EvaluateBasis(
                self.ptr,
                patch_index as i32,
                u,
                v,
                w_p.as_mut_ptr(),
                w_du.as_mut_ptr(),
                w_dv.as_mut_ptr(),
                w_duu.as_mut_ptr(),
                w_duv.as_mut_ptr(),
                w_dvv.as_mut_ptr(),
            );

            if success {
                Some((w_p, w_du, w_dv, w_duu, w_duv, w_dvv))
            } else {
                None
            }
        }
    }

    /// Evaluate a patch at given parametric coordinates using control points
    pub fn evaluate_point(
        &self,
        patch_index: usize,
        u: f32,
        v: f32,
        control_points: &[[f32; 3]],
    ) -> Option<PatchEvalResult> {
        if patch_index >= self.patch_count() {
            return None;
        }

        unsafe {
            let mut result = std::mem::zeroed::<sys::far::PatchEvalResult>();

            let success = sys::far::PatchTable_EvaluatePoint(
                self.ptr,
                patch_index as i32,
                u,
                v,
                control_points.as_ptr() as *const f32,
                control_points.len() as i32,
                &mut result,
            );

            if success {
                Some(result.into())
            } else {
                None
            }
        }
    }
}

/// Map for efficient patch location from face coordinates
pub struct PatchMap {
    ptr: *mut sys::far::PatchMap,
    _phantom: PhantomData<sys::far::PatchMap>,
}

impl PatchMap {
    /// Create a new patch map from a patch table
    pub fn new(patch_table: &PatchTable) -> Option<Self> {
        unsafe {
            let ptr = sys::far::PatchMap_Create(patch_table.as_ptr());
            if ptr.is_null() {
                None
            } else {
                Some(Self {
                    ptr,
                    _phantom: PhantomData,
                })
            }
        }
    }

    /// Find the patch containing a given face at parametric coordinates
    pub fn find_patch(&self, face_index: usize, u: f32, v: f32) -> Option<(usize, f32, f32)> {
        unsafe {
            let mut patch_index = 0i32;
            let mut patch_u = 0.0f32;
            let mut patch_v = 0.0f32;

            let found = sys::far::PatchMap_FindPatch(
                self.ptr,
                face_index as i32,
                u,
                v,
                &mut patch_index,
                &mut patch_u,
                &mut patch_v,
            );

            if found {
                Some((patch_index as usize, patch_u, patch_v))
            } else {
                None
            }
        }
    }
}

impl Drop for PatchMap {
    fn drop(&mut self) {
        unsafe {
            sys::far::PatchMap_delete(self.ptr);
        }
    }
}

unsafe impl Send for PatchMap {}
unsafe impl Sync for PatchMap {}