vector3.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2016, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file vector3.h
  35. * @brief 3D vector structure, including operators when compiling in C++
  36. */
  37. #pragma once
  38. #ifndef AI_VECTOR3D_H_INC
  39. #define AI_VECTOR3D_H_INC
  40. #ifdef __cplusplus
  41. # include <cmath>
  42. #else
  43. # include <math.h>
  44. #endif
  45. #include "defs.h"
  46. #ifdef __cplusplus
  47. template<typename TReal> class aiMatrix3x3t;
  48. template<typename TReal> class aiMatrix4x4t;
  49. // ---------------------------------------------------------------------------
  50. /** Represents a three-dimensional vector. */
  51. template <typename TReal>
  52. class aiVector3t
  53. {
  54. public:
  55. aiVector3t () : x(), y(), z() {}
  56. aiVector3t (TReal _x, TReal _y, TReal _z) : x(_x), y(_y), z(_z) {}
  57. explicit aiVector3t (TReal _xyz) : x(_xyz), y(_xyz), z(_xyz) {}
  58. aiVector3t (const aiVector3t& o) : x(o.x), y(o.y), z(o.z) {}
  59. public:
  60. // combined operators
  61. const aiVector3t& operator += (const aiVector3t& o);
  62. const aiVector3t& operator -= (const aiVector3t& o);
  63. const aiVector3t& operator *= (TReal f);
  64. const aiVector3t& operator /= (TReal f);
  65. // transform vector by matrix
  66. aiVector3t& operator *= (const aiMatrix3x3t<TReal>& mat);
  67. aiVector3t& operator *= (const aiMatrix4x4t<TReal>& mat);
  68. // access a single element
  69. TReal operator[](unsigned int i) const;
  70. TReal& operator[](unsigned int i);
  71. // comparison
  72. bool operator== (const aiVector3t& other) const;
  73. bool operator!= (const aiVector3t& other) const;
  74. bool operator < (const aiVector3t& other) const;
  75. bool Equal(const aiVector3t& other, TReal epsilon = 1e-6) const;
  76. template <typename TOther>
  77. operator aiVector3t<TOther> () const;
  78. public:
  79. /** @brief Set the components of a vector
  80. * @param pX X component
  81. * @param pY Y component
  82. * @param pZ Z component */
  83. void Set( TReal pX, TReal pY, TReal pZ);
  84. /** @brief Get the squared length of the vector
  85. * @return Square length */
  86. TReal SquareLength() const;
  87. /** @brief Get the length of the vector
  88. * @return length */
  89. TReal Length() const;
  90. /** @brief Normalize the vector */
  91. aiVector3t& Normalize();
  92. /** @brief Normalize the vector with extra check for zero vectors */
  93. aiVector3t& NormalizeSafe();
  94. /** @brief Componentwise multiplication of two vectors
  95. *
  96. * Note that vec*vec yields the dot product.
  97. * @param o Second factor */
  98. const aiVector3t SymMul(const aiVector3t& o);
  99. TReal x, y, z;
  100. };
  101. typedef aiVector3t<ai_real> aiVector3D;
  102. #else
  103. struct aiVector3D {
  104. ai_real x, y, z;
  105. };
  106. #endif // __cplusplus
  107. #ifdef __cplusplus
  108. #endif // __cplusplus
  109. #endif // AI_VECTOR3D_H_INC