Salome HOME
Merge branch 'master' of https://git.salome-platform.org/git/modules/hydro
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Polyline.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROGUI_Polyline.h>
20
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>
26 #include <gp_Pnt.hxx>
27 #include <gp_Dir.hxx>
28 #include <Prs3d_Arrow.hxx>
29 #include <TopExp.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopExp.hxx>
32 #include <TopExp_Explorer.hxx>
33 #include <TopoDS.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>
43
44 IMPLEMENT_STANDARD_HANDLE (HYDROGUI_Polyline, AIS_Shape)
45 IMPLEMENT_STANDARD_RTTIEXT(HYDROGUI_Polyline, AIS_Shape)
46
47
48 HYDROGUI_Polyline::HYDROGUI_Polyline(const TopoDS_Shape& shape)
49   : AIS_Shape(shape)
50
51 }
52
53 HYDROGUI_Polyline::~HYDROGUI_Polyline()
54 {
55 }
56
57 Handle( Graphic3d_ArrayOfPolylines ) BuildEdgePresentation( const TopoDS_Edge& theEdge, double theDeviation )
58 {
59   BRepAdaptor_Curve aCurveAdaptor( theEdge );
60   GCPnts_QuasiUniformDeflection aPnts( aCurveAdaptor, theDeviation );
61
62   Handle( Graphic3d_ArrayOfPolylines ) anArray;
63   if( !aPnts.IsDone() )
64     return anArray;
65
66   int n = aPnts.NbPoints();
67   anArray = new Graphic3d_ArrayOfPolylines( n );
68   for( int i=1; i<=n; i++ )
69     anArray->AddVertex( aPnts.Value( i ) );
70
71   return anArray;
72 }
73
74 void HYDROGUI_Polyline::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
75                             const Handle(Prs3d_Presentation)& aPrs,
76                             const Standard_Integer aMode)
77 {  
78   //AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
79
80   aPrs->Clear();
81   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aPrs );
82   Quantity_Color aColor;
83   Aspect_TypeOfLine aType;
84   Standard_Real anWidth;
85   Attributes()->LineAspect()->Aspect()->Values( aColor, aType, anWidth );
86   Handle(Graphic3d_AspectLine3d) anAspect = new Graphic3d_AspectLine3d( aColor, aType, anWidth );
87
88   TopExp_Explorer Exp1 ( myshape, TopAbs_EDGE );
89   for ( ; Exp1.More(); Exp1.Next() )
90   {
91     TopoDS_Edge anEdge = TopoDS::Edge( Exp1.Current() );
92     Handle( Graphic3d_ArrayOfPolylines ) anArray = BuildEdgePresentation( anEdge, 0.1 );
93     if( !anArray.IsNull() )
94     {
95       aGroup->SetPrimitivesAspect( anAspect );
96       aGroup->AddPrimitiveArray( anArray );
97     }
98   }
99
100    
101   TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
102   for ( ; Exp.More(); Exp.Next() ) {
103     TopoDS_Vertex aV1, aV2;
104     TopoDS_Edge anEdge = TopoDS::Edge(Exp.Current());
105     
106     if ( anEdge.IsNull() ) 
107       continue;
108     TopExp::Vertices(anEdge, aV1, aV2);
109     gp_Pnt aP = BRep_Tool::Pnt(aV1);
110     gp_Vec aDirVec;
111     double aFp, aLp;
112     Handle(Geom_Curve) C = BRep_Tool::Curve(anEdge,aFp,aLp);
113     if ( C.IsNull() ) 
114       continue;
115   
116     Bnd_Box aBB;
117     BRepBndLib::Add(anEdge, aBB);
118     Standard_Real aXmin,aYmin,aZmin,aXmax,aYmax,aZmax;
119     aBB.Get(aXmin,aYmin,aZmin,aXmax,aYmax,aZmax);
120     Standard_Real aLen = Max(Abs(aXmax - aXmin), Max (Abs(aZmax - aZmin), Abs(aYmax - aYmin)));
121   
122     GeomAdaptor_Curve aAdC;
123     aAdC.Load(C, aFp, aLp);
124     double aLenC;
125     double aLenH;
126     int aNbSegments = 20;
127     double aArrLen = aLen / 10.;
128     double anIncr = (aLp-aFp)/aNbSegments;
129     double aMaxRatio = 0;
130     double aMaxRatioStep = 1;
131     for (double t = aFp; t < aLp - anIncr; t += anIncr)
132     {
133        aLenC  = GCPnts_AbscissaPoint::Length(aAdC, t, t + anIncr);
134        aLenH = C->Value (t).Distance (C->Value (t + anIncr));
135        if ( aLenH / aLenC > aMaxRatio) {
136          aMaxRatio = aLenH / aLenC;
137          aMaxRatioStep = t;
138        }
139     }
140
141     bool UseD1 = false;
142     if (Abs(aMaxRatioStep) < Precision::Confusion())
143     {
144       aMaxRatioStep = (aLp - aFp)/2.0;
145       UseD1 = true;
146     }
147
148     if (Abs(aLp - aMaxRatioStep) < Precision::Confusion())
149     {
150       aMaxRatioStep =  (aLp - aFp)/2.0;
151       UseD1 = true;
152     }      
153     
154     gp_Pnt aPnt1 = C->Value (aMaxRatioStep);
155     gp_Vec aDir;
156     if (!UseD1) {
157       GCPnts_AbscissaPoint aAbsPoint(aAdC, -aArrLen, aMaxRatioStep);
158       double aParam = aAbsPoint.Parameter();      
159       gp_Pnt aPnt2 = C->Value (aParam);
160       gp_XYZ D = aPnt1.XYZ();
161       D.Subtract (aPnt2.XYZ());
162       aDir = D;
163     }
164     else
165       C->D1(aMaxRatioStep, aPnt1, aDir);
166   
167     if ( anEdge.Orientation() == TopAbs_REVERSED )
168       aDir = -aDir;
169   
170     Prs3d_Arrow::Draw(aPrs, aPnt1, aDir, M_PI/180.*12., aArrLen);     
171   }
172 }