1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROGUI_Polyline.h>
21 #include <AIS_InteractiveContext.hxx>
22 #include <BRep_Tool.hxx>
23 #include <GCPnts_AbscissaPoint.hxx>
24 #include <GeomAdaptor_Curve.hxx>
25 #include <Graphic3d_AspectLine3d.hxx>
28 #include <Prs3d_Arrow.hxx>
30 #include <TopoDS_Shape.hxx>
32 #include <TopExp_Explorer.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <BRepBndLib.hxx>
38 #include <Precision.hxx>
39 #include <Graphic3d_ArrayOfPolylines.hxx>
40 #include <GCPnts_QuasiUniformDeflection.hxx>
41 #include <BRepAdaptor_Curve.hxx>
42 #include <Prs3d_LineAspect.hxx>
44 IMPLEMENT_STANDARD_RTTIEXT(HYDROGUI_Polyline, AIS_Shape)
47 HYDROGUI_Polyline::HYDROGUI_Polyline(const TopoDS_Shape& shape)
52 HYDROGUI_Polyline::~HYDROGUI_Polyline()
56 Handle( Graphic3d_ArrayOfPolylines ) BuildEdgePresentation( const TopoDS_Edge& theEdge, double theDeviation )
58 BRepAdaptor_Curve aCurveAdaptor( theEdge );
59 GCPnts_QuasiUniformDeflection aPnts( aCurveAdaptor, theDeviation );
61 Handle( Graphic3d_ArrayOfPolylines ) anArray;
65 int n = aPnts.NbPoints();
66 anArray = new Graphic3d_ArrayOfPolylines( n );
67 for( int i=1; i<=n; i++ )
68 anArray->AddVertex( aPnts.Value( i ) );
73 void HYDROGUI_Polyline::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
74 const Handle(Prs3d_Presentation)& aPrs,
75 const Standard_Integer aMode)
77 //AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
80 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aPrs );
81 Quantity_Color aColor;
82 Aspect_TypeOfLine aType;
83 Standard_Real anWidth;
84 Attributes()->LineAspect()->Aspect()->Values( aColor, aType, anWidth );
86 Handle(Graphic3d_AspectLine3d) anAspect = new Graphic3d_AspectLine3d( aColor, aType, anWidth );
88 TopExp_Explorer Exp1 ( myshape, TopAbs_EDGE );
90 BRepBndLib::AddClose(myshape, BB);
91 double xmin, xmax, ymin, ymax, zmin, zmax;
92 double devCoeff = 0.05;
95 BB.Get(xmin, ymin, zmin, xmax, ymax, zmax); //ignore Z coord
96 double minSide = Min(Abs(xmax - xmin), Abs(ymax - ymin));
97 devCoeff = minSide > 50 ? 0.05 : minSide / 3000;
99 for ( ; Exp1.More(); Exp1.Next() )
101 TopoDS_Edge anEdge = TopoDS::Edge( Exp1.Current() );
102 Handle( Graphic3d_ArrayOfPolylines ) anArray = BuildEdgePresentation( anEdge, devCoeff );
103 if( !anArray.IsNull() )
105 aGroup->SetPrimitivesAspect( anAspect );
106 aGroup->AddPrimitiveArray( anArray );
111 TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
112 for ( ; Exp.More(); Exp.Next() ) {
113 TopoDS_Vertex aV1, aV2;
114 TopoDS_Edge anEdge = TopoDS::Edge(Exp.Current());
116 if ( anEdge.IsNull() )
118 TopExp::Vertices(anEdge, aV1, aV2);
119 gp_Pnt aP = BRep_Tool::Pnt(aV1);
122 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdge,aFp,aLp);
127 BRepBndLib::Add(anEdge, aBB);
128 Standard_Real aXmin,aYmin,aZmin,aXmax,aYmax,aZmax;
129 aBB.Get(aXmin,aYmin,aZmin,aXmax,aYmax,aZmax);
130 Standard_Real aLen = Max(Abs(aXmax - aXmin), Max (Abs(aZmax - aZmin), Abs(aYmax - aYmin)));
132 GeomAdaptor_Curve aAdC;
133 aAdC.Load(C, aFp, aLp);
136 int aNbSegments = 20;
137 double aArrLen = aLen / 10.;
138 double anIncr = (aLp-aFp)/aNbSegments;
139 double aMaxRatio = 0;
140 double aMaxRatioStep = 1;
141 for (double t = aFp; t < aLp - anIncr; t += anIncr)
143 aLenC = GCPnts_AbscissaPoint::Length(aAdC, t, t + anIncr);
144 aLenH = C->Value (t).Distance (C->Value (t + anIncr));
145 if ( aLenH / aLenC > aMaxRatio) {
146 aMaxRatio = aLenH / aLenC;
152 if (Abs(aMaxRatioStep) < Precision::Confusion())
154 aMaxRatioStep = (aLp - aFp)/2.0;
158 if (Abs(aLp - aMaxRatioStep) < Precision::Confusion())
160 aMaxRatioStep = (aLp - aFp)/2.0;
164 gp_Pnt aPnt1 = C->Value (aMaxRatioStep);
168 GCPnts_AbscissaPoint aAbsPoint(aAdC, -aArrLen, aMaxRatioStep);
169 double aParam = aAbsPoint.Parameter();
170 gp_Pnt aPnt2 = C->Value (aParam);
171 gp_XYZ D = aPnt1.XYZ();
172 D.Subtract (aPnt2.XYZ());
176 if (UseD1 || aDir.IsEqual (gp_Vec(0,0,0), Precision::Confusion(), Precision::Angular()))
177 C->D1(aMaxRatioStep, aPnt1, aDir);
179 if ( anEdge.Orientation() == TopAbs_REVERSED )
182 Prs3d_Arrow::Draw(aPrs, aPnt1, aDir, M_PI/180.*12., aArrLen);