curve.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*************************************************************************/
  2. /* curve.h */
  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. #ifndef CURVE_H
  31. #define CURVE_H
  32. #include "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. protected:
  106. static void _bind_methods();
  107. private:
  108. void mark_dirty();
  109. Vector<Point> _points;
  110. bool _baked_cache_dirty;
  111. Vector<real_t> _baked_cache;
  112. int _bake_resolution;
  113. float _min_value;
  114. float _max_value;
  115. };
  116. VARIANT_ENUM_CAST(Curve::TangentMode)
  117. class Curve2D : public Resource {
  118. GDCLASS(Curve2D, Resource);
  119. struct Point {
  120. Vector2 in;
  121. Vector2 out;
  122. Vector2 pos;
  123. };
  124. Vector<Point> points;
  125. struct BakedPoint {
  126. float ofs;
  127. Vector2 point;
  128. };
  129. mutable bool baked_cache_dirty;
  130. mutable PoolVector2Array baked_point_cache;
  131. mutable float baked_max_ofs;
  132. void _bake() const;
  133. float bake_interval;
  134. 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;
  135. Dictionary _get_data() const;
  136. void _set_data(const Dictionary &p_data);
  137. protected:
  138. static void _bind_methods();
  139. public:
  140. int get_point_count() const;
  141. void add_point(const Vector2 &p_pos, const Vector2 &p_in = Vector2(), const Vector2 &p_out = Vector2(), int p_atpos = -1);
  142. void set_point_position(int p_index, const Vector2 &p_pos);
  143. Vector2 get_point_position(int p_index) const;
  144. void set_point_in(int p_index, const Vector2 &p_in);
  145. Vector2 get_point_in(int p_index) const;
  146. void set_point_out(int p_index, const Vector2 &p_out);
  147. Vector2 get_point_out(int p_index) const;
  148. void remove_point(int p_index);
  149. void clear_points();
  150. Vector2 interpolate(int p_index, float p_offset) const;
  151. Vector2 interpolatef(real_t p_findex) const;
  152. void set_bake_interval(float p_tolerance);
  153. float get_bake_interval() const;
  154. float get_baked_length() const;
  155. Vector2 interpolate_baked(float p_offset, bool p_cubic = false) const;
  156. PoolVector2Array get_baked_points() const; //useful for going through
  157. PoolVector2Array tessellate(int p_max_stages = 5, float p_tolerance = 4) const; //useful for display
  158. Curve2D();
  159. };
  160. class Curve3D : public Resource {
  161. GDCLASS(Curve3D, Resource);
  162. struct Point {
  163. Vector3 in;
  164. Vector3 out;
  165. Vector3 pos;
  166. float tilt;
  167. Point() { tilt = 0; }
  168. };
  169. Vector<Point> points;
  170. struct BakedPoint {
  171. float ofs;
  172. Vector3 point;
  173. };
  174. mutable bool baked_cache_dirty;
  175. mutable PoolVector3Array baked_point_cache;
  176. mutable PoolRealArray baked_tilt_cache;
  177. mutable float baked_max_ofs;
  178. void _bake() const;
  179. float bake_interval;
  180. 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;
  181. Dictionary _get_data() const;
  182. void _set_data(const Dictionary &p_data);
  183. protected:
  184. static void _bind_methods();
  185. public:
  186. int get_point_count() const;
  187. void add_point(const Vector3 &p_pos, const Vector3 &p_in = Vector3(), const Vector3 &p_out = Vector3(), int p_atpos = -1);
  188. void set_point_position(int p_index, const Vector3 &p_pos);
  189. Vector3 get_point_position(int p_index) const;
  190. void set_point_tilt(int p_index, float p_tilt);
  191. float get_point_tilt(int p_index) const;
  192. void set_point_in(int p_index, const Vector3 &p_in);
  193. Vector3 get_point_in(int p_index) const;
  194. void set_point_out(int p_index, const Vector3 &p_out);
  195. Vector3 get_point_out(int p_index) const;
  196. void remove_point(int p_index);
  197. void clear_points();
  198. Vector3 interpolate(int p_index, float p_offset) const;
  199. Vector3 interpolatef(real_t p_findex) const;
  200. void set_bake_interval(float p_tolerance);
  201. float get_bake_interval() const;
  202. float get_baked_length() const;
  203. Vector3 interpolate_baked(float p_offset, bool p_cubic = false) const;
  204. float interpolate_baked_tilt(float p_offset) const;
  205. PoolVector3Array get_baked_points() const; //useful for going through
  206. PoolRealArray get_baked_tilts() const; //useful for going through
  207. PoolVector3Array tessellate(int p_max_stages = 5, float p_tolerance = 4) const; //useful for display
  208. Curve3D();
  209. };
  210. #endif // CURVE_H