Salome HOME
Comments moved.
[modules/hydro.git] / src / HYDROData / HYDROData_Profile.cxx
1
2 #include "HYDROData_Profile.h"
3
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Iterator.h"
6 #include "HYDROData_Tool.h"
7
8 #include <BRepBuilderAPI_MakeEdge.hxx>
9 #include <BRepBuilderAPI_MakeWire.hxx>
10
11 #include <gp_XY.hxx>
12 #include <gp_XYZ.hxx>
13 #include <gp_Pnt2d.hxx>
14
15 #include <TDataStd_AsciiString.hxx>
16 #include <TDataStd_RealArray.hxx>
17
18 #include <TopoDS_Edge.hxx>
19 #include <TopoDS_Wire.hxx>
20
21 #include <OSD_File.hxx>
22 #include <OSD_Protection.hxx>
23
24 #include <QColor>
25 #include <QStringList>
26
27 #define PYTHON_PROFILE_ID "KIND_PROFILE"
28
29 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
30 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
31
32 HYDROData_Profile::HYDROData_Profile()
33 : HYDROData_Object()
34 {
35 }
36
37 HYDROData_Profile::~HYDROData_Profile()
38 {
39 }
40
41 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
42 {
43   QStringList aResList;
44
45   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
46   if ( aDocument.IsNull() )
47     return aResList;
48                              
49   QString aDocName = aDocument->GetDocPyName();
50   QString aProfileName = GetName();
51
52   aResList << QString( "%1 = %2.CreateObject( %3 );" )
53               .arg( aProfileName ).arg( aDocName ).arg( PYTHON_PROFILE_ID );
54   aResList << QString( "%1.SetName( \"%1\" );" ).arg( aProfileName );
55
56   return aResList;
57 }
58
59 TopoDS_Shape HYDROData_Profile::GetTopShape() const
60 {
61   TopoDS_Wire aWire;
62
63   gp_XY aFirstPoint, aLastPoint;
64   if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
65     return aWire;
66
67   gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
68   gp_Pnt aPnt2( aLastPoint.X(),  aLastPoint.Y(),  0 );
69
70   BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
71   TopoDS_Edge anEdge = aMakeEdge;
72
73   BRepBuilderAPI_MakeWire aMakeWire( anEdge );
74   aWire = aMakeWire;
75
76   return aWire;
77 }
78
79 TopoDS_Shape HYDROData_Profile::GetShape3D() const
80 {
81   return getShape3D();
82 }
83
84 void HYDROData_Profile::Update()
85 {
86   HYDROData_Object::Update();
87
88   BRepBuilderAPI_MakeWire aMakeWire;
89
90   ProfilePoints aProfilePoints = GetProfilePoints();
91   for ( int i = 1, n = aProfilePoints.Length(); i < n ; ++i )
92   {
93     ProfilePoint aFirstPoint = aProfilePoints.Value( i );
94     ProfilePoint aSecPoint = aProfilePoints.Value( i + 1 );
95
96     gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
97     gp_Pnt aPnt2( aSecPoint.X(),   aSecPoint.Y(),   aSecPoint.Z()   );
98
99     BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
100     TopoDS_Edge anEdge = aMakeEdge;
101
102     aMakeWire.Add( anEdge );
103   }
104
105   TopoDS_Wire aWire;
106   if ( aMakeWire.IsDone() )
107     aWire = aMakeWire;
108
109   SetShape3D( aWire );
110 }
111
112 QColor HYDROData_Profile::DefaultFillingColor()
113 {
114   return QColor( Qt::transparent );
115 }
116
117 QColor HYDROData_Profile::DefaultBorderColor()
118 {
119   return QColor( Qt::black );
120 }
121
122 QColor HYDROData_Profile::getDefaultFillingColor() const
123 {
124   return DefaultFillingColor();
125 }
126
127 QColor HYDROData_Profile::getDefaultBorderColor() const
128 {
129   return DefaultBorderColor();
130 }
131
132 bool HYDROData_Profile::IsValid() const
133 {
134   gp_XY aFirstPoint, aLastPoint;
135   if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
136     return false;
137
138   int aNbPoints = NbPoints();
139   return aNbPoints > 1;
140 }
141
142 void HYDROData_Profile::SetFirstPoint( const gp_XY& thePoint )
143 {
144   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
145
146   Handle(TDataStd_RealArray) anArray;
147   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
148     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
149
150   anArray->SetValue( 0, thePoint.X() );
151   anArray->SetValue( 1, thePoint.Y() );
152
153   SetToUpdate( true );
154 }
155
156 bool HYDROData_Profile::GetFirstPoint( gp_XY& thePoint ) const
157 {
158   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
159   if ( aLabel.IsNull() )
160     return false;
161
162   Handle(TDataStd_RealArray) anArray;
163   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
164     return false;
165
166   thePoint.SetX( anArray->Value( 0 ) );
167   thePoint.SetY( anArray->Value( 1 ) );
168
169   return true;
170 }
171
172 void HYDROData_Profile::SetLastPoint( const gp_XY& thePoint )
173 {
174   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
175
176   Handle(TDataStd_RealArray) anArray;
177   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
178     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
179
180   anArray->SetValue( 0, thePoint.X() );
181   anArray->SetValue( 1, thePoint.Y() );
182
183   SetToUpdate( true );
184 }
185
186 bool HYDROData_Profile::GetLastPoint( gp_XY& thePoint ) const
187 {
188   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
189   if ( aLabel.IsNull() )
190     return false;
191
192   Handle(TDataStd_RealArray) anArray;
193   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
194     return false;
195
196   thePoint.SetX( anArray->Value( 0 ) );
197   thePoint.SetY( anArray->Value( 1 ) );
198
199   return true;
200 }
201
202 void HYDROData_Profile::Invalidate()
203 {
204   TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
205   if ( !aFirstLabel.IsNull() )
206     aFirstLabel.ForgetAllAttributes();
207
208   TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
209   if ( !aLastLabel.IsNull() )
210     aLastLabel.ForgetAllAttributes();
211
212   SetToUpdate( true );
213 }
214
215 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
216 {
217   Handle(HYDROData_ProfileUZ) aProfileUZ;
218
219   TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
220   if ( aLabel.IsNull() )
221     return aProfileUZ;
222
223   aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
224   if ( aProfileUZ.IsNull() && theIsCreate )
225   {
226     aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
227       HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
228   }
229
230   return aProfileUZ;
231 }
232
233 int HYDROData_Profile::NbPoints() const
234 {
235   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
236   return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
237 }
238
239 void HYDROData_Profile::RemovePoints()
240 {
241   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
242   if ( !aProfileUZ.IsNull() )
243   {
244     aProfileUZ->RemoveSections();
245     SetToUpdate( true );
246   }
247 }
248
249 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
250 {
251   RemovePoints();
252
253   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
254   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
255   {
256     const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
257     aProfileUZ->AddPoint( 0, aPoint );
258   }
259
260   SetToUpdate( true );
261 }
262
263 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
264 {
265   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
266   return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
267 }
268
269 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
270 {
271   RemovePoints();
272   if ( thePoints.Length() < 2 )
273     return;
274
275   gp_XY aFirstPoint, aLastPoint;
276
277   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
278   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
279   {
280     const ProfilePoint& aPoint = thePoints.Value( i );
281     gp_XY aPointXY( aPoint.X(), aPoint.Y() );
282
283     if ( i == 1 )
284       aFirstPoint = aPointXY;
285     else if ( i == n )
286       aLastPoint = aPointXY;
287
288     double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
289     
290     HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
291     aProfileUZ->AddPoint( 0, aParPoint );
292   }
293
294   SetFirstPoint( aFirstPoint );
295   SetLastPoint( aLastPoint );
296 }
297
298 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
299 {
300   ProfilePoints aResPoints;
301
302   gp_XY aFirstPoint, aLastPoint;
303   if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
304     return aResPoints;
305
306   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
307   if ( aParametricPoints.Length() < 2 )
308     return aResPoints;
309
310   const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
311   const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
312
313   double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
314   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
315
316   // Add first point as is
317   aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
318
319   // Compute all other points
320   for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
321   {
322     const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
323
324     double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
325     
326     double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
327
328     double aRatio = aParLen / ( aGeoDistance - aParLen );
329
330     double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
331     double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
332
333     ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
334     aResPoints.Append( aCompPoint );
335   }
336
337   // Add last point as is
338   aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
339
340   return aResPoints;
341 }
342
343 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
344 {
345   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
346 }
347
348 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
349 {
350   TCollection_AsciiString aRes;
351
352   Handle(TDataStd_AsciiString) anAsciiStr;
353   if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
354     aRes = anAsciiStr->Get();
355
356   return aRes;
357 }
358
359 bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
360                                         const TCollection_AsciiString&    theFileName )
361 {
362   if ( theDoc.IsNull() || theFileName.IsEmpty() )
363     return false;
364
365   OSD_File aFile( theFileName );
366   if ( !aFile.IsReadable() )
367     return false;
368
369   aFile.Open( OSD_ReadOnly, OSD_Protection() );
370   if ( !aFile.IsOpen() )
371     return false;
372
373   NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
374
375   Handle(HYDROData_Profile) aNewProfile;
376   while ( !aFile.IsAtEnd() )
377   {
378     if ( aNewProfile.IsNull() )
379       aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
380     
381     if ( aNewProfile->ImportFromFile( aFile ) )
382     {
383       aCreatedProfiles.Append( aNewProfile );
384       aNewProfile.Nullify();
385     }
386   }
387
388   if ( !aNewProfile.IsNull() )
389     aNewProfile->Remove();
390
391   // Close the file
392   aFile.Close();
393
394   for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
395   {
396     Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
397
398     QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
399     aProfile->SetName( aProfileName );
400
401     aProfile->SetFilePath( theFileName );
402
403     aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
404   }
405
406   return !aCreatedProfiles.IsEmpty();
407 }
408
409 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
410 {
411   // Try to open the file
412   OSD_File aFile( theFileName );
413   if ( !aFile.IsReadable() )
414     return false;
415
416   aFile.Open( OSD_ReadOnly, OSD_Protection() );
417   if ( !aFile.IsOpen() )
418     return false;
419
420   bool aRes = ImportFromFile( aFile );
421
422   // Close the file
423   aFile.Close();
424
425   if ( aRes )
426   {
427     // Update file path
428     SetFilePath( theFileName );
429   }
430
431   return aRes;
432 }
433
434 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
435 {
436   if ( !theFile.IsOpen() )
437     return false;
438
439   bool aRes = true;
440
441   bool anIsParametric = false;
442   bool anIsGeoref     = false;
443
444   HYDROData_ProfileUZ::PointsList aPointsUZ;
445   ProfilePoints                   aPointsXYZ;
446
447   double aPrevVal = -DBL_MAX;
448   while ( !theFile.IsAtEnd() )
449   {
450     Standard_Integer aNbRead = 0;
451     TCollection_AsciiString aLine;
452     theFile.ReadLine( aLine, 1024, aNbRead );
453
454     aLine.LeftAdjust(); aLine.RightAdjust();
455     if ( aLine.IsEmpty() )
456     {
457       if ( !anIsParametric && !anIsGeoref )
458         continue; // Definition is not started yet
459
460       break; // Next profile started
461     }
462
463     TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
464     TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
465     TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
466
467     if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
468          aValY.IsEmpty() || !aValY.IsRealValue() )
469     {
470       aRes = false;
471       break;
472     }
473
474     if ( !anIsParametric && !anIsGeoref )
475     {
476       anIsParametric = aValZ.IsEmpty();
477       anIsGeoref = !aValZ.IsEmpty();
478     }
479
480     double aCoordX = aValX.RealValue();
481     double aCoordY = aValY.RealValue();
482
483     if ( anIsParametric )
484     {
485       if ( aCoordX < aPrevVal )
486       {
487         // Move back readed line
488         theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
489         break;
490       }
491
492       HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
493       aPointsUZ.Append( aPoint );
494
495       aPrevVal = aCoordX;
496     }
497     else
498     {
499       if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
500       {
501         aRes = false;
502         break;
503       }
504
505       double aCoordZ = aValZ.RealValue();
506
507       ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
508       aPointsXYZ.Append( aPoint );
509     }
510   }
511   
512   aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() || 
513                    anIsGeoref && !aPointsXYZ.IsEmpty() );
514   if ( aRes )
515   {
516     // Update profile points
517     if ( anIsParametric )
518     {
519       SetParametricPoints( aPointsUZ );
520     }
521     else if ( anIsGeoref )
522     {
523       SetProfilePoints( aPointsXYZ );
524     }
525
526     Update();
527   }
528
529   return aRes;
530 }
531
532
533