Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 20 | #include <math.h> |
| 21 | #include <utils/Log.h> |
| 22 | |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | // Classes |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 30 | // MUST BE A POD - this means no ctor or dtor! |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 31 | struct Vector2 { |
| 32 | float x; |
| 33 | float y; |
| 34 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 35 | float lengthSquared() const { return x * x + y * y; } |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 36 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 37 | float length() const { return sqrt(x * x + y * y); } |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 38 | |
| 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 69 | Vector2 operator+(const Vector2& v) const { return (Vector2){x + v.x, y + v.y}; } |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 70 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 71 | Vector2 operator-(const Vector2& v) const { return (Vector2){x - v.x, y - v.y}; } |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 72 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 73 | Vector2 operator/(float s) const { return (Vector2){x / s, y / s}; } |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 74 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 75 | Vector2 operator*(float s) const { return (Vector2){x * s, y * s}; } |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 76 | |
| 77 | void normalize() { |
| 78 | float s = 1.0f / length(); |
| 79 | x *= s; |
| 80 | y *= s; |
| 81 | } |
| 82 | |
| 83 | Vector2 copyNormalized() const { |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 84 | Vector2 v = {x, y}; |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 85 | v.normalize(); |
| 86 | return v; |
| 87 | } |
| 88 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 89 | float dot(const Vector2& v) const { return x * v.x + y * v.y; } |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 90 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 91 | float cross(const Vector2& v) const { return x * v.y - y * v.x; } |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 92 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 93 | void dump() { ALOGD("Vector2[%.2f, %.2f]", x, y); } |
| 94 | }; // class Vector2 |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 95 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 96 | // MUST BE A POD - this means no ctor or dtor! |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 97 | class Vector3 { |
| 98 | public: |
| 99 | float x; |
| 100 | float y; |
| 101 | float z; |
| 102 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 103 | Vector3 operator+(const Vector3& v) const { return (Vector3){x + v.x, y + v.y, z + v.z}; } |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 104 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 105 | Vector3 operator-(const Vector3& v) const { return (Vector3){x - v.x, y - v.y, z - v.z}; } |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 106 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 107 | Vector3 operator/(float s) const { return (Vector3){x / s, y / s, z / s}; } |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 108 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 109 | Vector3 operator*(float s) const { return (Vector3){x * s, y * s, z * s}; } |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 110 | |
John Reck | 82f5e0c | 2015-10-22 17:07:45 -0700 | [diff] [blame] | 111 | void dump(const char* label = "Vector3") const { |
| 112 | ALOGD("%s[%.2f, %.2f, %.2f]", label, x, y, z); |
Chris Craik | b79a3e3 | 2014-03-11 12:20:17 -0700 | [diff] [blame] | 113 | } |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 116 | } // namespace uirenderer |
| 117 | } // namespace android |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 118 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 119 | #endif // ANDROID_HWUI_VECTOR_H |