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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
|
/*
* Copyright (c) Contributors
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSimulator Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace PrimMesher
{
public struct Quat
{
/// <summary>X value</summary>
public float X;
/// <summary>Y value</summary>
public float Y;
/// <summary>Z value</summary>
public float Z;
/// <summary>W value</summary>
public float W;
public Quat(float x, float y, float z, float w)
{
X = x;
Y = y;
Z = z;
W = w;
}
public Quat(Coord axis, float angle)
{
axis = axis.Normalize();
angle *= 0.5f;
float c = (float)Math.Cos(angle);
float s = (float)Math.Sin(angle);
X = axis.X * s;
Y = axis.Y * s;
Z = axis.Z * s;
W = c;
Normalize();
}
public float Length()
{
return (float)Math.Sqrt(X * X + Y * Y + Z * Z + W * W);
}
public Quat Normalize()
{
const float MAG_THRESHOLD = 0.0000001f;
float mag = Length();
// Catch very small rounding errors when normalizing
if (mag > MAG_THRESHOLD)
{
float oomag = 1f / mag;
X *= oomag;
Y *= oomag;
Z *= oomag;
W *= oomag;
}
else
{
X = 0f;
Y = 0f;
Z = 0f;
W = 1f;
}
return this;
}
public static Quat operator *(Quat q1, Quat q2)
{
float x = q1.W * q2.X + q1.X * q2.W + q1.Y * q2.Z - q1.Z * q2.Y;
float y = q1.W * q2.Y - q1.X * q2.Z + q1.Y * q2.W + q1.Z * q2.X;
float z = q1.W * q2.Z + q1.X * q2.Y - q1.Y * q2.X + q1.Z * q2.W;
float w = q1.W * q2.W - q1.X * q2.X - q1.Y * q2.Y - q1.Z * q2.Z;
return new Quat(x, y, z, w);
}
public override string ToString()
{
return "< X: " + this.X.ToString() + ", Y: " + this.Y.ToString() + ", Z: " + this.Z.ToString() + ", W: " + this.W.ToString() + ">";
}
}
public struct Coord
{
public float X;
public float Y;
public float Z;
public Coord(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public float Length()
{
return (float)Math.Sqrt(this.X * this.X + this.Y * this.Y + this.Z * this.Z);
}
public Coord Invert()
{
this.X = -this.X;
this.Y = -this.Y;
this.Z = -this.Z;
return this;
}
public Coord Normalize()
{
const float MAG_THRESHOLD = 0.0000001f;
float mag = Length();
// Catch very small rounding errors when normalizing
if (mag > MAG_THRESHOLD)
{
float oomag = 1.0f / mag;
this.X *= oomag;
this.Y *= oomag;
this.Z *= oomag;
}
else
{
this.X = 0.0f;
this.Y = 0.0f;
this.Z = 0.0f;
}
return this;
}
public override string ToString()
{
return this.X.ToString() + " " + this.Y.ToString() + " " + this.Z.ToString();
}
public static Coord Cross(Coord c1, Coord c2)
{
return new Coord(
c1.Y * c2.Z - c2.Y * c1.Z,
c1.Z * c2.X - c2.Z * c1.X,
c1.X * c2.Y - c2.X * c1.Y
);
}
public static Coord operator +(Coord v, Coord a)
{
return new Coord(v.X + a.X, v.Y + a.Y, v.Z + a.Z);
}
public static Coord operator *(Coord v, Coord m)
{
return new Coord(v.X * m.X, v.Y * m.Y, v.Z * m.Z);
}
public static Coord operator *(Coord v, Quat q)
{
// From http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/transforms/
Coord c2 = new Coord(0.0f, 0.0f, 0.0f);
c2.X = q.W * q.W * v.X +
2f * q.Y * q.W * v.Z -
2f * q.Z * q.W * v.Y +
q.X * q.X * v.X +
2f * q.Y * q.X * v.Y +
2f * q.Z * q.X * v.Z -
q.Z * q.Z * v.X -
q.Y * q.Y * v.X;
c2.Y =
2f * q.X * q.Y * v.X +
q.Y * q.Y * v.Y +
2f * q.Z * q.Y * v.Z +
2f * q.W * q.Z * v.X -
q.Z * q.Z * v.Y +
q.W * q.W * v.Y -
2f * q.X * q.W * v.Z -
q.X * q.X * v.Y;
c2.Z =
2f * q.X * q.Z * v.X +
2f * q.Y * q.Z * v.Y +
q.Z * q.Z * v.Z -
2f * q.W * q.Y * v.X -
q.Y * q.Y * v.Z +
2f * q.W * q.X * v.Y -
q.X * q.X * v.Z +
q.W * q.W * v.Z;
return c2;
}
}
public struct Face
{
public int primFace;
// vertices
public int v1;
public int v2;
public int v3;
public Face(int v1, int v2, int v3)
{
primFace = 0;
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
public Coord SurfaceNormal(List<Coord> coordList)
{
Coord c1 = coordList[this.v1];
Coord c2 = coordList[this.v2];
Coord c3 = coordList[this.v3];
Coord edge1 = new Coord(c2.X - c1.X, c2.Y - c1.Y, c2.Z - c1.Z);
Coord edge2 = new Coord(c3.X - c1.X, c3.Y - c1.Y, c3.Z - c1.Z);
return Coord.Cross(edge1, edge2).Normalize();
}
}
internal struct Angle
{
internal float angle;
internal float X;
internal float Y;
internal Angle(float angle, float x, float y)
{
this.angle = angle;
this.X = x;
this.Y = y;
}
}
internal class AngleList
{
private float iX, iY; // intersection point
private static Angle[] angles3 =
{
new Angle(0.0f, 1.0f, 0.0f),
new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
new Angle(1.0f, 1.0f, 0.0f)
};
private static Angle[] angles4 =
{
new Angle(0.0f, 1.0f, 0.0f),
new Angle(0.25f, 0.0f, 1.0f),
new Angle(0.5f, -1.0f, 0.0f),
new Angle(0.75f, 0.0f, -1.0f),
new Angle(1.0f, 1.0f, 0.0f)
};
private static Angle[] angles6 =
{
new Angle(0.0f, 1.0f, 0.0f),
new Angle(0.16666666666666667f, 0.5f, 0.8660254037844386f),
new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
new Angle(0.5f, -1.0f, 0.0f),
new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
new Angle(0.83333333333333326f, 0.5f, -0.86602540378443904f),
new Angle(1.0f, 1.0f, 0.0f)
};
private static Angle[] angles12 =
{
new Angle(0.0f, 1.0f, 0.0f),
new Angle(0.083333333333333329f, 0.86602540378443871f, 0.5f),
new Angle(0.16666666666666667f, 0.5f, 0.8660254037844386f),
new Angle(0.25f, 0.0f, 1.0f),
new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
new Angle(0.41666666666666663f, -0.86602540378443849f, 0.5f),
new Angle(0.5f, -1.0f, 0.0f),
new Angle(0.58333333333333326f, -0.86602540378443882f, -0.5f),
new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
new Angle(0.75f, 0.0f, -1.0f),
new Angle(0.83333333333333326f, 0.5f, -0.86602540378443904f),
new Angle(0.91666666666666663f, 0.86602540378443837f, -0.5f),
new Angle(1.0f, 1.0f, 0.0f)
};
private static Angle[] angles24 =
{
new Angle(0.0f, 1.0f, 0.0f),
new Angle(0.041666666666666664f, 0.96592582628906831f, 0.25881904510252074f),
new Angle(0.083333333333333329f, 0.86602540378443871f, 0.5f),
new Angle(0.125f, 0.70710678118654757f, 0.70710678118654746f),
new Angle(0.16666666666666667f, 0.5f, 0.8660254037844386f),
new Angle(0.20833333333333331f, 0.25881904510252096f, 0.9659258262890682f),
new Angle(0.25f, 0.0f, 1.0f),
new Angle(0.29166666666666663f, -0.25881904510252063f, 0.96592582628906831f),
new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
new Angle(0.375f, -0.70710678118654746f, 0.70710678118654757f),
new Angle(0.41666666666666663f, -0.86602540378443849f, 0.5f),
new Angle(0.45833333333333331f, -0.9659258262890682f, 0.25881904510252102f),
new Angle(0.5f, -1.0f, 0.0f),
new Angle(0.54166666666666663f, -0.96592582628906842f, -0.25881904510252035f),
new Angle(0.58333333333333326f, -0.86602540378443882f, -0.5f),
new Angle(0.62499999999999989f, -0.70710678118654791f, -0.70710678118654713f),
new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
new Angle(0.70833333333333326f, -0.25881904510252152f, -0.96592582628906809f),
new Angle(0.75f, 0.0f, -1.0f),
new Angle(0.79166666666666663f, 0.2588190451025203f, -0.96592582628906842f),
new Angle(0.83333333333333326f, 0.5f, -0.86602540378443904f),
new Angle(0.875f, 0.70710678118654735f, -0.70710678118654768f),
new Angle(0.91666666666666663f, 0.86602540378443837f, -0.5f),
new Angle(0.95833333333333326f, 0.96592582628906809f, -0.25881904510252157f),
new Angle(1.0f, 1.0f, 0.0f)
};
private Angle interpolatePoints(float newPoint, Angle p1, Angle p2)
{
float m = (newPoint - p1.angle) / (p2.angle - p1.angle);
return new Angle(newPoint, p1.X + m * (p2.X - p1.X), p1.Y + m * (p2.Y - p1.Y));
}
private void intersection(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
{ // ref: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
double uaNumerator = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
if (denom != 0.0)
{
double ua = uaNumerator / denom;
iX = (float)(x1 + ua * (x2 - x1));
iY = (float)(y1 + ua * (y2 - y1));
}
}
internal List<Angle> angles;
internal void makeAngles(int sides, float startAngle, float stopAngle, bool hasCut)
{
angles = new List<Angle>();
const double twoPi = System.Math.PI * 2.0;
const float twoPiInv = (float)(1.0d / twoPi);
if (sides < 1)
throw new Exception("number of sides not greater than zero");
if (stopAngle <= startAngle)
throw new Exception("stopAngle not greater than startAngle");
if ((sides == 3 || sides == 4 || sides == 6 || sides == 12 || sides == 24))
{
startAngle *= twoPiInv;
stopAngle *= twoPiInv;
Angle[] sourceAngles;
switch (sides)
{
case 3:
sourceAngles = angles3;
break;
case 4:
sourceAngles = angles4;
break;
case 6:
sourceAngles = angles6;
break;
case 12:
sourceAngles = angles12;
break;
default:
sourceAngles = angles24;
break;
}
int startAngleIndex = (int)(startAngle * sides);
int endAngleIndex = sourceAngles.Length - 1;
if (hasCut)
{
if (stopAngle < 1.0f)
endAngleIndex = (int)(stopAngle * sides) + 1;
if (endAngleIndex == startAngleIndex)
endAngleIndex++;
for (int angleIndex = startAngleIndex; angleIndex < endAngleIndex + 1; angleIndex++)
{
angles.Add(sourceAngles[angleIndex]);
}
if (startAngle > 0.0f)
angles[0] = interpolatePoints(startAngle, angles[0], angles[1]);
if (stopAngle < 1.0f)
{
int lastAngleIndex = angles.Count - 1;
angles[lastAngleIndex] = interpolatePoints(stopAngle, angles[lastAngleIndex - 1], angles[lastAngleIndex]);
}
}
else
{
for (int angleIndex = startAngleIndex; angleIndex < endAngleIndex; angleIndex++)
angles.Add(sourceAngles[angleIndex]);
}
}
else
{
double stepSize = twoPi / sides;
int startStep = (int)(startAngle / stepSize);
double angle = stepSize * startStep;
int step = startStep;
double stopAngleTest = stopAngle;
if (stopAngle < twoPi)
{
stopAngleTest = stepSize * ((int)(stopAngle / stepSize) + 1);
if (stopAngleTest < stopAngle)
stopAngleTest += stepSize;
if (stopAngleTest > twoPi)
stopAngleTest = twoPi;
}
while (angle <= stopAngleTest)
{
Angle newAngle;
newAngle.angle = (float)angle;
newAngle.X = (float)System.Math.Cos(angle);
newAngle.Y = (float)System.Math.Sin(angle);
angles.Add(newAngle);
step += 1;
angle = stepSize * step;
}
if (startAngle > angles[0].angle)
{
Angle newAngle;
intersection(angles[0].X, angles[0].Y, angles[1].X, angles[1].Y, 0.0f, 0.0f, (float)Math.Cos(startAngle), (float)Math.Sin(startAngle));
newAngle.angle = startAngle;
newAngle.X = iX;
newAngle.Y = iY;
angles[0] = newAngle;
}
int index = angles.Count - 1;
if (stopAngle < angles[index].angle)
{
Angle newAngle;
intersection(angles[index - 1].X, angles[index - 1].Y, angles[index].X, angles[index].Y, 0.0f, 0.0f, (float)Math.Cos(stopAngle), (float)Math.Sin(stopAngle));
newAngle.angle = stopAngle;
newAngle.X = iX;
newAngle.Y = iY;
angles[index] = newAngle;
}
}
}
}
/// <summary>
/// generates a profile for extrusion
/// </summary>
public class Profile
{
private const float twoPi = 2.0f * (float)Math.PI;
public string errorMessage = null;
public List<Coord> coords;
public List<Face> faces;
// use these for making individual meshes for each prim face
public List<int> outerCoordIndices = null;
public List<int> hollowCoordIndices = null;
public int numOuterVerts = 0;
public int numHollowVerts = 0;
public int outerFaceNumber = -1;
public int hollowFaceNumber = -1;
public int bottomFaceNumber = 0;
public int numPrimFaces = 0;
public Profile()
{
coords = new List<Coord>();
faces = new List<Face>();
}
public Profile(int sides, float profileStart, float profileEnd, float hollow, int hollowSides, bool hasProfileCut, bool createFaces)
{
const float halfSqr2 = 0.7071067811866f;
coords = new List<Coord>();
faces = new List<Face>();
List<Coord> hollowCoords = new List<Coord>();
bool hasHollow = (hollow > 0.0f);
AngleList angles = new AngleList();
AngleList hollowAngles = new AngleList();
float xScale = 0.5f;
float yScale = 0.5f;
if (sides == 4) // corners of a square are sqrt(2) from center
{
xScale = halfSqr2;
yScale = halfSqr2;
}
float startAngle = profileStart * twoPi;
float stopAngle = profileEnd * twoPi;
try { angles.makeAngles(sides, startAngle, stopAngle,hasProfileCut); }
catch (Exception ex)
{
errorMessage = "makeAngles failed: Exception: " + ex.ToString()
+ "\nsides: " + sides.ToString() + " startAngle: " + startAngle.ToString() + " stopAngle: " + stopAngle.ToString();
return;
}
numOuterVerts = angles.angles.Count;
Angle angle;
Coord newVert = new Coord();
// flag to create as few triangles as possible for 3 or 4 side profile
bool simpleFace = (sides < 5 && !hasHollow && !hasProfileCut);
if (hasHollow)
{
if (sides == hollowSides)
hollowAngles = angles;
else
{
try { hollowAngles.makeAngles(hollowSides, startAngle, stopAngle, hasProfileCut); }
catch (Exception ex)
{
errorMessage = "makeAngles failed: Exception: " + ex.ToString()
+ "\nsides: " + sides.ToString() + " startAngle: " + startAngle.ToString() + " stopAngle: " + stopAngle.ToString();
return;
}
int numHollowAngles = hollowAngles.angles.Count;
for (int i = 0; i < numHollowAngles; i++)
{
angle = hollowAngles.angles[i];
newVert.X = hollow * xScale * angle.X;
newVert.Y = hollow * yScale * angle.Y;
newVert.Z = 0.0f;
hollowCoords.Add(newVert);
}
}
numHollowVerts = hollowAngles.angles.Count;
}
else if (!simpleFace)
{
Coord center = new Coord(0.0f, 0.0f, 0.0f);
this.coords.Add(center);
}
int numAngles = angles.angles.Count;
bool hollowsame = (hasHollow && hollowSides == sides);
for (int i = 0; i < numAngles; i++)
{
angle = angles.angles[i];
newVert.X = angle.X * xScale;
newVert.Y = angle.Y * yScale;
newVert.Z = 0.0f;
coords.Add(newVert);
if (hollowsame)
{
newVert.X *= hollow;
newVert.Y *= hollow;
hollowCoords.Add(newVert);
}
}
if (hasHollow)
{
hollowCoords.Reverse();
coords.AddRange(hollowCoords);
if (createFaces)
{
int numTotalVerts = numOuterVerts + numHollowVerts;
if (numOuterVerts == numHollowVerts)
{
Face newFace = new Face();
for (int coordIndex = 0; coordIndex < numOuterVerts - 1; coordIndex++)
{
newFace.v1 = coordIndex;
newFace.v2 = coordIndex + 1;
newFace.v3 = numTotalVerts - coordIndex - 1;
faces.Add(newFace);
newFace.v1 = coordIndex + 1;
newFace.v2 = numTotalVerts - coordIndex - 2;
newFace.v3 = numTotalVerts - coordIndex - 1;
faces.Add(newFace);
}
if (!hasProfileCut)
{
newFace.v1 = numOuterVerts - 1;
newFace.v2 = 0;
newFace.v3 = numOuterVerts;
faces.Add(newFace);
newFace.v1 = 0;
newFace.v2 = numTotalVerts - 1;
newFace.v3 = numOuterVerts;
faces.Add(newFace);
}
}
else if (numOuterVerts < numHollowVerts)
{
Face newFace = new Face();
int j = 0; // j is the index for outer vertices
int i;
int maxJ = numOuterVerts - 1;
float curHollowAngle = 0;
for (i = 0; i < numHollowVerts; i++) // i is the index for inner vertices
{
curHollowAngle = hollowAngles.angles[i].angle;
if (j < maxJ)
{
if (angles.angles[j + 1].angle - curHollowAngle < curHollowAngle - angles.angles[j].angle + 0.000001f)
{
newFace.v1 = numTotalVerts - i - 1;
newFace.v2 = j;
newFace.v3 = j + 1;
faces.Add(newFace);
j++;
}
}
else
{
if (1.0f - curHollowAngle < curHollowAngle - angles.angles[j].angle + 0.000001f)
break;
}
newFace.v1 = j;
newFace.v2 = numTotalVerts - i - 2;
newFace.v3 = numTotalVerts - i - 1;
faces.Add(newFace);
}
if (!hasProfileCut)
{
if (i == numHollowVerts)
{
newFace.v1 = numTotalVerts - numHollowVerts;
newFace.v2 = maxJ;
newFace.v3 = 0;
faces.Add(newFace);
}
else
{
if (1.0f - curHollowAngle < curHollowAngle - angles.angles[maxJ].angle + 0.000001f)
{
newFace.v1 = numTotalVerts - i - 1;
newFace.v2 = maxJ;
newFace.v3 = 0;
faces.Add(newFace);
}
for (; i < numHollowVerts - 1; i++)
{
newFace.v1 = 0;
newFace.v2 = numTotalVerts - i - 2;
newFace.v3 = numTotalVerts - i - 1;
faces.Add(newFace);
}
}
newFace.v1 = 0;
newFace.v2 = numTotalVerts - 1;
newFace.v3 = numTotalVerts - numHollowVerts;
faces.Add(newFace);
}
}
else // numHollowVerts < numOuterVerts
{
Face newFace = new Face();
int j = 0; // j is the index for inner vertices
int maxJ = numHollowVerts - 1;
for (int i = 0; i < numOuterVerts; i++)
{
if (j < maxJ)
if (hollowAngles.angles[j + 1].angle - angles.angles[i].angle < angles.angles[i].angle - hollowAngles.angles[j].angle + 0.000001f)
{
newFace.v1 = i;
newFace.v2 = numTotalVerts - j - 2;
newFace.v3 = numTotalVerts - j - 1;
faces.Add(newFace);
j += 1;
}
newFace.v1 = numTotalVerts - j - 1;
newFace.v2 = i;
newFace.v3 = i + 1;
faces.Add(newFace);
}
if (!hasProfileCut)
{
int i = numOuterVerts - 1;
if (hollowAngles.angles[0].angle - angles.angles[i].angle < angles.angles[i].angle - hollowAngles.angles[maxJ].angle + 0.000001f)
{
newFace.v1 = 0;
newFace.v2 = numTotalVerts - 1;
newFace.v3 = numTotalVerts - maxJ - 1;
faces.Add(newFace);
}
newFace.v1 = numTotalVerts - maxJ - 1;
newFace.v2 = i;
newFace.v3 = 0;
faces.Add(newFace);
}
}
}
}
else if (createFaces)
{
if (simpleFace)
{
if (sides == 3)
faces.Add(new Face(0, 1, 2));
else if (sides == 4)
{
faces.Add(new Face(0, 1, 2));
faces.Add(new Face(0, 2, 3));
}
}
else
{
for (int i = 1; i < numAngles ; i++)
{
Face newFace = new Face();
newFace.v1 = 0;
newFace.v2 = i;
newFace.v3 = i + 1;
faces.Add(newFace);
}
if (!hasProfileCut)
{
Face newFace = new Face();
newFace.v1 = 0;
newFace.v2 = numAngles;
newFace.v3 = 1;
faces.Add(newFace);
}
}
}
hollowCoords = null;
}
public Profile Copy()
{
return Copy(true);
}
public Profile Copy(bool needFaces)
{
Profile copy = new Profile();
copy.coords.AddRange(coords);
if (needFaces)
copy.faces.AddRange(faces);
copy.numOuterVerts = numOuterVerts;
copy.numHollowVerts = numHollowVerts;
return copy;
}
public void AddPos(Coord v)
{
this.AddPos(v.X, v.Y, v.Z);
}
public void AddPos(float x, float y, float z)
{
int i;
int numVerts = coords.Count;
Coord vert;
for (i = 0; i < numVerts; i++)
{
vert = coords[i];
vert.X += x;
vert.Y += y;
vert.Z += z;
this.coords[i] = vert;
}
}
public void AddRot(Quat q)
{
int i;
int numVerts = coords.Count;
for (i = 0; i < numVerts; i++)
coords[i] *= q;
}
public void Scale(float x, float y)
{
int i;
int numVerts = coords.Count;
Coord vert;
for (i = 0; i < numVerts; i++)
{
vert = coords[i];
vert.X *= x;
vert.X = (float)Math.Round(vert.X,5);
vert.Y *= y;
vert.Y = (float)Math.Round(vert.Y,5);
coords[i] = vert;
}
if(x == 0f || y == 0f)
faces = new List<Face>();
}
/// <summary>
/// Changes order of the vertex indices and negates the center vertex normal. Does not alter vertex normals of radial vertices
/// </summary>
public void FlipNormals()
{
int numFaces = faces.Count;
if(numFaces == 0)
return;
int i;
Face tmpFace;
int tmp;
for (i = 0; i < numFaces; i++)
{
tmpFace = faces[i];
tmp = tmpFace.v3;
tmpFace.v3 = tmpFace.v1;
tmpFace.v1 = tmp;
faces[i] = tmpFace;
}
}
public void AddValue2FaceVertexIndices(int num)
{
int numFaces = faces.Count;
if(numFaces == 0)
return;
Face tmpFace;
for (int i = 0; i < numFaces; i++)
{
tmpFace = faces[i];
tmpFace.v1 += num;
tmpFace.v2 += num;
tmpFace.v3 += num;
faces[i] = tmpFace;
}
}
public void DumpRaw(String path, String name, String title)
{
if (path == null)
return;
String fileName = name + "_" + title + ".raw";
String completePath = System.IO.Path.Combine(path, fileName);
StreamWriter sw = new StreamWriter(completePath);
for (int i = 0; i < faces.Count; i++)
{
string s = coords[faces[i].v1].ToString();
s += " " + coords[faces[i].v2].ToString();
s += " " + coords[faces[i].v3].ToString();
sw.WriteLine(s);
}
sw.Close();
}
}
public struct PathNode
{
public Coord position;
public Quat rotation;
public float xScale;
public float yScale;
public float percentOfPath;
}
public enum PathType { Linear = 0, Circular = 1, Flexible = 2 }
public class Path
{
public List<PathNode> pathNodes = new List<PathNode>();
public float twistBegin = 0.0f;
public float twistEnd = 0.0f;
public float topShearX = 0.0f;
public float topShearY = 0.0f;
public float pathCutBegin = 0.0f;
public float pathCutEnd = 1.0f;
public float dimpleBegin = 0.0f;
public float dimpleEnd = 1.0f;
public float skew = 0.0f;
public float holeSizeX = 1.0f; // called pathScaleX in pbs
public float holeSizeY = 0.25f;
public float taperX = 0.0f;
public float taperY = 0.0f;
public float radius = 0.0f;
public float revolutions = 1.0f;
public int stepsPerRevolution = 24;
private const float twoPi = 2.0f * (float)Math.PI;
public void Create(PathType pathType, int steps)
{
if (taperX > .9999f)
taperX = 1.0f;
else if (taperX < -.9999f)
taperX = -1.0f;
if (taperY > .9999f)
taperY = 1.0f;
else if (taperY < -.9999f)
taperY = -1.0f;
if (pathType == PathType.Linear || pathType == PathType.Flexible)
{
int step = 0;
float length = pathCutEnd - pathCutBegin;
float twistTotal = twistEnd - twistBegin;
float twistTotalAbs = Math.Abs(twistTotal);
if (twistTotalAbs > 0.01f)
steps += (int)(twistTotalAbs * 3.66); // dahlia's magic number
float start = -0.5f;
float stepSize = length / (float)steps;
float percentOfPathMultiplier = stepSize * 0.999999f;
float xOffset = topShearX * pathCutBegin;
float yOffset = topShearY * pathCutBegin;
float zOffset = start;
float xOffsetStepIncrement = topShearX * length / steps;
float yOffsetStepIncrement = topShearY * length / steps;
float percentOfPath = pathCutBegin;
zOffset += percentOfPath;
// sanity checks
bool done = false;
while (!done)
{
PathNode newNode = new PathNode();
newNode.xScale = 1.0f;
if (taperX > 0.0f)
newNode.xScale -= percentOfPath * taperX;
else if(taperX < 0.0f)
newNode.xScale += (1.0f - percentOfPath) * taperX;
newNode.yScale = 1.0f;
if (taperY > 0.0f)
newNode.yScale -= percentOfPath * taperY;
else if(taperY < 0.0f)
newNode.yScale += (1.0f - percentOfPath) * taperY;
float twist = twistBegin + twistTotal * percentOfPath;
newNode.rotation = new Quat(new Coord(0.0f, 0.0f, 1.0f), twist);
newNode.position = new Coord(xOffset, yOffset, zOffset);
newNode.percentOfPath = percentOfPath;
pathNodes.Add(newNode);
if (step < steps)
{
step += 1;
percentOfPath += percentOfPathMultiplier;
xOffset += xOffsetStepIncrement;
yOffset += yOffsetStepIncrement;
zOffset += stepSize;
if (percentOfPath > pathCutEnd)
done = true;
}
else done = true;
}
} // end of linear path code
else // pathType == Circular
{
float twistTotal = twistEnd - twistBegin;
// if the profile has a lot of twist, add more layers otherwise the layers may overlap
// and the resulting mesh may be quite inaccurate. This method is arbitrary and doesn't
// accurately match the viewer
float twistTotalAbs = Math.Abs(twistTotal);
if (twistTotalAbs > 0.01f)
{
if (twistTotalAbs > Math.PI * 1.5f)
steps *= 2;
if (twistTotalAbs > Math.PI * 3.0f)
steps *= 2;
}
float yPathScale = holeSizeY * 0.5f;
float pathLength = pathCutEnd - pathCutBegin;
float totalSkew = skew * 2.0f * pathLength;
float skewStart = pathCutBegin * 2.0f * skew - skew;
float xOffsetTopShearXFactor = topShearX * (0.25f + 0.5f * (0.5f - holeSizeY));
float yShearCompensation = 1.0f + Math.Abs(topShearY) * 0.25f;
// It's not quite clear what pushY (Y top shear) does, but subtracting it from the start and end
// angles appears to approximate it's effects on path cut. Likewise, adding it to the angle used
// to calculate the sine for generating the path radius appears to approximate it's effects there
// too, but there are some subtle differences in the radius which are noticeable as the prim size
// increases and it may affect megaprims quite a bit. The effect of the Y top shear parameter on
// the meshes generated with this technique appear nearly identical in shape to the same prims when
// displayed by the viewer.
float startAngle = (twoPi * pathCutBegin * revolutions) - topShearY * 0.9f;
float endAngle = (twoPi * pathCutEnd * revolutions) - topShearY * 0.9f;
float stepSize = twoPi / stepsPerRevolution;
int step = (int)(startAngle / stepSize);
float angle = startAngle;
bool done = false;
while (!done) // loop through the length of the path and add the layers
{
PathNode newNode = new PathNode();
float xProfileScale = (1.0f - Math.Abs(skew)) * holeSizeX;
float yProfileScale = holeSizeY;
float percentOfPath = angle / (twoPi * revolutions);
float percentOfAngles = (angle - startAngle) / (endAngle - startAngle);
if (taperX > 0.01f)
xProfileScale *= 1.0f - percentOfPath * taperX;
else if (taperX < -0.01f)
xProfileScale *= 1.0f + (1.0f - percentOfPath) * taperX;
if (taperY > 0.01f)
yProfileScale *= 1.0f - percentOfPath * taperY;
else if (taperY < -0.01f)
yProfileScale *= 1.0f + (1.0f - percentOfPath) * taperY;
newNode.xScale = xProfileScale;
newNode.yScale = yProfileScale;
float radiusScale = 1.0f;
if (radius > 0.001f)
radiusScale = 1.0f - radius * percentOfPath;
else if (radius < 0.001f)
radiusScale = 1.0f + radius * (1.0f - percentOfPath);
float twist = twistBegin + twistTotal * percentOfPath;
float xOffset = 0.5f * (skewStart + totalSkew * percentOfAngles);
xOffset += (float)Math.Sin(angle) * xOffsetTopShearXFactor;
float yOffset = yShearCompensation * (float)Math.Cos(angle) * (0.5f - yPathScale) * radiusScale;
float zOffset = (float)Math.Sin(angle + topShearY) * (0.5f - yPathScale) * radiusScale;
newNode.position = new Coord(xOffset, yOffset, zOffset);
// now orient the rotation of the profile layer relative to it's position on the path
// adding taperY to the angle used to generate the quat appears to approximate the viewer
newNode.rotation = new Quat(new Coord(1.0f, 0.0f, 0.0f), angle + topShearY);
// next apply twist rotation to the profile layer
if (twistTotal != 0.0f || twistBegin != 0.0f)
newNode.rotation *= new Quat(new Coord(0.0f, 0.0f, 1.0f), twist);
newNode.percentOfPath = percentOfPath;
pathNodes.Add(newNode);
// calculate terms for next iteration
// calculate the angle for the next iteration of the loop
if (angle >= endAngle - 0.01)
done = true;
else
{
step += 1;
angle = stepSize * step;
if (angle > endAngle)
angle = endAngle;
}
}
}
}
}
public class PrimMesh
{
public string errorMessage = "";
private const float twoPi = 2.0f * (float)Math.PI;
public List<Coord> coords;
// public List<Coord> normals;
public List<Face> faces;
private int sides = 4;
private int hollowSides = 4;
private float profileStart = 0.0f;
private float profileEnd = 1.0f;
private float hollow = 0.0f;
public int twistBegin = 0;
public int twistEnd = 0;
public float topShearX = 0.0f;
public float topShearY = 0.0f;
public float pathCutBegin = 0.0f;
public float pathCutEnd = 1.0f;
public float dimpleBegin = 0.0f;
public float dimpleEnd = 1.0f;
public float skew = 0.0f;
public float holeSizeX = 1.0f; // called pathScaleX in pbs
public float holeSizeY = 0.25f;
public float taperX = 0.0f;
public float taperY = 0.0f;
public float radius = 0.0f;
public float revolutions = 1.0f;
public int stepsPerRevolution = 24;
private bool hasProfileCut = false;
private bool hasHollow = false;
public int numPrimFaces = 0;
/// <summary>
/// Human readable string representation of the parameters used to create a mesh.
/// </summary>
/// <returns></returns>
public string ParamsToDisplayString()
{
string s = "";
s += "sides..................: " + this.sides.ToString();
s += "\nhollowSides..........: " + this.hollowSides.ToString();
s += "\nprofileStart.........: " + this.profileStart.ToString();
s += "\nprofileEnd...........: " + this.profileEnd.ToString();
s += "\nhollow...............: " + this.hollow.ToString();
s += "\ntwistBegin...........: " + this.twistBegin.ToString();
s += "\ntwistEnd.............: " + this.twistEnd.ToString();
s += "\ntopShearX............: " + this.topShearX.ToString();
s += "\ntopShearY............: " + this.topShearY.ToString();
s += "\npathCutBegin.........: " + this.pathCutBegin.ToString();
s += "\npathCutEnd...........: " + this.pathCutEnd.ToString();
s += "\ndimpleBegin..........: " + this.dimpleBegin.ToString();
s += "\ndimpleEnd............: " + this.dimpleEnd.ToString();
s += "\nskew.................: " + this.skew.ToString();
s += "\nholeSizeX............: " + this.holeSizeX.ToString();
s += "\nholeSizeY............: " + this.holeSizeY.ToString();
s += "\ntaperX...............: " + this.taperX.ToString();
s += "\ntaperY...............: " + this.taperY.ToString();
s += "\nradius...............: " + this.radius.ToString();
s += "\nrevolutions..........: " + this.revolutions.ToString();
s += "\nstepsPerRevolution...: " + this.stepsPerRevolution.ToString();
s += "\nhasProfileCut........: " + this.hasProfileCut.ToString();
s += "\nhasHollow............: " + this.hasHollow.ToString();
return s;
}
public bool HasProfileCut
{
get { return hasProfileCut; }
set { hasProfileCut = value; }
}
public bool HasHollow
{
get { return hasHollow; }
}
/// <summary>
/// Constructs a PrimMesh object and creates the profile for extrusion.
/// </summary>
/// <param name="sides"></param>
/// <param name="profileStart"></param>
/// <param name="profileEnd"></param>
/// <param name="hollow"></param>
/// <param name="hollowSides"></param>
/// <param name="sphereMode"></param>
public PrimMesh(int _sides, float _profileStart, float _profileEnd, float _hollow, int _hollowSides)
{
coords = new List<Coord>();
faces = new List<Face>();
sides = _sides;
profileStart = _profileStart;
profileEnd = _profileEnd;
hollow = _hollow;
hollowSides = _hollowSides;
if (sides < 3)
sides = 3;
if (hollowSides < 3)
hollowSides = 3;
if (profileStart < 0.0f)
profileStart = 0.0f;
if (profileEnd > 1.0f)
profileEnd = 1.0f;
if (profileEnd < 0.02f)
profileEnd = 0.02f;
if (profileStart >= profileEnd)
profileStart = profileEnd - 0.02f;
if (hollow > 0.99f)
hollow = 0.99f;
if (hollow < 0.0f)
hollow = 0.0f;
}
/// <summary>
/// Extrudes a profile along a path.
/// </summary>
public void Extrude(PathType pathType)
{
bool needEndFaces = false;
coords = new List<Coord>();
faces = new List<Face>();
int steps = 1;
float length = pathCutEnd - pathCutBegin;
hasProfileCut = this.profileEnd - this.profileStart < 0.9999f;
hasHollow = (this.hollow > 0.001f);
float twistBegin = this.twistBegin / 360.0f * twoPi;
float twistEnd = this.twistEnd / 360.0f * twoPi;
float twistTotal = twistEnd - twistBegin;
float twistTotalAbs = Math.Abs(twistTotal);
if (twistTotalAbs > 0.01f)
steps += (int)(twistTotalAbs * 3.66); // dahlia's magic number
float hollow = this.hollow;
float initialProfileRot = 0.0f;
if (pathType == PathType.Circular)
{
needEndFaces = false;
if (pathCutBegin != 0.0f || pathCutEnd != 1.0f)
needEndFaces = true;
else if (taperX != 0.0f || taperY != 0.0f)
needEndFaces = true;
else if (skew != 0.0f)
needEndFaces = true;
else if (twistTotal != 0.0f)
needEndFaces = true;
else if (radius != 0.0f)
needEndFaces = true;
}
else needEndFaces = true;
if (pathType == PathType.Circular)
{
if (sides == 3)
{
initialProfileRot = (float)Math.PI;
if (hollowSides == 4)
{
if (hollow > 0.7f)
hollow = 0.7f;
hollow *= 0.707f;
}
else hollow *= 0.5f;
}
else if (sides == 4)
{
initialProfileRot = 0.25f * (float)Math.PI;
if (hollowSides != 4)
hollow *= 0.707f;
}
else if (sides > 4)
{
initialProfileRot = (float)Math.PI;
if (hollowSides == 4)
{
if (hollow > 0.7f)
hollow = 0.7f;
hollow /= 0.7f;
}
}
}
else
{
if (sides == 3)
{
if (hollowSides == 4)
{
if (hollow > 0.7f)
hollow = 0.7f;
hollow *= 0.707f;
}
else hollow *= 0.5f;
}
else if (sides == 4)
{
initialProfileRot = 1.25f * (float)Math.PI;
if (hollowSides != 4)
hollow *= 0.707f;
}
else if (sides == 24 && hollowSides == 4)
hollow *= 1.414f;
}
Profile profile = new Profile(sides, profileStart, profileEnd, hollow, hollowSides,
HasProfileCut,true);
errorMessage = profile.errorMessage;
numPrimFaces = profile.numPrimFaces;
if (initialProfileRot != 0.0f)
{
profile.AddRot(new Quat(new Coord(0.0f, 0.0f, 1.0f), initialProfileRot));
}
Path path = new Path();
path.twistBegin = twistBegin;
path.twistEnd = twistEnd;
path.topShearX = topShearX;
path.topShearY = topShearY;
path.pathCutBegin = pathCutBegin;
path.pathCutEnd = pathCutEnd;
path.dimpleBegin = dimpleBegin;
path.dimpleEnd = dimpleEnd;
path.skew = skew;
path.holeSizeX = holeSizeX;
path.holeSizeY = holeSizeY;
path.taperX = taperX;
path.taperY = taperY;
path.radius = radius;
path.revolutions = revolutions;
path.stepsPerRevolution = stepsPerRevolution;
path.Create(pathType, steps);
int lastNode = path.pathNodes.Count - 1;
for (int nodeIndex = 0; nodeIndex < path.pathNodes.Count; nodeIndex++)
{
PathNode node = path.pathNodes[nodeIndex];
Profile newLayer = profile.Copy();
newLayer.Scale(node.xScale, node.yScale);
newLayer.AddRot(node.rotation);
newLayer.AddPos(node.position);
// append this layer
int coordsStart = coords.Count;
coords.AddRange(newLayer.coords);
if (needEndFaces && nodeIndex == 0 && newLayer.faces.Count > 0)
{
newLayer.AddValue2FaceVertexIndices(coordsStart);
newLayer.FlipNormals();
faces.AddRange(newLayer.faces);
}
// fill faces between layers
List<Face> linkfaces = new List<Face>();
int numVerts = newLayer.coords.Count;
Face newFace1 = new Face();
Face newFace2 = new Face();
if (nodeIndex > 0)
{
int startVert = coordsStart;
int endVert = coords.Count;
if (!hasProfileCut)
{
if(numVerts > 5 && !hasHollow)
startVert++;
int i = startVert;
for (int l = 0; l < profile.numOuterVerts - 1; l++)
{
newFace1.v1 = i;
newFace1.v2 = i - numVerts;
newFace1.v3 = i + 1;
linkfaces.Add(newFace1);
newFace2.v1 = i + 1;
newFace2.v2 = i - numVerts;
newFace2.v3 = i + 1 - numVerts;
linkfaces.Add(newFace2);
i++;
}
newFace1.v1 = i;
newFace1.v2 = i - numVerts;
newFace1.v3 = startVert;
linkfaces.Add(newFace1);
newFace2.v1 = startVert;
newFace2.v2 = i - numVerts;
newFace2.v3 = startVert - numVerts;
linkfaces.Add(newFace2);
if (hasHollow)
{
startVert = ++i;
for (int l = 0; l < profile.numHollowVerts - 1; l++)
{
newFace1.v1 = i;
newFace1.v2 = i - numVerts;
newFace1.v3 = i + 1;
linkfaces.Add(newFace1);
newFace2.v1 = i + 1;
newFace2.v2 = i - numVerts;
newFace2.v3 = i + 1 - numVerts;
linkfaces.Add(newFace2);
i++;
}
newFace1.v1 = i;
newFace1.v2 = i - numVerts;
newFace1.v3 = startVert;
linkfaces.Add(newFace1);
newFace2.v1 = startVert;
newFace2.v2 = i - numVerts;
newFace2.v3 = startVert - numVerts;
linkfaces.Add(newFace2);
}
}
else
{
for (int i = startVert; i < endVert; i++)
{
int iNext = i + 1;
if (i == endVert - 1)
iNext = startVert;
newFace1.v1 = i;
newFace1.v2 = i - numVerts;
newFace1.v3 = iNext;
linkfaces.Add(newFace1);
newFace2.v1 = iNext;
newFace2.v2 = i - numVerts;
newFace2.v3 = iNext - numVerts;
linkfaces.Add(newFace2);
}
}
}
if(linkfaces.Count > 0)
faces.AddRange(linkfaces);
if (needEndFaces && nodeIndex == lastNode && newLayer.faces.Count > 0)
{
newLayer.AddValue2FaceVertexIndices(coordsStart);
faces.AddRange(newLayer.faces);
}
} // for (int nodeIndex = 0; nodeIndex < path.pathNodes.Count; nodeIndex++)
// more cleanup will be done at Meshmerizer.cs
}
/// <summary>
/// DEPRICATED - use Extrude(PathType.Linear) instead
/// Extrudes a profile along a straight line path. Used for prim types box, cylinder, and prism.
/// </summary>
///
public void ExtrudeLinear()
{
Extrude(PathType.Linear);
}
/// <summary>
/// DEPRICATED - use Extrude(PathType.Circular) instead
/// Extrude a profile into a circular path prim mesh. Used for prim types torus, tube, and ring.
/// </summary>
///
public void ExtrudeCircular()
{
Extrude(PathType.Circular);
}
private Coord SurfaceNormal(Coord c1, Coord c2, Coord c3)
{
Coord edge1 = new Coord(c2.X - c1.X, c2.Y - c1.Y, c2.Z - c1.Z);
Coord edge2 = new Coord(c3.X - c1.X, c3.Y - c1.Y, c3.Z - c1.Z);
Coord normal = Coord.Cross(edge1, edge2);
normal.Normalize();
return normal;
}
private Coord SurfaceNormal(Face face)
{
return SurfaceNormal(this.coords[face.v1], this.coords[face.v2], this.coords[face.v3]);
}
/// <summary>
/// Calculate the surface normal for a face in the list of faces
/// </summary>
/// <param name="faceIndex"></param>
/// <returns></returns>
public Coord SurfaceNormal(int faceIndex)
{
int numFaces = this.faces.Count;
if (faceIndex < 0 || faceIndex >= numFaces)
throw new Exception("faceIndex out of range");
return SurfaceNormal(this.faces[faceIndex]);
}
/// <summary>
/// Duplicates a PrimMesh object. All object properties are copied by value, including lists.
/// </summary>
/// <returns></returns>
public PrimMesh Copy()
{
PrimMesh copy = new PrimMesh(this.sides, this.profileStart, this.profileEnd, this.hollow, this.hollowSides);
copy.twistBegin = this.twistBegin;
copy.twistEnd = this.twistEnd;
copy.topShearX = this.topShearX;
copy.topShearY = this.topShearY;
copy.pathCutBegin = this.pathCutBegin;
copy.pathCutEnd = this.pathCutEnd;
copy.dimpleBegin = this.dimpleBegin;
copy.dimpleEnd = this.dimpleEnd;
copy.skew = this.skew;
copy.holeSizeX = this.holeSizeX;
copy.holeSizeY = this.holeSizeY;
copy.taperX = this.taperX;
copy.taperY = this.taperY;
copy.radius = this.radius;
copy.revolutions = this.revolutions;
copy.stepsPerRevolution = this.stepsPerRevolution;
copy.numPrimFaces = this.numPrimFaces;
copy.errorMessage = this.errorMessage;
copy.coords = new List<Coord>(this.coords);
copy.faces = new List<Face>(this.faces);
return copy;
}
/// <summary>
/// Adds a value to each XYZ vertex coordinate in the mesh
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
public void AddPos(float x, float y, float z)
{
int i;
int numVerts = this.coords.Count;
Coord vert;
for (i = 0; i < numVerts; i++)
{
vert = this.coords[i];
vert.X += x;
vert.Y += y;
vert.Z += z;
this.coords[i] = vert;
}
}
/// <summary>
/// Rotates the mesh
/// </summary>
/// <param name="q"></param>
public void AddRot(Quat q)
{
int i;
int numVerts = this.coords.Count;
for (i = 0; i < numVerts; i++)
this.coords[i] *= q;
}
#if VERTEX_INDEXER
public VertexIndexer GetVertexIndexer()
{
return null;
}
#endif
/// <summary>
/// Scales the mesh
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
public void Scale(float x, float y, float z)
{
int i;
int numVerts = this.coords.Count;
//Coord vert;
Coord m = new Coord(x, y, z);
for (i = 0; i < numVerts; i++)
this.coords[i] *= m;
}
/// <summary>
/// Dumps the mesh to a Blender compatible "Raw" format file
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
/// <param name="title"></param>
public void DumpRaw(String path, String name, String title)
{
if (path == null)
return;
String fileName = name + "_" + title + ".raw";
String completePath = System.IO.Path.Combine(path, fileName);
StreamWriter sw = new StreamWriter(completePath);
for (int i = 0; i < this.faces.Count; i++)
{
string s = this.coords[this.faces[i].v1].ToString();
s += " " + this.coords[this.faces[i].v2].ToString();
s += " " + this.coords[this.faces[i].v3].ToString();
sw.WriteLine(s);
}
sw.Close();
}
}
}
|