-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Curve3.cs
862 lines (774 loc) · 23.4 KB
/
Curve3.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// Organizes a 3D Bezier curve into a list of knots. Provides a function to
/// retrieve a point and tangent on a curve from a step in the range [0.0, 1.0]
/// .
/// </summary>
public class Curve3 : IEnumerable<Knot3>
{
/// <summary>
/// A flag for whether or not the curve is a closed loop.
/// </summary>
protected bool closedLoop = false;
/// <summary>
/// The list of knots contained by the curve.
/// </summary>
protected readonly List<Knot3> knots = new();
/// <summary>
/// A flag for whether or not the curve is a closed loop.
/// </summary>
/// <value>closed loop flag</value>
public bool ClosedLoop
{
get
{
return this.closedLoop;
}
set
{
this.closedLoop = value;
}
}
/// <summary>
/// Gets the knots of this curve, copied to an array.
/// </summary>
/// <value>knots</value>
public Knot3[] Knots
{
get
{
Knot3[] result = new Knot3[this.knots.Count];
this.knots.CopyTo(result);
return result;
}
}
/// <summary>
/// Gets the number of knots in the curve.
/// </summary>
/// <value>length</value>
public int Length
{
get
{
return this.knots.Count;
}
}
/// <summary>
/// Retrieves a knot from this curve. If the curve is a closed loop, wraps
/// the index by the number of elements in the list of knots.
/// </summary>
/// <value>knot</value>
public Knot3 this[int i]
{
get
{
return this.closedLoop ?
this.knots[Utils.RemFloor(i, this.knots.Count)] :
this.knots[i];
}
}
/// <summary>
/// Evaluates the coordinate and normalized tangent given a step in the
/// range [0.0, 1.0] .
///
/// Returns a named value tuple containing two vectors.
/// </summary>
/// <value>tuple</value>
public (Vec3 coord, Vec3 tangent) this[float i]
{
get
{
return Curve3.Eval(this, i);
}
}
/// <summary>
/// The default curve constructor.
/// </summary>
protected Curve3() { }
/// <summary>
/// Creates a curve from a list of knots and a closed loop flag.
/// </summary>
/// <param name="cl">closed loop</param>
/// <param name="kn">knots</param>
public Curve3(in bool cl, params Knot3[] kn)
{
this.closedLoop = cl;
this.AppendAll(kn);
}
/// <summary>
/// Creates a curve with a given number of knots.
/// </summary>
/// <param name="cl">closed loop</param>
/// <param name="count">count</param>
public Curve3(in bool cl, in int count)
{
this.closedLoop = cl;
this.Resize(count);
}
/// <summary>
/// Returns a hash code representing this curve.
/// </summary>
/// <returns>hash code</returns>
public override int GetHashCode()
{
unchecked
{
int hash = Utils.HashBase;
hash = hash * Utils.HashMul ^ this.closedLoop.GetHashCode();
hash = hash * Utils.HashMul ^ this.knots.GetHashCode();
return hash;
}
}
/// <summary>
/// Returns a string representation of this curve.
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return Curve3.ToString(this);
}
/// <summary>
/// Append a knot to the curve's list of knots.
/// </summary>
/// <param name="knot">knot</param>
/// <returns>this curve</returns>
public Curve3 Append(in Knot3 knot)
{
this.knots.Add(knot);
return this;
}
/// <summary>
/// Append an collection of 2D knots to the curve's list of knots.
/// </summary>
/// <param name="kn">knots</param>
/// <returns>this curve</returns>
public Curve3 AppendAll(params Knot2[] kn)
{
int len = kn.Length;
for (int i = 0; i < len; ++i) { this.knots.Add(kn[i]); }
return this;
}
/// <summary>
/// Append an collection of knots to the curve's list of knots.
/// </summary>
/// <param name="kn">knots</param>
/// <returns>this curve</returns>
public Curve3 AppendAll(params Knot3[] kn)
{
this.knots.AddRange(kn);
return this;
}
/// <summary>
/// Evaluates whether a knot is contained by this curve.
/// </summary>
/// <param name="kn">knot</param>
/// <returns>evaluation</returns>
public bool Contains(in Knot3 kn)
{
return this.knots.Contains(kn);
}
/// <summary>
/// Flips this curve on the x axis, then reverses the curve.
/// </summary>
/// <returns>this curve</returns>
public Curve3 FlipX()
{
this.knots.Reverse();
foreach (Knot3 kn in this.knots) { kn.FlipX(); kn.Reverse(); }
return this;
}
/// <summary>
/// Flips this curve on the y axis, then reverses the curve.
/// </summary>
/// <returns>this curve</returns>
public Curve3 FlipY()
{
this.knots.Reverse();
foreach (Knot3 kn in this.knots) { kn.FlipY(); kn.Reverse(); }
return this;
}
/// <summary>
/// Flips this curve on the z axis, then reverses the curve.
/// </summary>
/// <returns>this curve</returns>
public Curve3 FlipZ()
{
this.knots.Reverse();
foreach (Knot3 kn in this.knots) { kn.FlipZ(); kn.Reverse(); }
return this;
}
/// <summary>
/// Gets the enumerator for this curve.
/// </summary>
/// <returns>enumerator</returns>
public IEnumerator<Knot3> GetEnumerator()
{
return this.knots.GetEnumerator();
}
/// <summary>
/// Gets the enumerator for this curve.
/// </summary>
/// <returns>enumerator</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
/// <summary>
/// Gets the first knot in the curve.
/// </summary>
/// <returns>the knot</returns>
public Knot3 GetFirst()
{
return this.knots[0];
}
/// <summary>
/// Gets the last knot in the curve.
/// </summary>
/// <returns>the knot</returns>
public Knot3 GetLast()
{
return this.knots[^1];
}
/// <summary>
/// Inserts a knot at a given index. When the curve is a closed loop, the
/// index wraps around; this means negative indices are accepted.
/// </summary>
/// <param name="i">index</param>
/// <param name="kn">knot</param>
/// <returns>this curve</returns>
public Curve3 Insert(in int i, in Knot3 kn)
{
int k = this.closedLoop ? Utils.RemFloor(i, this.knots.Count + 1) : i;
this.knots.Insert(k, kn);
return this;
}
/// <summary>
/// Prepend a knot to the curve's list of knots.
/// </summary>
/// <param name="kn">knot</param>
/// <returns>this curve</returns>
public Curve3 Prepend(in Knot3 kn)
{
this.knots.Insert(0, kn);
return this;
}
/// <summary>
/// Prepend an array of knots to the curve's list of knots.
/// Promotes the knots from 2D to 3D.
/// </summary>
/// <param name="kn">array of knots</param>
/// <returns>this curve</returns>
public Curve3 PrependAll(params Knot2[] kn)
{
int len = kn.Length;
for (int i = 0, j = 0; i < len; ++i)
{
Knot3 knot = (Knot3)kn[i];
this.knots.Insert(j, knot);
++j;
}
return this;
}
/// <summary>
/// Prepend an array of knots to the curve's list of knots.
/// </summary>
/// <param name="kn">array of knots</param>
/// <returns>this curve</returns>
public Curve3 PrependAll(params Knot3[] kn)
{
int len = kn.Length;
for (int i = 0, j = 0; i < len; ++i)
{
Knot3 knot = kn[i];
this.knots.Insert(j, knot);
++j;
}
return this;
}
/// <summary>
/// Returns and removes a knot at a given index.
/// </summary>
/// <param name="i">index</param>
/// <returns>the knot</returns>
protected Knot3 RemoveAt(in int i)
{
int j = this.closedLoop ? Utils.RemFloor(i, this.knots.Count) : i;
Knot3 knot = this.knots[j];
this.knots.RemoveAt(j);
return knot;
}
/// <summary>
/// Removes and returns the first knot in the curve.
/// </summary>
/// <returns>first knot</returns>
protected Knot3 RemoveFirst()
{
return this.RemoveAt(0);
}
/// <summary>
/// Removes and returns the last knot in the curve.
/// </summary>
/// <returns>last knot</returns>
protected Knot3 RemoveLast()
{
return this.RemoveAt(this.knots.Count - 1);
}
/// <summary>
/// For internal use. Resizes a curve to the specified length. The length
/// may be no less than 2. When the new length is greater than the old, new
/// Knot2s are added.
///
/// This does not check if remaining elements in the list are null.
/// </summary>
/// <param name="len">length</param>
/// <returns>this curve</returns>
protected Curve3 Resize(in int len)
{
int vlen = len < 2 ? 2 : len;
int oldLen = this.knots.Count;
int diff = vlen - oldLen;
if (diff < 0)
{
int last = oldLen - 1;
for (int i = 0; i < -diff; ++i)
{
this.knots.RemoveAt(last - i);
}
}
else if (diff > 0)
{
this.knots.Capacity = vlen;
for (int i = 0; i < diff; ++i)
{
this.knots.Add(new Knot3());
}
}
return this;
}
/// <summary>
/// Reverses the curve. This is done by reversing the list of knots and
/// swapping the fore- and rear-handle of each knot.
/// </summary>
/// <returns>this curve</returns>
public Curve3 Reverse()
{
this.knots.Reverse();
foreach (Knot3 kn in this.knots) { kn.Reverse(); }
return this;
}
/// <summary>
/// Rotates all knots in a curve by a quaternion.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>this curve</returns>
public Curve3 Rotate(in Quat q)
{
foreach (Knot3 kn in this.knots) { kn.Rotate(q); }
return this;
}
/// <summary>
/// Rotates all knots in the curve by an angle in radians around an
/// arbitrary axis.
/// </summary>
/// <param name="radians">angle</param>
/// <param name="axis">axis</param>
/// <returns>this curve</returns>
public Curve3 Rotate(in float radians, in Vec3 axis)
{
float cosa = MathF.Cos(radians);
float sina = MathF.Sin(radians);
foreach (Knot3 kn in this.knots) { kn.Rotate(cosa, sina, axis); }
return this;
}
/// <summary>
/// Rotates all knots in the curve by an angle in radians around the x axis.
/// </summary>
/// <param name="radians">angle</param>
/// <returns>this curve</returns>
public Curve3 RotateX(in float radians)
{
float cosa = MathF.Cos(radians);
float sina = MathF.Sin(radians);
foreach (Knot3 kn in this.knots) { kn.RotateX(cosa, sina); }
return this;
}
/// <summary>
/// Rotates all knots in the curve by an angle in radians around the y axis.
/// </summary>
/// <param name="radians">angle</param>
/// <returns>this curve</returns>
public Curve3 RotateY(in float radians)
{
float cosa = MathF.Cos(radians);
float sina = MathF.Sin(radians);
foreach (Knot3 kn in this.knots) { kn.RotateY(cosa, sina); }
return this;
}
/// <summary>
/// Rotates all knots in the curve by an angle in radians around the z axis.
/// </summary>
/// <param name="radians">angle</param>
/// <returns>this curve</returns>
public Curve3 RotateZ(in float radians)
{
float cosa = MathF.Cos(radians);
float sina = MathF.Sin(radians);
foreach (Knot3 kn in this.knots) { kn.RotateZ(cosa, sina); }
return this;
}
/// <summary>
/// Scales all knots in the curve by a scalar.
/// </summary>
/// <param name="scale">uniform scale</param>
/// <returns>this curve</returns>
public Curve3 Scale(in float scale)
{
if (scale != 0.0f)
{
foreach (Knot3 kn in this.knots) { kn.Scale(scale); }
}
return this;
}
/// <summary>
/// Scales all knots in the curve by a vector.
/// </summary>
/// <param name="scale">nonuniform scale</param>
/// <returns>this curve</returns>
public Curve3 Scale(in Vec3 scale)
{
if (Vec3.All(scale))
{
foreach (Knot3 kn in this.knots) { kn.Scale(scale); }
}
return this;
}
/// <summary>
/// Toggles whether or not the curve is a closed loop.
/// </summary>
/// <returns>this curve</returns>
public Curve3 ToggleLoop()
{
this.closedLoop = !this.closedLoop;
return this;
}
/// <summary>
/// Transforms all knots in the curve by a matrix.
/// </summary>
/// <param name="m">matrix</param>
/// <returns>this curve</returns>
public Curve3 Transform(in Mat4 m)
{
foreach (Knot3 kn in this.knots) { kn.Transform(m); }
return this;
}
/// <summary>
/// Transforms all coordinates in the curve permanently by a transform.
///
/// Not to be confused with the temporary transformations applied by a curve
/// entity's transform to the meshes contained within the entity.
///
/// Useful when consolidating multiple curve entities into one curve entity.
/// </summary>
/// <param name="tr">transform</param>
/// <returns>this curve</returns>
public Curve3 Transform(in Transform3 tr)
{
foreach (Knot3 kn in this.knots) { kn.Transform(tr); }
return this;
}
/// <summary>
/// Translates all knots in the curve by a vector.
/// </summary>
/// <param name="v">vector</param>
/// <returns>this curve</returns>
public Curve3 Translate(in Vec3 v)
{
foreach (Knot3 kn in this.knots) { kn.Translate(v); }
return this;
}
/// <summary>
/// Converts a 2D curve to a 3D curve.
/// </summary>
/// <param name="c">2D curve</param>
public static implicit operator Curve3(in Curve2 c)
{
Curve3 result = new()
{
closedLoop = c.ClosedLoop
};
foreach (Knot2 kn in c) { result.Append(kn); }
return result;
}
/// <summary>
/// Evaluates the coordinate and normalized tangent given a step in the
/// range [0.0, 1.0] .
///
/// Returns a named value tuple containing two vectors.
/// </summary>
/// <param name="c">curve</param>
/// <param name="step">step</param>
/// <returns>tuple</returns>
public static (Vec3 coord, Vec3 tangent) Eval(in Curve3 c, in float step)
{
List<Knot3> knots = c.knots;
int knotLength = knots.Count;
float tScaled;
int i;
Knot3 a;
Knot3 b;
if (c.closedLoop)
{
tScaled = Utils.RemFloor(step, 1.0f) * knotLength;
i = (int)tScaled;
a = knots[Utils.RemFloor(i, knotLength)];
b = knots[Utils.RemFloor(i + 1, knotLength)];
}
else
{
if (knotLength == 1 || step <= 0.0f)
{
return Curve3.EvalFirst(c);
}
if (step >= 1.0f)
{
return Curve3.EvalLast(c);
}
tScaled = step * (knotLength - 1);
i = (int)tScaled;
a = knots[i];
b = knots[i + 1];
}
float t = tScaled - i;
return (
coord: Knot3.BezierPoint(a, b, t),
tangent: Knot3.BezierTanUnit(a, b, t));
}
/// <summary>
/// Evaluates the coordinate and normalized tangent of the first knot in the
/// curve.
///
/// Returns a named value tuple containing two vectors.
/// </summary>
/// <param name="c">curve</param>
/// <returns>tuple</returns>
public static (Vec3 coord, Vec3 tangent) EvalFirst(in Curve3 c)
{
Knot3 kn = c.knots[0];
return (
coord: kn.Coord,
tangent: Vec3.Normalize(kn.ForeHandle - kn.Coord));
}
/// <summary>
/// Evaluates the coordinate and normalized tangent of the last knot in the
/// curve.
///
/// Returns a named value tuple containing two vectors.
/// </summary>
/// <param name="c">curve</param>
/// <returns>tuple</returns>
public static (Vec3 coord, Vec3 tangent) EvalLast(in Curve3 c)
{
List<Knot3> kns = c.knots;
Knot3 kn = kns[^1];
return (
coord: kn.Coord,
tangent: Vec3.Normalize(kn.Coord - kn.RearHandle));
}
/// <summary>
/// Evaluates a range of points and tangents on a curve
/// at a resolution, or count.
/// </summary>
/// <param name="c">curve</param>
/// <param name="count">count</param>
/// <returns>range</returns>
public static (Vec3 coord, Vec3 tangent)[] EvalRange(in Curve3 c, in int count)
{
return Curve3.EvalRange(c, count, 0.0f,
c.closedLoop ? 1.0f - 1.0f / Utils.Max(3, count) : 1.0f);
}
/// <summary>
/// Evaluates a range of points and tangents on a curve
/// for a given origin and destination factor at a
/// resolution, or count.
///
/// For closed loops, an origin and destination of
/// 0.0 and 1.0 would yield a duplicate point, and so
/// should be calculated accordingly. I.e., 0.0 to
/// 1.0 - 1.0 / count would avoid the duplicate.
/// </summary>
/// <param name="c">curve</param>
/// <param name="count">count</param>
/// <param name="origin">origin</param>
/// <param name="dest">destination</param>
/// <returns>range</returns>
public static (Vec3 coord, Vec3 tangent)[] EvalRange(
in Curve3 c,
in int count,
in float origin,
in float dest)
{
int vCount = count < 3 ? 3 : count;
float vOrigin = origin;
float vDest = dest;
if (c.closedLoop)
{
vOrigin = Utils.Clamp(vOrigin, 0.0f, 1.0f);
vDest = Utils.Clamp(vDest, 0.0f, 1.0f);
}
(Vec3 coord, Vec3 tangent)[] result = new (Vec3 coord, Vec3 tangent)[vCount];
float toPercent = 1.0f / (vCount - 1.0f);
for (int i = 0; i < vCount; ++i)
{
float fac = Utils.Mix(vOrigin, vDest, i * toPercent);
result[i] = Curve3.Eval(c, fac);
}
return result;
}
/// <summary>
/// Sets a curve to a series of points.
/// </summary>
/// <param name="target">target curve</param>
/// <param name="points">points array</param>
/// <param name="closedLoop">closed loop flag</param>
/// <returns>curve</returns>
public static Curve3 FromLinear(
in Curve3 target,
in Vec3[] points,
in bool closedLoop = false)
{
int len = points.Length;
if (len < 2) { return target; }
target.Resize(len);
target.closedLoop = closedLoop;
List<Knot3> knots = target.knots;
for (int i = 0; i < len; ++i)
{
Vec3 v = points[i];
Knot3 kn = knots[i];
kn.Coord = v;
kn.ForeHandle = v;
kn.RearHandle = v;
}
return Curve3.StraightHandles(target);
}
///<summary>
///Adjust knot handles to create a smooth, continuous curve.
///</summary>
///<param name="target">target curve</param>
///<returns>smoothed curve</returns>
public static Curve3 SmoothHandles(in Curve3 target)
{
List<Knot3> knots = target.knots;
int len = knots.Count;
if (len < 3) { return target; }
Vec3 carry = Vec3.Zero;
Knot3 first = knots[0];
if (target.closedLoop)
{
Knot3 prev = knots[len - 1];
Knot3 curr = first;
for (int i = 1; i < len; ++i)
{
Knot3 next = knots[i];
carry = Knot3.SmoothHandles(prev, curr, next, carry);
prev = curr;
curr = next;
}
Knot3.SmoothHandles(prev, curr, first, carry);
}
else
{
Knot3 prev = first;
Knot3 curr = knots[1];
carry = Knot3.SmoothHandlesFirst(prev, curr, carry);
curr.MirrorHandlesForward();
for (int i = 2; i < len; ++i)
{
Knot3 next = knots[i];
carry = Knot3.SmoothHandles(prev, curr, next, carry);
prev = curr;
curr = next;
}
Knot3.SmoothHandlesLast(prev, curr, carry);
curr.MirrorHandlesBackward();
}
return target;
}
/// <summary>
/// Straightens the fore handles and rear handles of
/// a curve's knots so they are collinear with its
/// coordinates.
/// </summary>
/// <param name="target">target curve</param>
/// <returns>curve</returns>
public static Curve3 StraightHandles(in Curve3 target)
{
List<Knot3> knots = target.knots;
int len = knots.Count;
if (len < 2) { return target; }
for (int i = 1; i < len; ++i)
{
Knot3 prev = knots[i - 1];
Knot3 next = knots[i];
prev.ForeHandle = Vec3.Mix(
prev.Coord, next.Coord, Utils.OneThird);
next.RearHandle = Vec3.Mix(
next.Coord, prev.Coord, Utils.OneThird);
}
Knot3 first = knots[0];
Knot3 last = knots[len - 1];
if (target.closedLoop)
{
first.RearHandle = Vec3.Mix(
first.Coord, last.Coord, Utils.OneThird);
last.ForeHandle = Vec3.Mix(
last.Coord, first.Coord, Utils.OneThird);
}
else
{
first.MirrorHandlesForward();
last.MirrorHandlesBackward();
}
return target;
}
/// <summary>
/// Returns the string representation of a curve.
/// </summary>
/// <param name="c">curve</param>
/// <param name="places">number of places</param>
/// <returns>string</returns>
public static string ToString(in Curve3 c, in int places = 4)
{
return Curve3.ToString(new StringBuilder(1024), c, places).ToString();
}
/// <summary>
/// Appends a string representation of a curve to a string builder.
/// </summary>
/// <param name="sb">string builder</param>
/// <param name="c">curve</param>
/// <param name="places">number of places</param>
/// <returns>string</returns>
public static StringBuilder ToString(
in StringBuilder sb,
in Curve3 c,
in int places = 4)
{
List<Knot3> knots = c.knots;
int len = knots.Count;
int last = len - 1;
sb.Append("{\"closedLoop\":");
sb.Append(c.closedLoop ? "true" : "false");
sb.Append(",\"knots\":[");
for (int i = 0; i < last; ++i)
{
Knot3.ToString(sb, knots[i], places);
sb.Append(',');
}
Knot3.ToString(sb, knots[last], places);
sb.Append("]}");
return sb;
}
}