forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curve.cpp
2332 lines (1887 loc) · 70.2 KB
/
curve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* curve.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "curve.h"
#include "core/math/math_funcs.h"
const char *Curve::SIGNAL_RANGE_CHANGED = "range_changed";
Curve::Curve() {
}
void Curve::set_point_count(int p_count) {
ERR_FAIL_COND(p_count < 0);
int old_size = _points.size();
if (old_size == p_count) {
return;
}
if (old_size > p_count) {
_points.resize(p_count);
mark_dirty();
} else {
for (int i = p_count - old_size; i > 0; i--) {
_add_point(Vector2());
}
}
notify_property_list_changed();
}
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) {
// Add a point and preserve order
// Curve bounds is in 0..1
if (p_position.x > MAX_X) {
p_position.x = MAX_X;
} else if (p_position.x < MIN_X) {
p_position.x = MIN_X;
}
int ret = -1;
if (_points.size() == 0) {
_points.push_back(Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
ret = 0;
} else if (_points.size() == 1) {
// TODO Is the `else` able to handle this block already?
real_t diff = p_position.x - _points[0].position.x;
if (diff > 0) {
_points.push_back(Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
ret = 1;
} else {
_points.insert(0, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
ret = 0;
}
} else {
int i = get_index(p_position.x);
if (i == 0 && p_position.x < _points[0].position.x) {
// Insert before anything else
_points.insert(0, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
ret = 0;
} else {
// Insert between i and i+1
++i;
_points.insert(i, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
ret = i;
}
}
update_auto_tangents(ret);
mark_dirty();
return ret;
}
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) {
int ret = _add_point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode);
notify_property_list_changed();
return ret;
}
// TODO: Needed to make the curve editor function properly until https://github.com/godotengine/godot/issues/76985 is fixed.
int Curve::add_point_no_update(Vector2 p_position, real_t p_left_tangent, real_t p_right_tangent, TangentMode p_left_mode, TangentMode p_right_mode) {
int ret = _add_point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode);
return ret;
}
int Curve::get_index(real_t p_offset) const {
// Lower-bound float binary search
int imin = 0;
int imax = _points.size() - 1;
while (imax - imin > 1) {
int m = (imin + imax) / 2;
real_t a = _points[m].position.x;
real_t b = _points[m + 1].position.x;
if (a < p_offset && b < p_offset) {
imin = m;
} else if (a > p_offset) {
imax = m;
} else {
return m;
}
}
// Will happen if the offset is out of bounds
if (p_offset > _points[imax].position.x) {
return imax;
}
return imin;
}
void Curve::clean_dupes() {
bool dirty = false;
for (int i = 1; i < _points.size(); ++i) {
real_t diff = _points[i - 1].position.x - _points[i].position.x;
if (diff <= CMP_EPSILON) {
_points.remove_at(i);
--i;
dirty = true;
}
}
if (dirty) {
mark_dirty();
}
}
void Curve::set_point_left_tangent(int p_index, real_t p_tangent) {
ERR_FAIL_INDEX(p_index, _points.size());
_points.write[p_index].left_tangent = p_tangent;
_points.write[p_index].left_mode = TANGENT_FREE;
mark_dirty();
}
void Curve::set_point_right_tangent(int p_index, real_t p_tangent) {
ERR_FAIL_INDEX(p_index, _points.size());
_points.write[p_index].right_tangent = p_tangent;
_points.write[p_index].right_mode = TANGENT_FREE;
mark_dirty();
}
void Curve::set_point_left_mode(int p_index, TangentMode p_mode) {
ERR_FAIL_INDEX(p_index, _points.size());
_points.write[p_index].left_mode = p_mode;
if (p_index > 0) {
if (p_mode == TANGENT_LINEAR) {
Vector2 v = (_points[p_index - 1].position - _points[p_index].position).normalized();
_points.write[p_index].left_tangent = v.y / v.x;
}
}
mark_dirty();
}
void Curve::set_point_right_mode(int p_index, TangentMode p_mode) {
ERR_FAIL_INDEX(p_index, _points.size());
_points.write[p_index].right_mode = p_mode;
if (p_index + 1 < _points.size()) {
if (p_mode == TANGENT_LINEAR) {
Vector2 v = (_points[p_index + 1].position - _points[p_index].position).normalized();
_points.write[p_index].right_tangent = v.y / v.x;
}
}
mark_dirty();
}
real_t Curve::get_point_left_tangent(int p_index) const {
ERR_FAIL_INDEX_V(p_index, _points.size(), 0);
return _points[p_index].left_tangent;
}
real_t Curve::get_point_right_tangent(int p_index) const {
ERR_FAIL_INDEX_V(p_index, _points.size(), 0);
return _points[p_index].right_tangent;
}
Curve::TangentMode Curve::get_point_left_mode(int p_index) const {
ERR_FAIL_INDEX_V(p_index, _points.size(), TANGENT_FREE);
return _points[p_index].left_mode;
}
Curve::TangentMode Curve::get_point_right_mode(int p_index) const {
ERR_FAIL_INDEX_V(p_index, _points.size(), TANGENT_FREE);
return _points[p_index].right_mode;
}
void Curve::_remove_point(int p_index) {
ERR_FAIL_INDEX(p_index, _points.size());
_points.remove_at(p_index);
mark_dirty();
}
void Curve::remove_point(int p_index) {
_remove_point(p_index);
notify_property_list_changed();
}
void Curve::clear_points() {
if (_points.is_empty()) {
return;
}
_points.clear();
mark_dirty();
notify_property_list_changed();
}
void Curve::set_point_value(int p_index, real_t p_position) {
ERR_FAIL_INDEX(p_index, _points.size());
_points.write[p_index].position.y = p_position;
update_auto_tangents(p_index);
mark_dirty();
}
int Curve::set_point_offset(int p_index, real_t p_offset) {
ERR_FAIL_INDEX_V(p_index, _points.size(), -1);
Point p = _points[p_index];
_remove_point(p_index);
int i = _add_point(Vector2(p_offset, p.position.y));
_points.write[i].left_tangent = p.left_tangent;
_points.write[i].right_tangent = p.right_tangent;
_points.write[i].left_mode = p.left_mode;
_points.write[i].right_mode = p.right_mode;
if (p_index != i) {
update_auto_tangents(p_index);
}
update_auto_tangents(i);
return i;
}
Vector2 Curve::get_point_position(int p_index) const {
ERR_FAIL_INDEX_V(p_index, _points.size(), Vector2(0, 0));
return _points[p_index].position;
}
Curve::Point Curve::get_point(int p_index) const {
ERR_FAIL_INDEX_V(p_index, _points.size(), Point());
return _points[p_index];
}
void Curve::update_auto_tangents(int p_index) {
Point &p = _points.write[p_index];
if (p_index > 0) {
if (p.left_mode == TANGENT_LINEAR) {
Vector2 v = (_points[p_index - 1].position - p.position).normalized();
p.left_tangent = v.y / v.x;
}
if (_points[p_index - 1].right_mode == TANGENT_LINEAR) {
Vector2 v = (_points[p_index - 1].position - p.position).normalized();
_points.write[p_index - 1].right_tangent = v.y / v.x;
}
}
if (p_index + 1 < _points.size()) {
if (p.right_mode == TANGENT_LINEAR) {
Vector2 v = (_points[p_index + 1].position - p.position).normalized();
p.right_tangent = v.y / v.x;
}
if (_points[p_index + 1].left_mode == TANGENT_LINEAR) {
Vector2 v = (_points[p_index + 1].position - p.position).normalized();
_points.write[p_index + 1].left_tangent = v.y / v.x;
}
}
}
#define MIN_Y_RANGE 0.01
void Curve::set_min_value(real_t p_min) {
if (_minmax_set_once & 0b11 && p_min > _max_value - MIN_Y_RANGE) {
_min_value = _max_value - MIN_Y_RANGE;
} else {
_minmax_set_once |= 0b10; // first bit is "min set"
_min_value = p_min;
}
// Note: min and max are indicative values,
// it's still possible that existing points are out of range at this point.
emit_signal(SNAME(SIGNAL_RANGE_CHANGED));
}
void Curve::set_max_value(real_t p_max) {
if (_minmax_set_once & 0b11 && p_max < _min_value + MIN_Y_RANGE) {
_max_value = _min_value + MIN_Y_RANGE;
} else {
_minmax_set_once |= 0b01; // second bit is "max set"
_max_value = p_max;
}
emit_signal(SNAME(SIGNAL_RANGE_CHANGED));
}
real_t Curve::sample(real_t p_offset) const {
if (_points.size() == 0) {
return 0;
}
if (_points.size() == 1) {
return _points[0].position.y;
}
int i = get_index(p_offset);
if (i == _points.size() - 1) {
return _points[i].position.y;
}
real_t local = p_offset - _points[i].position.x;
if (i == 0 && local <= 0) {
return _points[0].position.y;
}
return sample_local_nocheck(i, local);
}
real_t Curve::sample_local_nocheck(int p_index, real_t p_local_offset) const {
const Point a = _points[p_index];
const Point b = _points[p_index + 1];
/* Cubic bézier
*
* ac-----bc
* / \
* / \ Here with a.right_tangent > 0
* / \ and b.left_tangent < 0
* / \
* a b
*
* |-d1--|-d2--|-d3--|
*
* d1 == d2 == d3 == d / 3
*/
// Control points are chosen at equal distances
real_t d = b.position.x - a.position.x;
if (Math::is_zero_approx(d)) {
return b.position.y;
}
p_local_offset /= d;
d /= 3.0;
real_t yac = a.position.y + d * a.right_tangent;
real_t ybc = b.position.y - d * b.left_tangent;
real_t y = Math::bezier_interpolate(a.position.y, yac, ybc, b.position.y, p_local_offset);
return y;
}
void Curve::mark_dirty() {
_baked_cache_dirty = true;
emit_changed();
}
Array Curve::get_data() const {
Array output;
const unsigned int ELEMS = 5;
output.resize(_points.size() * ELEMS);
for (int j = 0; j < _points.size(); ++j) {
const Point p = _points[j];
int i = j * ELEMS;
output[i] = p.position;
output[i + 1] = p.left_tangent;
output[i + 2] = p.right_tangent;
output[i + 3] = p.left_mode;
output[i + 4] = p.right_mode;
}
return output;
}
void Curve::set_data(const Array p_input) {
const unsigned int ELEMS = 5;
ERR_FAIL_COND(p_input.size() % ELEMS != 0);
// Validate input
for (int i = 0; i < p_input.size(); i += ELEMS) {
ERR_FAIL_COND(p_input[i].get_type() != Variant::VECTOR2);
ERR_FAIL_COND(!p_input[i + 1].is_num());
ERR_FAIL_COND(p_input[i + 2].get_type() != Variant::FLOAT);
ERR_FAIL_COND(p_input[i + 3].get_type() != Variant::INT);
int left_mode = p_input[i + 3];
ERR_FAIL_COND(left_mode < 0 || left_mode >= TANGENT_MODE_COUNT);
ERR_FAIL_COND(p_input[i + 4].get_type() != Variant::INT);
int right_mode = p_input[i + 4];
ERR_FAIL_COND(right_mode < 0 || right_mode >= TANGENT_MODE_COUNT);
}
int old_size = _points.size();
int new_size = p_input.size() / ELEMS;
if (old_size != new_size) {
_points.resize(new_size);
}
for (int j = 0; j < _points.size(); ++j) {
Point &p = _points.write[j];
int i = j * ELEMS;
p.position = p_input[i];
p.left_tangent = p_input[i + 1];
p.right_tangent = p_input[i + 2];
int left_mode = p_input[i + 3];
int right_mode = p_input[i + 4];
p.left_mode = (TangentMode)left_mode;
p.right_mode = (TangentMode)right_mode;
}
mark_dirty();
if (old_size != new_size) {
notify_property_list_changed();
}
}
void Curve::bake() {
_baked_cache.clear();
_baked_cache.resize(_bake_resolution);
for (int i = 1; i < _bake_resolution - 1; ++i) {
real_t x = i / static_cast<real_t>(_bake_resolution - 1);
real_t y = sample(x);
_baked_cache.write[i] = y;
}
if (_points.size() != 0) {
_baked_cache.write[0] = _points[0].position.y;
_baked_cache.write[_baked_cache.size() - 1] = _points[_points.size() - 1].position.y;
}
_baked_cache_dirty = false;
}
void Curve::set_bake_resolution(int p_resolution) {
ERR_FAIL_COND(p_resolution < 1);
ERR_FAIL_COND(p_resolution > 1000);
_bake_resolution = p_resolution;
_baked_cache_dirty = true;
}
real_t Curve::sample_baked(real_t p_offset) const {
if (_baked_cache_dirty) {
// Last-second bake if not done already
const_cast<Curve *>(this)->bake();
}
// Special cases if the cache is too small
if (_baked_cache.size() == 0) {
if (_points.size() == 0) {
return 0;
}
return _points[0].position.y;
} else if (_baked_cache.size() == 1) {
return _baked_cache[0];
}
// Get interpolation index
real_t fi = p_offset * (_baked_cache.size() - 1);
int i = Math::floor(fi);
if (i < 0) {
i = 0;
fi = 0;
} else if (i >= _baked_cache.size()) {
i = _baked_cache.size() - 1;
fi = 0;
}
// Sample
if (i + 1 < _baked_cache.size()) {
real_t t = fi - i;
return Math::lerp(_baked_cache[i], _baked_cache[i + 1], t);
} else {
return _baked_cache[_baked_cache.size() - 1];
}
}
void Curve::ensure_default_setup(real_t p_min, real_t p_max) {
if (_points.size() == 0 && _min_value == 0 && _max_value == 1) {
add_point(Vector2(0, 1));
add_point(Vector2(1, 1));
set_min_value(p_min);
set_max_value(p_max);
}
}
bool Curve::_set(const StringName &p_name, const Variant &p_value) {
Vector<String> components = String(p_name).split("/", true, 2);
if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
int point_index = components[0].trim_prefix("point_").to_int();
String property = components[1];
if (property == "position") {
Vector2 position = p_value.operator Vector2();
set_point_offset(point_index, position.x);
set_point_value(point_index, position.y);
return true;
} else if (property == "left_tangent") {
set_point_left_tangent(point_index, p_value);
return true;
} else if (property == "left_mode") {
int mode = p_value;
set_point_left_mode(point_index, (TangentMode)mode);
return true;
} else if (property == "right_tangent") {
set_point_right_tangent(point_index, p_value);
return true;
} else if (property == "right_mode") {
int mode = p_value;
set_point_right_mode(point_index, (TangentMode)mode);
return true;
}
}
return false;
}
bool Curve::_get(const StringName &p_name, Variant &r_ret) const {
Vector<String> components = String(p_name).split("/", true, 2);
if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
int point_index = components[0].trim_prefix("point_").to_int();
String property = components[1];
if (property == "position") {
r_ret = get_point_position(point_index);
return true;
} else if (property == "left_tangent") {
r_ret = get_point_left_tangent(point_index);
return true;
} else if (property == "left_mode") {
r_ret = get_point_left_mode(point_index);
return true;
} else if (property == "right_tangent") {
r_ret = get_point_right_tangent(point_index);
return true;
} else if (property == "right_mode") {
r_ret = get_point_right_mode(point_index);
return true;
}
}
return false;
}
void Curve::_get_property_list(List<PropertyInfo> *p_list) const {
for (int i = 0; i < _points.size(); i++) {
PropertyInfo pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/position", i));
pi.usage &= ~PROPERTY_USAGE_STORAGE;
p_list->push_back(pi);
if (i != 0) {
pi = PropertyInfo(Variant::FLOAT, vformat("point_%d/left_tangent", i));
pi.usage &= ~PROPERTY_USAGE_STORAGE;
p_list->push_back(pi);
pi = PropertyInfo(Variant::INT, vformat("point_%d/left_mode", i), PROPERTY_HINT_ENUM, "Free,Linear");
pi.usage &= ~PROPERTY_USAGE_STORAGE;
p_list->push_back(pi);
}
if (i != _points.size() - 1) {
pi = PropertyInfo(Variant::FLOAT, vformat("point_%d/right_tangent", i));
pi.usage &= ~PROPERTY_USAGE_STORAGE;
p_list->push_back(pi);
pi = PropertyInfo(Variant::INT, vformat("point_%d/right_mode", i), PROPERTY_HINT_ENUM, "Free,Linear");
pi.usage &= ~PROPERTY_USAGE_STORAGE;
p_list->push_back(pi);
}
}
}
void Curve::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_point_count"), &Curve::get_point_count);
ClassDB::bind_method(D_METHOD("set_point_count", "count"), &Curve::set_point_count);
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));
ClassDB::bind_method(D_METHOD("remove_point", "index"), &Curve::remove_point);
ClassDB::bind_method(D_METHOD("clear_points"), &Curve::clear_points);
ClassDB::bind_method(D_METHOD("get_point_position", "index"), &Curve::get_point_position);
ClassDB::bind_method(D_METHOD("set_point_value", "index", "y"), &Curve::set_point_value);
ClassDB::bind_method(D_METHOD("set_point_offset", "index", "offset"), &Curve::set_point_offset);
ClassDB::bind_method(D_METHOD("sample", "offset"), &Curve::sample);
ClassDB::bind_method(D_METHOD("sample_baked", "offset"), &Curve::sample_baked);
ClassDB::bind_method(D_METHOD("get_point_left_tangent", "index"), &Curve::get_point_left_tangent);
ClassDB::bind_method(D_METHOD("get_point_right_tangent", "index"), &Curve::get_point_right_tangent);
ClassDB::bind_method(D_METHOD("get_point_left_mode", "index"), &Curve::get_point_left_mode);
ClassDB::bind_method(D_METHOD("get_point_right_mode", "index"), &Curve::get_point_right_mode);
ClassDB::bind_method(D_METHOD("set_point_left_tangent", "index", "tangent"), &Curve::set_point_left_tangent);
ClassDB::bind_method(D_METHOD("set_point_right_tangent", "index", "tangent"), &Curve::set_point_right_tangent);
ClassDB::bind_method(D_METHOD("set_point_left_mode", "index", "mode"), &Curve::set_point_left_mode);
ClassDB::bind_method(D_METHOD("set_point_right_mode", "index", "mode"), &Curve::set_point_right_mode);
ClassDB::bind_method(D_METHOD("get_min_value"), &Curve::get_min_value);
ClassDB::bind_method(D_METHOD("set_min_value", "min"), &Curve::set_min_value);
ClassDB::bind_method(D_METHOD("get_max_value"), &Curve::get_max_value);
ClassDB::bind_method(D_METHOD("set_max_value", "max"), &Curve::set_max_value);
ClassDB::bind_method(D_METHOD("clean_dupes"), &Curve::clean_dupes);
ClassDB::bind_method(D_METHOD("bake"), &Curve::bake);
ClassDB::bind_method(D_METHOD("get_bake_resolution"), &Curve::get_bake_resolution);
ClassDB::bind_method(D_METHOD("set_bake_resolution", "resolution"), &Curve::set_bake_resolution);
ClassDB::bind_method(D_METHOD("_get_data"), &Curve::get_data);
ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve::set_data);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_min_value", "get_min_value");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_max_value", "get_max_value");
ADD_PROPERTY(PropertyInfo(Variant::INT, "bake_resolution", PROPERTY_HINT_RANGE, "1,1000,1"), "set_bake_resolution", "get_bake_resolution");
ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
ADD_ARRAY_COUNT("Points", "point_count", "set_point_count", "get_point_count", "point_");
ADD_SIGNAL(MethodInfo(SIGNAL_RANGE_CHANGED));
BIND_ENUM_CONSTANT(TANGENT_FREE);
BIND_ENUM_CONSTANT(TANGENT_LINEAR);
BIND_ENUM_CONSTANT(TANGENT_MODE_COUNT);
}
int Curve2D::get_point_count() const {
return points.size();
}
void Curve2D::set_point_count(int p_count) {
ERR_FAIL_COND(p_count < 0);
int old_size = points.size();
if (old_size == p_count) {
return;
}
if (old_size > p_count) {
points.resize(p_count);
mark_dirty();
} else {
for (int i = p_count - old_size; i > 0; i--) {
_add_point(Vector2());
}
}
notify_property_list_changed();
}
void Curve2D::_add_point(const Vector2 &p_position, const Vector2 &p_in, const Vector2 &p_out, int p_atpos) {
Point n;
n.position = p_position;
n.in = p_in;
n.out = p_out;
if (p_atpos >= 0 && p_atpos < points.size()) {
points.insert(p_atpos, n);
} else {
points.push_back(n);
}
mark_dirty();
}
void Curve2D::add_point(const Vector2 &p_position, const Vector2 &p_in, const Vector2 &p_out, int p_atpos) {
_add_point(p_position, p_in, p_out, p_atpos);
notify_property_list_changed();
}
void Curve2D::set_point_position(int p_index, const Vector2 &p_position) {
ERR_FAIL_INDEX(p_index, points.size());
points.write[p_index].position = p_position;
mark_dirty();
}
Vector2 Curve2D::get_point_position(int p_index) const {
ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
return points[p_index].position;
}
void Curve2D::set_point_in(int p_index, const Vector2 &p_in) {
ERR_FAIL_INDEX(p_index, points.size());
points.write[p_index].in = p_in;
mark_dirty();
}
Vector2 Curve2D::get_point_in(int p_index) const {
ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
return points[p_index].in;
}
void Curve2D::set_point_out(int p_index, const Vector2 &p_out) {
ERR_FAIL_INDEX(p_index, points.size());
points.write[p_index].out = p_out;
mark_dirty();
}
Vector2 Curve2D::get_point_out(int p_index) const {
ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
return points[p_index].out;
}
void Curve2D::_remove_point(int p_index) {
ERR_FAIL_INDEX(p_index, points.size());
points.remove_at(p_index);
mark_dirty();
}
void Curve2D::remove_point(int p_index) {
_remove_point(p_index);
notify_property_list_changed();
}
void Curve2D::clear_points() {
if (!points.is_empty()) {
points.clear();
mark_dirty();
notify_property_list_changed();
}
}
Vector2 Curve2D::sample(int p_index, const real_t p_offset) const {
int pc = points.size();
ERR_FAIL_COND_V(pc == 0, Vector2());
if (p_index >= pc - 1) {
return points[pc - 1].position;
} else if (p_index < 0) {
return points[0].position;
}
Vector2 p0 = points[p_index].position;
Vector2 p1 = p0 + points[p_index].out;
Vector2 p3 = points[p_index + 1].position;
Vector2 p2 = p3 + points[p_index + 1].in;
return p0.bezier_interpolate(p1, p2, p3, p_offset);
}
Vector2 Curve2D::samplef(real_t p_findex) const {
if (p_findex < 0) {
p_findex = 0;
} else if (p_findex >= points.size()) {
p_findex = points.size();
}
return sample((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
}
void Curve2D::mark_dirty() {
baked_cache_dirty = true;
emit_changed();
}
void Curve2D::_bake_segment2d(RBMap<real_t, Vector2> &r_bake, real_t p_begin, real_t p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_max_depth, real_t p_tol) const {
real_t mp = p_begin + (p_end - p_begin) * 0.5;
Vector2 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
Vector2 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
Vector2 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
Vector2 na = (mid - beg).normalized();
Vector2 nb = (end - mid).normalized();
real_t dp = na.dot(nb);
if (dp < Math::cos(Math::deg_to_rad(p_tol))) {
r_bake[mp] = mid;
}
if (p_depth < p_max_depth) {
_bake_segment2d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
_bake_segment2d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
}
}
void Curve2D::_bake_segment2d_even_length(RBMap<real_t, Vector2> &r_bake, real_t p_begin, real_t p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_max_depth, real_t p_length) const {
Vector2 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
Vector2 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
real_t length = beg.distance_to(end);
if (length > p_length && p_depth < p_max_depth) {
real_t mp = (p_begin + p_end) * 0.5;
Vector2 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
r_bake[mp] = mid;
_bake_segment2d_even_length(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
_bake_segment2d_even_length(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
}
}
Vector2 Curve2D::_calculate_tangent(const Vector2 &p_begin, const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) {
// Handle corner cases.
if (Math::is_zero_approx(p_t - 0.0f) && p_control_1.is_equal_approx(p_begin)) {
return (p_end - p_begin).normalized();
}
if (Math::is_zero_approx(p_t - 1.0f) && p_control_2.is_equal_approx(p_end)) {
return (p_end - p_begin).normalized();
}
return p_begin.bezier_derivative(p_control_1, p_control_2, p_end, p_t).normalized();
}
void Curve2D::_bake() const {
if (!baked_cache_dirty) {
return;
}
baked_max_ofs = 0;
baked_cache_dirty = false;
if (points.size() == 0) {
baked_point_cache.clear();
baked_dist_cache.clear();
baked_forward_vector_cache.clear();
return;
}
if (points.size() == 1) {
baked_point_cache.resize(1);
baked_point_cache.set(0, points[0].position);
baked_dist_cache.resize(1);
baked_dist_cache.set(0, 0.0);
baked_forward_vector_cache.resize(1);
baked_forward_vector_cache.set(0, Vector2(0.0, 0.1));
return;
}
// Tessellate curve to (almost) even length segments
{
Vector<RBMap<real_t, Vector2>> midpoints = _tessellate_even_length(10, bake_interval);
int pc = 1;
for (int i = 0; i < points.size() - 1; i++) {
pc++;
pc += midpoints[i].size();
}
baked_point_cache.resize(pc);
baked_dist_cache.resize(pc);
baked_forward_vector_cache.resize(pc);
Vector2 *bpw = baked_point_cache.ptrw();
Vector2 *bfw = baked_forward_vector_cache.ptrw();
// Collect positions and sample tilts and tangents for each baked points.
bpw[0] = points[0].position;
bfw[0] = _calculate_tangent(points[0].position, points[0].position + points[0].out, points[1].position + points[1].in, points[1].position, 0.0);
int pidx = 0;
for (int i = 0; i < points.size() - 1; i++) {
for (const KeyValue<real_t, Vector2> &E : midpoints[i]) {
pidx++;
bpw[pidx] = E.value;
bfw[pidx] = _calculate_tangent(points[i].position, points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, E.key);
}
pidx++;
bpw[pidx] = points[i + 1].position;
bfw[pidx] = _calculate_tangent(points[i].position, points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, 1.0);
}
// Recalculate the baked distances.
real_t *bdw = baked_dist_cache.ptrw();
bdw[0] = 0.0;
for (int i = 0; i < pc - 1; i++) {
bdw[i + 1] = bdw[i] + bpw[i].distance_to(bpw[i + 1]);
}
baked_max_ofs = bdw[pc - 1];
}
}
real_t Curve2D::get_baked_length() const {
if (baked_cache_dirty) {
_bake();
}
return baked_max_ofs;
}
Curve2D::Interval Curve2D::_find_interval(real_t p_offset) const {
Interval interval = {
-1,
0.0
};
ERR_FAIL_COND_V_MSG(baked_cache_dirty, interval, "Backed cache is dirty");
int pc = baked_point_cache.size();
ERR_FAIL_COND_V_MSG(pc < 2, interval, "Less than two points in cache");
int start = 0;
int end = pc;
int idx = (end + start) / 2;
// Binary search to find baked points.
while (start < idx) {
real_t offset = baked_dist_cache[idx];
if (p_offset <= offset) {
end = idx;
} else {
start = idx;
}
idx = (end + start) / 2;
}
real_t offset_begin = baked_dist_cache[idx];
real_t offset_end = baked_dist_cache[idx + 1];
real_t idx_interval = offset_end - offset_begin;
ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, interval, "Offset out of range.");
interval.idx = idx;
if (idx_interval < FLT_EPSILON) {
interval.frac = 0.5; // For a very short interval, 0.5 is a reasonable choice.
ERR_FAIL_V_MSG(interval, "Zero length interval.");
}
interval.frac = (p_offset - offset_begin) / idx_interval;
return interval;
}
Vector2 Curve2D::_sample_baked(Interval p_interval, bool p_cubic) const {
// Assuming p_interval is valid.
ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Vector2(), "Invalid interval");
int idx = p_interval.idx;
real_t frac = p_interval.frac;
const Vector2 *r = baked_point_cache.ptr();
int pc = baked_point_cache.size();
if (p_cubic) {
Vector2 pre = idx > 0 ? r[idx - 1] : r[idx];
Vector2 post = (idx < (pc - 2)) ? r[idx + 2] : r[idx + 1];
return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
} else {
return r[idx].lerp(r[idx + 1], frac);
}
}
Transform2D Curve2D::_sample_posture(Interval p_interval) const {
// Assuming that p_interval is valid.
ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Transform2D(), "Invalid interval");
int idx = p_interval.idx;
real_t frac = p_interval.frac;
Vector2 forward_begin = baked_forward_vector_cache[idx];
Vector2 forward_end = baked_forward_vector_cache[idx + 1];
// Build frames at both ends of the interval, then interpolate.
const Vector2 forward = forward_begin.slerp(forward_end, frac).normalized();
const Vector2 side = Vector2(-forward.y, forward.x);
return Transform2D(side, forward, Vector2(0.0, 0.0));
}
Vector2 Curve2D::sample_baked(real_t p_offset, bool p_cubic) const {
if (baked_cache_dirty) {
_bake();
}
// Validate: Curve may not have baked points.
int pc = baked_point_cache.size();
ERR_FAIL_COND_V_MSG(pc == 0, Vector2(), "No points in Curve2D.");
if (pc == 1) {
return baked_point_cache[0];
}
p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
Curve2D::Interval interval = _find_interval(p_offset);
return _sample_baked(interval, p_cubic);
}