Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Prs3d / Prs3d_WFRestrictedFace.gxx
1 // File:        Prs3d_WFRestrictedFace.gxx
2 // Created:     Fri Jun 10 11:31:42 1994
3 // Author:      Laurent PAINNOT
4 //              <lpa@metrox>
5
6
7 #ifdef DEBUG
8 #include <OSD_Timer.hxx>
9 extern OSD_Timer RestrictedFaceTimer1,RestrictedFaceTimer2,RestrictedFaceTimer3,RestrictedFaceTimer4;
10 #endif
11
12 #include <Hatch_Hatcher.hxx>
13 #include <Graphic3d_Array1OfVertex.hxx>
14 #include <Graphic3d_ArrayOfPrimitives.hxx>
15 #include <Graphic3d_Group.hxx>
16 #include <gp_Pnt.hxx>
17 #include <Prs3d_IsoAspect.hxx>
18 #include <Adaptor3d_IsoCurve.hxx>
19 #include <Bnd_Box2d.hxx>
20 #include <BndLib_Add2dCurve.hxx>
21 #include <Precision.hxx>
22 #include <GeomAdaptor_Curve.hxx>
23 #include <Geom_Curve.hxx>
24 #include <GeomAbs_SurfaceType.hxx>
25 #include <Geom_Surface.hxx>
26
27 //=========================================================================
28 // function: Add
29 // purpose
30 //=========================================================================
31 void Prs3d_WFRestrictedFace::Add
32   (const Handle (Prs3d_Presentation)&  aPresentation,
33    const Handle(BRepAdaptor_HSurface)& aFace,
34    const Standard_Boolean              DrawUIso,
35    const Standard_Boolean              DrawVIso,
36    const Quantity_Length               aDeflection,
37    const Standard_Integer              NBUiso,
38    const Standard_Integer              NBViso,
39    const Handle(Prs3d_Drawer)&         aDrawer,
40    Prs3d_NListOfSequenceOfPnt&         Curves)
41 {
42   Standard_Boolean isPA = Graphic3d_ArrayOfPrimitives::IsEnable();
43   Standard_Real aLimit = aDrawer->MaximalParameterValue();
44   Standard_Integer nbPoints = aDrawer->Discretisation();
45
46 #ifdef DEBUG
47   RestrictedFaceTimer1.Start();
48 #endif
49
50   RestrictionTool ToolRst (aFace);
51
52   // compute bounds of the restriction
53   Standard_Real UMin,UMax,VMin,VMax;
54 //  Standard_Real u,v,step;
55 //  Standard_Integer i, nbp= 10;
56   Standard_Integer i;
57   gp_Pnt2d P1,P2;
58   Bnd_Box2d B;
59   
60   for (ToolRst.Init(); ToolRst.More(); ToolRst.Next()) {
61     Adaptor2d_Curve2dPtr TheRCurve = ToolRst.Value();
62     BndLib_Add2dCurve::Add(*TheRCurve, Precision::PConfusion(), B);
63   }
64   if ( ! B.IsVoid() )
65     B.Get(UMin, VMin, UMax, VMax);
66   else
67   { // no pcurves -- take natural bounds
68     UMin = aFace->Surface().FirstUParameter();
69     VMin = aFace->Surface().FirstVParameter();
70     UMax = aFace->Surface().LastUParameter();
71     VMax = aFace->Surface().LastVParameter();
72   }
73
74 #ifdef DEBUG
75   RestrictedFaceTimer1.Stop();
76
77   RestrictedFaceTimer2.Start();
78 #endif
79
80   // load the isos
81   Hatch_Hatcher isobuild(1.e-5,ToolRst.IsOriented());
82   Standard_Boolean UClosed = aFace->IsUClosed();
83   Standard_Boolean VClosed = aFace->IsVClosed();
84
85   if ( ! UClosed ) {
86     UMin = UMin + ( UMax - UMin) /1000.;
87     UMax = UMax - ( UMax - UMin) /1000.; 
88   }
89
90   if ( ! VClosed ) {
91     VMin = VMin + ( VMax - VMin) /1000.;
92     VMax = VMax - ( VMax - VMin) /1000.; 
93   }
94
95   if (DrawUIso){
96     if (NBUiso > 0) {
97       UClosed = Standard_False; // En attendant un hatcher de course.
98       Standard_Real du= UClosed ? (UMax-UMin)/NBUiso : (UMax-UMin)/(1+NBUiso);
99       for (i=1; i<=NBUiso;i++){
100         isobuild.AddXLine(UMin+du*i);
101       }
102     }
103   }
104   if (DrawVIso){
105     if ( NBViso > 0) {
106       VClosed = Standard_False;
107       Standard_Real dv= VClosed ?(VMax-VMin)/NBViso : (VMax-VMin)/(1+NBViso);
108       for (i=1; i<=NBViso;i++){
109         isobuild.AddYLine(VMin+dv*i);
110       }
111     }
112   }
113
114 #ifdef DEBUG
115   RestrictedFaceTimer2.Stop();
116   RestrictedFaceTimer3.Start();
117 #endif
118
119   // trim the isos
120   Standard_Real U1, U2, U, DU;
121
122   for (ToolRst.Init(); ToolRst.More(); ToolRst.Next()) {
123     TopAbs_Orientation Orient = ToolRst.Orientation();
124     if ( Orient == TopAbs_FORWARD || Orient == TopAbs_REVERSED ) {
125       Adaptor2d_Curve2dPtr TheRCurve = ToolRst.Value();
126       U1 = TheRCurve->FirstParameter();
127       U2 = TheRCurve->LastParameter();
128       if (TheRCurve->GetType() != GeomAbs_Line) {
129         DU = (U2-U1)/(nbPoints-1);
130         P2 = TheRCurve->Value(U1);
131         for (i = 2; i <= nbPoints; i++) {
132           U = U1 + (i-1)*DU;
133           P1 = P2;
134           P2 = TheRCurve->Value(U);
135           if(Orient == TopAbs_FORWARD )
136             isobuild.Trim(P1,P2);
137           else
138             isobuild.Trim(P2,P1);
139         }
140       }
141       else {
142         P1 = TheRCurve->Value(U1);
143         P2 = TheRCurve->Value(U2);
144         if(Orient == TopAbs_FORWARD )
145           isobuild.Trim(P1,P2);
146         else
147           isobuild.Trim(P2,P1);
148       }   
149     }
150   }
151
152 #ifdef DEBUG  
153   RestrictedFaceTimer3.Stop();
154   RestrictedFaceTimer4.Start();
155 #endif
156
157   // draw the isos
158
159
160   Adaptor3d_IsoCurve anIso;
161   anIso.Load(aFace);
162   Handle(Geom_Curve) BC;
163   const BRepAdaptor_Surface& BS = *(BRepAdaptor_Surface*)&(aFace->Surface());
164   GeomAbs_SurfaceType thetype = aFace->GetType();
165
166   Standard_Integer NumberOfLines = isobuild.NbLines();
167   Handle(Geom_Surface) GB;
168   if (thetype == GeomAbs_BezierSurface) {
169     GB = BS.Bezier();
170   }
171   else if (thetype == GeomAbs_BSplineSurface){
172     GB = BS.BSpline();
173   }
174
175   for (i = 1; i <= NumberOfLines; i++) {
176     Standard_Integer NumberOfIntervals = isobuild.NbIntervals(i);
177     Standard_Real Coord = isobuild.Coordinate(i);
178     for (Standard_Integer j = 1; j <= NumberOfIntervals; j++) {
179       Standard_Real b1=isobuild.Start(i,j),b2=isobuild.End(i,j);
180
181       if(b1 == RealFirst() || b2 == RealLast())
182         continue;
183       //b1 = b1 == RealFirst() ? - aLimit : b1;
184       //b2 = b2 == RealLast()  ?   aLimit : b2;
185
186       TColgp_SequenceOfPnt Pnts;
187       if (!GB.IsNull()) {
188         if (isobuild.IsXLine(i))
189           BC = GB->UIso(Coord);
190         else 
191           BC = GB->VIso(Coord);
192         //Note that the isos are the part of the shape, it will be displayed after a computation the whole shape
193         //NbPoints = 30 - default parameter for computation of such curves
194         DrawFaceIso::Add(aPresentation,GeomAdaptor_Curve(BC), b1, b2, aDeflection, Pnts, 30, !isPA);
195         Curves.Append(Pnts);
196       }
197       else {
198         if (isobuild.IsXLine(i))
199           anIso.Load(GeomAbs_IsoU,Coord,b1,b2);
200         else
201           anIso.Load(GeomAbs_IsoV,Coord,b1,b2);
202         DrawFaceIso::Add(aPresentation,anIso, aDeflection, aDrawer, Pnts, !isPA);
203         Curves.Append(Pnts);
204       }
205     }
206   }
207 #ifdef DEBUG
208   RestrictedFaceTimer4.Stop();
209 #endif
210 }
211
212
213 //=========================================================================
214 // function: Match
215 // purpose
216 //=========================================================================
217 Standard_Boolean Prs3d_WFRestrictedFace::Match
218   (const Quantity_Length X,
219    const Quantity_Length Y,
220    const Quantity_Length Z,
221    const Quantity_Length aDistance,
222    const Handle(BRepAdaptor_HSurface)& aFace,
223    const Standard_Boolean DrawUIso,
224    const Standard_Boolean DrawVIso,
225    const Quantity_Length aDeflection,
226    const Standard_Integer NBUiso,
227    const Standard_Integer NBViso,
228    const Handle(Prs3d_Drawer)& aDrawer)
229 {
230   Standard_Real aLimit = aDrawer->MaximalParameterValue();
231   Standard_Integer nbPoints = aDrawer->Discretisation();
232   RestrictionTool ToolRst (aFace);
233
234   // compute bounds of the restriction
235   Standard_Real UMin,UMax,VMin,VMax;
236   Standard_Real u,v,step;
237   Standard_Integer i,nbP = 10;
238   UMin = VMin = RealLast();
239   UMax = VMax = RealFirst();
240   gp_Pnt2d P1,P2;
241
242   for (ToolRst.Init(); ToolRst.More(); ToolRst.Next()) {
243     Adaptor2d_Curve2dPtr TheRCurve = ToolRst.Value();
244     u = TheRCurve->FirstParameter();
245     v = TheRCurve->LastParameter();
246     if (TheRCurve->GetType() != GeomAbs_Line) {
247       step = ( v - u) / nbP;
248       for (i = 0; i <= nbP; i++) {
249         gp_Pnt2d P = TheRCurve->Value(u);
250         if (P.X() < UMin) UMin = P.X();
251         if (P.X() > UMax) UMax = P.X();
252         if (P.Y() < VMin) VMin = P.Y();
253         if (P.Y() > VMax) VMax = P.Y();
254         u += step;
255       }
256     }
257     else {
258       P1 = TheRCurve->Value(u);
259       if (P1.X() < UMin) UMin = P1.X();
260       if (P1.X() > UMax) UMax = P1.X();
261       if (P1.Y() < VMin) VMin = P1.Y();
262       if (P1.Y() > VMax) VMax = P1.Y();
263
264       P2 = TheRCurve->Value(v);
265       if (P2.X() < UMin) UMin = P2.X();
266       if (P2.X() > UMax) UMax = P2.X();
267       if (P2.Y() < VMin) VMin = P2.Y();
268       if (P2.Y() > VMax) VMax = P2.Y();
269     }
270   }
271
272   // load the isos
273   Hatch_Hatcher isobuild(1.e-5,ToolRst.IsOriented());
274   Standard_Boolean UClosed = aFace->IsUClosed();
275   Standard_Boolean VClosed = aFace->IsVClosed();
276
277   if ( ! UClosed ) {
278     UMin = UMin + ( UMax - UMin) /1000.;
279     UMax = UMax - ( UMax - UMin) /1000.; 
280   }
281
282   if ( ! VClosed ) {
283     VMin = VMin + ( VMax - VMin) /1000.;
284     VMax = VMax - ( VMax - VMin) /1000.; 
285   }
286
287   if (DrawUIso){
288     if (NBUiso > 0) {
289       UClosed = Standard_False; // En attendant un hatcher de course.
290       Standard_Real du= UClosed ? (UMax-UMin)/NBUiso : (UMax-UMin)/(1+NBUiso);
291       for (i=1; i<=NBUiso;i++){
292         isobuild.AddXLine(UMin+du*i);
293       }
294     }
295   }
296   if (DrawVIso){
297     if ( NBViso > 0) {
298       VClosed = Standard_False;
299       Standard_Real dv= VClosed ?(VMax-VMin)/NBViso : (VMax-VMin)/(1+NBViso);
300       for (i=1; i<=NBViso;i++){
301         isobuild.AddYLine(VMin+dv*i);
302       }
303     }
304   }
305
306   // trim the isos
307   Standard_Real U1, U2, U, DU;
308
309   for (ToolRst.Init(); ToolRst.More(); ToolRst.Next()) {
310     TopAbs_Orientation Orient = ToolRst.Orientation();
311     if ( Orient == TopAbs_FORWARD || Orient == TopAbs_REVERSED ) {
312       Adaptor2d_Curve2dPtr TheRCurve = ToolRst.Value();
313       U1 = TheRCurve->FirstParameter();
314       U2 = TheRCurve->LastParameter();
315       if (TheRCurve->GetType() != GeomAbs_Line) {
316         DU = (U2-U1)/(nbPoints-1);
317         P2 = TheRCurve->Value(U1);
318         for (i = 2; i <= nbPoints; i++) {
319           U = U1 + (i-1)*DU;
320           P1 = P2;
321           P2 = TheRCurve->Value(U);
322           if(Orient == TopAbs_FORWARD )
323             isobuild.Trim(P1,P2);
324           else
325             isobuild.Trim(P2,P1);
326         }
327       }
328       else {
329         P1 = TheRCurve->Value(U1);
330         P2 = TheRCurve->Value(U2);
331         if(Orient == TopAbs_FORWARD )
332           isobuild.Trim(P1,P2);
333         else
334           isobuild.Trim(P2,P1);
335       }   
336     }
337   }
338   
339   // draw the isos
340
341   Adaptor3d_IsoCurve anIso;
342   anIso.Load(aFace);
343   Standard_Integer NumberOfLines = isobuild.NbLines();
344
345   for (i = 1; i <= NumberOfLines; i++) {
346     Standard_Integer NumberOfIntervals = isobuild.NbIntervals(i);
347     Standard_Real Coord = isobuild.Coordinate(i);
348     for (Standard_Integer j = 1; j <= NumberOfIntervals; j++) {
349       Standard_Real b1=isobuild.Start(i,j),b2=isobuild.End(i,j);
350
351       b1 = b1 == RealFirst() ? - aLimit : b1;
352       b2 = b2 == RealLast()  ?   aLimit : b2;
353
354
355       if (isobuild.IsXLine(i))
356         anIso.Load(GeomAbs_IsoU,Coord,b1,b2);
357       else
358         anIso.Load(GeomAbs_IsoV,Coord,b1,b2);
359     
360       if (DrawFaceIso::Match(X,Y,Z,aDistance,anIso, 
361                              aDeflection, aLimit, nbPoints))
362           return Standard_True;
363         
364         
365     }
366   }
367     
368    return Standard_False;
369 }
370
371
372
373 //=========================================================================
374 // function: Add
375 // purpose
376 //=========================================================================
377 void Prs3d_WFRestrictedFace::Add
378   (const Handle (Prs3d_Presentation)& aPresentation,
379    const Handle(BRepAdaptor_HSurface)&    aFace,
380    const Handle (Prs3d_Drawer)&       aDrawer){
381
382   Standard_Integer finu = aDrawer->UIsoAspect()->Number();
383   Standard_Integer finv = aDrawer->VIsoAspect()->Number();
384   Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();
385
386   Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation);
387   TheGroup->BeginPrimitives();
388   Prs3d_NListOfSequenceOfPnt Curves;
389   Prs3d_WFRestrictedFace::Add (  
390                       aPresentation,
391                       aFace,
392                       Standard_True,
393                       Standard_True,
394                       aDeflection,
395                       finu,
396                       finv,
397                       aDrawer,
398                       Curves);
399
400   TheGroup->EndPrimitives();
401 }
402
403
404 //=========================================================================
405 // function: AddUIso
406 // purpose
407 //=========================================================================
408 void Prs3d_WFRestrictedFace::AddUIso
409   (const Handle (Prs3d_Presentation)& aPresentation,
410    const Handle(BRepAdaptor_HSurface)& aFace,
411    const Handle (Prs3d_Drawer)& aDrawer) {
412
413   Standard_Integer finu = aDrawer->UIsoAspect()->Number();
414   Standard_Integer finv = aDrawer->VIsoAspect()->Number();
415   Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();
416   Prs3d_NListOfSequenceOfPnt Curves;
417   Prs3d_WFRestrictedFace::Add ( 
418                       aPresentation,
419                       aFace,
420                       Standard_True,
421                       Standard_False,
422                       aDeflection,
423                       finu,
424                       finv,
425                       aDrawer,
426                       Curves);
427 }
428
429
430 //=========================================================================
431 // function: AddVIso
432 // purpose
433 //=========================================================================
434 void Prs3d_WFRestrictedFace::AddVIso
435   (const Handle (Prs3d_Presentation)& aPresentation,
436    const Handle(BRepAdaptor_HSurface)& aFace,
437    const Handle (Prs3d_Drawer)& aDrawer) {
438
439   Standard_Integer finu = aDrawer->UIsoAspect()->Number();
440   Standard_Integer finv = aDrawer->VIsoAspect()->Number();
441   Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();
442   Prs3d_NListOfSequenceOfPnt Curves;
443   Prs3d_WFRestrictedFace::Add ( 
444                       aPresentation,
445                       aFace,
446                       Standard_False,
447                       Standard_True,
448                       aDeflection,
449                       finu,
450                       finv,
451                       aDrawer,
452                       Curves);
453 }
454
455
456 //=========================================================================
457 // function: Match
458 // purpose
459 //=========================================================================
460 Standard_Boolean Prs3d_WFRestrictedFace::Match
461   (const Quantity_Length X,
462    const Quantity_Length Y,
463    const Quantity_Length Z,
464    const Quantity_Length aDistance,
465    const Handle(BRepAdaptor_HSurface)& aFace,
466    const Handle (Prs3d_Drawer)& aDrawer){
467
468   Standard_Integer finu = aDrawer->UIsoAspect()->Number();
469   Standard_Integer finv = aDrawer->VIsoAspect()->Number();
470   Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();
471   return Prs3d_WFRestrictedFace::Match (  
472                       X,Y,Z,aDistance,
473                       aFace,
474                       Standard_True,
475                       Standard_True,
476                       aDeflection,
477                       finu,
478                       finv,
479                       aDrawer);
480 }
481
482
483 //=========================================================================
484 // function: MatchUIso
485 // purpose
486 //=========================================================================
487 Standard_Boolean Prs3d_WFRestrictedFace::MatchUIso
488   (const Quantity_Length X,
489    const Quantity_Length Y,
490    const Quantity_Length Z,
491    const Quantity_Length aDistance,
492    const Handle(BRepAdaptor_HSurface)& aFace,
493    const Handle (Prs3d_Drawer)& aDrawer) {
494
495   Standard_Integer finu = aDrawer->UIsoAspect()->Number();
496   Standard_Integer finv = aDrawer->VIsoAspect()->Number();
497   Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();
498   return Prs3d_WFRestrictedFace::Match ( 
499                       X,Y,Z,aDistance,
500                       aFace,
501                       Standard_True,
502                       Standard_False,
503                       aDeflection,
504                       finu,
505                       finv,
506                       aDrawer);
507 }
508
509
510 //=========================================================================
511 // function: MatchVIso
512 // purpose
513 //=========================================================================
514 Standard_Boolean Prs3d_WFRestrictedFace::MatchVIso
515   (const Quantity_Length X,
516    const Quantity_Length Y,
517    const Quantity_Length Z,
518    const Quantity_Length aDistance,
519    const Handle(BRepAdaptor_HSurface)& aFace,
520    const Handle (Prs3d_Drawer)& aDrawer) {
521
522   Standard_Integer finu = aDrawer->UIsoAspect()->Number();
523   Standard_Integer finv = aDrawer->VIsoAspect()->Number();
524   Quantity_Length aDeflection = aDrawer->MaximalChordialDeviation();
525   return Prs3d_WFRestrictedFace::Match ( 
526                       X,Y,Z,aDistance,
527                       aFace,
528                       Standard_False,
529                       Standard_True,
530                       aDeflection,
531                       finu,
532                       finv,
533                       aDrawer);
534 }