Salome HOME
refs #653, #665 - 669: start implementation of features.
[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 #include <Graphic3d_ArrayOfPolylines.hxx>
43 #include <GCPnts_QuasiUniformDeflection.hxx>
44 #include <BRepAdaptor_Curve.hxx>
45 #include <Prs3d_LineAspect.hxx>
46
47 IMPLEMENT_STANDARD_HANDLE (HYDROGUI_Polyline, AIS_Shape)
48 IMPLEMENT_STANDARD_RTTIEXT(HYDROGUI_Polyline, AIS_Shape)
49
50
51 HYDROGUI_Polyline::HYDROGUI_Polyline(const TopoDS_Shape& shape)
52   : AIS_Shape(shape)
53
54 }
55
56 HYDROGUI_Polyline::~HYDROGUI_Polyline()
57 {
58 }
59
60 Handle( Graphic3d_ArrayOfPolylines ) BuildEdgePresentation( const TopoDS_Edge& theEdge, double theDeviation )
61 {
62   BRepAdaptor_Curve aCurveAdaptor( theEdge );
63   GCPnts_QuasiUniformDeflection aPnts( aCurveAdaptor, theDeviation );
64
65   Handle( Graphic3d_ArrayOfPolylines ) anArray;
66   if( !aPnts.IsDone() )
67     return anArray;
68
69   int n = aPnts.NbPoints();
70   anArray = new Graphic3d_ArrayOfPolylines( n );
71   for( int i=1; i<=n; i++ )
72     anArray->AddVertex( aPnts.Value( i ) );
73
74   return anArray;
75 }
76
77 void HYDROGUI_Polyline::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
78                             const Handle(Prs3d_Presentation)& aPrs,
79                             const Standard_Integer aMode)
80 {  
81   //AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
82
83   aPrs->Clear();
84   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aPrs );
85   Handle(Graphic3d_AspectLine3d) anAspect = Attributes()->FaceBoundaryAspect()->Aspect();
86   aGroup->SetGroupPrimitivesAspect( anAspect );
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       aGroup->AddPrimitiveArray ( anArray );
95   }
96
97    
98   TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
99   for ( ; Exp.More(); Exp.Next() ) {
100     TopoDS_Vertex aV1, aV2;
101     TopoDS_Edge anEdge = TopoDS::Edge(Exp.Current());
102     
103     if ( anEdge.IsNull() ) 
104       continue;
105     TopExp::Vertices(anEdge, aV1, aV2);
106     gp_Pnt aP = BRep_Tool::Pnt(aV1);
107     gp_Vec aDirVec;
108     double aFp, aLp;
109     Handle(Geom_Curve) C = BRep_Tool::Curve(anEdge,aFp,aLp);
110     if ( C.IsNull() ) 
111       continue;
112   
113     Bnd_Box aBB;
114     BRepBndLib::Add(anEdge, aBB);
115     Standard_Real aXmin,aYmin,aZmin,aXmax,aYmax,aZmax;
116     aBB.Get(aXmin,aYmin,aZmin,aXmax,aYmax,aZmax);
117     Standard_Real aLen = Max(Abs(aXmax - aXmin), Max (Abs(aZmax - aZmin), Abs(aYmax - aYmin)));
118   
119     GeomAdaptor_Curve aAdC;
120     aAdC.Load(C, aFp, aLp);
121     double aLenC;
122     double aLenH;
123     int aNbSegments = 20;
124     double aArrLen = aLen / 10.;
125     double anIncr = (aLp-aFp)/aNbSegments;
126     double aMaxRatio = 0;
127     double aMaxRatioStep = 1;
128     for (double t = aFp; t < aLp - anIncr; t += anIncr)
129     {
130        aLenC  = GCPnts_AbscissaPoint::Length(aAdC, t, t + anIncr);
131        aLenH = C->Value (t).Distance (C->Value (t + anIncr));
132        if ( aLenH / aLenC > aMaxRatio) {
133          aMaxRatio = aLenH / aLenC;
134          aMaxRatioStep = t;
135        }
136     }
137
138     bool UseD1 = false;
139     if (Abs(aMaxRatioStep) < Precision::Confusion())
140     {
141       aMaxRatioStep = (aLp - aFp)/2.0;
142       UseD1 = true;
143     }
144
145     if (Abs(aLp - aMaxRatioStep) < Precision::Confusion())
146     {
147       aMaxRatioStep =  (aLp - aFp)/2.0;
148       UseD1 = true;
149     }      
150     
151     gp_Pnt aPnt1 = C->Value (aMaxRatioStep);
152     gp_Vec aDir;
153     if (!UseD1) {
154       GCPnts_AbscissaPoint aAbsPoint(aAdC, -aArrLen, aMaxRatioStep);
155       double aParam = aAbsPoint.Parameter();      
156       gp_Pnt aPnt2 = C->Value (aParam);
157       gp_XYZ D = aPnt1.XYZ();
158       D.Subtract (aPnt2.XYZ());
159       aDir = D;
160     }
161     else
162       C->D1(aMaxRatioStep, aPnt1, aDir);
163   
164     if ( anEdge.Orientation() == TopAbs_REVERSED )
165       aDir = -aDir;
166   
167     Prs3d_Arrow::Draw(aPrs, aPnt1, aDir, M_PI/180.*12., aArrLen);     
168   }
169 }