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
//! Allocation-free declarations for heterogeneous primitive batches.
use super::{AnisotropicEllipsoid, CarbohydrateSymbol, Particle, PlanarRegion, Primitive};
use crate::{CoreError, StructureHandle};
use molgfx_math::Rgba8;
impl Primitive {
/// Declares one analytic displacement ellipsoid for later batch insertion.
///
/// # Errors
///
/// Returns [`CoreError::InvalidPrimitive`] for malformed opacity.
pub fn ellipsoid(
owner: StructureHandle,
value: AnisotropicEllipsoid,
color: Rgba8,
opacity: f32,
) -> Result<Self, CoreError> {
validate_opacity(opacity)?;
Ok(Self::Ellipsoid {
owner,
value,
color,
opacity,
visible: true,
})
}
/// Declares one caller-resolved carbohydrate symbol.
#[must_use]
pub const fn carbohydrate(value: CarbohydrateSymbol) -> Self {
Self::Carbohydrate(value)
}
/// Declares one filled analytic planar region for batch insertion.
///
/// # Errors
///
/// Returns [`CoreError::InvalidPrimitive`] for malformed opacity.
pub fn planar(value: PlanarRegion, color: Rgba8, opacity: f32) -> Result<Self, CoreError> {
validate_opacity(opacity)?;
Ok(Self::Planar {
value,
color,
opacity,
visible: true,
})
}
/// Declares one validated particle for batch insertion.
#[must_use]
pub const fn particle(value: Particle) -> Self {
Self::Particle(value)
}
}
fn validate_opacity(opacity: f32) -> Result<(), CoreError> {
if opacity.is_finite() && (0.0..=1.0).contains(&opacity) {
Ok(())
} else {
Err(CoreError::InvalidPrimitive {
reason: "primitive opacity must be finite in [0, 1]",
})
}
}