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