Skip to content

Commit e1a81ba

Browse files
reed-at-googleSkia Commit-Bot
authored andcommitted
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>
1 parent 2382366 commit e1a81ba

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

include/private/SkM44.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,40 @@
1111
#include "include/core/SkMatrix.h"
1212
#include "include/core/SkScalar.h"
1313

14+
struct SkVec2 {
15+
SkScalar x, y;
16+
17+
bool operator==(const SkVec2 v) const { return x == v.x && y == v.y; }
18+
bool operator!=(const SkVec2 v) const { return !(*this == v); }
19+
20+
static SkScalar Dot(SkVec2 a, SkVec2 b) { return a.x * b.x + a.y * b.y; }
21+
static SkScalar Cross(SkVec2 a, SkVec2 b) { return a.x * b.y - a.y * b.x; }
22+
static SkVec2 Normalize(SkVec2 v) { return v * (1.0f / v.length()); }
23+
24+
SkVec2 operator-() const { return {-x, -y}; }
25+
SkVec2 operator+(SkVec2 v) const { return {x+v.x, y+v.y}; }
26+
SkVec2 operator-(SkVec2 v) const { return {x-v.x, y-v.y}; }
27+
28+
SkVec2 operator*(SkVec2 v) const { return {x*v.x, y*v.y}; }
29+
friend SkVec2 operator*(SkVec2 v, SkScalar s) { return {v.x*s, v.y*s}; }
30+
friend SkVec2 operator*(SkScalar s, SkVec2 v) { return {v.x*s, v.y*s}; }
31+
32+
void operator+=(SkVec2 v) { *this = *this + v; }
33+
void operator-=(SkVec2 v) { *this = *this - v; }
34+
void operator*=(SkVec2 v) { *this = *this * v; }
35+
void operator*=(SkScalar s) { *this = *this * s; }
36+
37+
SkScalar lengthSquared() const { return Dot(*this, *this); }
38+
SkScalar length() const { return SkScalarSqrt(this->lengthSquared()); }
39+
40+
SkScalar dot(SkVec2 v) const { return Dot(*this, v); }
41+
SkScalar cross(SkVec2 v) const { return Cross(*this, v); }
42+
SkVec2 normalize() const { return Normalize(*this); }
43+
44+
const float* ptr() const { return &x; }
45+
float* ptr() { return &x; }
46+
};
47+
1448
struct SkV3 {
1549
float x, y, z;
1650

@@ -23,6 +57,7 @@ struct SkV3 {
2357
static SkV3 Cross(const SkV3& a, const SkV3& b) {
2458
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 };
2559
}
60+
static SkV3 Normalize(const SkV3& v) { return v * (1.0f / v.length()); }
2661

2762
SkV3 operator-() const { return {-x, -y, -z}; }
2863
SkV3 operator+(const SkV3& v) const { return { x + v.x, y + v.y, z + v.z }; }
@@ -40,7 +75,8 @@ struct SkV3 {
4075
SkScalar length() const { return SkScalarSqrt(Dot(*this, *this)); }
4176

4277
SkScalar dot(const SkV3& v) const { return Dot(*this, v); }
43-
SkV3 cross(const SkV3& v) const { return Cross(*this, v); }
78+
SkV3 cross(const SkV3& v) const { return Cross(*this, v); }
79+
SkV3 normalize() const { return Normalize(*this); }
4480

4581
const float* ptr() const { return &x; }
4682
float* ptr() { return &x; }

samplecode/Sample3D.cpp

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,6 @@
1313
#include "samplecode/Sample.h"
1414
#include "tools/Resources.h"
1515

16-
static SkV3 normalize(SkV3 v) { return v * (1.0f / v.length()); }
17-
18-
struct SkVec2 {
19-
SkScalar x, y;
20-
21-
bool operator==(const SkVec2 v) const { return x == v.x && y == v.y; }
22-
bool operator!=(const SkVec2 v) const { return !(*this == v); }
23-
24-
static SkScalar Dot(SkVec2 a, SkVec2 b) { return a.x * b.x + a.y * b.y; }
25-
static SkScalar Cross(SkVec2 a, SkVec2 b) { return a.x * b.y - a.y * b.x; }
26-
27-
SkVec2 operator-() const { return {-x, -y}; }
28-
SkVec2 operator+(SkVec2 v) const { return {x+v.x, y+v.y}; }
29-
SkVec2 operator-(SkVec2 v) const { return {x-v.x, y-v.y}; }
30-
31-
SkVec2 operator*(SkVec2 v) const { return {x*v.x, y*v.y}; }
32-
friend SkVec2 operator*(SkVec2 v, SkScalar s) { return {v.x*s, v.y*s}; }
33-
friend SkVec2 operator*(SkScalar s, SkVec2 v) { return {v.x*s, v.y*s}; }
34-
35-
void operator+=(SkVec2 v) { *this = *this + v; }
36-
void operator-=(SkVec2 v) { *this = *this - v; }
37-
void operator*=(SkVec2 v) { *this = *this * v; }
38-
void operator*=(SkScalar s) { *this = *this * s; }
39-
40-
SkScalar lengthSquared() const { return Dot(*this, *this); }
41-
SkScalar length() const { return SkScalarSqrt(this->lengthSquared()); }
42-
43-
SkScalar dot(SkVec2 v) const { return Dot(*this, v); }
44-
SkScalar cross(SkVec2 v) const { return Cross(*this, v); }
45-
};
46-
47-
static SkVec2 normalize(SkVec2 v) {
48-
SkScalar len = v.length();
49-
SkASSERT(len > 0);
50-
return v * (1.0f / len);
51-
}
52-
5316
struct VSphere {
5417
SkVec2 fCenter;
5518
SkScalar fRadius;
@@ -72,7 +35,7 @@ struct VSphere {
7235
v = (v - fCenter) * (1 / fRadius);
7336
SkScalar len2 = v.lengthSquared();
7437
if (len2 > 1) {
75-
v = normalize(v);
38+
v = v.normalize();
7639
len2 = 1;
7740
}
7841
SkScalar z = SkScalarSqrt(1 - len2);
@@ -220,7 +183,7 @@ const Face faces[] = {
220183

221184
static SkColorMatrix comput_planar_lighting(SkCanvas* canvas, SkV3 lightDir) {
222185
SkM44 l2w = canvas->experimental_getLocalToWorld();
223-
auto normal = normalize(l2w * SkV3{0, 0, 1});
186+
auto normal = SkV3::Normalize(l2w * SkV3{0, 0, 1});
224187
float dot = -normal.dot(lightDir);
225188

226189
SkColorMatrix cm;
@@ -259,7 +222,7 @@ struct Light {
259222

260223
SkV3 getDir() const {
261224
auto pt = fEndPt - fCenter;
262-
return normalize({pt.fX, pt.fY, -fHeight});
225+
return SkV3::Normalize({pt.fX, pt.fY, -fHeight});
263226
}
264227

265228
void draw(SkCanvas* canvas) {

0 commit comments

Comments
 (0)