Vector.h revision 7b4516e7ea552ad08d6e7277d311ef11bd8f12e8
1a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy/*
2a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy * Copyright (C) 2010 The Android Open Source Project
3a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy *
4a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy * you may not use this file except in compliance with the License.
6a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy * You may obtain a copy of the License at
7a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy *
8a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy *
10a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy * Unless required by applicable law or agreed to in writing, software
11a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy * See the License for the specific language governing permissions and
14a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy * limitations under the License.
15a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy */
16a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
17a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy#ifndef ANDROID_HWUI_VECTOR_H
18a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy#define ANDROID_HWUI_VECTOR_H
19a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
20a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guynamespace android {
21a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guynamespace uirenderer {
22a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
23a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy///////////////////////////////////////////////////////////////////////////////
24a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy// Classes
25a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy///////////////////////////////////////////////////////////////////////////////
26a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
27a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guystruct Vector2 {
28a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float x;
29a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float y;
30a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
31a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2() :
32a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x(0.0f), y(0.0f) {
33a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
34a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
35a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2(float px, float py) :
36a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x(px), y(py) {
37a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
38a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
397b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    float lengthSquared() const {
407b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        return x * x + y * y;
417b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
427b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
43a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float length() const {
44a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return sqrt(x * x + y * y);
45a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
46a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
47a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator+=(const Vector2& v) {
48a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x += v.x;
49a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y += v.y;
50a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
51a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
52a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator-=(const Vector2& v) {
53a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x -= v.x;
54a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y -= v.y;
55a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
56a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
57a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator+=(const float v) {
58a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x += v;
59a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y += v;
60a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
61a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
62a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator-=(const float v) {
63a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x -= v;
64a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y -= v;
65a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
66a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
67a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator/=(float s) {
68a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x /= s;
69a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y /= s;
70a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
71a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
72a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator*=(float s) {
73a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x *= s;
74a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y *= s;
75a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
76a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
77a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator+(const Vector2& v) const {
78a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return Vector2(x + v.x, y + v.y);
79a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
80a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
81a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator-(const Vector2& v) const {
82a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return Vector2(x - v.x, y - v.y);
83a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
84a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
85a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator/(float s) const {
86a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return Vector2(x / s, y / s);
87a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
88a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
89a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator*(float s) const {
90a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return Vector2(x * s, y * s);
91a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
92a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
93a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void normalize() {
94a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        float s = 1.0f / length();
95a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x *= s;
96a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y *= s;
97a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
98a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
99a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 copyNormalized() const {
100a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        Vector2 v(x, y);
101a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        v.normalize();
102a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return v;
103a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
104a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
105a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float dot(const Vector2& v) const {
106a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return x * v.x + y * v.y;
107a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
108a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
109a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void dump() {
1105baa3a62a97544669fba6d65a11c07f252e654ddSteve Block        ALOGD("Vector2[%.2f, %.2f]", x, y);
111a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
112a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy}; // class Vector2
113a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
114f57776b2d195f0937906eb88b777bb55ccc36967Chris Craikclass Vector3 {
115f57776b2d195f0937906eb88b777bb55ccc36967Chris Craikpublic:
116f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    float x;
117f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    float y;
118f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    float z;
119f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik
120f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    Vector3() :
121f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik        x(0.0f), y(0.0f), z(0.0f) {
122f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    }
123f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik
124f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    Vector3(float px, float py, float pz) :
125f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik        x(px), y(py), z(pz) {
126f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    }
127f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik};
128f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik
129a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy///////////////////////////////////////////////////////////////////////////////
130a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy// Types
131a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy///////////////////////////////////////////////////////////////////////////////
132a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
133a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guytypedef Vector2 vec2;
134a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
135a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy}; // namespace uirenderer
136a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy}; // namespace android
137a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
138a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy#endif // ANDROID_HWUI_VECTOR_H
139