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 
23 namespace android {
24 namespace uirenderer {
25 
26 ///////////////////////////////////////////////////////////////////////////////
27 // Classes
28 ///////////////////////////////////////////////////////////////////////////////
29 
30 // MUST BE A POD - this means no ctor or dtor!
31 struct Vector2 {
32     float x;
33     float y;
34 
lengthSquaredVector235     float lengthSquared() const { return x * x + y * y; }
36 
lengthVector237     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 
normalizeVector277     void normalize() {
78         float s = 1.0f / length();
79         x *= s;
80         y *= s;
81     }
82 
copyNormalizedVector283     Vector2 copyNormalized() const {
84         Vector2 v = {x, y};
85         v.normalize();
86         return v;
87     }
88 
dotVector289     float dot(const Vector2& v) const { return x * v.x + y * v.y; }
90 
crossVector291     float cross(const Vector2& v) const { return x * v.y - y * v.x; }
92 
dumpVector293     void dump() { ALOGD("Vector2[%.2f, %.2f]", x, y); }
94 };  // class Vector2
95 
96 // MUST BE A POD - this means no ctor or dtor!
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 }  // namespace uirenderer
117 }  // namespace android
118 
119 #endif  // ANDROID_HWUI_VECTOR_H
120