0023959: Getting rid of generic classes in Visualization: generic classes were remove...
[occt.git] / src / StdPrs / StdPrs_WFRestrictedFace.cxx
1 // Copyright (c) 2013 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18
19 #include <Hatch_Hatcher.hxx>
20 #include <Graphic3d_Group.hxx>
21 #include <gp_Pnt.hxx>
22 #include <Prs3d_IsoAspect.hxx>
23 #include <Adaptor3d_IsoCurve.hxx>
24 #include <Bnd_Box2d.hxx>
25 #include <BndLib_Add2dCurve.hxx>
26 #include <Precision.hxx>
27 #include <GeomAdaptor_Curve.hxx>
28 #include <Geom_Curve.hxx>
29 #include <GeomAbs_SurfaceType.hxx>
30 #include <Geom_Surface.hxx>
31 #include <StdPrs_WFRestrictedFace.hxx>
32 #include <StdPrs_ToolRFace.hxx>
33 #include <StdPrs_Curve.hxx>
34 #include <Prs3d_NListOfSequenceOfPnt.hxx>
35
36 //=========================================================================
37 // function: Add
38 // purpose
39 //=========================================================================
40
41 void StdPrs_WFRestrictedFace::Add
42                 (const Handle (Prs3d_Presentation)&  thePresentation,
43                  const Handle(BRepAdaptor_HSurface)& theFace,
44                  const Standard_Boolean              theDrawUIso,
45                  const Standard_Boolean              theDrawVIso,
46                  const Quantity_Length               theDeflection,
47                  const Standard_Integer              theNbUIso,
48                  const Standard_Integer              theNBVIso,
49                  const Handle(Prs3d_Drawer)&         theDrawer,
50                  Prs3d_NListOfSequenceOfPnt&         theCurves)
51 {
52   Standard_Real aLimit = theDrawer->MaximalParameterValue();
53   Standard_Integer aNbPoints = theDrawer->Discretisation();
54   StdPrs_ToolRFace aToolRst (theFace);
55
56   // Compute bounds of the restriction
57   Standard_Real aUMin,aUMax,aVMin,aVMax;
58   Standard_Integer anI;
59   gp_Pnt2d aPoint1,aPoint2;
60   Bnd_Box2d aBndBox;
61
62   for (aToolRst.Init(); aToolRst.More(); aToolRst.Next())
63   {
64     Adaptor2d_Curve2dPtr aRCurve = aToolRst.Value();
65     BndLib_Add2dCurve::Add(*aRCurve, Precision::PConfusion(), aBndBox);
66   }
67   if (!aBndBox.IsVoid())
68     aBndBox.Get(aUMin, aVMin, aUMax, aVMax);
69   else
70   { // No pcurves -- take natural bounds
71     aUMin = theFace->Surface().FirstUParameter();
72     aVMin = theFace->Surface().FirstVParameter();
73     aUMax = theFace->Surface().LastUParameter();
74     aVMax = theFace->Surface().LastVParameter();
75   }
76
77   // Load the isos
78   Hatch_Hatcher anIsoBuild(1.e-5,aToolRst.IsOriented());
79   Standard_Boolean isFaceUClosed = theFace->IsUClosed();
80   Standard_Boolean isFaceVClosed = theFace->IsVClosed();
81
82   if (!isFaceUClosed)
83   {
84     aUMin = aUMin + ( aUMax - aUMin) /1000.;
85     aUMax = aUMax - ( aUMax - aUMin) /1000.; 
86   }
87
88   if (!isFaceVClosed)
89   {
90     aVMin = aVMin + ( aVMax - aVMin) /1000.;
91     aVMax = aVMax - ( aVMax - aVMin) /1000.; 
92   }
93
94   if (theDrawUIso)
95   {
96     if (theNbUIso > 0)
97     {
98       isFaceUClosed = Standard_False;
99       Standard_Real du= isFaceUClosed ? (aUMax-aUMin)/theNbUIso : (aUMax-aUMin)/(1+theNbUIso);
100       for (anI=1; anI<=theNbUIso;anI++)
101       {
102         anIsoBuild.AddXLine(aUMin+du*anI);
103       }
104     }
105   }
106   if (theDrawVIso)
107   {
108     if (theNBVIso > 0)
109     {
110       isFaceVClosed = Standard_False;
111       Standard_Real dv= isFaceVClosed ?(aVMax-aVMin)/theNBVIso : (aVMax-aVMin)/(1+theNBVIso);
112       for (anI=1; anI<=theNBVIso;anI++)
113       {
114         anIsoBuild.AddYLine(aVMin+dv*anI);
115       }
116     }
117   }
118
119   // Trim the isos
120   Standard_Real anU1, anU2, anU, aDU;
121
122   for (aToolRst.Init(); aToolRst.More(); aToolRst.Next())
123   {
124     TopAbs_Orientation anOrientation = aToolRst.Orientation();
125     if (anOrientation == TopAbs_FORWARD || anOrientation == TopAbs_REVERSED)
126     {
127       Adaptor2d_Curve2dPtr aRCurve = aToolRst.Value();
128       anU1 = aRCurve->FirstParameter();
129       anU2 = aRCurve->LastParameter();
130       if (aRCurve->GetType() != GeomAbs_Line)
131       {
132         aDU = (anU2-anU1)/(aNbPoints-1);
133         aPoint2 = aRCurve->Value(anU1);
134         for (anI = 2; anI <= aNbPoints; ++anI)
135         {
136           anU = anU1 + (anI-1)*aDU;
137           aPoint1 = aPoint2;
138           aPoint2 = aRCurve->Value(anU);
139           if(anOrientation == TopAbs_FORWARD )
140             anIsoBuild.Trim(aPoint1,aPoint2);
141           else
142             anIsoBuild.Trim(aPoint2,aPoint1);
143         }
144       }
145       else {
146         aPoint1 = aRCurve->Value(anU1);
147         aPoint2 = aRCurve->Value(anU2);
148         if(anOrientation == TopAbs_FORWARD )
149           anIsoBuild.Trim(aPoint1,aPoint2);
150         else
151           anIsoBuild.Trim(aPoint2,aPoint1);
152       }
153     }
154   }
155
156   // Draw the isos
157   Adaptor3d_IsoCurve anIsoCurve;
158   anIsoCurve.Load(theFace);
159   Handle(Geom_Curve) aBCurve;
160   const BRepAdaptor_Surface& aBSurface = *(BRepAdaptor_Surface*)&(theFace->Surface());
161   GeomAbs_SurfaceType aFaceType = theFace->GetType();
162
163   Standard_Integer aNbLines = anIsoBuild.NbLines();
164   Handle(Geom_Surface) aGeomBSurface;
165   if (aFaceType == GeomAbs_BezierSurface)
166   {
167     aGeomBSurface = aBSurface.Bezier();
168   }
169   else if (aFaceType == GeomAbs_BSplineSurface)
170   {
171     aGeomBSurface = aBSurface.BSpline();
172   }
173
174   for (anI = 1; anI <= aNbLines; ++anI)
175   {
176     Standard_Integer NumberOfIntervals = anIsoBuild.NbIntervals(anI);
177     Standard_Real anIsoCoord = anIsoBuild.Coordinate(anI);
178     for (Standard_Integer aJ = 1; aJ <= NumberOfIntervals; aJ++)
179     {
180       Standard_Real b1=anIsoBuild.Start(anI,aJ),b2=anIsoBuild.End(anI,aJ);
181
182       if(b1 == RealFirst() || b2 == RealLast())
183         continue;
184
185       TColgp_SequenceOfPnt aPoints;
186       if (!aGeomBSurface.IsNull())
187       {
188         if (anIsoBuild.IsXLine(anI))
189           aBCurve = aGeomBSurface->UIso(anIsoCoord);
190         else 
191           aBCurve = aGeomBSurface->VIso(anIsoCoord);
192
193         //Note that the isos are the part of the shape, it will be displayed after a computation the whole shape
194         //NbPoints = 30 - default parameter for computation of such curves
195         StdPrs_Curve::Add(thePresentation,GeomAdaptor_Curve(aBCurve), b1, b2, theDeflection, aPoints, 30, Standard_False);
196         theCurves.Append(aPoints);
197       }
198       else
199       {
200         if (anIsoBuild.IsXLine(anI))
201           anIsoCurve.Load(GeomAbs_IsoU,anIsoCoord,b1,b2);
202         else
203           anIsoCurve.Load(GeomAbs_IsoV,anIsoCoord,b1,b2);
204         StdPrs_Curve::Add(thePresentation,anIsoCurve, theDeflection, theDrawer, aPoints, Standard_False);
205         theCurves.Append(aPoints);
206       }
207     }
208   }
209 }
210
211
212 //=========================================================================
213 // function: Match
214 // purpose
215 //=========================================================================
216
217 Standard_Boolean StdPrs_WFRestrictedFace::Match
218                   (const Quantity_Length               theX,
219                    const Quantity_Length               theY,
220                    const Quantity_Length               theZ,
221                    const Quantity_Length               theDistance,
222                    const Handle(BRepAdaptor_HSurface)& theFace,
223                    const Standard_Boolean              theDrawUIso,
224                    const Standard_Boolean              theDrawVIso,
225                    const Quantity_Length               theDeflection,
226                    const Standard_Integer              theNbUIso,
227                    const Standard_Integer              theNBVIso,
228                    const Handle(Prs3d_Drawer)&         theDrawer)
229 {
230   Standard_Real aLimit = theDrawer->MaximalParameterValue();
231   Standard_Integer aNbPoints = theDrawer->Discretisation();
232   StdPrs_ToolRFace aToolRst (theFace);
233
234   // Compute bounds of the restriction
235   Standard_Real anUMin,anUMax,aVMin,aVMax;
236   Standard_Real anU,aV,aStep;
237   Standard_Integer anI,anNbP = 10;
238   anUMin = aVMin = RealLast();
239   anUMax = aVMax = RealFirst();
240   gp_Pnt2d aPoint1,aPoint2;
241
242   for (aToolRst.Init(); aToolRst.More(); aToolRst.Next())
243   {
244     Adaptor2d_Curve2dPtr aRCurve = aToolRst.Value();
245     anU = aRCurve->FirstParameter();
246     aV = aRCurve->LastParameter();
247     if (aRCurve->GetType() != GeomAbs_Line)
248     {
249       aStep = ( aV - anU) / anNbP;
250       for (anI = 0; anI <= anNbP; ++anI)
251       {
252         gp_Pnt2d aRCurvePoint = aRCurve->Value(anU);
253         if (aRCurvePoint.X() < anUMin) anUMin = aRCurvePoint.X();
254         if (aRCurvePoint.X() > anUMax) anUMax = aRCurvePoint.X();
255         if (aRCurvePoint.Y() < aVMin) aVMin = aRCurvePoint.Y();
256         if (aRCurvePoint.Y() > aVMax) aVMax = aRCurvePoint.Y();
257         anU += aStep;
258       }
259     }
260     else
261     {
262       aPoint1 = aRCurve->Value(anU);
263       if (aPoint1.X() < anUMin) anUMin = aPoint1.X();
264       if (aPoint1.X() > anUMax) anUMax = aPoint1.X();
265       if (aPoint1.Y() < aVMin) aVMin = aPoint1.Y();
266       if (aPoint1.Y() > aVMax) aVMax = aPoint1.Y();
267
268       aPoint2 = aRCurve->Value(aV);
269       if (aPoint2.X() < anUMin) anUMin = aPoint2.X();
270       if (aPoint2.X() > anUMax) anUMax = aPoint2.X();
271       if (aPoint2.Y() < aVMin) aVMin = aPoint2.Y();
272       if (aPoint2.Y() > aVMax) aVMax = aPoint2.Y();
273     }
274   }
275
276   // Load the isos
277   Hatch_Hatcher anIsoBuild(1.e-5,aToolRst.IsOriented());
278   Standard_Boolean anUClosed = theFace->IsUClosed();
279   Standard_Boolean aVClosed = theFace->IsVClosed();
280
281   if ( ! anUClosed )
282   {
283     anUMin = anUMin + ( anUMax - anUMin) /1000.;
284     anUMax = anUMax - ( anUMax - anUMin) /1000.; 
285   }
286
287   if ( ! aVClosed )
288   {
289     aVMin = aVMin + ( aVMax - aVMin) /1000.;
290     aVMax = aVMax - ( aVMax - aVMin) /1000.; 
291   }
292
293   if (theDrawUIso)
294   {
295     if (theNbUIso > 0)
296     {
297       anUClosed = Standard_False;
298       Standard_Real du= anUClosed ? (anUMax-anUMin)/theNbUIso : (anUMax-anUMin)/(1+theNbUIso);
299       for (anI=1; anI<=theNbUIso;anI++){
300         anIsoBuild.AddXLine(anUMin+du*anI);
301       }
302     }
303   }
304   if (theDrawVIso){
305     if ( theNBVIso > 0) {
306       aVClosed = Standard_False;
307       Standard_Real dv= aVClosed ?(aVMax-aVMin)/theNBVIso : (aVMax-aVMin)/(1+theNBVIso);
308       for (anI=1; anI<=theNBVIso;anI++){
309         anIsoBuild.AddYLine(aVMin+dv*anI);
310       }
311     }
312   }
313
314   // Trim the isos
315   Standard_Real anU1, anU2, aDU;
316
317   for (aToolRst.Init(); aToolRst.More(); aToolRst.Next())
318   {
319     TopAbs_Orientation Orient = aToolRst.Orientation();
320     if (Orient == TopAbs_FORWARD || Orient == TopAbs_REVERSED)
321     {
322       Adaptor2d_Curve2dPtr aRCurve = aToolRst.Value();
323       anU1 = aRCurve->FirstParameter();
324       anU2 = aRCurve->LastParameter();
325       if (aRCurve->GetType() != GeomAbs_Line) {
326         aDU = (anU2-anU1)/(aNbPoints-1);
327         aPoint2 = aRCurve->Value(anU1);
328         for (anI = 2; anI <= aNbPoints; anI++) {
329           anU = anU1 + (anI-1)*aDU;
330           aPoint1 = aPoint2;
331           aPoint2 = aRCurve->Value(anU);
332           if(Orient == TopAbs_FORWARD )
333             anIsoBuild.Trim(aPoint1,aPoint2);
334           else
335             anIsoBuild.Trim(aPoint2,aPoint1);
336         }
337       }
338       else {
339         aPoint1 = aRCurve->Value(anU1);
340         aPoint2 = aRCurve->Value(anU2);
341         if(Orient == TopAbs_FORWARD )
342           anIsoBuild.Trim(aPoint1,aPoint2);
343         else
344           anIsoBuild.Trim(aPoint2,aPoint1);
345       }   
346     }
347   }
348
349   // Draw the isos
350
351   Adaptor3d_IsoCurve anIso;
352   anIso.Load(theFace);
353   Standard_Integer aNbLines = anIsoBuild.NbLines();
354
355   for (anI = 1; anI <= aNbLines; anI++)
356   {
357     Standard_Integer aNbIntervals = anIsoBuild.NbIntervals(anI);
358     Standard_Real aCoord = anIsoBuild.Coordinate(anI);
359     for (Standard_Integer j = 1; j <= aNbIntervals; j++)
360     {
361       Standard_Real anIsoStart=anIsoBuild.Start(anI,j),anIsoEnd=anIsoBuild.End(anI,j);
362
363       anIsoStart = anIsoStart == RealFirst() ? - aLimit : anIsoStart;
364       anIsoEnd = anIsoEnd == RealLast()  ?   aLimit : anIsoEnd;
365
366
367       if (anIsoBuild.IsXLine(anI))
368         anIso.Load(GeomAbs_IsoU,aCoord,anIsoStart,anIsoEnd);
369       else
370         anIso.Load(GeomAbs_IsoV,aCoord,anIsoStart,anIsoEnd);
371
372       if (StdPrs_Curve::Match(theX,theY,theZ,theDistance,anIso, 
373         theDeflection, aLimit, aNbPoints))
374         return Standard_True;
375
376
377     }
378   }
379   return Standard_False;
380 }
381
382
383 //=========================================================================
384 // function: Add
385 // purpose
386 //=========================================================================
387
388 void StdPrs_WFRestrictedFace::Add
389     (const Handle (Prs3d_Presentation)&  thePresentation,
390      const Handle(BRepAdaptor_HSurface)& theFace,
391      const Handle (Prs3d_Drawer)&        theDrawer)
392 {
393   Prs3d_NListOfSequenceOfPnt aCurves;
394   StdPrs_WFRestrictedFace::Add (thePresentation,
395                                 theFace,
396                                 Standard_True,
397                                 Standard_True,
398                                 theDrawer->MaximalChordialDeviation(),
399                                 theDrawer->UIsoAspect()->Number(),
400                                 theDrawer->VIsoAspect()->Number(),
401                                 theDrawer,
402                                 aCurves);
403 }
404
405
406 //=========================================================================
407 // function: AddUIso
408 // purpose
409 //=========================================================================
410
411 void StdPrs_WFRestrictedFace::AddUIso
412     (const Handle (Prs3d_Presentation)&  thePresentation,
413      const Handle(BRepAdaptor_HSurface)& theFace,
414      const Handle (Prs3d_Drawer)&        theDrawer)
415 {
416   Prs3d_NListOfSequenceOfPnt aCurves;
417   StdPrs_WFRestrictedFace::Add (thePresentation,
418                                 theFace,
419                                 Standard_True,
420                                 Standard_False,
421                                 theDrawer->MaximalChordialDeviation(),
422                                 theDrawer->UIsoAspect()->Number(),
423                                 theDrawer->VIsoAspect()->Number(),
424                                 theDrawer,
425                                 aCurves);
426 }
427
428
429 //=========================================================================
430 // function: AddVIso
431 // purpose
432 //=========================================================================
433
434 void StdPrs_WFRestrictedFace::AddVIso
435     (const Handle (Prs3d_Presentation)&  thePresentation,
436      const Handle(BRepAdaptor_HSurface)& theFace,
437      const Handle (Prs3d_Drawer)&        theDrawer)
438 {
439   Prs3d_NListOfSequenceOfPnt aCurves;
440   StdPrs_WFRestrictedFace::Add (thePresentation,
441                                 theFace,
442                                 Standard_False,
443                                 Standard_True,
444                                 theDrawer->MaximalChordialDeviation(),
445                                 theDrawer->UIsoAspect()->Number(),
446                                 theDrawer->VIsoAspect()->Number(),
447                                 theDrawer,
448                                 aCurves);
449 }
450
451
452 //=========================================================================
453 // function: Match
454 // purpose
455 //=========================================================================
456
457 Standard_Boolean StdPrs_WFRestrictedFace::Match
458                 (const Quantity_Length               theX,
459                  const Quantity_Length               theY,
460                  const Quantity_Length               theZ,
461                  const Quantity_Length               theDistance,
462                  const Handle(BRepAdaptor_HSurface)& theFace,
463                  const Handle (Prs3d_Drawer)&        theDrawer)
464 {
465   return StdPrs_WFRestrictedFace::Match (
466                       theX, theY, theZ, theDistance,
467                       theFace,
468                       Standard_True,
469                       Standard_True,
470                       theDrawer->MaximalChordialDeviation(),
471                       theDrawer->UIsoAspect()->Number(),
472                       theDrawer->VIsoAspect()->Number(),
473                       theDrawer);
474 }
475
476
477 //=========================================================================
478 // function: MatchUIso
479 // purpose
480 //=========================================================================
481
482 Standard_Boolean StdPrs_WFRestrictedFace::MatchUIso
483                 (const Quantity_Length               theX,
484                  const Quantity_Length               theY,
485                  const Quantity_Length               theZ,
486                  const Quantity_Length               theDistance,
487                  const Handle(BRepAdaptor_HSurface)& theFace,
488                  const Handle (Prs3d_Drawer)&        theDrawer)
489 {
490   return StdPrs_WFRestrictedFace::Match (
491                       theX, theY, theZ,theDistance,
492                       theFace,
493                       Standard_True,
494                       Standard_False,
495                       theDrawer->MaximalChordialDeviation(),
496                       theDrawer->UIsoAspect()->Number(),
497                       theDrawer->VIsoAspect()->Number(),
498                       theDrawer);
499 }
500
501
502 //=========================================================================
503 // function: MatchVIso
504 // purpose
505 //=========================================================================
506
507 Standard_Boolean StdPrs_WFRestrictedFace::MatchVIso
508                 (const Quantity_Length               theX,
509                  const Quantity_Length               theY,
510                  const Quantity_Length               theZ,
511                  const Quantity_Length               theDistance,
512                  const Handle(BRepAdaptor_HSurface)& theFace,
513                  const Handle (Prs3d_Drawer)&        theDrawer)
514 {
515   return StdPrs_WFRestrictedFace::Match (
516                       theX, theY, theZ, theDistance,
517                       theFace,
518                       Standard_False,
519                       Standard_True,
520                       theDrawer->MaximalChordialDeviation(),
521                       theDrawer->UIsoAspect()->Number(),
522                       theDrawer->VIsoAspect()->Number(),
523                       theDrawer);
524 }