1 // 2 // Copyright 2016 Pixar 3 // 4 // Licensed under the Apache License, Version 2.0 (the "Apache License") 5 // with the following modification; you may not use this file except in 6 // compliance with the Apache License and the following modification to it: 7 // Section 6. Trademarks. is deleted and replaced with: 8 // 9 // 6. Trademarks. This License does not grant permission to use the trade 10 // names, trademarks, service marks, or product names of the Licensor 11 // and its affiliates, except as required to comply with Section 4(c) of 12 // the License and to reproduce the content of the NOTICE file. 13 // 14 // You may obtain a copy of the Apache License at 15 // 16 // http://www.apache.org/licenses/LICENSE-2.0 17 // 18 // Unless required by applicable law or agreed to in writing, software 19 // distributed under the Apache License with the above modification is 20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 21 // KIND, either express or implied. See the Apache License for the specific 22 // language governing permissions and limitations under the Apache License. 23 // 24 //////////////////////////////////////////////////////////////////////// 25 // This file is generated by a script. Do not edit directly. Edit the 26 // vec.template.h file to make changes. 27 28 #ifndef PXR_BASE_GF_VEC2H_H 29 #define PXR_BASE_GF_VEC2H_H 30 31 /// \file gf/vec2h.h 32 /// \ingroup group_gf_LinearAlgebra 33 34 #include "pxr/pxr.h" 35 #include "pxr/base/tf/diagnostic.h" 36 #include "pxr/base/gf/api.h" 37 #include "pxr/base/gf/limits.h" 38 #include "pxr/base/gf/traits.h" 39 #include "pxr/base/gf/math.h" 40 #include "pxr/base/gf/half.h" 41 42 #include <boost/functional/hash.hpp> 43 44 #include <cstddef> 45 #include <cmath> 46 47 #include <iosfwd> 48 49 PXR_NAMESPACE_OPEN_SCOPE 50 51 class GfVec2h; 52 53 template <> 54 struct GfIsGfVec<class GfVec2h> { static const bool value = true; }; 55 56 /// \class GfVec2h 57 /// \ingroup group_gf_LinearAlgebra 58 /// 59 /// Basic type for a vector of 2 GfHalf components. 60 /// 61 /// Represents a vector of 2 components of type \c GfHalf. 62 /// It is intended to be fast and simple. 63 /// 64 class GfVec2h 65 { 66 public: 67 /// Scalar element type and dimension. 68 typedef GfHalf ScalarType; 69 static const size_t dimension = 2; 70 71 /// Default constructor does no initialization. 72 GfVec2h() = default; 73 74 /// Initialize all elements to a single value. 75 constexpr explicit GfVec2h(GfHalf value) 76 : _data{ value, value } 77 { 78 } 79 80 /// Initialize all elements with explicit arguments. 81 constexpr GfVec2h(GfHalf s0, GfHalf s1) 82 : _data{ s0, s1 } 83 { 84 } 85 86 /// Construct with pointer to values. 87 template <class Scl> 88 constexpr explicit GfVec2h(Scl const *p) 89 : _data{ p[0], p[1] } 90 { 91 } 92 93 /// Construct from GfVec2d. 94 explicit GfVec2h(class GfVec2d const &other); 95 96 /// Construct from GfVec2f. 97 explicit GfVec2h(class GfVec2f const &other); 98 99 /// Implicitly convert from GfVec2i. 100 GfVec2h(class GfVec2i const &other); 101 102 /// Create a unit vector along the X-axis. 103 static GfVec2h XAxis() { 104 GfVec2h result(0); 105 result[0] = 1; 106 return result; 107 } 108 /// Create a unit vector along the Y-axis. 109 static GfVec2h YAxis() { 110 GfVec2h result(0); 111 result[1] = 1; 112 return result; 113 } 114 115 /// Create a unit vector along the i-th axis, zero-based. Return the zero 116 /// vector if \p i is greater than or equal to 2. 117 static GfVec2h Axis(size_t i) { 118 GfVec2h result(0); 119 if (i < 2) 120 result[i] = 1; 121 return result; 122 } 123 124 /// Set all elements with passed arguments. 125 GfVec2h &Set(GfHalf s0, GfHalf s1) { 126 _data[0] = s0; 127 _data[1] = s1; 128 return *this; 129 } 130 131 /// Set all elements with a pointer to data. 132 GfVec2h &Set(GfHalf const *a) { 133 return Set(a[0], a[1]); 134 } 135 136 /// Direct data access. 137 GfHalf const *data() const { return _data; } 138 GfHalf *data() { return _data; } 139 GfHalf const *GetArray() const { return data(); } 140 141 /// Indexing. 142 GfHalf const &operator[](size_t i) const { return _data[i]; } 143 GfHalf &operator[](size_t i) { return _data[i]; } 144 145 /// Hash. 146 friend inline size_t hash_value(GfVec2h const &vec) { 147 size_t h = 0; 148 boost::hash_combine(h, vec[0]); 149 boost::hash_combine(h, vec[1]); 150 return h; 151 } 152 153 /// Equality comparison. 154 bool operator==(GfVec2h const &other) const { 155 return _data[0] == other[0] && 156 _data[1] == other[1]; 157 } 158 bool operator!=(GfVec2h const &other) const { 159 return !(*this == other); 160 } 161 162 // TODO Add inequality for other vec types... 163 /// Equality comparison. 164 GF_API 165 bool operator==(class GfVec2d const &other) const; 166 /// Equality comparison. 167 GF_API 168 bool operator==(class GfVec2f const &other) const; 169 /// Equality comparison. 170 GF_API 171 bool operator==(class GfVec2i const &other) const; 172 173 /// Create a vec with negated elements. 174 GfVec2h operator-() const { 175 return GfVec2h(-_data[0], -_data[1]); 176 } 177 178 /// Addition. 179 GfVec2h &operator+=(GfVec2h const &other) { 180 _data[0] += other[0]; 181 _data[1] += other[1]; 182 return *this; 183 } 184 friend GfVec2h operator+(GfVec2h const &l, GfVec2h const &r) { 185 return GfVec2h(l) += r; 186 } 187 188 /// Subtraction. 189 GfVec2h &operator-=(GfVec2h const &other) { 190 _data[0] -= other[0]; 191 _data[1] -= other[1]; 192 return *this; 193 } 194 friend GfVec2h operator-(GfVec2h const &l, GfVec2h const &r) { 195 return GfVec2h(l) -= r; 196 } 197 198 /// Multiplication by scalar. 199 GfVec2h &operator*=(double s) { 200 _data[0] *= s; 201 _data[1] *= s; 202 return *this; 203 } 204 GfVec2h operator*(double s) const { 205 return GfVec2h(*this) *= s; 206 } 207 friend GfVec2h operator*(double s, GfVec2h const &v) { 208 return v * s; 209 } 210 211 /// Division by scalar. 212 // TODO should divide by the scalar type. 213 GfVec2h &operator/=(double s) { 214 // TODO This should not multiply by 1/s, it should do the division. 215 // Doing the division is more numerically stable when s is close to 216 // zero. 217 return *this *= (1.0 / s); 218 } 219 GfVec2h operator/(double s) const { 220 return *this * (1.0 / s); 221 } 222 223 /// See GfDot(). 224 GfHalf operator*(GfVec2h const &v) const { 225 return _data[0] * v[0] + _data[1] * v[1]; 226 } 227 228 /// Returns the projection of \p this onto \p v. That is: 229 /// \code 230 /// v * (*this * v) 231 /// \endcode 232 GfVec2h GetProjection(GfVec2h const &v) const { 233 return v * (*this * v); 234 } 235 236 /// Returns the orthogonal complement of \p this->GetProjection(b). 237 /// That is: 238 /// \code 239 /// *this - this->GetProjection(b) 240 /// \endcode 241 GfVec2h GetComplement(GfVec2h const &b) const { 242 return *this - this->GetProjection(b); 243 } 244 245 /// Squared length. 246 GfHalf GetLengthSq() const { 247 return *this * *this; 248 } 249 250 /// Length 251 GfHalf GetLength() const { 252 // TODO should use GfSqrt. 253 return sqrt(GetLengthSq()); 254 } 255 256 /// Normalizes the vector in place to unit length, returning the 257 /// length before normalization. If the length of the vector is 258 /// smaller than \p eps, then the vector is set to vector/\c eps. 259 /// The original length of the vector is returned. See also GfNormalize(). 260 /// 261 /// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug. 262 /// By tickling the code, it no longer tries to write into 263 /// an illegal memory address (in the code section of memory). 264 GfHalf Normalize(GfHalf eps = 0.001) { 265 // TODO this seems suspect... suggest dividing by length so long as 266 // length is not zero. 267 GfHalf length = GetLength(); 268 *this /= (length > eps) ? length : eps; 269 return length; 270 } 271 272 GfVec2h GetNormalized(GfHalf eps = 0.001) const { 273 GfVec2h normalized(*this); 274 normalized.Normalize(eps); 275 return normalized; 276 } 277 278 279 private: 280 GfHalf _data[2]; 281 }; 282 283 /// Output a GfVec2h. 284 /// \ingroup group_gf_DebuggingOutput 285 GF_API std::ostream& operator<<(std::ostream &, GfVec2h const &); 286 287 288 PXR_NAMESPACE_CLOSE_SCOPE 289 290 #include "pxr/base/gf/vec2d.h" 291 #include "pxr/base/gf/vec2f.h" 292 #include "pxr/base/gf/vec2i.h" 293 294 PXR_NAMESPACE_OPEN_SCOPE 295 296 inline 297 GfVec2h::GfVec2h(class GfVec2d const &other) 298 { 299 _data[0] = other[0]; 300 _data[1] = other[1]; 301 } 302 inline 303 GfVec2h::GfVec2h(class GfVec2f const &other) 304 { 305 _data[0] = other[0]; 306 _data[1] = other[1]; 307 } 308 inline 309 GfVec2h::GfVec2h(class GfVec2i const &other) 310 { 311 _data[0] = other[0]; 312 _data[1] = other[1]; 313 } 314 315 /// Returns component-wise multiplication of vectors \p v1 and \p v2. 316 inline GfVec2h 317 GfCompMult(GfVec2h const &v1, GfVec2h const &v2) { 318 return GfVec2h( 319 v1[0] * v2[0], 320 v1[1] * v2[1] 321 ); 322 } 323 324 /// Returns component-wise quotient of vectors \p v1 and \p v2. 325 inline GfVec2h 326 GfCompDiv(GfVec2h const &v1, GfVec2h const &v2) { 327 return GfVec2h( 328 v1[0] / v2[0], 329 v1[1] / v2[1] 330 ); 331 } 332 333 /// Returns the dot (inner) product of two vectors. 334 inline GfHalf 335 GfDot(GfVec2h const &v1, GfVec2h const &v2) { 336 return v1 * v2; 337 } 338 339 340 /// Returns the geometric length of \c v. 341 inline GfHalf 342 GfGetLength(GfVec2h const &v) 343 { 344 return v.GetLength(); 345 } 346 347 /// Normalizes \c *v in place to unit length, returning the length before 348 /// normalization. If the length of \c *v is smaller than \p eps then \c *v is 349 /// set to \c *v/eps. The original length of \c *v is returned. 350 inline GfHalf 351 GfNormalize(GfVec2h *v, GfHalf eps = 0.001) 352 { 353 return v->Normalize(eps); 354 } 355 356 /// Returns a normalized (unit-length) vector with the same direction as \p v. 357 /// If the length of this vector is smaller than \p eps, the vector divided by 358 /// \p eps is returned. 359 inline GfVec2h 360 GfGetNormalized(GfVec2h const &v, GfHalf eps = 0.001) 361 { 362 return v.GetNormalized(eps); 363 } 364 365 /// Returns the projection of \p a onto \p b. That is: 366 /// \code 367 /// b * (a * b) 368 /// \endcode 369 inline GfVec2h 370 GfGetProjection(GfVec2h const &a, GfVec2h const &b) 371 { 372 return a.GetProjection(b); 373 } 374 375 /// Returns the orthogonal complement of \p a.GetProjection(b). That is: 376 /// \code 377 /// a - a.GetProjection(b) 378 /// \endcode 379 inline GfVec2h 380 GfGetComplement(GfVec2h const &a, GfVec2h const &b) 381 { 382 return a.GetComplement(b); 383 } 384 385 /// Tests for equality within a given tolerance, returning \c true if the 386 /// length of the difference vector is less than or equal to \p tolerance. 387 inline bool 388 GfIsClose(GfVec2h const &v1, GfVec2h const &v2, double tolerance) 389 { 390 GfVec2h delta = v1 - v2; 391 return delta.GetLengthSq() <= tolerance * tolerance; 392 } 393 394 395 396 PXR_NAMESPACE_CLOSE_SCOPE 397 398 #endif // PXR_BASE_GF_VEC2H_H 399