curve.cpp 47 KB

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