| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef ANDROID_HWUI_VECTOR_H |
| 18 |
#define ANDROID_HWUI_VECTOR_H |
| 19 |
|
| 20 |
#include <math.h> |
| 21 |
#include <utils/Log.h> |
| 22 |
|
| 23 |
namespace android { |
| 24 |
namespace uirenderer { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
struct Vector2 { |
| 32 |
float x; |
| 33 |
float y; |
| 34 |
|
| 35 |
float lengthSquared() const { return x * x + y * y; } |
| 36 |
|
| 37 |
float length() const { return sqrt(x * x + y * y); } |
| 38 |
|
| 39 |
void operator+=(const Vector2& v) { |
| 40 |
x += v.x; |
| 41 |
y += v.y; |
| 42 |
} |
| 43 |
|
| 44 |
void operator-=(const Vector2& v) { |
| 45 |
x -= v.x; |
| 46 |
y -= v.y; |
| 47 |
} |
| 48 |
|
| 49 |
void operator+=(const float v) { |
| 50 |
x += v; |
| 51 |
y += v; |
| 52 |
} |
| 53 |
|
| 54 |
void operator-=(const float v) { |
| 55 |
x -= v; |
| 56 |
y -= v; |
| 57 |
} |
| 58 |
|
| 59 |
void operator/=(float s) { |
| 60 |
x /= s; |
| 61 |
y /= s; |
| 62 |
} |
| 63 |
|
| 64 |
void operator*=(float s) { |
| 65 |
x *= s; |
| 66 |
y *= s; |
| 67 |
} |
| 68 |
|
| 69 |
Vector2 operator+(const Vector2& v) const { return (Vector2){x + v.x, y + v.y}; } |
| 70 |
|
| 71 |
Vector2 operator-(const Vector2& v) const { return (Vector2){x - v.x, y - v.y}; } |
| 72 |
|
| 73 |
Vector2 operator/(float s) const { return (Vector2){x / s, y / s}; } |
| 74 |
|
| 75 |
Vector2 operator*(float s) const { return (Vector2){x * s, y * s}; } |
| 76 |
|
| 77 |
void normalize() { |
| 78 |
float s = 1.0f / length(); |
| 79 |
x *= s; |
| 80 |
y *= s; |
| 81 |
} |
| 82 |
|
| 83 |
Vector2 copyNormalized() const { |
| 84 |
Vector2 v = {x, y}; |
| 85 |
v.normalize(); |
| 86 |
return v; |
| 87 |
} |
| 88 |
|
| 89 |
float dot(const Vector2& v) const { return x * v.x + y * v.y; } |
| 90 |
|
| 91 |
float cross(const Vector2& v) const { return x * v.y - y * v.x; } |
| 92 |
|
| 93 |
void dump() { ALOGD("Vector2[%.2f, %.2f]", x, y); } |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
class Vector3 { |
| 98 |
public: |
| 99 |
float x; |
| 100 |
float y; |
| 101 |
float z; |
| 102 |
|
| 103 |
Vector3 operator+(const Vector3& v) const { return (Vector3){x + v.x, y + v.y, z + v.z}; } |
| 104 |
|
| 105 |
Vector3 operator-(const Vector3& v) const { return (Vector3){x - v.x, y - v.y, z - v.z}; } |
| 106 |
|
| 107 |
Vector3 operator/(float s) const { return (Vector3){x / s, y / s, z / s}; } |
| 108 |
|
| 109 |
Vector3 operator*(float s) const { return (Vector3){x * s, y * s, z * s}; } |
| 110 |
|
| 111 |
void dump(const char* label = "Vector3") const { |
| 112 |
ALOGD("%s[%.2f, %.2f, %.2f]", label, x, y, z); |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
#endif |