Salome HOME
Merge remote-tracking branch 'origin/BR_LAND_COVER' into BR_v14_rc
[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 <gp_Pnt.hxx>
30 #include <gp_Dir.hxx>
31 #include <Prs3d_Arrow.hxx>
32 #include <TopExp.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopExp.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <BRepBndLib.hxx>
41 #include <Precision.hxx>
42
43 IMPLEMENT_STANDARD_HANDLE (HYDROGUI_Polyline, AIS_Shape)
44 IMPLEMENT_STANDARD_RTTIEXT(HYDROGUI_Polyline, AIS_Shape)
45
46
47 HYDROGUI_Polyline::HYDROGUI_Polyline(const TopoDS_Shape& shape)
48   : AIS_Shape(shape)
49
50 }
51
52 HYDROGUI_Polyline::~HYDROGUI_Polyline()
53 {
54 }
55
56 void HYDROGUI_Polyline::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
57                             const Handle(Prs3d_Presentation)& aPrs,
58                             const Standard_Integer aMode)
59 {  
60   AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
61    
62   TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
63   for ( ; Exp.More(); Exp.Next() ) {
64     TopoDS_Vertex aV1, aV2;
65     TopoDS_Edge anEdge = TopoDS::Edge(Exp.Current());
66     
67     if ( anEdge.IsNull() ) 
68       continue;
69     TopExp::Vertices(anEdge, aV1, aV2);
70     gp_Pnt aP = BRep_Tool::Pnt(aV1);
71     gp_Vec aDirVec;
72     double aFp, aLp;
73     Handle(Geom_Curve) C = BRep_Tool::Curve(anEdge,aFp,aLp);
74     if ( C.IsNull() ) 
75       continue;
76   
77     Bnd_Box aBB;
78     BRepBndLib::Add(anEdge, aBB);
79     Standard_Real aXmin,aYmin,aZmin,aXmax,aYmax,aZmax;
80     aBB.Get(aXmin,aYmin,aZmin,aXmax,aYmax,aZmax);
81     Standard_Real aLen = Max(Abs(aXmax - aXmin), Max (Abs(aZmax - aZmin), Abs(aYmax - aYmin)));
82   
83     GeomAdaptor_Curve aAdC;
84     aAdC.Load(C, aFp, aLp);
85     double aLenC;
86     double aLenH;
87     int aNbSegments = 20;
88     double aArrLen = aLen / 10.;
89     double anIncr = (aLp-aFp)/aNbSegments;
90     double aMaxRatio = 0;
91     double aMaxRatioStep = 1;
92     for (double t = aFp; t < aLp - anIncr; t += anIncr)
93     {
94        aLenC  = GCPnts_AbscissaPoint::Length(aAdC, t, t + anIncr);
95        aLenH = C->Value (t).Distance (C->Value (t + anIncr));
96        if ( aLenH / aLenC > aMaxRatio) {
97          aMaxRatio = aLenH / aLenC;
98          aMaxRatioStep = t;
99        }
100     }
101
102     bool UseD1 = false;
103     if (Abs(aMaxRatioStep) < Precision::Confusion())
104     {
105       aMaxRatioStep = (aLp - aFp)/2.0;
106       UseD1 = true;
107     }
108
109     if (Abs(aLp - aMaxRatioStep) < Precision::Confusion())
110     {
111       aMaxRatioStep =  (aLp - aFp)/2.0;
112       UseD1 = true;
113     }      
114     
115     gp_Pnt aPnt1 = C->Value (aMaxRatioStep);
116     gp_Vec aDir;
117     if (!UseD1) {
118       GCPnts_AbscissaPoint aAbsPoint(aAdC, -aArrLen, aMaxRatioStep);
119       double aParam = aAbsPoint.Parameter();      
120       gp_Pnt aPnt2 = C->Value (aParam);
121       gp_XYZ D = aPnt1.XYZ();
122       D.Subtract (aPnt2.XYZ());
123       aDir = D;
124     }
125     else
126       C->D1(aMaxRatioStep, aPnt1, aDir);
127   
128     if ( anEdge.Orientation() == TopAbs_REVERSED )
129       aDir = -aDir;
130   
131     Prs3d_Arrow::Draw(aPrs, aPnt1, aDir, M_PI/180.*12., aArrLen);     
132   }
133 }