move SkVec2 into shared header
Change-Id: I2b39242fe0519fa188151974dea883bad5652eb4 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272118 Reviewed-by: Mike Reed <reed@google.com> Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
parent
23823669b3
commit
e1a81bad5f
@ -11,6 +11,40 @@
|
||||
#include "include/core/SkMatrix.h"
|
||||
#include "include/core/SkScalar.h"
|
||||
|
||||
struct SkVec2 {
|
||||
SkScalar x, y;
|
||||
|
||||
bool operator==(const SkVec2 v) const { return x == v.x && y == v.y; }
|
||||
bool operator!=(const SkVec2 v) const { return !(*this == v); }
|
||||
|
||||
static SkScalar Dot(SkVec2 a, SkVec2 b) { return a.x * b.x + a.y * b.y; }
|
||||
static SkScalar Cross(SkVec2 a, SkVec2 b) { return a.x * b.y - a.y * b.x; }
|
||||
static SkVec2 Normalize(SkVec2 v) { return v * (1.0f / v.length()); }
|
||||
|
||||
SkVec2 operator-() const { return {-x, -y}; }
|
||||
SkVec2 operator+(SkVec2 v) const { return {x+v.x, y+v.y}; }
|
||||
SkVec2 operator-(SkVec2 v) const { return {x-v.x, y-v.y}; }
|
||||
|
||||
SkVec2 operator*(SkVec2 v) const { return {x*v.x, y*v.y}; }
|
||||
friend SkVec2 operator*(SkVec2 v, SkScalar s) { return {v.x*s, v.y*s}; }
|
||||
friend SkVec2 operator*(SkScalar s, SkVec2 v) { return {v.x*s, v.y*s}; }
|
||||
|
||||
void operator+=(SkVec2 v) { *this = *this + v; }
|
||||
void operator-=(SkVec2 v) { *this = *this - v; }
|
||||
void operator*=(SkVec2 v) { *this = *this * v; }
|
||||
void operator*=(SkScalar s) { *this = *this * s; }
|
||||
|
||||
SkScalar lengthSquared() const { return Dot(*this, *this); }
|
||||
SkScalar length() const { return SkScalarSqrt(this->lengthSquared()); }
|
||||
|
||||
SkScalar dot(SkVec2 v) const { return Dot(*this, v); }
|
||||
SkScalar cross(SkVec2 v) const { return Cross(*this, v); }
|
||||
SkVec2 normalize() const { return Normalize(*this); }
|
||||
|
||||
const float* ptr() const { return &x; }
|
||||
float* ptr() { return &x; }
|
||||
};
|
||||
|
||||
struct SkV3 {
|
||||
float x, y, z;
|
||||
|
||||
@ -23,6 +57,7 @@ struct SkV3 {
|
||||
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 };
|
||||
}
|
||||
static SkV3 Normalize(const SkV3& v) { return v * (1.0f / v.length()); }
|
||||
|
||||
SkV3 operator-() const { return {-x, -y, -z}; }
|
||||
SkV3 operator+(const SkV3& v) const { return { x + v.x, y + v.y, z + v.z }; }
|
||||
@ -40,7 +75,8 @@ struct SkV3 {
|
||||
SkScalar length() const { return SkScalarSqrt(Dot(*this, *this)); }
|
||||
|
||||
SkScalar dot(const SkV3& v) const { return Dot(*this, v); }
|
||||
SkV3 cross(const SkV3& v) const { return Cross(*this, v); }
|
||||
SkV3 cross(const SkV3& v) const { return Cross(*this, v); }
|
||||
SkV3 normalize() const { return Normalize(*this); }
|
||||
|
||||
const float* ptr() const { return &x; }
|
||||
float* ptr() { return &x; }
|
||||
|
@ -13,43 +13,6 @@
|
||||
#include "samplecode/Sample.h"
|
||||
#include "tools/Resources.h"
|
||||
|
||||
static SkV3 normalize(SkV3 v) { return v * (1.0f / v.length()); }
|
||||
|
||||
struct SkVec2 {
|
||||
SkScalar x, y;
|
||||
|
||||
bool operator==(const SkVec2 v) const { return x == v.x && y == v.y; }
|
||||
bool operator!=(const SkVec2 v) const { return !(*this == v); }
|
||||
|
||||
static SkScalar Dot(SkVec2 a, SkVec2 b) { return a.x * b.x + a.y * b.y; }
|
||||
static SkScalar Cross(SkVec2 a, SkVec2 b) { return a.x * b.y - a.y * b.x; }
|
||||
|
||||
SkVec2 operator-() const { return {-x, -y}; }
|
||||
SkVec2 operator+(SkVec2 v) const { return {x+v.x, y+v.y}; }
|
||||
SkVec2 operator-(SkVec2 v) const { return {x-v.x, y-v.y}; }
|
||||
|
||||
SkVec2 operator*(SkVec2 v) const { return {x*v.x, y*v.y}; }
|
||||
friend SkVec2 operator*(SkVec2 v, SkScalar s) { return {v.x*s, v.y*s}; }
|
||||
friend SkVec2 operator*(SkScalar s, SkVec2 v) { return {v.x*s, v.y*s}; }
|
||||
|
||||
void operator+=(SkVec2 v) { *this = *this + v; }
|
||||
void operator-=(SkVec2 v) { *this = *this - v; }
|
||||
void operator*=(SkVec2 v) { *this = *this * v; }
|
||||
void operator*=(SkScalar s) { *this = *this * s; }
|
||||
|
||||
SkScalar lengthSquared() const { return Dot(*this, *this); }
|
||||
SkScalar length() const { return SkScalarSqrt(this->lengthSquared()); }
|
||||
|
||||
SkScalar dot(SkVec2 v) const { return Dot(*this, v); }
|
||||
SkScalar cross(SkVec2 v) const { return Cross(*this, v); }
|
||||
};
|
||||
|
||||
static SkVec2 normalize(SkVec2 v) {
|
||||
SkScalar len = v.length();
|
||||
SkASSERT(len > 0);
|
||||
return v * (1.0f / len);
|
||||
}
|
||||
|
||||
struct VSphere {
|
||||
SkVec2 fCenter;
|
||||
SkScalar fRadius;
|
||||
@ -72,7 +35,7 @@ struct VSphere {
|
||||
v = (v - fCenter) * (1 / fRadius);
|
||||
SkScalar len2 = v.lengthSquared();
|
||||
if (len2 > 1) {
|
||||
v = normalize(v);
|
||||
v = v.normalize();
|
||||
len2 = 1;
|
||||
}
|
||||
SkScalar z = SkScalarSqrt(1 - len2);
|
||||
@ -220,7 +183,7 @@ const Face faces[] = {
|
||||
|
||||
static SkColorMatrix comput_planar_lighting(SkCanvas* canvas, SkV3 lightDir) {
|
||||
SkM44 l2w = canvas->experimental_getLocalToWorld();
|
||||
auto normal = normalize(l2w * SkV3{0, 0, 1});
|
||||
auto normal = SkV3::Normalize(l2w * SkV3{0, 0, 1});
|
||||
float dot = -normal.dot(lightDir);
|
||||
|
||||
SkColorMatrix cm;
|
||||
@ -259,7 +222,7 @@ struct Light {
|
||||
|
||||
SkV3 getDir() const {
|
||||
auto pt = fEndPt - fCenter;
|
||||
return normalize({pt.fX, pt.fY, -fHeight});
|
||||
return SkV3::Normalize({pt.fX, pt.fY, -fHeight});
|
||||
}
|
||||
|
||||
void draw(SkCanvas* canvas) {
|
||||
|
Loading…
Reference in New Issue
Block a user