blob: d2c15ad872a599514f2bbe35c752af3be92de9a2 [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
John Reck1bcacfd2017-11-03 10:12:19 -070035 float lengthSquared() const { return x * x + y * y; }
ztenghui7b4516e2014-01-07 10:42:55 -080036
John Reck1bcacfd2017-11-03 10:12:19 -070037 float length() const { return sqrt(x * x + y * y); }
Romain Guya957eea2010-12-08 18:34:42 -080038
39 void operator+=(const Vector2& v) {
40 x += v.x;
41 y += v.y;
42 }
43
44 void operator-=(const Vector2& v) {
45 x -= v.x;
46 y -= v.y;
47 }
48
49 void operator+=(const float v) {
50 x += v;
51 y += v;
52 }
53
54 void operator-=(const float v) {
55 x -= v;
56 y -= v;
57 }
58
59 void operator/=(float s) {
60 x /= s;
61 y /= s;
62 }
63
64 void operator*=(float s) {
65 x *= s;
66 y *= s;
67 }
68
John Reck1bcacfd2017-11-03 10:12:19 -070069 Vector2 operator+(const Vector2& v) const { return (Vector2){x + v.x, y + v.y}; }
Romain Guya957eea2010-12-08 18:34:42 -080070
John Reck1bcacfd2017-11-03 10:12:19 -070071 Vector2 operator-(const Vector2& v) const { return (Vector2){x - v.x, y - v.y}; }
Romain Guya957eea2010-12-08 18:34:42 -080072
John Reck1bcacfd2017-11-03 10:12:19 -070073 Vector2 operator/(float s) const { return (Vector2){x / s, y / s}; }
Romain Guya957eea2010-12-08 18:34:42 -080074
John Reck1bcacfd2017-11-03 10:12:19 -070075 Vector2 operator*(float s) const { return (Vector2){x * s, y * s}; }
Romain Guya957eea2010-12-08 18:34:42 -080076
77 void normalize() {
78 float s = 1.0f / length();
79 x *= s;
80 y *= s;
81 }
82
83 Vector2 copyNormalized() const {
John Reck1aa5d2d2014-07-24 13:38:28 -070084 Vector2 v = {x, y};
Romain Guya957eea2010-12-08 18:34:42 -080085 v.normalize();
86 return v;
87 }
88
John Reck1bcacfd2017-11-03 10:12:19 -070089 float dot(const Vector2& v) const { return x * v.x + y * v.y; }
Romain Guya957eea2010-12-08 18:34:42 -080090
John Reck1bcacfd2017-11-03 10:12:19 -070091 float cross(const Vector2& v) const { return x * v.y - y * v.x; }
ztenghuid2dcd6f2014-10-29 16:04:29 -070092
John Reck1bcacfd2017-11-03 10:12:19 -070093 void dump() { ALOGD("Vector2[%.2f, %.2f]", x, y); }
94}; // class Vector2
Romain Guya957eea2010-12-08 18:34:42 -080095
John Reck1aa5d2d2014-07-24 13:38:28 -070096// MUST BE A POD - this means no ctor or dtor!
Chris Craikf57776b2013-10-25 18:30:17 -070097class Vector3 {
98public:
99 float x;
100 float y;
101 float z;
102
John Reck1bcacfd2017-11-03 10:12:19 -0700103 Vector3 operator+(const Vector3& v) const { return (Vector3){x + v.x, y + v.y, z + v.z}; }
ztenghuid5e8ade2014-08-13 15:48:02 -0700104
John Reck1bcacfd2017-11-03 10:12:19 -0700105 Vector3 operator-(const Vector3& v) const { return (Vector3){x - v.x, y - v.y, z - v.z}; }
ztenghuid5e8ade2014-08-13 15:48:02 -0700106
John Reck1bcacfd2017-11-03 10:12:19 -0700107 Vector3 operator/(float s) const { return (Vector3){x / s, y / s, z / s}; }
ztenghuid5e8ade2014-08-13 15:48:02 -0700108
John Reck1bcacfd2017-11-03 10:12:19 -0700109 Vector3 operator*(float s) const { return (Vector3){x * s, y * s, z * s}; }
ztenghuid5e8ade2014-08-13 15:48:02 -0700110
John Reck82f5e0c2015-10-22 17:07:45 -0700111 void dump(const char* label = "Vector3") const {
112 ALOGD("%s[%.2f, %.2f, %.2f]", label, x, y, z);
Chris Craikb79a3e32014-03-11 12:20:17 -0700113 }
Chris Craikf57776b2013-10-25 18:30:17 -0700114};
115
John Reck1bcacfd2017-11-03 10:12:19 -0700116}; // namespace uirenderer
117}; // namespace android
Romain Guya957eea2010-12-08 18:34:42 -0800118
John Reck1bcacfd2017-11-03 10:12:19 -0700119#endif // ANDROID_HWUI_VECTOR_H