Salome HOME
improve test h003 h008 h016
[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 bool HYDROGUI_Polyline::GetColorOfSubShape(const TopoDS_Shape& SubShape, Quantity_Color& outColor)
66 {
67   for (int i=1; i<=myShapeToColor.Extent();i++)
68   {
69     const TopoDS_Shape& aCurShape = myShapeToColor.FindKey(i);
70     TopTools_IndexedMapOfShape aSubShMap;
71     TopExp::MapShapes(aCurShape, aSubShMap);
72     if (aSubShMap.Contains(SubShape))
73     {
74       outColor = myShapeToColor(i);
75       return true;
76     }
77   }
78   return false;
79 }
80
81 void HYDROGUI_Polyline::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
82                             const Handle(Prs3d_Presentation)& aPrs,
83                             const Standard_Integer aMode)
84 {
85   //AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
86   // return;
87
88   aPrs->Clear();
89   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aPrs );
90   Quantity_Color aColor = Attributes()->LineAspect()->Aspect()->Color();
91   Aspect_TypeOfLine aType = Attributes()->LineAspect()->Aspect()->Type();
92   Standard_Real anWidth = Attributes()->LineAspect()->Aspect()->Width();
93   anWidth =2;
94   Handle(Graphic3d_AspectLine3d) anAspect = new Graphic3d_AspectLine3d( aColor, aType, anWidth );
95
96   Bnd_Box BB;
97   BRepBndLib::AddClose(myshape, BB);
98   double xmin, xmax, ymin, ymax, zmin, zmax;
99   double devCoeff = 0.05;
100   if (!BB.IsVoid())
101   {
102     BB.Get(xmin, ymin, zmin, xmax, ymax, zmax); //ignore Z coord
103     double minSide = Min(Abs(xmax - xmin), Abs(ymax - ymin));
104     devCoeff = minSide > 50 ? 0.05 : minSide / 3000;
105   }
106
107   TopExp_Explorer Exp ( myshape, TopAbs_WIRE );
108   for ( ; Exp.More(); Exp.Next() )
109   {
110     TopoDS_Shape W = Exp.Current();
111     TopExp_Explorer Exp1 (W, TopAbs_EDGE );
112     Quantity_Color aWCol = aColor; //from drawer
113     if (myShapeToColor.Contains(W))
114     {
115       Quantity_Color aWCol = myShapeToColor.FindFromKey(W);
116       anAspect = new Graphic3d_AspectLine3d( aWCol, aType, anWidth );
117     }
118     for ( ; Exp1.More(); Exp1.Next() )
119     {
120       TopoDS_Edge anEdge = TopoDS::Edge( Exp1.Current() );
121       Handle( Graphic3d_ArrayOfPolylines ) anArray = BuildEdgePresentation( anEdge, devCoeff);
122       if( !anArray.IsNull() )
123       {
124         aGroup->SetPrimitivesAspect( anAspect );
125         aGroup->AddPrimitiveArray( anArray );
126       }
127     }
128   }
129 }
130
131 QList<Handle(AIS_InteractiveObject)> HYDROGUI_Polyline::createPresentations
132   ( const TopoDS_Shape& theShape, int theType, int theSize )
133 {
134   QList<Handle(AIS_InteractiveObject)> shapes;
135
136   // 1. Main shape
137   Handle(HYDROGUI_Polyline) aMPoly = new HYDROGUI_Polyline( theShape );
138   shapes.append( aMPoly );
139
140   // 2. Shapes for direction arrows on edges
141   TopExp_Explorer Exp ( theShape, TopAbs_EDGE );
142   for ( ; Exp.More(); Exp.Next() )
143   {
144     TopoDS_Edge anEdge = TopoDS::Edge(Exp.Current());
145     if ( !anEdge.IsNull() )
146     {
147       Handle(HYDROGUI_Arrow) arrow = new HYDROGUI_Arrow( anEdge, aMPoly );
148       if( theType>=0 )
149         arrow->SetType( (HYDROGUI_Arrow::Type)theType );
150       if( theSize>=0 )
151         arrow->SetSize( theSize );
152       shapes.append( arrow );
153     }
154   }
155
156   return shapes;
157 }
158
159 void HYDROGUI_Polyline::update( const QList<Handle(AIS_InteractiveObject)>& theObjects, int theType, int theSize )
160 {
161   foreach( Handle(AIS_InteractiveObject) obj, theObjects )
162   {
163     Handle(HYDROGUI_Arrow) arrow = Handle(HYDROGUI_Arrow)::DownCast( obj );
164     if( !arrow.IsNull() )
165     {
166       if( theType>=0 )
167         arrow->SetType( (HYDROGUI_Arrow::Type)theType );
168       if( theSize>=0 )
169         arrow->SetSize( theSize );
170     }
171   }
172 }
173
174
175
176
177 IMPLEMENT_STANDARD_RTTIEXT(HYDROGUI_Arrow, AIS_Shape)
178
179
180   HYDROGUI_Arrow::HYDROGUI_Arrow( const TopoDS_Edge& edge, const Handle(HYDROGUI_Polyline)& polyline )
181   : AIS_Shape( edge ), myType( Cone ), mySize( 35 ), myParentPoly (polyline)
182 {
183 }
184
185 HYDROGUI_Arrow::~HYDROGUI_Arrow()
186 {
187 }
188
189 HYDROGUI_Arrow::Type HYDROGUI_Arrow::GetType() const
190 {
191   return myType;
192 }
193
194 void HYDROGUI_Arrow::SetType( HYDROGUI_Arrow::Type theType )
195 {
196   myType = theType;
197 }
198
199 int HYDROGUI_Arrow::GetSize() const
200 {
201   return mySize;
202 }
203
204 void HYDROGUI_Arrow::SetSize( int theSize )
205 {
206   mySize = qMax( theSize, 0 );
207 }
208
209 const Handle(HYDROGUI_Polyline)& HYDROGUI_Arrow::getParentPolyline() const
210 {
211   return myParentPoly;
212 }
213
214 void HYDROGUI_Arrow::BoundingBox (Bnd_Box& theBndBox)
215 {
216   //Nothing to change, we consider arrow as object with empty bounding box
217 }
218
219 void HYDROGUI_Arrow::Compute( const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
220                                                       const Handle(Prs3d_Presentation)& aPrs,
221                                                       const Standard_Integer aMode )
222 {
223   aPrs->Clear();
224   if( myType==None )
225     return;
226
227   TopoDS_Edge anEdge = TopoDS::Edge( myshape );
228   if( anEdge.IsNull() )
229     return;
230
231   BRepAdaptor_Curve anAdaptor( anEdge );
232   double curveLen = GCPnts_AbscissaPoint::Length( anAdaptor, anAdaptor.FirstParameter(), anAdaptor.LastParameter() );
233   double arrowLen;
234   if( mySize==0 )
235     // if size==0, then the arrow length is proportional to curve length
236     arrowLen = curveLen/10;
237   else
238   {
239     //arrowLen = qMin( curveLen/10, (double)mySize );
240     arrowLen = (double)mySize;
241   }
242
243   double t = ( anAdaptor.FirstParameter() + anAdaptor.LastParameter() ) / 2;
244   gp_Pnt P, P1;
245   gp_Vec V;
246   anAdaptor.D1( t, P, V );
247
248   bool isFixed = mySize>0;
249
250   if( isFixed )
251   {
252     gp_Trsf tr;
253     tr.SetTranslation( -gp_Vec( gp_Pnt(), P ) );
254     aPrs->SetTransformation( new Geom_Transformation( tr ) );
255
256     Handle(Graphic3d_TransformPers) tp = new Graphic3d_TransformPers( Graphic3d_TMF_ZoomPers, P );
257     SetTransformPersistence( tp );
258
259     P1 = gp_Pnt();
260   }
261   else
262   {
263     aPrs->SetTransformation( Handle(Geom_Transformation)() );
264     SetTransformPersistence( Handle(Graphic3d_TransformPers)() );
265     P1 = P;
266   }
267
268   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aPrs );
269   Quantity_Color aColor = Attributes()->LineAspect()->Aspect()->Color();
270   Aspect_TypeOfLine aType = Attributes()->LineAspect()->Aspect()->Type();
271   Standard_Real anWidth = Attributes()->LineAspect()->Aspect()->Width();
272   anWidth = 1;
273
274   const Handle(HYDROGUI_Polyline)& aParentPoly = getParentPolyline();
275   aParentPoly->GetColorOfSubShape(anEdge, aColor);
276
277   if( myType==Cone )
278   {
279     Handle(Graphic3d_AspectLine3d) anAspect = new Graphic3d_AspectLine3d( aColor, aType, anWidth );
280     aGroup->SetPrimitivesAspect( anAspect );
281
282     Prs3d_Arrow::Draw( aGroup, P1, V, M_PI/180.*12., arrowLen );
283   }
284   else
285   {
286     Graphic3d_MaterialAspect m( Graphic3d_NOM_PLASTIC );
287     Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d
288       ( Aspect_IS_SOLID, aColor, aColor, Aspect_TOL_SOLID, 1, m, m );
289     aGroup->SetPrimitivesAspect( anAspect );
290
291     gp_Vec d = -V.Normalized() * arrowLen;
292     const double k = 5.0;
293     gp_Vec n( -d.Y()/k, d.X()/k, 0 );
294
295     gp_Pnt Pa = P1.Translated( d );
296     gp_Pnt P2 = Pa.Translated( n );
297     gp_Pnt P3 = Pa.Translated( -n );
298
299     Handle(Graphic3d_ArrayOfTriangles) prim = new Graphic3d_ArrayOfTriangles(3);
300     prim->AddVertex( P1 );
301     prim->AddVertex( P2 );
302     prim->AddVertex( P3 );
303
304     aGroup->AddPrimitiveArray( prim );
305   }
306 }