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
//! The contract every point type implements, explicit or implicit.
//!
//! A predicate reads a point twice. Its fast path, the floating-point
//! filter, uses [`Point::approx`], an approximation of the coordinates, and
//! [`Point::err`], a bound on the error of each one. Only when the filter
//! cannot decide does it ask for [`Point::exact`], the coordinates as
//! expansions in a [`Scratch`] arena, in homogeneous form.
//!
//! `[f64; 3]` implements the contract as an explicit point: its components
//! are its exact coordinates, with no error.
use fmt;
use crateexpansion;
use crate;
/// A point in three dimensions, as the predicates read it.
///
/// Implementations must uphold, for each axis `k`:
///
/// - `err()[k]` is not negative (it may be `+∞`) and bounds the distance
/// between `approx()[k]` and coordinate `k` of the point, `x_k / w`,
/// where `x_k` is the numerator and `w` the weight that
/// [`exact`](Self::exact) writes (`x_k` itself when `w` is
/// [`Weight::One`]);
/// - `err()[k]` is zero only when `approx()[k]` is that coordinate;
/// - [`is_explicit`](Self::is_explicit) returns `true` only if `approx()`
/// holds the exact coordinates, `err()` is zero on every axis and
/// [`exact`](Self::exact) gives [`Weight::One`]: for such points the
/// predicates use `approx()` alone, with no uncertainty;
/// - every span of the [`ExactCoords`] that [`exact`](Self::exact) returns
/// holds a valid expansion (see [`expansion`]: nonzero components of
/// strictly increasing magnitude that do not overlap), written in the
/// arena it was given;
/// - [`exact`](Self::exact) leaves the arena below the top it had when
/// `exact` was called as it was: it writes only above that top and never
/// resets the arena to a mark taken before the call, since a predicate
/// keeps the exact coordinates of the points it read before there;
/// - while a predicate runs, which may call each method more than once,
/// [`approx`](Self::approx), [`err`](Self::err) and
/// [`is_explicit`](Self::is_explicit) give the same answer every time and
/// [`exact`](Self::exact) the same coordinates: a type with interior
/// mutability must not change them in the middle of a call.
///
/// A predicate given a point that breaks this contract can return a wrong
/// sign: predicates do not check it.
///
/// # Examples
///
/// ```
/// use implicit_predicates_core::{Point, Scratch, Weight};
///
/// let p = [1.5, 0.0, -2.0];
/// assert!(p.is_explicit());
/// assert_eq!(p.err(), &[0.0; 3]);
///
/// let mut buf = [0.0; 3];
/// let mut scratch = Scratch::borrowed(&mut buf);
/// let exact = p.exact(&mut scratch)?;
/// assert_eq!(scratch.get(exact.x), &[1.5]);
/// assert!(exact.y.is_empty()); // zero
/// assert_eq!(exact.w, Weight::One);
/// # Ok::<(), implicit_predicates_core::ExactError>(())
/// ```
/// The exact coordinates of a point, in homogeneous form: the point is
/// `(x / w, y / w, z / w)`. The spans belong to the arena given to
/// [`Point::exact`], and each holds a valid expansion (see [`expansion`]);
/// the predicates that read them do not check it.
/// The common denominator of the exact coordinates of a point.
/// The error of [`Point::exact`].
/// An explicit point, not validated: its components are its exact
/// coordinates. A component that is not finite makes
/// [`exact`](Point::exact) fail with [`ExactError::OutOfRange`].