curve.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. /*************************************************************************/
  2. /* curve.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "curve.h"
  31. #include "core/core_string_names.h"
  32. template <class T>
  33. static _FORCE_INLINE_ T _bezier_interp(real_t p_t, T p_start, T p_control_1, T p_control_2, T p_end) {
  34. /* Formula from Wikipedia article on Bezier curves. */
  35. real_t omt = (1.0 - p_t);
  36. real_t omt2 = omt * omt;
  37. real_t omt3 = omt2 * omt;
  38. real_t t2 = p_t * p_t;
  39. real_t t3 = t2 * p_t;
  40. return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
  41. }
  42. const char *Curve::SIGNAL_RANGE_CHANGED = "range_changed";
  43. Curve::Curve() {
  44. }
  45. int Curve::add_point(Vector2 p_position, real_t p_left_tangent, real_t p_right_tangent, TangentMode p_left_mode, TangentMode p_right_mode) {
  46. // Add a point and preserve order
  47. // Curve bounds is in 0..1
  48. if (p_position.x > MAX_X) {
  49. p_position.x = MAX_X;
  50. } else if (p_position.x < MIN_X) {
  51. p_position.x = MIN_X;
  52. }
  53. int ret = -1;
  54. if (_points.size() == 0) {
  55. _points.push_back(Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
  56. ret = 0;
  57. } else if (_points.size() == 1) {
  58. // TODO Is the `else` able to handle this block already?
  59. real_t diff = p_position.x - _points[0].position.x;
  60. if (diff > 0) {
  61. _points.push_back(Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
  62. ret = 1;
  63. } else {
  64. _points.insert(0, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
  65. ret = 0;
  66. }
  67. } else {
  68. int i = get_index(p_position.x);
  69. if (i == 0 && p_position.x < _points[0].position.x) {
  70. // Insert before anything else
  71. _points.insert(0, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
  72. ret = 0;
  73. } else {
  74. // Insert between i and i+1
  75. ++i;
  76. _points.insert(i, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
  77. ret = i;
  78. }
  79. }
  80. update_auto_tangents(ret);
  81. mark_dirty();
  82. return ret;
  83. }
  84. int Curve::get_index(real_t p_offset) const {
  85. // Lower-bound float binary search
  86. int imin = 0;
  87. int imax = _points.size() - 1;
  88. while (imax - imin > 1) {
  89. int m = (imin + imax) / 2;
  90. real_t a = _points[m].position.x;
  91. real_t b = _points[m + 1].position.x;
  92. if (a < p_offset && b < p_offset) {
  93. imin = m;
  94. } else if (a > p_offset) {
  95. imax = m;
  96. } else {
  97. return m;
  98. }
  99. }
  100. // Will happen if the offset is out of bounds
  101. if (p_offset > _points[imax].position.x) {
  102. return imax;
  103. }
  104. return imin;
  105. }
  106. void Curve::clean_dupes() {
  107. bool dirty = false;
  108. for (int i = 1; i < _points.size(); ++i) {
  109. real_t diff = _points[i - 1].position.x - _points[i].position.x;
  110. if (diff <= CMP_EPSILON) {
  111. _points.remove_at(i);
  112. --i;
  113. dirty = true;
  114. }
  115. }
  116. if (dirty) {
  117. mark_dirty();
  118. }
  119. }
  120. void Curve::set_point_left_tangent(int p_index, real_t p_tangent) {
  121. ERR_FAIL_INDEX(p_index, _points.size());
  122. _points.write[p_index].left_tangent = p_tangent;
  123. _points.write[p_index].left_mode = TANGENT_FREE;
  124. mark_dirty();
  125. }
  126. void Curve::set_point_right_tangent(int p_index, real_t p_tangent) {
  127. ERR_FAIL_INDEX(p_index, _points.size());
  128. _points.write[p_index].right_tangent = p_tangent;
  129. _points.write[p_index].right_mode = TANGENT_FREE;
  130. mark_dirty();
  131. }
  132. void Curve::set_point_left_mode(int p_index, TangentMode p_mode) {
  133. ERR_FAIL_INDEX(p_index, _points.size());
  134. _points.write[p_index].left_mode = p_mode;
  135. if (p_index > 0) {
  136. if (p_mode == TANGENT_LINEAR) {
  137. Vector2 v = (_points[p_index - 1].position - _points[p_index].position).normalized();
  138. _points.write[p_index].left_tangent = v.y / v.x;
  139. }
  140. }
  141. mark_dirty();
  142. }
  143. void Curve::set_point_right_mode(int p_index, TangentMode p_mode) {
  144. ERR_FAIL_INDEX(p_index, _points.size());
  145. _points.write[p_index].right_mode = p_mode;
  146. if (p_index + 1 < _points.size()) {
  147. if (p_mode == TANGENT_LINEAR) {
  148. Vector2 v = (_points[p_index + 1].position - _points[p_index].position).normalized();
  149. _points.write[p_index].right_tangent = v.y / v.x;
  150. }
  151. }
  152. mark_dirty();
  153. }
  154. real_t Curve::get_point_left_tangent(int p_index) const {
  155. ERR_FAIL_INDEX_V(p_index, _points.size(), 0);
  156. return _points[p_index].left_tangent;
  157. }
  158. real_t Curve::get_point_right_tangent(int p_index) const {
  159. ERR_FAIL_INDEX_V(p_index, _points.size(), 0);
  160. return _points[p_index].right_tangent;
  161. }
  162. Curve::TangentMode Curve::get_point_left_mode(int p_index) const {
  163. ERR_FAIL_INDEX_V(p_index, _points.size(), TANGENT_FREE);
  164. return _points[p_index].left_mode;
  165. }
  166. Curve::TangentMode Curve::get_point_right_mode(int p_index) const {
  167. ERR_FAIL_INDEX_V(p_index, _points.size(), TANGENT_FREE);
  168. return _points[p_index].right_mode;
  169. }
  170. void Curve::remove_point(int p_index) {
  171. ERR_FAIL_INDEX(p_index, _points.size());
  172. _points.remove_at(p_index);
  173. mark_dirty();
  174. }
  175. void Curve::clear_points() {
  176. _points.clear();
  177. mark_dirty();
  178. }
  179. void Curve::set_point_value(int p_index, real_t p_position) {
  180. ERR_FAIL_INDEX(p_index, _points.size());
  181. _points.write[p_index].position.y = p_position;
  182. update_auto_tangents(p_index);
  183. mark_dirty();
  184. }
  185. int Curve::set_point_offset(int p_index, real_t p_offset) {
  186. ERR_FAIL_INDEX_V(p_index, _points.size(), -1);
  187. Point p = _points[p_index];
  188. remove_point(p_index);
  189. int i = add_point(Vector2(p_offset, p.position.y));
  190. _points.write[i].left_tangent = p.left_tangent;
  191. _points.write[i].right_tangent = p.right_tangent;
  192. _points.write[i].left_mode = p.left_mode;
  193. _points.write[i].right_mode = p.right_mode;
  194. if (p_index != i) {
  195. update_auto_tangents(p_index);
  196. }
  197. update_auto_tangents(i);
  198. return i;
  199. }
  200. Vector2 Curve::get_point_position(int p_index) const {
  201. ERR_FAIL_INDEX_V(p_index, _points.size(), Vector2(0, 0));
  202. return _points[p_index].position;
  203. }
  204. Curve::Point Curve::get_point(int p_index) const {
  205. ERR_FAIL_INDEX_V(p_index, _points.size(), Point());
  206. return _points[p_index];
  207. }
  208. void Curve::update_auto_tangents(int p_index) {
  209. Point &p = _points.write[p_index];
  210. if (p_index > 0) {
  211. if (p.left_mode == TANGENT_LINEAR) {
  212. Vector2 v = (_points[p_index - 1].position - p.position).normalized();
  213. p.left_tangent = v.y / v.x;
  214. }
  215. if (_points[p_index - 1].right_mode == TANGENT_LINEAR) {
  216. Vector2 v = (_points[p_index - 1].position - p.position).normalized();
  217. _points.write[p_index - 1].right_tangent = v.y / v.x;
  218. }
  219. }
  220. if (p_index + 1 < _points.size()) {
  221. if (p.right_mode == TANGENT_LINEAR) {
  222. Vector2 v = (_points[p_index + 1].position - p.position).normalized();
  223. p.right_tangent = v.y / v.x;
  224. }
  225. if (_points[p_index + 1].left_mode == TANGENT_LINEAR) {
  226. Vector2 v = (_points[p_index + 1].position - p.position).normalized();
  227. _points.write[p_index + 1].left_tangent = v.y / v.x;
  228. }
  229. }
  230. }
  231. #define MIN_Y_RANGE 0.01
  232. void Curve::set_min_value(real_t p_min) {
  233. if (_minmax_set_once & 0b11 && p_min > _max_value - MIN_Y_RANGE) {
  234. _min_value = _max_value - MIN_Y_RANGE;
  235. } else {
  236. _minmax_set_once |= 0b10; // first bit is "min set"
  237. _min_value = p_min;
  238. }
  239. // Note: min and max are indicative values,
  240. // it's still possible that existing points are out of range at this point.
  241. emit_signal(SNAME(SIGNAL_RANGE_CHANGED));
  242. }
  243. void Curve::set_max_value(real_t p_max) {
  244. if (_minmax_set_once & 0b11 && p_max < _min_value + MIN_Y_RANGE) {
  245. _max_value = _min_value + MIN_Y_RANGE;
  246. } else {
  247. _minmax_set_once |= 0b01; // second bit is "max set"
  248. _max_value = p_max;
  249. }
  250. emit_signal(SNAME(SIGNAL_RANGE_CHANGED));
  251. }
  252. real_t Curve::interpolate(real_t p_offset) const {
  253. if (_points.size() == 0) {
  254. return 0;
  255. }
  256. if (_points.size() == 1) {
  257. return _points[0].position.y;
  258. }
  259. int i = get_index(p_offset);
  260. if (i == _points.size() - 1) {
  261. return _points[i].position.y;
  262. }
  263. real_t local = p_offset - _points[i].position.x;
  264. if (i == 0 && local <= 0) {
  265. return _points[0].position.y;
  266. }
  267. return interpolate_local_nocheck(i, local);
  268. }
  269. real_t Curve::interpolate_local_nocheck(int p_index, real_t p_local_offset) const {
  270. const Point a = _points[p_index];
  271. const Point b = _points[p_index + 1];
  272. /* Cubic bezier
  273. *
  274. * ac-----bc
  275. * / \
  276. * / \ Here with a.right_tangent > 0
  277. * / \ and b.left_tangent < 0
  278. * / \
  279. * a b
  280. *
  281. * |-d1--|-d2--|-d3--|
  282. *
  283. * d1 == d2 == d3 == d / 3
  284. */
  285. // Control points are chosen at equal distances
  286. real_t d = b.position.x - a.position.x;
  287. if (Math::is_zero_approx(d)) {
  288. return b.position.y;
  289. }
  290. p_local_offset /= d;
  291. d /= 3.0;
  292. real_t yac = a.position.y + d * a.right_tangent;
  293. real_t ybc = b.position.y - d * b.left_tangent;
  294. real_t y = _bezier_interp(p_local_offset, a.position.y, yac, ybc, b.position.y);
  295. return y;
  296. }
  297. void Curve::mark_dirty() {
  298. _baked_cache_dirty = true;
  299. emit_signal(CoreStringNames::get_singleton()->changed);
  300. }
  301. Array Curve::get_data() const {
  302. Array output;
  303. const unsigned int ELEMS = 5;
  304. output.resize(_points.size() * ELEMS);
  305. for (int j = 0; j < _points.size(); ++j) {
  306. const Point p = _points[j];
  307. int i = j * ELEMS;
  308. output[i] = p.position;
  309. output[i + 1] = p.left_tangent;
  310. output[i + 2] = p.right_tangent;
  311. output[i + 3] = p.left_mode;
  312. output[i + 4] = p.right_mode;
  313. }
  314. return output;
  315. }
  316. void Curve::set_data(const Array p_input) {
  317. const unsigned int ELEMS = 5;
  318. ERR_FAIL_COND(p_input.size() % ELEMS != 0);
  319. _points.clear();
  320. // Validate input
  321. for (int i = 0; i < p_input.size(); i += ELEMS) {
  322. ERR_FAIL_COND(p_input[i].get_type() != Variant::VECTOR2);
  323. ERR_FAIL_COND(!p_input[i + 1].is_num());
  324. ERR_FAIL_COND(p_input[i + 2].get_type() != Variant::FLOAT);
  325. ERR_FAIL_COND(p_input[i + 3].get_type() != Variant::INT);
  326. int left_mode = p_input[i + 3];
  327. ERR_FAIL_COND(left_mode < 0 || left_mode >= TANGENT_MODE_COUNT);
  328. ERR_FAIL_COND(p_input[i + 4].get_type() != Variant::INT);
  329. int right_mode = p_input[i + 4];
  330. ERR_FAIL_COND(right_mode < 0 || right_mode >= TANGENT_MODE_COUNT);
  331. }
  332. _points.resize(p_input.size() / ELEMS);
  333. for (int j = 0; j < _points.size(); ++j) {
  334. Point &p = _points.write[j];
  335. int i = j * ELEMS;
  336. p.position = p_input[i];
  337. p.left_tangent = p_input[i + 1];
  338. p.right_tangent = p_input[i + 2];
  339. // TODO For some reason the compiler won't convert from Variant to enum
  340. int left_mode = p_input[i + 3];
  341. int right_mode = p_input[i + 4];
  342. p.left_mode = (TangentMode)left_mode;
  343. p.right_mode = (TangentMode)right_mode;
  344. }
  345. mark_dirty();
  346. }
  347. void Curve::bake() {
  348. _baked_cache.clear();
  349. _baked_cache.resize(_bake_resolution);
  350. for (int i = 1; i < _bake_resolution - 1; ++i) {
  351. real_t x = i / static_cast<real_t>(_bake_resolution);
  352. real_t y = interpolate(x);
  353. _baked_cache.write[i] = y;
  354. }
  355. if (_points.size() != 0) {
  356. _baked_cache.write[0] = _points[0].position.y;
  357. _baked_cache.write[_baked_cache.size() - 1] = _points[_points.size() - 1].position.y;
  358. }
  359. _baked_cache_dirty = false;
  360. }
  361. void Curve::set_bake_resolution(int p_resolution) {
  362. ERR_FAIL_COND(p_resolution < 1);
  363. ERR_FAIL_COND(p_resolution > 1000);
  364. _bake_resolution = p_resolution;
  365. _baked_cache_dirty = true;
  366. }
  367. real_t Curve::interpolate_baked(real_t p_offset) const {
  368. if (_baked_cache_dirty) {
  369. // Last-second bake if not done already
  370. const_cast<Curve *>(this)->bake();
  371. }
  372. // Special cases if the cache is too small
  373. if (_baked_cache.size() == 0) {
  374. if (_points.size() == 0) {
  375. return 0;
  376. }
  377. return _points[0].position.y;
  378. } else if (_baked_cache.size() == 1) {
  379. return _baked_cache[0];
  380. }
  381. // Get interpolation index
  382. real_t fi = p_offset * _baked_cache.size();
  383. int i = Math::floor(fi);
  384. if (i < 0) {
  385. i = 0;
  386. fi = 0;
  387. } else if (i >= _baked_cache.size()) {
  388. i = _baked_cache.size() - 1;
  389. fi = 0;
  390. }
  391. // Interpolate
  392. if (i + 1 < _baked_cache.size()) {
  393. real_t t = fi - i;
  394. return Math::lerp(_baked_cache[i], _baked_cache[i + 1], t);
  395. } else {
  396. return _baked_cache[_baked_cache.size() - 1];
  397. }
  398. }
  399. void Curve::ensure_default_setup(real_t p_min, real_t p_max) {
  400. if (_points.size() == 0 && _min_value == 0 && _max_value == 1) {
  401. add_point(Vector2(0, 1));
  402. add_point(Vector2(1, 1));
  403. set_min_value(p_min);
  404. set_max_value(p_max);
  405. }
  406. }
  407. void Curve::_bind_methods() {
  408. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve::get_point_count);
  409. ClassDB::bind_method(D_METHOD("add_point", "position", "left_tangent", "right_tangent", "left_mode", "right_mode"), &Curve::add_point, DEFVAL(0), DEFVAL(0), DEFVAL(TANGENT_FREE), DEFVAL(TANGENT_FREE));
  410. ClassDB::bind_method(D_METHOD("remove_point", "index"), &Curve::remove_point);
  411. ClassDB::bind_method(D_METHOD("clear_points"), &Curve::clear_points);
  412. ClassDB::bind_method(D_METHOD("get_point_position", "index"), &Curve::get_point_position);
  413. ClassDB::bind_method(D_METHOD("set_point_value", "index", "y"), &Curve::set_point_value);
  414. ClassDB::bind_method(D_METHOD("set_point_offset", "index", "offset"), &Curve::set_point_offset);
  415. ClassDB::bind_method(D_METHOD("interpolate", "offset"), &Curve::interpolate);
  416. ClassDB::bind_method(D_METHOD("interpolate_baked", "offset"), &Curve::interpolate_baked);
  417. ClassDB::bind_method(D_METHOD("get_point_left_tangent", "index"), &Curve::get_point_left_tangent);
  418. ClassDB::bind_method(D_METHOD("get_point_right_tangent", "index"), &Curve::get_point_right_tangent);
  419. ClassDB::bind_method(D_METHOD("get_point_left_mode", "index"), &Curve::get_point_left_mode);
  420. ClassDB::bind_method(D_METHOD("get_point_right_mode", "index"), &Curve::get_point_right_mode);
  421. ClassDB::bind_method(D_METHOD("set_point_left_tangent", "index", "tangent"), &Curve::set_point_left_tangent);
  422. ClassDB::bind_method(D_METHOD("set_point_right_tangent", "index", "tangent"), &Curve::set_point_right_tangent);
  423. ClassDB::bind_method(D_METHOD("set_point_left_mode", "index", "mode"), &Curve::set_point_left_mode);
  424. ClassDB::bind_method(D_METHOD("set_point_right_mode", "index", "mode"), &Curve::set_point_right_mode);
  425. ClassDB::bind_method(D_METHOD("get_min_value"), &Curve::get_min_value);
  426. ClassDB::bind_method(D_METHOD("set_min_value", "min"), &Curve::set_min_value);
  427. ClassDB::bind_method(D_METHOD("get_max_value"), &Curve::get_max_value);
  428. ClassDB::bind_method(D_METHOD("set_max_value", "max"), &Curve::set_max_value);
  429. ClassDB::bind_method(D_METHOD("clean_dupes"), &Curve::clean_dupes);
  430. ClassDB::bind_method(D_METHOD("bake"), &Curve::bake);
  431. ClassDB::bind_method(D_METHOD("get_bake_resolution"), &Curve::get_bake_resolution);
  432. ClassDB::bind_method(D_METHOD("set_bake_resolution", "resolution"), &Curve::set_bake_resolution);
  433. ClassDB::bind_method(D_METHOD("_get_data"), &Curve::get_data);
  434. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve::set_data);
  435. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_min_value", "get_min_value");
  436. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_max_value", "get_max_value");
  437. ADD_PROPERTY(PropertyInfo(Variant::INT, "bake_resolution", PROPERTY_HINT_RANGE, "1,1000,1"), "set_bake_resolution", "get_bake_resolution");
  438. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  439. ADD_SIGNAL(MethodInfo(SIGNAL_RANGE_CHANGED));
  440. BIND_ENUM_CONSTANT(TANGENT_FREE);
  441. BIND_ENUM_CONSTANT(TANGENT_LINEAR);
  442. BIND_ENUM_CONSTANT(TANGENT_MODE_COUNT);
  443. }
  444. int Curve2D::get_point_count() const {
  445. return points.size();
  446. }
  447. void Curve2D::add_point(const Vector2 &p_position, const Vector2 &p_in, const Vector2 &p_out, int p_atpos) {
  448. Point n;
  449. n.position = p_position;
  450. n.in = p_in;
  451. n.out = p_out;
  452. if (p_atpos >= 0 && p_atpos < points.size()) {
  453. points.insert(p_atpos, n);
  454. } else {
  455. points.push_back(n);
  456. }
  457. baked_cache_dirty = true;
  458. emit_signal(CoreStringNames::get_singleton()->changed);
  459. }
  460. void Curve2D::set_point_position(int p_index, const Vector2 &p_position) {
  461. ERR_FAIL_INDEX(p_index, points.size());
  462. points.write[p_index].position = p_position;
  463. baked_cache_dirty = true;
  464. emit_signal(CoreStringNames::get_singleton()->changed);
  465. }
  466. Vector2 Curve2D::get_point_position(int p_index) const {
  467. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  468. return points[p_index].position;
  469. }
  470. void Curve2D::set_point_in(int p_index, const Vector2 &p_in) {
  471. ERR_FAIL_INDEX(p_index, points.size());
  472. points.write[p_index].in = p_in;
  473. baked_cache_dirty = true;
  474. emit_signal(CoreStringNames::get_singleton()->changed);
  475. }
  476. Vector2 Curve2D::get_point_in(int p_index) const {
  477. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  478. return points[p_index].in;
  479. }
  480. void Curve2D::set_point_out(int p_index, const Vector2 &p_out) {
  481. ERR_FAIL_INDEX(p_index, points.size());
  482. points.write[p_index].out = p_out;
  483. baked_cache_dirty = true;
  484. emit_signal(CoreStringNames::get_singleton()->changed);
  485. }
  486. Vector2 Curve2D::get_point_out(int p_index) const {
  487. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  488. return points[p_index].out;
  489. }
  490. void Curve2D::remove_point(int p_index) {
  491. ERR_FAIL_INDEX(p_index, points.size());
  492. points.remove_at(p_index);
  493. baked_cache_dirty = true;
  494. emit_signal(CoreStringNames::get_singleton()->changed);
  495. }
  496. void Curve2D::clear_points() {
  497. if (!points.is_empty()) {
  498. points.clear();
  499. baked_cache_dirty = true;
  500. emit_signal(CoreStringNames::get_singleton()->changed);
  501. }
  502. }
  503. Vector2 Curve2D::interpolate(int p_index, const real_t p_offset) const {
  504. int pc = points.size();
  505. ERR_FAIL_COND_V(pc == 0, Vector2());
  506. if (p_index >= pc - 1) {
  507. return points[pc - 1].position;
  508. } else if (p_index < 0) {
  509. return points[0].position;
  510. }
  511. Vector2 p0 = points[p_index].position;
  512. Vector2 p1 = p0 + points[p_index].out;
  513. Vector2 p3 = points[p_index + 1].position;
  514. Vector2 p2 = p3 + points[p_index + 1].in;
  515. return _bezier_interp(p_offset, p0, p1, p2, p3);
  516. }
  517. Vector2 Curve2D::interpolatef(real_t p_findex) const {
  518. if (p_findex < 0) {
  519. p_findex = 0;
  520. } else if (p_findex >= points.size()) {
  521. p_findex = points.size();
  522. }
  523. return interpolate((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
  524. }
  525. void Curve2D::_bake_segment2d(Map<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 {
  526. real_t mp = p_begin + (p_end - p_begin) * 0.5;
  527. Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  528. Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  529. Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  530. Vector2 na = (mid - beg).normalized();
  531. Vector2 nb = (end - mid).normalized();
  532. real_t dp = na.dot(nb);
  533. if (dp < Math::cos(Math::deg2rad(p_tol))) {
  534. r_bake[mp] = mid;
  535. }
  536. if (p_depth < p_max_depth) {
  537. _bake_segment2d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  538. _bake_segment2d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  539. }
  540. }
  541. void Curve2D::_bake() const {
  542. if (!baked_cache_dirty) {
  543. return;
  544. }
  545. baked_max_ofs = 0;
  546. baked_cache_dirty = false;
  547. if (points.size() == 0) {
  548. baked_point_cache.clear();
  549. baked_dist_cache.clear();
  550. return;
  551. }
  552. if (points.size() == 1) {
  553. baked_point_cache.resize(1);
  554. baked_point_cache.set(0, points[0].position);
  555. baked_dist_cache.resize(1);
  556. baked_dist_cache.set(0, 0.0);
  557. return;
  558. }
  559. Vector2 position = points[0].position;
  560. real_t dist = 0.0;
  561. List<Vector2> pointlist;
  562. List<real_t> distlist;
  563. pointlist.push_back(position); //start always from origin
  564. distlist.push_back(0.0);
  565. for (int i = 0; i < points.size() - 1; i++) {
  566. real_t step = 0.1; // at least 10 substeps ought to be enough?
  567. real_t p = 0.0;
  568. while (p < 1.0) {
  569. real_t np = p + step;
  570. if (np > 1.0) {
  571. np = 1.0;
  572. }
  573. Vector2 npp = _bezier_interp(np, points[i].position, points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position);
  574. real_t d = position.distance_to(npp);
  575. if (d > bake_interval) {
  576. // OK! between P and NP there _has_ to be Something, let's go searching!
  577. int iterations = 10; //lots of detail!
  578. real_t low = p;
  579. real_t hi = np;
  580. real_t mid = low + (hi - low) * 0.5;
  581. for (int j = 0; j < iterations; j++) {
  582. npp = _bezier_interp(mid, points[i].position, points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position);
  583. d = position.distance_to(npp);
  584. if (bake_interval < d) {
  585. hi = mid;
  586. } else {
  587. low = mid;
  588. }
  589. mid = low + (hi - low) * 0.5;
  590. }
  591. position = npp;
  592. p = mid;
  593. dist += d;
  594. pointlist.push_back(position);
  595. distlist.push_back(dist);
  596. } else {
  597. p = np;
  598. }
  599. }
  600. }
  601. Vector2 lastpos = points[points.size() - 1].position;
  602. real_t rem = position.distance_to(lastpos);
  603. dist += rem;
  604. baked_max_ofs = dist;
  605. pointlist.push_back(lastpos);
  606. distlist.push_back(dist);
  607. baked_point_cache.resize(pointlist.size());
  608. baked_dist_cache.resize(distlist.size());
  609. Vector2 *w = baked_point_cache.ptrw();
  610. real_t *wd = baked_dist_cache.ptrw();
  611. for (int i = 0; i < pointlist.size(); i++) {
  612. w[i] = pointlist[i];
  613. wd[i] = distlist[i];
  614. }
  615. }
  616. real_t Curve2D::get_baked_length() const {
  617. if (baked_cache_dirty) {
  618. _bake();
  619. }
  620. return baked_max_ofs;
  621. }
  622. Vector2 Curve2D::interpolate_baked(real_t p_offset, bool p_cubic) const {
  623. if (baked_cache_dirty) {
  624. _bake();
  625. }
  626. //validate//
  627. int pc = baked_point_cache.size();
  628. ERR_FAIL_COND_V_MSG(pc == 0, Vector2(), "No points in Curve2D.");
  629. if (pc == 1) {
  630. return baked_point_cache.get(0);
  631. }
  632. const Vector2 *r = baked_point_cache.ptr();
  633. if (p_offset < 0) {
  634. return r[0];
  635. }
  636. if (p_offset >= baked_max_ofs) {
  637. return r[pc - 1];
  638. }
  639. int start = 0, end = pc, idx = (end + start) / 2;
  640. // binary search to find baked points
  641. while (start < idx) {
  642. real_t offset = baked_dist_cache[idx];
  643. if (p_offset <= offset) {
  644. end = idx;
  645. } else {
  646. start = idx;
  647. }
  648. idx = (end + start) / 2;
  649. }
  650. real_t offset_begin = baked_dist_cache[idx];
  651. real_t offset_end = baked_dist_cache[idx + 1];
  652. real_t idx_interval = offset_end - offset_begin;
  653. ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, Vector2(), "failed to find baked segment");
  654. real_t frac = (p_offset - offset_begin) / idx_interval;
  655. if (p_cubic) {
  656. Vector2 pre = idx > 0 ? r[idx - 1] : r[idx];
  657. Vector2 post = (idx < (pc - 2)) ? r[idx + 2] : r[idx + 1];
  658. return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
  659. } else {
  660. return r[idx].lerp(r[idx + 1], frac);
  661. }
  662. }
  663. PackedVector2Array Curve2D::get_baked_points() const {
  664. if (baked_cache_dirty) {
  665. _bake();
  666. }
  667. return baked_point_cache;
  668. }
  669. void Curve2D::set_bake_interval(real_t p_tolerance) {
  670. bake_interval = p_tolerance;
  671. baked_cache_dirty = true;
  672. emit_signal(CoreStringNames::get_singleton()->changed);
  673. }
  674. real_t Curve2D::get_bake_interval() const {
  675. return bake_interval;
  676. }
  677. Vector2 Curve2D::get_closest_point(const Vector2 &p_to_point) const {
  678. // Brute force method
  679. if (baked_cache_dirty) {
  680. _bake();
  681. }
  682. //validate//
  683. int pc = baked_point_cache.size();
  684. ERR_FAIL_COND_V_MSG(pc == 0, Vector2(), "No points in Curve2D.");
  685. if (pc == 1) {
  686. return baked_point_cache.get(0);
  687. }
  688. const Vector2 *r = baked_point_cache.ptr();
  689. Vector2 nearest;
  690. real_t nearest_dist = -1.0f;
  691. for (int i = 0; i < pc - 1; i++) {
  692. Vector2 origin = r[i];
  693. Vector2 direction = (r[i + 1] - origin) / bake_interval;
  694. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
  695. Vector2 proj = origin + direction * d;
  696. real_t dist = proj.distance_squared_to(p_to_point);
  697. if (nearest_dist < 0.0f || dist < nearest_dist) {
  698. nearest = proj;
  699. nearest_dist = dist;
  700. }
  701. }
  702. return nearest;
  703. }
  704. real_t Curve2D::get_closest_offset(const Vector2 &p_to_point) const {
  705. // Brute force method
  706. if (baked_cache_dirty) {
  707. _bake();
  708. }
  709. //validate//
  710. int pc = baked_point_cache.size();
  711. ERR_FAIL_COND_V_MSG(pc == 0, 0.0f, "No points in Curve2D.");
  712. if (pc == 1) {
  713. return 0.0f;
  714. }
  715. const Vector2 *r = baked_point_cache.ptr();
  716. real_t nearest = 0.0f;
  717. real_t nearest_dist = -1.0f;
  718. real_t offset = 0.0f;
  719. for (int i = 0; i < pc - 1; i++) {
  720. Vector2 origin = r[i];
  721. Vector2 direction = (r[i + 1] - origin) / bake_interval;
  722. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
  723. Vector2 proj = origin + direction * d;
  724. real_t dist = proj.distance_squared_to(p_to_point);
  725. if (nearest_dist < 0.0f || dist < nearest_dist) {
  726. nearest = offset + d;
  727. nearest_dist = dist;
  728. }
  729. offset += bake_interval;
  730. }
  731. return nearest;
  732. }
  733. Dictionary Curve2D::_get_data() const {
  734. Dictionary dc;
  735. PackedVector2Array d;
  736. d.resize(points.size() * 3);
  737. Vector2 *w = d.ptrw();
  738. for (int i = 0; i < points.size(); i++) {
  739. w[i * 3 + 0] = points[i].in;
  740. w[i * 3 + 1] = points[i].out;
  741. w[i * 3 + 2] = points[i].position;
  742. }
  743. dc["points"] = d;
  744. return dc;
  745. }
  746. void Curve2D::_set_data(const Dictionary &p_data) {
  747. ERR_FAIL_COND(!p_data.has("points"));
  748. PackedVector2Array rp = p_data["points"];
  749. int pc = rp.size();
  750. ERR_FAIL_COND(pc % 3 != 0);
  751. points.resize(pc / 3);
  752. const Vector2 *r = rp.ptr();
  753. for (int i = 0; i < points.size(); i++) {
  754. points.write[i].in = r[i * 3 + 0];
  755. points.write[i].out = r[i * 3 + 1];
  756. points.write[i].position = r[i * 3 + 2];
  757. }
  758. baked_cache_dirty = true;
  759. }
  760. PackedVector2Array Curve2D::tessellate(int p_max_stages, real_t p_tolerance) const {
  761. PackedVector2Array tess;
  762. if (points.size() == 0) {
  763. return tess;
  764. }
  765. Vector<Map<real_t, Vector2>> midpoints;
  766. midpoints.resize(points.size() - 1);
  767. int pc = 1;
  768. for (int i = 0; i < points.size() - 1; i++) {
  769. _bake_segment2d(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[i + 1].position, points[i + 1].in, 0, p_max_stages, p_tolerance);
  770. pc++;
  771. pc += midpoints[i].size();
  772. }
  773. tess.resize(pc);
  774. Vector2 *bpw = tess.ptrw();
  775. bpw[0] = points[0].position;
  776. int pidx = 0;
  777. for (int i = 0; i < points.size() - 1; i++) {
  778. for (const KeyValue<real_t, Vector2> &E : midpoints[i]) {
  779. pidx++;
  780. bpw[pidx] = E.value;
  781. }
  782. pidx++;
  783. bpw[pidx] = points[i + 1].position;
  784. }
  785. return tess;
  786. }
  787. void Curve2D::_bind_methods() {
  788. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve2D::get_point_count);
  789. ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "at_position"), &Curve2D::add_point, DEFVAL(Vector2()), DEFVAL(Vector2()), DEFVAL(-1));
  790. ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve2D::set_point_position);
  791. ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve2D::get_point_position);
  792. ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve2D::set_point_in);
  793. ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve2D::get_point_in);
  794. ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve2D::set_point_out);
  795. ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve2D::get_point_out);
  796. ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve2D::remove_point);
  797. ClassDB::bind_method(D_METHOD("clear_points"), &Curve2D::clear_points);
  798. ClassDB::bind_method(D_METHOD("interpolate", "idx", "t"), &Curve2D::interpolate);
  799. ClassDB::bind_method(D_METHOD("interpolatef", "fofs"), &Curve2D::interpolatef);
  800. //ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve2D::bake,DEFVAL(10));
  801. ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve2D::set_bake_interval);
  802. ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve2D::get_bake_interval);
  803. ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve2D::get_baked_length);
  804. ClassDB::bind_method(D_METHOD("interpolate_baked", "offset", "cubic"), &Curve2D::interpolate_baked, DEFVAL(false));
  805. ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve2D::get_baked_points);
  806. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Curve2D::get_closest_point);
  807. ClassDB::bind_method(D_METHOD("get_closest_offset", "to_point"), &Curve2D::get_closest_offset);
  808. ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve2D::tessellate, DEFVAL(5), DEFVAL(4));
  809. ClassDB::bind_method(D_METHOD("_get_data"), &Curve2D::_get_data);
  810. ClassDB::bind_method(D_METHOD("_set_data"), &Curve2D::_set_data);
  811. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
  812. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  813. }
  814. Curve2D::Curve2D() {
  815. /* add_point(Vector2(-1,0,0));
  816. add_point(Vector2(0,2,0));
  817. add_point(Vector2(0,3,5));*/
  818. }
  819. /***********************************************************************************/
  820. /***********************************************************************************/
  821. /***********************************************************************************/
  822. /***********************************************************************************/
  823. /***********************************************************************************/
  824. /***********************************************************************************/
  825. int Curve3D::get_point_count() const {
  826. return points.size();
  827. }
  828. void Curve3D::add_point(const Vector3 &p_position, const Vector3 &p_in, const Vector3 &p_out, int p_atpos) {
  829. Point n;
  830. n.position = p_position;
  831. n.in = p_in;
  832. n.out = p_out;
  833. if (p_atpos >= 0 && p_atpos < points.size()) {
  834. points.insert(p_atpos, n);
  835. } else {
  836. points.push_back(n);
  837. }
  838. baked_cache_dirty = true;
  839. emit_signal(CoreStringNames::get_singleton()->changed);
  840. }
  841. void Curve3D::set_point_position(int p_index, const Vector3 &p_position) {
  842. ERR_FAIL_INDEX(p_index, points.size());
  843. points.write[p_index].position = p_position;
  844. baked_cache_dirty = true;
  845. emit_signal(CoreStringNames::get_singleton()->changed);
  846. }
  847. Vector3 Curve3D::get_point_position(int p_index) const {
  848. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  849. return points[p_index].position;
  850. }
  851. void Curve3D::set_point_tilt(int p_index, real_t p_tilt) {
  852. ERR_FAIL_INDEX(p_index, points.size());
  853. points.write[p_index].tilt = p_tilt;
  854. baked_cache_dirty = true;
  855. emit_signal(CoreStringNames::get_singleton()->changed);
  856. }
  857. real_t Curve3D::get_point_tilt(int p_index) const {
  858. ERR_FAIL_INDEX_V(p_index, points.size(), 0);
  859. return points[p_index].tilt;
  860. }
  861. void Curve3D::set_point_in(int p_index, const Vector3 &p_in) {
  862. ERR_FAIL_INDEX(p_index, points.size());
  863. points.write[p_index].in = p_in;
  864. baked_cache_dirty = true;
  865. emit_signal(CoreStringNames::get_singleton()->changed);
  866. }
  867. Vector3 Curve3D::get_point_in(int p_index) const {
  868. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  869. return points[p_index].in;
  870. }
  871. void Curve3D::set_point_out(int p_index, const Vector3 &p_out) {
  872. ERR_FAIL_INDEX(p_index, points.size());
  873. points.write[p_index].out = p_out;
  874. baked_cache_dirty = true;
  875. emit_signal(CoreStringNames::get_singleton()->changed);
  876. }
  877. Vector3 Curve3D::get_point_out(int p_index) const {
  878. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  879. return points[p_index].out;
  880. }
  881. void Curve3D::remove_point(int p_index) {
  882. ERR_FAIL_INDEX(p_index, points.size());
  883. points.remove_at(p_index);
  884. baked_cache_dirty = true;
  885. emit_signal(CoreStringNames::get_singleton()->changed);
  886. }
  887. void Curve3D::clear_points() {
  888. if (!points.is_empty()) {
  889. points.clear();
  890. baked_cache_dirty = true;
  891. emit_signal(CoreStringNames::get_singleton()->changed);
  892. }
  893. }
  894. Vector3 Curve3D::interpolate(int p_index, real_t p_offset) const {
  895. int pc = points.size();
  896. ERR_FAIL_COND_V(pc == 0, Vector3());
  897. if (p_index >= pc - 1) {
  898. return points[pc - 1].position;
  899. } else if (p_index < 0) {
  900. return points[0].position;
  901. }
  902. Vector3 p0 = points[p_index].position;
  903. Vector3 p1 = p0 + points[p_index].out;
  904. Vector3 p3 = points[p_index + 1].position;
  905. Vector3 p2 = p3 + points[p_index + 1].in;
  906. return _bezier_interp(p_offset, p0, p1, p2, p3);
  907. }
  908. Vector3 Curve3D::interpolatef(real_t p_findex) const {
  909. if (p_findex < 0) {
  910. p_findex = 0;
  911. } else if (p_findex >= points.size()) {
  912. p_findex = points.size();
  913. }
  914. return interpolate((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
  915. }
  916. void Curve3D::_bake_segment3d(Map<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 {
  917. real_t mp = p_begin + (p_end - p_begin) * 0.5;
  918. Vector3 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  919. Vector3 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  920. Vector3 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  921. Vector3 na = (mid - beg).normalized();
  922. Vector3 nb = (end - mid).normalized();
  923. real_t dp = na.dot(nb);
  924. if (dp < Math::cos(Math::deg2rad(p_tol))) {
  925. r_bake[mp] = mid;
  926. }
  927. if (p_depth < p_max_depth) {
  928. _bake_segment3d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  929. _bake_segment3d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  930. }
  931. }
  932. void Curve3D::_bake() const {
  933. if (!baked_cache_dirty) {
  934. return;
  935. }
  936. baked_max_ofs = 0;
  937. baked_cache_dirty = false;
  938. if (points.size() == 0) {
  939. baked_point_cache.clear();
  940. baked_tilt_cache.clear();
  941. baked_up_vector_cache.clear();
  942. baked_dist_cache.clear();
  943. return;
  944. }
  945. if (points.size() == 1) {
  946. baked_point_cache.resize(1);
  947. baked_point_cache.set(0, points[0].position);
  948. baked_tilt_cache.resize(1);
  949. baked_tilt_cache.set(0, points[0].tilt);
  950. baked_dist_cache.resize(1);
  951. baked_dist_cache.set(0, 0.0);
  952. if (up_vector_enabled) {
  953. baked_up_vector_cache.resize(1);
  954. baked_up_vector_cache.set(0, Vector3(0, 1, 0));
  955. } else {
  956. baked_up_vector_cache.clear();
  957. }
  958. return;
  959. }
  960. Vector3 position = points[0].position;
  961. real_t dist = 0.0;
  962. List<Plane> pointlist;
  963. List<real_t> distlist;
  964. pointlist.push_back(Plane(position, points[0].tilt));
  965. distlist.push_back(0.0);
  966. for (int i = 0; i < points.size() - 1; i++) {
  967. real_t step = 0.1; // at least 10 substeps ought to be enough?
  968. real_t p = 0.0;
  969. while (p < 1.0) {
  970. real_t np = p + step;
  971. if (np > 1.0) {
  972. np = 1.0;
  973. }
  974. Vector3 npp = _bezier_interp(np, points[i].position, points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position);
  975. real_t d = position.distance_to(npp);
  976. if (d > bake_interval) {
  977. // OK! between P and NP there _has_ to be Something, let's go searching!
  978. int iterations = 10; //lots of detail!
  979. real_t low = p;
  980. real_t hi = np;
  981. real_t mid = low + (hi - low) * 0.5;
  982. for (int j = 0; j < iterations; j++) {
  983. npp = _bezier_interp(mid, points[i].position, points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position);
  984. d = position.distance_to(npp);
  985. if (bake_interval < d) {
  986. hi = mid;
  987. } else {
  988. low = mid;
  989. }
  990. mid = low + (hi - low) * 0.5;
  991. }
  992. position = npp;
  993. p = mid;
  994. Plane post;
  995. post.normal = position;
  996. post.d = Math::lerp(points[i].tilt, points[i + 1].tilt, mid);
  997. dist += d;
  998. pointlist.push_back(post);
  999. distlist.push_back(dist);
  1000. } else {
  1001. p = np;
  1002. }
  1003. }
  1004. }
  1005. Vector3 lastpos = points[points.size() - 1].position;
  1006. real_t lastilt = points[points.size() - 1].tilt;
  1007. real_t rem = position.distance_to(lastpos);
  1008. dist += rem;
  1009. baked_max_ofs = dist;
  1010. pointlist.push_back(Plane(lastpos, lastilt));
  1011. distlist.push_back(dist);
  1012. baked_point_cache.resize(pointlist.size());
  1013. Vector3 *w = baked_point_cache.ptrw();
  1014. int idx = 0;
  1015. baked_tilt_cache.resize(pointlist.size());
  1016. real_t *wt = baked_tilt_cache.ptrw();
  1017. baked_up_vector_cache.resize(up_vector_enabled ? pointlist.size() : 0);
  1018. Vector3 *up_write = baked_up_vector_cache.ptrw();
  1019. baked_dist_cache.resize(pointlist.size());
  1020. real_t *wd = baked_dist_cache.ptrw();
  1021. Vector3 sideways;
  1022. Vector3 up;
  1023. Vector3 forward;
  1024. Vector3 prev_sideways = Vector3(1, 0, 0);
  1025. Vector3 prev_up = Vector3(0, 1, 0);
  1026. Vector3 prev_forward = Vector3(0, 0, 1);
  1027. for (const Plane &E : pointlist) {
  1028. w[idx] = E.normal;
  1029. wt[idx] = E.d;
  1030. wd[idx] = distlist[idx];
  1031. if (!up_vector_enabled) {
  1032. idx++;
  1033. continue;
  1034. }
  1035. forward = idx > 0 ? (w[idx] - w[idx - 1]).normalized() : prev_forward;
  1036. real_t y_dot = prev_up.dot(forward);
  1037. if (y_dot > (1.0f - CMP_EPSILON)) {
  1038. sideways = prev_sideways;
  1039. up = -prev_forward;
  1040. } else if (y_dot < -(1.0f - CMP_EPSILON)) {
  1041. sideways = prev_sideways;
  1042. up = prev_forward;
  1043. } else {
  1044. sideways = prev_up.cross(forward).normalized();
  1045. up = forward.cross(sideways).normalized();
  1046. }
  1047. if (idx == 1) {
  1048. up_write[0] = up;
  1049. }
  1050. up_write[idx] = up;
  1051. prev_sideways = sideways;
  1052. prev_up = up;
  1053. prev_forward = forward;
  1054. idx++;
  1055. }
  1056. }
  1057. real_t Curve3D::get_baked_length() const {
  1058. if (baked_cache_dirty) {
  1059. _bake();
  1060. }
  1061. return baked_max_ofs;
  1062. }
  1063. Vector3 Curve3D::interpolate_baked(real_t p_offset, bool p_cubic) const {
  1064. if (baked_cache_dirty) {
  1065. _bake();
  1066. }
  1067. //validate//
  1068. int pc = baked_point_cache.size();
  1069. ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
  1070. if (pc == 1) {
  1071. return baked_point_cache.get(0);
  1072. }
  1073. const Vector3 *r = baked_point_cache.ptr();
  1074. if (p_offset < 0) {
  1075. return r[0];
  1076. }
  1077. if (p_offset >= baked_max_ofs) {
  1078. return r[pc - 1];
  1079. }
  1080. int start = 0, end = pc, idx = (end + start) / 2;
  1081. // binary search to find baked points
  1082. while (start < idx) {
  1083. real_t offset = baked_dist_cache[idx];
  1084. if (p_offset <= offset) {
  1085. end = idx;
  1086. } else {
  1087. start = idx;
  1088. }
  1089. idx = (end + start) / 2;
  1090. }
  1091. real_t offset_begin = baked_dist_cache[idx];
  1092. real_t offset_end = baked_dist_cache[idx + 1];
  1093. real_t idx_interval = offset_end - offset_begin;
  1094. ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, Vector3(), "failed to find baked segment");
  1095. real_t frac = (p_offset - offset_begin) / idx_interval;
  1096. if (p_cubic) {
  1097. Vector3 pre = idx > 0 ? r[idx - 1] : r[idx];
  1098. Vector3 post = (idx < (pc - 2)) ? r[idx + 2] : r[idx + 1];
  1099. return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
  1100. } else {
  1101. return r[idx].lerp(r[idx + 1], frac);
  1102. }
  1103. }
  1104. real_t Curve3D::interpolate_baked_tilt(real_t p_offset) const {
  1105. if (baked_cache_dirty) {
  1106. _bake();
  1107. }
  1108. //validate//
  1109. int pc = baked_tilt_cache.size();
  1110. ERR_FAIL_COND_V_MSG(pc == 0, 0, "No tilts in Curve3D.");
  1111. if (pc == 1) {
  1112. return baked_tilt_cache.get(0);
  1113. }
  1114. const real_t *r = baked_tilt_cache.ptr();
  1115. if (p_offset < 0) {
  1116. return r[0];
  1117. }
  1118. if (p_offset >= baked_max_ofs) {
  1119. return r[pc - 1];
  1120. }
  1121. int start = 0, end = pc, idx = (end + start) / 2;
  1122. // binary search to find baked points
  1123. while (start < idx) {
  1124. real_t offset = baked_dist_cache[idx];
  1125. if (p_offset <= offset) {
  1126. end = idx;
  1127. } else {
  1128. start = idx;
  1129. }
  1130. idx = (end + start) / 2;
  1131. }
  1132. real_t offset_begin = baked_dist_cache[idx];
  1133. real_t offset_end = baked_dist_cache[idx + 1];
  1134. real_t idx_interval = offset_end - offset_begin;
  1135. ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, 0, "failed to find baked segment");
  1136. real_t frac = (p_offset - offset_begin) / idx_interval;
  1137. return Math::lerp(r[idx], r[idx + 1], (real_t)frac);
  1138. }
  1139. Vector3 Curve3D::interpolate_baked_up_vector(real_t p_offset, bool p_apply_tilt) const {
  1140. if (baked_cache_dirty) {
  1141. _bake();
  1142. }
  1143. //validate//
  1144. // curve may not have baked up vectors
  1145. int count = baked_up_vector_cache.size();
  1146. ERR_FAIL_COND_V_MSG(count == 0, Vector3(0, 1, 0), "No up vectors in Curve3D.");
  1147. if (count == 1) {
  1148. return baked_up_vector_cache.get(0);
  1149. }
  1150. const Vector3 *r = baked_up_vector_cache.ptr();
  1151. const Vector3 *rp = baked_point_cache.ptr();
  1152. const real_t *rt = baked_tilt_cache.ptr();
  1153. int start = 0, end = count, idx = (end + start) / 2;
  1154. // binary search to find baked points
  1155. while (start < idx) {
  1156. real_t offset = baked_dist_cache[idx];
  1157. if (p_offset <= offset) {
  1158. end = idx;
  1159. } else {
  1160. start = idx;
  1161. }
  1162. idx = (end + start) / 2;
  1163. }
  1164. real_t offset_begin = baked_dist_cache[idx];
  1165. real_t offset_end = baked_dist_cache[idx + 1];
  1166. real_t idx_interval = offset_end - offset_begin;
  1167. ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, Vector3(0, 1, 0), "failed to find baked segment");
  1168. real_t frac = (p_offset - offset_begin) / idx_interval;
  1169. if (idx == count - 1) {
  1170. return p_apply_tilt ? r[idx].rotated((rp[idx] - rp[idx - 1]).normalized(), rt[idx]) : r[idx];
  1171. }
  1172. Vector3 forward = (rp[idx + 1] - rp[idx]).normalized();
  1173. Vector3 up = r[idx];
  1174. Vector3 up1 = r[idx + 1];
  1175. if (p_apply_tilt) {
  1176. up.rotate(forward, rt[idx]);
  1177. up1.rotate(idx + 2 >= count ? forward : (rp[idx + 2] - rp[idx + 1]).normalized(), rt[idx + 1]);
  1178. }
  1179. Vector3 axis = up.cross(up1);
  1180. if (axis.length_squared() < CMP_EPSILON2) {
  1181. axis = forward;
  1182. } else {
  1183. axis.normalize();
  1184. }
  1185. return up.rotated(axis, up.angle_to(up1) * frac);
  1186. }
  1187. PackedVector3Array Curve3D::get_baked_points() const {
  1188. if (baked_cache_dirty) {
  1189. _bake();
  1190. }
  1191. return baked_point_cache;
  1192. }
  1193. Vector<real_t> Curve3D::get_baked_tilts() const {
  1194. if (baked_cache_dirty) {
  1195. _bake();
  1196. }
  1197. return baked_tilt_cache;
  1198. }
  1199. PackedVector3Array Curve3D::get_baked_up_vectors() const {
  1200. if (baked_cache_dirty) {
  1201. _bake();
  1202. }
  1203. return baked_up_vector_cache;
  1204. }
  1205. Vector3 Curve3D::get_closest_point(const Vector3 &p_to_point) const {
  1206. // Brute force method
  1207. if (baked_cache_dirty) {
  1208. _bake();
  1209. }
  1210. //validate//
  1211. int pc = baked_point_cache.size();
  1212. ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
  1213. if (pc == 1) {
  1214. return baked_point_cache.get(0);
  1215. }
  1216. const Vector3 *r = baked_point_cache.ptr();
  1217. Vector3 nearest;
  1218. real_t nearest_dist = -1.0f;
  1219. for (int i = 0; i < pc - 1; i++) {
  1220. Vector3 origin = r[i];
  1221. Vector3 direction = (r[i + 1] - origin) / bake_interval;
  1222. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
  1223. Vector3 proj = origin + direction * d;
  1224. real_t dist = proj.distance_squared_to(p_to_point);
  1225. if (nearest_dist < 0.0f || dist < nearest_dist) {
  1226. nearest = proj;
  1227. nearest_dist = dist;
  1228. }
  1229. }
  1230. return nearest;
  1231. }
  1232. real_t Curve3D::get_closest_offset(const Vector3 &p_to_point) const {
  1233. // Brute force method
  1234. if (baked_cache_dirty) {
  1235. _bake();
  1236. }
  1237. //validate//
  1238. int pc = baked_point_cache.size();
  1239. ERR_FAIL_COND_V_MSG(pc == 0, 0.0f, "No points in Curve3D.");
  1240. if (pc == 1) {
  1241. return 0.0f;
  1242. }
  1243. const Vector3 *r = baked_point_cache.ptr();
  1244. real_t nearest = 0.0f;
  1245. real_t nearest_dist = -1.0f;
  1246. real_t offset = 0.0f;
  1247. for (int i = 0; i < pc - 1; i++) {
  1248. Vector3 origin = r[i];
  1249. Vector3 direction = (r[i + 1] - origin) / bake_interval;
  1250. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
  1251. Vector3 proj = origin + direction * d;
  1252. real_t dist = proj.distance_squared_to(p_to_point);
  1253. if (nearest_dist < 0.0f || dist < nearest_dist) {
  1254. nearest = offset + d;
  1255. nearest_dist = dist;
  1256. }
  1257. offset += bake_interval;
  1258. }
  1259. return nearest;
  1260. }
  1261. void Curve3D::set_bake_interval(real_t p_tolerance) {
  1262. bake_interval = p_tolerance;
  1263. baked_cache_dirty = true;
  1264. emit_signal(CoreStringNames::get_singleton()->changed);
  1265. }
  1266. real_t Curve3D::get_bake_interval() const {
  1267. return bake_interval;
  1268. }
  1269. void Curve3D::set_up_vector_enabled(bool p_enable) {
  1270. up_vector_enabled = p_enable;
  1271. baked_cache_dirty = true;
  1272. emit_signal(CoreStringNames::get_singleton()->changed);
  1273. }
  1274. bool Curve3D::is_up_vector_enabled() const {
  1275. return up_vector_enabled;
  1276. }
  1277. Dictionary Curve3D::_get_data() const {
  1278. Dictionary dc;
  1279. PackedVector3Array d;
  1280. d.resize(points.size() * 3);
  1281. Vector3 *w = d.ptrw();
  1282. Vector<real_t> t;
  1283. t.resize(points.size());
  1284. real_t *wt = t.ptrw();
  1285. for (int i = 0; i < points.size(); i++) {
  1286. w[i * 3 + 0] = points[i].in;
  1287. w[i * 3 + 1] = points[i].out;
  1288. w[i * 3 + 2] = points[i].position;
  1289. wt[i] = points[i].tilt;
  1290. }
  1291. dc["points"] = d;
  1292. dc["tilts"] = t;
  1293. return dc;
  1294. }
  1295. void Curve3D::_set_data(const Dictionary &p_data) {
  1296. ERR_FAIL_COND(!p_data.has("points"));
  1297. ERR_FAIL_COND(!p_data.has("tilts"));
  1298. PackedVector3Array rp = p_data["points"];
  1299. int pc = rp.size();
  1300. ERR_FAIL_COND(pc % 3 != 0);
  1301. points.resize(pc / 3);
  1302. const Vector3 *r = rp.ptr();
  1303. Vector<real_t> rtl = p_data["tilts"];
  1304. const real_t *rt = rtl.ptr();
  1305. for (int i = 0; i < points.size(); i++) {
  1306. points.write[i].in = r[i * 3 + 0];
  1307. points.write[i].out = r[i * 3 + 1];
  1308. points.write[i].position = r[i * 3 + 2];
  1309. points.write[i].tilt = rt[i];
  1310. }
  1311. baked_cache_dirty = true;
  1312. }
  1313. PackedVector3Array Curve3D::tessellate(int p_max_stages, real_t p_tolerance) const {
  1314. PackedVector3Array tess;
  1315. if (points.size() == 0) {
  1316. return tess;
  1317. }
  1318. Vector<Map<real_t, Vector3>> midpoints;
  1319. midpoints.resize(points.size() - 1);
  1320. int pc = 1;
  1321. for (int i = 0; i < points.size() - 1; i++) {
  1322. _bake_segment3d(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[i + 1].position, points[i + 1].in, 0, p_max_stages, p_tolerance);
  1323. pc++;
  1324. pc += midpoints[i].size();
  1325. }
  1326. tess.resize(pc);
  1327. Vector3 *bpw = tess.ptrw();
  1328. bpw[0] = points[0].position;
  1329. int pidx = 0;
  1330. for (int i = 0; i < points.size() - 1; i++) {
  1331. for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
  1332. pidx++;
  1333. bpw[pidx] = E.value;
  1334. }
  1335. pidx++;
  1336. bpw[pidx] = points[i + 1].position;
  1337. }
  1338. return tess;
  1339. }
  1340. void Curve3D::_bind_methods() {
  1341. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve3D::get_point_count);
  1342. ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "at_position"), &Curve3D::add_point, DEFVAL(Vector3()), DEFVAL(Vector3()), DEFVAL(-1));
  1343. ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve3D::set_point_position);
  1344. ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve3D::get_point_position);
  1345. ClassDB::bind_method(D_METHOD("set_point_tilt", "idx", "tilt"), &Curve3D::set_point_tilt);
  1346. ClassDB::bind_method(D_METHOD("get_point_tilt", "idx"), &Curve3D::get_point_tilt);
  1347. ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve3D::set_point_in);
  1348. ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve3D::get_point_in);
  1349. ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve3D::set_point_out);
  1350. ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve3D::get_point_out);
  1351. ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve3D::remove_point);
  1352. ClassDB::bind_method(D_METHOD("clear_points"), &Curve3D::clear_points);
  1353. ClassDB::bind_method(D_METHOD("interpolate", "idx", "t"), &Curve3D::interpolate);
  1354. ClassDB::bind_method(D_METHOD("interpolatef", "fofs"), &Curve3D::interpolatef);
  1355. //ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve3D::bake,DEFVAL(10));
  1356. ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve3D::set_bake_interval);
  1357. ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve3D::get_bake_interval);
  1358. ClassDB::bind_method(D_METHOD("set_up_vector_enabled", "enable"), &Curve3D::set_up_vector_enabled);
  1359. ClassDB::bind_method(D_METHOD("is_up_vector_enabled"), &Curve3D::is_up_vector_enabled);
  1360. ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve3D::get_baked_length);
  1361. ClassDB::bind_method(D_METHOD("interpolate_baked", "offset", "cubic"), &Curve3D::interpolate_baked, DEFVAL(false));
  1362. ClassDB::bind_method(D_METHOD("interpolate_baked_up_vector", "offset", "apply_tilt"), &Curve3D::interpolate_baked_up_vector, DEFVAL(false));
  1363. ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve3D::get_baked_points);
  1364. ClassDB::bind_method(D_METHOD("get_baked_tilts"), &Curve3D::get_baked_tilts);
  1365. ClassDB::bind_method(D_METHOD("get_baked_up_vectors"), &Curve3D::get_baked_up_vectors);
  1366. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Curve3D::get_closest_point);
  1367. ClassDB::bind_method(D_METHOD("get_closest_offset", "to_point"), &Curve3D::get_closest_offset);
  1368. ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve3D::tessellate, DEFVAL(5), DEFVAL(4));
  1369. ClassDB::bind_method(D_METHOD("_get_data"), &Curve3D::_get_data);
  1370. ClassDB::bind_method(D_METHOD("_set_data"), &Curve3D::_set_data);
  1371. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
  1372. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  1373. ADD_GROUP("Up Vector", "up_vector_");
  1374. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "up_vector_enabled"), "set_up_vector_enabled", "is_up_vector_enabled");
  1375. }
  1376. Curve3D::Curve3D() {
  1377. /* add_point(Vector3(-1,0,0));
  1378. add_point(Vector3(0,2,0));
  1379. add_point(Vector3(0,3,5));*/
  1380. }