Vec3.inl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. Copyright 2013 BlackBerry Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. Original file from GamePlay3D: http://gameplay3d.org
  13. This file was modified to fit the cocos2d-x project
  14. */
  15. #include "math/Vec3.h"
  16. #include "math/Mat4.h"
  17. NS_CC_MATH_BEGIN
  18. inline bool Vec3::isZero() const
  19. {
  20. return x == 0.0f && y == 0.0f && z == 0.0f;
  21. }
  22. inline bool Vec3::isOne() const
  23. {
  24. return x == 1.0f && y == 1.0f && z == 1.0f;
  25. }
  26. inline void Vec3::add(const Vec3& v)
  27. {
  28. x += v.x;
  29. y += v.y;
  30. z += v.z;
  31. }
  32. inline void Vec3::add(float xx, float yy, float zz)
  33. {
  34. x += xx;
  35. y += yy;
  36. z += zz;
  37. }
  38. inline float Vec3::length() const
  39. {
  40. return std::sqrt(x * x + y * y + z * z);
  41. }
  42. inline float Vec3::lengthSquared() const
  43. {
  44. return (x * x + y * y + z * z);
  45. }
  46. inline void Vec3::negate()
  47. {
  48. x = -x;
  49. y = -y;
  50. z = -z;
  51. }
  52. inline void Vec3::scale(float scalar)
  53. {
  54. x *= scalar;
  55. y *= scalar;
  56. z *= scalar;
  57. }
  58. inline Vec3 Vec3::lerp(const Vec3 &target, float alpha) const
  59. {
  60. return *this * (1.f - alpha) + target * alpha;
  61. }
  62. inline void Vec3::set(float xx, float yy, float zz)
  63. {
  64. this->x = xx;
  65. this->y = yy;
  66. this->z = zz;
  67. }
  68. inline void Vec3::set(const float* array)
  69. {
  70. GP_ASSERT(array);
  71. x = array[0];
  72. y = array[1];
  73. z = array[2];
  74. }
  75. inline void Vec3::set(const Vec3& v)
  76. {
  77. this->x = v.x;
  78. this->y = v.y;
  79. this->z = v.z;
  80. }
  81. inline void Vec3::set(const Vec3& p1, const Vec3& p2)
  82. {
  83. x = p2.x - p1.x;
  84. y = p2.y - p1.y;
  85. z = p2.z - p1.z;
  86. }
  87. inline void Vec3::setZero()
  88. {
  89. x = y = z = 0.0f;
  90. }
  91. inline void Vec3::subtract(const Vec3& v)
  92. {
  93. x -= v.x;
  94. y -= v.y;
  95. z -= v.z;
  96. }
  97. inline Vec3 Vec3::operator+(const Vec3& v) const
  98. {
  99. Vec3 result(*this);
  100. result.add(v);
  101. return result;
  102. }
  103. inline Vec3& Vec3::operator+=(const Vec3& v)
  104. {
  105. add(v);
  106. return *this;
  107. }
  108. inline Vec3 Vec3::operator-(const Vec3& v) const
  109. {
  110. Vec3 result(*this);
  111. result.subtract(v);
  112. return result;
  113. }
  114. inline Vec3& Vec3::operator-=(const Vec3& v)
  115. {
  116. subtract(v);
  117. return *this;
  118. }
  119. inline Vec3 Vec3::operator-() const
  120. {
  121. Vec3 result(*this);
  122. result.negate();
  123. return result;
  124. }
  125. inline Vec3 Vec3::operator*(float s) const
  126. {
  127. Vec3 result(*this);
  128. result.scale(s);
  129. return result;
  130. }
  131. inline Vec3& Vec3::operator*=(float s)
  132. {
  133. scale(s);
  134. return *this;
  135. }
  136. inline Vec3 Vec3::operator/(const float s) const
  137. {
  138. return Vec3(this->x / s, this->y / s, this->z / s);
  139. }
  140. inline bool Vec3::operator==(const Vec3& v) const
  141. {
  142. return x==v.x && y==v.y && z==v.z;
  143. }
  144. inline bool Vec3::operator!=(const Vec3& v) const
  145. {
  146. return x!=v.x || y!=v.y || z!=v.z;
  147. }
  148. inline Vec3 operator*(float x, const Vec3& v)
  149. {
  150. Vec3 result(v);
  151. result.scale(x);
  152. return result;
  153. }
  154. NS_CC_MATH_END