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
//! Coordinate systems and conversions.
//!
//! CSXCAD knows two coordinate systems: Cartesian `(x, y, z)` and cylindrical
//! `(ρ, α, z)` where `α` is the azimuth in radians measured from the +x axis
//! towards +y, and `z` is shared with the Cartesian system. Both are stored
//! in a [`Vec3`]; the meaning of its components depends on the system.
//!
//! # Crate-wide conventions
//!
//! * **Axis indices** are `usize`: `0` = x (or ρ), `1` = y (or α),
//! `2` = z. They index [`Vec3`] components, grid line sets, material axes,
//! polygon normals and so on. [`crate::Axis`] is the typed equivalent used
//! by a few property fields; it converts to an index with `.into()`
//! (`grid.lines(Axis::Z.into())`) where a named axis reads better.
//! * **Lengths** are in *drawing units*; the mesh's
//! [`crate::RectilinearGrid::delta_unit`] gives the size of one unit in
//! metres. Material parameters are in SI units.
//! * **Angles** are radians everywhere (cylindrical `α`, rotation
//! arguments, revolved polygon angles), except for transforms that opt into
//! degrees with [`crate::Transform::set_angles_in_degrees`].
//! * **Queries** always state the coordinate system of the query point
//! explicitly; nothing is converted implicitly.
//!
//! ```
//! use core::f64::consts::FRAC_PI_2;
//! use csx::{CoordSystem, Vec3};
//!
//! // ρ = 2, α = 90°, z = 1 is the Cartesian point (0, 2, 1).
//! let cyl = Vec3::new(2.0, FRAC_PI_2, 1.0);
//! let cart = CoordSystem::Cylindrical.to_cartesian(cyl);
//! assert!((cart - Vec3::new(0.0, 2.0, 1.0)).norm() < 1e-12);
//! let back = CoordSystem::Cartesian.convert(cart, CoordSystem::Cylindrical);
//! assert!((back - cyl).norm() < 1e-12);
//! ```
use ;
use crateVec3;
/// A coordinate system.
///
/// Marked `#[non_exhaustive]`: CSXCAD reserves a (currently unimplemented)
/// spherical system, which may be added later.
///
/// ```
/// use csx::CoordSystem;
///
/// assert_eq!(CoordSystem::default(), CoordSystem::Cartesian);
/// assert_eq!(CoordSystem::from_id(1), Some(CoordSystem::Cylindrical));
/// assert_eq!(CoordSystem::Cylindrical.id(), 1);
/// assert_eq!(CoordSystem::from_id(7), None);
/// ```
/// `(ρ, α, z)` → `(x, y, z)`. Negative `ρ` is allowed and mirrors the
/// point through the z axis.
///
/// ```
/// use csx::coords::cylindrical_to_cartesian;
/// use csx::Vec3;
///
/// let p = cylindrical_to_cartesian(Vec3::new(1.0, core::f64::consts::PI, 3.0));
/// assert!((p - Vec3::new(-1.0, 0.0, 3.0)).norm() < 1e-12);
/// ```
/// `(x, y, z)` → `(ρ, α, z)` with `ρ ≥ 0` and `α ∈ (-π, π]` (`α = 0` on
/// the z axis).
///
/// ```
/// use csx::coords::cartesian_to_cylindrical;
/// use csx::Vec3;
///
/// let c = cartesian_to_cylindrical(Vec3::new(0.0, -2.0, 5.0));
/// assert_eq!(c.x(), 2.0);
/// assert!((c.y() + core::f64::consts::FRAC_PI_2).abs() < 1e-12);
/// ```
/// Spherical radius `r`, polar angle `θ` (from +z, in `[0, π]`) and
/// azimuth `α` (in `(-π, π]`) of a Cartesian point. These are the `r`, `t`
/// and `a` variables of weighting functions.
///
/// ```
/// use csx::coords::cartesian_to_spherical;
/// use csx::Vec3;
///
/// let (r, theta, alpha) = cartesian_to_spherical(Vec3::new(0.0, 0.0, -3.0));
/// assert_eq!((r, alpha), (3.0, 0.0));
/// assert!((theta - core::f64::consts::PI).abs() < 1e-12);
/// ```
/// Wraps an angle into `[-π, π)`.
///
/// ```
/// use core::f64::consts::PI;
/// use csx::coords::wrap_angle;
///
/// assert!((wrap_angle(1.5 * PI) + 0.5 * PI).abs() < 1e-12);
/// assert_eq!(wrap_angle(PI), -PI);
/// ```
/// Tests whether angle `a` lies within the angular interval going
/// counter-clockwise from `start` to `stop` (inclusive, tolerance `eps`).
/// Intervals spanning `2π` or more contain every angle; `stop < start`
/// describes the same interval as `[stop, start]`.
///
/// ```
/// use core::f64::consts::PI;
/// use csx::coords::angle_in_range;
///
/// // An interval crossing the ±π branch cut.
/// assert!(angle_in_range(-PI + 0.05, 3.0, 3.3, 0.0));
/// assert!(!angle_in_range(0.0, 3.0, 3.3, 0.0));
/// ```