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