blob: aa6acc90b0fff01e720fa63b7dfb0b7033d95e62 [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
20namespace android {
21namespace uirenderer {
22
23///////////////////////////////////////////////////////////////////////////////
24// Classes
25///////////////////////////////////////////////////////////////////////////////
26
John Reck1aa5d2d2014-07-24 13:38:28 -070027// MUST BE A POD - this means no ctor or dtor!
Romain Guya957eea2010-12-08 18:34:42 -080028struct Vector2 {
29 float x;
30 float y;
31
ztenghui7b4516e2014-01-07 10:42:55 -080032 float lengthSquared() const {
33 return x * x + y * y;
34 }
35
Romain Guya957eea2010-12-08 18:34:42 -080036 float length() const {
37 return sqrt(x * x + y * y);
38 }
39
40 void operator+=(const Vector2& v) {
41 x += v.x;
42 y += v.y;
43 }
44
45 void operator-=(const Vector2& v) {
46 x -= v.x;
47 y -= v.y;
48 }
49
50 void operator+=(const float v) {
51 x += v;
52 y += v;
53 }
54
55 void operator-=(const float v) {
56 x -= v;
57 y -= v;
58 }
59
60 void operator/=(float s) {
61 x /= s;
62 y /= s;
63 }
64
65 void operator*=(float s) {
66 x *= s;
67 y *= s;
68 }
69
70 Vector2 operator+(const Vector2& v) const {
John Reck1aa5d2d2014-07-24 13:38:28 -070071 return (Vector2){x + v.x, y + v.y};
Romain Guya957eea2010-12-08 18:34:42 -080072 }
73
74 Vector2 operator-(const Vector2& v) const {
John Reck1aa5d2d2014-07-24 13:38:28 -070075 return (Vector2){x - v.x, y - v.y};
Romain Guya957eea2010-12-08 18:34:42 -080076 }
77
78 Vector2 operator/(float s) const {
John Reck1aa5d2d2014-07-24 13:38:28 -070079 return (Vector2){x / s, y / s};
Romain Guya957eea2010-12-08 18:34:42 -080080 }
81
82 Vector2 operator*(float s) const {
John Reck1aa5d2d2014-07-24 13:38:28 -070083 return (Vector2){x * s, y * s};
Romain Guya957eea2010-12-08 18:34:42 -080084 }
85
86 void normalize() {
87 float s = 1.0f / length();
88 x *= s;
89 y *= s;
90 }
91
92 Vector2 copyNormalized() const {
John Reck1aa5d2d2014-07-24 13:38:28 -070093 Vector2 v = {x, y};
Romain Guya957eea2010-12-08 18:34:42 -080094 v.normalize();
95 return v;
96 }
97
98 float dot(const Vector2& v) const {
99 return x * v.x + y * v.y;
100 }
101
ztenghuid2dcd6f2014-10-29 16:04:29 -0700102 float cross(const Vector2& v) const {
103 return x * v.y - y * v.x;
104 }
105
Romain Guya957eea2010-12-08 18:34:42 -0800106 void dump() {
Steve Block5baa3a62011-12-20 16:23:08 +0000107 ALOGD("Vector2[%.2f, %.2f]", x, y);
Romain Guya957eea2010-12-08 18:34:42 -0800108 }
109}; // class Vector2
110
John Reck1aa5d2d2014-07-24 13:38:28 -0700111// MUST BE A POD - this means no ctor or dtor!
Chris Craikf57776b2013-10-25 18:30:17 -0700112class Vector3 {
113public:
114 float x;
115 float y;
116 float z;
117
ztenghuid5e8ade2014-08-13 15:48:02 -0700118 Vector3 operator+(const Vector3& v) const {
119 return (Vector3){x + v.x, y + v.y, z + v.z};
120 }
121
122 Vector3 operator-(const Vector3& v) const {
123 return (Vector3){x - v.x, y - v.y, z - v.z};
124 }
125
126 Vector3 operator/(float s) const {
127 return (Vector3){x / s, y / s, z / s};
128 }
129
130 Vector3 operator*(float s) const {
131 return (Vector3){x * s, y * s, z * s};
132 }
133
134
Chris Craikb79a3e32014-03-11 12:20:17 -0700135 void dump() {
136 ALOGD("Vector3[%.2f, %.2f, %.2f]", x, y, z);
137 }
Chris Craikf57776b2013-10-25 18:30:17 -0700138};
139
Romain Guya957eea2010-12-08 18:34:42 -0800140}; // namespace uirenderer
141}; // namespace android
142
143#endif // ANDROID_HWUI_VECTOR_H