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
//! Scan positions and their consituant parts.

use {CameraCalibration, Error, MountCalibration, Project, Result};
use nalgebra::Projective3;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// A scan position
#[derive(Clone, Debug, Serialize, PartialEq)]
pub struct ScanPosition {
    /// The name of the scan position.
    pub name: String,
    /// The scan position images.
    pub images: HashMap<String, Image>,
    /// The scanner's own position.
    pub sop: Projective3<f64>,
    /// The scans taken at this position.
    pub scans: HashMap<String, Scan>,
}

/// A scan.
#[derive(Clone, Debug, Serialize, PartialEq)]
pub struct Scan {
    /// The name of the scan.
    pub name: String,
    /// The file name of the scan.
    pub file: String,
    /// The number of measurements in the phi direction.
    pub phi_count: usize,
    /// The number of measurements in the theta direction.
    pub theta_count: usize,
}

/// A scan position image.
#[derive(Clone, Debug, Serialize, PartialEq)]
pub struct Image {
    /// The name of the image.
    pub name: String,
    /// The camera's own position when taking the image.
    pub cop: Projective3<f64>,
    /// The name of the image's camera calibration.
    pub camera_calibration_name: String,
    /// The name of the image's mount calibration.
    pub mount_calibration_name: String,
}

impl ScanPosition {
    /// Returns a scan position image, as determined by the path.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::Project;
    /// let project = Project::from_path("data/project.RiSCAN").unwrap();
    /// let scan_position = project.scan_positions.get("SP01").unwrap();
    /// let image1 = scan_position.images.get("SP01 - Image001").unwrap();
    /// let path = "data/project.RiSCAN/SCANS/SP01/SCANPOSIMAGES/SP01 - Image001.csv";
    /// let image2 = scan_position.image_from_path(path).unwrap();
    /// assert_eq!(image1, image2);
    /// ```
    pub fn image_from_path<P: AsRef<Path>>(&self, path: P) -> Result<&Image> {
        use Error;
        path.as_ref()
            .file_stem()
            .map(|file_stem| file_stem.to_string_lossy())
            .and_then(|file_stem| self.images.get(file_stem.as_ref()))
            .ok_or_else(|| Error::ImageFromPath(path.as_ref().to_path_buf()))
    }

    /// Returns a vector of all paths to rxps in the singlescan directory.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::Project;
    /// let project = Project::from_path("data/project.RiSCAN").unwrap();
    /// let scan_position = project
    ///     .scan_positions
    ///     .get("SP01")
    ///     .unwrap();
    /// let paths = scan_position.singlescan_rxp_paths(&project);
    /// assert_eq!(4, paths.len());
    /// ```
    pub fn singlescan_rxp_paths(&self, project: &Project) -> Vec<PathBuf> {
        let mut path = project
            .path
            .parent()
            .expect("Project path should always have a parent")
            .to_path_buf();
        path.push("SCANS");
        path.push(&self.name);
        path.push("SINGLESCANS");
        self.scans
            .values()
            .map(|scan| {
                let mut path = path.clone();
                path.push(&scan.file);
                path
            })
            .collect()
    }
}

impl Image {
    /// Finds and returns this image's camera calibration.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::Project;
    /// let project = Project::from_path("data/project.RiSCAN").unwrap();
    /// let mut image = project.scan_positions
    ///     .get("SP01")
    ///     .unwrap()
    ///     .images
    ///     .get("SP01 - Image001")
    ///     .unwrap()
    ///     .clone();
    /// image.camera_calibration(&project).unwrap();
    /// image.camera_calibration_name = "Not a camera calibration".to_string();
    /// assert!(image.camera_calibration(&project).is_err());
    /// ```
    pub fn camera_calibration<'a>(&self, project: &'a Project) -> Result<&'a CameraCalibration> {
        project
            .camera_calibrations
            .get(&self.camera_calibration_name)
            .ok_or_else(|| {
                Error::MissingCameraCalibration(self.camera_calibration_name.clone())
            })
    }

    /// Finds and returns this image's mount calibration.
    ///
    /// # Examples
    ///
    /// ```
    /// use riscan_pro::Project;
    /// let project = Project::from_path("data/project.RiSCAN").unwrap();
    /// let mut image = project.scan_positions
    ///     .get("SP01")
    ///     .unwrap()
    ///     .images
    ///     .get("SP01 - Image001")
    ///     .unwrap()
    ///     .clone();
    /// image.mount_calibration(&project).unwrap();
    /// image.mount_calibration_name = "Not a mount calibration".to_string();
    /// assert!(image.mount_calibration(&project).is_err());
    /// ```
    pub fn mount_calibration<'a>(&self, project: &'a Project) -> Result<&'a MountCalibration> {
        project
            .mount_calibrations
            .get(&self.mount_calibration_name)
            .ok_or_else(|| {
                Error::MissingMountCalibration(self.mount_calibration_name.clone())
            })
    }
}

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

    #[test]
    fn scan_position_singlescan_rxp_paths() {
        let project = Project::from_path("data/project.RiSCAN").unwrap();
        let scan_position = project.scan_positions.get("SP01").unwrap();
        let paths = scan_position.singlescan_rxp_paths(&project);
        assert_eq!(4, paths.len());
    }

}