curve.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**************************************************************************/
  2. /* curve.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/io/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 position;
  47. real_t left_tangent = 0.0;
  48. real_t right_tangent = 0.0;
  49. TangentMode left_mode = TANGENT_FREE;
  50. TangentMode right_mode = TANGENT_FREE;
  51. Point() {
  52. }
  53. Point(const Vector2 &p_position,
  54. real_t p_left = 0.0,
  55. real_t p_right = 0.0,
  56. TangentMode p_left_mode = TANGENT_FREE,
  57. TangentMode p_right_mode = TANGENT_FREE) {
  58. position = p_position;
  59. left_tangent = p_left;
  60. right_tangent = p_right;
  61. left_mode = p_left_mode;
  62. right_mode = p_right_mode;
  63. }
  64. };
  65. Curve();
  66. int get_point_count() const { return _points.size(); }
  67. void set_point_count(int p_count);
  68. int add_point(Vector2 p_position,
  69. real_t left_tangent = 0,
  70. real_t right_tangent = 0,
  71. TangentMode left_mode = TANGENT_FREE,
  72. TangentMode right_mode = TANGENT_FREE);
  73. int add_point_no_update(Vector2 p_position,
  74. real_t left_tangent = 0,
  75. real_t right_tangent = 0,
  76. TangentMode left_mode = TANGENT_FREE,
  77. TangentMode right_mode = TANGENT_FREE);
  78. void remove_point(int p_index);
  79. void clear_points();
  80. int get_index(real_t p_offset) const;
  81. void set_point_value(int p_index, real_t p_position);
  82. int set_point_offset(int p_index, real_t p_offset);
  83. Vector2 get_point_position(int p_index) const;
  84. Point get_point(int p_index) const;
  85. real_t get_min_value() const { return _min_value; }
  86. void set_min_value(real_t p_min);
  87. real_t get_max_value() const { return _max_value; }
  88. void set_max_value(real_t p_max);
  89. real_t get_range() const { return _max_value - _min_value; }
  90. real_t sample(real_t p_offset) const;
  91. real_t sample_local_nocheck(int p_index, real_t p_local_offset) const;
  92. void clean_dupes();
  93. void set_point_left_tangent(int p_index, real_t p_tangent);
  94. void set_point_right_tangent(int p_index, real_t p_tangent);
  95. void set_point_left_mode(int p_index, TangentMode p_mode);
  96. void set_point_right_mode(int p_index, TangentMode p_mode);
  97. real_t get_point_left_tangent(int p_index) const;
  98. real_t get_point_right_tangent(int p_index) const;
  99. TangentMode get_point_left_mode(int p_index) const;
  100. TangentMode get_point_right_mode(int p_index) const;
  101. void update_auto_tangents(int i);
  102. Array get_data() const;
  103. void set_data(Array input);
  104. void bake();
  105. int get_bake_resolution() const { return _bake_resolution; }
  106. void set_bake_resolution(int p_resolution);
  107. real_t sample_baked(real_t p_offset) const;
  108. void ensure_default_setup(real_t p_min, real_t p_max);
  109. bool _set(const StringName &p_name, const Variant &p_value);
  110. bool _get(const StringName &p_name, Variant &r_ret) const;
  111. void _get_property_list(List<PropertyInfo> *p_list) const;
  112. protected:
  113. static void _bind_methods();
  114. private:
  115. void mark_dirty();
  116. int _add_point(Vector2 p_position,
  117. real_t left_tangent = 0,
  118. real_t right_tangent = 0,
  119. TangentMode left_mode = TANGENT_FREE,
  120. TangentMode right_mode = TANGENT_FREE);
  121. void _remove_point(int p_index);
  122. Vector<Point> _points;
  123. bool _baked_cache_dirty = false;
  124. Vector<real_t> _baked_cache;
  125. int _bake_resolution = 100;
  126. real_t _min_value = 0.0;
  127. real_t _max_value = 1.0;
  128. int _minmax_set_once = 0b00; // Encodes whether min and max have been set a first time, first bit for min and second for max.
  129. };
  130. VARIANT_ENUM_CAST(Curve::TangentMode)
  131. class Curve2D : public Resource {
  132. GDCLASS(Curve2D, Resource);
  133. struct Point {
  134. Vector2 in;
  135. Vector2 out;
  136. Vector2 position;
  137. };
  138. Vector<Point> points;
  139. struct BakedPoint {
  140. real_t ofs = 0.0;
  141. Vector2 point;
  142. };
  143. mutable bool baked_cache_dirty = false;
  144. mutable PackedVector2Array baked_point_cache;
  145. mutable PackedVector2Array baked_forward_vector_cache;
  146. mutable Vector<real_t> baked_dist_cache;
  147. mutable real_t baked_max_ofs = 0.0;
  148. void mark_dirty();
  149. static Vector2 _calculate_tangent(const Vector2 &p_begin, const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t);
  150. void _bake() const;
  151. real_t bake_interval = 5.0;
  152. struct Interval {
  153. int idx;
  154. real_t frac;
  155. };
  156. Interval _find_interval(real_t p_offset) const;
  157. Vector2 _sample_baked(Interval p_interval, bool p_cubic) const;
  158. Transform2D _sample_posture(Interval p_interval) const;
  159. void _bake_segment2d(RBMap<real_t, Vector2> &r_bake, real_t p_begin, real_t 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, real_t p_tol) const;
  160. void _bake_segment2d_even_length(RBMap<real_t, Vector2> &r_bake, real_t p_begin, real_t 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, real_t p_length) const;
  161. Dictionary _get_data() const;
  162. void _set_data(const Dictionary &p_data);
  163. bool _set(const StringName &p_name, const Variant &p_value);
  164. bool _get(const StringName &p_name, Variant &r_ret) const;
  165. void _get_property_list(List<PropertyInfo> *p_list) const;
  166. void _add_point(const Vector2 &p_position, const Vector2 &p_in = Vector2(), const Vector2 &p_out = Vector2(), int p_atpos = -1);
  167. void _remove_point(int p_index);
  168. Vector<RBMap<real_t, Vector2>> _tessellate_even_length(int p_max_stages = 5, real_t p_length = 0.2) const;
  169. protected:
  170. static void _bind_methods();
  171. public:
  172. int get_point_count() const;
  173. void set_point_count(int p_count);
  174. void add_point(const Vector2 &p_position, const Vector2 &p_in = Vector2(), const Vector2 &p_out = Vector2(), int p_atpos = -1);
  175. void set_point_position(int p_index, const Vector2 &p_position);
  176. Vector2 get_point_position(int p_index) const;
  177. void set_point_in(int p_index, const Vector2 &p_in);
  178. Vector2 get_point_in(int p_index) const;
  179. void set_point_out(int p_index, const Vector2 &p_out);
  180. Vector2 get_point_out(int p_index) const;
  181. void remove_point(int p_index);
  182. void clear_points();
  183. Vector2 sample(int p_index, real_t p_offset) const;
  184. Vector2 samplef(real_t p_findex) const;
  185. void set_bake_interval(real_t p_tolerance);
  186. real_t get_bake_interval() const;
  187. real_t get_baked_length() const;
  188. Vector2 sample_baked(real_t p_offset, bool p_cubic = false) const;
  189. Transform2D sample_baked_with_rotation(real_t p_offset, bool p_cubic = false) const;
  190. PackedVector2Array get_points() const;
  191. PackedVector2Array get_baked_points() const; //useful for going through
  192. Vector2 get_closest_point(const Vector2 &p_to_point) const;
  193. real_t get_closest_offset(const Vector2 &p_to_point) const;
  194. PackedVector2Array tessellate(int p_max_stages = 5, real_t p_tolerance = 4) const; //useful for display
  195. PackedVector2Array tessellate_even_length(int p_max_stages = 5, real_t p_length = 20.0) const; // Useful for baking.
  196. Curve2D();
  197. };
  198. class Curve3D : public Resource {
  199. GDCLASS(Curve3D, Resource);
  200. struct Point {
  201. Vector3 in;
  202. Vector3 out;
  203. Vector3 position;
  204. real_t tilt = 0.0;
  205. };
  206. Vector<Point> points;
  207. #ifdef TOOLS_ENABLED
  208. // For Path3DGizmo.
  209. mutable Vector<size_t> points_in_cache;
  210. #endif
  211. bool closed = false;
  212. mutable bool baked_cache_dirty = false;
  213. mutable PackedVector3Array baked_point_cache;
  214. mutable Vector<real_t> baked_tilt_cache;
  215. mutable PackedVector3Array baked_up_vector_cache;
  216. mutable PackedVector3Array baked_forward_vector_cache;
  217. mutable Vector<real_t> baked_dist_cache;
  218. mutable real_t baked_max_ofs = 0.0;
  219. void mark_dirty();
  220. static Vector3 _calculate_tangent(const Vector3 &p_begin, const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t);
  221. void _bake() const;
  222. struct Interval {
  223. int idx;
  224. real_t frac;
  225. };
  226. Interval _find_interval(real_t p_offset) const;
  227. Vector3 _sample_baked(Interval p_interval, bool p_cubic) const;
  228. real_t _sample_baked_tilt(Interval p_interval) const;
  229. Basis _sample_posture(Interval p_interval, bool p_apply_tilt = false) const;
  230. Basis _compose_posture(int p_index) const;
  231. real_t bake_interval = 0.2;
  232. bool up_vector_enabled = true;
  233. void _bake_segment3d(RBMap<real_t, Vector3> &r_bake, real_t p_begin, real_t 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, real_t p_tol) const;
  234. void _bake_segment3d_even_length(RBMap<real_t, Vector3> &r_bake, real_t p_begin, real_t 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, real_t p_length) const;
  235. Dictionary _get_data() const;
  236. void _set_data(const Dictionary &p_data);
  237. bool _set(const StringName &p_name, const Variant &p_value);
  238. bool _get(const StringName &p_name, Variant &r_ret) const;
  239. void _get_property_list(List<PropertyInfo> *p_list) const;
  240. void _add_point(const Vector3 &p_position, const Vector3 &p_in = Vector3(), const Vector3 &p_out = Vector3(), int p_atpos = -1);
  241. void _remove_point(int p_index);
  242. Vector<RBMap<real_t, Vector3>> _tessellate_even_length(int p_max_stages = 5, real_t p_length = 0.2) const;
  243. protected:
  244. static void _bind_methods();
  245. public:
  246. #ifdef TOOLS_ENABLED
  247. // For Path3DGizmo.
  248. Basis get_point_baked_posture(int p_index, bool p_apply_tilt = false) const;
  249. #endif
  250. int get_point_count() const;
  251. void set_point_count(int p_count);
  252. void add_point(const Vector3 &p_position, const Vector3 &p_in = Vector3(), const Vector3 &p_out = Vector3(), int p_atpos = -1);
  253. void set_point_position(int p_index, const Vector3 &p_position);
  254. Vector3 get_point_position(int p_index) const;
  255. void set_point_tilt(int p_index, real_t p_tilt);
  256. real_t get_point_tilt(int p_index) const;
  257. void set_point_in(int p_index, const Vector3 &p_in);
  258. Vector3 get_point_in(int p_index) const;
  259. void set_point_out(int p_index, const Vector3 &p_out);
  260. Vector3 get_point_out(int p_index) const;
  261. void remove_point(int p_index);
  262. void clear_points();
  263. Vector3 sample(int p_index, real_t p_offset) const;
  264. Vector3 samplef(real_t p_findex) const;
  265. void set_closed(bool p_closed);
  266. bool is_closed() const;
  267. void set_bake_interval(real_t p_tolerance);
  268. real_t get_bake_interval() const;
  269. void set_up_vector_enabled(bool p_enable);
  270. bool is_up_vector_enabled() const;
  271. real_t get_baked_length() const;
  272. Vector3 sample_baked(real_t p_offset, bool p_cubic = false) const;
  273. Transform3D sample_baked_with_rotation(real_t p_offset, bool p_cubic = false, bool p_apply_tilt = false) const;
  274. real_t sample_baked_tilt(real_t p_offset) const;
  275. Vector3 sample_baked_up_vector(real_t p_offset, bool p_apply_tilt = false) const;
  276. PackedVector3Array get_baked_points() const; // Useful for going through.
  277. Vector<real_t> get_baked_tilts() const; //useful for going through
  278. PackedVector3Array get_baked_up_vectors() const;
  279. Vector3 get_closest_point(const Vector3 &p_to_point) const;
  280. real_t get_closest_offset(const Vector3 &p_to_point) const;
  281. PackedVector3Array get_points() const;
  282. PackedVector3Array tessellate(int p_max_stages = 5, real_t p_tolerance = 4) const; // Useful for display.
  283. PackedVector3Array tessellate_even_length(int p_max_stages = 5, real_t p_length = 0.2) const; // Useful for baking.
  284. Curve3D();
  285. };
  286. #endif // CURVE_H