alloy/model/
clip.rs

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
//! The `clip` module provides definitions for clip-space information.

use crate::math::vec::{Vec2, Vector2};

/// Representation of a depth range.
#[derive(Debug)]
pub struct Depth {
  near: f32,
  far: f32,
}

// Constructors

impl Depth {
  /// Creates a new depth range.
  ///
  /// # Panics
  ///
  /// Panics if `near` is less than or equal to zero, or if `far` is less than
  /// `near`.
  ///
  /// # Parameters
  ///
  /// * `near` - The near depth value.
  /// * `far` - The far depth value.
  pub const fn new(near: f32, far: f32) -> Self {
    // debug_assert!(near > 0.0);
    // debug_assert!(far > near);
    Self { near, far }
  }
}

impl Default for Depth {
  fn default() -> Self {
    Self::new(0.1, 100.0)
  }
}

impl From<Vector2> for Depth {
  fn from(vector: Vector2) -> Self {
    Self::new(vector.x(), vector.y())
  }
}

impl From<&Vec2> for Depth {
  fn from(vector: &Vec2) -> Self {
    Self::new(vector.x(), vector.y())
  }
}

// Properties

impl Depth {
  /// Retrieves the near depth value.
  pub const fn near(&self) -> f32 {
    self.near
  }

  /// Retrieves the far depth value.
  pub const fn far(&self) -> f32 {
    self.far
  }
}

/// Representation of a horizontal range.
#[derive(Debug)]
pub struct Horizontal {
  left: f32,
  right: f32,
}

impl Horizontal {
  /// Creates a new horizontal range.
  ///
  /// # Panics
  ///
  /// Panics if `left` is greater than or equal to `right`.
  ///
  /// # Parameters
  ///
  /// * `left` - The left horizontal value.
  /// * `right` - The right horizontal value.
  #[inline(always)]
  pub fn new(left: f32, right: f32) -> Self {
    debug_assert!(left < right);
    Self { left, right }
  }

  /// Creates a new horizontal range with a uniform value.
  ///
  /// # Panics
  ///
  /// Panics if `value` is less than or equal to zero.
  ///
  /// # Parameters
  ///
  /// * `value` - The uniform value.
  #[inline(always)]
  pub fn uniform(value: f32) -> Self {
    debug_assert!(value > 0.0);
    Self::new(-value, value)
  }
}

impl Default for Horizontal {
  fn default() -> Self {
    Self::uniform(1.0)
  }
}

impl From<Vector2> for Horizontal {
  fn from(vector: Vector2) -> Self {
    Self::new(vector.x(), vector.y())
  }
}

impl From<&Vec2> for Horizontal {
  fn from(vector: &Vec2) -> Self {
    Self::new(vector.x(), vector.y())
  }
}

impl Horizontal {
  /// Retrieves the left horizontal value.
  pub const fn left(&self) -> f32 {
    self.left
  }

  /// Retrieves the right horizontal value.
  pub const fn right(&self) -> f32 {
    self.right
  }
}

/// Representation of a vertical range.
#[derive(Debug)]
pub struct Vertical {
  bottom: f32,
  top: f32,
}

impl Vertical {
  /// Creates a new vertical range.
  ///
  /// # Panics
  ///
  /// Panics if `bottom` is greater than or equal to `top`.
  ///
  /// # Parameters
  ///
  /// * `bottom` - The bottom vertical value.
  /// * `top` - The top vertical value.
  #[inline(always)]
  pub fn new(bottom: f32, top: f32) -> Self {
    debug_assert!(bottom < top);
    Self { bottom, top }
  }

  /// Creates a new vertical range with a uniform value.
  ///
  /// # Panics
  ///
  /// Panics if `value` is less than or equal to zero.
  ///
  /// # Parameters
  ///
  /// * `value` - The uniform value.
  #[inline(always)]
  pub fn uniform(value: f32) -> Self {
    debug_assert!(value > 0.0);
    Self::new(-value, value)
  }
}

impl Default for Vertical {
  fn default() -> Self {
    Self::uniform(1.0)
  }
}

impl From<Vector2> for Vertical {
  fn from(vector: Vector2) -> Self {
    Self::new(vector.x(), vector.y())
  }
}

impl From<&Vec2> for Vertical {
  fn from(vector: &Vec2) -> Self {
    Self::new(vector.x(), vector.y())
  }
}

impl Vertical {
  /// Retrieves the bottom vertical value.
  pub const fn bottom(&self) -> f32 {
    self.bottom
  }

  /// Retrieves the top vertical value.
  pub const fn top(&self) -> f32 {
    self.top
  }
}

/// Representation of clip-space information.
#[derive(Default, Debug)]
pub struct ClipSpace {
  /// The depth range.
  pub depth: Depth,

  /// The horizontal range.
  pub horizontal: Horizontal,

  /// The vertical range.
  pub vertical: Vertical,
}

impl ClipSpace {
  /// Creates a new clip-space information.
  ///
  /// # Parameters
  ///
  /// * `depth` - The depth range.
  /// * `horizontal` - The horizontal range.
  /// * `vertical` - The vertical range.
  pub const fn new(depth: Depth, horizontal: Horizontal, vertical: Vertical) -> Self {
    Self {
      depth,
      horizontal,
      vertical,
    }
  }

  /// Creates a new clip-space information with symmetric horizontal and
  /// vertical
  ///
  /// # Panics
  ///
  /// Panics if `depth` is less than or equal to zero, or if `horizontal` or
  /// `vertical` are less than or equal to zero.
  ///
  /// # Parameters
  ///
  /// * `depth` - The depth range.
  /// * `horizontal` - The horizontal range.
  /// * `vertical` - The vertical range.
  #[inline(always)]
  pub fn uniform(depth: f32, horizontal: f32, vertical: f32) -> Self {
    Self::new(
      Depth::new(0.0, depth),
      Horizontal::uniform(horizontal),
      Vertical::uniform(vertical),
    )
  }
}