imgal 0.3.0

A fast and open-source scientific image processing and algorithm library.
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
use ndarray::{Array1, ArrayBase, AsArray, Ix1, Ix2, ViewRepr};
use rayon::prelude::*;

use crate::prelude::*;

/// Determine if a query point is inside a polyhedron.
///
/// # Description
///
/// Determines if a 3D query point is inside the given polyhedron's interior.
/// Each face of the polyhedron is used to form a tetrahedron with the `center`
/// point. The query point is considered inside the polyhedron if it is inside
/// one of the constituent tetrahedra. This function expects points in
/// `(pln, row, col)` order.
///
/// # Arguments
///
/// * `vertices`: The hull vertices with `(n_points, 3)` shape.
/// * `faces`: The hull faces with `(n_triangle, 3)` shape.
/// * `center`: The center point of the polyhedron.
/// * `query`: The query point to check if inside the polyhedron.
/// * `threads`: The requested number of threads to use for parallel execution.
///   If `None` or `Some(1)` sequential execution is used. If `Some(0)`, then
///   the maximum available parallelism is used. Thread counts are clamped to
///   the systems maximum.
///
/// # Returns
///
/// * `Ok(bool)`: Returns `true` if `query` is inside the polyhedron, otherwise
///   it returns `false`.
/// * `Err(ImgalError)`: If `vertices` and/or `faces` is empty. If `vertices`
///   and/or `faces` axis 1 `!= 3`. If `center` or `query` length does not equal
///   `3`.
#[inline]
pub fn inside_polyhedron<'a, T, A, B, C>(
    vertices: A,
    faces: B,
    center: C,
    query: C,
    threads: Option<usize>,
) -> Result<bool, ImgalError>
where
    A: AsArray<'a, T, Ix2>,
    B: AsArray<'a, usize, Ix2>,
    C: AsArray<'a, T, Ix1>,
    T: 'a + AsNumeric,
{
    let vertices: ArrayBase<ViewRepr<&'a T>, Ix2> = vertices.into();
    let faces: ArrayBase<ViewRepr<&'a usize>, Ix2> = faces.into();
    let center: ArrayBase<ViewRepr<&'a T>, Ix1> = center.into();
    let query: ArrayBase<ViewRepr<&'a T>, Ix1> = query.into();
    if vertices.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray {
            param_name: "vertices",
        });
    }
    if vertices.dim().1 != 3 {
        return Err(ImgalError::InvalidAxisLengthExpected {
            arr_name: "vertices",
            axis_idx: 1,
            expected: 3,
            got: vertices.dim().1,
        });
    }
    if faces.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray {
            param_name: "faces",
        });
    }
    if faces.dim().1 != 3 {
        return Err(ImgalError::InvalidAxisLengthExpected {
            arr_name: "faces",
            axis_idx: 1,
            expected: 3,
            got: faces.dim().1,
        });
    }
    if center.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray {
            param_name: "center",
        });
    }
    if center.len() != 3 {
        return Err(ImgalError::InvalidArrayLengthExpected {
            arr_name: "center",
            expected: 3,
            got: center.len(),
        });
    }
    if query.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray {
            param_name: "query",
        });
    }
    if query.len() != 3 {
        return Err(ImgalError::InvalidArrayLengthExpected {
            arr_name: "query",
            expected: 3,
            got: query.len(),
        });
    }
    let tetrahedron_check = |i: usize| {
        let [a_idx, b_idx, c_idx] = [faces[[i, 0]], faces[[i, 1]], faces[[i, 2]]];
        let a = vertices.row(a_idx);
        let b = vertices.row(b_idx);
        let c = vertices.row(c_idx);
        // SAFE: this unwrap is safe because we validated the inputs already
        inside_tetrahedron(a, b, c, center, query).unwrap()
    };
    Ok(par!(threads,
        seq_exp: (0..faces.dim().0).any(tetrahedron_check),
        par_exp: (0..faces.dim().0).into_par_iter().any(tetrahedron_check)))
}

/// Determine if a query point is inside a tetrahedron.
///
/// # Description
///
/// Determines if a 3D query point is inside the given tetrahedron's interior.
/// The query point is considered inside the tetrahedron if the point is found
/// in the interior halfspace of each face. The function expects points and
/// vertices in `(pln, row, col)` order.
///
/// # Arguments
///
/// * `a`: Vertex `a` of the oriented plane.
/// * `b`: Vertex `b` of the oriented plane.
/// * `c`: Vertex `c` of the oriented plane.
/// * `d`: The reference point relative to plane `(a, b, c)`.
/// * `query`: The query point to check if inside the polyhedron.
///
/// # Returns
///
/// * `Ok(bool)`: Returns `true` if `query` is inside the tetrahedron, otherwise
///   it returns `false`.
/// * `Err(ImgalError)`: If points `a`, `b`, `c`, `d`, and `query` are empty or
///   do not have length equal to `3`.
#[inline]
pub fn inside_tetrahedron<'a, T, A>(a: A, b: A, c: A, d: A, query: A) -> Result<bool, ImgalError>
where
    A: AsArray<'a, T, Ix1>,
    T: 'a + AsNumeric,
{
    let a: ArrayBase<ViewRepr<&'a T>, Ix1> = a.into();
    let b: ArrayBase<ViewRepr<&'a T>, Ix1> = b.into();
    let c: ArrayBase<ViewRepr<&'a T>, Ix1> = c.into();
    let d: ArrayBase<ViewRepr<&'a T>, Ix1> = d.into();
    let query: ArrayBase<ViewRepr<&'a T>, Ix1> = query.into();
    let orient_abc = orient_pred_3d(a, b, c, query)?.is_sign_negative();
    let orient_dba = orient_pred_3d(d, b, a, query)?.is_sign_negative();
    let orient_dcb = orient_pred_3d(d, c, b, query)?.is_sign_negative();
    let orient_dac = orient_pred_3d(d, a, c, query)?.is_sign_negative();
    Ok(orient_abc && orient_dba && orient_dcb && orient_dac)
}

/// Compute the 2D orientation predicate of a triangle.
///
/// # Description
///
/// Computes the 2D orientation predicate of triangle `(o, a, b)` in
/// `(row, col)` dimension order. The sign of the returned value indicates
/// on which side of the oriented line `(o, a)` the point `b` lies:
///
/// - *Positive*: Point `b` lies to the left of the directed line `o -> a`
///   (counterclockwise turn).
/// - *Negative*: Point `b` lies to the right of the directed line `o -> a`
///   (clockwise turn).
/// - *Zero*: Points `o`, `a`, and `b` are collinear.
///
/// # Arguments
///
/// * `o`: The origin vertex of the directed line.
/// * `a`: The endpoint vertex of the directed line.
/// * `b`: The reference point relative to line `(o, a)`.
///
/// # Returns
///
/// * `Ok(f64)`: The orientation of the triangle.
/// * `Err(ImgalError)`: If points `o`, `a`, or `b` are empty or do not have
///   length equal to `3`.
///
/// # Reference
///
/// <https://www.cs.cmu.edu/afs/cs/project/quake/public/code/predicates.c>\
/// <https://doi.org/10.1007/PL00009321>
#[inline]
pub fn orient_pred_2d<'a, T, A>(o: A, a: A, b: A) -> Result<f64, ImgalError>
where
    A: AsArray<'a, T, Ix1>,
    T: 'a + AsNumeric,
{
    let o: ArrayBase<ViewRepr<&'a T>, Ix1> = o.into();
    let a: ArrayBase<ViewRepr<&'a T>, Ix1> = a.into();
    let b: ArrayBase<ViewRepr<&'a T>, Ix1> = b.into();
    if o.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray { param_name: "o" });
    }
    if o.len() != 2 {
        return Err(ImgalError::InvalidArrayLengthExpected {
            arr_name: "o",
            expected: 2,
            got: o.len(),
        });
    }
    if a.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray { param_name: "a" });
    }
    if a.len() != 2 {
        return Err(ImgalError::InvalidArrayLengthExpected {
            arr_name: "a",
            expected: 2,
            got: a.len(),
        });
    }
    if b.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray { param_name: "b" });
    }
    if b.len() != 2 {
        return Err(ImgalError::InvalidArrayLengthExpected {
            arr_name: "b",
            expected: 2,
            got: b.len(),
        });
    }
    let [oy, ox] = [o[0].to_f64(), o[1].to_f64()];
    let [ay, ax] = [a[0].to_f64(), a[1].to_f64()];
    let [by, bx] = [b[0].to_f64(), b[1].to_f64()];
    Ok((ax - ox) * (by - oy) - (ay - oy) * (bx - ox))
}

/// Compute the 3D orientation predicate of a tetrahedron.
///
/// # Description
///
/// Computes the 3D orientation prpedicate of tetrahedrion `(a, b, c, d)` in
/// `(pln, row, col)` dimension order. The sign of the returned value indicates
/// on which side of the oriented plane `(a, b, c)` the point `d` lies:
///
/// - *Positive*: Point `d` lies above the plane where `(a, b, c)` appears in
///   counterclockwise order.
/// - *Negative*: Point `d` lies below the plane where `(a, b, c)` appears in
///   clockwise order.
/// - *Zero*: Point `d` is coplanar with plane `(a, b, c)`.
///
/// # Arguments
///
/// * `a`: Vertex `a` of the oriented plane.
/// * `b`: Vertex `b` of the oriented plane.
/// * `c`: Vertex `c` of the oriented plane.
/// * `d`: The reference point relative to plane `(a, b, c)`.
///
/// # Returns
///
/// * `Ok(f64)`: The orientation of the tetrahedron.
/// * `Err(ImgalError)`: If points `a`, `b`, `c` and `d` are empty or do not
///   have length equal to `3`.
///
/// # Reference
///
/// <https://www.cs.cmu.edu/afs/cs/project/quake/public/code/predicates.c>\
/// <https://doi.org/10.1007/PL00009321>
#[inline]
pub fn orient_pred_3d<'a, T, A>(a: A, b: A, c: A, d: A) -> Result<f64, ImgalError>
where
    A: AsArray<'a, T, Ix1>,
    T: 'a + AsNumeric,
{
    let a: ArrayBase<ViewRepr<&'a T>, Ix1> = a.into();
    let b: ArrayBase<ViewRepr<&'a T>, Ix1> = b.into();
    let c: ArrayBase<ViewRepr<&'a T>, Ix1> = c.into();
    let d: ArrayBase<ViewRepr<&'a T>, Ix1> = d.into();
    if a.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray { param_name: "a" });
    }
    if a.len() != 3 {
        return Err(ImgalError::InvalidArrayLengthExpected {
            arr_name: "a",
            expected: 3,
            got: a.len(),
        });
    }
    if b.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray { param_name: "b" });
    }
    if b.len() != 3 {
        return Err(ImgalError::InvalidArrayLengthExpected {
            arr_name: "b",
            expected: 3,
            got: b.len(),
        });
    }
    if c.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray { param_name: "c" });
    }
    if c.len() != 3 {
        return Err(ImgalError::InvalidArrayLengthExpected {
            arr_name: "c",
            expected: 3,
            got: c.len(),
        });
    }
    if d.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray { param_name: "d" });
    }
    if d.len() != 3 {
        return Err(ImgalError::InvalidArrayLengthExpected {
            arr_name: "d",
            expected: 3,
            got: d.len(),
        });
    }
    let [az, ay, ax] = [a[0].to_f64(), a[1].to_f64(), a[2].to_f64()];
    let [bz, by, bx] = [b[0].to_f64(), b[1].to_f64(), b[2].to_f64()];
    let [cz, cy, cx] = [c[0].to_f64(), c[1].to_f64(), c[2].to_f64()];
    let [dz, dy, dx] = [d[0].to_f64(), d[1].to_f64(), d[2].to_f64()];
    let [adx, ady, adz] = [ax - dx, ay - dy, az - dz];
    let [bdx, bdy, bdz] = [bx - dx, by - dy, bz - dz];
    let [cdx, cdy, cdz] = [cx - dx, cy - dy, cz - dz];
    Ok(
        adx * (bdy * cdz - bdz * cdy) - ady * (bdx * cdz - bdz * cdx)
            + adz * (bdx * cdy - bdy * cdx),
    )
}

/// Compute the volume of a polyhedron.
///
/// # Description
///
/// Computes the volume of a closed polyhedron defined by `vertices` and
/// `faces`. Each face is turned into a tetrahedron with the `apex` point and
/// their signed volumes summed. The function expects the polyhedron (*i.e.*
/// hull) to have outward-facing normals. This function expects points in
/// `(pln, row, col)` order.
///
/// # Arguments
///
/// * `vertices`: The polyhedron (hull) vertices with `(n_points, 3)` shape.
/// * `faces`: The polyhedron (hull) faces with `(n_triangle, 3)` shape.
/// * `apex`: The shared apex point of all tetrahedra. If `None`, then
///   `[0, 0, 0]` is used. Using a vertex of the hull can improve floating-point
///   accuracy if the hull is far from the origin.
/// * `threads`: The requested number of threads to use for parallel execution.
///   If `None` or `Some(1)` sequential execution is used. If `Some(0)`, then
///   the maximum available parallelism is used. Thread counts are clamped to
///   the systems maximum.
///
/// # Returns
///
/// * `Ok(f64)`: The volume of the polyhedron.
/// * `Err(ImgalError)`: If `vertices` and/or `faces` is empty. If `vertices`
///   and/or `faces` axis 1 `!= 3`.
#[inline]
pub fn polyhedron_volume<'a, T, A, B, C>(
    vertices: A,
    faces: B,
    apex: Option<C>,
    threads: Option<usize>,
) -> Result<f64, ImgalError>
where
    A: AsArray<'a, T, Ix2>,
    B: AsArray<'a, usize, Ix2>,
    C: AsArray<'a, T, Ix1>,
    T: 'a + AsNumeric,
{
    let vertices: ArrayBase<ViewRepr<&'a T>, Ix2> = vertices.into();
    let faces: ArrayBase<ViewRepr<&'a usize>, Ix2> = faces.into();
    if vertices.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray {
            param_name: "vertices",
        });
    }
    if vertices.dim().1 != 3 {
        return Err(ImgalError::InvalidAxisLengthExpected {
            arr_name: "vertices",
            axis_idx: 1,
            expected: 3,
            got: vertices.dim().1,
        });
    }
    if faces.is_empty() {
        return Err(ImgalError::InvalidParameterEmptyArray {
            param_name: "faces",
        });
    }
    if faces.dim().1 != 3 {
        return Err(ImgalError::InvalidAxisLengthExpected {
            arr_name: "faces",
            axis_idx: 1,
            expected: 3,
            got: faces.dim().1,
        });
    }
    let apex = match apex {
        Some(ap) => ap.into().to_owned(),
        None => Array1::from_vec(vec![T::default(); 3]),
    };
    let polyhedron_vol_calc = |acc: f64, i: usize| {
        let [a_idx, b_idx, c_idx] = [faces[[i, 0]], faces[[i, 1]], faces[[i, 2]]];
        // SAFE: this unwrap is safe because we validated the inputs already
        acc + tetrahedron_volume(
            vertices.row(a_idx),
            vertices.row(b_idx),
            vertices.row(c_idx),
            apex.view(),
        )
        .unwrap()
    };
    Ok(par!(threads,
        seq_exp: (0..faces.dim().0).fold(0.0_f64, polyhedron_vol_calc).abs(),
        par_exp: (0..faces.dim().0).into_par_iter().fold(|| 0.0_f64, polyhedron_vol_calc)
            .reduce(|| 0.0_f64, |a, b| a + b)
            .abs()))
}

/// Compute the signed volume of a tetrahedron.
///
/// # Description
///
/// Computes the signed volume of tetrahedron `(a, b, c, d)` in
/// `(pln, row, col)` dimension order. The sign of the returned value indicates
/// on which side of the oriented plane `(a, b, c)` the point `d` lies:
///
/// - *Positive*: Point `d` lies above the plane where `(a, b, c)` appears in
///   counterclockwise order.
/// - *Negative*: Point `d` lies below the plane where `(a, b, c)` appears in
///   clockwise order.
/// - *Zero*: Point `d` is coplanar with plane `(a, b, c)`.
///
/// # Arguments
///
/// * `a`: Vertex `a` of the oriented plane.
/// * `b`: Vertex `b` of the oriented plane.
/// * `c`: Vertex `c` of the oriented plane.
/// * `d`: The reference point relative to plane `(a, b, c)`.
///
/// # Returns
///
/// * `Ok(f64)`: The signed volume of the tetrahedron. Negative signs have
///   volumes pointing towards `d` and positive signs have volumes pointing
///   away.
/// * `Err(ImgalError)`: If points `a`, `b`, `c` and `d` are empty or do not
///   have length equal to `3`.
///
/// # Reference
///
/// <https://www.cs.cmu.edu/afs/cs/project/quake/public/code/predicates.c>\
/// <https://doi.org/10.1007/PL00009321>
#[inline]
pub fn tetrahedron_volume<'a, T, A>(a: A, b: A, c: A, d: A) -> Result<f64, ImgalError>
where
    A: AsArray<'a, T, Ix1>,
    T: 'a + AsNumeric,
{
    Ok(orient_pred_3d(a, b, c, d)? / 6.0)
}