-
Notifications
You must be signed in to change notification settings - Fork 26
/
BSpline.cs
4158 lines (4104 loc) · 173 KB
/
BSpline.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
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
using CADability.Attribute;
using CADability.Curve2D;
using CADability.UserInterface;
using System;
using System.Collections.Generic;
#if WEBASSEMBLY
using CADability.WebDrawing;
using Point = CADability.WebDrawing.Point;
#else
using System.Drawing;
using Point = System.Drawing.Point;
#endif
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using Wintellect.PowerCollections;
using MathNet.Numerics.LinearAlgebra.Double;
namespace CADability.GeoObject
{
public class BSplineException : ApplicationException
{
public BSplineException(string msg)
: base(msg)
{
}
}
/* NURBS aus OpenCascade übernehmen:
* BSplCLib::D0 in BSplCLib_CurveComputation.gxx ist die gesuchte Funktion. Verzeichnis C:\OpenCASCADE5.2\ros\inc
* in C:\OpenCASCADE5.2\ros\src\BSplCLib befinden sich offensichtlich die wichtigen Unterfunktionen
*/
/// <summary>
/// A BSpline is a smooth curve defined by a set of control points. It is implemented as a NURBS - non uniform rational b-spline.
/// </summary>
[Serializable]
#if DEBUG
[System.Diagnostics.DebuggerVisualizer(typeof(GeoObjectVisualizer))]
#endif
public class BSpline : IGeoObjectImpl, IColorDef, ILineWidth, ILinePattern, ISerializable, ICurve, IExplicitPCurve3D, IExportStep
{
// im folgenden die wesentlichen Daten zur Darstellung:
private GeoPoint[] poles; // die Kontrollpunkte, deren Anzahl bestimmt die Größe der anderen Arrays
private double[] weights; // kann leer sein oder genausogroß wie poles (letzerer Fall ist rational)
private double[] knots; // die Knotenwerte (für non uniform)
private int[] multiplicities; // wie oft wird jeder Knotenwert verwendet
// die Summe der multiplicities ist (Anzahl poles)-degree-1 (offen) bzw.
// die Summe der multiplicities ohne die letzte ist (Anzahl poles) (geschlossen)
internal int degree; // Grad (muss private sein, nur zum Debuggen internal gemacht)
private bool periodic; // geschlossen
private double startParam; // geht hier los
private double endParam; // und endet da
// im folgenden die erzeugenden Daten, die aber nicht vorhanden sein müssen
// wenn vorhanden, werden sie trotzdem mit abgespeichert.
private int maxDegree; // diese Angabe soll beim Erzeugen aus den Punkten verwendet werden
// Im 3d-Fall handelt es sich um eine Kurve, die nicht (notwendig) in einer Ebene liegt.
private GeoPoint[] throughPoints3d; // geht durch diese Punkte
private GeoVector[] direction3D; // hat optional diese Richtungen
private double[] throughPointsParam; // die Parameter der throughPoints3d
// alternativ dazu liegen alle Punkte in einer Ebene
// TODO: wieder wegmachen, wozu eigentlich?
// Plane hat eine Methode aus vielen Punkten die Ebene zu finden
private Plane? plane; // liegt in dieser Ebene, wenn spezifiziert
#if NET_45
private WeakReference<ExplicitPCurve3D> explicitPCurve3D;
#else
private WeakReference explicitPCurve3D;
#endif
// NurbsHelper Daten
private bool nurbsHelper;
// nur eine der 4 folgenden Variablen ist gültig, wenn nurbsHelper true ist (sonst keine)
// es ist allemal günstiger diese 4 zu halten als alles auf 3d und homogen zu rechnen
private Nurbs<GeoPoint, GeoPointPole> nubs3d;
private Nurbs<GeoPointH, GeoPointHPole> nurbs3d;
private Nurbs<GeoPoint2D, GeoPoint2DPole> nubs2d;
private Nurbs<GeoPoint2DH, GeoPoint2DHPole> nurbs2d;
private GeoPoint[] interpol; // Interpolation mit einer gewissen Genauigkeit
private GeoVector[] interdir; // Interpolation mit einer gewissen Genauigkeit
private double[] interparam; // die Parameter zur Interpolation
private double maxInterpolError; // der größte Fehler bei der Interpolation
private BoundingCube extent;
private TetraederHull tetraederHull;
private GeoPoint[] approximation; // Interpolation mit der Genauigkeit der Auflösung
private double approxPrecision; // Genauigkeit zu approximation
private object lockApproximationRecalc;
private WeakReference extrema;
private GeoPoint[] GetCashedApproximation(double precision)
{
lock (lockApproximationRecalc)
{
if (((precision > 0) && (approxPrecision > precision)) || approximation == null)
{
approxPrecision = precision;
ICurve cv = (this as ICurve).Approximate(true, precision);
if (cv is Path)
{
Path path = (cv as Path);
approximation = new GeoPoint[path.CurveCount + 1];
for (int i = 0; i < path.CurveCount; ++i)
{
approximation[i] = path.Curve(i).StartPoint;
}
approximation[path.CurveCount] = path.Curve(path.CurveCount - 1).EndPoint;
}
else if (cv is Polyline)
{
approximation = (cv as Polyline).Vertices;
}
else if (cv is Line)
{
approximation = new GeoPoint[2];
approximation[0] = cv.StartPoint;
approximation[1] = cv.EndPoint;
}
else
{
throw new ApplicationException("internal error BSpline.GetApproximation");
}
}
return approximation;
}
}
public void GetData(out GeoPoint[] poles, out double[] weights, out double[] knots, out int degree)
{ // nur intern für OpenGL Test
poles = (GeoPoint[])this.poles.Clone();
weights = (double[])this.weights.Clone();
List<double> lknots = new List<double>();
for (int i = 0; i < multiplicities.Length; ++i)
{
for (int j = 0; j < multiplicities[i]; ++j)
{
lknots.Add(this.knots[i]);
}
}
knots = lknots.ToArray();
degree = this.degree;
}
public bool IsSingular
{
get
{
for (int i = 1; i < poles.Length; ++i)
{
if (!Precision.IsEqual(poles[0], poles[i])) return false;
}
return true;
}
}
private void MakeNurbsHelper()
{
lock (this)
{
if (nurbsHelper) return; // has already been calculated
// Knotenliste ist von allem anderen unabhängig
if (periodic)
{ // folgendes wurde notwendig wg. "Ele_matrice.stp". Dort gibt es Splines vom Grad 9 und periodic
// denen ein Pol fehlt. Aber durch zufügen des ersten Pols werden Sie nicht ganz richtig.
int msum = 0;
for (int i = 0; i < multiplicities.Length; i++)
{
msum += multiplicities[i];
}
while (msum - degree - 1 < poles.Length)
{
if (multiplicities[0] < multiplicities[multiplicities.Length - 1])
{
++multiplicities[0];
}
else
{
++multiplicities[multiplicities.Length - 1];
}
++msum;
}
if (msum - degree - 1 < poles.Length)
{
List<GeoPoint> lpoles = new List<GeoPoint>(poles);
lpoles.Add(poles[0]);
poles = lpoles.ToArray();
if (weights != null)
{
List<double> lweights = new List<double>(weights);
lweights.Add(weights[0]);
weights = lweights.ToArray();
}
}
}
List<double> knotslist = new List<double>();
for (int i = 0; i < knots.Length; ++i)
{
for (int j = 0; j < multiplicities[i]; ++j)
{
knotslist.Add(knots[i]);
}
}
if (periodic && poles.Length > 2)
{
double dknot = knots[knots.Length - 1] - knots[0];
// letztlich ist es komisch, dass zwei knoten vornedran müssen
//for (int i = 0; i < 1; ++i) //
//{
// knotslist.Insert(0, knotslist[knotslist.Count - degree - i] - dknot);
//}
//for (int i = 0; i < 2 * degree - 2; ++i)
//{
// knotslist.Add(knotslist[2 * (degree - 1) + i] + dknot);
//}
// neue Idee:
// 1. es werden immer "degree" poles hinten angehängt
// 2. der 1. Knoten muss immer degree+1 mal vorkommen ( siehe STEP/piece0: dort sind alle Knoten 4-fach
// bei degree=5 und FindSpan muss immer eine Stelle finden, an der es gerade wechselt
// 3. es werden soviele Knoten hinten angehängt, dass "knotslist.Length-degree-1 == poles.length" gilt
//for (int i = 0; i < 1; ++i) //
//{
// knotslist.Insert(0, knotslist[knotslist.Count - degree - i] - dknot);
//}
int secondknotindex = multiplicities[0];
for (int i = 0; i <= degree - multiplicities[0]; ++i)
{
knotslist.Insert(0, knotslist[knotslist.Count - degree - i] - dknot);
++secondknotindex;
}
while (knotslist.Count - degree - 1 < poles.Length + degree)
{
knotslist.Add(knotslist[secondknotindex] + dknot);
++secondknotindex;
}
}
if ((this as ICurve).GetPlanarState() == PlanarState.Planar || (this as ICurve).GetPlanarState() == PlanarState.UnderDetermined)
{ // in Wirklichkeit ein 2d spline, nur im Raum gelegen
if ((this as ICurve).GetPlanarState() == PlanarState.UnderDetermined)
{
GeoVector nrm = poles[poles.Length - 1] - poles[0];
if (nrm.IsNullVector()) nrm = poles[poles.Length - 2] - poles[0];
Plane tmp = new Plane(poles[0], nrm);
this.plane = new Plane(tmp.Location, tmp.DirectionX, tmp.Normal);
}
if (weights == null)
{
GeoPoint2D[] npoles;
if (periodic)
{
npoles = new GeoPoint2D[poles.Length + degree];
for (int i = 0; i < poles.Length; ++i)
{
npoles[i] = plane.Value.Project(poles[i]);
}
for (int i = 0; i < degree; ++i)
{
npoles[poles.Length + i] = plane.Value.Project(poles[i]);
}
}
else
{
npoles = new GeoPoint2D[poles.Length];
for (int i = 0; i < poles.Length; ++i)
{
npoles[i] = plane.Value.Project(poles[i]);
}
}
this.nubs2d = new Nurbs<GeoPoint2D, GeoPoint2DPole>(degree, npoles, knotslist.ToArray());
nubs2d.InitDeriv1();
}
else
{
GeoPoint2DH[] npoles;
if (plane == null) plane = getPlane();
if (periodic && poles.Length > 2)
{
npoles = new GeoPoint2DH[poles.Length + degree];
for (int i = 0; i < poles.Length; ++i)
{
npoles[i] = new GeoPoint2DH(plane.Value.Project(poles[i]), weights[i]);
}
for (int i = 0; i < degree; ++i)
{
npoles[poles.Length + i] = new GeoPoint2DH(plane.Value.Project(poles[i]), weights[i]);
}
}
else
{
npoles = new GeoPoint2DH[poles.Length];
for (int i = 0; i < poles.Length; ++i)
{
npoles[i] = new GeoPoint2DH(plane.Value.Project(poles[i]), weights[i]);
}
}
this.nurbs2d = new Nurbs<GeoPoint2DH, GeoPoint2DHPole>(degree, npoles, knotslist.ToArray());
//int dbg = nurbs2d.CurveKnotIns(knotslist[0], degree, out double[] newkn, out GeoPoint2DH[] newpo);
nurbs2d.InitDeriv1();
}
}
else
{ // echte 3d Kurve
if (weights == null)
{
GeoPoint[] npoles;
if (periodic && poles.Length > 2)
{
npoles = new GeoPoint[poles.Length + 2 * degree - 2];
for (int i = 0; i < poles.Length; ++i)
{
npoles[i] = poles[i];
}
for (int i = 0; i < 2 * degree - 2; ++i)
{
npoles[poles.Length + i] = poles[i];
}
}
else
{
npoles = new GeoPoint[poles.Length];
for (int i = 0; i < poles.Length; ++i)
{
npoles[i] = poles[i];
}
}
this.nubs3d = new Nurbs<GeoPoint, GeoPointPole>(degree, npoles, knotslist.ToArray());
nubs3d.InitDeriv1();
}
else
{
GeoPointH[] npoles;
if (periodic && poles.Length > 2)
{
npoles = new GeoPointH[poles.Length + degree];
for (int i = 0; i < poles.Length; ++i)
{
npoles[i] = new GeoPointH(poles[i], weights[i]);
}
for (int i = 0; i < degree; ++i)
{
npoles[poles.Length + i] = new GeoPointH(poles[i], weights[i]);
}
// war vorher so, gab aber mitunter falsch Anzahl von Poles zu knots
//npoles = new GeoPointH[poles.Length + 2 * degree - 2];
//for (int i = 0; i < poles.Length; ++i)
//{
// npoles[i] = new GeoPointH(poles[i], weights[i]);
//}
//for (int i = 0; i < 2 * degree - 2; ++i)
//{
// npoles[poles.Length + i] = new GeoPointH(poles[i], weights[i]);
//}
}
else
{
npoles = new GeoPointH[poles.Length];
for (int i = 0; i < poles.Length; ++i)
{
npoles[i] = new GeoPointH(poles[i], weights[i]);
}
}
this.nurbs3d = new Nurbs<GeoPointH, GeoPointHPole>(degree, npoles, knotslist.ToArray());
nurbs3d.InitDeriv1();
}
}
nurbsHelper = true;
}
}
private void MakeInterpol()
{ // die Interpolation geht mindestens durch die Knotenpunkte
// die Abweichung von der Kurve wird erstmal auf ein Verhältnis zur Gesamtgröße festgelegt
BoundingCube ext = new BoundingCube(poles);
double maxError = Math.Max(ext.Size / 100.0, Precision.eps * 100); // testweise 1/100 der gesamten Ausdehnung
// Ein Problem bleibt bestehen. Der Fehlertest in der Mitte des Segments ist unsicher, denn bei einer
// Art Wendepunkt kann das Segment die Sehne schneiden und so fälschlicherweise einen zu kleinen
// Fehler melden. Man müsste auch noch die mittlere Richtung betrachten, aber mit welcher Abweichung?
List<GeoPoint> pointList = new List<GeoPoint>();
List<GeoVector> dirList = new List<GeoVector>();
List<double> paramList = new List<double>();
GeoPoint sp;
GeoVector sd;
PointDirAtParam(startParam, out sp, out sd);
pointList.Add(sp);
dirList.Add(sd);
paramList.Add(startParam);
for (int i = 0; i < knots.Length - 1; ++i)
{
InsertInterpol(knots[i], knots[i + 1], pointList, dirList, paramList, maxError);
}
interpol = pointList.ToArray();
interdir = dirList.ToArray();
interparam = paramList.ToArray();
}
private void InsertInterpol(double sp, double ep, List<GeoPoint> pointList, List<GeoVector> dirList, List<double> paramList, double maxError)
{
// Der Punkt am Anfang ist schon drin
GeoPoint point;
GeoVector dir;
PointDirAtParam(ep, out point, out dir);
// jetzt überprüfen, ob der Fehler klein genug
Plane pln;
GeoVector dirs = dirList[dirList.Count - 1];
GeoPoint spoint = pointList[pointList.Count - 1];
if (plane.HasValue) pln = plane.Value;
else
{ // die durch die beiden Vektoren aufgespannte Ebene
if (Precision.SameDirection(dirs, dir, false))
{ // vermutlich eine Linie
if (Precision.SameDirection(dirs, GeoVector.ZAxis, false))
{
pln = new Plane(GeoPoint.Origin, dirs, GeoVector.XAxis);
}
else
{
pln = new Plane(GeoPoint.Origin, dirs, GeoVector.ZAxis);
}
}
else
{
try
{
pln = new Plane(GeoPoint.Origin, dirs, dir);
}
catch (PlaneException)
{
try
{
if (Precision.SameDirection(dirs, GeoVector.ZAxis, false))
{
pln = new Plane(GeoPoint.Origin, dirs, GeoVector.XAxis);
}
else
{
pln = new Plane(GeoPoint.Origin, dirs, GeoVector.ZAxis);
}
}
catch (PlaneException)
{
pln = Plane.XYPlane;
}
}
}
}
// jetzt in der Ebene testen
GeoPoint mpoint;
GeoVector mdir;
PointDirAtParam((sp + ep) / 2.0, out mpoint, out mdir);
double merror = Geometry.DistPL(mpoint, spoint, point);
if (merror > maxError)
{
InsertInterpol(sp, (sp + ep) / 2.0, pointList, dirList, paramList, maxError);
InsertInterpol((sp + ep) / 2.0, ep, pointList, dirList, paramList, maxError);
}
else
{
GeoVector2D dirs2d = pln.Project(dirs);
GeoVector2D dirm2d = pln.Project(mdir);
GeoVector2D dire2d = pln.Project(dir);
// wenn dirm in dem von dirs und dire aufgespannten Bereich liegt, dann soll es gut sein
// liese sich auch durch ein 2x2 lineares system lösen
//SweepAngle sa = new SweepAngle(dirs2d, dire2d);
//SweepAngle sm = new SweepAngle(dirs2d, dirm2d);
//bool ok = false;
//if (sa >= 0 && sm >= 0) ok = sm <= sa;
//if (sa < 0 && sm < 0) ok = sm >= sa;
// Die Frage ist, wie groß ist der Fehler. Man kann nicht einfach in der Mitte testen,
// da der Verlauf der Kurve nicht bekannt ist, insbesondere ein Wendepunkt kann zu Fehlern
// führen. So testen wir hier jeweils noch die 1/4 und 3/4 Punkte um einigerm´ßen sicher
// zu gehen
GeoPoint tmppoint = PointAtParam(sp + (ep - sp) * 0.25);
double error1 = Geometry.DistPL(tmppoint, spoint, point);
tmppoint = PointAtParam(sp + (ep - sp) * 0.75);
double error2 = Geometry.DistPL(tmppoint, spoint, point);
// bool ok = error1 <= merror && error2 <= merror; // führt zu endlosrekursion
bool ok = error1 <= maxError && error2 <= maxError;
if (ok)
{
pointList.Add(point);
dirList.Add(dir);
paramList.Add(ep);
}
else
{
InsertInterpol(sp, (sp + ep) / 2.0, pointList, dirList, paramList, maxError);
InsertInterpol((sp + ep) / 2.0, ep, pointList, dirList, paramList, maxError);
}
}
}
// private CndOCas.Edge oCasBuddy; in GeneralCurve implementiert.
// Wenn alles von GeneralCurve implementiert ist, dann kann man diese Klasse
// auch überspringen und IGeoObjectImpl als Basis nehmen. Hier also erstmal
// die quick-and-dirty Implementierung.
#region polymorph construction
public delegate BSpline ConstructionDelegate();
public static ConstructionDelegate Constructor;
public static BSpline Construct()
{
if (Constructor != null) return Constructor();
return new BSpline();
}
public delegate void ConstructedDelegate(BSpline justConstructed);
public static event ConstructedDelegate Constructed;
#endregion
protected BSpline()
: base()
{
lockApproximationRecalc = new object();
if (Constructed != null) Constructed(this);
extent = BoundingCube.EmptyBoundingCube;
}
private void InvalidateSecondaryData()
{
lock (this)
{
nurbsHelper = false;
nubs3d = null;
nurbs3d = null;
nubs2d = null;
nurbs2d = null;
plane = null;
interpol = null;
interdir = null;
interparam = null;
approximation = null;
maxInterpolError = 0.0;
extent = BoundingCube.EmptyBoundingCube;
tetraederHull = null;
extrema = null;
}
}
private TetraederHull TetraederHull
{ // alle Kurven sollte zusätzlich ein Interface implementieren ITetraederHull, die genau diese Property überschreibt
// dann kann man immer ungeachtet der Kurvenart auf die Hülle zugreifen
get
{
if (tetraederHull == null)
{
tetraederHull = new TetraederHull(this);
}
return tetraederHull;
}
}
#if DEBUG
#endif
#region Methoden zum Initialisieren/Setzen
new private class Changing : IGeoObjectImpl.Changing
{
private BSpline bSpline;
public Changing(BSpline bSpline, bool keepNurbs)
: base(bSpline, "CopyGeometry", bSpline.Clone())
{
bSpline.nurbsHelper = false;
bSpline.plane = null;
bSpline.interpol = null;
bSpline.interdir = null;
bSpline.interparam = null;
bSpline.maxInterpolError = 0.0;
if (!keepNurbs)
{
bSpline.nubs3d = null;
bSpline.nurbs3d = null;
bSpline.nubs2d = null;
bSpline.nurbs2d = null;
}
this.bSpline = bSpline;
}
public Changing(BSpline bSpline)
: base(bSpline, "CopyGeometry", bSpline.Clone())
{
bSpline.InvalidateSecondaryData();
this.bSpline = bSpline;
}
public Changing(BSpline bSpline, string PropertyName)
: base(bSpline, PropertyName)
{
bSpline.InvalidateSecondaryData();
this.bSpline = bSpline;
}
public Changing(BSpline bSpline, string MethodOrPropertyName, params object[] Parameters)
: base(bSpline, MethodOrPropertyName, Parameters)
{
bSpline.InvalidateSecondaryData();
this.bSpline = bSpline;
}
public Changing(BSpline bSpline, Type interfaceForMethod, string MethodOrPropertyName, params object[] Parameters)
: base(bSpline, interfaceForMethod, MethodOrPropertyName, Parameters)
{
bSpline.InvalidateSecondaryData();
this.bSpline = bSpline;
}
public override void Dispose()
{
base.Dispose();
#if DEBUG
if (bSpline.knots.Length == 2 && bSpline.knots[0] == bSpline.knots[1])
{
}
#endif
}
}
/// <summary>
/// Makes this BSpline go through the given points. Previous data of this BSpline
/// (if any) is discarded. The BSpline remembers both this points and the calculated
/// poles, multiplicities, knots and weights values. If all points lie in a single plane
/// it is better to use the appropriate ThroughPoints method.
/// </summary>
/// <param name="points">List of points to pass through</param>
/// <param name="maxDegree">maximum degree for the BSpline. Must be between 3 an 25</param>
/// <param name="closed">true if the resulting BSpline should be closed</param>
/// <returns>success</returns>
public bool ThroughPoints(GeoPoint[] points, int maxDegree, bool closed)
{
try
{
if (points.Length < 2) return false;
if (points.Length == 2 && (points[0] | points[1]) < Precision.eps) return false;
maxDegree = Math.Min(maxDegree, points.Length - 1); // bei 2 Punkten nur 1. Grad, also Linie, u.s.w
Nurbs<GeoPoint, GeoPointPole> tmp = new Nurbs<GeoPoint, GeoPointPole>(maxDegree, points, (points.Length > 2) && closed, out throughPointsParam, true); // Hennings neue Methode
// kann in der Zeile vorher rausfliegen, also nubs3d dort noch nicht überschreiben
using (new Changing(this))
{
nubs3d = tmp;
throughPoints3d = points;
poles = nubs3d.Poles;
double[] flatknots = nubs3d.UKnots;
List<double> hknots = new List<double>();
List<int> hmult = new List<int>();
for (int i = 0; i < flatknots.Length; ++i)
{
if (i == 0)
{
hknots.Add(flatknots[i]);
hmult.Add(1);
}
else
{
if (flatknots[i] == hknots[hknots.Count - 1])
{
++hmult[hmult.Count - 1];
}
else
{
hknots.Add(flatknots[i]);
hmult.Add(1);
}
}
}
// weights wieder rausschmeißen, aber z.Z. gibt es noch Probleme, wenn null
weights = new double[poles.Length];
for (int i = 0; i < weights.Length; ++i)
{
weights[i] = 1.0;
}
knots = hknots.ToArray();
multiplicities = hmult.ToArray();
this.degree = maxDegree;
this.periodic = closed;
this.startParam = knots[0];
this.endParam = knots[knots.Length - 1];
nubs3d.InitDeriv1();
//// DEBUG: wo sind die durchgangspunkte?
//for (int i = 0; i < throughPointsParam.Length; ++i)
//{
// GeoPoint p = PointAtParam(throughPointsParam[i]);
//}
//// END DEBUG
return true;
}
}
catch (NurbsException)
{
return false;
}
}
// public bool ThroughPoints(Plane p, GeoPoint2D [] points, int maxDegree, bool closed)
// {
// double [] pointarray = new double[points.Length*2];
// for (int i=0; i<points.Length; ++i)
// {
// pointarray[2*i] = points[i].x;
// pointarray[2*i+1] = points[i].y;
// }
// try
// {
// CndOCas.GeomCurve2DClass curve2d = new CndOCas.GeomCurve2DClass();
// curve2d.MakeBSpline(points.Length,ref pointarray[0],8);
// CndOCas.Edge edg = curve2d.MakeEdge(p.OCasBuddy);
// bSplineEdge = edg.GetBSplineEdge();
// int np = bSplineEdge.NumPoles;
// int nk = bSplineEdge.NumKnots;
// // hier angekommen kann man davon ausgehen, dass nichts mehr schief geht
// // die Erzeugungsdaten behalten, obwohl sie ja nicht von Bedeutung für die
// // Darstellung sind.
// GeoObjectChangeEvent ce = MakeChangeAllEvent();
// projectionData.Clear();
// FireWillChange(ce);
// plane = p;
// throughPoints2d = points;
// throughPoints3d = null;
//
// gp.Pnt [] tpoles = new gp.Pnt[np];
// bSplineEdge.GetPoles(ref tpoles[0]);
// poles = new GeoPoint [np];
// for (int i=0; i<np; ++i) poles[i] = new GeoPoint(tpoles[i]);
// weights = new double[np];
// bSplineEdge.GetWeights(ref weights[0]);
//
// knots = new double [nk];
// bSplineEdge.GetKnots(ref knots[0]);
// multiplicities = new int [nk];
// bSplineEdge.GetMultiplicities(ref multiplicities[0]);
// degree = bSplineEdge.Degree;
// periodic = bSplineEdge.IsPeriodic!=0;
// startParam = bSplineEdge.StartParameter;
// endParam = bSplineEdge.EndParameter;
// SetOcasBuddy(edg.GetGeneralEdge());
// FireDidChange(ce);
// return true;
// }
// catch (OpenCascade.Exception)
// {
// return false;
// }
// }
public bool ThroughPoints(GeoPoint[] points, GeoVector[] directions, int maxDegree, bool closed)
{
try
{
if (points.Length < 2) return false;
maxDegree = Math.Min(maxDegree, 2 * points.Length - 1); // bei 2 Punkten nur 1. Grad, also Linie, u.s.w
GeoPoint[] dirs = new GeoPoint[directions.Length]; // wir brauchen Punkte statt vektoren, weil Nurbs keine Vektoren kennt
for (int i = 0; i < dirs.Length; ++i)
{
dirs[i] = GeoPoint.Origin + directions[i];
}
Nurbs<GeoPoint, GeoPointPole> tmp = new Nurbs<GeoPoint, GeoPointPole>(maxDegree, points, dirs, closed);
// kann in der Zeile vorher rausfliegen, also nubs3d dort noch nicht überschreiben
using (new Changing(this))
{
nubs3d = tmp;
throughPoints3d = points;
poles = nubs3d.Poles;
double[] flatknots = nubs3d.UKnots;
List<double> hknots = new List<double>();
List<int> hmult = new List<int>();
for (int i = 0; i < flatknots.Length; ++i)
{
if (i == 0)
{
hknots.Add(flatknots[i]);
hmult.Add(1);
}
else
{
if (flatknots[i] == hknots[hknots.Count - 1])
{
++hmult[hmult.Count - 1];
}
else
{
hknots.Add(flatknots[i]);
hmult.Add(1);
}
}
}
// weights wieder rausschmeißen, aber z.Z. gibt es noch Probleme, wenn null
weights = new double[poles.Length];
for (int i = 0; i < weights.Length; ++i)
{
weights[i] = 1.0;
}
knots = hknots.ToArray();
multiplicities = hmult.ToArray();
this.degree = maxDegree;
this.periodic = closed;
this.startParam = knots[0];
this.endParam = knots[knots.Length - 1];
nubs3d.InitDeriv1();
return true;
}
//if (points.Length < 2) return false;
//CndHlp3D.GeoPoint3D[] hlppoints = new CndHlp3D.GeoPoint3D[points.Length];
//for (int i = 0; i < points.Length; ++i)
//{
// hlppoints[i] = (CndHlp3D.GeoPoint3D)points[i].ToCndHlp();
//}
//CndHlp3D.GeoVector3D[] hlpdirs = new CndHlp3D.GeoVector3D[directions.Length];
//for (int i = 0; i < directions.Length; ++i)
//{
// hlpdirs[i] = (CndHlp3D.GeoVector3D)directions[i].ToCndHlp();
//}
//// doppelte Punkte sollen möglich sein, die machen ggf in dem Spline einen Knick
//using (new Changing(this))
//{
// CndHlp3D.BSpline3D hlp3d = new CndHlp3D.BSpline3D(hlppoints, hlpdirs, maxDegree, closed);
// (this as ICndHlp3DEdge).Edge = hlp3d;
// throughPoints3d = points;
// this.maxDegree = maxDegree;
// this.periodic = closed;
// if (poles == null) return false;
// // double MaxDist;
// // Plane pl = Plane.FromPoints(poles,out MaxDist);
// // if (MaxDist<Precision.eps) plane = pl;
// return true;
//}
}
catch (Exception)
{
return false;
}
}
public bool ThroughPoints(Plane p, GeoPoint2D[] points, GeoVector2D[] directions, int maxDegree, bool closed)
{
return false;
}
// not a good implementaion:
//public BSpline Extend(double atStart=0.1, double atEnd=0.1)
//{
// BSpline res = BSpline.Construct();
// this.PointAtParam(-0.1);
// List<GeoPoint> newPoles = new List<GeoPoint>(Poles);
// List<double> newKnots = new List<double>(Knots);
// List<double> newWeights = null;
// if (weights!=null) newWeights = new List<double>(weights);
// List<int> newMultiplicities = new List<int>(Multiplicities);
// if (atStart > 0.0)
// {
// GeoPoint spole = (this as ICurve).StartPoint - atStart * (this as ICurve).StartDirection;
// newPoles.Insert(0, spole);
// newKnots.Insert(0, -atStart);
// newMultiplicities.Insert(0, 1);
// //newMultiplicities[1] = 1;
// if (newWeights != null) newWeights.Insert(0, 1);
// }
// if (atEnd > 0.0)
// {
// GeoPoint epole = (this as ICurve).EndPoint + atEnd * (this as ICurve).EndDirection;
// newPoles.Add(epole);
// newKnots.Add(1.0+atEnd);
// newMultiplicities.Add(1);
// //newMultiplicities[newMultiplicities.Count - 2] = 1;
// if (newWeights != null) newWeights.Add(1);
// }
// res.SetData(degree, newPoles.ToArray(), newWeights != null ? newWeights.ToArray() : null, newKnots.ToArray(), newMultiplicities.ToArray(), false);
// res.TrimParam(0.001,0.999);
// return res;
//}
/// <summary>
/// Modifies the value of a pole. The Index must be between 0 and PoleCount.
/// </summary>
/// <param name="Index">Index of the pole</param>
/// <param name="ThePoint">The new value</param>
public void SetPole(int Index, GeoPoint ThePoint)
{
using (new Changing(this, "SetPole", Index, poles[Index]))
{
poles[Index] = ThePoint;
// RecalcOcasBuddies();
throughPoints3d = null; // Durchgangspunkte werden ungültig!
}
}
public GeoPoint GetPole(int Index)
{
return poles[Index];
}
public int PoleCount
{
get { return poles.Length; }
}
public GeoPoint[] Poles
{
get
{
return poles;
}
}
public double[] Knots
{
get
{
return knots;
}
}
public int[] Multiplicities
{
get
{
return multiplicities;
}
}
public double[] Weights
{
get
{
return weights;
}
}
public int Degree { get => degree; }
public void GetData(out int degree, out GeoPoint[] poles, out double[] weights, out double[] knots, out int[] multiplicities)
{
degree = this.degree;
poles = (GeoPoint[])this.poles.Clone();
weights = (double[])this.weights.Clone();
knots = (double[])this.knots.Clone();
multiplicities = (int[])this.multiplicities.Clone();
}
public bool ThroughPoints3dExist
{
get { return throughPoints3d != null; }
}
public bool SetData(int degree, GeoPoint[] poles, double[] weights, double[] knots, int[] multiplicities, bool periodic)
{
return SetData(degree, poles, weights, knots, multiplicities, periodic, knots[0], knots[knots.Length - 1]);
}
public bool SetData(int degree, GeoPoint[] poles, double[] weights, double[] knots, int[] multiplicities, bool periodic, double startParam, double endParam)
{
for (int i = 1; i < knots.Length; ++i)
{
if (knots[i] < knots[i - 1]) throw new ApplicationException("wrong order of knots");
}
InvalidateSecondaryData();
// Bedingungen
//- Degree is in the range 1 to Geom_BSplineCurve::MaxDegree(),
//- the Poles and Weights arrays have the same dimension and this dimension is greater than or equal to 2,
//- the Knots and Multiplicities arrays have the same dimension and this dimension is greater than or equal to 2,
//- the knots sequence is in ascending order, i.e. Knots(i) is less than Knots(i+1),
//- the multiplicity coefficients are in the range 1 to Degree. However, on a non-periodic curve, the first and last multiplicities
// may be Degree + 1 (this is recommended if you want the curve to start and finish on the first and last poles),
//- on a periodic curve the first and last multiplicities must be the same,
//- on a non-periodic curve, the number of poles is equal to the sum of the multiplicity coefficients, minus Degree, minus 1,
//- on a periodic curve, the number of poles is equal to the sum of knot multiplicities, excluding the last knot.
if (weights == null || weights.Length != poles.Length)
{
weights = new double[poles.Length];
for (int i = 0; i < poles.Length; ++i)
{
weights[i] = 1.0;
}
}
if (multiplicities == null || multiplicities.Length != knots.Length)
{
List<double> knlist = new List<double>();
List<int> mulist = new List<int>();
for (int i = 0; i < knots.Length; ++i)
{
if (i == 0 || knots[i] != knots[i - 1])
{
knlist.Add(knots[i]);
mulist.Add(1);
}
else
{
mulist[mulist.Count - 1] += 1;
}
}
// siehe weiter unten "if (multiplicities.Length>1)"
multiplicities = mulist.ToArray();
knots = knlist.ToArray();
// leider kommen in DWG periodische mit multiplicities[0] > degree vor. Die sind in OCas nicht erlaubt
if (multiplicities[0] > degree) periodic = false;
}
if (periodic)
{
#if DEBUG
//List<double> fknot = new List<double>();
//for (int i = 0; i < knots.Length; i++)
//{
// for (int j = 0; j < multiplicities[i]; j++)
// {
// fknot.Add(knots[i]);
// }
//}
//Nurbs<GeoPoint, GeoPointPole> nbs = new Nurbs<GeoPoint, GeoPointPole>(true, degree, poles, fknot.ToArray());
//GeoPoint dbg = nbs.CurvePoint(1.0);
//FromNurbs(nbs, startParam, endParam);
//return true;
#endif
}
else
{
// es kommen manchmal zu kurze knotenvektoern vor. hier wird korrigiert
int sum = 0;
for (int i = 0; i < multiplicities.Length; ++i) sum += multiplicities[i];
if (degree > poles.Length) degree = poles.Length;
int diff = poles.Length + degree + 1 - sum;
if (diff != 0)
{
int d1 = diff / 2;
int d2 = diff - d1;
multiplicities[0] += d2;
multiplicities[multiplicities.Length - 1] += d1;
}
}
if (startParam < knots[0]) startParam = knots[0];
if (endParam > knots[knots.Length - 1]) endParam = knots[knots.Length - 1];