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