Vector.h revision 1aa5d2d7068147ff781cfe911a93f01593a68c79
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
271aa5d2d7068147ff781cfe911a93f01593a68c79John Reck// MUST BE A POD - this means no ctor or dtor!
28a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guystruct Vector2 {
29a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float x;
30a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float y;
31a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
327b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    float lengthSquared() const {
337b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        return x * x + y * y;
347b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
357b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
36a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float length() const {
37a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return sqrt(x * x + y * y);
38a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
39a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
40a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator+=(const Vector2& v) {
41a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x += v.x;
42a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y += v.y;
43a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
44a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
45a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator-=(const Vector2& v) {
46a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x -= v.x;
47a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y -= v.y;
48a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
49a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
50a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator+=(const float v) {
51a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x += v;
52a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y += v;
53a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
54a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
55a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator-=(const float v) {
56a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x -= v;
57a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y -= v;
58a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
59a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
60a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator/=(float s) {
61a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x /= s;
62a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y /= s;
63a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
64a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
65a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator*=(float s) {
66a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x *= s;
67a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y *= s;
68a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
69a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
70a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator+(const Vector2& v) const {
711aa5d2d7068147ff781cfe911a93f01593a68c79John Reck        return (Vector2){x + v.x, y + v.y};
72a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
73a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
74a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator-(const Vector2& v) const {
751aa5d2d7068147ff781cfe911a93f01593a68c79John Reck        return (Vector2){x - v.x, y - v.y};
76a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
77a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
78a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator/(float s) const {
791aa5d2d7068147ff781cfe911a93f01593a68c79John Reck        return (Vector2){x / s, y / s};
80a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
81a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
82a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator*(float s) const {
831aa5d2d7068147ff781cfe911a93f01593a68c79John Reck        return (Vector2){x * s, y * s};
84a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
85a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
86a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void normalize() {
87a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        float s = 1.0f / length();
88a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x *= s;
89a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y *= s;
90a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
91a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
92a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 copyNormalized() const {
931aa5d2d7068147ff781cfe911a93f01593a68c79John Reck        Vector2 v = {x, y};
94a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        v.normalize();
95a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return v;
96a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
97a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
98a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float dot(const Vector2& v) const {
99a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return x * v.x + y * v.y;
100a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
101a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
102a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void dump() {
1035baa3a62a97544669fba6d65a11c07f252e654ddSteve Block        ALOGD("Vector2[%.2f, %.2f]", x, y);
104a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
105a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy}; // class Vector2
106a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1071aa5d2d7068147ff781cfe911a93f01593a68c79John Reck// MUST BE A POD - this means no ctor or dtor!
108f57776b2d195f0937906eb88b777bb55ccc36967Chris Craikclass Vector3 {
109f57776b2d195f0937906eb88b777bb55ccc36967Chris Craikpublic:
110f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    float x;
111f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    float y;
112f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    float z;
113f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik
114b79a3e301a8d89b9e1b1f6f3d7fd6aa56610a6f0Chris Craik    void dump() {
115b79a3e301a8d89b9e1b1f6f3d7fd6aa56610a6f0Chris Craik        ALOGD("Vector3[%.2f, %.2f, %.2f]", x, y, z);
116b79a3e301a8d89b9e1b1f6f3d7fd6aa56610a6f0Chris Craik    }
117f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik};
118f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik
119a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy}; // namespace uirenderer
120a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy}; // namespace android
121a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
122a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy#endif // ANDROID_HWUI_VECTOR_H
123