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
use MountCalibration;
use nalgebra::{Point3, Projective3};
use std::marker::PhantomData;
use std::ops::Deref;

/// A three-dimensional point.
#[derive(Clone, Copy, Debug)]
pub struct Point<C: CoordinateReferenceSystem> {
    phantom: PhantomData<C>,
    point: Point3<f64>,
}

/// A marker trait for coordinate reference systems.
pub trait CoordinateReferenceSystem {}

/// The GLobal Coordinate System.
#[derive(Clone, Copy, Debug)]
pub struct Glcs {}

/// The PRoject Coordinate System.
#[derive(Clone, Copy, Debug)]
pub struct Prcs {}

/// The Scanner's Own Coordiate System.
#[derive(Clone, Copy, Debug)]
pub struct Socs {}

/// The CaMera's Coordinate System.
#[derive(Clone, Copy, Debug)]
pub struct Cmcs {}

impl Point<Glcs> {
    /// Returns a point in the global coordinate system.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::Point;
    /// let glcs = Point::glcs(1., 2., 3.);
    /// ```
    pub fn glcs<T: Into<f64>>(x: T, y: T, z: T) -> Point<Glcs> {
        Point {
            phantom: PhantomData,
            point: Point3::new(x.into(), y.into(), z.into()),
        }
    }

    /// Converts this glcs point to prcs.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::{Point, Project};
    /// let project = Project::from_path("data/project.RiSCAN").unwrap();
    /// let prcs = Point::glcs(1., 2., 3.).to_prcs(project.pop);
    /// ```
    pub fn to_prcs(&self, pop: Projective3<f64>) -> Point<Prcs> {
        (pop.inverse() * self.deref()).into()
    }
}

impl Point<Prcs> {
    /// Returns a point in the project's coordinate system.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::Point;
    /// let prcs = Point::prcs(1., 2., 3.);
    /// ```
    pub fn prcs<T: Into<f64>>(x: T, y: T, z: T) -> Point<Prcs> {
        Point {
            phantom: PhantomData,
            point: Point3::new(x.into(), y.into(), z.into()),
        }
    }

    /// Converts this point to the global coordinate system.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::{Point, Project};
    /// let project = Project::from_path("data/project.RiSCAN").unwrap();
    /// let glcs = Point::prcs(1., 2., 3.).to_glcs(project.pop);
    /// ```
    pub fn to_glcs(&self, pop: Projective3<f64>) -> Point<Glcs> {
        (pop * self.deref()).into()
    }

    /// Converts this prcs point to socs.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::{Point, Project};
    /// let scan_position = Project::from_path("data/project.RiSCAN")
    ///     .unwrap()
    ///     .scan_positions
    ///     .get("SP01")
    ///     .unwrap()
    ///     .clone();
    /// let socs = Point::prcs(1., 2., 3.).to_socs(scan_position.sop);
    /// ```
    pub fn to_socs(&self, sop: Projective3<f64>) -> Point<Socs> {
        (sop.inverse() * self.deref()).into()
    }
}

impl Point<Socs> {
    /// Returns a point in the scanner's own coordinate system.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::Point;
    /// let socs = Point::socs(1., 2., 3.);
    /// ```
    pub fn socs<T: Into<f64>>(x: T, y: T, z: T) -> Point<Socs> {
        Point {
            phantom: PhantomData,
            point: Point3::new(x.into(), y.into(), z.into()),
        }
    }

    /// Converts this socs point to the project coordinate system.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::{Point, Project};
    /// let scan_position = Project::from_path("data/project.RiSCAN")
    ///     .unwrap()
    ///     .scan_positions
    ///     .get("SP01")
    ///     .unwrap()
    ///     .clone();
    /// let prcs = Point::socs(1., 2., 3.).to_prcs(scan_position.sop);
    /// ```
    pub fn to_prcs(&self, sop: Projective3<f64>) -> Point<Prcs> {
        (sop * self.deref()).into()
    }

    /// Converts this socs point to cmcs.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::{Point, Project};
    /// let project = Project::from_path("data/project.RiSCAN").unwrap();
    /// let image = project.scan_positions
    ///     .get("SP01")
    ///     .unwrap()
    ///     .images
    ///     .get("SP01 - Image001")
    ///     .unwrap();
    /// let mount_calibration = image.mount_calibration(&project).unwrap();
    /// let cmcs = Point::socs(1., 2., 3.).to_cmcs(image.cop, &mount_calibration);
    /// ```
    pub fn to_cmcs(
        &self,
        cop: Projective3<f64>,
        mount_calibration: &MountCalibration,
    ) -> Point<Cmcs> {
        (mount_calibration.deref() * cop.inverse() * self.deref()).into()
    }
}

impl Point<Cmcs> {
    /// Returns a point in the camera's coordinate system.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::Point;
    /// let cmcs = Point::cmcs(1., 2., 3.);
    /// ```
    pub fn cmcs<T: Into<f64>>(x: T, y: T, z: T) -> Point<Cmcs> {
        Point {
            phantom: PhantomData,
            point: Point3::new(x.into(), y.into(), z.into()),
        }
    }

    /// Converts this cmcs point to socs.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::{Point, Project};
    /// let project = Project::from_path("data/project.RiSCAN").unwrap();
    /// let image = project.scan_positions
    ///     .get("SP01")
    ///     .unwrap()
    ///     .images
    ///     .get("SP01 - Image001")
    ///     .unwrap();
    /// let mount_calibration = image.mount_calibration(&project).unwrap();
    /// let socs = Point::cmcs(1., 2., 3.).to_socs(image.cop, &mount_calibration);
    /// ```
    pub fn to_socs(
        &self,
        cop: Projective3<f64>,
        mount_calibration: &MountCalibration,
    ) -> Point<Socs> {
        (cop * (*mount_calibration).inverse() * self.deref()).into()
    }

    /// Is this point behind the camera?
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::Point;
    /// assert!(!Point::cmcs(1., 2., 3.).is_behind_camera());
    /// assert!(Point::cmcs(1., 2., -3.).is_behind_camera());
    /// ```
    pub fn is_behind_camera(&self) -> bool {
        self.z <= 0.
    }

    /// Returns the horizontal tangent of this point.
    ///
    /// The horizontal tangent is the tangent of the angle of the point, as projected to the yz
    /// plane, to the z axis, i.e. `y / z`.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::Point;
    /// assert_eq!(2., Point::cmcs(3., 2., 1.).tan_horz());
    /// ```
    pub fn tan_horz(&self) -> f64 {
        self.y / self.z
    }

    /// Returns the vertical tangent of this point.
    ///
    /// The vertical tangent is the tangent of the angle of the point, as projected to the xz
    /// plane, to the z axis, i.e. `x / z`.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::Point;
    /// assert_eq!(3., Point::cmcs(3., 2., 1.).tan_vert());
    /// ```
    pub fn tan_vert(&self) -> f64 {
        self.x / self.z
    }
}

impl<C: CoordinateReferenceSystem> From<Point3<f64>> for Point<C> {
    fn from(point: Point3<f64>) -> Point<C> {
        Point {
            phantom: PhantomData,
            point: point,
        }
    }
}

impl<C: CoordinateReferenceSystem> Deref for Point<C> {
    type Target = Point3<f64>;
    fn deref(&self) -> &Point3<f64> {
        &self.point
    }
}

impl<C: CoordinateReferenceSystem> PartialEq<Point<C>> for Point<C> {
    fn eq(&self, other: &Point<C>) -> bool {
        self.deref().eq(other)
    }
}

impl CoordinateReferenceSystem for Glcs {}
impl CoordinateReferenceSystem for Prcs {}
impl CoordinateReferenceSystem for Socs {}
impl CoordinateReferenceSystem for Cmcs {}

#[cfg(test)]
mod tests {
    use super::*;
    use Project;

    #[test]
    fn cmcs_is_behind_camera() {
        assert!(!Point::cmcs(1., 1., 1.).is_behind_camera());
        assert!(Point::cmcs(1., 1., -1.).is_behind_camera());
    }

    #[test]
    fn roundtrip() {
        let glcs = Point::glcs(1., 2., 3.);
        let project = Project::from_path("data/project.RiSCAN").unwrap();
        let scan_position = project.scan_positions.get("SP01").unwrap();
        let image = scan_position.images.get("SP01 - Image001").unwrap();
        let mount_calibration = image.mount_calibration(&project).unwrap();

        let prcs = glcs.to_prcs(project.pop);
        let socs = prcs.to_socs(scan_position.sop);
        let cmcs = socs.to_cmcs(image.cop, &mount_calibration);
        let socs2 = cmcs.to_socs(image.cop, &mount_calibration);
        assert_relative_eq!(socs.deref(), socs2.deref(), epsilon = 1e-6);
        let prcs2 = socs2.to_prcs(scan_position.sop);
        assert_relative_eq!(prcs.deref(), prcs2.deref(), epsilon = 1e-6);
        let glcs2 = prcs2.to_glcs(project.pop);
        assert_relative_eq!(glcs.deref(), glcs2.deref(), epsilon = 1e-6);
    }
}