BREP_kernel 0.5.0

A boundary representation (BREP) geometry kernel for building CAD applications.
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
use super::*;

#[wasm_bindgen]
pub struct WasmBrepSolid {
    pub(crate) solid: BrepSolid,
}

#[wasm_bindgen]
impl WasmBrepSolid {
    #[wasm_bindgen(constructor)]
    pub fn new(solid_json: &str) -> Result<WasmBrepSolid, JsValue> {
        let solid: BrepSolid = serde_json::from_str(solid_json)
            .map_err(|error| javascript_error(error.to_string()))?;
        let solid = validate_imported_solid(solid, "WasmBrepSolid")?;
        Ok(Self { solid })
    }

    /// Construct from the flat f64 codec instead of JSON — same arena
    /// validation as the JSON constructor.
    pub fn from_buffer(data: &[f64], names_json: &str) -> Result<WasmBrepSolid, JsValue> {
        let solid = decode_solid_buffer(data, names_json)?;
        let solid = validate_imported_solid(solid, "WasmBrepSolid")?;
        Ok(Self { solid })
    }

    pub fn to_buffer(&self) -> Result<WasmSolidBuffer, JsValue> {
        solid_buffer(&self.solid, "{}".into())
    }

    pub fn to_json(&self) -> Result<String, JsValue> {
        serde_json::to_string(&self.solid).map_err(|error| javascript_error(error.to_string()))
    }

    pub fn validate_json(&self) -> Result<String, JsValue> {
        serde_json::to_string(&self.solid.validate())
            .map_err(|error| javascript_error(error.to_string()))
    }

    pub fn mass_properties_json(&self) -> Result<String, JsValue> {
        let properties = solid_mass_properties(&self.solid).map_err(javascript_error)?;
        serde_json::to_string(&properties).map_err(|error| javascript_error(error.to_string()))
    }

    pub fn full_mass_properties_json(&self) -> Result<String, JsValue> {
        let properties = solid_mass_properties_full(&self.solid).map_err(javascript_error)?;
        serde_json::to_string(&properties).map_err(|error| javascript_error(error.to_string()))
    }

    /// Full mass properties scaled to a physical `density` (Golovanov §8.11):
    /// `mass = density * volume`, inertia and the principal moments scale by
    /// `density`, while volume, surface area, centroid, and the principal axes
    /// are density-independent. Returns the `DensityMassProperties` JSON.
    pub fn full_mass_properties_with_density_json(&self, density: f64) -> Result<String, JsValue> {
        let properties = solid_mass_properties_full(&self.solid).map_err(javascript_error)?;
        serde_json::to_string(&properties.with_density(density))
            .map_err(|error| javascript_error(error.to_string()))
    }

    pub fn tessellate_json(
        &self,
        slabs_per_span_u: usize,
        steps_per_span_v: usize,
    ) -> Result<String, JsValue> {
        let mesh = tessellate_brep(
            &self.solid,
            TessellationOptions {
                slabs_per_span_u: slabs_per_span_u.max(1),
                steps_per_span_v: steps_per_span_v.max(1),
            },
        )
        .map_err(javascript_error)?;
        serde_json::to_string(&mesh).map_err(|error| javascript_error(error.to_string()))
    }

    pub fn transform_json(
        &self,
        matrix: &[f64],
        reverse_orientation: bool,
    ) -> Result<String, JsValue> {
        let matrix: [f64; 16] = matrix
            .try_into()
            .map_err(|_| javascript_error("transform matrix must contain 16 values".into()))?;
        let transform = AffineTransform::new(matrix).map_err(javascript_error)?;
        let solid = transform_brep(&self.solid, transform, reverse_orientation)
            .map_err(javascript_error)?;
        serde_json::to_string(&solid).map_err(|error| javascript_error(error.to_string()))
    }

    pub fn transform_buffer(
        &self,
        matrix: &[f64],
        reverse_orientation: bool,
    ) -> Result<WasmSolidBuffer, JsValue> {
        let matrix: [f64; 16] = matrix
            .try_into()
            .map_err(|_| javascript_error("transform matrix must contain 16 values".into()))?;
        let transform = AffineTransform::new(matrix).map_err(javascript_error)?;
        let solid = transform_brep(&self.solid, transform, reverse_orientation)
            .map_err(javascript_error)?;
        solid_buffer(&solid, "{}".into())
    }

    pub fn boolean_buffer(
        &self,
        other: &WasmBrepSolid,
        operation: &str,
        tolerance: f64,
        merge_coplanar_faces: bool,
    ) -> Result<WasmSolidBuffer, JsValue> {
        let operation = match operation {
            "union" => BooleanOperation::Union,
            "intersect" => BooleanOperation::Intersect,
            "subtract" => BooleanOperation::Subtract,
            _ => return Err(javascript_error("unknown boolean operation".into())),
        };
        let solid = boolean_operation(
            &self.solid,
            &other.solid,
            operation,
            &BooleanOptions {
                tolerance,
                merge_coplanar_faces,
                ..BooleanOptions::default()
            },
        )
        .map_err(javascript_refusal)?;
        solid_buffer(&solid, "{}".into())
    }

    pub fn offset_shell_buffer(
        &self,
        opening_face_ids: &[f64],
        distance: f64,
    ) -> Result<WasmSolidBuffer, JsValue> {
        crate::panic_hook::set_once();
        let face_ids = opening_face_ids
            .iter()
            .map(|id| {
                if !id.is_finite() || *id < 0.0 || id.fract() != 0.0 {
                    Err(javascript_error("opening face IDs must be integers".into()))
                } else {
                    Ok(*id as u64)
                }
            })
            .collect::<Result<Vec<_>, _>>()?;
        let result = offset_shell(&self.solid, &face_ids, distance).map_err(javascript_error)?;
        let metadata = serde_json::json!({ "face_images": result.face_images });
        solid_buffer(&result.solid, metadata.to_string())
    }

    pub fn tessellate_watertight_buffers(
        &self,
        chord_tolerance: f64,
    ) -> Result<WasmMeshBuffers, JsValue> {
        let mesh =
            tessellate_brep_watertight(&self.solid, chord_tolerance).map_err(javascript_error)?;
        Ok(WasmMeshBuffers {
            positions: mesh.positions,
            normals: mesh.normals,
            indices: mesh.indices,
            face_ids: mesh.face_ids,
        })
    }

    /// Tessellate only the faces whose global sequential index `i` satisfies
    /// `i % stride == offset` (a face subset), so a heavy solid can be meshed
    /// in parallel across worker instances (dispatch `stride` calls, offsets
    /// 0..stride-1, concatenate the fragments on the caller side). No
    /// closed-shell validation — a fragment is not a closed mesh; the caller
    /// validates the concatenated whole. See
    /// `tessellate_brep_watertight_face_stride`.
    pub fn tessellate_watertight_face_stride_buffers(
        &self,
        chord_tolerance: f64,
        stride: usize,
        offset: usize,
    ) -> Result<WasmMeshBuffers, JsValue> {
        let mesh =
            tessellate_brep_watertight_face_stride(&self.solid, chord_tolerance, stride, offset)
                .map_err(javascript_error)?;
        Ok(WasmMeshBuffers {
            positions: mesh.positions,
            normals: mesh.normals,
            indices: mesh.indices,
            face_ids: mesh.face_ids,
        })
    }

    /// Compute + serialize every edge's shared samples once, so the face-range
    /// split can hand the same buffer to each worker instead of recomputing
    /// `sample_all_edges` in all of them. See `sample_edges_encoded`.
    pub fn sample_edges_buffer(&self, chord_tolerance: f64) -> Result<Vec<f64>, JsValue> {
        sample_edges_encoded(&self.solid, chord_tolerance).map_err(javascript_error)
    }

    /// Face-stride tessellation using pre-computed edge samples (from
    /// `sample_edges_buffer`), skipping the redundant per-worker resampling.
    pub fn tessellate_watertight_face_stride_with_samples_buffers(
        &self,
        chord_tolerance: f64,
        stride: usize,
        offset: usize,
        samples: &[f64],
    ) -> Result<WasmMeshBuffers, JsValue> {
        let mesh = tessellate_brep_watertight_face_stride_with_samples(
            &self.solid,
            chord_tolerance,
            stride,
            offset,
            samples,
        )
        .map_err(javascript_error)?;
        Ok(WasmMeshBuffers {
            positions: mesh.positions,
            normals: mesh.normals,
            indices: mesh.indices,
            face_ids: mesh.face_ids,
        })
    }

    pub fn tessellate_buffers(
        &self,
        slabs_per_span_u: usize,
        steps_per_span_v: usize,
    ) -> Result<WasmMeshBuffers, JsValue> {
        let mesh = tessellate_brep(
            &self.solid,
            TessellationOptions {
                slabs_per_span_u: slabs_per_span_u.max(1),
                steps_per_span_v: steps_per_span_v.max(1),
            },
        )
        .map_err(javascript_error)?;
        Ok(WasmMeshBuffers {
            positions: mesh.positions,
            normals: mesh.normals,
            indices: mesh.indices,
            face_ids: mesh.face_ids,
        })
    }

    pub fn boolean_json(
        &self,
        other: &WasmBrepSolid,
        operation: &str,
        tolerance: f64,
        merge_coplanar_faces: bool,
    ) -> Result<String, JsValue> {
        let operation = match operation {
            "union" => BooleanOperation::Union,
            "intersect" => BooleanOperation::Intersect,
            "subtract" => BooleanOperation::Subtract,
            _ => return Err(javascript_error("unknown boolean operation".into())),
        };
        let solid = boolean_operation(
            &self.solid,
            &other.solid,
            operation,
            &BooleanOptions {
                tolerance,
                merge_coplanar_faces,
                ..BooleanOptions::default()
            },
        )
        .map_err(javascript_refusal)?;
        serde_json::to_string(&solid).map_err(|error| javascript_error(error.to_string()))
    }

    pub fn offset_shell_json(
        &self,
        opening_face_ids: &[f64],
        distance: f64,
    ) -> Result<String, JsValue> {
        crate::panic_hook::set_once();
        let face_ids = opening_face_ids
            .iter()
            .map(|id| {
                if !id.is_finite() || *id < 0.0 || id.fract() != 0.0 {
                    Err(javascript_error("opening face IDs must be integers".into()))
                } else {
                    Ok(*id as u64)
                }
            })
            .collect::<Result<Vec<_>, _>>()?;
        let result = offset_shell(&self.solid, &face_ids, distance).map_err(javascript_error)?;
        serde_json::to_string(&result).map_err(|error| javascript_error(error.to_string()))
    }

    /// Golovanov §6.12 direct editing — delete a transition face (chamfer /
    /// fillet / simple 4-sided face) and heal the hole by extending and
    /// re-intersecting its neighbours. `face_id` is the wire id of the face to
    /// remove.
    pub fn delete_face_and_heal_buffer(&self, face_id: f64) -> Result<WasmSolidBuffer, JsValue> {
        crate::panic_hook::set_once();
        if !face_id.is_finite() || face_id < 0.0 || face_id.fract() != 0.0 {
            return Err(javascript_error(
                "delete_face_and_heal: face id must be a non-negative integer".into(),
            ));
        }
        let solid = delete_face_and_heal(&self.solid, face_id as u64).map_err(javascript_error)?;
        solid_buffer(&solid, "{}".into())
    }

    pub fn delete_face_and_heal_json(&self, face_id: f64) -> Result<String, JsValue> {
        crate::panic_hook::set_once();
        if !face_id.is_finite() || face_id < 0.0 || face_id.fract() != 0.0 {
            return Err(javascript_error(
                "delete_face_and_heal: face id must be a non-negative integer".into(),
            ));
        }
        let solid = delete_face_and_heal(&self.solid, face_id as u64).map_err(javascript_error)?;
        serde_json::to_string(&solid).map_err(|error| javascript_error(error.to_string()))
    }

    /// Golovanov §6.12 direct editing — translate a face group rigidly by
    /// (dx, dy, dz) and heal the adjacency by re-intersecting the planar
    /// neighbours. Mirrors `delete_face_and_heal_json`; `face_ids` follows
    /// `offset_shell_json`'s integer-id validation.
    pub fn move_faces_json(
        &self,
        face_ids: &[f64],
        dx: f64,
        dy: f64,
        dz: f64,
    ) -> Result<String, JsValue> {
        crate::panic_hook::set_once();
        let face_ids = face_ids
            .iter()
            .map(|id| {
                if !id.is_finite() || *id < 0.0 || id.fract() != 0.0 {
                    Err(javascript_error(
                        "move_faces: face ids must be non-negative integers".into(),
                    ))
                } else {
                    Ok(*id as u64)
                }
            })
            .collect::<Result<Vec<_>, _>>()?;
        let solid =
            move_faces(&self.solid, &face_ids, Vec3::new(dx, dy, dz)).map_err(javascript_error)?;
        serde_json::to_string(&solid).map_err(|error| javascript_error(error.to_string()))
    }

    pub fn export_step(&self, name: &str, unit: &str, timestamp: &str) -> Result<String, JsValue> {
        export_step(std::slice::from_ref(&self.solid), name, unit, timestamp)
            .map_err(javascript_error)
    }

    /// Standalone heal/sew: pair coincident one-use boundary edges (open
    /// chains and closed rims), merge the shells they join, and re-orient the
    /// result outward. Best-effort — unsewable gaps stay open and are counted
    /// in the returned report. Returns `{ "solid": …, "report": … }`.
    pub fn sew_json(&self, tolerance: f64) -> Result<String, JsValue> {
        crate::panic_hook::set_once();
        let tolerance = if tolerance.is_finite() && tolerance > 0.0 {
            tolerance
        } else {
            1e-4
        };
        let (solid, report) = sew_solid(&self.solid, tolerance).map_err(javascript_error)?;
        serde_json::to_string(&serde_json::json!({ "solid": solid, "report": report }))
            .map_err(|error| javascript_error(error.to_string()))
    }
}

#[wasm_bindgen]
impl WasmNurbsSurface {
    #[wasm_bindgen(constructor)]
    pub fn new(surface_json: &str) -> Result<WasmNurbsSurface, JsValue> {
        let serialized: NurbsSurface = serde_json::from_str(surface_json)
            .map_err(|error| javascript_error(error.to_string()))?;
        let surface = NurbsSurface::new(
            serialized.degree_u,
            serialized.degree_v,
            serialized.knots_u,
            serialized.knots_v,
            serialized.control_points,
        )
        .map_err(javascript_error)?;
        Ok(Self { surface })
    }

    pub fn evaluate_many(&self, parameters_uv: &[f64]) -> Result<Vec<f64>, JsValue> {
        if parameters_uv.len() % 2 != 0 {
            return Err(javascript_error(
                "surface parameter buffer must contain u,v pairs".into(),
            ));
        }
        let mut points = Vec::with_capacity(parameters_uv.len() / 2 * 3);
        for pair in parameters_uv.chunks_exact(2) {
            let point = self
                .surface
                .evaluate(pair[0], pair[1])
                .map_err(javascript_error)?;
            points.extend([point.x, point.y, point.z]);
        }
        Ok(points)
    }

    pub fn normals_many(&self, parameters_uv: &[f64]) -> Result<Vec<f64>, JsValue> {
        if parameters_uv.len() % 2 != 0 {
            return Err(javascript_error(
                "surface parameter buffer must contain u,v pairs".into(),
            ));
        }
        let mut normals = Vec::with_capacity(parameters_uv.len() / 2 * 3);
        for pair in parameters_uv.chunks_exact(2) {
            let normal = self
                .surface
                .normal(pair[0], pair[1])
                .map_err(javascript_error)?;
            normals.extend([normal.x, normal.y, normal.z]);
        }
        Ok(normals)
    }

    pub fn derivatives_flat(
        &self,
        u: f64,
        v: f64,
        derivative_count: usize,
    ) -> Result<Vec<f64>, JsValue> {
        let derivatives = self
            .surface
            .derivatives(u, v, derivative_count)
            .map_err(javascript_error)?;
        let side = derivative_count + 1;
        let mut flat = vec![0.0; side * side * 3];
        for k in 0..side {
            for l in 0..side {
                let offset = (k * side + l) * 3;
                flat[offset] = derivatives[k][l].x;
                flat[offset + 1] = derivatives[k][l].y;
                flat[offset + 2] = derivatives[k][l].z;
            }
        }
        Ok(flat)
    }
}

#[wasm_bindgen]
impl WasmNurbsCurve {
    #[wasm_bindgen(constructor)]
    pub fn new(curve_json: &str) -> Result<WasmNurbsCurve, JsValue> {
        let serialized: NurbsCurve = serde_json::from_str(curve_json)
            .map_err(|error| javascript_error(error.to_string()))?;
        let curve = NurbsCurve::new(
            serialized.degree,
            serialized.knots,
            serialized.control_points,
        )
        .map_err(javascript_error)?;
        Ok(Self { curve })
    }

    pub fn evaluate_many(&self, parameters: &[f64]) -> Result<Vec<f64>, JsValue> {
        let mut points = Vec::with_capacity(parameters.len() * 3);
        for parameter in parameters {
            let point = self.curve.evaluate(*parameter).map_err(javascript_error)?;
            points.extend([point.x, point.y, point.z]);
        }
        Ok(points)
    }

    pub fn derivatives_flat(
        &self,
        parameter: f64,
        derivative_count: usize,
    ) -> Result<Vec<f64>, JsValue> {
        let derivatives = self
            .curve
            .derivatives(parameter, derivative_count)
            .map_err(javascript_error)?;
        let mut result = Vec::with_capacity(derivatives.len() * 3);
        for derivative in derivatives {
            result.extend([derivative.x, derivative.y, derivative.z]);
        }
        Ok(result)
    }
}