vector3.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*************************************************************************/
  2. /* vector3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "vector3.h"
  31. #include "matrix3.h"
  32. void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) {
  33. *this = Basis(p_axis, p_phi).xform(*this);
  34. }
  35. Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_phi) const {
  36. Vector3 r = *this;
  37. r.rotate(p_axis, p_phi);
  38. return r;
  39. }
  40. void Vector3::set_axis(int p_axis, real_t p_value) {
  41. ERR_FAIL_INDEX(p_axis, 3);
  42. coord[p_axis] = p_value;
  43. }
  44. real_t Vector3::get_axis(int p_axis) const {
  45. ERR_FAIL_INDEX_V(p_axis, 3, 0);
  46. return operator[](p_axis);
  47. }
  48. int Vector3::min_axis() const {
  49. return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
  50. }
  51. int Vector3::max_axis() const {
  52. return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
  53. }
  54. void Vector3::snap(Vector3 p_val) {
  55. x = Math::stepify(x, p_val.x);
  56. y = Math::stepify(y, p_val.y);
  57. z = Math::stepify(z, p_val.z);
  58. }
  59. Vector3 Vector3::snapped(Vector3 p_val) const {
  60. Vector3 v = *this;
  61. v.snap(p_val);
  62. return v;
  63. }
  64. Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const {
  65. Vector3 p0 = p_pre_a;
  66. Vector3 p1 = *this;
  67. Vector3 p2 = p_b;
  68. Vector3 p3 = p_post_b;
  69. {
  70. //normalize
  71. real_t ab = p0.distance_to(p1);
  72. real_t bc = p1.distance_to(p2);
  73. real_t cd = p2.distance_to(p3);
  74. if (ab > 0)
  75. p0 = p1 + (p0 - p1) * (bc / ab);
  76. if (cd > 0)
  77. p3 = p2 + (p3 - p2) * (bc / cd);
  78. }
  79. real_t t = p_t;
  80. real_t t2 = t * t;
  81. real_t t3 = t2 * t;
  82. Vector3 out;
  83. out = 0.5 * ((p1 * 2.0) +
  84. (-p0 + p2) * t +
  85. (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
  86. (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
  87. return out;
  88. }
  89. Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const {
  90. Vector3 p0 = p_pre_a;
  91. Vector3 p1 = *this;
  92. Vector3 p2 = p_b;
  93. Vector3 p3 = p_post_b;
  94. real_t t = p_t;
  95. real_t t2 = t * t;
  96. real_t t3 = t2 * t;
  97. Vector3 out;
  98. out = 0.5 * ((p1 * 2.0) +
  99. (-p0 + p2) * t +
  100. (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
  101. (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
  102. return out;
  103. }
  104. Vector3::operator String() const {
  105. return (rtos(x) + ", " + rtos(y) + ", " + rtos(z));
  106. }