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 27 // MUST BE A POD - this means no ctor or dtor! 28 struct Vector2 { 29 float x; 30 float y; 31 lengthSquaredVector232 float lengthSquared() const { 33 return x * x + y * y; 34 } 35 lengthVector236 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 { 71 return (Vector2){x + v.x, y + v.y}; 72 } 73 74 Vector2 operator-(const Vector2& v) const { 75 return (Vector2){x - v.x, y - v.y}; 76 } 77 78 Vector2 operator/(float s) const { 79 return (Vector2){x / s, y / s}; 80 } 81 82 Vector2 operator*(float s) const { 83 return (Vector2){x * s, y * s}; 84 } 85 normalizeVector286 void normalize() { 87 float s = 1.0f / length(); 88 x *= s; 89 y *= s; 90 } 91 copyNormalizedVector292 Vector2 copyNormalized() const { 93 Vector2 v = {x, y}; 94 v.normalize(); 95 return v; 96 } 97 dotVector298 float dot(const Vector2& v) const { 99 return x * v.x + y * v.y; 100 } 101 crossVector2102 float cross(const Vector2& v) const { 103 return x * v.y - y * v.x; 104 } 105 dumpVector2106 void dump() { 107 ALOGD("Vector2[%.2f, %.2f]", x, y); 108 } 109 }; // class Vector2 110 111 // MUST BE A POD - this means no ctor or dtor! 112 class Vector3 { 113 public: 114 float x; 115 float y; 116 float z; 117 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 dump()135 void dump() { 136 ALOGD("Vector3[%.2f, %.2f, %.2f]", x, y, z); 137 } 138 }; 139 140 }; // namespace uirenderer 141 }; // namespace android 142 143 #endif // ANDROID_HWUI_VECTOR_H 144