1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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
23namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
27// Classes
28///////////////////////////////////////////////////////////////////////////////
29
30// MUST BE A POD - this means no ctor or dtor!
31struct Vector2 {
32    float x;
33    float y;
34
35    float lengthSquared() const {
36        return x * x + y * y;
37    }
38
39    float length() const {
40        return sqrt(x * x + y * y);
41    }
42
43    void operator+=(const Vector2& v) {
44        x += v.x;
45        y += v.y;
46    }
47
48    void operator-=(const Vector2& v) {
49        x -= v.x;
50        y -= v.y;
51    }
52
53    void operator+=(const float v) {
54        x += v;
55        y += v;
56    }
57
58    void operator-=(const float v) {
59        x -= v;
60        y -= v;
61    }
62
63    void operator/=(float s) {
64        x /= s;
65        y /= s;
66    }
67
68    void operator*=(float s) {
69        x *= s;
70        y *= s;
71    }
72
73    Vector2 operator+(const Vector2& v) const {
74        return (Vector2){x + v.x, y + v.y};
75    }
76
77    Vector2 operator-(const Vector2& v) const {
78        return (Vector2){x - v.x, y - v.y};
79    }
80
81    Vector2 operator/(float s) const {
82        return (Vector2){x / s, y / s};
83    }
84
85    Vector2 operator*(float s) const {
86        return (Vector2){x * s, y * s};
87    }
88
89    void normalize() {
90        float s = 1.0f / length();
91        x *= s;
92        y *= s;
93    }
94
95    Vector2 copyNormalized() const {
96        Vector2 v = {x, y};
97        v.normalize();
98        return v;
99    }
100
101    float dot(const Vector2& v) const {
102        return x * v.x + y * v.y;
103    }
104
105    float cross(const Vector2& v) const {
106        return x * v.y - y * v.x;
107    }
108
109    void dump() {
110        ALOGD("Vector2[%.2f, %.2f]", x, y);
111    }
112}; // class Vector2
113
114// MUST BE A POD - this means no ctor or dtor!
115class Vector3 {
116public:
117    float x;
118    float y;
119    float z;
120
121    Vector3 operator+(const Vector3& v) const {
122        return (Vector3){x + v.x, y + v.y, z + v.z};
123    }
124
125    Vector3 operator-(const Vector3& v) const {
126        return (Vector3){x - v.x, y - v.y, z - v.z};
127    }
128
129    Vector3 operator/(float s) const {
130        return (Vector3){x / s, y / s, z / s};
131    }
132
133    Vector3 operator*(float s) const {
134        return (Vector3){x * s, y * s, z * s};
135    }
136
137
138    void dump(const char* label = "Vector3") const {
139        ALOGD("%s[%.2f, %.2f, %.2f]", label, x, y, z);
140    }
141};
142
143}; // namespace uirenderer
144}; // namespace android
145
146#endif // ANDROID_HWUI_VECTOR_H
147