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 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 35 | float lengthSquared() const { |
| 36 | return x * x + y * y; |
| 37 | } |
| 38 | |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 39 | 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 Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 74 | return (Vector2){x + v.x, y + v.y}; |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | Vector2 operator-(const Vector2& v) const { |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 78 | return (Vector2){x - v.x, y - v.y}; |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | Vector2 operator/(float s) const { |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 82 | return (Vector2){x / s, y / s}; |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | Vector2 operator*(float s) const { |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 86 | return (Vector2){x * s, y * s}; |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void normalize() { |
| 90 | float s = 1.0f / length(); |
| 91 | x *= s; |
| 92 | y *= s; |
| 93 | } |
| 94 | |
| 95 | Vector2 copyNormalized() const { |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 96 | Vector2 v = {x, y}; |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 97 | v.normalize(); |
| 98 | return v; |
| 99 | } |
| 100 | |
| 101 | float dot(const Vector2& v) const { |
| 102 | return x * v.x + y * v.y; |
| 103 | } |
| 104 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 105 | float cross(const Vector2& v) const { |
| 106 | return x * v.y - y * v.x; |
| 107 | } |
| 108 | |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 109 | void dump() { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 110 | ALOGD("Vector2[%.2f, %.2f]", x, y); |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 111 | } |
| 112 | }; // class Vector2 |
| 113 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 114 | // MUST BE A POD - this means no ctor or dtor! |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 115 | class Vector3 { |
| 116 | public: |
| 117 | float x; |
| 118 | float y; |
| 119 | float z; |
| 120 | |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 121 | 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 Craik | b79a3e3 | 2014-03-11 12:20:17 -0700 | [diff] [blame] | 138 | void dump() { |
| 139 | ALOGD("Vector3[%.2f, %.2f, %.2f]", x, y, z); |
| 140 | } |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 143 | }; // namespace uirenderer |
| 144 | }; // namespace android |
| 145 | |
| 146 | #endif // ANDROID_HWUI_VECTOR_H |