curve.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*************************************************************************/
  2. /* curve.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #ifndef CURVE_H
  31. #define CURVE_H
  32. #include "core/resource.h"
  33. // y(x) curve
  34. class Curve : public Resource {
  35. GDCLASS(Curve, Resource)
  36. public:
  37. static const int MIN_X = 0.f;
  38. static const int MAX_X = 1.f;
  39. static const char *SIGNAL_RANGE_CHANGED;
  40. enum TangentMode {
  41. TANGENT_FREE = 0,
  42. TANGENT_LINEAR,
  43. TANGENT_MODE_COUNT
  44. };
  45. struct Point {
  46. Vector2 pos;
  47. real_t left_tangent;
  48. real_t right_tangent;
  49. TangentMode left_mode;
  50. TangentMode right_mode;
  51. Point() {
  52. left_tangent = 0;
  53. right_tangent = 0;
  54. left_mode = TANGENT_FREE;
  55. right_mode = TANGENT_FREE;
  56. }
  57. Point(Vector2 p_pos,
  58. real_t p_left = 0,
  59. real_t p_right = 0,
  60. TangentMode p_left_mode = TANGENT_FREE,
  61. TangentMode p_right_mode = TANGENT_FREE) {
  62. pos = p_pos;
  63. left_tangent = p_left;
  64. right_tangent = p_right;
  65. left_mode = p_left_mode;
  66. right_mode = p_right_mode;
  67. }
  68. };
  69. Curve();
  70. int get_point_count() const { return _points.size(); }
  71. int add_point(Vector2 p_pos,
  72. real_t left_tangent = 0,
  73. real_t right_tangent = 0,
  74. TangentMode left_mode = TANGENT_FREE,
  75. TangentMode right_mode = TANGENT_FREE);
  76. void remove_point(int p_index);
  77. void clear_points();
  78. int get_index(real_t offset) const;
  79. void set_point_value(int p_index, real_t pos);
  80. int set_point_offset(int p_index, float offset);
  81. Vector2 get_point_position(int p_index) const;
  82. Point get_point(int p_index) const;
  83. float get_min_value() const { return _min_value; }
  84. void set_min_value(float p_min);
  85. float get_max_value() const { return _max_value; }
  86. void set_max_value(float p_max);
  87. real_t interpolate(real_t offset) const;
  88. real_t interpolate_local_nocheck(int index, real_t local_offset) const;
  89. void clean_dupes();
  90. void set_point_left_tangent(int i, real_t tangent);
  91. void set_point_right_tangent(int i, real_t tangent);
  92. void set_point_left_mode(int i, TangentMode p_mode);
  93. void set_point_right_mode(int i, TangentMode p_mode);
  94. real_t get_point_left_tangent(int i) const;
  95. real_t get_point_right_tangent(int i) const;
  96. TangentMode get_point_left_mode(int i) const;
  97. TangentMode get_point_right_mode(int i) const;
  98. void update_auto_tangents(int i);
  99. Array get_data() const;
  100. void set_data(Array input);
  101. void bake();
  102. int get_bake_resolution() const { return _bake_resolution; }
  103. void set_bake_resolution(int p_resolution);
  104. real_t interpolate_baked(real_t offset);
  105. void ensure_default_setup(float p_min, float p_max);
  106. protected:
  107. static void _bind_methods();
  108. private:
  109. void mark_dirty();
  110. Vector<Point> _points;
  111. bool _baked_cache_dirty;
  112. Vector<real_t> _baked_cache;
  113. int _bake_resolution;
  114. float _min_value;
  115. float _max_value;
  116. };
  117. VARIANT_ENUM_CAST(Curve::TangentMode)
  118. class Curve2D : public Resource {
  119. GDCLASS(Curve2D, Resource);
  120. struct Point {
  121. Vector2 in;
  122. Vector2 out;
  123. Vector2 pos;
  124. };
  125. Vector<Point> points;
  126. struct BakedPoint {
  127. float ofs;
  128. Vector2 point;
  129. };
  130. mutable bool baked_cache_dirty;
  131. mutable PoolVector2Array baked_point_cache;
  132. mutable float baked_max_ofs;
  133. void _bake() const;
  134. float bake_interval;
  135. void _bake_segment2d(Map<float, Vector2> &r_bake, float p_begin, float p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_max_depth, float p_tol) const;
  136. Dictionary _get_data() const;
  137. void _set_data(const Dictionary &p_data);
  138. protected:
  139. static void _bind_methods();
  140. public:
  141. int get_point_count() const;
  142. void add_point(const Vector2 &p_pos, const Vector2 &p_in = Vector2(), const Vector2 &p_out = Vector2(), int p_atpos = -1);
  143. void set_point_position(int p_index, const Vector2 &p_pos);
  144. Vector2 get_point_position(int p_index) const;
  145. void set_point_in(int p_index, const Vector2 &p_in);
  146. Vector2 get_point_in(int p_index) const;
  147. void set_point_out(int p_index, const Vector2 &p_out);
  148. Vector2 get_point_out(int p_index) const;
  149. void remove_point(int p_index);
  150. void clear_points();
  151. Vector2 interpolate(int p_index, float p_offset) const;
  152. Vector2 interpolatef(real_t p_findex) const;
  153. void set_bake_interval(float p_tolerance);
  154. float get_bake_interval() const;
  155. float get_baked_length() const;
  156. Vector2 interpolate_baked(float p_offset, bool p_cubic = false) const;
  157. PoolVector2Array get_baked_points() const; //useful for going through
  158. Vector2 get_closest_point(const Vector2 &p_to_point) const;
  159. float get_closest_offset(const Vector2 &p_to_point) const;
  160. PoolVector2Array tessellate(int p_max_stages = 5, float p_tolerance = 4) const; //useful for display
  161. Curve2D();
  162. };
  163. class Curve3D : public Resource {
  164. GDCLASS(Curve3D, Resource);
  165. struct Point {
  166. Vector3 in;
  167. Vector3 out;
  168. Vector3 pos;
  169. float tilt;
  170. Point() { tilt = 0; }
  171. };
  172. Vector<Point> points;
  173. struct BakedPoint {
  174. float ofs;
  175. Vector3 point;
  176. };
  177. mutable bool baked_cache_dirty;
  178. mutable PoolVector3Array baked_point_cache;
  179. mutable PoolRealArray baked_tilt_cache;
  180. mutable PoolVector3Array baked_up_vector_cache;
  181. mutable float baked_max_ofs;
  182. void _bake() const;
  183. float bake_interval;
  184. bool up_vector_enabled;
  185. void _bake_segment3d(Map<float, Vector3> &r_bake, float p_begin, float p_end, const Vector3 &p_a, const Vector3 &p_out, const Vector3 &p_b, const Vector3 &p_in, int p_depth, int p_max_depth, float p_tol) const;
  186. Dictionary _get_data() const;
  187. void _set_data(const Dictionary &p_data);
  188. protected:
  189. static void _bind_methods();
  190. public:
  191. int get_point_count() const;
  192. void add_point(const Vector3 &p_pos, const Vector3 &p_in = Vector3(), const Vector3 &p_out = Vector3(), int p_atpos = -1);
  193. void set_point_position(int p_index, const Vector3 &p_pos);
  194. Vector3 get_point_position(int p_index) const;
  195. void set_point_tilt(int p_index, float p_tilt);
  196. float get_point_tilt(int p_index) const;
  197. void set_point_in(int p_index, const Vector3 &p_in);
  198. Vector3 get_point_in(int p_index) const;
  199. void set_point_out(int p_index, const Vector3 &p_out);
  200. Vector3 get_point_out(int p_index) const;
  201. void remove_point(int p_index);
  202. void clear_points();
  203. Vector3 interpolate(int p_index, float p_offset) const;
  204. Vector3 interpolatef(real_t p_findex) const;
  205. void set_bake_interval(float p_tolerance);
  206. float get_bake_interval() const;
  207. void set_up_vector_enabled(bool p_enable);
  208. bool is_up_vector_enabled() const;
  209. float get_baked_length() const;
  210. Vector3 interpolate_baked(float p_offset, bool p_cubic = false) const;
  211. float interpolate_baked_tilt(float p_offset) const;
  212. Vector3 interpolate_baked_up_vector(float p_offset, bool p_apply_tilt = false) const;
  213. PoolVector3Array get_baked_points() const; //useful for going through
  214. PoolRealArray get_baked_tilts() const; //useful for going through
  215. PoolVector3Array get_baked_up_vectors() const;
  216. Vector3 get_closest_point(const Vector3 &p_to_point) const;
  217. float get_closest_offset(const Vector3 &p_to_point) const;
  218. PoolVector3Array tessellate(int p_max_stages = 5, float p_tolerance = 4) const; //useful for display
  219. Curve3D();
  220. };
  221. #endif // CURVE_H