2
0

curve.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*************************************************************************/
  2. /* curve.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. int _minmax_set_once; // Encodes whether min and max have been set a first time, first bit for min and second for max.
  117. };
  118. VARIANT_ENUM_CAST(Curve::TangentMode)
  119. class Curve2D : public Resource {
  120. GDCLASS(Curve2D, Resource);
  121. struct Point {
  122. Vector2 in;
  123. Vector2 out;
  124. Vector2 pos;
  125. };
  126. Vector<Point> points;
  127. struct BakedPoint {
  128. float ofs;
  129. Vector2 point;
  130. };
  131. mutable bool baked_cache_dirty;
  132. mutable PoolVector2Array baked_point_cache;
  133. mutable float baked_max_ofs;
  134. void _bake() const;
  135. float bake_interval;
  136. 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;
  137. Dictionary _get_data() const;
  138. void _set_data(const Dictionary &p_data);
  139. protected:
  140. static void _bind_methods();
  141. public:
  142. int get_point_count() const;
  143. void add_point(const Vector2 &p_pos, const Vector2 &p_in = Vector2(), const Vector2 &p_out = Vector2(), int p_atpos = -1);
  144. void set_point_position(int p_index, const Vector2 &p_pos);
  145. Vector2 get_point_position(int p_index) const;
  146. void set_point_in(int p_index, const Vector2 &p_in);
  147. Vector2 get_point_in(int p_index) const;
  148. void set_point_out(int p_index, const Vector2 &p_out);
  149. Vector2 get_point_out(int p_index) const;
  150. void remove_point(int p_index);
  151. void clear_points();
  152. Vector2 interpolate(int p_index, float p_offset) const;
  153. Vector2 interpolatef(real_t p_findex) const;
  154. void set_bake_interval(float p_tolerance);
  155. float get_bake_interval() const;
  156. float get_baked_length() const;
  157. Vector2 interpolate_baked(float p_offset, bool p_cubic = false) const;
  158. PoolVector2Array get_baked_points() const; //useful for going through
  159. Vector2 get_closest_point(const Vector2 &p_to_point) const;
  160. float get_closest_offset(const Vector2 &p_to_point) const;
  161. PoolVector2Array tessellate(int p_max_stages = 5, float p_tolerance = 4) const; //useful for display
  162. Curve2D();
  163. };
  164. class Curve3D : public Resource {
  165. GDCLASS(Curve3D, Resource);
  166. struct Point {
  167. Vector3 in;
  168. Vector3 out;
  169. Vector3 pos;
  170. float tilt;
  171. Point() { tilt = 0; }
  172. };
  173. Vector<Point> points;
  174. struct BakedPoint {
  175. float ofs;
  176. Vector3 point;
  177. };
  178. mutable bool baked_cache_dirty;
  179. mutable PoolVector3Array baked_point_cache;
  180. mutable PoolRealArray baked_tilt_cache;
  181. mutable PoolVector3Array baked_up_vector_cache;
  182. mutable float baked_max_ofs;
  183. void _bake() const;
  184. float bake_interval;
  185. bool up_vector_enabled;
  186. 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;
  187. Dictionary _get_data() const;
  188. void _set_data(const Dictionary &p_data);
  189. protected:
  190. static void _bind_methods();
  191. public:
  192. int get_point_count() const;
  193. void add_point(const Vector3 &p_pos, const Vector3 &p_in = Vector3(), const Vector3 &p_out = Vector3(), int p_atpos = -1);
  194. void set_point_position(int p_index, const Vector3 &p_pos);
  195. Vector3 get_point_position(int p_index) const;
  196. void set_point_tilt(int p_index, float p_tilt);
  197. float get_point_tilt(int p_index) const;
  198. void set_point_in(int p_index, const Vector3 &p_in);
  199. Vector3 get_point_in(int p_index) const;
  200. void set_point_out(int p_index, const Vector3 &p_out);
  201. Vector3 get_point_out(int p_index) const;
  202. void remove_point(int p_index);
  203. void clear_points();
  204. Vector3 interpolate(int p_index, float p_offset) const;
  205. Vector3 interpolatef(real_t p_findex) const;
  206. void set_bake_interval(float p_tolerance);
  207. float get_bake_interval() const;
  208. void set_up_vector_enabled(bool p_enable);
  209. bool is_up_vector_enabled() const;
  210. float get_baked_length() const;
  211. Vector3 interpolate_baked(float p_offset, bool p_cubic = false) const;
  212. float interpolate_baked_tilt(float p_offset) const;
  213. Vector3 interpolate_baked_up_vector(float p_offset, bool p_apply_tilt = false) const;
  214. PoolVector3Array get_baked_points() const; //useful for going through
  215. PoolRealArray get_baked_tilts() const; //useful for going through
  216. PoolVector3Array get_baked_up_vectors() const;
  217. Vector3 get_closest_point(const Vector3 &p_to_point) const;
  218. float get_closest_offset(const Vector3 &p_to_point) const;
  219. PoolVector3Array tessellate(int p_max_stages = 5, float p_tolerance = 4) const; //useful for display
  220. Curve3D();
  221. };
  222. #endif // CURVE_H