2020-01-08 15:40:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkM44_DEFINED
|
|
|
|
#define SkM44_DEFINED
|
|
|
|
|
2020-01-22 19:31:21 +00:00
|
|
|
#include "include/core/SkMatrix.h"
|
2020-01-08 15:40:39 +00:00
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
|
2020-01-16 03:26:17 +00:00
|
|
|
struct SkV3 {
|
|
|
|
float x, y, z;
|
|
|
|
|
|
|
|
bool operator==(const SkV3& v) const {
|
|
|
|
return x == v.x && y == v.y && z == v.z;
|
|
|
|
}
|
2020-01-22 19:31:21 +00:00
|
|
|
bool operator!=(const SkV3& v) const { return !(*this == v); }
|
2020-01-16 03:26:17 +00:00
|
|
|
|
|
|
|
static SkScalar Dot(const SkV3& a, const SkV3& b) { return a.x*b.x + a.y*b.y + a.z*b.z; }
|
|
|
|
static SkV3 Cross(const SkV3& a, const SkV3& b) {
|
|
|
|
return { a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x };
|
|
|
|
}
|
|
|
|
|
2020-01-16 18:58:22 +00:00
|
|
|
SkV3 operator-() const { return {-x, -y, -z}; }
|
2020-01-16 03:26:17 +00:00
|
|
|
SkV3 operator+(const SkV3& v) const { return { x + v.x, y + v.y, z + v.z }; }
|
|
|
|
SkV3 operator-(const SkV3& v) const { return { x - v.x, y - v.y, z - v.z }; }
|
|
|
|
|
2020-01-23 14:56:45 +00:00
|
|
|
SkV3 operator*(const SkV3& v) const {
|
|
|
|
return { x*v.x, y*v.y, z*v.z };
|
|
|
|
}
|
2020-01-16 03:26:17 +00:00
|
|
|
friend SkV3 operator*(const SkV3& v, SkScalar s) {
|
|
|
|
return { v.x*s, v.y*s, v.z*s };
|
|
|
|
}
|
|
|
|
friend SkV3 operator*(SkScalar s, const SkV3& v) { return v*s; }
|
|
|
|
|
|
|
|
SkScalar lengthSquared() const { return Dot(*this, *this); }
|
|
|
|
SkScalar length() const { return SkScalarSqrt(Dot(*this, *this)); }
|
2020-01-22 15:06:26 +00:00
|
|
|
|
|
|
|
SkScalar dot(const SkV3& v) const { return Dot(*this, v); }
|
|
|
|
SkV3 cross(const SkV3& v) const { return Cross(*this, v); }
|
2020-01-23 14:56:45 +00:00
|
|
|
|
2020-01-24 20:26:44 +00:00
|
|
|
const float* ptr() const { return &x; }
|
|
|
|
float* ptr() { return &x; }
|
2020-01-16 03:26:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SkV4 {
|
|
|
|
float x, y, z, w;
|
|
|
|
|
|
|
|
bool operator==(const SkV4& v) const {
|
|
|
|
return x == v.x && y == v.y && z == v.z && w == v.w;
|
|
|
|
}
|
2020-01-22 19:31:21 +00:00
|
|
|
bool operator!=(const SkV4& v) const { return !(*this == v); }
|
|
|
|
|
|
|
|
SkV4 operator-() const { return {-x, -y, -z, -w}; }
|
|
|
|
SkV4 operator+(const SkV4& v) const { return { x + v.x, y + v.y, z + v.z, w + v.w }; }
|
|
|
|
SkV4 operator-(const SkV4& v) const { return { x - v.x, y - v.y, z - v.z, w - v.w }; }
|
|
|
|
|
2020-01-23 14:56:45 +00:00
|
|
|
SkV4 operator*(const SkV4& v) const {
|
|
|
|
return { x*v.x, y*v.y, z*v.z, w*v.w };
|
|
|
|
}
|
|
|
|
friend SkV4 operator*(const SkV4& v, SkScalar s) {
|
|
|
|
return { v.x*s, v.y*s, v.z*s, v.w*s };
|
|
|
|
}
|
|
|
|
friend SkV4 operator*(SkScalar s, const SkV4& v) { return v*s; }
|
|
|
|
|
2020-01-24 20:26:44 +00:00
|
|
|
const float* ptr() const { return &x; }
|
|
|
|
float* ptr() { return &x; }
|
2020-01-16 03:26:17 +00:00
|
|
|
};
|
|
|
|
|
2020-01-24 20:26:44 +00:00
|
|
|
/**
|
|
|
|
* 4x4 matrix used by SkCanvas and other parts of Skia.
|
|
|
|
*
|
|
|
|
* Skia assumes a right-handed coordinate system:
|
|
|
|
* +X goes to the right
|
|
|
|
* +Y goes down
|
|
|
|
* +Z goes into the screen (away from the viewer)
|
|
|
|
*/
|
2020-01-08 15:40:39 +00:00
|
|
|
class SkM44 {
|
|
|
|
public:
|
|
|
|
SkM44(const SkM44& src) = default;
|
|
|
|
SkM44& operator=(const SkM44& src) = default;
|
|
|
|
|
|
|
|
constexpr SkM44()
|
|
|
|
: fMat{1, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
0, 0, 0, 1}
|
|
|
|
{}
|
|
|
|
|
|
|
|
SkM44(const SkM44& a, const SkM44& b) {
|
|
|
|
this->setConcat(a, b);
|
|
|
|
}
|
|
|
|
|
2020-01-21 01:18:14 +00:00
|
|
|
enum Uninitialized_Constructor {
|
|
|
|
kUninitialized_Constructor
|
|
|
|
};
|
Revert "use SkM44 internally"
This reverts commit 295cdf8775791abdcdcc9e098d31f3e40780c805.
Reason for revert: wacky gm colors, must have busted colormatrixfilter
Original change's description:
> use SkM44 internally
>
> Today we use SkM44 in canvas, and SkMatrix44 in sksl (and colormatrix).
> This CL tries to move core to use a single type.
>
> SkMatrix44 has callers in android and chrome, is loaded with double/float
> variants in its API. I am suggesting moving to a private, clean-start,
> API for internal use. SkM44 is much faster, and has a leaner API.
>
> If eventually we can migrate clients to a shared impl/api, great. For now,
> I want to have one we are free to optimize/use as we see fit without
> worrying about changing client results.
>
> Change-Id: Id782ac1cc8b8d7f6621970e44e1f9729964d2a94
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/265299
> Commit-Queue: Mike Reed <reed@google.com>
> Reviewed-by: Brian Salomon <bsalomon@google.com>
TBR=mtklein@google.com,bsalomon@google.com,brianosman@google.com,fmalita@chromium.org,reed@google.com
Change-Id: I35fcd636f1b57001bb65684e78523b6a94cfebee
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/265764
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
2020-01-22 18:05:16 +00:00
|
|
|
SkM44(Uninitialized_Constructor) {}
|
2020-01-22 17:00:38 +00:00
|
|
|
|
2020-01-22 19:31:21 +00:00
|
|
|
enum NaN_Constructor {
|
|
|
|
kNaN_Constructor
|
|
|
|
};
|
|
|
|
SkM44(NaN_Constructor)
|
|
|
|
: fMat{SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN,
|
|
|
|
SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN,
|
|
|
|
SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN,
|
|
|
|
SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN}
|
|
|
|
{}
|
|
|
|
|
2020-01-08 15:40:39 +00:00
|
|
|
/**
|
|
|
|
* Parameters are treated as row-major.
|
|
|
|
*/
|
|
|
|
SkM44(SkScalar m0, SkScalar m4, SkScalar m8, SkScalar m12,
|
|
|
|
SkScalar m1, SkScalar m5, SkScalar m9, SkScalar m13,
|
|
|
|
SkScalar m2, SkScalar m6, SkScalar m10, SkScalar m14,
|
|
|
|
SkScalar m3, SkScalar m7, SkScalar m11, SkScalar m15)
|
|
|
|
{
|
2020-01-23 16:06:20 +00:00
|
|
|
this->set44(m0, m4, m8, m12,
|
|
|
|
m1, m5, m9, m13,
|
|
|
|
m2, m6, m10, m14,
|
|
|
|
m3, m7, m11, m15);
|
2020-01-08 15:40:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 14:56:45 +00:00
|
|
|
static SkM44 Rows(const SkV4& r0, const SkV4& r1, const SkV4& r2, const SkV4& r3) {
|
|
|
|
SkM44 m(kUninitialized_Constructor);
|
|
|
|
m.setRow(0, r0);
|
|
|
|
m.setRow(1, r1);
|
|
|
|
m.setRow(2, r2);
|
|
|
|
m.setRow(3, r3);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
static SkM44 Cols(const SkV4& c0, const SkV4& c1, const SkV4& c2, const SkV4& c3) {
|
|
|
|
SkM44 m(kUninitialized_Constructor);
|
|
|
|
m.setCol(0, c0);
|
|
|
|
m.setCol(1, c1);
|
|
|
|
m.setCol(2, c2);
|
|
|
|
m.setCol(3, c3);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:40:39 +00:00
|
|
|
static SkM44 Translate(SkScalar x, SkScalar y, SkScalar z = 0) {
|
|
|
|
return SkM44(1, 0, 0, x,
|
|
|
|
0, 1, 0, y,
|
|
|
|
0, 0, 1, z,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkM44 Scale(SkScalar x, SkScalar y, SkScalar z = 1) {
|
|
|
|
return SkM44(x, 0, 0, 0,
|
|
|
|
0, y, 0, 0,
|
|
|
|
0, 0, z, 0,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:26:44 +00:00
|
|
|
static SkM44 Rotate(SkV3 axis, SkScalar radians) {
|
|
|
|
SkM44 m(kUninitialized_Constructor);
|
|
|
|
m.setRotate(axis, radians);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:40:39 +00:00
|
|
|
bool operator==(const SkM44& other) const;
|
|
|
|
bool operator!=(const SkM44& other) const {
|
|
|
|
return !(other == *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getColMajor(SkScalar v[]) const {
|
|
|
|
memcpy(v, fMat, sizeof(fMat));
|
|
|
|
}
|
|
|
|
void getRowMajor(SkScalar v[]) const;
|
|
|
|
|
|
|
|
SkM44& setColMajor(const SkScalar v[]) {
|
|
|
|
memcpy(fMat, v, sizeof(fMat));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
SkM44& setRowMajor(const SkScalar v[]);
|
|
|
|
|
2020-01-23 16:06:20 +00:00
|
|
|
/* Parameters in same order as constructor.
|
2020-01-08 15:40:39 +00:00
|
|
|
*/
|
2020-01-23 16:06:20 +00:00
|
|
|
SkM44& set44(SkScalar m0, SkScalar m4, SkScalar m8, SkScalar m12,
|
|
|
|
SkScalar m1, SkScalar m5, SkScalar m9, SkScalar m13,
|
|
|
|
SkScalar m2, SkScalar m6, SkScalar m10, SkScalar m14,
|
|
|
|
SkScalar m3, SkScalar m7, SkScalar m11, SkScalar m15) {
|
2020-01-08 15:40:39 +00:00
|
|
|
fMat[0] = m0; fMat[4] = m4; fMat[8] = m8; fMat[12] = m12;
|
|
|
|
fMat[1] = m1; fMat[5] = m5; fMat[9] = m9; fMat[13] = m13;
|
|
|
|
fMat[2] = m2; fMat[6] = m6; fMat[10] = m10; fMat[14] = m14;
|
|
|
|
fMat[3] = m3; fMat[7] = m7; fMat[11] = m11; fMat[15] = m15;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-01-23 16:06:20 +00:00
|
|
|
SkScalar rc(int r, int c) const {
|
|
|
|
SkASSERT(r >= 0 && r <= 3);
|
|
|
|
SkASSERT(c >= 0 && c <= 3);
|
|
|
|
return fMat[c*4 + r];
|
|
|
|
}
|
|
|
|
void setRC(int r, int c, SkScalar value) {
|
|
|
|
SkASSERT(r >= 0 && r <= 3);
|
|
|
|
SkASSERT(c >= 0 && c <= 3);
|
|
|
|
fMat[c*4 + r] = value;
|
2020-01-08 15:40:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 14:56:45 +00:00
|
|
|
SkV4 row(int i) const {
|
|
|
|
SkASSERT(i >= 0 && i <= 3);
|
|
|
|
return {fMat[i + 0], fMat[i + 4], fMat[i + 8], fMat[i + 12]};
|
|
|
|
}
|
|
|
|
SkV4 col(int i) const {
|
|
|
|
SkASSERT(i >= 0 && i <= 3);
|
|
|
|
return {fMat[i*4 + 0], fMat[i*4 + 1], fMat[i*4 + 2], fMat[i*4 + 3]};
|
|
|
|
}
|
|
|
|
|
|
|
|
void setRow(int i, const SkV4& v) {
|
|
|
|
SkASSERT(i >= 0 && i <= 3);
|
|
|
|
fMat[i + 0] = v.x;
|
|
|
|
fMat[i + 4] = v.y;
|
|
|
|
fMat[i + 8] = v.z;
|
|
|
|
fMat[i + 12] = v.w;
|
|
|
|
}
|
|
|
|
void setCol(int i, const SkV4& v) {
|
|
|
|
SkASSERT(i >= 0 && i <= 3);
|
2020-01-24 20:26:44 +00:00
|
|
|
memcpy(&fMat[i*4], v.ptr(), sizeof(v));
|
2020-01-23 14:56:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 15:40:39 +00:00
|
|
|
SkM44& setIdentity() {
|
2020-01-23 16:06:20 +00:00
|
|
|
return this->set44(1, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
0, 0, 0, 1);
|
2020-01-08 15:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkM44& setTranslate(SkScalar x, SkScalar y, SkScalar z = 0) {
|
2020-01-23 16:06:20 +00:00
|
|
|
return this->set44(1, 0, 0, x,
|
|
|
|
0, 1, 0, y,
|
|
|
|
0, 0, 1, z,
|
|
|
|
0, 0, 0, 1);
|
2020-01-08 15:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkM44& setScale(SkScalar x, SkScalar y, SkScalar z = 1) {
|
2020-01-23 16:06:20 +00:00
|
|
|
return this->set44(x, 0, 0, 0,
|
|
|
|
0, y, 0, 0,
|
|
|
|
0, 0, z, 0,
|
|
|
|
0, 0, 0, 1);
|
2020-01-08 15:40:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 20:26:44 +00:00
|
|
|
/**
|
|
|
|
* Set this matrix to rotate about the specified unit-length axis vector,
|
|
|
|
* by an angle specified by its sin() and cos().
|
|
|
|
*
|
|
|
|
* This does not attempt to verify that axis.length() == 1 or that the sin,cos values
|
|
|
|
* are correct.
|
|
|
|
*/
|
|
|
|
SkM44& setRotateUnitSinCos(SkV3 axis, SkScalar sinAngle, SkScalar cosAngle);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this matrix to rotate about the specified unit-length axis vector,
|
|
|
|
* by an angle specified in radians.
|
|
|
|
*
|
|
|
|
* This does not attempt to verify that axis.length() == 1.
|
|
|
|
*/
|
|
|
|
SkM44& setRotateUnit(SkV3 axis, SkScalar radians) {
|
|
|
|
return this->setRotateUnitSinCos(axis, SkScalarSin(radians), SkScalarCos(radians));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this matrix to rotate about the specified axis vector,
|
|
|
|
* by an angle specified in radians.
|
|
|
|
*
|
|
|
|
* Note: axis is not assumed to be unit-length, so it will be normalized internally.
|
|
|
|
* If axis is already unit-length, call setRotateAboutUnitRadians() instead.
|
|
|
|
*/
|
|
|
|
SkM44& setRotate(SkV3 axis, SkScalar radians);
|
|
|
|
|
2020-01-16 18:58:22 +00:00
|
|
|
SkM44& setConcat16(const SkM44& a, const SkScalar colMajor[16]);
|
2020-01-08 22:33:04 +00:00
|
|
|
|
|
|
|
SkM44& setConcat(const SkM44& a, const SkM44& b) {
|
2020-01-16 18:58:22 +00:00
|
|
|
return this->setConcat16(a, b.fMat);
|
2020-01-08 22:33:04 +00:00
|
|
|
}
|
2020-01-08 15:40:39 +00:00
|
|
|
|
|
|
|
friend SkM44 operator*(const SkM44& a, const SkM44& b) {
|
|
|
|
return SkM44(a, b);
|
|
|
|
}
|
|
|
|
|
2020-01-16 18:58:22 +00:00
|
|
|
SkM44& preConcat16(const SkScalar colMajor[16]) {
|
|
|
|
return this->setConcat16(*this, colMajor);
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:40:39 +00:00
|
|
|
/** If this is invertible, return that in inverse and return true. If it is
|
|
|
|
* not invertible, return false and leave the inverse parameter unchanged.
|
|
|
|
*/
|
|
|
|
bool invert(SkM44* inverse) const;
|
|
|
|
|
2020-01-21 01:18:14 +00:00
|
|
|
SkM44 transpose() const;
|
|
|
|
|
2020-01-08 15:40:39 +00:00
|
|
|
void dump() const;
|
|
|
|
|
2020-01-16 03:26:17 +00:00
|
|
|
////////////
|
|
|
|
|
|
|
|
SkV4 map(float x, float y, float z, float w) const;
|
|
|
|
SkV4 operator*(const SkV4& v) const {
|
2020-01-17 22:35:04 +00:00
|
|
|
return this->map(v.x, v.y, v.z, v.w);
|
2020-01-16 03:26:17 +00:00
|
|
|
}
|
2020-01-24 20:26:44 +00:00
|
|
|
SkV3 operator*(SkV3 v) const {
|
2020-01-16 03:26:17 +00:00
|
|
|
auto v4 = this->map(v.x, v.y, v.z, 0);
|
|
|
|
return {v4.x, v4.y, v4.z};
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:40:39 +00:00
|
|
|
////////////////////// Converting to/from SkMatrix
|
|
|
|
|
|
|
|
/* When converting from SkM44 to SkMatrix, the third row and
|
|
|
|
* column is dropped. When converting from SkMatrix to SkM44
|
|
|
|
* the third row and column remain as identity:
|
|
|
|
* [ a b c ] [ a b 0 c ]
|
|
|
|
* [ d e f ] -> [ d e 0 f ]
|
|
|
|
* [ g h i ] [ 0 0 1 0 ]
|
|
|
|
* [ g h 0 i ]
|
|
|
|
*/
|
|
|
|
SkMatrix asM33() const {
|
|
|
|
return SkMatrix::MakeAll(fMat[0], fMat[4], fMat[12],
|
|
|
|
fMat[1], fMat[5], fMat[13],
|
|
|
|
fMat[3], fMat[7], fMat[15]);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkM44(const SkMatrix& src)
|
|
|
|
: SkM44(src[SkMatrix::kMScaleX], src[SkMatrix::kMSkewX], 0, src[SkMatrix::kMTransX],
|
|
|
|
src[SkMatrix::kMSkewY], src[SkMatrix::kMScaleY], 0, src[SkMatrix::kMTransY],
|
|
|
|
0, 0, 1, 0,
|
|
|
|
src[SkMatrix::kMPersp0], src[SkMatrix::kMPersp1], 0, src[SkMatrix::kMPersp2])
|
|
|
|
{}
|
|
|
|
|
|
|
|
SkM44& operator=(const SkMatrix& src) {
|
|
|
|
*this = SkM44(src);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkM44& preTranslate(SkScalar x, SkScalar y);
|
2020-01-13 14:40:56 +00:00
|
|
|
SkM44& preScale(SkScalar x, SkScalar y);
|
2020-01-08 15:40:39 +00:00
|
|
|
SkM44& preConcat(const SkMatrix&);
|
|
|
|
|
|
|
|
private:
|
|
|
|
/* Stored in column-major.
|
|
|
|
* Indices
|
|
|
|
* 0 4 8 12 1 0 0 trans_x
|
|
|
|
* 1 5 9 13 e.g. 0 1 0 trans_y
|
|
|
|
* 2 6 10 14 0 0 1 trans_z
|
|
|
|
* 3 7 11 15 0 0 0 1
|
|
|
|
*/
|
|
|
|
SkScalar fMat[16];
|
|
|
|
|
|
|
|
double determinant() const;
|
2020-01-23 16:06:20 +00:00
|
|
|
|
|
|
|
friend class SkMatrixPriv;
|
2020-01-08 15:40:39 +00:00
|
|
|
};
|
|
|
|
|
2020-01-25 23:42:23 +00:00
|
|
|
SkM44 Sk3LookAt(const SkV3& eye, const SkV3& center, const SkV3& up);
|
|
|
|
SkM44 Sk3Perspective(float near, float far, float angle);
|
|
|
|
|
2020-01-08 15:40:39 +00:00
|
|
|
#endif
|