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