-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathXbimOccShape.cpp
More file actions
666 lines (616 loc) · 24.2 KB
/
Copy pathXbimOccShape.cpp
File metadata and controls
666 lines (616 loc) · 24.2 KB
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
#include "XbimOccShape.h"
#include "XbimFaceSet.h"
#include "XbimShell.h"
#include "XbimSolid.h"
#include "XbimCompound.h"
#include "XbimPoint3DWithTolerance.h"
#include "XbimConvert.h"
#include <BRepCheck_Analyzer.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
#include <Poly_Triangulation.hxx>
#include <TShort_Array1OfShortReal.hxx>
#include <BRep_Tool.hxx>
#include <Poly.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <BRepTools.hxx>
#include <Bnd_Box.hxx>
#include <BRepBndLib.hxx>
#include <gp_Quaternion.hxx>
#include "XbimWire.h"
#include <TopExp.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom_Line.hxx>
#include <BRepTools_WireExplorer.hxx>
#include <BRepBuilderAPI_GTransform.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <Geom_Plane.hxx>
using namespace System::Threading;
using namespace System::Collections::Generic;
namespace Xbim
{
namespace Geometry
{
XbimOccShape::XbimOccShape()
{
}
void XbimOccShape::WriteTriangulation(TextWriter^ textWriter, double tolerance, double deflection, double angle)
{
if (!IsValid) return;
XbimFaceSet^ faces = gcnew XbimFaceSet(this);
if (faces->Count == 0) return;
Monitor::Enter(this);
try
{
BRepMesh_IncrementalMesh incrementalMesh(this, deflection, Standard_False, angle); //triangulate the first time
}
finally
{
Monitor::Exit(this);
}
Dictionary<XbimPoint3DWithTolerance^, size_t>^ pointMap = gcnew Dictionary<XbimPoint3DWithTolerance^, size_t>();
List<List<size_t>^>^ pointLookup = gcnew List<List<size_t>^>(faces->Count);
List<XbimPoint3D>^ points = gcnew List<XbimPoint3D>(faces->Count * 5);;
Dictionary<XbimPoint3DWithTolerance^, size_t>^ normalMap = gcnew Dictionary<XbimPoint3DWithTolerance^, size_t>();
List<List<size_t>^>^ normalLookup = gcnew List<List<size_t>^>(faces->Count);
List<XbimVector3D>^ normals = gcnew List<XbimVector3D>(faces->Count * 4);
List<XbimFace^>^ writtenFaces = gcnew List<XbimFace^>(faces->Count);
//First write out all the vertices
int faceIndex = 0;
int triangleCount = 0;
for each (XbimFace ^ face in faces)
{
TopLoc_Location loc;
const Handle(Poly_Triangulation)& mesh = BRep_Tool::Triangulation(face, loc);
if (mesh.IsNull())
continue;
gp_Trsf transform = loc.Transformation();
gp_Quaternion quaternion = transform.GetRotation();
triangleCount += mesh->NbTriangles();
bool faceReversed = face->IsReversed;
bool isPolygonal = face->IsPolygonal;
pointLookup->Add(gcnew List<size_t>(mesh->NbNodes()));
List<size_t>^ norms;
if (!isPolygonal)
{
Poly::ComputeNormals(mesh); //we need the normals
norms = gcnew List<size_t>(mesh->NbNodes());
for (Standard_Integer i = 1; i <= mesh->NbNodes(); i++) //visit each node (it's 1-based)
{
gp_Dir dir = mesh->Normal(i);
if (faceReversed)
dir.Reverse();
size_t index;
dir = quaternion.Multiply(dir);
XbimPoint3DWithTolerance^ n = gcnew XbimPoint3DWithTolerance(dir.X(), dir.Y(), dir.Z(), tolerance);
if (!normalMap->TryGetValue(n, index))
{
index = normalMap->Count;
normalMap->Add(n, index);
normals->Add(XbimVector3D(dir.X(), dir.Y(), dir.Z()));
}
norms->Add(index);
}
}
else
{
norms = gcnew List<size_t>(1);
size_t index;
XbimPoint3DWithTolerance^ n = gcnew XbimPoint3DWithTolerance(face->Normal.X, face->Normal.Y, face->Normal.Z, tolerance);
if (!normalMap->TryGetValue(n, index))
{
index = normalMap->Count;
normalMap->Add(n, index);
normals->Add(XbimVector3D(n->X, n->Y, n->Z));
}
norms->Add(index);
}
normalLookup->Add(norms);
for (Standard_Integer i = 1; i <= mesh->NbNodes(); i++) //visit each node for vertices
{
gp_XYZ p = mesh->Node(i).XYZ();
transform.Transforms(p);
size_t index;
XbimPoint3DWithTolerance^ pt = gcnew XbimPoint3DWithTolerance(p.X(), p.Y(), p.Z(), tolerance);
if (!pointMap->TryGetValue(pt, index))
{
index = pointMap->Count;
pointMap->Add(pt, index);
points->Add(pt->VertexGeometry);
}
pointLookup[faceIndex]->Add(index);
}
writtenFaces->Add(face);
faceIndex++;
}
// Write out header
textWriter->WriteLine(String::Format("P {0} {1} {2} {3} {4}", 1, points->Count, faces->Count, triangleCount, normals->Count));
//write out vertices and normals
textWriter->Write("V");
for each (XbimPoint3D p in points) textWriter->Write(String::Format(" {0},{1},{2}", p.X, p.Y, p.Z));
textWriter->WriteLine();
textWriter->Write("N");
for each (XbimVector3D n in normals) textWriter->Write(String::Format(" {0},{1},{2}", n.X, n.Y, n.Z));
textWriter->WriteLine();
//now write out the faces
faceIndex = 0;
for each (XbimFace ^ face in writtenFaces)
{
bool isPlanar = face->IsPlanar;
List<size_t>^ norms = normalLookup[faceIndex];
textWriter->Write("T");
List<size_t>^ nodeLookup = pointLookup[faceIndex];
TopLoc_Location loc;
const Handle(Poly_Triangulation)& mesh = BRep_Tool::Triangulation(face, loc);
/*const TColgp_Array1OfPnt & nodes = mesh->Nodes();*/
const Poly_Array1OfTriangle& triangles = mesh->Triangles();
Standard_Integer nbTriangles = mesh->NbTriangles();
bool faceReversed = face->IsReversed;
Standard_Integer t[3];
for (Standard_Integer i = 1; i <= nbTriangles; i++) //add each triangle as a face
{
if (faceReversed) //get nodes in the correct order of triangulation
triangles(i).Get(t[2], t[1], t[0]);
else
triangles(i).Get(t[0], t[1], t[2]);
if (isPlanar)
if (i == 1)
textWriter->Write(String::Format(" {0}/{3},{1},{2}", nodeLookup[t[0] - 1], nodeLookup[t[1] - 1], nodeLookup[t[2] - 1], norms[0]));
else
textWriter->Write(String::Format(" {0},{1},{2}", nodeLookup[t[0] - 1], nodeLookup[t[1] - 1], nodeLookup[t[2] - 1]));
else //need to write every one
textWriter->Write(String::Format(" {0}/{3},{1}/{4},{2}/{5}", nodeLookup[t[0] - 1], nodeLookup[t[1] - 1], nodeLookup[t[2] - 1], norms[t[0] - 1], norms[t[1] - 1], norms[t[2] - 1]));
}
faceIndex++;
textWriter->WriteLine();
}
textWriter->Flush();
GC::KeepAlive(this);
}
void XbimOccShape::WriteTriangulation(IXbimMeshReceiver^ meshReceiver, double tolerance, double deflection, double angle)
{
if (!IsValid) return;
if (meshReceiver == nullptr)
{
try
{
Monitor::Enter(this);
BRepMesh_IncrementalMesh incrementalMesh(this, deflection, Standard_False, angle); //triangulate the first time
}
finally
{
Monitor::Exit(this);
}
return;
}
TopTools_IndexedMapOfShape faceMap;
TopoDS_Shape shape = this; //hold on to it
TopExp::MapShapes(shape, TopAbs_FACE, faceMap);
int faceCount = faceMap.Extent();
if (faceCount == 0) return;
array<bool>^ hasSeams = gcnew array<bool>(faceCount);
for (int f = 0; f < faceMap.Extent(); f++)
{
TopTools_IndexedMapOfShape edgeMap;
TopExp::MapShapes(faceMap(f + 1), TopAbs_EDGE, edgeMap);
hasSeams[f] = false;
//deal with seams
for (Standard_Integer i = 1; i <= edgeMap.Extent(); i++)
{
//find any seams
hasSeams[f] = (BRep_Tool::IsClosed(edgeMap(i)) == Standard_True); //just check a seam once
if (hasSeams[f]) break; //this face has a seam no need to do more
}
}
BRepMesh_IncrementalMesh incrementalMesh(this, deflection, Standard_False, angle); //triangulate the first time
for (int f = 1; f <= faceMap.Extent(); f++)
{
const TopoDS_Face& face = TopoDS::Face(faceMap(f));
int faceId = meshReceiver->AddFace();
bool faceReversed = (face.Orientation() == TopAbs_REVERSED);
TopLoc_Location loc;
const Handle(Poly_Triangulation)& mesh = BRep_Tool::Triangulation(face, loc);
if (mesh.IsNull())
continue;
//check if we have a seam
bool hasSeam = hasSeams[f - 1];
gp_Trsf transform = loc.Transformation();
gp_Quaternion quaternion = transform.GetRotation();
Poly::ComputeNormals(mesh); //we need the normals
if (hasSeam)
{
TColStd_Array1OfReal norms(1, mesh->NbNodes());
for (Standard_Integer i = 1; i <= mesh->NbNodes(); i++) //visit each node
{
gp_Dir dir = mesh->Normal(i);
if (faceReversed)
dir.Reverse();
dir = quaternion.Multiply(dir);
norms.SetValue(i, dir.X());
norms.SetValue(i + 1, dir.Y());
norms.SetValue(i + 2, dir.Z());
}
Dictionary<XbimPoint3DWithTolerance^, int>^ uniquePointsOnFace = gcnew Dictionary<XbimPoint3DWithTolerance^, int>(mesh->NbNodes());
for (Standard_Integer j = 1; j <= mesh->NbNodes(); j++) //visit each node for vertices
{
gp_Pnt p = mesh->Node(j);
XbimPoint3DWithTolerance^ pt = gcnew XbimPoint3DWithTolerance(p.X(), p.Y(), p.Z(), tolerance);
int nodeIndex;
if (uniquePointsOnFace->TryGetValue(pt, nodeIndex)) //we have a duplicate point on face need to smooth the normal
{
//balance the two normals
gp_Vec normalA(norms.Value(nodeIndex), norms.Value(nodeIndex) + 1, norms.Value(nodeIndex) + 2);
gp_Vec normalB(norms.Value(j), norms.Value(j) + 1, norms.Value(j) + 2);
gp_Vec normalBalanced = normalA + normalB;
normalBalanced.Normalize();
norms.SetValue(nodeIndex, normalBalanced.X());
norms.SetValue(nodeIndex + 1, normalBalanced.Y());
norms.SetValue(nodeIndex + 2, normalBalanced.Z());
norms.SetValue(j, normalBalanced.X());
norms.SetValue(j + 1, normalBalanced.Y());
norms.SetValue(j + 2, normalBalanced.Z());
}
else
uniquePointsOnFace->Add(pt, j);
}
//write the nodes
for (Standard_Integer j = 0; j < mesh->NbNodes(); j++) //visit each node for vertices
{
gp_Pnt p = mesh->Node(j + 1);
Standard_Real px = p.X();
Standard_Real py = p.Y();
Standard_Real pz = p.Z();
transform.Transforms(px, py, pz); //transform the point to the right location
gp_Dir dir(norms.Value((j * 3) + 1), norms.Value((j * 3) + 2), norms.Value((j * 3) + 3));
meshReceiver->AddNode(faceId, px, py, pz, dir.X(), dir.Y(), dir.Z()); //add the node to the face
}
}
else //write the nodes
{
for (Standard_Integer j = 0; j < mesh->NbNodes(); j++) //visit each node for vertices
{
gp_Pnt p = mesh->Node(j + 1);
Standard_Real px = p.X();
Standard_Real py = p.Y();
Standard_Real pz = p.Z();
transform.Transforms(px, py, pz); //transform the point to the right location
gp_Dir dir = mesh->Normal(j + 1);
if (faceReversed)
dir.Reverse();
dir = quaternion.Multiply(dir); //rotate the norm to the new location
meshReceiver->AddNode(faceId, px, py, pz, dir.X(), dir.Y(), dir.Z()); //add the node to the face
}
}
Standard_Integer t[3];
const Poly_Array1OfTriangle& triangles = mesh->Triangles();
for (Standard_Integer j = 1; j <= mesh->NbTriangles(); j++) //add each triangle as a face
{
if (faceReversed) //get nodes in the correct order of triangulation
triangles(j).Get(t[2], t[1], t[0]);
else
triangles(j).Get(t[0], t[1], t[2]);
meshReceiver->AddTriangle(faceId, t[0] - 1, t[1] - 1, t[2] - 1);
}
}
GC::KeepAlive(this);
}
void XbimOccShape::WriteIndex(BinaryWriter^ bw, UInt32 index, UInt32 maxInt)
{
if (maxInt <= 0xFF)
bw->Write((unsigned char)index);
else if (maxInt <= 0xFFFF)
bw->Write((UInt16)index);
else
bw->Write(index);
}
void XbimOccShape::WriteTriangulation(BinaryWriter^ binaryWriter, double tolerance, double deflection, double angle)
{
if (!IsValid) return;
TopTools_IndexedMapOfShape faceMap;
TopoDS_Shape shape = this; //hold on to it
TopExp::MapShapes(shape, TopAbs_FACE, faceMap);
int faceCount = faceMap.Extent();
if (faceCount == 0) return;
Dictionary<XbimPoint3DWithTolerance^, int>^ pointMap = gcnew Dictionary<XbimPoint3DWithTolerance^, int>();
List<List<int>^>^ pointLookup = gcnew List<List<int>^>(faceCount);
List<XbimPoint3D>^ points = gcnew List<XbimPoint3D>(faceCount * 3);;
List<List<XbimPackedNormal>^>^ normalLookup = gcnew List<List<XbimPackedNormal>^>(faceCount);
//First write out all the vertices
int faceIndex = 0;
int triangleCount = 0;
List<List<int>^>^ tessellations = gcnew List<List<int>^>(faceCount);
bool isPolyhedron = true;
array<bool>^ hasSeams = gcnew array<bool>(faceCount);
bool foundDegenerate = false;
//we check if the shape is a faceted poltgon, i.e. all faces are planar and all edges are linear, if so then we do not need to use OCC meshing which is general purpose and a little slower than LibMesh
for (int f = 1; f <= faceMap.Extent(); f++)
{
const TopoDS_Face& face = TopoDS::Face(faceMap(f));
Handle(Geom_Plane) plane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(face));
bool isPlane = !plane.IsNull();
//set the seam value to false for default, seams cannot be on planar surfaces
hasSeams[f - 1] = false;
if (!isPlane) isPolyhedron = false; //must be a plane to be a polyhedron
if (isPolyhedron && isPlane) //if the shape is still potentially a polyhedron then check that this planar face has no curves
{
for (TopExp_Explorer edgeExplorer(face, TopAbs_EDGE); edgeExplorer.More(); edgeExplorer.Next())
{
Standard_Real start, end;
Handle(Geom_Curve) c3d = BRep_Tool::Curve(TopoDS::Edge(edgeExplorer.Current()), start, end);
if (!c3d.IsNull())
{
if (c3d->DynamicType() == STANDARD_TYPE(Geom_Line)) //if it is a line all is well skip to next edge
continue;
if (c3d->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve)) //if it is a trimmed curve determine if basis curve is a line
{
//it must be a trimmed curve
Handle(Geom_TrimmedCurve) tc = Handle(Geom_TrimmedCurve)::DownCast(c3d);
//flatten any trimeed curve nesting
while (tc->BasisCurve()->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
tc = Handle(Geom_TrimmedCurve)::DownCast(tc->BasisCurve());
//get the type of the basis curve
Handle(Standard_Type) tcType = tc->BasisCurve()->DynamicType();
//if its a line all is well skip to next edge
if (tcType == STANDARD_TYPE(Geom_Line))
continue;
}
//if here then the shape has curves and we need to use OCC meshing
isPolyhedron = false;
break;
}
}
}
if (!isPlane) //curved surface check for any seams that will need smoothing
{
for (TopExp_Explorer edgeExplorer(face,TopAbs_EDGE); edgeExplorer.More(); edgeExplorer.Next())
{
//find any seams
bool isSeam = (BRep_Tool::IsClosed(edgeExplorer.Current()) == Standard_True);
if (isSeam)
{
hasSeams[f - 1] = true; //just check a seam once
break;
}
}
}
}
if (!isPolyhedron)
BRepMesh_IncrementalMesh incrementalMesh(this, deflection, Standard_False, angle); //triangulate the first time
for (int f = 1; f <= faceMap.Extent(); f++)
{
const TopoDS_Face& face = TopoDS::Face(faceMap(f));
bool faceReversed = (face.Orientation() == TopAbs_REVERSED);
Handle(Geom_Plane) plane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(face));
bool isPlanar = !plane.IsNull();
//bool isFaceWithCurve = isCurveFace[f - 1];
List<XbimPackedNormal>^ norms;
Tess^ tess = gcnew Tess();
if (!isPolyhedron)
{
TopLoc_Location loc;
const Handle(Poly_Triangulation)& mesh = BRep_Tool::Triangulation(face, loc);
if (mesh.IsNull())
continue;
//check if we have a seam
bool hasSeam = hasSeams[f - 1];
gp_Trsf transform = loc.Transformation();
gp_Quaternion quaternion = transform.GetRotation();
triangleCount += mesh->NbTriangles();
pointLookup->Add(gcnew List<int>(mesh->NbNodes()));
if (!isPlanar)
{
Poly::ComputeNormals(mesh); //we need the normals
norms = gcnew List<XbimPackedNormal>(mesh->NbNodes());
for (Standard_Integer i = 1; i <= mesh->NbNodes(); i ++) //visit each node
{
gp_Dir dir = mesh->Normal(i);
if (faceReversed)
dir.Reverse();
dir = quaternion.Multiply(dir);
XbimPackedNormal packedNormal = XbimPackedNormal(dir.X(), dir.Y(), dir.Z());
norms->Add(packedNormal);
}
normalLookup->Add(norms);
}
else //just need one normal
{
norms = gcnew List<XbimPackedNormal>(1);
gp_Dir faceNormal = faceReversed ? plane->Axis().Direction().Reversed() : plane->Axis().Direction();
XbimPackedNormal packedNormal = XbimPackedNormal(faceNormal.X(), faceNormal.Y(), faceNormal.Z());
norms->Add(packedNormal);
normalLookup->Add(norms);
}
Dictionary<XbimPoint3DWithTolerance^, int>^ uniquePointsOnFace = nullptr;
for (Standard_Integer j = 1; j <= mesh->NbNodes(); j++) //visit each node for vertices
{
gp_XYZ p = mesh->Node(j).XYZ();
transform.Transforms(p);
int index;
XbimPoint3DWithTolerance^ pt = gcnew XbimPoint3DWithTolerance(p.X(), p.Y(), p.Z(), tolerance);
if (!pointMap->TryGetValue(pt, index))
{
index = points->Count;
pointMap->Add(pt, index);
points->Add(pt->VertexGeometry);
}
pointLookup[faceIndex]->Add(index);
if (hasSeam) //keep a record of duplicate points on face triangulation so we can average the normals
{
if (uniquePointsOnFace == nullptr) uniquePointsOnFace = gcnew Dictionary<XbimPoint3DWithTolerance^, int>(mesh->NbNodes());
int nodeIndex;
if (uniquePointsOnFace->TryGetValue(pt, nodeIndex)) //we have a duplicate point on face need to smooth the normal
{
//balance the two normals
XbimPackedNormal normalA = norms[nodeIndex - 1];
XbimPackedNormal normalB = norms[j - 1];
XbimVector3D vec = normalA.Normal + normalB.Normal;
vec = vec.Normalized();
XbimPackedNormal normalBalanced = XbimPackedNormal(vec);
norms[nodeIndex - 1] = normalBalanced;
norms[j - 1] = normalBalanced;
}
else
uniquePointsOnFace->Add(pt, j);
}
}
Standard_Integer t[3];
const Poly_Array1OfTriangle& triangles = mesh->Triangles();
List<int>^ elems = gcnew List<int>(mesh->NbTriangles() * 3);
for (Standard_Integer j = 1; j <= mesh->NbTriangles(); j++) //add each triangle as a face
{
if (faceReversed) //get nodes in the correct order of triangulation
triangles(j).Get(t[2], t[1], t[0]);
else
triangles(j).Get(t[0], t[1], t[2]);
elems->Add(t[0] - 1);
elems->Add(t[1] - 1);
elems->Add(t[2] - 1);
}
tessellations->Add(elems);
faceIndex++;
}
else //it is planar we can use LibMeshDotNet
{
//need to consider whoch side is front annd back
gp_Dir faceNormal = faceReversed ? plane->Axis().Direction().Reversed() : plane->Axis().Direction();
XbimPackedNormal packedNormal = XbimPackedNormal(faceNormal.X(), faceNormal.Y(), faceNormal.Z());
norms = gcnew List<XbimPackedNormal>(1);
TopTools_IndexedMapOfShape wireMap;
TopExp::MapShapes(face, TopAbs_WIRE, wireMap);
if (wireMap.Extent() == 1) //just one loop, it is the outer
{
TopoDS_Wire outerWire = TopoDS::Wire(wireMap(1));
int numberEdges = outerWire.NbChildren();
if (numberEdges > 2)
{
array<ContourVertex>^ outerContour = gcnew array<ContourVertex>(numberEdges);
BRepTools_WireExplorer exp(outerWire, face);
int j = 0;
for (j = 0; exp.More(); exp.Next())
{
gp_Pnt p = BRep_Tool::Pnt(exp.CurrentVertex());
outerContour[j].Position.X = p.X();
outerContour[j].Position.Y = p.Y();
outerContour[j].Position.Z = p.Z();
j++;
}
if (j == numberEdges)
{
tess->AddContour(outerContour); //the original winding is correct as we have oriented the wire to the face in BRepTools_WireExplorer
}
else
{
foundDegenerate = true;
}
}
}
else
{
for (int i = 1; i <= wireMap.Extent(); i++)
{
TopoDS_Wire ccWire = TopoDS::Wire(wireMap(i));
int numberOfEdges = ccWire.NbChildren();
if (numberOfEdges > 2)
{
array<ContourVertex>^ contour = gcnew array<ContourVertex>(numberOfEdges);
BRepTools_WireExplorer exp(ccWire, face);
for (int j = 0; exp.More(); exp.Next())
{
gp_Pnt p = BRep_Tool::Pnt(exp.CurrentVertex());
contour[j].Position.X = p.X();
contour[j].Position.Y = p.Y();
contour[j].Position.Z = p.Z();
j++;
}
tess->AddContour(contour); //the original winding is correct as we have oriented the wire to the face in BRepTools_WireExplorer
}
}
}
if (foundDegenerate)
{
foundDegenerate = false;
continue;
}
tess->Tessellate(Xbim::Tessellator::WindingRule::EvenOdd, Xbim::Tessellator::ElementType::Polygons, 3);
if (tess->ElementCount > 0) //we have some triangles
{
int numTriangles = tess->ElementCount;
triangleCount += numTriangles;
array<ContourVertex>^ contourVerts = tess->Vertices;
array<int>^ elements = tess->Elements;
pointLookup->Add(gcnew List<int>(tess->VertexCount));
/*System::Diagnostics::Debug::Assert(Math::Abs(nor[0] - faceNormal.X()) < 1e-3);
System::Diagnostics::Debug::Assert(Math::Abs(nor[1] - faceNormal.Y()) < 1e-3);
System::Diagnostics::Debug::Assert(Math::Abs(nor[2] - faceNormal.Z()) < 1e-3);*/
norms->Add(packedNormal);
normalLookup->Add(norms);
for (int i = 0; i < tess->VertexCount; i++) //visit each node for vertices
{
Vec3 p = contourVerts[i].Position;
int index;
XbimPoint3DWithTolerance^ pt = gcnew XbimPoint3DWithTolerance(p.X, p.Y, p.Z, tolerance);
if (!pointMap->TryGetValue(pt, index))
{
index = points->Count;
pointMap->Add(pt, index);
points->Add(pt->VertexGeometry);
}
pointLookup[faceIndex]->Add(index);
}
List<int>^ elems = gcnew List<int>(numTriangles * 3);
for (int j = 0; j < numTriangles; j++)
{
elems->Add(elements[j * 3]);
elems->Add(elements[j * 3 + 1]);
elems->Add(elements[j * 3 + 2]);
}
tessellations->Add(elems);
faceIndex++;
}
}
}
// Write out header
binaryWriter->Write((unsigned char)1); //stream format version
int numVertices = points->Count;
binaryWriter->Write((UInt32)numVertices); //number of vertices
binaryWriter->Write((UInt32)triangleCount); //number of triangles
//write out vertices
for each (XbimPoint3D p in points)
{
binaryWriter->Write((float)p.X);
binaryWriter->Write((float)p.Y);
binaryWriter->Write((float)p.Z);
}
//now write out the faces
faceIndex = 0;
binaryWriter->Write((Int32)tessellations->Count);
for each (List<int> ^ tess in tessellations)
{
List<XbimPackedNormal>^ norms = normalLookup[faceIndex];
bool isPlanar = norms->Count == 1;
List<int>^ nodeLookup = pointLookup[faceIndex];
if (isPlanar)
{
binaryWriter->Write((Int32)tess->Count / 3);
norms[0].Write(binaryWriter); //write the normal for the face
}
else
binaryWriter->Write((Int32)(-tess->Count / 3)); //use negative count to indicate that every index has a normal
for (int i = 0; i < tess->Count; i++)
{
if (isPlanar)
{
WriteIndex(binaryWriter, nodeLookup[tess[i]], numVertices);
}
else //need to write every one
{
WriteIndex(binaryWriter, nodeLookup[tess[i]], numVertices);
norms[tess[i]].Write(binaryWriter);
}
}
faceIndex++;
}
GC::KeepAlive(this);
binaryWriter->Flush();
}
}
}