Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 17 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 18 | #ifndef UI_TVECHELPERS_H_ |
| 19 | #define UI_TVECHELPERS_H_ |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 20 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 21 | #include <math.h> |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
| 24 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 25 | #include <cmath> |
| 26 | #include <limits> |
| 27 | #include <iostream> |
| 28 | |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 29 | #define PURE __attribute__((pure)) |
| 30 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 31 | #if __cplusplus >= 201402L |
| 32 | #define CONSTEXPR constexpr |
| 33 | #else |
| 34 | #define CONSTEXPR |
| 35 | #endif |
| 36 | |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 37 | namespace android { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 38 | namespace details { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 39 | // ------------------------------------------------------------------------------------- |
| 40 | |
| 41 | /* |
| 42 | * No user serviceable parts here. |
| 43 | * |
| 44 | * Don't use this file directly, instead include ui/vec{2|3|4}.h |
| 45 | */ |
| 46 | |
| 47 | /* |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 48 | * TVec{Add|Product}Operators implements basic arithmetic and basic compound assignments |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 49 | * operators on a vector of type BASE<T>. |
| 50 | * |
| 51 | * BASE only needs to implement operator[] and size(). |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 52 | * By simply inheriting from TVec{Add|Product}Operators<BASE, T> BASE will automatically |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 53 | * get all the functionality here. |
| 54 | */ |
| 55 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 56 | template <template<typename T> class VECTOR, typename T> |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 57 | class TVecAddOperators { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 58 | public: |
| 59 | /* compound assignment from a another vector of the same size but different |
| 60 | * element type. |
| 61 | */ |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 62 | template<typename OTHER> |
| 63 | VECTOR<T>& operator +=(const VECTOR<OTHER>& v) { |
| 64 | VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this); |
| 65 | for (size_t i = 0; i < lhs.size(); i++) { |
| 66 | lhs[i] += v[i]; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 67 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 68 | return lhs; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 69 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 70 | template<typename OTHER> |
| 71 | VECTOR<T>& operator -=(const VECTOR<OTHER>& v) { |
| 72 | VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this); |
| 73 | for (size_t i = 0; i < lhs.size(); i++) { |
| 74 | lhs[i] -= v[i]; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 75 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 76 | return lhs; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 77 | } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 78 | |
| 79 | /* compound assignment from a another vector of the same type. |
| 80 | * These operators can be used for implicit conversion and handle operations |
| 81 | * like "vector *= scalar" by letting the compiler implicitly convert a scalar |
| 82 | * to a vector (assuming the BASE<T> allows it). |
| 83 | */ |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 84 | VECTOR<T>& operator +=(const VECTOR<T>& v) { |
| 85 | VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this); |
| 86 | for (size_t i = 0; i < lhs.size(); i++) { |
| 87 | lhs[i] += v[i]; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 88 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 89 | return lhs; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 90 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 91 | VECTOR<T>& operator -=(const VECTOR<T>& v) { |
| 92 | VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this); |
| 93 | for (size_t i = 0; i < lhs.size(); i++) { |
| 94 | lhs[i] -= v[i]; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 95 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 96 | return lhs; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 97 | } |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * NOTE: the functions below ARE NOT member methods. They are friend functions |
| 101 | * with they definition inlined with their declaration. This makes these |
| 102 | * template functions available to the compiler when (and only when) this class |
| 103 | * is instantiated, at which point they're only templated on the 2nd parameter |
| 104 | * (the first one, BASE<T> being known). |
| 105 | */ |
| 106 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 107 | /* The operators below handle operation between vectors of the same size |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 108 | * but of a different element type. |
| 109 | */ |
| 110 | template<typename RT> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 111 | friend inline constexpr VECTOR<T> PURE operator +(VECTOR<T> lv, const VECTOR<RT>& rv) { |
| 112 | // don't pass lv by reference because we need a copy anyways |
| 113 | return lv += rv; |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 114 | } |
| 115 | template<typename RT> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 116 | friend inline constexpr VECTOR<T> PURE operator -(VECTOR<T> lv, const VECTOR<RT>& rv) { |
| 117 | // don't pass lv by reference because we need a copy anyways |
| 118 | return lv -= rv; |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* The operators below (which are not templates once this class is instanced, |
| 122 | * i.e.: BASE<T> is known) can be used for implicit conversion on both sides. |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 123 | * These handle operations like "vector + scalar" and "scalar + vector" by |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 124 | * letting the compiler implicitly convert a scalar to a vector (assuming |
| 125 | * the BASE<T> allows it). |
| 126 | */ |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 127 | friend inline constexpr VECTOR<T> PURE operator +(VECTOR<T> lv, const VECTOR<T>& rv) { |
| 128 | // don't pass lv by reference because we need a copy anyways |
| 129 | return lv += rv; |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 130 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 131 | friend inline constexpr VECTOR<T> PURE operator -(VECTOR<T> lv, const VECTOR<T>& rv) { |
| 132 | // don't pass lv by reference because we need a copy anyways |
| 133 | return lv -= rv; |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 134 | } |
| 135 | }; |
| 136 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 137 | template<template<typename T> class VECTOR, typename T> |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 138 | class TVecProductOperators { |
| 139 | public: |
| 140 | /* compound assignment from a another vector of the same size but different |
| 141 | * element type. |
| 142 | */ |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 143 | template<typename OTHER> |
| 144 | VECTOR<T>& operator *=(const VECTOR<OTHER>& v) { |
| 145 | VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this); |
| 146 | for (size_t i = 0; i < lhs.size(); i++) { |
| 147 | lhs[i] *= v[i]; |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 148 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 149 | return lhs; |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 150 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 151 | template<typename OTHER> |
| 152 | VECTOR<T>& operator /=(const VECTOR<OTHER>& v) { |
| 153 | VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this); |
| 154 | for (size_t i = 0; i < lhs.size(); i++) { |
| 155 | lhs[i] /= v[i]; |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 156 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 157 | return lhs; |
Mathias Agopian | 1d4d8f9 | 2013-09-01 21:35:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* compound assignment from a another vector of the same type. |
| 161 | * These operators can be used for implicit conversion and handle operations |
| 162 | * like "vector *= scalar" by letting the compiler implicitly convert a scalar |
| 163 | * to a vector (assuming the BASE<T> allows it). |
| 164 | */ |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 165 | VECTOR<T>& operator *=(const VECTOR<T>& v) { |
| 166 | VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this); |
| 167 | for (size_t i = 0; i < lhs.size(); i++) { |
| 168 | lhs[i] *= v[i]; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 169 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 170 | return lhs; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 171 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 172 | VECTOR<T>& operator /=(const VECTOR<T>& v) { |
| 173 | VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this); |
| 174 | for (size_t i = 0; i < lhs.size(); i++) { |
| 175 | lhs[i] /= v[i]; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 176 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 177 | return lhs; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
| 181 | * NOTE: the functions below ARE NOT member methods. They are friend functions |
| 182 | * with they definition inlined with their declaration. This makes these |
| 183 | * template functions available to the compiler when (and only when) this class |
| 184 | * is instantiated, at which point they're only templated on the 2nd parameter |
| 185 | * (the first one, BASE<T> being known). |
| 186 | */ |
| 187 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 188 | /* The operators below handle operation between vectors of the same size |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 189 | * but of a different element type. |
| 190 | */ |
| 191 | template<typename RT> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 192 | friend inline constexpr VECTOR<T> PURE operator *(VECTOR<T> lv, const VECTOR<RT>& rv) { |
| 193 | // don't pass lv by reference because we need a copy anyways |
| 194 | return lv *= rv; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 195 | } |
| 196 | template<typename RT> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 197 | friend inline constexpr VECTOR<T> PURE operator /(VECTOR<T> lv, const VECTOR<RT>& rv) { |
| 198 | // don't pass lv by reference because we need a copy anyways |
| 199 | return lv /= rv; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* The operators below (which are not templates once this class is instanced, |
| 203 | * i.e.: BASE<T> is known) can be used for implicit conversion on both sides. |
| 204 | * These handle operations like "vector * scalar" and "scalar * vector" by |
| 205 | * letting the compiler implicitly convert a scalar to a vector (assuming |
| 206 | * the BASE<T> allows it). |
| 207 | */ |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 208 | friend inline constexpr VECTOR<T> PURE operator *(VECTOR<T> lv, const VECTOR<T>& rv) { |
| 209 | // don't pass lv by reference because we need a copy anyways |
| 210 | return lv *= rv; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 211 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 212 | friend inline constexpr VECTOR<T> PURE operator /(VECTOR<T> lv, const VECTOR<T>& rv) { |
| 213 | // don't pass lv by reference because we need a copy anyways |
| 214 | return lv /= rv; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 215 | } |
| 216 | }; |
| 217 | |
| 218 | /* |
| 219 | * TVecUnaryOperators implements unary operators on a vector of type BASE<T>. |
| 220 | * |
| 221 | * BASE only needs to implement operator[] and size(). |
| 222 | * By simply inheriting from TVecUnaryOperators<BASE, T> BASE will automatically |
| 223 | * get all the functionality here. |
| 224 | * |
| 225 | * These operators are implemented as friend functions of TVecUnaryOperators<BASE, T> |
| 226 | */ |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 227 | template<template<typename T> class VECTOR, typename T> |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 228 | class TVecUnaryOperators { |
| 229 | public: |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 230 | VECTOR<T>& operator ++() { |
| 231 | VECTOR<T>& rhs = static_cast<VECTOR<T>&>(*this); |
| 232 | for (size_t i = 0; i < rhs.size(); i++) { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 233 | ++rhs[i]; |
| 234 | } |
| 235 | return rhs; |
| 236 | } |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 237 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 238 | VECTOR<T>& operator --() { |
| 239 | VECTOR<T>& rhs = static_cast<VECTOR<T>&>(*this); |
| 240 | for (size_t i = 0; i < rhs.size(); i++) { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 241 | --rhs[i]; |
| 242 | } |
| 243 | return rhs; |
| 244 | } |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 245 | |
| 246 | CONSTEXPR VECTOR<T> operator -() const { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 247 | VECTOR<T> r(VECTOR<T>::NO_INIT); |
| 248 | VECTOR<T> const& rv(static_cast<VECTOR<T> const&>(*this)); |
| 249 | for (size_t i = 0; i < r.size(); i++) { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 250 | r[i] = -rv[i]; |
| 251 | } |
| 252 | return r; |
| 253 | } |
| 254 | }; |
| 255 | |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 256 | /* |
| 257 | * TVecComparisonOperators implements relational/comparison operators |
| 258 | * on a vector of type BASE<T>. |
| 259 | * |
| 260 | * BASE only needs to implement operator[] and size(). |
| 261 | * By simply inheriting from TVecComparisonOperators<BASE, T> BASE will automatically |
| 262 | * get all the functionality here. |
| 263 | */ |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 264 | template<template<typename T> class VECTOR, typename T> |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 265 | class TVecComparisonOperators { |
| 266 | public: |
| 267 | /* |
| 268 | * NOTE: the functions below ARE NOT member methods. They are friend functions |
| 269 | * with they definition inlined with their declaration. This makes these |
| 270 | * template functions available to the compiler when (and only when) this class |
| 271 | * is instantiated, at which point they're only templated on the 2nd parameter |
| 272 | * (the first one, BASE<T> being known). |
| 273 | */ |
| 274 | template<typename RT> |
| 275 | friend inline |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 276 | bool PURE operator ==(const VECTOR<T>& lv, const VECTOR<RT>& rv) { |
| 277 | for (size_t i = 0; i < lv.size(); i++) |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 278 | if (lv[i] != rv[i]) |
| 279 | return false; |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | template<typename RT> |
| 284 | friend inline |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 285 | bool PURE operator !=(const VECTOR<T>& lv, const VECTOR<RT>& rv) { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 286 | return !operator ==(lv, rv); |
| 287 | } |
| 288 | |
| 289 | template<typename RT> |
| 290 | friend inline |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 291 | bool PURE operator >(const VECTOR<T>& lv, const VECTOR<RT>& rv) { |
| 292 | for (size_t i = 0; i < lv.size(); i++) { |
| 293 | if (lv[i] == rv[i]) { |
| 294 | continue; |
| 295 | } |
| 296 | return lv[i] > rv[i]; |
| 297 | } |
| 298 | return false; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | template<typename RT> |
| 302 | friend inline |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 303 | constexpr bool PURE operator <=(const VECTOR<T>& lv, const VECTOR<RT>& rv) { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 304 | return !(lv > rv); |
| 305 | } |
| 306 | |
| 307 | template<typename RT> |
| 308 | friend inline |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 309 | bool PURE operator <(const VECTOR<T>& lv, const VECTOR<RT>& rv) { |
| 310 | for (size_t i = 0; i < lv.size(); i++) { |
| 311 | if (lv[i] == rv[i]) { |
| 312 | continue; |
| 313 | } |
| 314 | return lv[i] < rv[i]; |
| 315 | } |
| 316 | return false; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | template<typename RT> |
| 320 | friend inline |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 321 | constexpr bool PURE operator >=(const VECTOR<T>& lv, const VECTOR<RT>& rv) { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 322 | return !(lv < rv); |
| 323 | } |
| 324 | }; |
| 325 | |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 326 | /* |
| 327 | * TVecFunctions implements functions on a vector of type BASE<T>. |
| 328 | * |
| 329 | * BASE only needs to implement operator[] and size(). |
| 330 | * By simply inheriting from TVecFunctions<BASE, T> BASE will automatically |
| 331 | * get all the functionality here. |
| 332 | */ |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 333 | template<template<typename T> class VECTOR, typename T> |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 334 | class TVecFunctions { |
| 335 | public: |
| 336 | /* |
| 337 | * NOTE: the functions below ARE NOT member methods. They are friend functions |
| 338 | * with they definition inlined with their declaration. This makes these |
| 339 | * template functions available to the compiler when (and only when) this class |
| 340 | * is instantiated, at which point they're only templated on the 2nd parameter |
| 341 | * (the first one, BASE<T> being known). |
| 342 | */ |
| 343 | template<typename RT> |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 344 | friend inline CONSTEXPR T PURE dot(const VECTOR<T>& lv, const VECTOR<RT>& rv) { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 345 | T r(0); |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 346 | for (size_t i = 0; i < lv.size(); i++) { |
| 347 | //r = std::fma(lv[i], rv[i], r); |
| 348 | r += lv[i] * rv[i]; |
| 349 | } |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 350 | return r; |
| 351 | } |
| 352 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 353 | friend inline constexpr T PURE norm(const VECTOR<T>& lv) { |
| 354 | return std::sqrt(dot(lv, lv)); |
| 355 | } |
| 356 | |
| 357 | friend inline constexpr T PURE length(const VECTOR<T>& lv) { |
| 358 | return norm(lv); |
| 359 | } |
| 360 | |
| 361 | friend inline constexpr T PURE norm2(const VECTOR<T>& lv) { |
| 362 | return dot(lv, lv); |
| 363 | } |
| 364 | |
| 365 | friend inline constexpr T PURE length2(const VECTOR<T>& lv) { |
| 366 | return norm2(lv); |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | template<typename RT> |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 370 | friend inline constexpr T PURE distance(const VECTOR<T>& lv, const VECTOR<RT>& rv) { |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 371 | return length(rv - lv); |
| 372 | } |
| 373 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 374 | template<typename RT> |
| 375 | friend inline constexpr T PURE distance2(const VECTOR<T>& lv, const VECTOR<RT>& rv) { |
| 376 | return length2(rv - lv); |
| 377 | } |
| 378 | |
| 379 | friend inline constexpr VECTOR<T> PURE normalize(const VECTOR<T>& lv) { |
| 380 | return lv * (T(1) / length(lv)); |
| 381 | } |
| 382 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 383 | friend inline constexpr VECTOR<T> PURE rcp(VECTOR<T> v) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 384 | return T(1) / v; |
| 385 | } |
| 386 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 387 | friend inline CONSTEXPR VECTOR<T> PURE abs(VECTOR<T> v) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 388 | for (size_t i=0 ; i<v.size() ; i++) { |
| 389 | v[i] = std::abs(v[i]); |
| 390 | } |
| 391 | return v; |
| 392 | } |
| 393 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 394 | friend inline CONSTEXPR VECTOR<T> PURE floor(VECTOR<T> v) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 395 | for (size_t i=0 ; i<v.size() ; i++) { |
| 396 | v[i] = std::floor(v[i]); |
| 397 | } |
| 398 | return v; |
| 399 | } |
| 400 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 401 | friend inline CONSTEXPR VECTOR<T> PURE ceil(VECTOR<T> v) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 402 | for (size_t i=0 ; i<v.size() ; i++) { |
| 403 | v[i] = std::ceil(v[i]); |
| 404 | } |
| 405 | return v; |
| 406 | } |
| 407 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 408 | friend inline CONSTEXPR VECTOR<T> PURE round(VECTOR<T> v) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 409 | for (size_t i=0 ; i<v.size() ; i++) { |
| 410 | v[i] = std::round(v[i]); |
| 411 | } |
| 412 | return v; |
| 413 | } |
| 414 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 415 | friend inline CONSTEXPR VECTOR<T> PURE inversesqrt(VECTOR<T> v) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 416 | for (size_t i=0 ; i<v.size() ; i++) { |
| 417 | v[i] = T(1) / std::sqrt(v[i]); |
| 418 | } |
| 419 | return v; |
| 420 | } |
| 421 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 422 | friend inline CONSTEXPR VECTOR<T> PURE sqrt(VECTOR<T> v) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 423 | for (size_t i=0 ; i<v.size() ; i++) { |
| 424 | v[i] = std::sqrt(v[i]); |
| 425 | } |
| 426 | return v; |
| 427 | } |
| 428 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 429 | friend inline CONSTEXPR VECTOR<T> PURE pow(VECTOR<T> v, T p) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 430 | for (size_t i=0 ; i<v.size() ; i++) { |
| 431 | v[i] = std::pow(v[i], p); |
| 432 | } |
| 433 | return v; |
| 434 | } |
| 435 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 436 | friend inline CONSTEXPR VECTOR<T> PURE saturate(const VECTOR<T>& lv) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 437 | return clamp(lv, T(0), T(1)); |
| 438 | } |
| 439 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 440 | friend inline CONSTEXPR VECTOR<T> PURE clamp(VECTOR<T> v, T min, T max) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 441 | for (size_t i=0 ; i< v.size() ; i++) { |
| 442 | v[i] = std::min(max, std::max(min, v[i])); |
| 443 | } |
| 444 | return v; |
| 445 | } |
| 446 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 447 | friend inline CONSTEXPR VECTOR<T> PURE fma(const VECTOR<T>& lv, const VECTOR<T>& rv, VECTOR<T> a) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 448 | for (size_t i=0 ; i<lv.size() ; i++) { |
| 449 | //a[i] = std::fma(lv[i], rv[i], a[i]); |
| 450 | a[i] += (lv[i] * rv[i]); |
| 451 | } |
| 452 | return a; |
| 453 | } |
| 454 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 455 | friend inline CONSTEXPR VECTOR<T> PURE min(const VECTOR<T>& u, VECTOR<T> v) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 456 | for (size_t i=0 ; i<v.size() ; i++) { |
| 457 | v[i] = std::min(u[i], v[i]); |
| 458 | } |
| 459 | return v; |
| 460 | } |
| 461 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 462 | friend inline CONSTEXPR VECTOR<T> PURE max(const VECTOR<T>& u, VECTOR<T> v) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 463 | for (size_t i=0 ; i<v.size() ; i++) { |
| 464 | v[i] = std::max(u[i], v[i]); |
| 465 | } |
| 466 | return v; |
| 467 | } |
| 468 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 469 | friend inline CONSTEXPR T PURE max(const VECTOR<T>& v) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 470 | T r(std::numeric_limits<T>::lowest()); |
| 471 | for (size_t i=0 ; i<v.size() ; i++) { |
| 472 | r = std::max(r, v[i]); |
| 473 | } |
| 474 | return r; |
| 475 | } |
| 476 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 477 | friend inline CONSTEXPR T PURE min(const VECTOR<T>& v) { |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 478 | T r(std::numeric_limits<T>::max()); |
| 479 | for (size_t i=0 ; i<v.size() ; i++) { |
| 480 | r = std::min(r, v[i]); |
| 481 | } |
| 482 | return r; |
| 483 | } |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 484 | |
| 485 | friend inline CONSTEXPR VECTOR<T> PURE apply(VECTOR<T> v, const std::function<T(T)>& f) { |
| 486 | for (size_t i=0 ; i<v.size() ; i++) { |
| 487 | v[i] = f(v[i]); |
| 488 | } |
| 489 | return v; |
| 490 | } |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 491 | }; |
| 492 | |
| 493 | /* |
| 494 | * TVecDebug implements functions on a vector of type BASE<T>. |
| 495 | * |
| 496 | * BASE only needs to implement operator[] and size(). |
| 497 | * By simply inheriting from TVecDebug<BASE, T> BASE will automatically |
| 498 | * get all the functionality here. |
| 499 | */ |
| 500 | template<template<typename T> class VECTOR, typename T> |
| 501 | class TVecDebug { |
| 502 | public: |
| 503 | /* |
| 504 | * NOTE: the functions below ARE NOT member methods. They are friend functions |
| 505 | * with they definition inlined with their declaration. This makes these |
| 506 | * template functions available to the compiler when (and only when) this class |
| 507 | * is instantiated, at which point they're only templated on the 2nd parameter |
| 508 | * (the first one, BASE<T> being known). |
| 509 | */ |
| 510 | friend std::ostream& operator<<(std::ostream& stream, const VECTOR<T>& v) { |
| 511 | stream << "< "; |
| 512 | for (size_t i = 0; i < v.size() - 1; i++) { |
| 513 | stream << T(v[i]) << ", "; |
| 514 | } |
| 515 | stream << T(v[v.size() - 1]) << " >"; |
| 516 | return stream; |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 517 | } |
| 518 | }; |
| 519 | |
Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 520 | #undef CONSTEXPR |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 521 | #undef PURE |
| 522 | |
| 523 | // ------------------------------------------------------------------------------------- |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 524 | } // namespace details |
| 525 | } // namespace android |
Mathias Agopian | 595ea77 | 2013-08-21 23:10:41 -0700 | [diff] [blame] | 526 | |
| 527 | |
Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 528 | #endif // UI_TVECHELPERS_H_ |