2 #include "HYDROData_Profile.h"
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Iterator.h"
6 #include "HYDROData_Tool.h"
7 #include "HYDROData_PolylineXY.h"
9 #include <boost/math/special_functions/fpclassify.hpp>
11 #include <BRepBuilderAPI_MakeEdge.hxx>
12 #include <BRepBuilderAPI_MakeWire.hxx>
13 #include <BRepBuilderAPI_MakePolygon.hxx>
17 #include <gp_Pnt2d.hxx>
19 #include <TDataStd_AsciiString.hxx>
20 #include <TDataStd_RealArray.hxx>
22 #include <TopoDS_Edge.hxx>
23 #include <TopoDS_Wire.hxx>
25 #include <OSD_File.hxx>
26 #include <OSD_Protection.hxx>
29 #include <QStringList>
31 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
32 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
34 HYDROData_Profile::HYDROData_Profile()
39 HYDROData_Profile::~HYDROData_Profile()
43 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
45 QStringList aResList = dumpObjectCreation( theTreatedObjects );
46 QString aProfileName = GetObjPyName();
48 //TCollection_AsciiString aFilePath = GetFilePath();
49 //if ( !aFilePath.IsEmpty() )
51 // aResList << QString( "%1.ImportFromFile( \"%2\" );" )
52 // .arg( aName ).arg( aFilePath.ToCString() );
55 bool anIsValidProfile = IsValid();
57 QStringList aPntsDefinition;
58 QString aPntsListName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "profile_points" );
60 QString aGap = QString().fill( ' ', aPntsListName.length() + 5 );
61 if ( anIsValidProfile )
63 HYDROData_Profile::ProfilePoints aPointsList = GetProfilePoints( true );
64 for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
66 const ProfilePoint& aPoint = aPointsList.Value( k );
67 aPntsDefinition << QString( aGap + "gp_XYZ( %1, %2, %3 )%4" )
68 .arg( aPoint.X() ).arg( aPoint.Y() ).arg( aPoint.Z() )
69 .arg( ( k < aNbPoints ? "," : "" ) );
74 HYDROData_IPolyline::PointsList aPointsList = GetParametricPoints();
75 for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
77 const HYDROData_IPolyline::Point& aPoint = aPointsList.Value( k );
78 aPntsDefinition << QString( aGap + "gp_XY( %1, %2 )%3" )
79 .arg( aPoint.X() ).arg( aPoint.Y() )
80 .arg( ( k < aNbPoints ? "," : "" ) );
84 if ( !aPntsDefinition.isEmpty() )
86 QString& aFirstStr = aPntsDefinition.first();
87 aFirstStr = aFirstStr.trimmed();
88 aFirstStr.prepend( QString( "%1 = [ " ).arg( aPntsListName ) );
90 aPntsDefinition.last().append( " ];" );
92 aResList << aPntsDefinition;
94 aResList << QString( "%1.%3( %2 );" )
95 .arg( aProfileName ).arg( aPntsListName )
96 .arg( anIsValidProfile ? "SetProfilePoints" : "SetParametricPoints" );
98 aResList << QString( "" );
101 // Set a polyline type if it is not default
102 Handle(HYDROData_ProfileUZ) aPrf = GetProfileUZ( false );
103 if ( !aPrf.IsNull() )
105 HYDROData_IPolyline::SectionType aSecType = aPrf->GetSectionType( 0 );
106 if ( aSecType != HYDROData_IPolyline::SECTION_POLYLINE )
108 aResList << QString( "%1.GetProfileUZ().SetSectionType( 0, %2 );" )
109 .arg( aProfileName ).arg( "HYDROData_IPolyline.SECTION_SPLINE" );
110 aResList << QString( "" );
114 aResList << QString( "%1.Update();" ).arg( aProfileName );
115 aResList << QString( "" );
120 TopoDS_Shape HYDROData_Profile::GetTopShape() const
124 gp_XY aFirstPoint, aLastPoint;
125 if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
128 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
129 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
131 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
132 TopoDS_Edge anEdge = aMakeEdge;
134 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
140 TopoDS_Shape HYDROData_Profile::GetShape3D() const
145 void HYDROData_Profile::Update()
147 HYDROData_Object::Update();
150 Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
151 if ( !aProfile.IsNull() )
153 ProfilePoints aProfilePoints = GetProfilePoints( false );
154 HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
156 aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
161 QColor HYDROData_Profile::DefaultFillingColor()
163 return QColor( Qt::transparent );
166 QColor HYDROData_Profile::DefaultBorderColor()
168 return QColor( Qt::black );
171 QColor HYDROData_Profile::getDefaultFillingColor() const
173 return DefaultFillingColor();
176 QColor HYDROData_Profile::getDefaultBorderColor() const
178 return DefaultBorderColor();
181 bool HYDROData_Profile::IsValid() const
183 gp_XY aFirstPoint, aLastPoint;
184 if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
187 int aNbPoints = NbPoints();
188 return aNbPoints > 1;
191 void HYDROData_Profile::SetLeftPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
193 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
194 if ( aLabel.IsNull() )
197 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
198 gp_XY aLPoint = theGPoint;
199 if( IsConvertFromGlobal )
200 aDoc->Transform( aLPoint, true );
202 Handle(TDataStd_RealArray) anArray;
203 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
204 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
206 anArray->SetValue( 0, aLPoint.X() );
207 anArray->SetValue( 1, aLPoint.Y() );
212 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint, bool IsConvertToGlobal ) const
214 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
215 if ( aLabel.IsNull() )
218 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
219 Handle(TDataStd_RealArray) anArray;
220 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
223 thePoint.SetX( anArray->Value( 0 ) );
224 thePoint.SetY( anArray->Value( 1 ) );
226 if( IsConvertToGlobal )
227 aDoc->Transform( thePoint, false );
232 void HYDROData_Profile::SetRightPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
234 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
236 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
237 gp_XY aLPoint = theGPoint;
238 if( IsConvertFromGlobal )
239 aDoc->Transform( aLPoint, true );
241 Handle(TDataStd_RealArray) anArray;
242 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
243 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
245 anArray->SetValue( 0, aLPoint.X() );
246 anArray->SetValue( 1, aLPoint.Y() );
251 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint, bool IsConvertToGlobal ) const
253 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
254 if ( aLabel.IsNull() )
257 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
258 Handle(TDataStd_RealArray) anArray;
259 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
262 thePoint.SetX( anArray->Value( 0 ) );
263 thePoint.SetY( anArray->Value( 1 ) );
265 if( IsConvertToGlobal )
266 aDoc->Transform( thePoint, false );
271 void HYDROData_Profile::Invalidate()
273 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
274 if ( !aFirstLabel.IsNull() )
275 aFirstLabel.ForgetAllAttributes();
277 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
278 if ( !aLastLabel.IsNull() )
279 aLastLabel.ForgetAllAttributes();
284 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
286 Handle(HYDROData_ProfileUZ) aProfileUZ;
288 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
289 if ( aLabel.IsNull() )
292 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
293 if ( aProfileUZ.IsNull() && theIsCreate )
295 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
296 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
302 int HYDROData_Profile::NbPoints() const
304 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
305 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
308 void HYDROData_Profile::RemovePoints()
310 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
311 if ( !aProfileUZ.IsNull() )
313 aProfileUZ->RemoveSections();
318 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
322 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
323 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
325 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
326 aProfileUZ->AddPoint( 0, aPoint );
332 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
334 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
335 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
338 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints, bool IsConvertFromGlobal )
341 if ( thePoints.Length() < 2 )
344 gp_XY aFirstPoint, aLastPoint;
346 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
347 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
348 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
350 ProfilePoint aPoint = thePoints.Value( i );
351 if( IsConvertFromGlobal )
352 aDoc->Transform( aPoint, true );
354 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
357 aFirstPoint = aPointXY;
359 aLastPoint = aPointXY;
361 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
363 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
364 aProfileUZ->AddPoint( 0, aParPoint );
367 SetLeftPoint( aFirstPoint, false );//already converted to local CS
368 SetRightPoint( aLastPoint, false );
371 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints( bool IsConvertToGlobal ) const
373 ProfilePoints aResPoints;
375 gp_XY aFirstPoint, aLastPoint;
376 if ( !GetLeftPoint( aFirstPoint, IsConvertToGlobal ) ||
377 !GetRightPoint( aLastPoint, IsConvertToGlobal ) )
380 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
381 if ( aParametricPoints.Length() < 2 )
384 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
385 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
387 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
388 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
390 // Add first point as is
391 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
393 // Compute all other points
394 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
396 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
398 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
400 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
402 double aRatio = aParLen / ( aGeoDistance - aParLen );
404 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
405 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
407 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
408 aResPoints.Append( aCompPoint );
411 // Add last point as is
412 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
417 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
419 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
422 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
424 TCollection_AsciiString aRes;
426 Handle(TDataStd_AsciiString) anAsciiStr;
427 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
428 aRes = anAsciiStr->Get();
433 int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
434 const TCollection_AsciiString& theFileName,
435 NCollection_Sequence<int>& theBadProfilesIds )
437 if ( theDoc.IsNull() || theFileName.IsEmpty() )
440 OSD_File aFile( theFileName );
441 if ( !aFile.IsReadable() )
444 aFile.Open( OSD_ReadOnly, OSD_Protection() );
445 if ( !aFile.IsOpen() )
448 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
451 Handle(HYDROData_Profile) aNewProfile;
452 for ( ; !aFile.IsAtEnd(); ++aProfileId )
454 if ( aNewProfile.IsNull() )
455 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
457 bool anIsRead = false;
458 if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) )
460 aCreatedProfiles.Append( aNewProfile );
461 aNewProfile.Nullify();
465 theBadProfilesIds.Append( aProfileId );
469 if ( !aNewProfile.IsNull() )
470 aNewProfile->Remove();
475 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
477 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
479 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
480 aProfile->SetName( aProfileName );
482 aProfile->SetFilePath( theFileName );
484 aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
487 return aCreatedProfiles.Length();
490 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
496 // Try to open the file
497 OSD_File aFile( theFileName );
498 if ( !aFile.IsReadable() )
501 aFile.Open( OSD_ReadOnly, OSD_Protection() );
502 if ( !aFile.IsOpen() )
505 bool aRes = ImportFromFile( aFile, theIsRead );
513 SetFilePath( theFileName );
519 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
525 if ( !theFile.IsOpen() )
530 bool anIsParametric = false;
531 bool anIsGeoref = false;
533 HYDROData_ProfileUZ::PointsList aPointsUZ;
534 ProfilePoints aPointsXYZ;
536 double aPrevVal = -DBL_MAX;
537 while ( !theFile.IsAtEnd() )
539 Standard_Integer aNbRead = 0;
540 TCollection_AsciiString aLine;
541 theFile.ReadLine( aLine, 1024, aNbRead );
543 aLine.LeftAdjust(); aLine.RightAdjust();
544 if ( aLine.IsEmpty() )
546 if ( !anIsParametric && !anIsGeoref )
547 continue; // Definition is not started yet
549 break; // Next profile started
552 // Set flag of read status to true
556 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
557 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
558 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
560 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
561 aValY.IsEmpty() || !aValY.IsRealValue() )
567 if ( !anIsParametric && !anIsGeoref )
569 anIsParametric = aValZ.IsEmpty();
570 anIsGeoref = !aValZ.IsEmpty();
573 double aCoordX = aValX.RealValue();
574 double aCoordY = aValY.RealValue();
576 if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) ||
577 boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) )
580 if ( anIsParametric )
582 if ( aCoordX < aPrevVal )
584 // Move back readed line
585 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
589 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
590 aPointsUZ.Append( aPoint );
596 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
602 double aCoordZ = aValZ.RealValue();
603 if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) )
606 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
607 aPointsXYZ.Append( aPoint );
611 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
612 anIsGeoref && !aPointsXYZ.IsEmpty() );
615 // Update profile points
616 if ( anIsParametric )
618 SetParametricPoints( aPointsUZ );
620 else if ( anIsGeoref )
622 SetProfilePoints( aPointsXYZ, true );
631 void HYDROData_Profile::UpdateLocalCS( double theDx, double theDy )
633 gp_XY aDelta( theDx, theDy );
636 GetLeftPoint( aPnt, false );
638 SetLeftPoint( aPnt, false );
640 GetRightPoint( aPnt, false );
642 SetRightPoint( aPnt, false );