Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Polyline.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <HYDROGUI_Polyline.h>
24
25 #include <AIS_InteractiveContext.hxx>
26 #include <BRep_Tool.hxx>
27 #include <GCPnts_AbscissaPoint.hxx>
28 #include <GeomAdaptor_Curve.hxx>
29 #include <Graphic3d_AspectLine3d.hxx>
30 #include <gp_Pnt.hxx>
31 #include <gp_Dir.hxx>
32 #include <Prs3d_Arrow.hxx>
33 #include <TopExp.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopExp.hxx>
36 #include <TopExp_Explorer.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <BRepBndLib.hxx>
42 #include <Precision.hxx>
43 #include <Graphic3d_ArrayOfPolylines.hxx>
44 #include <GCPnts_QuasiUniformDeflection.hxx>
45 #include <BRepAdaptor_Curve.hxx>
46 #include <Prs3d_LineAspect.hxx>
47
48 IMPLEMENT_STANDARD_HANDLE (HYDROGUI_Polyline, AIS_Shape)
49 IMPLEMENT_STANDARD_RTTIEXT(HYDROGUI_Polyline, AIS_Shape)
50
51
52 HYDROGUI_Polyline::HYDROGUI_Polyline(const TopoDS_Shape& shape)
53   : AIS_Shape(shape)
54
55 }
56
57 HYDROGUI_Polyline::~HYDROGUI_Polyline()
58 {
59 }
60
61 Handle( Graphic3d_ArrayOfPolylines ) BuildEdgePresentation( const TopoDS_Edge& theEdge, double theDeviation )
62 {
63   BRepAdaptor_Curve aCurveAdaptor( theEdge );
64   GCPnts_QuasiUniformDeflection aPnts( aCurveAdaptor, theDeviation );
65
66   Handle( Graphic3d_ArrayOfPolylines ) anArray;
67   if( !aPnts.IsDone() )
68     return anArray;
69
70   int n = aPnts.NbPoints();
71   anArray = new Graphic3d_ArrayOfPolylines( n );
72   for( int i=1; i<=n; i++ )
73     anArray->AddVertex( aPnts.Value( i ) );
74
75   return anArray;
76 }
77
78 void HYDROGUI_Polyline::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
79                             const Handle(Prs3d_Presentation)& aPrs,
80                             const Standard_Integer aMode)
81 {  
82   //AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
83
84   aPrs->Clear();
85   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aPrs );
86   Quantity_Color aColor;
87   Aspect_TypeOfLine aType;
88   Standard_Real anWidth;
89   Attributes()->LineAspect()->Aspect()->Values( aColor, aType, anWidth );
90   Handle(Graphic3d_AspectLine3d) anAspect = new Graphic3d_AspectLine3d( aColor, aType, anWidth );
91
92   TopExp_Explorer Exp1 ( myshape, TopAbs_EDGE );
93   for ( ; Exp1.More(); Exp1.Next() )
94   {
95     TopoDS_Edge anEdge = TopoDS::Edge( Exp1.Current() );
96     Handle( Graphic3d_ArrayOfPolylines ) anArray = BuildEdgePresentation( anEdge, 0.1 );
97     if( !anArray.IsNull() )
98     {
99       aGroup->SetPrimitivesAspect( anAspect );
100       aGroup->AddPrimitiveArray( anArray );
101     }
102   }
103
104    
105   TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
106   for ( ; Exp.More(); Exp.Next() ) {
107     TopoDS_Vertex aV1, aV2;
108     TopoDS_Edge anEdge = TopoDS::Edge(Exp.Current());
109     
110     if ( anEdge.IsNull() ) 
111       continue;
112     TopExp::Vertices(anEdge, aV1, aV2);
113     gp_Pnt aP = BRep_Tool::Pnt(aV1);
114     gp_Vec aDirVec;
115     double aFp, aLp;
116     Handle(Geom_Curve) C = BRep_Tool::Curve(anEdge,aFp,aLp);
117     if ( C.IsNull() ) 
118       continue;
119   
120     Bnd_Box aBB;
121     BRepBndLib::Add(anEdge, aBB);
122     Standard_Real aXmin,aYmin,aZmin,aXmax,aYmax,aZmax;
123     aBB.Get(aXmin,aYmin,aZmin,aXmax,aYmax,aZmax);
124     Standard_Real aLen = Max(Abs(aXmax - aXmin), Max (Abs(aZmax - aZmin), Abs(aYmax - aYmin)));
125   
126     GeomAdaptor_Curve aAdC;
127     aAdC.Load(C, aFp, aLp);
128     double aLenC;
129     double aLenH;
130     int aNbSegments = 20;
131     double aArrLen = aLen / 10.;
132     double anIncr = (aLp-aFp)/aNbSegments;
133     double aMaxRatio = 0;
134     double aMaxRatioStep = 1;
135     for (double t = aFp; t < aLp - anIncr; t += anIncr)
136     {
137        aLenC  = GCPnts_AbscissaPoint::Length(aAdC, t, t + anIncr);
138        aLenH = C->Value (t).Distance (C->Value (t + anIncr));
139        if ( aLenH / aLenC > aMaxRatio) {
140          aMaxRatio = aLenH / aLenC;
141          aMaxRatioStep = t;
142        }
143     }
144
145     bool UseD1 = false;
146     if (Abs(aMaxRatioStep) < Precision::Confusion())
147     {
148       aMaxRatioStep = (aLp - aFp)/2.0;
149       UseD1 = true;
150     }
151
152     if (Abs(aLp - aMaxRatioStep) < Precision::Confusion())
153     {
154       aMaxRatioStep =  (aLp - aFp)/2.0;
155       UseD1 = true;
156     }      
157     
158     gp_Pnt aPnt1 = C->Value (aMaxRatioStep);
159     gp_Vec aDir;
160     if (!UseD1) {
161       GCPnts_AbscissaPoint aAbsPoint(aAdC, -aArrLen, aMaxRatioStep);
162       double aParam = aAbsPoint.Parameter();      
163       gp_Pnt aPnt2 = C->Value (aParam);
164       gp_XYZ D = aPnt1.XYZ();
165       D.Subtract (aPnt2.XYZ());
166       aDir = D;
167     }
168     else
169       C->D1(aMaxRatioStep, aPnt1, aDir);
170   
171     if ( anEdge.Orientation() == TopAbs_REVERSED )
172       aDir = -aDir;
173   
174     Prs3d_Arrow::Draw(aPrs, aPnt1, aDir, M_PI/180.*12., aArrLen);     
175   }
176 }