Salome HOME
Merge branch 'BR_H2018_2' of https://codev-tuleap.cea.fr/plugins/git/salome/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 #include <BRepAdaptor_Curve.hxx>
21 #include <BRepBndLib.hxx>
22 #include <BRep_Tool.hxx>
23 #include <GCPnts_AbscissaPoint.hxx>
24 #include <GCPnts_QuasiUniformDeflection.hxx>
25 #include <Graphic3d_ArrayOfPolylines.hxx>
26 #include <Prs3d_Arrow.hxx>
27 #include <Prs3d_LineAspect.hxx>
28 #include <Prs3d_Point.hxx>
29 #include <Prs3d_Root.hxx>
30 #include <TopExp.hxx>
31 #include <TopExp_Explorer.hxx>
32 #include <TopoDS.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Vertex.hxx>
35
36 IMPLEMENT_STANDARD_RTTIEXT(HYDROGUI_Polyline, AIS_Shape)
37
38
39 HYDROGUI_Polyline::HYDROGUI_Polyline(const TopoDS_Shape& shape)
40   : AIS_Shape(shape)
41
42 }
43
44 HYDROGUI_Polyline::~HYDROGUI_Polyline()
45 {
46 }
47
48 Handle( Graphic3d_ArrayOfPolylines ) BuildEdgePresentation( const TopoDS_Edge& theEdge, double theDeviation )
49 {
50   BRepAdaptor_Curve aCurveAdaptor( theEdge );
51   GCPnts_QuasiUniformDeflection aPnts( aCurveAdaptor, theDeviation );
52
53   Handle( Graphic3d_ArrayOfPolylines ) anArray;
54   if( !aPnts.IsDone() )
55     return anArray;
56
57   int n = aPnts.NbPoints();
58   anArray = new Graphic3d_ArrayOfPolylines( n );
59   for( int i=1; i<=n; i++ )
60     anArray->AddVertex( aPnts.Value( i ) );
61
62   return anArray;
63 }
64
65 void HYDROGUI_Polyline::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
66                             const Handle(Prs3d_Presentation)& aPrs,
67                             const Standard_Integer aMode)
68 {  
69   //AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
70   //return;
71
72   aPrs->Clear();
73   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aPrs );
74   Quantity_Color aColor;
75   Aspect_TypeOfLine aType;
76   Standard_Real anWidth;
77   Attributes()->LineAspect()->Aspect()->Values( aColor, aType, anWidth );
78   anWidth =2;
79   Handle(Graphic3d_AspectLine3d) anAspect = new Graphic3d_AspectLine3d( aColor, aType, anWidth );
80
81   TopExp_Explorer Exp1 ( myshape, TopAbs_EDGE );
82   Bnd_Box BB;
83   BRepBndLib::AddClose(myshape, BB);
84   double xmin, xmax, ymin, ymax, zmin, zmax;
85   double devCoeff = 0.05;
86   if (!BB.IsVoid())
87   {
88     BB.Get(xmin, ymin, zmin, xmax, ymax, zmax); //ignore Z coord
89     double minSide = Min(Abs(xmax - xmin), Abs(ymax - ymin));
90     devCoeff = minSide > 50 ? 0.05 : minSide / 3000;
91   }
92   for ( ; Exp1.More(); Exp1.Next() )
93   {
94     TopoDS_Edge anEdge = TopoDS::Edge( Exp1.Current() );
95     Handle( Graphic3d_ArrayOfPolylines ) anArray = BuildEdgePresentation( anEdge, devCoeff );
96     if( !anArray.IsNull() )
97     {
98       aGroup->SetPrimitivesAspect( anAspect );
99       aGroup->AddPrimitiveArray( anArray );
100     }
101   }
102 }
103
104 QList<Handle(AIS_InteractiveObject)> HYDROGUI_Polyline::createPresentations
105   ( const TopoDS_Shape& theShape, int theType, int theSize )
106 {
107   QList<Handle(AIS_InteractiveObject)> shapes;
108
109   // 1. Main shape
110   shapes.append( new HYDROGUI_Polyline( theShape ) );
111
112   // 2. Shapes for direction arrows on edges
113   TopExp_Explorer Exp ( theShape, TopAbs_EDGE );
114   for ( ; Exp.More(); Exp.Next() )
115   {
116     TopoDS_Edge anEdge = TopoDS::Edge(Exp.Current());
117     if ( !anEdge.IsNull() ) 
118     {
119       Handle(HYDROGUI_Arrow) arrow = new HYDROGUI_Arrow( anEdge );
120       if( theType>=0 )
121         arrow->SetType( (HYDROGUI_Arrow::Type)theType );
122       if( theSize>=0 )
123         arrow->SetSize( theSize );
124       shapes.append( arrow );
125     }
126   }
127
128   return shapes;
129 }
130
131 void HYDROGUI_Polyline::update( const QList<Handle(AIS_InteractiveObject)>& theObjects, int theType, int theSize )
132 {
133   foreach( Handle(AIS_InteractiveObject) obj, theObjects )
134   {
135     Handle(HYDROGUI_Arrow) arrow = Handle(HYDROGUI_Arrow)::DownCast( obj );
136     if( !arrow.IsNull() )
137     {
138       if( theType>=0 )
139         arrow->SetType( (HYDROGUI_Arrow::Type)theType );
140       if( theSize>=0 )
141         arrow->SetSize( theSize );
142     }
143   }
144 }
145
146
147
148
149 IMPLEMENT_STANDARD_RTTIEXT(HYDROGUI_Arrow, AIS_Shape)
150
151
152 HYDROGUI_Arrow::HYDROGUI_Arrow( const TopoDS_Edge& edge )
153   : AIS_Shape( edge ), myType( Cone ), mySize( 35 )
154 {
155 }
156
157 HYDROGUI_Arrow::~HYDROGUI_Arrow()
158 {
159 }
160
161 HYDROGUI_Arrow::Type HYDROGUI_Arrow::GetType() const
162 {
163   return myType;
164 }
165
166 void HYDROGUI_Arrow::SetType( HYDROGUI_Arrow::Type theType )
167 {
168   myType = theType;
169 }
170
171 int HYDROGUI_Arrow::GetSize() const
172 {
173   return mySize;
174 }
175
176 void HYDROGUI_Arrow::SetSize( int theSize )
177 {
178   mySize = qMax( theSize, 0 );
179 }
180
181 void HYDROGUI_Arrow::BoundingBox (Bnd_Box& theBndBox)
182 {
183   //Nothing to change, we consider arrow as object with empty bounding box
184 }
185
186 void HYDROGUI_Arrow::Compute( const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
187                                                       const Handle(Prs3d_Presentation)& aPrs,
188                                                       const Standard_Integer aMode )
189 {
190   aPrs->Clear();
191   if( myType==None )
192     return;
193   
194   TopoDS_Edge anEdge = TopoDS::Edge( myshape );
195   if( anEdge.IsNull() )
196     return;
197
198   BRepAdaptor_Curve anAdaptor( anEdge );
199   double curveLen = GCPnts_AbscissaPoint::Length( anAdaptor, anAdaptor.FirstParameter(), anAdaptor.LastParameter() );
200   double arrowLen;
201   if( mySize==0 )
202     // if size==0, then the arrow length is proportional to curve length
203     arrowLen = curveLen/10;
204   else
205   {
206     //arrowLen = qMin( curveLen/10, (double)mySize );
207     arrowLen = (double)mySize;
208   }
209
210   double t = ( anAdaptor.FirstParameter() + anAdaptor.LastParameter() ) / 2;
211   gp_Pnt P, P1;
212   gp_Vec V;
213   anAdaptor.D1( t, P, V );
214
215   bool isFixed = mySize>0;
216
217   if( isFixed )
218   {
219     gp_Trsf tr;
220     tr.SetTranslation( -gp_Vec( gp_Pnt(), P ) );
221     aPrs->SetTransformation( new Geom_Transformation( tr ) );
222
223     Handle(Graphic3d_TransformPers) tp = new Graphic3d_TransformPers( Graphic3d_TMF_ZoomPers, P );
224     SetTransformPersistence( tp );
225
226     P1 = gp_Pnt();
227   }
228   else
229   {
230     aPrs->SetTransformation( Handle(Geom_Transformation)() );
231     SetTransformPersistence( Handle(Graphic3d_TransformPers)() );
232     P1 = P;
233   }
234
235   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aPrs );
236   Quantity_Color aColor;
237   Aspect_TypeOfLine aType;
238   Standard_Real anWidth;
239   Attributes()->LineAspect()->Aspect()->Values( aColor, aType, anWidth );
240   anWidth = 1;
241
242   if( myType==Cone )
243   {
244     Handle(Graphic3d_AspectLine3d) anAspect = new Graphic3d_AspectLine3d( aColor, aType, anWidth );
245     aGroup->SetPrimitivesAspect( anAspect );
246
247     Prs3d_Arrow::Draw( aGroup, P1, V, M_PI/180.*12., arrowLen );
248   }
249   else
250   {
251     Graphic3d_MaterialAspect m( Graphic3d_NOM_PLASTIC );
252     Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d
253       ( Aspect_IS_SOLID, aColor, aColor, Aspect_TOL_SOLID, 1, m, m );
254     aGroup->SetPrimitivesAspect( anAspect );
255
256     gp_Vec d = -V.Normalized() * arrowLen;
257     const double k = 5.0;
258     gp_Vec n( -d.Y()/k, d.X()/k, 0 );
259
260     gp_Pnt Pa = P1.Translated( d );
261     gp_Pnt P2 = Pa.Translated( n );
262     gp_Pnt P3 = Pa.Translated( -n );
263
264     Handle(Graphic3d_ArrayOfTriangles) prim = new Graphic3d_ArrayOfTriangles(3);
265     prim->AddVertex( P1 );
266     prim->AddVertex( P2 );
267     prim->AddVertex( P3 );
268
269     aGroup->AddPrimitiveArray( prim );
270   }
271 }