blob: 7c3f2fd379e4dc30d902e4c3478fe3be8a468606 [file] [log] [blame]
Romain Guya957eea2010-12-08 18:34:42 -08001/*
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
Chris Craik96a5c4c2015-01-27 15:46:35 -080020#include <math.h>
21#include <utils/Log.h>
22
Romain Guya957eea2010-12-08 18:34:42 -080023namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
27// Classes
28///////////////////////////////////////////////////////////////////////////////
29
John Reck1aa5d2d2014-07-24 13:38:28 -070030// MUST BE A POD - this means no ctor or dtor!
Romain Guya957eea2010-12-08 18:34:42 -080031struct Vector2 {
32 float x;
33 float y;
34
ztenghui7b4516e2014-01-07 10:42:55 -080035 float lengthSquared() const {
36 return x * x + y * y;
37 }
38
Romain Guya957eea2010-12-08 18:34:42 -080039 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 {
John Reck1aa5d2d2014-07-24 13:38:28 -070074 return (Vector2){x + v.x, y + v.y};
Romain Guya957eea2010-12-08 18:34:42 -080075 }
76
77 Vector2 operator-(const Vector2& v) const {
John Reck1aa5d2d2014-07-24 13:38:28 -070078 return (Vector2){x - v.x, y - v.y};
Romain Guya957eea2010-12-08 18:34:42 -080079 }
80
81 Vector2 operator/(float s) const {
John Reck1aa5d2d2014-07-24 13:38:28 -070082 return (Vector2){x / s, y / s};
Romain Guya957eea2010-12-08 18:34:42 -080083 }
84
85 Vector2 operator*(float s) const {
John Reck1aa5d2d2014-07-24 13:38:28 -070086 return (Vector2){x * s, y * s};
Romain Guya957eea2010-12-08 18:34:42 -080087 }
88
89 void normalize() {
90 float s = 1.0f / length();
91 x *= s;
92 y *= s;
93 }
94
95 Vector2 copyNormalized() const {
John Reck1aa5d2d2014-07-24 13:38:28 -070096 Vector2 v = {x, y};
Romain Guya957eea2010-12-08 18:34:42 -080097 v.normalize();
98 return v;
99 }
100
101 float dot(const Vector2& v) const {
102 return x * v.x + y * v.y;
103 }
104
ztenghuid2dcd6f2014-10-29 16:04:29 -0700105 float cross(const Vector2& v) const {
106 return x * v.y - y * v.x;
107 }
108
Romain Guya957eea2010-12-08 18:34:42 -0800109 void dump() {
Steve Block5baa3a62011-12-20 16:23:08 +0000110 ALOGD("Vector2[%.2f, %.2f]", x, y);
Romain Guya957eea2010-12-08 18:34:42 -0800111 }
112}; // class Vector2
113
John Reck1aa5d2d2014-07-24 13:38:28 -0700114// MUST BE A POD - this means no ctor or dtor!
Chris Craikf57776b2013-10-25 18:30:17 -0700115class Vector3 {
116public:
117 float x;
118 float y;
119 float z;
120
ztenghuid5e8ade2014-08-13 15:48:02 -0700121 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
Chris Craikb79a3e32014-03-11 12:20:17 -0700138 void dump() {
139 ALOGD("Vector3[%.2f, %.2f, %.2f]", x, y, z);
140 }
Chris Craikf57776b2013-10-25 18:30:17 -0700141};
142
Romain Guya957eea2010-12-08 18:34:42 -0800143}; // namespace uirenderer
144}; // namespace android
145
146#endif // ANDROID_HWUI_VECTOR_H