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
310
311
// Copyright 2017 Nerijus Arlauskas
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! This is 32-bit 4-dimensional vector, where the first dimension has 2 bits, and
//! the last 3 dimensions have 10 bits each. It is useful for representing color with
//! an alpha, where the alpha does not require much precision.
//!
//! It is compatible with `GL_UNSIGNED_INT_2_10_10_10_REV` in OpenGL.
//!
//! ## Example
//!
//! ```rust
//! extern crate vec_2_10_10_10;
//!
//! fn main() {
//!     let value = vec_2_10_10_10::Vector::new(0.444, 0.555, 0.666, 0.2);
//!
//!     assert!(approx_equal(value.x(), 0.444));
//!     assert!(approx_equal(value.y(), 0.555));
//!     assert!(approx_equal(value.z(), 0.666));
//!
//!     // 2 bits means only possible values are 0, 0.3(3), 0.6(6) and 1.
//!     assert!(approx_equal(value.w(), 0.333));
//! }
//!
//! fn approx_equal(a: f32, b: f32) -> bool {
//!     const DELTA: f32 = 0.001;
//!     a > b - DELTA && a < b + DELTA
//! }
//! ```

use std::fmt;

/// Four dimensional 2-10-10-10 vector.
///
/// The binary data is mapped into floating point values from `0.0` to `1.0`.
/// The values outside this range are clamped.
///
/// The `w` dimension takes 2 bits, and can have values `0.0`, `0.3(3)`, `0.6(6)` and `1.0`.
/// The `x`, `y` and `z` dimensions take 10 bits, each.
///
/// The internal format is equivalent to `GL_UNSIGNED_INT_2_10_10_10_REV` OpenGL
/// vertex attribute type.
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct Vector {
    data: u32,
}

impl Vector {

    /// Creates a new Vector.
    ///
    /// First `x`, `y`, `z` values are stored in 10-bits, each.
    /// The `w` value is stored in 2 bits.
    ///
    /// Everything is packed internally into 4 bytes.
    ///
    /// The stored values are a bit wonky _precisely_ because of low stored precision.
    ///
    /// ```
    /// let value = vec_2_10_10_10::Vector::new(0.444, 0.555, 0.666, 0.2);
    ///
    /// assert!(approx_equal(value.x(), 0.444));
    /// assert!(approx_equal(value.y(), 0.555));
    /// assert!(approx_equal(value.z(), 0.666));
    ///
    /// // 2 bits means only possible values are 0, 0.3(3), 0.6(6) and 1.
    /// assert!(approx_equal(value.w(), 0.333));
    ///
    /// fn approx_equal(a: f32, b: f32) -> bool {
    ///     const DELTA: f32 = 0.001;
    ///     a > b - DELTA && a < b + DELTA
    /// }
    /// ```
    pub fn new(x: f32, y: f32, z: f32, w: f32) -> Vector {
        let x = (clamp(x) * 1023f32).round() as u32;
        let y = (clamp(y) * 1023f32).round() as u32;
        let z = (clamp(z) * 1023f32).round() as u32;
        let w = (clamp(w) * 3f32).round() as u32;

        let mut c: u32 = 0;
        c |= w << 30;
        c |= z << 20;
        c |= y << 10;
        c |= x << 0;

        Vector {
            data: c
        }
    }

    /// Creates a vector from raw 4-byte data.
    ///
    /// The vector can be used to inspect such data if it was created by other means.
    ///
    /// ```
    /// let other_value = *vec_2_10_10_10::Vector::new(0.444, 0.555, 0.666, 0.333).raw_value();
    /// let value = vec_2_10_10_10::Vector::from_raw(other_value);
    ///
    /// assert!(approx_equal(value.x(), 0.444));
    /// assert!(approx_equal(value.y(), 0.555));
    /// assert!(approx_equal(value.z(), 0.666));
    /// assert!(approx_equal(value.w(), 0.333));
    ///
    /// fn approx_equal(a: f32, b: f32) -> bool {
    ///     const DELTA: f32 = 0.001;
    ///     a > b - DELTA && a < b + DELTA
    /// }
    /// ```
    pub fn from_raw(data: u32) -> Vector {
        Vector {
            data: data
        }
    }

    /// Get `x` value.
    pub fn x(&self) -> f32 {
        (1023 & self.data) as f32 / 1023f32
    }

    /// Get `y` value.
    pub fn y(&self) -> f32 {
        ((1023 << 10 & self.data) >> 10) as f32 / 1023f32
    }

    /// Get `z` value.
    pub fn z(&self) -> f32 {
        ((1023 << 20 & self.data) >> 20) as f32 / 1023f32
    }

    /// Get `w` value.
    pub fn w(&self) -> f32 {
        ((0b11 << 30 & self.data) >> 30) as f32 / 3f32
    }

    /// Update `x` value.
    ///
    /// This changes internal 4-byte representation.
    ///
    /// ```
    /// let mut value = vec_2_10_10_10::Vector::new(0.0, 0.0, 0.0, 0.0);
    /// value.set_x(0.333);
    ///
    /// assert!(approx_equal(value.x(), 0.333));
    /// assert!(approx_equal(value.y(), 0.0));
    /// assert!(approx_equal(value.z(), 0.0));
    /// assert!(approx_equal(value.w(), 0.0));
    /// #
    /// # fn approx_equal(a: f32, b: f32) -> bool {
    /// #     const DELTA: f32 = 0.001;
    /// #     a > b - DELTA && a < b + DELTA
    /// # }
    /// ```
    pub fn set_x(&mut self, x: f32) {
        let x = (clamp(x) * 1023f32).round() as u32;
        let mut c: u32 =
            (
                3 << 30 | 1023 << 20 | 1023 << 10
            ) & self.data;
        c |= x;
        self.data = c;
    }

    /// Update `y` value.
    ///
    /// This changes internal 4-byte representation.
    ///
    /// ```
    /// let mut value = vec_2_10_10_10::Vector::new(0.0, 0.0, 0.0, 0.0);
    /// value.set_y(0.333);
    ///
    /// assert!(approx_equal(value.x(), 0.0));
    /// assert!(approx_equal(value.y(), 0.333));
    /// assert!(approx_equal(value.z(), 0.0));
    /// assert!(approx_equal(value.w(), 0.0));
    /// #
    /// # fn approx_equal(a: f32, b: f32) -> bool {
    /// #     const DELTA: f32 = 0.001;
    /// #     a > b - DELTA && a < b + DELTA
    /// # }
    /// ```
    pub fn set_y(&mut self, y: f32) {
        let y = (clamp(y) * 1023f32).round() as u32;
        let mut c: u32 =
            (
                3 << 30 | 1023 << 20 | 1023
            ) & self.data;
        c |= y << 10;
        self.data = c;
    }

    /// Update `z` value.
    ///
    /// This changes internal 4-byte representation.
    ///
    /// ```
    /// let mut value = vec_2_10_10_10::Vector::new(0.0, 0.0, 0.0, 0.0);
    /// value.set_z(0.333);
    ///
    /// assert!(approx_equal(value.x(), 0.0));
    /// assert!(approx_equal(value.y(), 0.0));
    /// assert!(approx_equal(value.z(), 0.333));
    /// assert!(approx_equal(value.w(), 0.0));
    /// #
    /// # fn approx_equal(a: f32, b: f32) -> bool {
    /// #     const DELTA: f32 = 0.001;
    /// #     a > b - DELTA && a < b + DELTA
    /// # }
    /// ```
    pub fn set_z(&mut self, z: f32) {
        let z = (clamp(z) * 1023f32).round() as u32;
        let mut c: u32 =
            (
                3 << 30 | 1023 << 10 | 1023
            ) & self.data;
        c |= z << 20;
        self.data = c;
    }

    /// Update `x`, `y` and `z`.
    ///
    /// This changes internal 4-byte representation.
    ///
    /// ```
    /// let mut value = vec_2_10_10_10::Vector::new(0.0, 0.0, 0.0, 0.0);
    /// value.set_xyz(0.333, 0.444, 0.555);
    ///
    /// assert!(approx_equal(value.x(), 0.333));
    /// assert!(approx_equal(value.y(), 0.444));
    /// assert!(approx_equal(value.z(), 0.555));
    /// assert!(approx_equal(value.w(), 0.0));
    /// #
    /// # fn approx_equal(a: f32, b: f32) -> bool {
    /// #     const DELTA: f32 = 0.001;
    /// #     a > b - DELTA && a < b + DELTA
    /// # }
    /// ```
    pub fn set_xyz(&mut self, x: f32, y: f32, z: f32) {
        let x = (clamp(x) * 1023f32).round() as u32;
        let y = (clamp(y) * 1023f32).round() as u32;
        let z = (clamp(z) * 1023f32).round() as u32;
        let mut c: u32 =
            (
                3 << 30
            ) & self.data;
        c |= z << 20;
        c |= y << 10;
        c |= x << 0;
        self.data = c;
    }

    /// Update `w`.
    ///
    /// This changes internal 4-byte representation.
    ///
    /// ```
    /// let mut value = vec_2_10_10_10::Vector::new(0.0, 0.0, 0.0, 0.0);
    /// value.set_w(0.333);
    ///
    /// assert!(approx_equal(value.x(), 0.0));
    /// assert!(approx_equal(value.y(), 0.0));
    /// assert!(approx_equal(value.z(), 0.0));
    /// assert!(approx_equal(value.w(), 0.333));
    /// #
    /// # fn approx_equal(a: f32, b: f32) -> bool {
    /// #     const DELTA: f32 = 0.001;
    /// #     a > b - DELTA && a < b + DELTA
    /// # }
    /// ```
    pub fn set_w(&mut self, w: f32) {
        let w = (clamp(w) * 3f32).round() as u32;
        let mut c: u32 =
            (
                1023 << 20 | 1023 << 10 | 1023
            ) & self.data;
        c |= w << 30;
        self.data = c;
    }

    /// Return raw internal value.
    pub fn raw_value(&self) -> &u32 {
        &self.data
    }
}

impl fmt::Debug for Vector {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_set()
            .entry(&self.x())
            .entry(&self.y())
            .entry(&self.z())
            .entry(&self.w())
            .finish()
    }
}

#[inline]
fn clamp(c: f32) -> f32 {
    if c < 0.0 {
        return 0.0;
    }
    if c > 1.0 {
        return 1.0;
    }
    c
}