-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProceduralShape.cs
1254 lines (1142 loc) · 56.7 KB
/
ProceduralShape.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
/*
-----------------------------------------------------------------------------
This source file is part of mogre-procedural
For the latest info, see http://code.google.com/p/mogre-procedural/
my blog:http://hi.baidu.com/rainssoft
this is overwrite ogre-procedural c++ project using c#, look ogre-procedural c++ source http://code.google.com/p/ogre-procedural/
Copyright (c) 2013-2020 rains soft
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.
-----------------------------------------------------------------------------
*/
//#ifndef PROCEDURAL_SHAPE_INCLUDED
#define PROCEDURAL_SHAPE_INCLUDED
namespace Mogre_Procedural
{
using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
using Math = Mogre.Math;
using Mogre_Procedural.std;
public enum Side : int
{
SIDE_LEFT,
SIDE_RIGHT
}
//* @}
//*
// * \ingroup shapegrp
// * Describes a succession of interconnected 2D points.
// * It can be closed or not, and there's always an outside and an inside
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport Shape
public class Shape
{
public class IntersectionInShape
{
public uint[] index = new uint[2];
public bool[] onVertex = new bool[2];
public Vector2 position = new Vector2();
public IntersectionInShape(uint i, uint j, Vector2 intersect) {
position = intersect;//new Vector2(intersect);
index[0] = i;
index[1] = j;
onVertex[0] = false;
onVertex[1] = false;
}
}
public enum BooleanOperationType : int
{
BOT_UNION,
BOT_INTERSECTION,
BOT_DIFFERENCE
}
private std_vector<Vector2> mPoints = new std_vector<Vector2>();
private bool mClosed;
private Side mOutSide;
/// Default constructor
public Shape() {
mClosed = false;
mOutSide = Side.SIDE_RIGHT;
}
/// Adds a point to the shape
public Shape addPoint(Vector2 pt) {
mPoints.push_back(pt);
return this;
}
/// Adds a point to the shape
public Shape addPoint(float x, float y) {
mPoints.push_back(new Vector2(x, y));
return this;
}
/// Inserts a point to the shape
/// @param index the index before the inserted point
/// @param x new point's x coordinate
/// @param y new point's y coordinate
public Shape insertPoint(int index, float x, float y) {
//
//mPoints.Insert(mPoints.GetEnumerator()+index, Vector2(x, y));
mPoints.insert(index, new Vector2(x, y));
return this;
}
/// Inserts a point to the shape
/// @param index the index before the inserted point
/// @param pt new point's position
public Shape insertPoint(int index, Vector2 pt) {
//
//mPoints.insert(mPoints.GetEnumerator()+index, pt);
mPoints.insert(index, pt);
return this;
}
/// Adds a point to the shape, relative to the last point added
public Shape addPointRel(Vector2 pt) {
if (mPoints.Count == 0) {
mPoints.push_back(pt);
}
else {
Vector2 end = mPoints[mPoints.Count - 1];
mPoints.push_back(pt + end);
}
return this;
}
/// Adds a point to the shape, relative to the last point added
public Shape addPointRel(float x, float y) {
if (mPoints.Count == 0)
mPoints.push_back(new Vector2(x, y));
else {
Vector2 end = mPoints[mPoints.Count - 1];
mPoints.push_back(new Vector2(x, y) + end);
}
return this;
}
public Shape addPointRel(std_vector<Vector2> pointList) {
if (mPoints.Count == 0) {
mPoints.AddRange(pointList.ToArray());
}
else {
Vector2 refVector = mPoints[mPoints.size() - 1];
foreach (var it in pointList) {
addPoint(it + refVector);
}
}
return this;
}
/// Appends another shape at the end of this one
public Shape appendShape(Shape other) {
//
//mPoints.insert(mPoints.end(), STLAllocator<U, AllocPolicy>.mPoints.GetEnumerator(), STLAllocator<U, AllocPolicy>.mPoints.end());
mPoints.AddRange(other.mPoints.ToArray());
return this;
}
/// Appends another shape at the end of this one, relative to the last point of this shape
public Shape appendShapeRel(Shape other) {
return addPointRel(other.mPoints);
//if (mPoints.Count == 0)
// appendShape(other);
//else {
// Vector2 refVector = mPoints[mPoints.Count - 1];// *(mPoints.end()-1);
// List<Vector2> pointList = other.mPoints;//new List<Vector2>(STLAllocator<U, AllocPolicy>.mPoints.GetEnumerator(), STLAllocator<U, AllocPolicy>.mPoints.end());
// // for (List<Vector2>.Enumerator it = pointList.GetEnumerator(); it.MoveNext(); ++it)
// // it.Current +=refVector;
// ////
// // mPoints.insert(mPoints.end(), pointList.GetEnumerator(), pointList.end());
// foreach (var it in pointList) {
// addPoint(it + refVector);
// }
//}
//return this;
}
/// Extracts a part of the shape as a new shape
/// @param first first index to be in the new shape
/// @param last last index to be in the new shape
public Shape extractSubShape(uint first, uint last) {
Shape s = new Shape();
for (int i = (int)first; i <= last; i++)
s.addPoint(mPoints[i]);
s.setOutSide(mOutSide);
if (mClosed)
s.close();
return s;
}
/// Reverses direction of the shape
/// The outside is preserved
public Shape reverse() {
//std.reverse(mPoints.GetEnumerator(), mPoints.end());
mPoints.Reverse();
switchSide();
return this;
}
///<summary>
///Clears the content of the shape
///</summary>
public Shape reset() {
mPoints.Clear();
return this;
}
/// Converts the shape to a path, with Y=0
//-----------------------------------------------------------------------
//
//ORIGINAL LINE: Path convertToPath() const
public Path convertToPath() {
Path p = new Path();
//for (List<Vector2>.Enumerator it = mPoints.GetEnumerator(); it.MoveNext(); ++it)
foreach (var it in mPoints) {
p.addPoint(it.x, 0, it.y);
}
if (mClosed)
p.close();
return p;
}
/// Outputs a track, with Key=X and Value=Y
//-----------------------------------------------------------------------
public Track convertToTrack() {
return convertToTrack(Track.AddressingMode.AM_RELATIVE_LINEIC);
}
//
//ORIGINAL LINE: Track convertToTrack(Track::AddressingMode addressingMode =Track::AM_RELATIVE_LINEIC) const
//
public Track convertToTrack(Track.AddressingMode addressingMode) {
Track t = new Track(addressingMode);
//for (List<Vector2>.Enumerator it = mPoints.GetEnumerator(); it.MoveNext(); ++it)
foreach (var it in mPoints) {
t.addKeyFrame(it.x, it.y);
}
return t;
}
/// Gets a copy of raw vector data of this shape
//
//ORIGINAL LINE: inline List<Ogre::Vector2> getPoints() const
public Vector2[] getPoints() {
return mPoints.ToArray();
}
/// Gets raw vector data of this shape as a non-const reference
//public List<Vector2> getPointsReference()
//{
// return mPoints;
//}
public std_vector<Vector2> _getPoints() {
return mPoints;
}
/// Gets raw vector data of this shape as a non-const reference
//
//ORIGINAL LINE: inline const List<Ogre::Vector2>& getPointsReference() const
public std_vector<Vector2> getPointsReference() {
return mPoints;
}
// *
// * Bounds-safe method to get a point : it will allow you to go beyond the bounds
//
//
//ORIGINAL LINE: inline const Ogre::Vector2& getPoint(int i) const
public Vector2 getPoint(int i) {
return mPoints[getBoundedIndex(i)];
}
//
//ORIGINAL LINE: inline uint getBoundedIndex(int i) const
public int getBoundedIndex(int i) {
if (mClosed)
return Utils.modulo(i, mPoints.size());
return Utils.cap(i, 0, mPoints.size() - 1);
}
/// Gets number of points in current point list
//
//ORIGINAL LINE: inline const List<Ogre::Vector2>::size_type getPointCount() const
public int getPointCount() {
return mPoints.size();
}
// *
// * Makes the shape a closed shape, ie it will automatically connect
// * the last point to the first point.
//
public Shape close() {
mClosed = true;
return this;
}
// *
// * Sets which side (left or right) is on the outside of the shape.
// * It is used for such things as normal generation
// * Default is right, which corresponds to placing points anti-clockwise.
//
public Shape setOutSide(Side side) {
mOutSide = side;
return this;
}
/// Gets which side is out
//
//ORIGINAL LINE: inline Side getOutSide() const
public Side getOutSide() {
return mOutSide;
}
/// Switches the inside and the outside
public Shape switchSide() {
mOutSide = (mOutSide == Side.SIDE_LEFT) ? Side.SIDE_RIGHT : Side.SIDE_LEFT;
return this;
}
/// Gets the number of segments in that shape
//
//ORIGINAL LINE: inline int getSegCount() const
public int getSegCount() {
return (mPoints.size() - 1) + (mClosed ? 1 : 0);
}
/// Gets whether the shape is closed or not
//
//ORIGINAL LINE: inline bool isClosed() const
public bool isClosed() {
return mClosed;
}
// *
// * Returns local direction after the current point
//
//
//ORIGINAL LINE: inline Ogre::Vector2 getDirectionAfter(uint i) const
public Vector2 getDirectionAfter(uint index) {
int i = (int)index;
// If the path isn't closed, we get a different calculation at the end, because
// the tangent shall not be null
if (!mClosed && i == mPoints.Count - 1 && i > 0)
return (mPoints[i] - mPoints[i - 1]).NormalisedCopy;
else
return (getPoint(i + 1) - getPoint(i)).NormalisedCopy;
}
// *
// * Returns local direction after the current point
//
//
//ORIGINAL LINE: inline Ogre::Vector2 getDirectionBefore(uint i) const
public Vector2 getDirectionBefore(uint index) {
int i = (int)index;
// If the path isn't closed, we get a different calculation at the end, because
// the tangent shall not be null
if (!mClosed && i == 1)
return (mPoints[1] - mPoints[0]).NormalisedCopy;
else
return (getPoint(i) - getPoint(i - 1)).NormalisedCopy;
}
/// Gets the average between before direction and after direction
//
//ORIGINAL LINE: inline Ogre::Vector2 getAvgDirection(uint i) const
public Vector2 getAvgDirection(uint i) {
return (getDirectionAfter(i) + getDirectionBefore(i)).NormalisedCopy;
}
/// Gets the shape normal just after that point
//
//ORIGINAL LINE: inline Ogre::Vector2 getNormalAfter(uint i) const
public Vector2 getNormalAfter(uint i) {
if (mOutSide == Side.SIDE_RIGHT)
return -getDirectionAfter(i).Perpendicular;
return getDirectionAfter(i).Perpendicular;
}
/// Gets the shape normal just before that point
//
//ORIGINAL LINE: inline Ogre::Vector2 getNormalBefore(uint i) const
public Vector2 getNormalBefore(uint i) {
if (mOutSide == Side.SIDE_RIGHT)
return -getDirectionBefore(i).Perpendicular;
return getDirectionBefore(i).Perpendicular;
}
/// Gets the "normal" of that point ie an average between before and after normals
//
//ORIGINAL LINE: inline Ogre::Vector2 getAvgNormal(uint i) const
public Vector2 getAvgNormal(uint i) {
if (mOutSide == Side.SIDE_RIGHT)
return -getAvgDirection(i).Perpendicular;
return getAvgDirection(i).Perpendicular;
}
// *
// * Outputs a mesh representing the shape.
// * Mostly for debugging purposes
//
//-----------------------------------------------------------------------
public MeshPtr realizeMesh() {
return realizeMesh("");
}
//
//ORIGINAL LINE: MeshPtr realizeMesh(const string& name ="") const
//
public MeshPtr realizeMesh(string name) {
if (string.IsNullOrEmpty(name)) {
name = Guid.NewGuid().ToString("N");
}
SceneManagerEnumerator.SceneManagerIterator item = Root.Singleton.GetSceneManagerIterator();
item.MoveNext();
Mogre.SceneManager smgr = item.Current;
item.Dispose();
ManualObject manual = smgr.CreateManualObject(name);
manual.Begin("BaseWhiteNoLighting", RenderOperation.OperationTypes.OT_LINE_STRIP);
_appendToManualObject(manual);
manual.End();
MeshPtr mesh = null;//new MeshPtr();
if (name == "")
mesh = manual.ConvertToMesh(Utils.getName("Procedural_Shape_"));
else
mesh = manual.ConvertToMesh(name);
smgr.DestroyManualObject(manual);
return mesh;
}
// *
// * Appends the shape vertices to a manual object being edited
//
//-----------------------------------------------------------------------
//
//ORIGINAL LINE: void _appendToManualObject(ManualObject* manual) const
public void _appendToManualObject(ManualObject manual) {
//for (List<Vector2>.Enumerator itPos = mPoints.GetEnumerator(); itPos.MoveNext(); itPos++)
foreach (var itPos in mPoints) {
manual.Position(new Vector3(itPos.x, itPos.y, 0.0f));
}
if (mClosed) {
//manual.Position(new Vector3(mPoints.GetEnumerator().x, mPoints.GetEnumerator().y, 0.0f));
manual.Position(new Vector3(mPoints[0].x, mPoints[0].y, 0f));
}
}
// *
// * Tells whether a point is inside a shape or not
// * @param point The point to check
// * @return true if the point is inside this shape, false otherwise
//
//
//ORIGINAL LINE: bool isPointInside(const Ogre::Vector2& point) const;
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
// bool isPointInside(Ogre::Vector2 point);
//-----------------------------------------------------------------------
//
//ORIGINAL LINE: bool Shape::isPointInside(const Vector2& point) const
public bool isPointInside(Vector2 point) {
// Draw a horizontal lines that goes through "point"
// Using the closest intersection, find whethe the point is actually inside
int closestSegmentIndex = -1;
float closestSegmentDistance = float.MaxValue;//std.numeric_limits<Real>.max();
Vector2 closestSegmentIntersection = new Vector2(0f, 0f);
for (int i = 0; i < getSegCount(); i++) {
Vector2 A = getPoint(i);
Vector2 B = getPoint(i + 1);
if (A.y != B.y && (A.y - point.y) * (B.y - point.y) <= 0.0f) {
Vector2 intersect = new Vector2(A.x + (point.y - A.y) * (B.x - A.x) / (B.y - A.y), point.y);
float dist = Math.Abs(point.x - intersect.x);
if (dist < closestSegmentDistance) {
closestSegmentIndex = i;
closestSegmentDistance = dist;
//
//ORIGINAL LINE: closestSegmentIntersection = intersect;
closestSegmentIntersection = (intersect);
}
}
}
if (closestSegmentIndex != -1) {
if (getNormalAfter((uint)closestSegmentIndex).x * (point.x - closestSegmentIntersection.x) < 0f)
return true;
else
return false;
}
if (findRealOutSide() == mOutSide)
return false;
else
return true;
}
//-----------------------------------------------------------------------
public bool _sortAngles(std_pair<Radian, uint> one, std_pair<Radian, uint> two) // waiting for lambda functions!
{
return one.first < two.first;
}
/// <summary>
/// one.KeyСÓÚtwo.Key
/// </summary>
/// <param name="one"></param>
/// <param name="two"></param>
/// <returns></returns>
public static bool _sortAngles(KeyValuePair<Radian, byte> one, KeyValuePair<Radian, byte> two) // waiting for lambda functions!
{
return one.Key < two.Key;
}
// *
// * Computes the intersection between this shape and another one.
// * Both shapes must be closed.
// * <table border="0" width="100%"><tr><td>\image html shape_booleansetup.png "Start shapes"</td><td>\image html shape_booleanintersection.png "Intersection of the two shapes"</td></tr></table>
// * @param other The shape against which the intersection is computed
// * @return The intersection of two shapes, as a new shape
// * @exception Ogre::InvalidStateException Current shapes must be closed and has to contain at least 2 points!
// * @exception Ogre::InvalidParametersException Other shapes must be closed and has to contain at least 2 points!
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//
//ORIGINAL LINE: MultiShape booleanIntersect(const Shape& STLAllocator<U, AllocPolicy>) const
public MultiShape booleanIntersect(Shape other) {
return _booleanOperation(other, BooleanOperationType.BOT_INTERSECTION);
}
// *
// * Computes the union between this shape and another one.
// * Both shapes must be closed.
// * <table border="0" width="100%"><tr><td>\image html shape_booleansetup.png "Start shapes"</td><td>\image html shape_booleanunion.png "Union of the two shapes"</td></tr></table>
// * @param other The shape against which the union is computed
// * @return The union of two shapes, as a new shape
// * @exception Ogre::InvalidStateException Current shapes must be closed and has to contain at least 2 points!
// * @exception Ogre::InvalidParametersException Other shapes must be closed and has to contain at least 2 points!
//
//-----------------------------------------------------------------------
//
//ORIGINAL LINE: MultiShape booleanUnion(const Shape& STLAllocator<U, AllocPolicy>) const
public MultiShape booleanUnion(Shape other) {
return _booleanOperation(other, BooleanOperationType.BOT_UNION);
}
// *
// * Computes the difference between this shape and another one.
// * Both shapes must be closed.
// * <table border="0" width="100%"><tr><td>\image html shape_booleansetup.png "Start shapes"</td><td>\image html shape_booleandifference.png "Difference of the two shapes"</td></tr></table>
// * @param other The shape against which the diffenrence is computed
// * @return The difference of two shapes, as a new shape
// * @exception Ogre::InvalidStateException Current shapes must be closed and has to contain at least 2 points!
// * @exception Ogre::InvalidParametersException Other shapes must be closed and has to contain at least 2 points!
//
//-----------------------------------------------------------------------
//
//ORIGINAL LINE: MultiShape booleanDifference(const Shape& STLAllocator<U, AllocPolicy>) const
public MultiShape booleanDifference(Shape other) {
return _booleanOperation(other, BooleanOperationType.BOT_DIFFERENCE);
}
// *
// * On a closed shape, find if the outside is located on the right
// * or on the left. If the outside can easily be guessed in your context,
// * you'd rather use setOutside(), which doesn't need any computation.
//
//-----------------------------------------------------------------------
//
//ORIGINAL LINE: Side findRealOutSide() const
public Side findRealOutSide() {
float x = mPoints[0].x;
uint index = 0;
for (ushort i = 1; i < mPoints.Count; i++) {
if (x < mPoints[i].x) {
x = mPoints[i].x;
index = i;
}
}
Radian alpha1 = Utils.angleTo(new Vector2(0f, 1f)/*Vector2.UNIT_Y*/, getDirectionAfter(index));
Radian alpha2 = Utils.angleTo(new Vector2(0f, 1f)/*Vector2.UNIT_Y*/, -getDirectionBefore(index));
if (alpha1 < alpha2)
return Side.SIDE_RIGHT;
else
return Side.SIDE_LEFT;
}
// *
// * Determines whether the outside as defined by user equals "real" outside
//
//-----------------------------------------------------------------------
//
//ORIGINAL LINE: bool isOutsideRealOutside() const
public bool isOutsideRealOutside() {
return findRealOutSide() == mOutSide;
}
/// Creates a shape with the keys of this shape and extra keys coming from a track
/// @param track the track to merge keys with
/// @return a new Shape coming from the merge between original shape and the track
//-----------------------------------------------------------------------
//
//ORIGINAL LINE: Shape mergeKeysWithTrack(const Track& track) const
public Shape mergeKeysWithTrack(Track track) {
if (!track.isInsertPoint() || track.getAddressingMode() == Track.AddressingMode.AM_POINT)
return this;
float totalLength = getTotalLength();
float lineicPos = 0;
float shapeLineicPos = 0;
Shape outputShape = new Shape();
if (mClosed)
outputShape.close();
outputShape.addPoint(getPoint(0));
for (int i = 1; i < mPoints.Count; ) {
float nextLineicPos = shapeLineicPos + (mPoints[i] - mPoints[i - 1]).Length;
//std.map<Real,Real>.Enumerator it = track._getKeyValueAfter(lineicPos, lineicPos/totalLength, i-1);
std_pair<float, float> it = track._getKeyValueAfter(lineicPos, lineicPos / totalLength, ((uint)i - 1));
float nextTrackPos = it.first;
if (track.getAddressingMode() == Track.AddressingMode.AM_RELATIVE_LINEIC)
nextTrackPos *= totalLength;
// Adds the closest point to the curve, being either from the shape or the track
if (nextLineicPos <= nextTrackPos || lineicPos >= nextTrackPos) {
outputShape.addPoint(mPoints[i]);
i++;
lineicPos = nextLineicPos;
shapeLineicPos = nextLineicPos;
}
else {
outputShape.addPoint(getPosition((uint)i - 1, (nextTrackPos - shapeLineicPos) / (nextLineicPos - shapeLineicPos)));
lineicPos = nextTrackPos;
}
}
return outputShape;
}
// *
// * Applies the given translation to all the points already defined.
// * Has strictly no effect on the points defined after that
// * @param translation the translation vector
//
public Shape translate(Vector2 translation) {
//for (List<Vector2>.Enumerator it = mPoints.GetEnumerator(); it.MoveNext(); ++it)
// it.Current+=translation;
for (int i = 0; i < mPoints.Count; i++) {
mPoints[i] += translation;
}
return this;
}
// *
// * Applies the given translation to all the points already defined.
// * Has strictly no effect on the points defined after that
// * @param translationX X component of the translation vector
// * @param translationY Y component of the translation vector
//
public Shape translate(float translationX, float translationY) {
return translate(new Vector2(translationX, translationY));
}
// *
// * Applies the given rotation to all the points already defined.
// * Has strictly no effect on the points defined after that
// * @param angle angle of rotation
//
public Shape rotate(Radian angle) {
float c = Math.Cos(angle.ValueRadians);
float s = Math.Sin(angle.ValueRadians);
//for (List<Vector2>.Enumerator it = mPoints.GetEnumerator(); it.MoveNext(); ++it)
for (int i = 0; i < mPoints.Count; i++) {
Vector2 it = mPoints[i];
float x = it.x;
float y = it.y;
float it_x = c * x - s * y;
float it_y = s * x + c * y;
it = new Vector2(it_x, it_y);
mPoints[i] = it;
}
return this;
}
// *
// * Applies the given scale to all the points already defined.
// * Has strictly no effect on the points defined after that
// * @param amount amount of scale
//
public Shape scale(float amount) {
return scale(amount, amount);
}
// *
// * Applies the given scale to all the points already defined.
// * Has strictly no effect on the points defined after that
// * @param scaleX amount of scale in the X direction
// * @param scaleY amount of scale in the Y direction
//
public Shape scale(float scaleX, float scaleY) {
//for (List<Vector2>.Enumerator it = mPoints.GetEnumerator(); it.MoveNext(); ++it)
for (int i = 0; i < mPoints.Count; i++) {
Vector2 it = mPoints[i];
float it_x = it.x * scaleX;
float it_y = it.y * scaleY;
it = new Vector2(it_x, it_y);
mPoints[i] = it;
}
return this;
}
// *
// * Applies the given scale to all the points already defined.
// * Has strictly no effect on the points defined after that
// * @param amount of scale
//
public Shape scale(Vector2 amount) {
return scale(amount.x, amount.y);
}
// *
// * Reflect all points in this shape against a zero-origined line with a given normal
// * @param normal the normal
//
public Shape reflect(Vector2 normal) {
//for (List<Vector2>.Enumerator it = mPoints.GetEnumerator(); it.MoveNext(); ++it)
for (int i = 0; i < mPoints.Count; i++) {
mPoints[i] = mPoints[i].Reflect(normal);
//it.Current = it.reflect(normal);
}
return this;
}
// *
// * Create a symetric copy at the origin point.
// * @parm flip \c true if function should start mirroring with the last point in list (default \c false)
//
public Shape mirror() {
return mirror(false);
}
//
//ORIGINAL LINE: Shape& mirror(bool flip = false)
public Shape mirror(bool flip) {
return mirrorAroundPoint(new Vector2(0f, 0f), flip);
}
// *
// * Create a symetric copy at a given point.
// * @param x x coordinate of point where to mirror
// * @param y y coordinate of point where to mirror
// * @parm flip \c true if function should start mirroring with the last point in list (default \c false)
//
public Shape mirror(float x, float y) {
return mirror(x, y, false);
}
//
//ORIGINAL LINE: Shape& mirror(Ogre::float x, Ogre::float y, bool flip = false)
public Shape mirror(float x, float y, bool flip) {
return mirrorAroundPoint(new Vector2(x, y), flip);
}
// *
// * Create a symetric copy at a given point.
// * @param point Point where to mirror
// * @parm flip \c true if function should start mirroring with the last point in list (default \c false)
//
public Shape mirrorAroundPoint(Vector2 point) {
return mirrorAroundPoint(point, false);
}
//
//ORIGINAL LINE: Shape& mirrorAroundPoint(Ogre::Vector2 point, bool flip = false)
public Shape mirrorAroundPoint(Vector2 point, bool flip) {
int l = (int)mPoints.Count;
if (flip)
for (int i = l - 1; i >= 0; i--) {
Vector2 pos = mPoints[i] - point;
mPoints.Add(-1.0f * pos + point);
}
else
for (int i = 0; i < l; i++) {
Vector2 pos = mPoints[i] - point;
mPoints.Add(-1.0f * pos + point);
}
return this;
}
// *
// * Create a symetric copy at a given axis.
// * @param axis Axis where to mirror
// * @param flip \c true if function should start mirroring with the first point in list (default \c false)
//
public Shape mirrorAroundAxis(Vector2 axis) {
return mirrorAroundAxis(axis, false);
}
//
//ORIGINAL LINE: Shape& mirrorAroundAxis(const Ogre::Vector2& axis, bool flip = false)
public Shape mirrorAroundAxis(Vector2 axis, bool flip) {
int l = (int)mPoints.Count;
Vector2 normal = axis.Perpendicular.NormalisedCopy;
if (flip)
for (int i = 0; i < l; i++) {
Vector2 pos = mPoints[i];
pos = pos.Reflect(normal);
if (pos != mPoints[i])
mPoints.Add(pos);
}
else
for (int i = l - 1; i >= 0; i--) {
Vector2 pos = mPoints[i];
pos = pos.Reflect(normal);
if (pos != mPoints[i])
mPoints.Add(pos);
}
return this;
}
/// Returns the total lineic length of that shape
//
//ORIGINAL LINE: Ogre::float getTotalLength() const
public float getTotalLength() {
float length = 0;
for (int i = 0; i < mPoints.Count - 1; i++)
length += (mPoints[i + 1] - mPoints[i]).Length;
if (mClosed)
length += (mPoints[mPoints.Count - 1] - mPoints[0]).Length;
return length;
}
/// Gets a position on the shape with index of the point and a percentage of position on the segment
/// @param i index of the segment
/// @param coord a number between 0 and 1 meaning the percentage of position on the segment
/// @exception Ogre::InvalidParametersException i is out of bounds
/// @exception Ogre::InvalidParametersException coord must be comprised between 0 and 1
//
//ORIGINAL LINE: inline Ogre::Vector2 getPosition(uint i, Ogre::float coord) const
public Vector2 getPosition(uint i, float coord) {
if (!mClosed || i >= mPoints.size())
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "Out of Bounds", "Procedural::Path::getPosition(unsigned int, Ogre::Real)");
if (coord < 0.0f || coord > 1.0f)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "Coord must be comprised between 0 and 1", "Procedural::Path::getPosition(unsigned int, Ogre::Real)"); ;
Vector2 A = getPoint((int)i);
Vector2 B = getPoint((int)i + 1);
return A + coord * (B - A);
}
private void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}
/// Gets a position on the shape from lineic coordinate
/// @param coord lineic coordinate
/// @exception Ogre::InvalidStateException The shape must at least contain 2 points
//
//ORIGINAL LINE: inline Ogre::Vector2 getPosition(Ogre::float coord) const
public Vector2 getPosition(float coord) {
if (mPoints.size() < 2)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALID_STATE", "The shape must at least contain 2 points", "Procedural::Shape::getPosition(Ogre::Real)");
;
int i = 0;
while (true) {
float nextLen = (getPoint(i + 1) - getPoint(i)).Length;
if (coord > nextLen)
coord -= nextLen;
else
return getPosition((uint)i, coord);
if (!mClosed && i >= mPoints.size() - 2)
return mPoints[mPoints.size() - 1];
i++;
}
}
/// Computes the radius of a bounding circle centered on the origin
//
//ORIGINAL LINE: Ogre::float findBoundingRadius() const
public float findBoundingRadius() {
float sqRadius = 0.0f;
for (int i = 0; i < mPoints.size(); i++)
sqRadius = System.Math.Max(sqRadius, mPoints[i].SquaredLength);
return Math.Sqrt(sqRadius);
}
// *
// * Applies a "thickness" to a shape, ie a bit like the extruder, but in 2D
// * <table border="0" width="100%"><tr><td>\image html shape_thick1.png "Start shape (before thicken)"</td><td>\image html shape_thick2.png "Result (after thicken)"</td></tr></table>
//
//-----------------------------------------------------------------------
public MultiShape thicken(float amount) {
if (!mClosed) {
Shape s = new Shape();
s.setOutSide(mOutSide);
for (int i = 0; i < mPoints.Count; i++)
s.addPoint(mPoints[i] + amount * getAvgNormal((uint)i));
for (int i = mPoints.Count - 1; i >= 0; i--)
s.addPoint(mPoints[i] - amount * getAvgNormal((uint)i));
s.close();
return new MultiShape().addShape(s);
}
else {
MultiShape ms = new MultiShape();
Shape s1 = new Shape();
for (int i = 0; i < mPoints.Count; i++)
s1.addPoint(mPoints[i] + amount * getAvgNormal((uint)i));
s1.close();
s1.setOutSide(mOutSide);
ms.addShape(s1);
Shape s2 = new Shape();
for (int i = 0; i < mPoints.Count; i++)
s2.addPoint(mPoints[i] - amount * getAvgNormal((uint)i));
s2.close();
s2.setOutSide(mOutSide == Side.SIDE_LEFT ? Side.SIDE_RIGHT : Side.SIDE_LEFT);
ms.addShape(s2);
return ms;
}
}
//-----------------------------------------------------------------------
//
//ORIGINAL LINE: MultiShape _booleanOperation(const Shape& STLAllocator<U, AllocPolicy>, BooleanOperationType opType) const
private MultiShape _booleanOperation(Shape other, BooleanOperationType opType) {
if (!mClosed || mPoints.size() < 2)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALID_STATE", "Current shapes must be closed and has to contain at least 2 points!", "Procedural::Shape::_booleanOperation(const Procedural::Shape&, Procedural::BooleanOperationType)");
if (!other.mClosed || other.mPoints.size() < 2)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "Other shapes must be closed and has to contain at least 2 points!", "Procedural::Shape::_booleanOperation(const Procedural::Shape&, Procedural::BooleanOperationType)");
;
// Compute the intersection between the 2 shapes
std_vector<IntersectionInShape> intersections = new std_vector<IntersectionInShape>();
_findAllIntersections(other, ref intersections);
// Build the resulting shape
if (intersections.empty()) {
if (isPointInside(other.getPoint(0))) {
// Shape B is completely inside shape A
if (opType == BooleanOperationType.BOT_UNION) {
MultiShape ms = new MultiShape();
ms.addShape(this);
return ms;
}
else if (opType == BooleanOperationType.BOT_INTERSECTION) {
MultiShape ms = new MultiShape();
ms.addShape(other);
return ms;
}
else if (opType == BooleanOperationType.BOT_DIFFERENCE) {
MultiShape ms = new MultiShape();
ms.addShape(this);
ms.addShape(other);
ms.getShape(1).switchSide();
return ms;
}
}
else if (other.isPointInside(getPoint(0))) {
// Shape A is completely inside shape B
if (opType == BooleanOperationType.BOT_UNION) {
MultiShape ms = new MultiShape();
ms.addShape(other);
return ms;
}
else if (opType == BooleanOperationType.BOT_INTERSECTION) {
MultiShape ms = new MultiShape();
ms.addShape(this);
return ms;
}
else if (opType == BooleanOperationType.BOT_DIFFERENCE) {
MultiShape ms = new MultiShape();
ms.addShape(this);
ms.addShape(other);
ms.getShape(0).switchSide();
return ms;
}
}
else {
if (opType == BooleanOperationType.BOT_UNION) {
MultiShape ms = new MultiShape();
ms.addShape(this);
ms.addShape(other);
return ms;
}
else if (opType == BooleanOperationType.BOT_INTERSECTION)
return new MultiShape(); //empty result
else if (opType == BooleanOperationType.BOT_DIFFERENCE)
return new MultiShape(); //empty result
}
}
MultiShape outputMultiShape = new MultiShape();
Shape[] inputShapes = new Shape[2];
inputShapes[0] = this;
inputShapes[1] = other;
while (!intersections.empty()) {
Shape outputShape = new Shape();
byte shapeSelector = 0; // 0 : first shape, 1 : second shape
Vector2 currentPosition = intersections[0].position;//intersections.GetEnumerator().position;
IntersectionInShape firstIntersection = intersections[0];//*intersections.GetEnumerator();