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
use crate::error::Error;
use nalgebra::{Isometry3, Point3, Rotation3, Scale3, Transform3, Vector3};
use std::fmt;
/// A 3-D coordinate triple in a coordinate reference system (CRS).
///
/// Corresponds to `gml:DirectPositionType` in ISO 19136. All three components
/// must be finite; `NaN` and ±infinity are rejected at construction time.
///
/// # Invariant
///
/// `x`, `y`, and `z` are always finite `f64` values.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct DirectPosition {
x: f64,
y: f64,
z: f64,
}
impl DirectPosition {
/// Creates a new position from Cartesian coordinates.
///
/// # Errors
///
/// Returns [`Error::NonFiniteCoordinate`] if any coordinate is NaN or infinite.
///
/// # Examples
///
/// ```rust
/// use egml_core::model::geometry::DirectPosition;
///
/// let pos = DirectPosition::new(1.0, 2.0, 3.0).unwrap();
/// assert_eq!(pos.x(), 1.0);
/// assert!(DirectPosition::new(f64::NAN, 0.0, 0.0).is_err());
/// ```
pub fn new(x: f64, y: f64, z: f64) -> Result<Self, Error> {
if !x.is_finite() {
return Err(Error::NonFiniteCoordinate {
axis: "x",
value: x,
});
}
if !y.is_finite() {
return Err(Error::NonFiniteCoordinate {
axis: "y",
value: y,
});
}
if !z.is_finite() {
return Err(Error::NonFiniteCoordinate {
axis: "z",
value: z,
});
}
Ok(Self { x, y, z })
}
pub fn new_unchecked(x: f64, y: f64, z: f64) -> Self {
Self { x, y, z }
}
/// Returns the X coordinate.
pub fn x(&self) -> f64 {
self.x
}
/// Returns the Y coordinate.
pub fn y(&self) -> f64 {
self.y
}
/// Returns the Z coordinate.
pub fn z(&self) -> f64 {
self.z
}
/// Returns the coordinates as a `[x, y, z]` array.
pub fn coords(&self) -> [f64; 3] {
[self.x, self.y, self.z]
}
/// Sets the X coordinate.
///
/// # Errors
///
/// Returns [`Error::NonFiniteCoordinate`] with the name `"x"` if `val` is NaN or infinite.
pub fn set_x(&mut self, val: f64) -> Result<(), Error> {
if !val.is_finite() {
return Err(Error::NonFiniteCoordinate {
axis: "x",
value: val,
});
}
self.x = val;
Ok(())
}
/// Sets the Y coordinate.
///
/// # Errors
///
/// Returns [`Error::NonFiniteCoordinate`] with the name `"y"` if `val` is NaN or infinite.
pub fn set_y(&mut self, val: f64) -> Result<(), Error> {
if !val.is_finite() {
return Err(Error::NonFiniteCoordinate {
axis: "y",
value: val,
});
}
self.y = val;
Ok(())
}
/// Sets the Z coordinate.
///
/// # Errors
///
/// Returns [`Error::NonFiniteCoordinate`] with the name `"z"` if `val` is NaN or infinite.
pub fn set_z(&mut self, val: f64) -> Result<(), Error> {
if !val.is_finite() {
return Err(Error::NonFiniteCoordinate {
axis: "z",
value: val,
});
}
self.z = val;
Ok(())
}
/// Returns a single-element list containing a reference to `self`.
///
/// This method exists so that `DirectPosition` satisfies the same
/// point-iteration pattern used by multi-point geometry types.
pub fn points(&self) -> Vec<&DirectPosition> {
vec![self]
}
}
impl DirectPosition {
/// Applies a rigid-body transform (rotation + translation) to this position in place.
///
/// `m` is a [`nalgebra::Isometry3`] — a combination of a rotation and a translation
/// that preserves distances and angles.
pub fn apply_transform(&mut self, transform: Transform3<f64>) {
let p: Point3<f64> = transform * Point3::new(self.x, self.y, self.z);
self.x = p.x;
self.y = p.y;
self.z = p.z;
}
/// Applies a rigid-body transform (rotation + translation) to this position in place.
///
/// Fast path: rotates and translates directly via [`nalgebra::Isometry3`] instead of
/// going through a full homogeneous [`Transform3`] multiply.
pub fn apply_isometry(&mut self, isometry: Isometry3<f64>) {
let p: Point3<f64> = isometry * Point3::new(self.x, self.y, self.z);
self.x = p.x;
self.y = p.y;
self.z = p.z;
}
/// Applies a pure translation to this position in place.
///
/// Fast path: a plain component-wise add, with no rotation or matrix math at all.
pub fn apply_translation(&mut self, vector: Vector3<f64>) {
self.x += vector.x;
self.y += vector.y;
self.z += vector.z;
}
/// Applies a pure rotation (about the origin) to this position in place.
///
/// Fast path: rotates directly via [`nalgebra::Rotation3`] instead of going through a
/// full homogeneous [`Transform3`] multiply.
pub fn apply_rotation(&mut self, rotation: Rotation3<f64>) {
let p: Point3<f64> = rotation * Point3::new(self.x, self.y, self.z);
self.x = p.x;
self.y = p.y;
self.z = p.z;
}
/// Applies a per-axis scale to this position in place.
///
/// Fast path: a plain component-wise multiply. Uniform scale is just
/// `Scale3::new(s, s, s)` at the call site.
pub fn apply_scale(&mut self, scale: Scale3<f64>) {
self.x *= scale.vector.x;
self.y *= scale.vector.y;
self.z *= scale.vector.z;
}
/// The position with the smallest representable coordinates `(f64::MIN, f64::MIN, f64::MIN)`.
pub const MIN: DirectPosition = DirectPosition {
x: f64::MIN,
y: f64::MIN,
z: f64::MIN,
};
/// The position with the largest representable coordinates `(f64::MAX, f64::MAX, f64::MAX)`.
pub const MAX: DirectPosition = DirectPosition {
x: f64::MAX,
y: f64::MAX,
z: f64::MAX,
};
/// The origin `(0.0, 0.0, 0.0)`.
pub const ORIGIN: DirectPosition = DirectPosition {
x: 0.0,
y: 0.0,
z: 0.0,
};
}
impl fmt::Display for DirectPosition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {}, {})", self.x, self.y, self.z)
}
}
impl From<DirectPosition> for nalgebra::Vector3<f64> {
fn from(item: DirectPosition) -> Self {
Self::new(item.x, item.y, item.z)
}
}
impl From<nalgebra::Vector3<f64>> for DirectPosition {
fn from(item: nalgebra::Vector3<f64>) -> Self {
Self::new(item.x, item.y, item.z).unwrap()
}
}
impl From<&DirectPosition> for nalgebra::Vector3<f64> {
fn from(item: &DirectPosition) -> Self {
Self::new(item.x, item.y, item.z)
}
}
impl From<&nalgebra::Vector3<f64>> for DirectPosition {
fn from(item: &nalgebra::Vector3<f64>) -> Self {
Self::new(item.x, item.y, item.z).unwrap()
}
}
impl From<DirectPosition> for nalgebra::Point3<f64> {
fn from(item: DirectPosition) -> Self {
Self::new(item.x, item.y, item.z)
}
}
impl From<DirectPosition> for nalgebra::Point3<f32> {
fn from(item: DirectPosition) -> Self {
Self::new(item.x as f32, item.y as f32, item.z as f32)
}
}
impl TryFrom<nalgebra::Point3<f64>> for DirectPosition {
type Error = Error;
fn try_from(item: nalgebra::Point3<f64>) -> Result<Self, Self::Error> {
Self::new(item.x, item.y, item.z)
}
}
impl From<DirectPosition> for parry3d_f64::math::Vector {
fn from(item: DirectPosition) -> Self {
Self::new(item.x, item.y, item.z)
}
}
impl From<parry3d_f64::math::Vector> for DirectPosition {
fn from(item: parry3d_f64::math::Vector) -> Self {
Self::new(item.x, item.y, item.z).expect("Should work")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::geometry::DirectPosition;
use approx::relative_eq;
use nalgebra::{Isometry3, Rotation3, Vector3};
use std::f64::consts::FRAC_PI_2;
#[test]
fn position_clone() {
let p = DirectPosition::new(1.0, 2.0, 3.0).unwrap();
let p2 = p;
assert_eq!(p, p2);
}
#[test]
fn apply_basic_transform() {
let mut position = DirectPosition::new(1.0, 2.0, 3.0).unwrap();
let isometry: Isometry3<f64> =
Isometry3::new(Vector3::new(-1.0, -2.0, 3.0), Default::default());
position.apply_transform(nalgebra::convert(isometry));
assert_eq!(position, DirectPosition::new(0.0, 0.0, 6.0).unwrap());
}
#[test]
fn apply_basic_translation_transform() {
let mut position = DirectPosition::new(1.0, 2.0, 3.0).unwrap();
let isometry: Isometry3<f64> =
Isometry3::new(Vector3::new(1.0, 1.0, 1.0), Default::default());
position.apply_transform(nalgebra::convert(isometry));
assert_eq!(position, DirectPosition::new(2.0, 3.0, 4.0).unwrap());
}
#[test]
fn apply_basic_rotation_transform() {
let mut position = DirectPosition::new(1.0, 1.0, 0.0).unwrap();
let isometry: Isometry3<f64> = Isometry3::from_parts(
Default::default(),
Rotation3::from_euler_angles(0.0, 0.0, FRAC_PI_2).into(),
);
position.apply_transform(nalgebra::convert(isometry));
relative_eq!(position.x(), -1.0, epsilon = f64::EPSILON);
relative_eq!(position.y(), 1.0, epsilon = f64::EPSILON);
relative_eq!(position.z(), 0.0, epsilon = f64::EPSILON);
}
}