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