forked from tpaviot/pythonocc-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesselator.cpp
More file actions
958 lines (829 loc) · 32.5 KB
/
Tesselator.cpp
File metadata and controls
958 lines (829 loc) · 32.5 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
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
// Copyright 2011 Fotios Sioutis (sfotis@gmail.com)
//
//This file is part of pythonOCC.
//
//pythonOCC is free software: you can redistribute it and/or modify
//it under the terms of the GNU Lesser General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//pythonOCC is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public License
//along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
//---------------------------------------------------------------------------
#include "Tesselator.h"
#include <sstream>
//---------------------------------------------------------------------------
#include <TopExp_Explorer.hxx>
#include <Bnd_Box.hxx>
#include <StdPrs_ToolShadedShape.hxx>
#include <BRepMesh.hxx>
#include <Poly_Connect.hxx>
#include <Poly_Triangulation.hxx>
#include <Poly_PolygonOnTriangulation.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <TColgp_Array1OfDir.hxx>
#include <TColgp_Array1OfPnt2d.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopExp.hxx>
#include <BRepTools.hxx>
#include <BRepBndLib.hxx>
//---------------------------------------------------------------------------
Tesselator::Tesselator(TopoDS_Shape aShape,
theTextureMappingRule aTxtMapType,
Standard_Real anAutoScaleSizeOnU,
Standard_Real anAutoScaleSizeOnV,
Standard_Real aDeviation,
Standard_Real aUOrigin,
Standard_Real aVOrigin,
Standard_Real aURepeat,
Standard_Real aVRepeat,
Standard_Real aScaleU,
Standard_Real aScaleV,
Standard_Real aRotationAngle) :
//set local variables
myDeviation(aDeviation),
myUOrigin(aUOrigin),
myVOrigin(aVOrigin),
myURepeat(aURepeat),
myVRepeat(aVRepeat),
myScaleU(aScaleU),
myScaleV(aScaleV),
myAutoScaleSizeOnU(anAutoScaleSizeOnU),
myAutoScaleSizeOnV(anAutoScaleSizeOnV),
myTxtMapType(aTxtMapType),
myShape(aShape),
myRotationAngle(aRotationAngle)
{
//prepare bbox tex coords
if (myTxtMapType == atCube) {
//create a compound for bbox
//prepare bbox tex coords
PrepareBoxTextureCoordinates(myShape);
}
locVertexcoord = NULL;
locNormalcoord = NULL;
locTexcoord = NULL;
// compute default deviation
ComputeDefaultDeviation();
//Tesselate();
TesselateWithUVCoords();
}
Tesselator::Tesselator(TopoDS_Shape aShape) :
//set local variables
myDeviation(1.0),
myUOrigin(0.),
myVOrigin(0.),
myURepeat(1.),
myVRepeat(1.),
myScaleU(1.),
myScaleV(1.),
myAutoScaleSizeOnU(1.),
myAutoScaleSizeOnV(1.),
myTxtMapType(atNormal),
myShape(aShape),
myRotationAngle(0.)
{
locVertexcoord = NULL;
locNormalcoord = NULL;
locTexcoord = NULL;
// compute default deviation
ComputeDefaultDeviation();
TesselateWithUVCoords();
}
//---------------------------------------------------------------------------
Tesselator::~Tesselator()
{
if (locVertexcoord)
delete [] locVertexcoord;
if (locNormalcoord)
delete [] locNormalcoord;
if (locTexcoord)
delete [] locTexcoord;
for (std::vector<aedge*>::iterator edgeit = edgelist.begin(); edgeit != edgelist.end(); ++edgeit) {
aedge* edge = *edgeit;
if (edge) {
if (edge->vertex_coord)
delete[] edge->vertex_coord;
delete edge;
*edgeit = NULL;
}
}
edgelist.clear();
}
//---------------------------------------------------------------------------
void Tesselator::SetDeviation(Standard_Real aDeviation)
{
myDeviation = aDeviation;
}
//---------------------------------------------------------------------------
void Tesselator::Tesselate()
{
Standard_Real Umin;
Standard_Real Umax;
Standard_Real Vmin;
Standard_Real Vmax;
Standard_Real dUmax;
Standard_Real dVmax;
TopExp_Explorer ExpFace;
StdPrs_ToolShadedShape SST;
gp_Dir d;
gp_Pnt p;
gp_Vec2d theCoord_p;
gp_Pnt2d d_coord;
//Triangulate
BRepMesh::Mesh(myShape, myDeviation);
for (ExpFace.Init(myShape, TopAbs_FACE); ExpFace.More(); ExpFace.Next()) {
const TopoDS_Face& myFace = TopoDS::Face(ExpFace.Current());
TopLoc_Location aLocation;
Handle(Poly_Triangulation) myT = BRep_Tool::Triangulation(myFace, aLocation);
if (!myT.IsNull()) {
Poly_Connect pc(myT);
aface *this_face = new aface;
//write vertex buffer
const TColgp_Array1OfPnt& Nodes = myT->Nodes();
this_face->vertex_coord = new float[Nodes.Length() * 3];
this_face->number_of_coords = Nodes.Length();
for (int i = Nodes.Lower(); i <= Nodes.Upper(); i++) {
p = Nodes(i).Transformed(aLocation.Transformation());
this_face->vertex_coord[((i-1) * 3)+ 0] = p.X();
this_face->vertex_coord[((i-1) * 3)+ 1] = p.Y();
this_face->vertex_coord[((i-1) * 3)+ 2] = p.Z();
}
//write normal buffer
TColgp_Array1OfDir myNormal(Nodes.Lower(), Nodes.Upper());
SST.Normal(myFace, pc, myNormal);
this_face->normal_coord = new float[myNormal.Length() * 3];
this_face->number_of_normals = myNormal.Length();
for (int i = myNormal.Lower(); i <= myNormal.Upper(); i++) {
d = myNormal(i).Transformed(aLocation.Transformation());
this_face->normal_coord[((i-1) * 3)+ 0] = d.X();
this_face->normal_coord[((i-1) * 3)+ 1] = d.Y();
this_face->normal_coord[((i-1) * 3)+ 2] = d.Z();
}
// set uvcoords buffers to NULL
// necessary for JoinPrimitive to be performed
this_face->tex_coord = NULL;
this_face->number_of_texcoords = 0;
//write triangle buffer
Standard_Integer validFaceTriCount = 0;
Standard_Integer n1 , n2 , n3;
TopAbs_Orientation orient = myFace.Orientation();
const Poly_Array1OfTriangle& triangles = myT->Triangles();
this_face->tri_indexes = new int [triangles.Length()* 3];
for (int nt = 1; nt <= myT->NbTriangles(); nt++) {
triangles(nt).Get(n1,n2,n3);
if (orient != TopAbs_FORWARD) {
Standard_Integer tmp=n1;
n1 = n2;
n2 = tmp;
}
if (TriangleIsValid(Nodes(n1),Nodes(n2),Nodes(n3))) {
this_face->tri_indexes[(validFaceTriCount * 3)+ 0] = n1;
this_face->tri_indexes[(validFaceTriCount * 3)+ 1] = n2;
this_face->tri_indexes[(validFaceTriCount * 3)+ 2] = n3;
validFaceTriCount++;
}
}
this_face->number_of_triangles = validFaceTriCount;
facelist.push_back(this_face);
}
}
JoinPrimitives();
ComputeEdges();
}
//---------------------------------------------------------------------------
void Tesselator::TesselateWithUVCoords()
{
Standard_Real Umin;
Standard_Real Umax;
Standard_Real Vmin;
Standard_Real Vmax;
Standard_Real dUmax;
Standard_Real dVmax;
TopExp_Explorer ExpFace;
StdPrs_ToolShadedShape SST;
gp_Dir d;
gp_Pnt p;
gp_Vec2d theCoord_p;
gp_Pnt2d d_coord;
//Triangulate
BRepMesh::Mesh(myShape, myDeviation);
for (ExpFace.Init(myShape, TopAbs_FACE); ExpFace.More(); ExpFace.Next()) {
const TopoDS_Face& myFace = TopoDS::Face(ExpFace.Current());
TopLoc_Location aLocation;
Handle(Poly_Triangulation) myT = BRep_Tool::Triangulation(myFace, aLocation);
if (!myT.IsNull()) {
Poly_Connect pc(myT);
aface *this_face = new aface;
//write vertex buffer
const TColgp_Array1OfPnt& Nodes = myT->Nodes();
this_face->vertex_coord = new float[Nodes.Length() * 3];
this_face->number_of_coords = Nodes.Length();
for (int i = Nodes.Lower(); i <= Nodes.Upper(); i++) {
p = Nodes(i).Transformed(aLocation.Transformation());
this_face->vertex_coord[((i-1) * 3)+ 0] = p.X();
this_face->vertex_coord[((i-1) * 3)+ 1] = p.Y();
this_face->vertex_coord[((i-1) * 3)+ 2] = p.Z();
}
//write normal buffer
TColgp_Array1OfDir myNormal(Nodes.Lower(), Nodes.Upper());
SST.Normal(myFace, pc, myNormal);
this_face->normal_coord = new float[myNormal.Length() * 3];
this_face->number_of_normals = myNormal.Length();
for (int i = myNormal.Lower(); i <= myNormal.Upper(); i++) {
d = myNormal(i).Transformed(aLocation.Transformation());
this_face->normal_coord[((i-1) * 3)+ 0] = d.X();
this_face->normal_coord[((i-1) * 3)+ 1] = d.Y();
this_face->normal_coord[((i-1) * 3)+ 2] = d.Z();
}
//write uvcoord buffer
int id1 = 0, id2 = 2, idNull = 1;
if (myTxtMapType == atNormal || myTxtMapType == atNormalAutoScale) {
BRepTools::UVBounds(myFace,Umin, Umax, Vmin, Vmax);
dUmax = (Umax - Umin);
dVmax = (Vmax - Vmin);
if (myTxtMapType == atNormalAutoScale) {
myScaleU = myAutoScaleSizeOnU/dUmax;
myScaleV = myAutoScaleSizeOnV/dVmax;
}
}
const TColgp_Array1OfPnt2d& UVNodes = myT->UVNodes();
this_face->tex_coord = new float[UVNodes.Length() * 3];
this_face->number_of_texcoords = UVNodes.Length();
for (int i = UVNodes.Lower(); i <= UVNodes.Upper();i++) {
if (myTxtMapType == atCube) {
GetBoxTextureCoordinate( Nodes(i).Transformed(aLocation.Transformation()),
myNormal(i).Transformed(aLocation.Transformation()),
theCoord_p);
d_coord.SetX((-myUOrigin+(myURepeat*theCoord_p.X())/aBndBoxSz)/myScaleU);
d_coord.SetY((-myVOrigin+(myVRepeat*theCoord_p.Y())/aBndBoxSz)/myScaleV);
}
else {
d_coord = UVNodes(i);
d_coord.SetX((-myUOrigin+(myURepeat*(d_coord.X()-Umin))/dUmax)/myScaleU);
d_coord.SetY((-myVOrigin+(myVRepeat*(d_coord.Y()-Vmin))/dVmax)/myScaleV);
}
d_coord.Rotate(gp::Origin2d(), myRotationAngle);
this_face->tex_coord[((i-1) * 3)+ id1] = d_coord.X();
this_face->tex_coord[((i-1) * 3)+ id2] = d_coord.Y();
this_face->tex_coord[((i-1) * 3)+ idNull] = 0;
}
//write triangle buffer
Standard_Integer validFaceTriCount = 0;
Standard_Integer n1 , n2 , n3;
TopAbs_Orientation orient = myFace.Orientation();
const Poly_Array1OfTriangle& triangles = myT->Triangles();
this_face->tri_indexes = new int [triangles.Length()* 3];
for (int nt = 1; nt <= myT->NbTriangles(); nt++) {
triangles(nt).Get(n1,n2,n3);
if (orient != TopAbs_FORWARD) {
Standard_Integer tmp=n1;
n1 = n2;
n2 = tmp;
}
if (TriangleIsValid(Nodes(n1),Nodes(n2),Nodes(n3))) {
this_face->tri_indexes[(validFaceTriCount * 3)+ 0] = n1;
this_face->tri_indexes[(validFaceTriCount * 3)+ 1] = n2;
this_face->tri_indexes[(validFaceTriCount * 3)+ 2] = n3;
validFaceTriCount++;
}
}
this_face->number_of_triangles = validFaceTriCount;
facelist.push_back(this_face);
}
}
JoinPrimitivesWithUVCoords();
ComputeEdges();
}
//---------------------------INTERFACE---------------------------------------
void Tesselator::ComputeDefaultDeviation()
{
// This method automatically computes precision from the bounding box of the shape
Bnd_Box aBox;
Standard_Real aXmin,aYmin ,aZmin ,aXmax ,aYmax ,aZmax;
//calculate the bounding box
BRepBndLib::Add(myShape, aBox);
aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
Standard_Real xDim = abs((long)aXmax - (long)aXmin);
Standard_Real yDim = abs((long)aYmax - (long)aYmin);
Standard_Real zDim = abs((long)aZmax - (long)aZmin);
Standard_Real adeviation = std::max(aXmax-aXmin, std::max(aYmax-aYmin, aZmax-aZmin)) * 1e-2 ;
myDeviation = adeviation;
}
void Tesselator::ComputeEdges()
{
TopLoc_Location aTrsf;
// clear current data
std::vector<aedge*>::iterator it;
for (it = edgelist.begin(); it != edgelist.end(); ++it) {
if (*it) {
if ((*it)->vertex_coord)
delete[] (*it)->vertex_coord;
delete *it;
*it = NULL;
}
}
edgelist.clear();
// explore all boundary edges
TopTools_IndexedDataMapOfShapeListOfShape edgeMap;
TopExp::MapShapesAndAncestors (myShape, TopAbs_EDGE, TopAbs_FACE, edgeMap);
for (int iEdge = 1 ; iEdge <= edgeMap.Extent (); iEdge++) {
// reject free edges
const TopTools_ListOfShape& faceList = edgeMap.FindFromIndex (iEdge);
if (faceList.Extent() == 0) {
continue;
}
// take one of the shared edges and get edge triangulation
const TopoDS_Face& aFace = TopoDS::Face (faceList.First ());
const TopoDS_Edge& anEdge = TopoDS::Edge (edgeMap.FindKey (iEdge));
Handle(Poly_Triangulation) trian = BRep_Tool::Triangulation (aFace, aTrsf);
if (trian.IsNull ())
continue;
Handle(Poly_PolygonOnTriangulation) anEdgePoly =
BRep_Tool::PolygonOnTriangulation (anEdge, trian, aTrsf);
if (anEdgePoly.IsNull ()) {
continue;
}
// get edge vertex indexes from face triangulation
const TColgp_Array1OfPnt& trainVerts = trian->Nodes ();
const TColStd_Array1OfInteger& edgeVerts = anEdgePoly->Nodes ();
if (edgeVerts.Length () < 2) {
continue;
}
aedge* theEdge = new aedge;
theEdge->number_of_coords = edgeVerts.Upper () - edgeVerts.Lower() + 1;
theEdge->vertex_coord = new float[theEdge->number_of_coords * 3 * sizeof(float)];
for (int aNodeIdx = edgeVerts.Lower (); aNodeIdx <= edgeVerts.Upper (); aNodeIdx++) {
// node index in face triangulation
int aTriIndex = edgeVerts.Value (aNodeIdx);
// get node and apply location transformation to the node
gp_Pnt aTriNode = trainVerts.Value (aTriIndex);
if (!aTrsf.IsIdentity ()) {
aTriNode.Transform (aTrsf);
}
int locIndex = aNodeIdx - edgeVerts.Lower ();
theEdge->vertex_coord[locIndex*3 + 0] = static_cast<float>(aTriNode.X());
theEdge->vertex_coord[locIndex*3 + 1] = static_cast<float>(aTriNode.Y());
theEdge->vertex_coord[locIndex*3 + 2] = static_cast<float>(aTriNode.Z());
}
edgelist.push_back(theEdge);
}
}
std::string Tesselator::ExportShapeToX3DIndexedFaceSet()
{
std::stringstream str1;
int *vertices_idx = new int[3];
int *texcoords_idx = new int[3];
int *normals_idx = new int[3];
str1 << "<IndexedFaceSet coordIndex='";
for (int i=0;i<tot_triangle_count;i++) {
ObjGetTriangle(i, vertices_idx, texcoords_idx, normals_idx);
// vertex indices
str1 << i*3 << " " << 1+i*3 << " " << 2+i*3 << " -1\n";
}
str1 << "' solid='false'>\n";
// write points coordinates
str1 << "<Coordinate point='";
for (int i=0;i<tot_triangle_count;i++) {
ObjGetTriangle(i, vertices_idx, texcoords_idx, normals_idx);
str1 <<locVertexcoord[vertices_idx[0]]<< " " <<locVertexcoord[vertices_idx[0]+1]<<" "
<< locVertexcoord[vertices_idx[0]+2]<<"\n";
// Second vertex
str1 <<locVertexcoord[vertices_idx[1]]<<" "<<locVertexcoord[vertices_idx[1]+1]<<" "
<< locVertexcoord[vertices_idx[1]+2]<<"\n";
// Third vertex
str1 << locVertexcoord[vertices_idx[2]]<<" "<<locVertexcoord[vertices_idx[2]+1]<<" "
<< locVertexcoord[vertices_idx[2]+2]<<"\n";
}
str1 << "' containerField='coord'></Coordinate>\n";
// write normals
str1 << "<Normal vector='";
for (int i=0;i<tot_triangle_count;i++) {
ObjGetTriangle(i, vertices_idx, texcoords_idx, normals_idx);
// First normal
str1 << locNormalcoord[normals_idx[0]]<<" "<<locNormalcoord[normals_idx[0]+1]<<" "
<< locNormalcoord[normals_idx[0]+2]<<"\n";
// Second normal
str1 << locNormalcoord[normals_idx[1]]<<" "<<locNormalcoord[normals_idx[1]+1]<<" "
<< locNormalcoord[normals_idx[1]+2]<<"\n";
// Third normal
str1 << locNormalcoord[normals_idx[2]]<<" "<<locNormalcoord[normals_idx[2]+1]<<" "
<< locNormalcoord[normals_idx[2]+2] << "\n";
}
str1 << "' containerField='normal'></Normal>\n";
// close all markups
str1 << "</IndexedFaceSet>\n";
delete [] vertices_idx;
delete [] texcoords_idx;
delete [] normals_idx;
return str1.str();
}
void Tesselator::ExportShapeToX3D(char * filename, int diffR, int diffG, int diffB)
{
ofstream X3Dfile;
X3Dfile.open (filename);
// write header
X3Dfile << "<?xml version='1.0' encoding='UTF-8'?>" ;
X3Dfile << "<!DOCTYPE X3D PUBLIC 'ISO//Web3D//DTD X3D 3.1//EN' 'http://www.web3d.org/specifications/x3d-3.1.dtd'>";
X3Dfile << "<X3D>";
X3Dfile << "<Head>";
X3Dfile << "<meta name='generator' content='pythonOCC, http://www.pythonocc.org'/>";
X3Dfile << "</Head>";
X3Dfile << "<Scene><Shape><Appearance><Material DEF='Shape_Mat' diffuseColor='0.65 0.65 0.65' ";
X3Dfile << "shininess='0.9' specularColor='1 1 1'></Material></Appearance>";
// write tesselation
X3Dfile << ExportShapeToX3DIndexedFaceSet();
X3Dfile << "</Shape></Scene></X3D>\n";
X3Dfile.close();
}
void Tesselator::ExportShapeToThreejs(char * filename)
{
ofstream js_file;
int *vertices_idx = new int[3];
int *texcoords_idx = new int[3];
int *normals_idx = new int[3];
js_file.open (filename);
// write header
js_file << "var Shape = function () {\n";
js_file << "var scope = this;\n";
js_file << "THREE.Geometry.call( this );\n";
// first write vertices coords
for (int i=0;i<tot_triangle_count;i++) {
ObjGetTriangle(i, vertices_idx, texcoords_idx, normals_idx);
// First vertex
js_file << "v("<<locVertexcoord[vertices_idx[0]]<<","<<locVertexcoord[vertices_idx[0]+1]<<","
<< locVertexcoord[vertices_idx[0]+2]<<");\n";
// Second vertex
js_file << "v("<<locVertexcoord[vertices_idx[1]]<<","<<locVertexcoord[vertices_idx[1]+1]<<","
<< locVertexcoord[vertices_idx[1]+2]<<");\n";
// Third vertex
js_file << "v("<<locVertexcoord[vertices_idx[2]]<<","<<locVertexcoord[vertices_idx[2]+1]<<","
<< locVertexcoord[vertices_idx[2]+2]<<");\n";
}
// write normals
for (int i=0;i<tot_triangle_count;i++) {
ObjGetTriangle(i, vertices_idx, texcoords_idx, normals_idx);
// vertex indices
js_file << "f3("<<i*3<<","<<1+i*3<<","<<2+i*3<<",";
// First normal
js_file << locNormalcoord[normals_idx[0]]<<","<<locNormalcoord[normals_idx[0]+1]<<","
<< locNormalcoord[normals_idx[0]+2]<<",";
// Second normal
js_file << locNormalcoord[normals_idx[1]]<<","<<locNormalcoord[normals_idx[1]+1]<<","
<< locNormalcoord[normals_idx[1]+2]<<",";
// Third normal
js_file << locNormalcoord[normals_idx[2]]<<","<<locNormalcoord[normals_idx[2]+1]<<","
<< locNormalcoord[normals_idx[2]+2];
js_file << ");\n";
}
// at last, write texcoords
for (int i=0;i<tot_triangle_count;i++) {
ObjGetTriangle(i, vertices_idx, texcoords_idx, normals_idx);
js_file << "uvs(";
js_file << locTexcoord[texcoords_idx[0]]<<","<<locTexcoord[texcoords_idx[0]+2]<<",";
js_file << locTexcoord[texcoords_idx[1]]<<","<<locTexcoord[texcoords_idx[1]+2]<<",";
js_file << locTexcoord[texcoords_idx[2]]<<","<<locTexcoord[texcoords_idx[2]+2];
js_file << ");\n";
}
// footer
js_file << "function v( x, y, z ) {\n";
js_file<< " scope.vertices.push(new THREE.Vector3(x,y,z));\n";
js_file<<"}\n";
js_file <<"function f3( a, b, c, n1_x,n1_y,n1_z,n2_x,n2_y,n2_z,n3_x,n3_y,n3_z ) {\n";
js_file << " scope.faces.push(new THREE.Face3(a,b,c,[new THREE.Vector3(n1_x,n1_y,n1_z),\n";
js_file << "new THREE.Vector3( n2_x, n2_y, n2_z ), new THREE.Vector3( n3_x, n3_y, n3_z ) ] ) );\n";
js_file << "}\n";
js_file << "function uvs(a,b,c,d,e,f) {\n";
js_file << "scope.faceVertexUvs[ 0 ].push( [new THREE.Vector2(a,b), new THREE.Vector2(c,d), new THREE.Vector2(e,f)] );\n";
js_file << "}\n}\n";
js_file << "Shape.prototype = new THREE.Geometry();\n";
js_file << "Shape.prototype.constructor = Shape;\n";
js_file.close();
delete [] vertices_idx;
delete [] texcoords_idx;
delete [] normals_idx;
}
//---------------------------------------------------------------------------
float* Tesselator::VerticesList()
{
return locVertexcoord;
}
//---------------------------------------------------------------------------
float* Tesselator::NormalsList()
{
return locNormalcoord;
}
//---------------------------------------------------------------------------
float* Tesselator::TextureCoordinatesList()
{
return locTexcoord;
}
//---------------------------------------------------------------------------
int Tesselator::ObjGetTriangleCount()
{
return tot_triangle_count;
}
//---------------------------------------------------------------------------
int Tesselator::ObjGetVertexCount()
{
return tot_vertex_count;
}
//---------------------------------------------------------------------------
int Tesselator::ObjGetNormalCount()
{
return tot_normal_count;
}
//---------------------------------------------------------------------------
int Tesselator::ObjGetTexCoordCount()
{
return tot_texcoord_count;
}
//---------------------------------------------------------------------------
int Tesselator::ObjGetEdgeCount()
{
return edgelist.size();
}
//---------------------------------------------------------------------------
int Tesselator::ObjEdgeGetVertexCount(int iEdge)
{
aedge* edge = edgelist.at(iEdge);
if (!edge) {
return 0;
}
return edge->number_of_coords;
}
//---------------------------------------------------------------------------
void Tesselator::GetVertex(int ivert, float& x, float& y, float& z)
{
x = locVertexcoord[ivert*3 + 0];
y = locVertexcoord[ivert*3 + 1];
z = locVertexcoord[ivert*3 + 2];
}
//---------------------------------------------------------------------------
void Tesselator::GetNormal(int ivert, float& x, float& y, float& z)
{
x = locNormalcoord[ivert*3 + 0];
y = locNormalcoord[ivert*3 + 1];
z = locNormalcoord[ivert*3 + 2];
}
//---------------------------------------------------------------------------
void Tesselator::GetTriangleIndex(int triangleIdx, int &v1, int &v2, int &v3)
{
v1 = loc_tri_indexes[3*triangleIdx + 0];
v2 = loc_tri_indexes[3*triangleIdx + 1];
v3 = loc_tri_indexes[3*triangleIdx + 2];
}
//---------------------------------------------------------------------------
void Tesselator::GetEdgeVertex(int iEdge, int ivert, float &x, float &y, float &z)
{
aedge* e = edgelist.at(iEdge);
if (!e) {
return;
}
x = e->vertex_coord[3*ivert + 0];
y = e->vertex_coord[3*ivert + 1];
z = e->vertex_coord[3*ivert + 2];
}
//---------------------------------------------------------------------------
void Tesselator::ObjGetTriangle(int trianglenum, int *vertices, int *texcoords, int *normals)
{
int pID = loc_tri_indexes[(trianglenum * 3) + 0] * 3;
int qID = loc_tri_indexes[(trianglenum * 3) + 1] * 3;
int rID = loc_tri_indexes[(trianglenum * 3) + 2] * 3;
vertices[0] = pID;
vertices[1] = qID;
vertices[2] = rID;
normals[0] = pID;
normals[1] = qID;
normals[2] = rID;
texcoords[0] = pID;
texcoords[1] = qID;
texcoords[2] = rID;
}
//---------------------------------------------------------------------------
//---------------------------------HELPERS-----------------------------------
//---------------------------------------------------------------------------
void Tesselator::JoinPrimitives()
{
int obP = 0;
int obN = 0;
int obT = 0;
int obTR = 0;
int advance = 0;
int total_poly_count = 0;
int total_vertex_count = 0;
int total_normal_count = 0;
std::vector<aface*>::iterator anIterator = facelist.begin();
while (anIterator != facelist.end()) {
aface* myface = *anIterator;
total_poly_count = total_poly_count + myface->number_of_triangles;
total_vertex_count = total_vertex_count + myface->number_of_coords;
total_normal_count = total_normal_count + myface->number_of_normals;
anIterator++;
}
loc_tri_indexes= new int[total_poly_count * 3 ];
locVertexcoord = new float[total_vertex_count * 3 ];
locNormalcoord = new float[total_normal_count * 3 ];
tot_triangle_count = total_poly_count;
tot_vertex_count = total_vertex_count;
tot_normal_count = total_normal_count;
anIterator = facelist.begin();
while (anIterator != facelist.end()) {
aface* myface = *anIterator;
for (int x = 0; x < myface->number_of_coords; x++) {
locVertexcoord[(obP * 3) + 0] = myface->vertex_coord[(x * 3) + 0];
locVertexcoord[(obP * 3) + 1] = myface->vertex_coord[(x * 3) + 1];
locVertexcoord[(obP * 3) + 2] = myface->vertex_coord[(x * 3) + 2];
obP++;
}
for (int x = 0; x < myface->number_of_normals; x++) {
locNormalcoord[(obN * 3) + 0] = myface->normal_coord[(x * 3) + 0];
locNormalcoord[(obN * 3) + 1] = myface->normal_coord[(x * 3) + 1];
locNormalcoord[(obN * 3) + 2] = myface->normal_coord[(x * 3) + 2];
obN++;
}
for (int x = 0; x < myface->number_of_triangles; x++) {
loc_tri_indexes[(obTR * 3) + 0] = myface->tri_indexes[(x * 3) + 0] + advance - 1;
loc_tri_indexes[(obTR * 3) + 1] = myface->tri_indexes[(x * 3) + 1] + advance - 1;
loc_tri_indexes[(obTR * 3) + 2] = myface->tri_indexes[(x * 3) + 2] + advance - 1;
obTR++;
}
advance = obP;
delete [] myface->vertex_coord;
myface->vertex_coord = NULL;
delete [] myface->normal_coord;
myface->normal_coord = NULL;
delete [] myface->tri_indexes;
myface->tri_indexes = NULL;
delete myface;
myface = NULL;
anIterator++;
}
}
void Tesselator::JoinPrimitivesWithUVCoords()
{
int obP = 0;
int obN = 0;
int obT = 0;
int obTR = 0;
int advance = 0;
int total_poly_count = 0;
int total_vertex_count = 0;
int total_normal_count = 0;
int total_texcoord_count = 0;
std::vector<aface*>::iterator anIterator = facelist.begin();
while (anIterator != facelist.end()) {
aface* myface = *anIterator;
total_poly_count = total_poly_count + myface->number_of_triangles;
total_vertex_count = total_vertex_count + myface->number_of_coords;
total_normal_count = total_normal_count + myface->number_of_normals;
total_texcoord_count = total_texcoord_count + myface->number_of_texcoords;
anIterator++;
}
loc_tri_indexes= new int[total_poly_count * 3 ];
locVertexcoord = new float[total_vertex_count * 3 ];
locNormalcoord = new float[total_normal_count * 3 ];
locTexcoord = new float[total_texcoord_count * 3 ];
tot_triangle_count = total_poly_count;
tot_vertex_count = total_vertex_count;
tot_normal_count = total_normal_count;
tot_texcoord_count = total_texcoord_count;
anIterator = facelist.begin();
while (anIterator != facelist.end()) {
aface* myface = *anIterator;
for (int x = 0; x < myface->number_of_coords; x++) {
locVertexcoord[(obP * 3) + 0] = myface->vertex_coord[(x * 3) + 0];
locVertexcoord[(obP * 3) + 1] = myface->vertex_coord[(x * 3) + 1];
locVertexcoord[(obP * 3) + 2] = myface->vertex_coord[(x * 3) + 2];
obP++;
}
for (int x = 0; x < myface->number_of_normals; x++) {
locNormalcoord[(obN * 3) + 0] = myface->normal_coord[(x * 3) + 0];
locNormalcoord[(obN * 3) + 1] = myface->normal_coord[(x * 3) + 1];
locNormalcoord[(obN * 3) + 2] = myface->normal_coord[(x * 3) + 2];
obN++;
}
for (int x = 0; x < myface->number_of_texcoords; x++) {
locTexcoord[(obT * 3) + 0] = myface->tex_coord[(x * 3) + 0];
locTexcoord[(obT * 3) + 1] = myface->tex_coord[(x * 3) + 1];
locTexcoord[(obT * 3) + 2] = myface->tex_coord[(x * 3) + 2];
obT++;
}
for (int x = 0; x < myface->number_of_triangles; x++) {
loc_tri_indexes[(obTR * 3) + 0] = myface->tri_indexes[(x * 3) + 0] + advance - 1;
loc_tri_indexes[(obTR * 3) + 1] = myface->tri_indexes[(x * 3) + 1] + advance - 1;
loc_tri_indexes[(obTR * 3) + 2] = myface->tri_indexes[(x * 3) + 2] + advance - 1;
obTR++;
}
advance = obP;
delete [] myface->vertex_coord;
myface->vertex_coord = NULL;
delete [] myface->normal_coord;
myface->normal_coord = NULL;
delete [] myface->tex_coord;
myface->tex_coord = NULL;
delete [] myface->tri_indexes;
myface->tri_indexes = NULL;
delete myface;
myface = NULL;
anIterator++;
}
}
//---------------------------------------------------------------------------
Standard_Boolean Tesselator::TriangleIsValid(const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3) const
{
gp_Vec V1(P1,P2);
gp_Vec V2(P2,P3);
gp_Vec V3(P3,P1);
if ((V1.SquareMagnitude() > 1.e-10) && (V2.SquareMagnitude() > 1.e-10) && (V3.SquareMagnitude() > 1.e-10)) {
V1.Cross(V2);
if (V1.SquareMagnitude() > 1.e-10)
return Standard_True;
else
return Standard_False;
}
else
return Standard_False;
}
//---------------------------------------------------------------------------
void Tesselator::PrepareBoxTextureCoordinates(const TopoDS_Shape& aShape)
{
//declare local variables
Bnd_Box aBox;
Standard_Real aXmin,aYmin ,aZmin ,aXmax ,aYmax ,aZmax;
Standard_Real aUmaxBack, aVmaxBack;
Standard_Real aUmaxLeft, aVmaxLeft;
Standard_Real aUmaxBottom, aVmaxBottom;
//calculate the bounding box
BRepBndLib::Add(aShape, aBox);
aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
//enlarge the bb so all edges have the size of the biggest one
Standard_Real xDim = abs((long)aXmax - (long)aXmin);
Standard_Real yDim = abs((long)aYmax - (long)aYmin);
Standard_Real zDim = abs((long)aZmax - (long)aZmin);
if ((xDim > yDim) && (xDim > zDim)) {
aYmin -= (xDim - yDim) / 2;
aYmax += (xDim - yDim) / 2;
aZmin -= (xDim - zDim) / 2;
aZmax += (xDim - zDim) / 2;
}
else if ((yDim > xDim) && (yDim > zDim)) {
aXmin -= (yDim - xDim) / 2;
aXmax += (yDim - xDim) / 2;
aZmin -= (yDim - zDim) / 2;
aZmax += (yDim - zDim) / 2;
}
else {
aXmin -= (zDim - xDim) / 2;
aXmax += (zDim - xDim) / 2;
aYmin -= (zDim - yDim) / 2;
aYmax += (zDim - yDim) / 2;
}
aBndBoxSz = aXmax-aXmin;
}
//---------------------------------------------------------------------------
void Tesselator::GetBoxTextureCoordinate(const gp_Pnt& p, const gp_Dir& N1, gp_Vec2d& theCoord_p)
{
Standard_Real x = abs(N1.X());
Standard_Real y = abs(N1.Y());
Standard_Real z = abs(N1.Z());
if (x >= y && x >= z) {
if (N1.X() > 0) { //right
theCoord_p.SetX(p.Y() - aYmin);
theCoord_p.SetY(p.Z() - aZmin);
theCoord_p.Rotate(M_PI/2.);
}
else { //left
theCoord_p.SetX(p.Z() - aZmin);
theCoord_p.SetY(p.Y() - aYmin);
}
}
else if ((y >= z) && (y >= x)) {
if (N1.Y() > 0) { //top
theCoord_p.SetX(p.X() - aXmin);
theCoord_p.SetY(-(p.Z() - aZmin));
}
else { //bottom
theCoord_p.SetY(p.Z() - aZmin);
theCoord_p.SetX(p.X() - aXmin);
theCoord_p.Rotate( M_PI);
}
}
else {
if (N1.Z() > 0) { //front
theCoord_p.SetX(p.X() - aXmin);
theCoord_p.SetY(p.Y() - aYmin);
}
else { //back
theCoord_p.SetX(p.Y() - aYmin);
theCoord_p.SetY(p.X() - aXmin);
theCoord_p.Rotate(M_PI/2.);
}
}
}