1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_Profile.h"
21 #include "HYDROData_Document.h"
22 #include "HYDROData_Iterator.h"
23 #include "HYDROData_Tool.h"
24 #include "HYDROData_PolylineXY.h"
26 #include <boost/math/special_functions/fpclassify.hpp>
28 #include <BRepBuilderAPI_MakeEdge.hxx>
29 #include <BRepBuilderAPI_MakeWire.hxx>
30 #include <BRepBuilderAPI_MakePolygon.hxx>
34 #include <gp_Pnt2d.hxx>
36 #include <TDataStd_AsciiString.hxx>
37 #include <TDataStd_RealArray.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Wire.hxx>
42 #include <OSD_File.hxx>
43 #include <OSD_Protection.hxx>
46 #include <QStringList>
48 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
49 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
51 HYDROData_Profile::HYDROData_Profile()
56 HYDROData_Profile::~HYDROData_Profile()
60 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
62 QStringList aResList = dumpObjectCreation( theTreatedObjects );
63 QString aProfileName = GetObjPyName();
65 //TCollection_AsciiString aFilePath = GetFilePath();
66 //if ( !aFilePath.IsEmpty() )
68 // aResList << QString( "%1.ImportFromFile( \"%2\" );" )
69 // .arg( aName ).arg( aFilePath.ToCString() );
72 bool anIsValidProfile = IsValid();
74 QStringList aPntsDefinition;
75 QString aPntsListName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "profile_points" );
77 QString aGap = QString().fill( ' ', aPntsListName.length() + 5 );
78 if ( anIsValidProfile )
80 HYDROData_Profile::ProfilePoints aPointsList = GetProfilePoints( true );
81 for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
83 const ProfilePoint& aPoint = aPointsList.Value( k );
84 aPntsDefinition << QString( aGap + "gp_XYZ( %1, %2, %3 )%4" )
85 .arg( aPoint.X() ).arg( aPoint.Y() ).arg( aPoint.Z() )
86 .arg( ( k < aNbPoints ? "," : "" ) );
91 HYDROData_IPolyline::PointsList aPointsList = GetParametricPoints();
92 for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
94 const HYDROData_IPolyline::Point& aPoint = aPointsList.Value( k );
95 aPntsDefinition << QString( aGap + "gp_XY( %1, %2 )%3" )
96 .arg( aPoint.X() ).arg( aPoint.Y() )
97 .arg( ( k < aNbPoints ? "," : "" ) );
101 if ( !aPntsDefinition.isEmpty() )
103 QString& aFirstStr = aPntsDefinition.first();
104 aFirstStr = aFirstStr.trimmed();
105 aFirstStr.prepend( QString( "%1 = [ " ).arg( aPntsListName ) );
107 aPntsDefinition.last().append( " ];" );
109 aResList << aPntsDefinition;
111 aResList << QString( "%1.%3( %2 );" )
112 .arg( aProfileName ).arg( aPntsListName )
113 .arg( anIsValidProfile ? "SetProfilePoints" : "SetParametricPoints" );
115 aResList << QString( "" );
118 // Set a polyline type if it is not default
119 Handle(HYDROData_ProfileUZ) aPrf = GetProfileUZ( false );
120 if ( !aPrf.IsNull() )
122 HYDROData_IPolyline::SectionType aSecType = aPrf->GetSectionType( 0 );
123 if ( aSecType != HYDROData_IPolyline::SECTION_POLYLINE )
125 aResList << QString( "%1.GetProfileUZ().SetSectionType( 0, %2 );" )
126 .arg( aProfileName ).arg( "HYDROData_IPolyline.SECTION_SPLINE" );
127 aResList << QString( "" );
131 aResList << QString( "%1.Update();" ).arg( aProfileName );
132 aResList << QString( "" );
137 TopoDS_Shape HYDROData_Profile::GetTopShape() const
141 gp_XY aFirstPoint, aLastPoint;
142 if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
145 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
146 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
148 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
149 TopoDS_Edge anEdge = aMakeEdge;
151 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
157 TopoDS_Shape HYDROData_Profile::GetShape3D() const
159 TopoDS_Shape aShape = getShape3D();
160 if( aShape.IsNull() )
161 aShape = CreateProfileWire( true );
165 TopoDS_Shape HYDROData_Profile::CreateProfileWire( bool canUseDefaultPoints ) const
168 Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
169 if ( !aProfile.IsNull() )
171 ProfilePoints aProfilePoints = GetProfilePoints( false, canUseDefaultPoints );
172 HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
174 aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
179 void HYDROData_Profile::Update()
181 HYDROData_Object::Update();
183 TopoDS_Shape aShape = CreateProfileWire( false );
184 SetShape3D( aShape );
187 QColor HYDROData_Profile::DefaultFillingColor()
189 return QColor( Qt::transparent );
192 QColor HYDROData_Profile::DefaultBorderColor()
194 return QColor( Qt::black );
197 QColor HYDROData_Profile::getDefaultFillingColor() const
199 return DefaultFillingColor();
202 QColor HYDROData_Profile::getDefaultBorderColor() const
204 return DefaultBorderColor();
207 bool HYDROData_Profile::IsValid() const
209 gp_XY aFirstPoint, aLastPoint;
210 if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
213 int aNbPoints = NbPoints();
214 return aNbPoints > 1;
217 void HYDROData_Profile::SetLeftPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
219 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
220 if ( aLabel.IsNull() )
223 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
224 gp_XY aLPoint = theGPoint;
225 if( IsConvertFromGlobal )
226 aDoc->Transform( aLPoint, true );
228 Handle(TDataStd_RealArray) anArray;
229 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
230 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
232 anArray->SetValue( 0, aLPoint.X() );
233 anArray->SetValue( 1, aLPoint.Y() );
238 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint, bool IsConvertToGlobal,
239 bool CanUseDefault ) const
241 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
242 if ( aParametricPoints.Length() < 2 )
245 thePoint = GetParametricPoints().First();
248 thePoint.SetY( 0 ); //default left point of not-georeferenced profile
249 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
250 if ( aLabel.IsNull() )
252 return CanUseDefault;
255 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
256 Handle(TDataStd_RealArray) anArray;
257 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
259 return CanUseDefault;
262 thePoint.SetX( anArray->Value( 0 ) );
263 thePoint.SetY( anArray->Value( 1 ) );
265 if( IsConvertToGlobal )
266 aDoc->Transform( thePoint, false );
271 void HYDROData_Profile::SetRightPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
273 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
275 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
276 gp_XY aLPoint = theGPoint;
277 if( IsConvertFromGlobal )
278 aDoc->Transform( aLPoint, true );
280 Handle(TDataStd_RealArray) anArray;
281 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
282 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
284 anArray->SetValue( 0, aLPoint.X() );
285 anArray->SetValue( 1, aLPoint.Y() );
290 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint, bool IsConvertToGlobal,
291 bool CanUseDefault ) const
293 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
294 if ( aParametricPoints.Length() < 2 )
297 thePoint = GetParametricPoints().Last();
299 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
300 if ( aLabel.IsNull() )
302 return CanUseDefault;
305 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
306 Handle(TDataStd_RealArray) anArray;
307 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
309 return CanUseDefault;
312 thePoint.SetX( anArray->Value( 0 ) );
313 thePoint.SetY( anArray->Value( 1 ) );
315 if( IsConvertToGlobal )
316 aDoc->Transform( thePoint, false );
321 void HYDROData_Profile::Invalidate()
323 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
324 if ( !aFirstLabel.IsNull() )
325 aFirstLabel.ForgetAllAttributes();
327 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
328 if ( !aLastLabel.IsNull() )
329 aLastLabel.ForgetAllAttributes();
334 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
336 Handle(HYDROData_ProfileUZ) aProfileUZ;
338 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
339 if ( aLabel.IsNull() )
342 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
343 if ( aProfileUZ.IsNull() && theIsCreate )
345 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
346 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
352 int HYDROData_Profile::NbPoints() const
354 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
355 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
358 void HYDROData_Profile::RemovePoints()
360 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
361 if ( !aProfileUZ.IsNull() )
363 aProfileUZ->RemoveSections();
368 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
372 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
373 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
375 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
376 aProfileUZ->AddPoint( 0, aPoint );
382 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
384 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
385 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
388 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints, bool IsConvertFromGlobal )
391 if ( thePoints.Length() < 2 )
394 gp_XY aFirstPoint, aLastPoint;
396 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
397 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
398 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
400 ProfilePoint aPoint = thePoints.Value( i );
401 if( IsConvertFromGlobal )
402 aDoc->Transform( aPoint, true );
404 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
407 aFirstPoint = aPointXY;
409 aLastPoint = aPointXY;
411 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
413 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
414 aProfileUZ->AddPoint( 0, aParPoint );
417 SetLeftPoint( aFirstPoint, false );//already converted to local CS
418 SetRightPoint( aLastPoint, false );
421 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints
422 ( bool IsConvertToGlobal, bool CanUseDefaultLeftRight ) const
424 ProfilePoints aResPoints;
426 gp_XY aFirstPoint, aLastPoint;
427 if ( !GetLeftPoint( aFirstPoint, IsConvertToGlobal, CanUseDefaultLeftRight ) ||
428 !GetRightPoint( aLastPoint, IsConvertToGlobal, CanUseDefaultLeftRight ) )
431 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
432 if ( aParametricPoints.Length() < 2 )
435 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
436 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
438 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
439 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
441 // Add first point as is
442 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
444 // Compute all other points
445 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
447 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
449 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
451 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
453 double aRatio = aParLen / ( aGeoDistance - aParLen );
455 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
456 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
458 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
459 aResPoints.Append( aCompPoint );
462 // Add last point as is
463 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
468 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
470 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
473 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
475 TCollection_AsciiString aRes;
477 Handle(TDataStd_AsciiString) anAsciiStr;
478 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
479 aRes = anAsciiStr->Get();
484 int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
485 const TCollection_AsciiString& theFileName,
486 NCollection_Sequence<int>& theBadProfilesIds )
488 if ( theDoc.IsNull() || theFileName.IsEmpty() )
491 OSD_File aFile( theFileName );
492 if ( !aFile.IsReadable() )
495 aFile.Open( OSD_ReadOnly, OSD_Protection() );
496 if ( !aFile.IsOpen() )
499 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
502 Handle(HYDROData_Profile) aNewProfile;
503 for ( ; !aFile.IsAtEnd(); ++aProfileId )
505 if ( aNewProfile.IsNull() )
506 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
508 bool anIsRead = false;
509 if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) )
511 aCreatedProfiles.Append( aNewProfile );
512 aNewProfile.Nullify();
516 theBadProfilesIds.Append( aProfileId );
520 if ( !aNewProfile.IsNull() )
521 aNewProfile->Remove();
526 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
528 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
530 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
531 aProfile->SetName( aProfileName );
533 aProfile->SetFilePath( theFileName );
535 aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
538 return aCreatedProfiles.Length();
541 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
547 // Try to open the file
548 OSD_File aFile( theFileName );
549 if ( !aFile.IsReadable() )
552 aFile.Open( OSD_ReadOnly, OSD_Protection() );
553 if ( !aFile.IsOpen() )
556 bool aRes = ImportFromFile( aFile, theIsRead );
564 SetFilePath( theFileName );
570 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
576 if ( !theFile.IsOpen() )
581 bool anIsParametric = false;
582 bool anIsGeoref = false;
584 HYDROData_ProfileUZ::PointsList aPointsUZ;
585 ProfilePoints aPointsXYZ;
587 double aPrevVal = -DBL_MAX;
588 while ( !theFile.IsAtEnd() )
590 Standard_Integer aNbRead = 0;
591 TCollection_AsciiString aLine;
592 theFile.ReadLine( aLine, 1024, aNbRead );
594 aLine.LeftAdjust(); aLine.RightAdjust();
595 if ( aLine.IsEmpty() )
597 if ( !anIsParametric && !anIsGeoref )
598 continue; // Definition is not started yet
600 break; // Next profile started
603 // Set flag of read status to true
607 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
608 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
609 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
611 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
612 aValY.IsEmpty() || !aValY.IsRealValue() )
618 if ( !anIsParametric && !anIsGeoref )
620 anIsParametric = aValZ.IsEmpty();
621 anIsGeoref = !aValZ.IsEmpty();
624 double aCoordX = aValX.RealValue();
625 double aCoordY = aValY.RealValue();
627 if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) ||
628 boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) )
631 if ( anIsParametric )
633 if ( aCoordX < aPrevVal )
635 // Move back readed line
636 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
640 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
641 aPointsUZ.Append( aPoint );
647 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
653 double aCoordZ = aValZ.RealValue();
654 if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) )
657 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
658 aPointsXYZ.Append( aPoint );
662 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
663 anIsGeoref && !aPointsXYZ.IsEmpty() );
666 // Update profile points
667 if ( anIsParametric )
669 SetParametricPoints( aPointsUZ );
671 else if ( anIsGeoref )
673 SetProfilePoints( aPointsXYZ, true );
682 void HYDROData_Profile::UpdateLocalCS( double theDx, double theDy )
684 gp_XY aDelta( theDx, theDy );
687 GetLeftPoint( aPnt, false );
689 SetLeftPoint( aPnt, false );
691 GetRightPoint( aPnt, false );
693 SetRightPoint( aPnt, false );
696 HYDROData_Profile::ProfilePoint HYDROData_Profile::GetBottomPoint() const
698 ProfilePoint aBottom;
700 // Get parametric points
701 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
702 if ( aParametricPoints.Length() < 1 ) {
706 // Calculate midvalue for U parameter
707 Standard_Real anUMidValue = aParametricPoints.First().X();
708 Standard_Real anUMinValue = anUMidValue;
709 Standard_Real anUMaxValue = anUMidValue;
711 for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
712 const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
713 Standard_Real anU = aParPoint.X();
715 if ( anU < anUMinValue ) {
717 } else if ( anU > anUMaxValue ) {
722 anUMidValue = ( anUMinValue + anUMaxValue ) / 2;
724 // Find index of the parametric point with minimal Z value
725 int aBottomIndex = 1;
726 HYDROData_IPolyline::Point aParBottom = aParametricPoints.First();
728 for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
729 const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
730 if ( aParPoint.Y() < aParBottom.Y() ) {
732 aParBottom = aParPoint;
733 } else if ( aParPoint.Y() == aParBottom.Y() ) {
734 // Check which point is neares to the U = 0.5
735 if ( fabs( aParPoint.X() - anUMidValue ) < fabs( aParBottom.X() - anUMidValue ) ) {
737 aParBottom = aParPoint;
742 // Find the corresponding profile point
743 ProfilePoints aProfilePoints = GetProfilePoints( false );
744 if ( aBottomIndex >= 1 && aBottomIndex <= aProfilePoints.Length() ) {
745 aBottom = aProfilePoints.Value( aBottomIndex );