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