summaryrefslogtreecommitdiff
path: root/inc/Prs3d_WFRestrictedFace.gxx
blob: edacd75882729ef2fcc4e7574ded117063b5747e (plain)
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
// File:	Prs3d_WFRestrictedFace.gxx
// Created:	Fri Jun 10 11:31:42 1994
// Author:	Laurent PAINNOT
//		<lpa@metrox>


#ifdef DEBUG
#include <OSD_Timer.hxx>
extern OSD_Timer RestrictedFaceTimer1,RestrictedFaceTimer2,RestrictedFaceTimer3,RestrictedFaceTimer4;
#endif

#include <Hatch_Hatcher.hxx>
#include <Graphic3d_Array1OfVertex.hxx>
#include <Graphic3d_ArrayOfPrimitives.hxx>
#include <Graphic3d_Group.hxx>
#include <gp_Pnt.hxx>
#include <Prs3d_IsoAspect.hxx>
#include <Adaptor3d_IsoCurve.hxx>
#include <Bnd_Box2d.hxx>
#include <BndLib_Add2dCurve.hxx>
#include <Precision.hxx>
#include <GeomAdaptor_Curve.hxx>
#include <Geom_Curve.hxx>
#include <GeomAbs_SurfaceType.hxx>
#include <Geom_Surface.hxx>

//=========================================================================
// function: Add
// purpose
//=========================================================================
void Prs3d_WFRestrictedFace::Add
  (const Handle (Prs3d_Presentation)&  aPresentation,
   const Handle(BRepAdaptor_HSurface)& aFace,
   const Standard_Boolean              DrawUIso,
   const Standard_Boolean              DrawVIso,
   const Quantity_Length               aDeflection,
   const Standard_Integer              NBUiso,
   const Standard_Integer              NBViso,
   const Handle(Prs3d_Drawer)&         aDrawer,
   Prs3d_NListOfSequenceOfPnt&         Curves)
{
  Standard_Boolean isPA = Graphic3d_ArrayOfPrimitives::IsEnable();
  Standard_Integer nbPoints = aDrawer->Discretisation();

#ifdef DEBUG
  RestrictedFaceTimer1.Start();
#endif

  RestrictionTool ToolRst (aFace);

  // compute bounds of the restriction
  Standard_Real UMin,UMax,VMin,VMax;
//  Standard_Real u,v,step;
//  Standard_Integer i, nbp= 10;
  Standard_Integer i;
  gp_Pnt2d P1,P2;
  Bnd_Box2d B;
  
  for (ToolRst.Init(); ToolRst.More(); ToolRst.Next()) {
    Adaptor2d_Curve2dPtr TheRCurve = ToolRst.Value();
    BndLib_Add2dCurve::Add(*TheRCurve, Precision::PConfusion(), B);
  }
  if ( ! B.IsVoid() )
    B.Get(UMin, VMin, UMax, VMax);
  else
  { // no pcurves -- take natural bounds
    UMin = aFace->Surface().FirstUParameter();
    VMin = aFace->Surface().FirstVParameter();
    UMax = aFace->Surface().LastUParameter();
    VMax = aFace->Surface().LastVParameter();
  }

#ifdef DEBUG
  RestrictedFaceTimer1.Stop();

  RestrictedFaceTimer2.Start();
#endif

  // load the isos
  Hatch_Hatcher isobuild(1.e-5,ToolRst.IsOriented());
  Standard_Boolean UClosed = aFace->IsUClosed();
  Standard_Boolean VClosed = aFace->IsVClosed();

  if ( ! UClosed ) {
    UMin = UMin + ( UMax - UMin) /1000.;
    UMax = UMax - ( UMax - UMin) /1000.; 
  }

  if ( ! VClosed ) {
    VMin = VMin + ( VMax - VMin) /1000.;
    VMax = VMax - ( VMax - VMin) /1000.; 
  }

  if (DrawUIso){
    if (NBUiso > 0) {
      UClosed = Standard_False; // En attendant un hatcher de course.
      Standard_Real du= UClosed ? (UMax-UMin)/NBUiso : (UMax-UMin)/(1+NBUiso);
      for (i=1; i<=NBUiso;i++){
	isobuild.AddXLine(UMin+du*i);
      }
    }
  }
  if (DrawVIso){
    if ( NBViso > 0) {
      VClosed = Standard_False;
      Standard_Real dv= VClosed ?(VMax-VMin)/NBViso : (VMax-VMin)/(1+NBViso);
      for (i=1; i<=NBViso;i++){
	isobuild.AddYLine(VMin+dv*i);
      }
    }
  }

#ifdef DEBUG
  RestrictedFaceTimer2.Stop();
  RestrictedFaceTimer3.Start();
#endif

  // trim the isos
  Standard_Real U1, U2, U, DU;

  for (ToolRst.Init(); ToolRst.More(); ToolRst.Next()) {
    TopAbs_Orientation Orient = ToolRst.Orientation();
    if ( Orient == TopAbs_FORWARD || Orient == TopAbs_REVERSED ) {
      Adaptor2d_Curve2dPtr TheRCurve = ToolRst.Value();
      U1 = TheRCurve->FirstParameter();
      U2 = TheRCurve->LastParameter();
      if (TheRCurve->GetType() != GeomAbs_Line) {
	DU = (U2-U1)/(nbPoints-1);
	P2 = TheRCurve->Value(U1);
	for (i = 2; i <= nbPoints; i++) {
	  U = U1 + (i-1)*DU;
	  P1 = P2;
	  P2 = TheRCurve->Value(U);
	  if(Orient == TopAbs_FORWARD )
	    isobuild.Trim(P1,P2);
	  else
	    isobuild.Trim(P2,P1);
	}
      }
      else {
	P1 = TheRCurve->Value(U1);
	P2 = TheRCurve->Value(U2);
	if(Orient == TopAbs_FORWARD )
	  isobuild.Trim(P1,P2);
	else
	  isobuild.Trim(P2,P1);
      }	  
    }
  }

#ifdef DEBUG  
  RestrictedFaceTimer3.Stop();
  RestrictedFaceTimer4.Start();
#endif

  // draw the isos


  Adaptor3d_IsoCurve anIso;
  anIso.Load(aFace);
  Handle(Geom_Curve) BC;
  const BRepAdaptor_Surface& BS = *(BRepAdaptor_Surface*)&(aFace->Surface());
  GeomAbs_SurfaceType thetype = aFace->GetType();

  Standard_Integer NumberOfLines = isobuild.NbLines();
  Handle(Geom_Surface) GB;
  if (thetype == GeomAbs_BezierSurface) {
    GB = BS.Bezier();
  }
  else if (thetype == GeomAbs_BSplineSurface){
    GB = BS.BSpline();
  }

  for (i = 1; i <= NumberOfLines; i++) {
    Standard_Integer NumberOfIntervals = isobuild.NbIntervals(i);
    Standard_Real Coord = isobuild.Coordinate(i);
    for (Standard_Integer j = 1; j <= NumberOfIntervals; j++) {
      Standard_Real b1=isobuild.Start(i,j),b2=isobuild.End(i,j);

      if(b1 == RealFirst() || b2 == RealLast())
        continue;
      //b1 = b1 == RealFirst() ? - aLimit : b1;
      //b2 = b2 == RealLast()  ?   aLimit : b2;

      TColgp_SequenceOfPnt Pnts;
      if (!GB.IsNull()) {
	if (isobuild.IsXLine(i))
	  BC = GB->UIso(Coord);
	else 
	  BC = GB->VIso(Coord);
	//Note that the isos are the part of the shape, it will be displayed after a computation the whole shape
	//NbPoints = 30 - default parameter for computation of such curves
	DrawFaceIso::Add(aPresentation,GeomAdaptor_Curve(BC), b1, b2, aDeflection, Pnts, 30, !isPA);
	Curves.Append(Pnts);
      }
      else {
	if (isobuild.IsXLine(i))
	  anIso.Load(GeomAbs_IsoU,Coord,b1,b2);
	else
	  anIso.Load(GeomAbs_IsoV,Coord,b1,b2);
	DrawFaceIso::Add(aPresentation,anIso, aDeflection, aDrawer, Pnts, !isPA);
	Curves.Append(Pnts);
      }
    }
  }
#ifdef DEBUG
  RestrictedFaceTimer4.Stop();
#endif
}


//=========================================================================
// function: Match
// purpose
//=========================================================================
Standard_Boolean Prs3d_WFRestrictedFace::Match
  (const Quantity_Length X,
   const Quantity_Length Y,
   const Quantity_Length Z,
   const Quantity_Length aDistance,
   const Handle(BRepAdaptor_HSurface)& aFace,
   const Standard_Boolean DrawUIso,
   const Standard_Boolean DrawVIso,
   const Quantity_Length aDeflection,
   const Standard_Integer NBUiso,
   const Standard_Integer NBViso,
   const Handle(Prs3d_Drawer)& aDrawer)
{
  Standard_Real aLimit = aDrawer->MaximalParameterValue();
  Standard_Integer nbPoints = aDrawer->Discretisation();
  RestrictionTool ToolRst (aFace);

  // compute bounds of the restriction
  Standard_Real UMin,UMax,VMin,VMax;
  Standard_Real u,v,step;
  Standard_Integer i,nbP = 10;
  UMin = VMin = RealLast();
  UMax = VMax = RealFirst();
  gp_Pnt2d P1,P2;

  for (ToolRst.Init(); ToolRst.More(); ToolRst.Next()) {
    Adaptor2d_Curve2dPtr TheRCurve = ToolRst.Value();
    u = TheRCurve->FirstParameter();
    v = TheRCurve->LastParameter();
    if (TheRCurve->GetType() != GeomAbs_Line) {
      step = ( v - u) / nbP;
      for (i = 0; i <= nbP; i++) {
	gp_Pnt2d P = TheRCurve->Value(u);
	if (P.X() < UMin) UMin = P.X();
	if (P.X() > UMax) UMax = P.X();
	if (P.Y() < VMin) VMin = P.Y();
	if (P.Y() > VMax) VMax = P.Y();
	u += step;
      }
    }
    else {
      P1 = TheRCurve->Value(u);
      if (P1.X() < UMin) UMin = P1.X();
      if (P1.X() > UMax) UMax = P1.X();
      if (P1.Y() < VMin) VMin = P1.Y();
      if (P1.Y() > VMax) VMax = P1.Y();

      P2 = TheRCurve->Value(v);
      if (P2.X() < UMin) UMin = P2.X();
      if (P2.X() > UMax) UMax = P2.X();
      if (P2.Y() < VMin) VMin = P2.Y();
      if (P2.Y() > VMax) VMax = P2.Y();
    }
  }

  // load the isos
  Hatch_Hatcher isobuild(1.e-5,ToolRst.IsOriented());
  Standard_Boolean UClosed = aFace->IsUClosed();
  Standard_Boolean VClosed = aFace->IsVClosed();

  if ( ! UClosed ) {
    UMin = UMin + ( UMax - UMin) /1000.;
    UMax = UMax - ( UMax - UMin) /1000.; 
  }

  if ( ! VClosed ) {
    VMin = VMin + ( VMax - VMin) /1000.;
    VMax = VMax - ( VMax - VMin) /1000.; 
  }

  if (DrawUIso){
    if (NBUiso > 0) {
      UClosed = Standard_False; // En attendant un hatcher de course.
      Standard_Real du= UClosed ? (UMax-UMin)/NBUiso : (UMax-UMin)/(1+NBUiso);
      for (i=1; i<=NBUiso;i++){
	isobuild.AddXLine(UMin+du*i);
      }
    }
  }
  if (DrawVIso){
    if ( NBViso > 0) {
      VClosed = Standard_False;
      Standard_Real dv= VClosed ?(VMax-VMin)/NBViso : (VMax-VMin)/(1+NBViso);
      for (i=1; i<=NBViso;i++){
	isobuild.AddYLine(VMin+dv*i);
      }
    }
  }

  // trim the isos
  Standard_Real U1, U2, U, DU;

  for (ToolRst.Init(); ToolRst.More(); ToolRst.Next()) {
    TopAbs_Orientation Orient = ToolRst.Orientation();
    if ( Orient == TopAbs_FORWARD || Orient == TopAbs_REVERSED ) {
      Adaptor2d_Curve2dPtr TheRCurve = ToolRst.Value();
      U1 = TheRCurve->FirstParameter();
      U2 = TheRCurve->LastParameter();
      if (TheRCurve->GetType() != GeomAbs_Line) {
	DU = (U2-U1)/(nbPoints-1);
	P2 = TheRCurve->Value(U1);
	for (i = 2; i <= nbPoints; i++) {
	  U = U1 + (i-1)*DU;
	  P1 = P2;
	  P2 = TheRCurve->Value(U);
	  if(Orient == TopAbs_FORWARD )
	    isobuild.Trim(P1,P2);
	  else
	    isobuild.Trim(P2,P1);
	}
      }
      else {
	P1 = TheRCurve->Value(U1);
	P2 = TheRCurve->Value(U2);
	if(Orient == TopAbs_FORWARD )
	  isobuild.Trim(P1,P2);
	else
	  isobuild.Trim(P2,P1);
      }	  
    }
  }
  
  // draw the isos

  Adaptor3d_IsoCurve anIso;
  anIso.Load(aFace);
  Standard_Integer NumberOfLines = isobuild.NbLines();

  for (i = 1; i <= NumberOfLines; i++) {
    Standard_Integer NumberOfIntervals = isobuild.NbIntervals(i);
    Standard_Real Coord = isobuild.Coordinate(i);
    for (Standard_Integer j = 1; j <= NumberOfIntervals; j++) {
      Standard_Real b1=isobuild.Start(i,j),b2=isobuild.End(i,j);

      b1 = b1 == RealFirst() ? - aLimit : b1;
      b2 = b2 == RealLast()  ?   aLimit : b2;


      if (isobuild.IsXLine(i))
	anIso.Load(GeomAbs_IsoU,Coord,b1,b2);
      else
	anIso.Load(GeomAbs_IsoV,Coord,b1,b2);
    
      if (DrawFaceIso::Match(X,Y,Z,aDistance,anIso, 
			     aDeflection, aLimit, nbPoints))
	  return Standard_True;
	
	
    }
  }
    
   return Standard_False;
}



//=========================================================================
// function: Add
// purpose
//=========================================================================
void Prs3d_WFRestrictedFace::Add
  (const Handle (Prs3d_Presentation)& aPresentation,
   const Handle(BRepAdaptor_HSurface)&    aFace,
   const Handle (Prs3d_Drawer)&       aDrawer){

  Standard_Integer finu = aDrawer->UIsoAspect()->Number();
  Standard_Integer finv = aDrawer->VIsoAspect()->Number();
  Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();

  Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation);
  TheGroup->BeginPrimitives();
  Prs3d_NListOfSequenceOfPnt Curves;
  Prs3d_WFRestrictedFace::Add (  
		      aPresentation,
		      aFace,
		      Standard_True,
		      Standard_True,
		      aDeflection,
		      finu,
		      finv,
		      aDrawer,
		      Curves);

  TheGroup->EndPrimitives();
}


//=========================================================================
// function: AddUIso
// purpose
//=========================================================================
void Prs3d_WFRestrictedFace::AddUIso
  (const Handle (Prs3d_Presentation)& aPresentation,
   const Handle(BRepAdaptor_HSurface)& aFace,
   const Handle (Prs3d_Drawer)& aDrawer) {

  Standard_Integer finu = aDrawer->UIsoAspect()->Number();
  Standard_Integer finv = aDrawer->VIsoAspect()->Number();
  Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();
  Prs3d_NListOfSequenceOfPnt Curves;
  Prs3d_WFRestrictedFace::Add ( 
		      aPresentation,
		      aFace,
		      Standard_True,
		      Standard_False,
		      aDeflection,
		      finu,
		      finv,
		      aDrawer,
		      Curves);
}


//=========================================================================
// function: AddVIso
// purpose
//=========================================================================
void Prs3d_WFRestrictedFace::AddVIso
  (const Handle (Prs3d_Presentation)& aPresentation,
   const Handle(BRepAdaptor_HSurface)& aFace,
   const Handle (Prs3d_Drawer)& aDrawer) {

  Standard_Integer finu = aDrawer->UIsoAspect()->Number();
  Standard_Integer finv = aDrawer->VIsoAspect()->Number();
  Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();
  Prs3d_NListOfSequenceOfPnt Curves;
  Prs3d_WFRestrictedFace::Add ( 
		      aPresentation,
		      aFace,
		      Standard_False,
		      Standard_True,
		      aDeflection,
		      finu,
		      finv,
		      aDrawer,
		      Curves);
}


//=========================================================================
// function: Match
// purpose
//=========================================================================
Standard_Boolean Prs3d_WFRestrictedFace::Match
  (const Quantity_Length X,
   const Quantity_Length Y,
   const Quantity_Length Z,
   const Quantity_Length aDistance,
   const Handle(BRepAdaptor_HSurface)& aFace,
   const Handle (Prs3d_Drawer)& aDrawer){

  Standard_Integer finu = aDrawer->UIsoAspect()->Number();
  Standard_Integer finv = aDrawer->VIsoAspect()->Number();
  Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();
  return Prs3d_WFRestrictedFace::Match (  
                      X,Y,Z,aDistance,
		      aFace,
		      Standard_True,
		      Standard_True,
		      aDeflection,
		      finu,
		      finv,
		      aDrawer);
}


//=========================================================================
// function: MatchUIso
// purpose
//=========================================================================
Standard_Boolean Prs3d_WFRestrictedFace::MatchUIso
  (const Quantity_Length X,
   const Quantity_Length Y,
   const Quantity_Length Z,
   const Quantity_Length aDistance,
   const Handle(BRepAdaptor_HSurface)& aFace,
   const Handle (Prs3d_Drawer)& aDrawer) {

  Standard_Integer finu = aDrawer->UIsoAspect()->Number();
  Standard_Integer finv = aDrawer->VIsoAspect()->Number();
  Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();
  return Prs3d_WFRestrictedFace::Match ( 
                      X,Y,Z,aDistance,
		      aFace,
		      Standard_True,
		      Standard_False,
		      aDeflection,
		      finu,
		      finv,
		      aDrawer);
}


//=========================================================================
// function: MatchVIso
// purpose
//=========================================================================
Standard_Boolean Prs3d_WFRestrictedFace::MatchVIso
  (const Quantity_Length X,
   const Quantity_Length Y,
   const Quantity_Length Z,
   const Quantity_Length aDistance,
   const Handle(BRepAdaptor_HSurface)& aFace,
   const Handle (Prs3d_Drawer)& aDrawer) {

  Standard_Integer finu = aDrawer->UIsoAspect()->Number();
  Standard_Integer finv = aDrawer->VIsoAspect()->Number();
  Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();
  return Prs3d_WFRestrictedFace::Match ( 
                      X,Y,Z,aDistance,
		      aFace,
		      Standard_False,
		      Standard_True,
		      aDeflection,
		      finu,
		      finv,
		      aDrawer);
}