1use alloc::vec::Vec;
2
3use neco_calculus::SurfacePolynomial;
4
5use crate::encoding_scalar::ScalarEncoder;
6use crate::{
7 ChartPatch, EncodingBounds, EncodingFailure, EncodingLocation, EncodingPrecision,
8 SurfaceOrientation, SurfaceTrimLoop2,
9};
10
11#[derive(Clone, Copy, Debug, PartialEq)]
12pub struct EncodedSurfaceTerm {
13 pub coefficient: f32,
14 pub exponent_u: u32,
15 pub exponent_v: u32,
16}
17
18#[derive(Clone, Debug, PartialEq)]
19pub struct EncodedCoefficients {
20 polynomials: [Vec<EncodedSurfaceTerm>; 4],
21}
22
23impl EncodedCoefficients {
24 pub fn polynomials(&self) -> &[Vec<EncodedSurfaceTerm>; 4] {
25 &self.polynomials
26 }
27}
28
29#[derive(Clone, Copy, Debug, PartialEq)]
30pub struct EncodedDomain {
31 endpoints: [[f32; 2]; 2],
32 closed: [[bool; 2]; 2],
33}
34
35impl EncodedDomain {
36 pub fn endpoints(self) -> [[f32; 2]; 2] {
37 self.endpoints
38 }
39 pub fn closed(self) -> [[bool; 2]; 2] {
40 self.closed
41 }
42}
43
44#[derive(Clone, Copy, Debug, PartialEq, Eq)]
45pub struct EncodedDiskConstraint;
46
47#[derive(Clone, Debug, PartialEq)]
48pub struct EncodedTrimRestriction {
49 outer: Vec<[f32; 2]>,
50 holes: Vec<Vec<[f32; 2]>>,
51}
52
53impl EncodedTrimRestriction {
54 pub fn outer(&self) -> &[[f32; 2]] {
55 &self.outer
56 }
57 pub fn holes(&self) -> &[Vec<[f32; 2]>] {
58 &self.holes
59 }
60}
61
62#[derive(Clone, Debug, PartialEq)]
63pub enum EncodedSurfacePatch {
64 Full {
65 coefficients: EncodedCoefficients,
66 domain: EncodedDomain,
67 disk: Option<EncodedDiskConstraint>,
68 orientation: SurfaceOrientation,
69 },
70 Trimmed {
71 coefficients: EncodedCoefficients,
72 domain: EncodedDomain,
73 restriction: EncodedTrimRestriction,
74 orientation: SurfaceOrientation,
75 },
76}
77
78pub fn encode_chart_patch(
79 patch: &ChartPatch,
80 precision: EncodingPrecision,
81) -> Result<(EncodedSurfacePatch, EncodingBounds), EncodingFailure> {
82 let mut encoder = ScalarEncoder::new(precision);
83 let source = patch.chart().surface();
84 let coefficients = EncodedCoefficients {
85 polynomials: [
86 encoder.polynomial(&source.x, 0)?,
87 encoder.polynomial(&source.y, 1)?,
88 encoder.polynomial(&source.z, 2)?,
89 encoder.polynomial(&source.denominator, 3)?,
90 ],
91 };
92 let mut endpoints = [[0.0; 2]; 2];
93 let mut closed = [[false; 2]; 2];
94 for (axis, interval) in [patch.domain().u(), patch.domain().v()]
95 .into_iter()
96 .enumerate()
97 {
98 for (endpoint, value) in [interval.lower(), interval.upper()].into_iter().enumerate() {
99 endpoints[axis][endpoint] = encoder.algebraic(
100 value,
101 EncodingLocation::DomainEndpoint {
102 axis,
103 upper: endpoint == 1,
104 },
105 )?;
106 }
107 closed[axis] = [interval.lower_closed(), interval.upper_closed()];
108 }
109 let domain = EncodedDomain { endpoints, closed };
110 let orientation = patch.chart().orientation();
111 let encoded = match patch {
112 ChartPatch::Full { disk, .. } => EncodedSurfacePatch::Full {
113 coefficients,
114 domain,
115 disk: disk.map(|_| EncodedDiskConstraint),
116 orientation,
117 },
118 ChartPatch::Trimmed { declaration } => {
119 let outer = encoder.trim_loop(declaration.outer(), None)?;
120 let mut holes = Vec::with_capacity(declaration.holes().len());
121 for (hole, source) in declaration.holes().iter().enumerate() {
122 holes.push(encoder.trim_loop(source, Some(hole))?);
123 }
124 EncodedSurfacePatch::Trimmed {
125 coefficients,
126 domain,
127 restriction: EncodedTrimRestriction { outer, holes },
128 orientation,
129 }
130 }
131 };
132 Ok((encoded, encoder.bounds()))
133}
134
135impl ScalarEncoder {
136 fn polynomial(
137 &mut self,
138 polynomial: &SurfacePolynomial,
139 index: usize,
140 ) -> Result<Vec<EncodedSurfaceTerm>, EncodingFailure> {
141 polynomial
142 .terms()
143 .iter()
144 .enumerate()
145 .map(|(term, source)| {
146 Ok(EncodedSurfaceTerm {
147 coefficient: self.rational(
148 &source.coefficient,
149 EncodingLocation::Coefficient {
150 polynomial: index,
151 term,
152 },
153 )?,
154 exponent_u: source.exponent_u,
155 exponent_v: source.exponent_v,
156 })
157 })
158 .collect()
159 }
160
161 fn trim_loop(
162 &mut self,
163 source: &SurfaceTrimLoop2,
164 hole: Option<usize>,
165 ) -> Result<Vec<[f32; 2]>, EncodingFailure> {
166 source
167 .vertices()
168 .iter()
169 .enumerate()
170 .map(|(vertex, point)| {
171 Ok([
172 self.algebraic(
173 point.u(),
174 EncodingLocation::TrimVertex {
175 hole,
176 vertex,
177 axis: 0,
178 },
179 )?,
180 self.algebraic(
181 point.v(),
182 EncodingLocation::TrimVertex {
183 hole,
184 vertex,
185 axis: 1,
186 },
187 )?,
188 ])
189 })
190 .collect()
191 }
192}