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
use crate::{
    error::{errcode_to_result, Result},
    geometry::Point,
};
use ffi::BLMatrix2DOp::*;

bl_enum! {
    #[doc(hidden)]
    pub enum Matrix2DOp {
        Reset = BL_MATRIX2D_OP_RESET,
        Assign = BL_MATRIX2D_OP_ASSIGN,
        Translate = BL_MATRIX2D_OP_TRANSLATE,
        Scale = BL_MATRIX2D_OP_SCALE,
        Skew = BL_MATRIX2D_OP_SKEW,
        Rotate= BL_MATRIX2D_OP_ROTATE,
        RotatePoint = BL_MATRIX2D_OP_ROTATE_PT,
        Transform = BL_MATRIX2D_OP_TRANSFORM,
        PostTranslate = BL_MATRIX2D_OP_POST_TRANSLATE,
        PostScale = BL_MATRIX2D_OP_POST_SCALE,
        PostSkew = BL_MATRIX2D_OP_POST_SKEW,
        PostRotate = BL_MATRIX2D_OP_POST_ROTATE,
        PostRotatePoint = BL_MATRIX2D_OP_POST_ROTATE_PT,
        PostTransform = BL_MATRIX2D_OP_POST_TRANSFORM,
    }
    Default => Reset
}

// Row-Major
#[repr(transparent)]
#[derive(Default, Copy, Clone)]
pub struct Matrix2D([f64; ffi::BLMatrix2DValue::BL_MATRIX2D_VALUE_COUNT as usize]);

impl Matrix2D {
    #[inline]
    pub fn new(m00: f64, m01: f64, m10: f64, m11: f64, m20: f64, m21: f64) -> Self {
        Matrix2D([m00, m01, m10, m11, m20, m21])
    }

    #[inline]
    pub fn identity() -> Matrix2D {
        Matrix2D([1.0, 0.0, 0.0, 1.0, 0.0, 0.0])
    }

    #[inline]
    pub fn translation(x: f64, y: f64) -> Matrix2D {
        Matrix2D([1.0, 0.0, 0.0, 1.0, x, y])
    }

    #[inline]
    pub fn translation_point<P: Point>(p: &P) -> Matrix2D {
        let p = p.into_f64();
        Matrix2D([1.0, 0.0, 0.0, 1.0, p[0], p[1]])
    }

    #[inline]
    pub fn scaling(x: f64, y: f64) -> Matrix2D {
        Matrix2D([x, 0.0, 0.0, y, 0.0, 0.0])
    }

    #[inline]
    pub fn scaling_point<P: Point>(p: &P) -> Matrix2D {
        let p = p.into_f64();
        Matrix2D([p[0], 0.0, 0.0, p[1], 0.0, 0.0])
    }

    #[inline]
    pub fn rotation(angle: f64, x: f64, y: f64) -> Matrix2D {
        let mut this = Matrix2D::identity();
        this.reset_to_rotation(angle, x, y);
        this
    }

    #[inline]
    pub fn rotation_point<P: Point>(angle: f64, p: &P) -> Matrix2D {
        let p = p.into_f64();
        let mut this = Matrix2D::identity();
        this.reset_to_rotation(angle, p[0], p[1]);
        this
    }

    #[inline]
    pub fn skewing(x: f64, y: f64) -> Matrix2D {
        let mut this = Matrix2D::identity();
        this.reset_to_skewing(x, y);
        this
    }

    #[inline]
    pub fn skewing_point<P: Point>(p: &P) -> Matrix2D {
        let p = p.into_f64();
        let mut this = Matrix2D::identity();
        this.reset_to_skewing(p[0], p[1]);
        this
    }

    #[inline]
    pub fn sin_cos(sin: f64, cos: f64, tx: f64, ty: f64) -> Matrix2D {
        Matrix2D([cos, sin, -sin, cos, tx, ty])
    }

    #[inline]
    pub fn sin_cos_point<P: Point>(sin: f64, cos: f64, t: &P) -> Matrix2D {
        let t = t.into_f64();
        Self::sin_cos(sin, cos, t[0], t[1])
    }

    #[inline]
    pub fn reset(&mut self) {
        *self = Self::identity();
    }

    #[inline]
    pub fn reset_to_translation(&mut self, x: f64, y: f64) {
        *self = Matrix2D([1.0, 0.0, 0.0, 1.0, x, y])
    }

    #[inline]
    pub fn reset_to_translation_point<P: Point>(&mut self, p: &P) {
        let p = p.into_f64();
        self.reset_to_translation(p[0], p[1]);
    }

    #[inline]
    pub fn reset_to_scaling(&mut self, x: f64, y: f64) {
        *self = Matrix2D([x, 0.0, 0.0, y, 0.0, 0.0])
    }

    #[inline]
    pub fn reset_to_scaling_point<P: Point>(&mut self, p: &P) {
        let p = p.into_f64();
        self.reset_to_scaling(p[0], p[1]);
    }

    #[inline]
    pub fn reset_to_skewing(&mut self, x: f64, y: f64) {
        unsafe { ffi::blMatrix2DSetSkewing(self as *mut _ as *mut _, x, y) };
    }

    #[inline]
    pub fn reset_to_skewing_point<P: Point>(&mut self, p: &P) {
        let p = p.into_f64();
        self.reset_to_skewing(p[0], p[1]);
    }

    #[inline]
    pub fn reset_to_sin_cos(&mut self, sin: f64, cos: f64, tx: f64, ty: f64) {
        *self = Matrix2D([cos, sin, -sin, cos, tx, ty])
    }

    #[inline]
    pub fn reset_to_sin_cos_point<P: Point>(&mut self, sin: f64, cos: f64, t: &P) {
        let t = t.into_f64();
        self.reset_to_sin_cos(sin, cos, t[0], t[1]);
    }

    #[inline]
    pub fn reset_to_rotation(&mut self, angle: f64, x: f64, y: f64) {
        unsafe { ffi::blMatrix2DSetRotation(self as *mut _ as *mut _, angle, x, y) };
    }

    #[inline]
    pub fn reset_to_rotation_point<P: Point>(&mut self, angle: f64, p: &P) {
        let p = p.into_f64();
        self.reset_to_rotation(angle, p[0], p[1]);
    }
}

impl MatrixTransform for Matrix2D {
    #[inline]
    #[doc(hidden)]
    fn apply_matrix_op(&mut self, op: Matrix2DOp, data: &[f64]) -> Result<()> {
        unsafe {
            errcode_to_result(ffi::blMatrix2DApplyOp(
                self as *mut _ as *mut _,
                op as u32,
                data.as_ptr() as *const _,
            ))
        }
    }
}

pub trait MatrixTransform {
    #[doc(hidden)]
    fn apply_matrix_op(&mut self, op: Matrix2DOp, data: &[f64]) -> Result<()>;

    #[inline]
    fn set_matrix(&mut self, m: &Matrix2D) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::Assign, &m.0)
    }

    #[inline]
    fn reset_matrix(&mut self) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::Reset, &[])
    }

    #[inline]
    fn translate(&mut self, x: f64, y: f64) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::Translate, &[x, y])
    }

    #[inline]
    fn translate_point<P: Point>(&mut self, p: &P) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::Translate, &p.into_f64())
    }

    #[inline]
    fn scale(&mut self, x: f64, y: f64) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::Scale, &[x, y])
    }

    #[inline]
    fn scale_point<P: Point>(&mut self, p: &P) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::Scale, &p.into_f64())
    }

    #[inline]
    fn skew(&mut self, x: f64, y: f64) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::Skew, &[x, y])
    }

    #[inline]
    fn skew_point<P: Point>(&mut self, p: &P) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::Skew, &p.into_f64())
    }

    #[inline]
    fn rotate(&mut self, angle: f64) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::Rotate, &[angle])
    }

    #[inline]
    fn rotate_around(&mut self, angle: f64, x: f64, y: f64) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::RotatePoint, &[angle, x, y])
    }

    #[inline]
    fn rotate_around_point<P: Point>(&mut self, angle: f64, p: &P) -> Result<()> {
        let arr = p.into_f64();
        self.apply_matrix_op(Matrix2DOp::RotatePoint, &[angle, arr[0], arr[1]])
    }

    #[inline]
    fn transform(&mut self, mat: &Matrix2D) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::Transform, &mat.0)
    }

    #[inline]
    fn post_translate(&mut self, x: f64, y: f64) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::PostTranslate, &[x, y])
    }

    #[inline]
    fn post_translate_point<P: Point>(&mut self, p: &P) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::PostTranslate, &p.into_f64())
    }

    #[inline]
    fn post_scale(&mut self, x: f64, y: f64) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::PostScale, &[x, y])
    }

    #[inline]
    fn post_scale_point<P: Point>(&mut self, p: &P) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::PostScale, &p.into_f64())
    }

    #[inline]
    fn post_skew(&mut self, x: f64, y: f64) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::PostSkew, &[x, y])
    }

    #[inline]
    fn post_skew_point<P: Point>(&mut self, p: &P) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::PostSkew, &p.into_f64())
    }

    #[inline]
    fn post_rotate(&mut self, angle: f64) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::PostRotate, &[angle])
    }

    #[inline]
    fn post_rotate_around(&mut self, angle: f64, x: f64, y: f64) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::PostRotatePoint, &[angle, x, y])
    }

    #[inline]
    fn post_rotate_around_point<P: Point>(&mut self, angle: f64, p: &P) -> Result<()> {
        let arr = p.into_f64();
        self.apply_matrix_op(Matrix2DOp::PostRotatePoint, &[angle, arr[0], arr[1]])
    }

    #[inline]
    fn post_transform(&mut self, mat: &Matrix2D) -> Result<()> {
        self.apply_matrix_op(Matrix2DOp::PostTransform, &mat.0)
    }
}