1 module skia.SKMatrix44; 2 3 import skia.Definitions; 4 import skia.MathTypes; 5 import skia.SkiaApi; 6 import skia.SKObject; 7 import skia.Exceptions; 8 import skia.SKMatrix; 9 10 class SKMatrix44 : SKObject 11 { 12 this (void* x, bool owns) 13 { 14 super(x, owns); 15 } 16 17 protected override void Dispose (bool disposing) 18 { 19 return super.Dispose (disposing); 20 } 21 22 override void Dispose() 23 { 24 return super.Dispose(); 25 } 26 27 28 protected override void DisposeNative () 29 { 30 return SkiaApi.sk_matrix44_destroy (cast(sk_matrix44_t*)Handle); 31 } 32 33 34 this () 35 { 36 super (SkiaApi.sk_matrix44_new (), true); 37 if (Handle is null) 38 throw new InvalidOperationException ("Unable to create a new SKMatrix44 instance."); 39 } 40 41 this (SKMatrix44 src) 42 { 43 this (null, true); 44 if (src is null) 45 throw new ArgumentNullException (src.stringof); 46 47 Handle = SkiaApi.sk_matrix44_new_copy (cast(sk_matrix44_t*)src.Handle); 48 49 if (Handle is null) 50 throw new InvalidOperationException ("Unable to create a new SKMatrix44 instance."); 51 } 52 53 this (SKMatrix44 a, SKMatrix44 b) 54 { 55 this (null, true); 56 if (a is null) 57 throw new ArgumentNullException (a.stringof); 58 if (b is null) 59 throw new ArgumentNullException (b.stringof); 60 61 Handle = SkiaApi.sk_matrix44_new_concat (cast(sk_matrix44_t*)a.Handle, cast(sk_matrix44_t*)b.Handle); 62 63 if (Handle is null) 64 throw new InvalidOperationException ("Unable to create a new SKMatrix44 instance."); 65 } 66 67 this (SKMatrix src) 68 { 69 this (SkiaApi.sk_matrix44_new_matrix (&src), true); 70 if (Handle is null) 71 throw new InvalidOperationException ("Unable to create a new SKMatrix44 instance."); 72 } 73 74 // properties 75 76 SKMatrix Matrix() { 77 SKMatrix matrix; 78 SkiaApi.sk_matrix44_to_matrix (cast(sk_matrix44_t*)Handle, &matrix); 79 return matrix; 80 } 81 82 SKMatrix44TypeMask Type() 83 { 84 return SkiaApi.sk_matrix44_get_type (cast(sk_matrix44_t*)Handle); 85 } 86 87 88 // float this[int row, int column] { 89 // get => SkiaApi.sk_matrix44_get (cast(sk_matrix44_t*)Handle, row, column); 90 // set => SkiaApi.sk_matrix44_set (cast(sk_matrix44_t*)Handle, row, column, value); 91 // } 92 93 // Create* 94 95 static SKMatrix44 CreateIdentity () 96 { 97 auto matrix = new SKMatrix44 (); 98 matrix.SetIdentity (); 99 return matrix; 100 } 101 static SKMatrix44 CreateTranslate (float x, float y, float z) 102 { 103 return CreateTranslation (x, y, z); 104 } 105 106 107 static SKMatrix44 CreateTranslation (float x, float y, float z) 108 { 109 auto matrix = new SKMatrix44 (); 110 matrix.SetTranslate (x, y, z); 111 return matrix; 112 } 113 114 static SKMatrix44 CreateScale (float x, float y, float z) 115 { 116 auto matrix = new SKMatrix44 (); 117 matrix.SetScale (x, y, z); 118 return matrix; 119 } 120 121 static SKMatrix44 CreateRotation (float x, float y, float z, float radians) 122 { 123 auto matrix = new SKMatrix44 (); 124 matrix.SetRotationAbout (x, y, z, radians); 125 return matrix; 126 } 127 128 static SKMatrix44 CreateRotationDegrees (float x, float y, float z, float degrees) 129 { 130 auto matrix = new SKMatrix44 (); 131 matrix.SetRotationAboutDegrees (x, y, z, degrees); 132 return matrix; 133 } 134 135 // From 136 137 static SKMatrix44 FromRowMajor (float[] src) 138 { 139 auto matrix = new SKMatrix44 (); 140 matrix.SetRowMajor (src); 141 return matrix; 142 } 143 144 static SKMatrix44 FromColumnMajor (float[] src) 145 { 146 auto matrix = new SKMatrix44 (); 147 matrix.SetColumnMajor (src); 148 return matrix; 149 } 150 151 // To* 152 153 float[] ToColumnMajor () 154 { 155 auto dst = new float[16]; 156 ToColumnMajor (dst); 157 return dst; 158 } 159 160 void ToColumnMajor (float[] dst) 161 { 162 if (dst is null) 163 throw new ArgumentNullException (dst.stringof); 164 if (dst.length != 16) 165 throw new ArgumentException ("The destination array must be 16 entries.", dst.stringof); 166 167 float* d = dst.ptr; 168 SkiaApi.sk_matrix44_as_col_major (cast(sk_matrix44_t*)Handle, d); 169 } 170 171 float[] ToRowMajor () 172 { 173 auto dst = new float[16]; 174 ToRowMajor (dst); 175 return dst; 176 } 177 178 void ToRowMajor (float[] dst) 179 { 180 if (dst is null) 181 throw new ArgumentNullException (dst.stringof); 182 if (dst.length != 16) 183 throw new ArgumentException ("The destination array must be 16 entries.", dst.stringof); 184 185 float* d = dst.ptr; 186 SkiaApi.sk_matrix44_as_row_major (cast(sk_matrix44_t*)Handle, d); 187 } 188 189 // Equal 190 191 static bool Equal (SKMatrix44 left, SKMatrix44 right) 192 { 193 if (left is null) 194 throw new ArgumentNullException (left.stringof); 195 if (right is null) 196 throw new ArgumentNullException (right.stringof); 197 198 return SkiaApi.sk_matrix44_equals (cast(sk_matrix44_t*)left.Handle, cast(sk_matrix44_t*)right.Handle); 199 } 200 201 // Set* 202 203 void SetIdentity () 204 { 205 return SkiaApi.sk_matrix44_set_identity (cast(sk_matrix44_t*)Handle); 206 } 207 208 209 void SetColumnMajor (float[] src) 210 { 211 if (src is null) 212 throw new ArgumentNullException (src.stringof); 213 if (src.length != 16) 214 throw new ArgumentException ("The source array must be 16 entries.", src.stringof); 215 216 float* s = src.ptr; 217 SkiaApi.sk_matrix44_set_col_major (cast(sk_matrix44_t*)Handle, s); 218 } 219 220 void SetRowMajor (float[] src) 221 { 222 if (src is null) 223 throw new ArgumentNullException (src.stringof); 224 if (src.length != 16) 225 throw new ArgumentException ("The source array must be 16 entries.", src.stringof); 226 227 float* s = src.ptr; 228 SkiaApi.sk_matrix44_set_row_major (cast(sk_matrix44_t*)Handle, s); 229 } 230 231 // void Set3x3ColumnMajor (float[] src) 232 // { 233 // if (src.length != 9) 234 // throw new ArgumentException ("The source array must be 9 entries.", src.stringof); 235 236 // auto row = stackalloc float[9] { src[0], src[3], src[6], src[1], src[4], src[7], src[2], src[5], src[8] }; 237 // SkiaApi.sk_matrix44_set_3x3_row_major (cast(sk_matrix44_t*)Handle, row); 238 // } 239 240 void Set3x3RowMajor (float[] src) 241 { 242 if (src.length != 9) 243 throw new ArgumentException ("The source array must be 9 entries.", src.stringof); 244 245 float* s = src.ptr; 246 SkiaApi.sk_matrix44_set_3x3_row_major (cast(sk_matrix44_t*)Handle, s); 247 } 248 249 void SetTranslate (float dx, float dy, float dz) 250 { 251 return SkiaApi.sk_matrix44_set_translate (cast(sk_matrix44_t*)Handle, dx, dy, dz); 252 } 253 254 void SetScale (float sx, float sy, float sz) 255 { 256 return SkiaApi.sk_matrix44_set_scale (cast(sk_matrix44_t*)Handle, sx, sy, sz); 257 } 258 259 void SetRotationAboutDegrees (float x, float y, float z, float degrees) 260 { 261 return SkiaApi.sk_matrix44_set_rotate_about_degrees (cast(sk_matrix44_t*)Handle, x, y, z, degrees); 262 } 263 264 void SetRotationAbout (float x, float y, float z, float radians) 265 { 266 return SkiaApi.sk_matrix44_set_rotate_about_radians (cast(sk_matrix44_t*)Handle, x, y, z, radians); 267 } 268 269 void SetRotationAboutUnit (float x, float y, float z, float radians) 270 { 271 return SkiaApi.sk_matrix44_set_rotate_about_radians_unit (cast(sk_matrix44_t*)Handle, x, y, z, radians); 272 } 273 274 void SetConcat (SKMatrix44 a, SKMatrix44 b) 275 { 276 if (a is null) 277 throw new ArgumentNullException (a.stringof); 278 if (b is null) 279 throw new ArgumentNullException (b.stringof); 280 281 SkiaApi.sk_matrix44_set_concat (cast(sk_matrix44_t*)Handle, cast(sk_matrix44_t*)a.Handle, cast(sk_matrix44_t*)b.Handle); 282 } 283 284 // Pre* / Post* 285 286 void PreTranslate (float dx, float dy, float dz) 287 { 288 return SkiaApi.sk_matrix44_pre_translate (cast(sk_matrix44_t*)Handle, dx, dy, dz); 289 } 290 291 void PostTranslate (float dx, float dy, float dz) 292 { 293 return SkiaApi.sk_matrix44_post_translate (cast(sk_matrix44_t*)Handle, dx, dy, dz); 294 } 295 296 void PreScale (float sx, float sy, float sz) 297 { 298 return SkiaApi.sk_matrix44_pre_scale (cast(sk_matrix44_t*)Handle, sx, sy, sz); 299 } 300 301 void PostScale (float sx, float sy, float sz) 302 { 303 return SkiaApi.sk_matrix44_post_scale (cast(sk_matrix44_t*)Handle, sx, sy, sz); 304 } 305 306 void PreConcat (SKMatrix44 m) 307 { 308 if (m is null) 309 throw new ArgumentNullException (m.stringof); 310 311 SkiaApi.sk_matrix44_pre_concat (cast(sk_matrix44_t*)Handle, cast(sk_matrix44_t*)m.Handle); 312 } 313 314 void PostConcat (SKMatrix44 m) 315 { 316 if (m is null) 317 throw new ArgumentNullException (m.stringof); 318 319 SkiaApi.sk_matrix44_post_concat (cast(sk_matrix44_t*)Handle, cast(sk_matrix44_t*)m.Handle); 320 } 321 322 // Invert 323 324 bool IsInvertible() 325 { 326 return SkiaApi.sk_matrix44_invert (cast(sk_matrix44_t*)Handle, null); 327 } 328 329 330 SKMatrix44 Invert () 331 { 332 SKMatrix44 inverse = new SKMatrix44 (); 333 if (!Invert (inverse)) { 334 inverse.Dispose (); 335 inverse = null; 336 } 337 return inverse; 338 } 339 340 bool Invert (SKMatrix44 inverse) 341 { 342 if (inverse is null) 343 throw new ArgumentNullException (inverse.stringof); 344 345 return SkiaApi.sk_matrix44_invert (cast(sk_matrix44_t*)Handle, cast(sk_matrix44_t*)inverse.Handle); 346 } 347 348 // Transpose 349 350 void Transpose () 351 { 352 return SkiaApi.sk_matrix44_transpose (cast(sk_matrix44_t*)Handle); 353 } 354 355 356 // MapScalars 357 358 // float[] MapScalars (float x, float y, float z, float w) 359 // { 360 // auto srcVector4 = new float[4] { x, y, z, w }; 361 // auto dstVector4 = new float[4]; 362 // MapScalars (srcVector4, dstVector4); 363 // return dstVector4; 364 // } 365 366 float[] MapScalars (float[] srcVector4) 367 { 368 auto dstVector4 = new float[4]; 369 MapScalars (srcVector4, dstVector4); 370 return dstVector4; 371 } 372 373 void MapScalars (float[] srcVector4, float[] dstVector4) 374 { 375 if (srcVector4 is null) 376 throw new ArgumentNullException (srcVector4.stringof); 377 if (srcVector4.length != 4) 378 throw new ArgumentException ("The source vector array must be 4 entries.", srcVector4.stringof); 379 if (dstVector4 is null) 380 throw new ArgumentNullException (dstVector4.stringof); 381 if (dstVector4.length != 4) 382 throw new ArgumentException ("The destination vector array must be 4 entries.", dstVector4.stringof); 383 384 float* s = srcVector4.ptr; 385 float* d = dstVector4.ptr; 386 SkiaApi.sk_matrix44_map_scalars (cast(sk_matrix44_t*)Handle, s, d); 387 } 388 389 // MapPoints 390 391 // SKPoint MapPoint (SKPoint src) 392 // { 393 // return MapPoints (new[] { src })[0]; 394 // } 395 396 397 SKPoint[] MapPoints (SKPoint[] src) 398 { 399 if (src is null) 400 throw new ArgumentNullException (src.stringof); 401 402 auto count = src.length; 403 auto src2Length = count * 2; 404 //var src4Length = count * 4; 405 406 auto src2 = new float[src2Length]; 407 for (int i = 0, i2 = 0; i < count; i++, i2 += 2) { 408 src2[i2] = src[i].X; 409 src2[i2 + 1] = src[i].Y; 410 } 411 412 auto dst4 = MapVector2 (src2); 413 414 auto dst = new SKPoint[count]; 415 for (int i = 0, i4 = 0; i < count; i++, i4 += 4) { 416 dst[i].X = dst4[i4]; 417 dst[i].Y = dst4[i4 + 1]; 418 } 419 420 return dst; 421 } 422 423 // MapVector2 424 425 float[] MapVector2 (float[] src2) 426 { 427 if (src2 is null) 428 throw new ArgumentNullException (src2.stringof); 429 if (src2.length % 2 != 0) 430 throw new ArgumentException ("The source vector array must be a set of pairs.", src2.stringof); 431 432 auto dst4 = new float[src2.length * 2]; 433 MapVector2 (src2, dst4); 434 return dst4; 435 } 436 437 void MapVector2 (float[] src2, float[] dst4) 438 { 439 if (src2 is null) 440 throw new ArgumentNullException (src2.stringof); 441 if (src2.length % 2 != 0) 442 throw new ArgumentException ("The source vector array must be a set of pairs.", src2.stringof); 443 if (dst4 is null) 444 throw new ArgumentNullException (dst4.stringof); 445 if (dst4.length % 4 != 0) 446 throw new ArgumentException ("The destination vector array must be a set quads.", dst4.stringof); 447 if (src2.length / 2 != dst4.length / 4) 448 throw new ArgumentException ("The source vector array must have the same number of pairs as the destination vector array has quads.", dst4.stringof); 449 450 float* s = src2.ptr; 451 float* d = dst4.ptr; 452 SkiaApi.sk_matrix44_map2 (cast(sk_matrix44_t*)Handle, s, cast(int)(src2.length / 2), d); 453 } 454 455 // Preserves2DAxisAlignment 456 457 bool Preserves2DAxisAlignment (float epsilon) 458 { 459 return SkiaApi.sk_matrix44_preserves_2d_axis_alignment (cast(sk_matrix44_t*)Handle, epsilon); 460 } 461 462 463 // Determinant 464 465 double Determinant () 466 { 467 return SkiaApi.sk_matrix44_determinant (cast(sk_matrix44_t*)Handle); 468 } 469 470 471 // operators 472 473 // static implicit operator SKMatrix44 (SKMatrix matrix) 474 // { 475 // return new SKMatrix44 (matrix); 476 // } 477 478 479 static SKMatrix44 GetObject (void* handle, bool owns = true) 480 { 481 482 return GetOrAddObject!(SKMatrix44)(handle, owns, delegate SKMatrix44 (h, o) { return new SKMatrix44 (h, o);}); 483 // return GetOrAddObject (handle, owns, (h, o) {return new SKMatrix44 (h, o);}); 484 } 485 486 }