Salome HOME
copyrights in HYDRO files are updated
[modules/hydro.git] / src / HYDROData / HYDROData_Polyline3D.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 "HYDROData_Polyline3D.h"
24
25 #include "HYDROData_IAltitudeObject.h"
26 #include "HYDROData_Document.h"
27 #include "HYDROData_PolylineXY.h"
28 #include "HYDROData_Profile.h"
29 #include "HYDROData_ProfileUZ.h"
30 #include "HYDROData_Tool.h"
31
32 #include <gp_Pnt2d.hxx>
33 #include <gp_XY.hxx>
34 #include <gp_XYZ.hxx>
35
36 #include <TopoDS.hxx>
37 #include <TopoDS_Wire.hxx>
38
39 #include <QColor>
40 #include <QStringList>
41
42 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline3D,HYDROData_Object)
43 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
44
45
46 HYDROData_Polyline3D::HYDROData_Polyline3D()
47 : HYDROData_Object()
48 {
49 }
50
51 HYDROData_Polyline3D::~HYDROData_Polyline3D()
52 {
53 }
54
55 QStringList HYDROData_Polyline3D::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
56 {
57   QStringList aResList = dumpObjectCreation( theTreatedObjects );
58   QString aPolylineName = GetObjPyName();
59
60   Handle(HYDROData_PolylineXY) aRefPolyline = GetPolylineXY();
61   setPythonReferenceObject( theTreatedObjects, aResList, aRefPolyline, "SetPolylineXY" );
62
63   Handle(HYDROData_ProfileUZ) aRefProfileUZ = GetProfileUZ();
64   if ( !aRefProfileUZ.IsNull() )
65   {
66     Handle(HYDROData_Profile) aProfile = 
67       Handle(HYDROData_Profile)::DownCast( aRefProfileUZ->GetFatherObject() );
68     if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
69     {
70       QString aProfileName = aProfile->GetObjPyName();
71       if ( !aProfileName.isEmpty() )
72       {
73         aResList << QString( "%1.SetProfileUZ( %2.GetProfileUZ() );" )
74                      .arg( aPolylineName ).arg( aProfileName );
75       }
76     }
77   }
78   else
79   {
80     Handle(HYDROData_IAltitudeObject) aRefBathymetry = GetAltitudeObject();
81     if ( !aRefBathymetry.IsNull() )
82     {
83       Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
84       if ( !aChildProfileUZ.IsNull() )
85       {
86         Handle(HYDROData_Profile) aProfile = 
87           Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
88         if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
89         {
90           QString aProfileName = aProfile->GetObjPyName();
91           if ( !aProfileName.isEmpty() )
92           {
93             aResList << QString( "%1.SetChildProfileUZ( %2.GetProfileUZ() );" )
94                          .arg( aPolylineName ).arg( aProfileName );
95           }
96         }
97       }
98
99       setPythonReferenceObject( theTreatedObjects, aResList, aRefBathymetry, "SetAltitudeObject" );
100     }
101   }
102
103   aResList << QString( "" );
104   aResList << QString( "%1.Update();" ).arg( aPolylineName );
105   aResList << QString( "" );
106
107   return aResList;
108 }
109
110 HYDROData_SequenceOfObjects HYDROData_Polyline3D::GetAllReferenceObjects() const
111 {
112   HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
113
114   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
115   if ( !aPolylineXY.IsNull() )
116     aResSeq.Append( aPolylineXY );
117
118   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
119   if ( !aProfileUZ.IsNull() )
120     aResSeq.Append( aProfileUZ );
121
122   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
123   if ( !aChildProfileUZ.IsNull() )
124     aResSeq.Append( aChildProfileUZ );
125
126   return aResSeq;
127 }
128
129 TopoDS_Shape HYDROData_Polyline3D::GetTopShape() const
130 {
131   return getTopShape();
132 }
133
134 TopoDS_Shape HYDROData_Polyline3D::GetShape3D() const
135 {
136   return getShape3D();
137 }
138
139 void HYDROData_Polyline3D::Update()
140 {
141   HYDROData_Object::Update();
142
143   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
144   if ( aPolylineXY.IsNull() )
145     return;
146
147   bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
148   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
149   HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
150   if ( aPolylinePoints.IsEmpty() )
151     return;
152
153   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
154
155   Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
156   if ( !anAltitude.IsNull() )
157     aProfileUZ = GetChildProfileUZ();
158
159   if ( aProfileUZ.IsNull() )
160     return;
161
162   HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
163   if ( aProfilePoints.IsEmpty() )
164     return;
165
166   const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
167   const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
168
169   const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
170   const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
171
172   double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
173   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
174
175   NCollection_Sequence<Polyline3DPoint> aResPoints;
176   
177   // Add first point as is
178   aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
179
180   for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
181   {
182     const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
183
184     double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
185
186     double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
187     double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
188
189     Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
190     aResPoints.Append( aCompPoint );
191   }
192
193   // Add last point as is
194   aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
195
196   TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
197   SetTopShape( aResWire );
198   SetShape3D( aResWire );
199
200
201 QColor HYDROData_Polyline3D::DefaultFillingColor()
202 {
203   return QColor( Qt::transparent );
204 }
205
206 QColor HYDROData_Polyline3D::DefaultBorderColor()
207 {
208   return QColor( Qt::red );
209 }
210
211 QColor HYDROData_Polyline3D::getDefaultFillingColor() const
212 {
213   return DefaultFillingColor();
214 }
215
216 QColor HYDROData_Polyline3D::getDefaultBorderColor() const
217 {
218   return DefaultBorderColor();
219 }
220
221 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
222                                           const bool                          theIsUpdateProfile )
223 {
224   if ( thePolyline.IsNull() )
225     return false;
226   
227   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
228   if ( IsEqual( aPrevPolyline, thePolyline ) )
229     return true;
230
231   SetReferenceObject( thePolyline, DataTag_PolylineXY );
232
233   // Update the child profile object 
234   if ( theIsUpdateProfile )
235     updateChildProfilePoints();
236
237   // Indicate model of the need to update the polyline presentation
238   SetToUpdate( true );
239
240   return true;
241 }
242
243 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
244 {
245   return Handle(HYDROData_PolylineXY)::DownCast( 
246            GetReferenceObject( DataTag_PolylineXY ) );
247 }
248
249 void HYDROData_Polyline3D::RemovePolylineXY()
250 {
251   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
252   if ( aPrevPolyline.IsNull() )
253     return;
254
255   ClearReferenceObjects( DataTag_PolylineXY );
256
257   // Indicate model of the need to update the polyline presentation
258   SetToUpdate( true );
259 }
260
261 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
262 {
263   if ( theProfile.IsNull() )
264     return false;
265   
266   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
267   if ( IsEqual( aPrevProfile, theProfile ) )
268     return true;
269
270   SetReferenceObject( theProfile, DataTag_ProfileUZ );
271
272   // Remove the bathymetry, because one altitude object can be presented at time
273   RemoveAltitudeObject();
274
275   // Indicate model of the need to update the polyline presentation
276   SetToUpdate( true );
277
278   return true;
279 }
280
281 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
282 {
283   return Handle(HYDROData_ProfileUZ)::DownCast( 
284            GetReferenceObject( DataTag_ProfileUZ ) );
285 }
286
287 void HYDROData_Polyline3D::RemoveProfileUZ()
288 {
289   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
290   if ( aPrevProfile.IsNull() )
291     return;
292
293   ClearReferenceObjects( DataTag_ProfileUZ );
294
295   // Indicate model of the need to update the polyline presentation
296   SetToUpdate( true );
297 }
298
299 bool HYDROData_Polyline3D::SetAltitudeObject( 
300   const Handle(HYDROData_IAltitudeObject)& theAltitude )
301 {
302   Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
303
304   if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
305     return false;
306
307   if ( IsEqual( aPrevAltitude, theAltitude ) )
308     return true;
309
310   // Remove the u,z profile, because one altitude object can be presented at time
311   RemoveProfileUZ();
312
313   // Create the child profile object 
314   updateChildProfilePoints();
315
316   return true;
317 }
318
319
320 void HYDROData_Polyline3D::RemoveAltitudeObject()
321 {
322   HYDROData_Object::RemoveAltitudeObject();
323
324   // Remove the child profile object 
325   removeChildProfileUZ();
326 }
327
328 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
329 {
330   Handle(HYDROData_ProfileUZ) aProfileUZ =
331     Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
332   if ( !theIsCreate || !aProfileUZ.IsNull() )
333     return aProfileUZ;
334
335   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
336   if ( aDocument.IsNull() )
337     return aProfileUZ;
338
339   Handle(HYDROData_Profile) aProfile =
340     Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
341   
342   QString aProfilePref = GetName() + "_Profile";
343   QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
344
345   aProfile->SetName( aProfileName );
346
347   aProfileUZ = aProfile->GetProfileUZ();
348
349   HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
350   me->SetChildProfileUZ( aProfileUZ );
351
352   return aProfileUZ;
353 }
354
355 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
356 {
357   SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
358 }
359
360 HYDROData_IPolyline::PointsList generateProfileUZPoints(
361   const Handle(HYDROData_PolylineXY)&      thePolyline,
362   const Handle(HYDROData_IAltitudeObject)& theAltitude )
363 {
364   HYDROData_IPolyline::PointsList aPointsList;
365   if ( thePolyline.IsNull() || theAltitude.IsNull() )
366     return aPointsList;
367
368   bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
369   HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
370   HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
371   if ( aPolylinePoints.IsEmpty() )
372     return aPointsList;
373
374   for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
375   {
376     const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
377
378     double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
379     double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
380     if( aPointDepth == theAltitude->GetInvalidAltitude() )
381       aPointDepth = 0.0;
382
383     HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
384     aPointsList.Append( anAltitudePoint );
385   }
386
387   return aPointsList;
388 }
389
390 void HYDROData_Polyline3D::updateChildProfilePoints()
391 {
392   Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
393   if ( anAltitude.IsNull() )
394     return;
395
396   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
397   if ( aChildProfileUZ.IsNull() )
398     return;
399
400   Handle(HYDROData_Profile) aProfile = 
401     Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
402   if ( aProfile.IsNull() )
403     return;
404
405   HYDROData_IPolyline::PointsList aProfilePoints = 
406     generateProfileUZPoints( GetPolylineXY(), anAltitude );
407   aProfile->SetParametricPoints( aProfilePoints );
408
409   aProfile->Update();
410 }
411
412 void HYDROData_Polyline3D::removeChildProfileUZ()
413 {
414   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
415   if ( aChildProfileUZ.IsNull() )
416     return;
417
418   ClearReferenceObjects( DataTag_ChildProfileUZ );
419
420   /* Uncomment if removing is requested
421   Handle(HYDROData_Profile) aProfile = 
422     Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
423   if ( !aProfile.IsNull() )
424     aProfile->Remove();
425   */
426 }
427
428