1 // Created on: 1995-08-07
2 // Created by: Modelistation
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <StdPrs_WFDeflectionRestrictedFace.ixx>
19 #include <Hatch_Hatcher.hxx>
20 #include <Graphic3d_ArrayOfPolylines.hxx>
21 #include <Graphic3d_AspectLine3d.hxx>
22 #include <Graphic3d_Group.hxx>
24 #include <gp_Pnt2d.hxx>
25 #include <Prs3d_IsoAspect.hxx>
26 #include <Adaptor2d_Curve2d.hxx>
27 #include <GCPnts_QuasiUniformDeflection.hxx>
28 #include <Adaptor3d_IsoCurve.hxx>
29 #include <StdPrs_DeflectionCurve.hxx>
30 #include <StdPrs_ToolRFace.hxx>
31 #include <Bnd_Box2d.hxx>
32 #include <BndLib_Add2dCurve.hxx>
33 #include <Precision.hxx>
34 #include <GeomAdaptor_Curve.hxx>
35 #include <Geom_Curve.hxx>
36 #include <GeomAbs_SurfaceType.hxx>
37 #include <Geom_Surface.hxx>
38 #include <TColgp_SequenceOfPnt2d.hxx>
44 #include <OSD_Chronometer.hxx>
45 extern OSD_Chronometer FFaceTimer1,FFaceTimer2,FFaceTimer3,FFaceTimer4;
49 //==================================================================
50 // function: FindLimits
52 //==================================================================
53 static void FindLimits(const Adaptor3d_Curve& aCurve,
54 const Standard_Real aLimit,
58 First = Max(aCurve.FirstParameter(), First);
59 Last = Min(aCurve.LastParameter(), Last);
60 Standard_Boolean firstInf = Precision::IsNegativeInfinite(First);
61 Standard_Boolean lastInf = Precision::IsPositiveInfinite(Last);
63 if (firstInf || lastInf) {
65 Standard_Real delta = 1;
66 if (firstInf && lastInf) {
73 } while (P1.Distance(P2) < aLimit);
81 } while (P1.Distance(P2) < aLimit);
89 } while (P1.Distance(P2) < aLimit);
96 //=========================================================================
99 //=========================================================================
100 void StdPrs_WFDeflectionRestrictedFace::Add
101 (const Handle (Prs3d_Presentation)& aPresentation,
102 const Handle(BRepAdaptor_HSurface)& aFace,
103 const Standard_Boolean DrawUIso,
104 const Standard_Boolean DrawVIso,
105 const Quantity_Length Deflection,
106 const Standard_Integer NBUiso,
107 const Standard_Integer NBViso,
108 const Handle(Prs3d_Drawer)& aDrawer,
109 Prs3d_NListOfSequenceOfPnt& Curves) {
115 StdPrs_ToolRFace ToolRst (aFace);
116 Standard_Real UF, UL, VF, VL;
117 UF = aFace->FirstUParameter();
118 UL = aFace->LastUParameter();
119 VF = aFace->FirstVParameter();
120 VL = aFace->LastVParameter();
122 Standard_Real aLimit = aDrawer->MaximalParameterValue();
124 // compute bounds of the restriction
125 Standard_Real UMin,UMax,VMin,VMax;
126 //Standard_Real u,v,step;
127 Standard_Integer i;//,nbPoints = 10;
129 UMin = Max(UF, -aLimit);
130 UMax = Min(UL, aLimit);
131 VMin = Max(VF, -aLimit);
132 VMax = Min(VL, aLimit);
135 // update min max for the hatcher.
137 Standard_Real U1, U2;
139 Standard_Real ddefle= Max(UMax-UMin, VMax-VMin) * aDrawer->DeviationCoefficient();
140 TColgp_SequenceOfPnt2d tabP;
141 Standard_Real aHatchingTol = 1.e100;
143 UMin = VMin = 1.e100;
144 UMax = VMax = -1.e100;
146 for (ToolRst.Init(); ToolRst.More(); ToolRst.Next()) {
147 TopAbs_Orientation Orient = ToolRst.Orientation();
148 if ( Orient == TopAbs_FORWARD || Orient == TopAbs_REVERSED ) {
149 Adaptor2d_Curve2dPtr TheRCurve = ToolRst.Value();
150 if (TheRCurve->GetType() != GeomAbs_Line) {
151 GCPnts_QuasiUniformDeflection UDP(*TheRCurve, ddefle);
153 Standard_Integer NumberOfPoints = UDP.NbPoints();
154 if ( NumberOfPoints >= 2 ) {
155 dummypnt = UDP.Value(1);
156 P2.SetCoord(dummypnt.X(), dummypnt.Y());
157 UMin = Min(P2.X(), UMin);
158 UMax = Max(P2.X(), UMax);
159 VMin = Min(P2.Y(), VMin);
160 VMax = Max(P2.Y(), VMax);
161 for (i = 2; i <= NumberOfPoints; i++) {
163 dummypnt = UDP.Value(i);
164 P2.SetCoord(dummypnt.X(), dummypnt.Y());
165 UMin = Min(P2.X(), UMin);
166 UMax = Max(P2.X(), UMax);
167 VMin = Min(P2.Y(), VMin);
168 VMax = Max(P2.Y(), VMax);
169 aHatchingTol = Min(P1.SquareDistance(P2), aHatchingTol);
171 if(Orient == TopAbs_FORWARD ) {
172 //isobuild.Trim(P1,P2);
177 //isobuild.Trim(P2,P1);
185 cout << "Cannot evaluate curve on surface"<<endl;
189 U1 = TheRCurve->FirstParameter();
190 U2 = TheRCurve->LastParameter();
191 // MSV 17.08.06 OCC13144: U2 occured less than U1, to overcome it
192 // ensure that distance U2-U1 is not greater than aLimit*2,
193 // if greater then choose an origin and use aLimit to define
195 Standard_Real aOrigin = 0.;
196 if (!Precision::IsNegativeInfinite(U1) || !Precision::IsPositiveInfinite(U2)) {
197 if (Precision::IsNegativeInfinite(U1))
198 aOrigin = U2 - aLimit;
199 else if (Precision::IsPositiveInfinite(U2))
200 aOrigin = U1 + aLimit;
202 aOrigin = (U1 + U2) * 0.5;
204 U1 = Max(aOrigin - aLimit, U1);
205 U2 = Min(aOrigin + aLimit, U2);
206 P1 = TheRCurve->Value(U1);
207 P2 = TheRCurve->Value(U2);
208 UMin = Min(P1.X(), UMin);
209 UMax = Max(P1.X(), UMax);
210 VMin = Min(P1.Y(), VMin);
211 VMax = Max(P1.Y(), VMax);
212 UMin = Min(P2.X(), UMin);
213 UMax = Max(P2.X(), UMax);
214 VMin = Min(P2.Y(), VMin);
215 VMax = Max(P2.Y(), VMax);
216 aHatchingTol = Min(P1.SquareDistance(P2), aHatchingTol);
218 if(Orient == TopAbs_FORWARD ) {
219 // isobuild.Trim(P1,P2);
224 //isobuild.Trim(P2,P1);
239 // Compute the hatching tolerance.
241 aHatchingTol = Max(Precision::Confusion(), aHatchingTol);
242 aHatchingTol = Min(1.e-5, aHatchingTol);
245 Hatch_Hatcher isobuild(aHatchingTol, ToolRst.IsOriented());
246 Standard_Boolean UClosed = aFace->IsUClosed();
247 Standard_Boolean VClosed = aFace->IsVClosed();
250 UMin = UMin + ( UMax - UMin) /1000.;
251 UMax = UMax - ( UMax - UMin) /1000.;
255 VMin = VMin + ( VMax - VMin) /1000.;
256 VMax = VMax - ( VMax - VMin) /1000.;
261 UClosed = Standard_False; // En attendant un hatcher de course.
262 Standard_Real du= UClosed ? (UMax-UMin)/NBUiso : (UMax-UMin)/(1+NBUiso);
263 for (i=1; i<=NBUiso;i++){
264 isobuild.AddXLine(UMin+du*i);
270 VClosed = Standard_False;
271 Standard_Real dv= VClosed ?(VMax-VMin)/NBViso : (VMax-VMin)/(1+NBViso);
272 for (i=1; i<=NBViso;i++){
273 isobuild.AddYLine(VMin+dv*i);
284 Standard_Integer ll = tabP.Length();
285 for (i = 1; i <= ll; i+=2) {
286 isobuild.Trim(tabP(i),tabP(i+1));
297 Adaptor3d_IsoCurve anIso;
299 Handle(Geom_Curve) BC;
300 const BRepAdaptor_Surface& BS = *(BRepAdaptor_Surface*)&(aFace->Surface());
301 GeomAbs_SurfaceType thetype = aFace->GetType();
303 Standard_Integer NumberOfLines = isobuild.NbLines();
304 Handle(Geom_Surface) GB;
305 if (thetype == GeomAbs_BezierSurface) {
308 else if (thetype == GeomAbs_BSplineSurface){
312 Standard_Real anAngle = aDrawer->DeviationAngle();
314 for (i = 1; i <= NumberOfLines; i++) {
315 Standard_Integer NumberOfIntervals = isobuild.NbIntervals(i);
316 Standard_Real Coord = isobuild.Coordinate(i);
317 for (Standard_Integer j = 1; j <= NumberOfIntervals; j++) {
318 Standard_Real b1=isobuild.Start(i,j),b2=isobuild.End(i,j);
322 if (isobuild.IsXLine(i))
323 BC = GB->UIso(Coord);
325 BC = GB->VIso(Coord);
326 GeomAdaptor_Curve GC(BC);
327 FindLimits(GC, aLimit,b1, b2);
328 if (b2-b1>Precision::Confusion()) {
329 Handle(TColgp_HSequenceOfPnt) Points = new TColgp_HSequenceOfPnt;
330 StdPrs_DeflectionCurve::Add (aPresentation, GC, b1, b2, Deflection,
331 Points->ChangeSequence(), anAngle, Standard_False);
332 Curves.Append(Points);
336 if (isobuild.IsXLine(i))
337 anIso.Load(GeomAbs_IsoU,Coord,b1,b2);
339 anIso.Load(GeomAbs_IsoV,Coord,b1,b2);
340 FindLimits(anIso, aLimit,b1, b2);
341 if (b2-b1>Precision::Confusion()) {
342 Handle(TColgp_HSequenceOfPnt) Points = new TColgp_HSequenceOfPnt;
343 StdPrs_DeflectionCurve::Add (aPresentation, anIso, b1, b2, Deflection,
344 Points->ChangeSequence(), anAngle, Standard_False);
345 Curves.Append(Points);
356 //=========================================================================
359 //=========================================================================
360 Standard_Boolean StdPrs_WFDeflectionRestrictedFace::Match
361 (const Quantity_Length X,
362 const Quantity_Length Y,
363 const Quantity_Length Z,
364 const Quantity_Length aDistance,
365 const Handle(BRepAdaptor_HSurface)& aFace,
366 const Handle(Prs3d_Drawer)& aDrawer,
367 const Standard_Boolean DrawUIso,
368 const Standard_Boolean DrawVIso,
369 const Quantity_Length Deflection,
370 const Standard_Integer NBUiso,
371 const Standard_Integer NBViso)
374 StdPrs_ToolRFace ToolRst (aFace);
375 const Standard_Real aLimit = aDrawer->MaximalParameterValue();
377 // compute bounds of the restriction
378 Standard_Real UMin,UMax,VMin,VMax;
379 Standard_Real u,v,step;
380 Standard_Integer i,nbPoints = 10;
381 UMin = VMin = RealLast();
382 UMax = VMax = RealFirst();
384 for (ToolRst.Init(); ToolRst.More(); ToolRst.Next()) {
385 Adaptor2d_Curve2dPtr TheRCurve = ToolRst.Value();
386 u = TheRCurve->FirstParameter();
387 v = TheRCurve->LastParameter();
388 step = ( v - u) / nbPoints;
389 for (i = 0; i <= nbPoints; i++) {
390 gp_Pnt2d P = TheRCurve->Value(u);
391 if (P.X() < UMin) UMin = P.X();
392 if (P.X() > UMax) UMax = P.X();
393 if (P.Y() < VMin) VMin = P.Y();
394 if (P.Y() > VMax) VMax = P.Y();
400 Hatch_Hatcher isobuild(1.e-5,ToolRst.IsOriented());
401 Standard_Boolean UClosed = aFace->IsUClosed();
402 Standard_Boolean VClosed = aFace->IsVClosed();
405 UMin = UMin + ( UMax - UMin) /1000.;
406 UMax = UMax - ( UMax - UMin) /1000.;
410 VMin = VMin + ( VMax - VMin) /1000.;
411 VMax = VMax - ( VMax - VMin) /1000.;
416 UClosed = Standard_False; // En attendant un hatcher de course.
417 Standard_Real du= UClosed ? (UMax-UMin)/NBUiso : (UMax-UMin)/(1+NBUiso);
418 for (i=1; i<=NBUiso;i++){
419 isobuild.AddXLine(UMin+du*i);
425 VClosed = Standard_False;
426 Standard_Real dv= VClosed ?(VMax-VMin)/NBViso : (VMax-VMin)/(1+NBViso);
427 for (i=1; i<=NBViso;i++){
428 isobuild.AddYLine(VMin+dv*i);
436 for (ToolRst.Init(); ToolRst.More(); ToolRst.Next()) {
437 TopAbs_Orientation Orient = ToolRst.Orientation();
438 if ( Orient == TopAbs_FORWARD || Orient == TopAbs_REVERSED ) {
439 Adaptor2d_Curve2dPtr TheRCurve = ToolRst.Value();
440 GCPnts_QuasiUniformDeflection UDP(*TheRCurve, Deflection);
442 Standard_Integer NumberOfPoints = UDP.NbPoints();
443 if ( NumberOfPoints >= 2 ) {
444 dummypnt = UDP.Value(1);
445 P2.SetCoord(dummypnt.X(), dummypnt.Y());
446 for (i = 2; i <= NumberOfPoints; i++) {
448 dummypnt = UDP.Value(i);
449 P2.SetCoord(dummypnt.X(), dummypnt.Y());
450 if(Orient == TopAbs_FORWARD )
451 isobuild.Trim(P1,P2);
453 isobuild.Trim(P2,P1);
458 cout << "Cannot evaluate curve on surface"<<endl;
465 Adaptor3d_IsoCurve anIso;
467 Standard_Integer NumberOfLines = isobuild.NbLines();
468 Standard_Real anAngle = aDrawer->DeviationAngle();
470 for (i = 1; i <= NumberOfLines; i++) {
471 Standard_Integer NumberOfIntervals = isobuild.NbIntervals(i);
472 Standard_Real Coord = isobuild.Coordinate(i);
473 for (Standard_Integer j = 1; j <= NumberOfIntervals; j++) {
474 Standard_Real b1=isobuild.Start(i,j),b2=isobuild.End(i,j);
476 b1 = b1 == RealFirst() ? - aLimit : b1;
477 b2 = b2 == RealLast() ? aLimit : b2;
479 if (isobuild.IsXLine(i))
480 anIso.Load(GeomAbs_IsoU,Coord,b1,b2);
482 anIso.Load(GeomAbs_IsoV,Coord,b1,b2);
484 if (StdPrs_DeflectionCurve::Match(X,Y,Z,aDistance,anIso, b1, b2, Deflection, anAngle))
485 return Standard_True;
488 return Standard_False;
492 //=========================================================================
495 //=========================================================================
496 void StdPrs_WFDeflectionRestrictedFace::Add
497 (const Handle (Prs3d_Presentation)& aPresentation,
498 const Handle(BRepAdaptor_HSurface)& aFace,
499 const Handle (Prs3d_Drawer)& aDrawer)
501 Prs3d_NListOfSequenceOfPnt Curves;
502 StdPrs_WFDeflectionRestrictedFace::Add (aPresentation,
506 aDrawer->MaximalChordialDeviation(),
507 aDrawer->UIsoAspect()->Number(),
508 aDrawer->VIsoAspect()->Number(),
514 //=========================================================================
517 //=========================================================================
518 void StdPrs_WFDeflectionRestrictedFace::AddUIso
519 (const Handle (Prs3d_Presentation)& aPresentation,
520 const Handle(BRepAdaptor_HSurface)& aFace,
521 const Handle (Prs3d_Drawer)& aDrawer)
523 Prs3d_NListOfSequenceOfPnt Curves;
524 StdPrs_WFDeflectionRestrictedFace::Add (
529 aDrawer->MaximalChordialDeviation(),
530 aDrawer->UIsoAspect()->Number(),
531 aDrawer->VIsoAspect()->Number(),
537 //=========================================================================
540 //=========================================================================
541 void StdPrs_WFDeflectionRestrictedFace::AddVIso
542 (const Handle (Prs3d_Presentation)& aPresentation,
543 const Handle(BRepAdaptor_HSurface)& aFace,
544 const Handle (Prs3d_Drawer)& aDrawer)
546 Prs3d_NListOfSequenceOfPnt Curves;
547 StdPrs_WFDeflectionRestrictedFace::Add (
552 aDrawer->MaximalChordialDeviation(),
553 aDrawer->UIsoAspect()->Number(),
554 aDrawer->VIsoAspect()->Number(),
560 //=========================================================================
563 //=========================================================================
564 Standard_Boolean StdPrs_WFDeflectionRestrictedFace::Match
565 (const Quantity_Length X,
566 const Quantity_Length Y,
567 const Quantity_Length Z,
568 const Quantity_Length aDistance,
569 const Handle(BRepAdaptor_HSurface)& aFace,
570 const Handle (Prs3d_Drawer)& aDrawer)
572 return StdPrs_WFDeflectionRestrictedFace::Match (
578 aDrawer->MaximalChordialDeviation(),
579 aDrawer->UIsoAspect()->Number(),
580 aDrawer->VIsoAspect()->Number());
584 //=========================================================================
585 // function: MatchUIso
587 //=========================================================================
588 Standard_Boolean StdPrs_WFDeflectionRestrictedFace::MatchUIso
589 (const Quantity_Length X,
590 const Quantity_Length Y,
591 const Quantity_Length Z,
592 const Quantity_Length aDistance,
593 const Handle(BRepAdaptor_HSurface)& aFace,
594 const Handle (Prs3d_Drawer)& aDrawer)
596 return StdPrs_WFDeflectionRestrictedFace::Match (
602 aDrawer->MaximalChordialDeviation(),
603 aDrawer->UIsoAspect()->Number(),
604 aDrawer->VIsoAspect()->Number());
608 //=========================================================================
609 // function: MatchVIso
611 //=========================================================================
612 Standard_Boolean StdPrs_WFDeflectionRestrictedFace::MatchVIso
613 (const Quantity_Length X,
614 const Quantity_Length Y,
615 const Quantity_Length Z,
616 const Quantity_Length aDistance,
617 const Handle(BRepAdaptor_HSurface)& aFace,
618 const Handle (Prs3d_Drawer)& aDrawer)
620 return StdPrs_WFDeflectionRestrictedFace::Match (
626 aDrawer->MaximalChordialDeviation(),
627 aDrawer->UIsoAspect()->Number(),
628 aDrawer->VIsoAspect()->Number());
633 static const Standard_Integer THE_INDICES[][3] =
634 { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 0, 1 }, { 0, 0, 1 },
635 { 0, 1, 1 }, { 1, 1, 1 }, { 1, 1, 0 }, { 0, 1, 0 },
636 { 0, 0, 0 }, { 0, 0, 1 }, { 1, 0, 1 }, { 1, 1, 1 },
637 { 0, 1, 1 }, { 0, 1, 0 }, { 1, 1, 0 }, { 1, 0, 0 } };
640 //=======================================================================
643 //=======================================================================
644 void StdPrs_WFDeflectionRestrictedFace::AddBox (const Handle(Prs3d_Presentation)& thePrs,
645 const Bnd_Box& theBndBox,
646 const Handle(Prs3d_Drawer)& theDrawer)
648 if (theBndBox.IsVoid())
653 Standard_Real X[2], Y[2], Z[2];
654 theBndBox.Get (X[0], Y[0], Z[0], X[1], Y[1], Z[1]);
656 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePrs);
657 Quantity_Color aColor;
658 Aspect_TypeOfLine aDummyLineType;
659 Standard_Real aWidth = 1.0;
660 theDrawer->LineAspect()->Aspect()->Values (aColor, aDummyLineType, aWidth);
662 aGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d (aColor, Aspect_TOL_DOTDASH, aWidth));
664 Handle(Graphic3d_ArrayOfPolylines) aPolyline = new Graphic3d_ArrayOfPolylines(16);
665 for(Standard_Integer aVertIter = 0; aVertIter < 16; ++aVertIter)
667 aPolyline->AddVertex (X[THE_INDICES[aVertIter][0]],
668 Y[THE_INDICES[aVertIter][1]],
669 Z[THE_INDICES[aVertIter][2]]);
671 aGroup->AddPrimitiveArray (aPolyline);