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
2096a5c4c7bab6718524de7253da8309143ab48befChris Craik#include <math.h>
2196a5c4c7bab6718524de7253da8309143ab48befChris Craik#include <utils/Log.h>
2296a5c4c7bab6718524de7253da8309143ab48befChris Craik
23a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guynamespace android {
24a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guynamespace uirenderer {
25a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
26a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy///////////////////////////////////////////////////////////////////////////////
27a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy// Classes
28a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy///////////////////////////////////////////////////////////////////////////////
29a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
301aa5d2d7068147ff781cfe911a93f01593a68c79John Reck// MUST BE A POD - this means no ctor or dtor!
31a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guystruct Vector2 {
32a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float x;
33a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float y;
34a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
357b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    float lengthSquared() const {
367b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        return x * x + y * y;
377b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
387b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
39a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float length() const {
40a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return sqrt(x * x + y * y);
41a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
42a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
43a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator+=(const Vector2& v) {
44a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x += v.x;
45a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y += v.y;
46a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
47a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
48a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator-=(const Vector2& v) {
49a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x -= v.x;
50a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y -= v.y;
51a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
52a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
53a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator+=(const float v) {
54a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x += v;
55a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y += v;
56a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
57a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
58a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator-=(const float v) {
59a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x -= v;
60a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y -= v;
61a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
62a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
63a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator/=(float s) {
64a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x /= s;
65a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y /= s;
66a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
67a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
68a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void operator*=(float s) {
69a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x *= s;
70a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y *= s;
71a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
72a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
73a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator+(const Vector2& v) const {
741aa5d2d7068147ff781cfe911a93f01593a68c79John Reck        return (Vector2){x + v.x, y + v.y};
75a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
76a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
77a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator-(const Vector2& v) const {
781aa5d2d7068147ff781cfe911a93f01593a68c79John Reck        return (Vector2){x - v.x, y - v.y};
79a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
80a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
81a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator/(float s) const {
821aa5d2d7068147ff781cfe911a93f01593a68c79John Reck        return (Vector2){x / s, y / s};
83a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
84a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
85a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 operator*(float s) const {
861aa5d2d7068147ff781cfe911a93f01593a68c79John Reck        return (Vector2){x * s, y * s};
87a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
88a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
89a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void normalize() {
90a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        float s = 1.0f / length();
91a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        x *= s;
92a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        y *= s;
93a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
94a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
95a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    Vector2 copyNormalized() const {
961aa5d2d7068147ff781cfe911a93f01593a68c79John Reck        Vector2 v = {x, y};
97a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        v.normalize();
98a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return v;
99a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
100a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
101a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    float dot(const Vector2& v) const {
102a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        return x * v.x + y * v.y;
103a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
104a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
105d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float cross(const Vector2& v) const {
106d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        return x * v.y - y * v.x;
107d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    }
108d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
109a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    void dump() {
1105baa3a62a97544669fba6d65a11c07f252e654ddSteve Block        ALOGD("Vector2[%.2f, %.2f]", x, y);
111a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
112a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy}; // class Vector2
113a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1141aa5d2d7068147ff781cfe911a93f01593a68c79John Reck// MUST BE A POD - this means no ctor or dtor!
115f57776b2d195f0937906eb88b777bb55ccc36967Chris Craikclass Vector3 {
116f57776b2d195f0937906eb88b777bb55ccc36967Chris Craikpublic:
117f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    float x;
118f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    float y;
119f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    float z;
120f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik
121d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui    Vector3 operator+(const Vector3& v) const {
122d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui        return (Vector3){x + v.x, y + v.y, z + v.z};
123d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui    }
124d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui
125d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui    Vector3 operator-(const Vector3& v) const {
126d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui        return (Vector3){x - v.x, y - v.y, z - v.z};
127d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui    }
128d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui
129d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui    Vector3 operator/(float s) const {
130d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui        return (Vector3){x / s, y / s, z / s};
131d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui    }
132d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui
133d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui    Vector3 operator*(float s) const {
134d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui        return (Vector3){x * s, y * s, z * s};
135d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui    }
136d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui
137d5e8ade498b41b42874273cbfa375aed7b4d6a08ztenghui
13882f5e0c5cd0fcbacd8094b7963a50a2e30e0846fJohn Reck    void dump(const char* label = "Vector3") const {
13982f5e0c5cd0fcbacd8094b7963a50a2e30e0846fJohn Reck        ALOGD("%s[%.2f, %.2f, %.2f]", label, x, y, z);
140b79a3e301a8d89b9e1b1f6f3d7fd6aa56610a6f0Chris Craik    }
141f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik};
142f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik
143a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy}; // namespace uirenderer
144a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy}; // namespace android
145a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
146a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy#endif // ANDROID_HWUI_VECTOR_H
147