forked from Derek-yfqiu/OCCReader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GEOM_WireframeFace.cxx
516 lines (458 loc) · 16.7 KB
/
GEOM_WireframeFace.cxx
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
// Copyright (C) 2014-2015 KIT-INR/NK
//
// This library 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 2.1 of the License.
//
// This library 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 this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//
#include "GEOM_WireframeFace.h"
#include <vtkObjectFactory.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyData.h>
#include <vtkInformation.h>
#include <vtkInformationVector.h>
#include <Precision.hxx>
#include <BRepTools.hxx>
#include <TopExp_Explorer.hxx>
#include <Geom2dHatch_Hatcher.hxx>
#include <Geom2dHatch_Intersector.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColStd_Array1OfInteger.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <BRep_Tool.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <Geom2d_Line.hxx>
#include <gp_Dir2d.hxx>
#include <gp_Pnt2d.hxx>
#include <Geom2dHatch_Hatcher.hxx>
#include <HatchGen_Domain.hxx>
#include <Adaptor3d_HCurve.hxx>
vtkStandardNewMacro(GEOM_WireframeFace);
GEOM_WireframeFace::GEOM_WireframeFace():
Discret(15)
{
NbIso[0] = 1;
NbIso[1] = 1;
this->SetNumberOfInputPorts(0);
}
GEOM_WireframeFace::~GEOM_WireframeFace()
{
}
int GEOM_WireframeFace::RequestData(vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkPolyData *aPolyData = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
aPolyData->Allocate();
vtkPoints* aPts = vtkPoints::New();
aPolyData->SetPoints(aPts);
aPts->Delete();
TFaceSet::Iterator anIter(myFaceSet);
for(; anIter.More(); anIter.Next()){
const TopoDS_Face& aFace = anIter.Value();
OCC2VTK(aFace,aPolyData,aPts,NbIso,Discret);
}
return 1;
}
void GEOM_WireframeFace::SetNbIso(const int theNb[2])
{
if ( theNb[0] == NbIso[0] && theNb[1] == NbIso[1])
return;
NbIso[0] = theNb[0];
NbIso[1] = theNb[1];
Modified();
}
void GEOM_WireframeFace::GetNbIso(int &theNbU,int &theNbV)
{
theNbU = NbIso[0];
theNbV = NbIso[1];
}
void
GEOM_WireframeFace::
OCC2VTK(const TopoDS_Face& theFace,
vtkPolyData* thePolyData,
vtkPoints* thePts,
const int theNbIso[2],
const int theDiscret)
{
TopoDS_Face aFace = theFace;
aFace.Orientation(TopAbs_FORWARD);
CreateIso(aFace,theNbIso,theDiscret,thePolyData,thePts);
}
void
GEOM_WireframeFace::
CreateIso(const TopoDS_Face& theFace,
const int theNbIso[2],
const int theDiscret,
vtkPolyData* thePolyData,
vtkPoints* thePts)
{
// Constants for iso building
static Standard_Real INTERSECTOR_CONFUSION = 1.e-10 ; // -8 ;
static Standard_Real INTERSECTOR_TANGENCY = 1.e-10 ; // -8 ;
static Standard_Real HATHCER_CONFUSION_2D = 1.e-8 ;
static Standard_Real HATHCER_CONFUSION_3D = 1.e-8 ;
Geom2dHatch_Hatcher
aHatcher(Geom2dHatch_Intersector(INTERSECTOR_CONFUSION,
INTERSECTOR_TANGENCY),
HATHCER_CONFUSION_2D,
HATHCER_CONFUSION_3D,
Standard_True,
Standard_False);
Standard_Real anUMin, anUMax, aVMin, aVMax;
TColStd_Array1OfReal anUPrm(0, theNbIso[0]), aVPrm(0, theNbIso[1]);
TColStd_Array1OfInteger anUInd(0, theNbIso[0]), aVInd(0, theNbIso[1]);
anUInd.Init(0);
aVInd.Init(0);
//-----------------------------------------------------------------------
// If the Min Max bounds are infinite, there are bounded to Infinite
// value.
//-----------------------------------------------------------------------
BRepTools::UVBounds(theFace, anUMin, anUMax, aVMin, aVMax) ;
Standard_Boolean InfiniteUMin = Precision::IsNegativeInfinite (anUMin) ;
Standard_Boolean InfiniteUMax = Precision::IsPositiveInfinite (anUMax) ;
Standard_Boolean InfiniteVMin = Precision::IsNegativeInfinite (aVMin) ;
Standard_Boolean InfiniteVMax = Precision::IsPositiveInfinite (aVMax) ;
static float VTKINFINITE = 1.0E38;
if(InfiniteUMin && InfiniteUMax){
anUMin = - VTKINFINITE ;
anUMax = VTKINFINITE ;
}else if(InfiniteUMin){
anUMin = anUMax - VTKINFINITE ;
}else if(InfiniteUMax){
anUMax = anUMin + VTKINFINITE ;
}
if(InfiniteVMin && InfiniteVMax){
aVMin = - VTKINFINITE ;
aVMax = VTKINFINITE ;
}else if(InfiniteVMin){
aVMin = aVMax - VTKINFINITE ;
}else if(InfiniteVMax){
aVMax = aVMin + VTKINFINITE ;
}
//-----------------------------------------------------------------------
// Retreiving the edges and loading them into the hatcher.
//-----------------------------------------------------------------------
TopExp_Explorer ExpEdges(theFace, TopAbs_EDGE);
for(; ExpEdges.More(); ExpEdges.Next()){
const TopoDS_Edge& anEdge = TopoDS::Edge(ExpEdges.Current());
Standard_Real U1, U2 ;
const Handle(Geom2d_Curve) PCurve =
BRep_Tool::CurveOnSurface(anEdge, theFace, U1, U2) ;
if(PCurve.IsNull() || U1 == U2)
return;
//-- Test if a TrimmedCurve is necessary
if(Abs(PCurve->FirstParameter()-U1) <= Precision::PConfusion() &&
Abs(PCurve->LastParameter()-U2) <= Precision::PConfusion())
{
aHatcher.AddElement(PCurve, anEdge.Orientation()) ;
}else{
if(!PCurve->IsPeriodic()){
Handle(Geom2d_TrimmedCurve) TrimPCurve =
Handle(Geom2d_TrimmedCurve)::DownCast(PCurve);
if(!TrimPCurve.IsNull()){
Handle_Geom2d_Curve aBasisCurve = TrimPCurve->BasisCurve();
if(aBasisCurve->FirstParameter()-U1 > Precision::PConfusion() ||
U2-aBasisCurve->LastParameter() > Precision::PConfusion())
{
aHatcher.AddElement(PCurve, anEdge.Orientation()) ;
return;
}
}else{
if(PCurve->FirstParameter()-U1 > Precision::PConfusion()){
U1=PCurve->FirstParameter();
}
if(U2-PCurve->LastParameter() > Precision::PConfusion()){
U2=PCurve->LastParameter();
}
}
}
Handle(Geom2d_TrimmedCurve) TrimPCurve =
new Geom2d_TrimmedCurve(PCurve, U1, U2);
aHatcher.AddElement(TrimPCurve, anEdge.Orientation());
}
}
//-----------------------------------------------------------------------
// Loading and trimming the hatchings.
//-----------------------------------------------------------------------
Standard_Integer IIso;
Standard_Real DeltaU = Abs(anUMax - anUMin) ;
Standard_Real DeltaV = Abs(aVMax - aVMin) ;
Standard_Real confusion = Min(DeltaU, DeltaV) * HATHCER_CONFUSION_3D ;
aHatcher.Confusion3d (confusion) ;
if ( theNbIso[0] ) {
Standard_Real StepU = DeltaU / (Standard_Real)theNbIso[0];
if(StepU > confusion){
Standard_Real UPrm = anUMin + StepU / 2.;
gp_Dir2d Dir(0., 1.) ;
for(IIso = 1 ; IIso <= theNbIso[0] ; IIso++) {
anUPrm(IIso) = UPrm ;
gp_Pnt2d Ori (UPrm, 0.) ;
Geom2dAdaptor_Curve HCur (new Geom2d_Line (Ori, Dir)) ;
anUInd(IIso) = aHatcher.AddHatching (HCur) ;
UPrm += StepU ;
}
}
}
if ( theNbIso[1] ) {
Standard_Real StepV = DeltaV / (Standard_Real) theNbIso[1] ;
if(StepV > confusion){
Standard_Real VPrm = aVMin + StepV / 2.;
gp_Dir2d Dir(1., 0.);
for(IIso = 1 ; IIso <= theNbIso[1] ; IIso++){
aVPrm(IIso) = VPrm;
gp_Pnt2d Ori (0., VPrm);
Geom2dAdaptor_Curve HCur(new Geom2d_Line (Ori, Dir));
aVInd(IIso) = aHatcher.AddHatching (HCur) ;
VPrm += StepV ;
}
}
}
//-----------------------------------------------------------------------
// Computation.
//-----------------------------------------------------------------------
aHatcher.Trim() ;
Standard_Integer aNbDom = 0 ; // for debug purpose
Standard_Integer Index ;
for(IIso = 1 ; IIso <= theNbIso[0] ; IIso++){
Index = anUInd(IIso) ;
if(Index != 0){
if(aHatcher.TrimDone(Index) && !aHatcher.TrimFailed(Index)){
aHatcher.ComputeDomains(Index);
if(aHatcher.IsDone (Index))
aNbDom = aHatcher.NbDomains (Index);
}
}
}
for(IIso = 1 ; IIso <= theNbIso[1] ; IIso++){
Index = aVInd(IIso);
if(Index != 0){
if(aHatcher.TrimDone (Index) && !aHatcher.TrimFailed(Index)){
aHatcher.ComputeDomains (Index);
if(aHatcher.IsDone (Index))
aNbDom = aHatcher.NbDomains (Index);
}
}
}
//-----------------------------------------------------------------------
// Push iso lines in vtk kernel
//-----------------------------------------------------------------------
for(Standard_Integer UIso = anUPrm.Lower() ; UIso <= anUPrm.Upper(); UIso++){
Standard_Integer UInd = anUInd.Value(UIso);
if(UInd != 0){
Standard_Real UPrm = anUPrm.Value(UIso);
if(aHatcher.IsDone(UInd)){
Standard_Integer NbDom = aHatcher.NbDomains(UInd);
for(Standard_Integer IDom = 1 ; IDom <= NbDom ; IDom++){
const HatchGen_Domain& Dom = aHatcher.Domain(UInd, IDom) ;
Standard_Real V1 = Dom.HasFirstPoint()? Dom.FirstPoint().Parameter(): aVMin - VTKINFINITE;
Standard_Real V2 = Dom.HasSecondPoint()? Dom.SecondPoint().Parameter(): aVMax + VTKINFINITE;
CreateIso_(theFace, GeomAbs_IsoU, UPrm, V1, V2, theDiscret, thePolyData, thePts);
}
}
}
}
for(Standard_Integer VIso = aVPrm.Lower() ; VIso <= aVPrm.Upper(); VIso++){
Standard_Integer VInd = aVInd.Value(VIso);
if(VInd != 0){
Standard_Real VPrm = aVPrm.Value(VIso);
if(aHatcher.IsDone (VInd)){
Standard_Integer NbDom = aHatcher.NbDomains(VInd);
for (Standard_Integer IDom = 1 ; IDom <= NbDom ; IDom++){
const HatchGen_Domain& Dom = aHatcher.Domain(VInd, IDom);
Standard_Real U1 = Dom.HasFirstPoint()? Dom.FirstPoint().Parameter(): aVMin - VTKINFINITE;
Standard_Real U2 = Dom.HasSecondPoint()? Dom.SecondPoint().Parameter(): aVMax + VTKINFINITE;
CreateIso_(theFace, GeomAbs_IsoV, VPrm, U1, U2, theDiscret, thePolyData, thePts);
}
}
}
}
}
void
GEOM_WireframeFace::
CreateIso_(const TopoDS_Face& theFace,
GeomAbs_IsoType theIsoType,
Standard_Real Par,
Standard_Real T1,
Standard_Real T2,
const int theDiscret,
vtkPolyData* thePolyData,
vtkPoints* thePts)
{
Standard_Real U1, U2, V1, V2, stepU=0., stepV=0.;
Standard_Integer j;
gp_Pnt P;
TopLoc_Location aLoc;
const Handle(Geom_Surface)& S = BRep_Tool::Surface(theFace,aLoc);
if(!S.IsNull()){
BRepAdaptor_Surface S(theFace,Standard_False);
GeomAbs_SurfaceType SurfType = S.GetType();
GeomAbs_CurveType CurvType = GeomAbs_OtherCurve;
Standard_Integer Intrv, nbIntv;
Standard_Integer nbUIntv = S.NbUIntervals(GeomAbs_CN);
Standard_Integer nbVIntv = S.NbVIntervals(GeomAbs_CN);
TColStd_Array1OfReal TI(1,Max(nbUIntv, nbVIntv)+1);
if(theIsoType == GeomAbs_IsoU){
S.VIntervals(TI, GeomAbs_CN);
V1 = Max(T1, TI(1));
V2 = Min(T2, TI(2));
U1 = Par;
U2 = Par;
stepU = 0;
nbIntv = nbVIntv;
}else{
S.UIntervals(TI, GeomAbs_CN);
U1 = Max(T1, TI(1));
U2 = Min(T2, TI(2));
V1 = Par;
V2 = Par;
stepV = 0;
nbIntv = nbUIntv;
}
S.D0(U1,V1,P);
MoveTo(P,thePts);
for(Intrv = 1; Intrv <= nbIntv; Intrv++){
if(TI(Intrv) <= T1 && TI(Intrv + 1) <= T1)
continue;
if(TI(Intrv) >= T2 && TI(Intrv + 1) >= T2)
continue;
if(theIsoType == GeomAbs_IsoU){
V1 = Max(T1, TI(Intrv));
V2 = Min(T2, TI(Intrv + 1));
stepV = (V2 - V1) / theDiscret;
}else{
U1 = Max(T1, TI(Intrv));
U2 = Min(T2, TI(Intrv + 1));
stepU = (U2 - U1) / theDiscret;
}
switch (SurfType) {
case GeomAbs_Plane :
break;
case GeomAbs_Cylinder :
case GeomAbs_Cone :
if(theIsoType == GeomAbs_IsoV){
for(j = 1; j < theDiscret; j++){
U1 += stepU;
V1 += stepV;
S.D0(U1,V1,P);
DrawTo(P,thePolyData,thePts);
}
}
break;
case GeomAbs_Sphere :
case GeomAbs_Torus :
case GeomAbs_OffsetSurface :
case GeomAbs_OtherSurface :
for(j = 1; j < theDiscret; j++){
U1 += stepU;
V1 += stepV;
S.D0(U1,V1,P);
DrawTo(P,thePolyData,thePts);
}
break;
case GeomAbs_BezierSurface :
case GeomAbs_BSplineSurface :
for(j = 1; j <= theDiscret/2; j++){
Standard_Real aStep = (theIsoType == GeomAbs_IsoV) ? stepU*2. : stepV*2.;
CreateIso__(S, theIsoType, U1, V1, aStep, thePolyData, thePts);
U1 += stepU*2.;
V1 += stepV*2.;
}
break;
case GeomAbs_SurfaceOfExtrusion :
case GeomAbs_SurfaceOfRevolution :
if((theIsoType == GeomAbs_IsoV && SurfType == GeomAbs_SurfaceOfRevolution) ||
(theIsoType == GeomAbs_IsoU && SurfType == GeomAbs_SurfaceOfExtrusion))
{
if(SurfType == GeomAbs_SurfaceOfExtrusion)
break;
for(j = 1; j < theDiscret; j++){
U1 += stepU;
V1 += stepV;
S.D0(U1,V1,P);
DrawTo(P,thePolyData,thePts);
}
}else{
CurvType = (S.BasisCurve())->GetType();
switch(CurvType){
case GeomAbs_Line :
break;
case GeomAbs_Circle :
case GeomAbs_Ellipse :
for (j = 1; j < theDiscret; j++) {
U1 += stepU;
V1 += stepV;
S.D0(U1,V1,P);
DrawTo(P,thePolyData,thePts);
}
break;
case GeomAbs_Parabola :
case GeomAbs_Hyperbola :
case GeomAbs_BezierCurve :
case GeomAbs_BSplineCurve :
case GeomAbs_OtherCurve :
for(j = 1; j <= theDiscret/2; j++){
Standard_Real aStep = (theIsoType == GeomAbs_IsoV) ? stepU*2. : stepV*2.;
CreateIso__(S, theIsoType, U1, V1, aStep, thePolyData, thePts);
U1 += stepU*2.;
V1 += stepV*2.;
}
break;
}
}
}
}
S.D0(U2,V2,P);
DrawTo(P,thePolyData,thePts);
}
}
void
GEOM_WireframeFace::
CreateIso__(const BRepAdaptor_Surface& theSurface,
GeomAbs_IsoType theIsoType,
Standard_Real& theU,
Standard_Real& theV,
Standard_Real theStep,
vtkPolyData* thePolyData,
vtkPoints* thePts)
{
gp_Pnt Pl, Pr, Pm;
if (theIsoType == GeomAbs_IsoU) {
theSurface.D0(theU, theV, Pl);
theSurface.D0(theU, theV + theStep/2., Pm);
theSurface.D0(theU, theV + theStep, Pr);
} else {
theSurface.D0(theU, theV, Pl);
theSurface.D0(theU + theStep/2., theV, Pm);
theSurface.D0(theU + theStep, theV, Pr);
}
static Standard_Real ISO_RATIO = 1.001;
if (Pm.Distance(Pl) + Pm.Distance(Pr) <= ISO_RATIO*Pl.Distance(Pr)) {
DrawTo(Pr,thePolyData,thePts);
} else {
if (theIsoType == GeomAbs_IsoU) {
CreateIso__(theSurface, theIsoType, theU, theV, theStep/2, thePolyData, thePts);
Standard_Real aLocalV = theV + theStep/2 ;
CreateIso__(theSurface, theIsoType, theU, aLocalV , theStep/2, thePolyData, thePts);
} else {
CreateIso__(theSurface, theIsoType, theU, theV, theStep/2, thePolyData, thePts);
Standard_Real aLocalU = theU + theStep/2 ;
CreateIso__(theSurface, theIsoType, aLocalU , theV, theStep/2, thePolyData, thePts);
}
}
}