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
use crate::Point;

///
/// Default tension of curve between passed points.
/// Will be used if not specified in [`SplineOpts`].
///
/// [`SplineOpts`]: struct.SplineOpts.html
pub const DEFAULT_TENSION: f64 = 0.5;

///
/// Default number of segments.
/// Will be used if not specified in [`SplineOpts`].
///
/// [`SplineOpts`]: struct.SplineOpts.html
pub const DEFAULT_SEGMENTS: u32 = 16;

///
/// A list of options indicating how the spline should be calculated
///
/// ```
/// use cubic_spline::SplineOpts;
///
/// let o1 = SplineOpts::default();
///
/// let o2 = SplineOpts::new();
///
/// let o3 = SplineOpts::new()
///   .tension(0.5)
///   .num_of_segments(16);
///
/// ```
/// Options list:
/// * `tension` -
///   Sets the bending strength of the curve.
///   The usual value ranges from `0.0` (straight) to `1.0` (very rounded).
///   If not specified [`DEFAULT_TENSION`] will be used.
///
/// * `num_of_segments` -
///   The number of points to be calculated between each two known points.
///   If not specified [`DEFAULT_SEGMENTS`] will be used.
///
/// * `hidden_point_at_start` - A point that will not be drawn,
///   but the beginning of the graph will bend as if it is there.
///
/// * `hidden_point_at_end` - Same as previous, but affects the end of the graph.
///
/// [`DEFAULT_TENSION`]: constant.DEFAULT_TENSION.html
/// [`DEFAULT_SEGMENTS`]: constant.DEFAULT_SEGMENTS.html
#[derive(Clone)]
pub struct SplineOpts {
  tension: f64,
  num_of_segments: u32,
  hidden_point_at_start: Option<Point>,
  hidden_point_at_end: Option<Point>,
}

impl SplineOpts {
  ///
  /// Creates new one with defaults.
  pub fn new() -> Self {
    SplineOpts::default()
  }

  ///
  /// Sets tension.
  pub fn tension(mut self, val: f64) -> Self {
    self.tension = val;
    self
  }

  ///
  /// Sets num_of_segments.
  pub fn num_of_segments(mut self, val: u32) -> Self {
    self.num_of_segments = val;
    self
  }

  ///
  /// Sets hidden_point_at_start.
  pub fn hidden_point_at_start<T: Into<Point>>(mut self, val: T) -> Self {
    self.hidden_point_at_start = Some(val.into());
    self
  }

  ///
  /// Sets hidden_point_at_end.
  pub fn hidden_point_at_end<T: Into<Point>>(mut self, val: T) -> Self {
    self.hidden_point_at_end = Some(val.into());
    self
  }

  //
  // Sets tension.
  pub fn get_tension(&self) -> f64 {
    self.tension
  }

  //
  // Sets num_of_segments.
  pub fn get_num_of_segments(&self) -> u32 {
    self.num_of_segments
  }

  //
  // Sets hidden_point_at_start.
  pub fn get_hidden_point_at_start(&self) -> Option<&Point> {
    self.hidden_point_at_start.as_ref()
  }

  //
  // Sets hidden_point_at_end.
  pub fn get_hidden_point_at_end(&self) -> Option<&Point> {
    self.hidden_point_at_end.as_ref()
  }
}

impl Default for SplineOpts {
  ///
  /// # Example
  /// ```
  /// use cubic_spline::{SplineOpts};
  /// let opts = SplineOpts::default();
  ///
  /// assert_eq!(opts.get_num_of_segments(), cubic_spline::DEFAULT_SEGMENTS);
  /// assert!((opts.get_tension() - cubic_spline::DEFAULT_TENSION).abs() < 1e-9);
  /// assert!(opts.get_hidden_point_at_end().is_none());
  ///
  /// ```
  fn default() -> Self {
    SplineOpts {
      tension: DEFAULT_TENSION,
      num_of_segments: DEFAULT_SEGMENTS,
      hidden_point_at_start: None,
      hidden_point_at_end: None,
    }
  }
}