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
162 void HYDROData_Profile::Update()
164 HYDROData_Object::Update();
167 Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
168 if ( !aProfile.IsNull() )
170 ProfilePoints aProfilePoints = GetProfilePoints( false );
171 HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
173 aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
178 QColor HYDROData_Profile::DefaultFillingColor()
180 return QColor( Qt::transparent );
183 QColor HYDROData_Profile::DefaultBorderColor()
185 return QColor( Qt::black );
188 QColor HYDROData_Profile::getDefaultFillingColor() const
190 return DefaultFillingColor();
193 QColor HYDROData_Profile::getDefaultBorderColor() const
195 return DefaultBorderColor();
198 bool HYDROData_Profile::IsValid() const
200 gp_XY aFirstPoint, aLastPoint;
201 if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
204 int aNbPoints = NbPoints();
205 return aNbPoints > 1;
208 void HYDROData_Profile::SetLeftPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
210 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
211 if ( aLabel.IsNull() )
214 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
215 gp_XY aLPoint = theGPoint;
216 if( IsConvertFromGlobal )
217 aDoc->Transform( aLPoint, true );
219 Handle(TDataStd_RealArray) anArray;
220 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
221 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
223 anArray->SetValue( 0, aLPoint.X() );
224 anArray->SetValue( 1, aLPoint.Y() );
229 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint, bool IsConvertToGlobal ) const
231 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
232 if ( aLabel.IsNull() )
235 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
236 Handle(TDataStd_RealArray) anArray;
237 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
240 thePoint.SetX( anArray->Value( 0 ) );
241 thePoint.SetY( anArray->Value( 1 ) );
243 if( IsConvertToGlobal )
244 aDoc->Transform( thePoint, false );
249 void HYDROData_Profile::SetRightPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
251 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
253 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
254 gp_XY aLPoint = theGPoint;
255 if( IsConvertFromGlobal )
256 aDoc->Transform( aLPoint, true );
258 Handle(TDataStd_RealArray) anArray;
259 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
260 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
262 anArray->SetValue( 0, aLPoint.X() );
263 anArray->SetValue( 1, aLPoint.Y() );
268 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint, bool IsConvertToGlobal ) const
270 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
271 if ( aLabel.IsNull() )
274 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
275 Handle(TDataStd_RealArray) anArray;
276 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
279 thePoint.SetX( anArray->Value( 0 ) );
280 thePoint.SetY( anArray->Value( 1 ) );
282 if( IsConvertToGlobal )
283 aDoc->Transform( thePoint, false );
288 void HYDROData_Profile::Invalidate()
290 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
291 if ( !aFirstLabel.IsNull() )
292 aFirstLabel.ForgetAllAttributes();
294 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
295 if ( !aLastLabel.IsNull() )
296 aLastLabel.ForgetAllAttributes();
301 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
303 Handle(HYDROData_ProfileUZ) aProfileUZ;
305 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
306 if ( aLabel.IsNull() )
309 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
310 if ( aProfileUZ.IsNull() && theIsCreate )
312 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
313 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
319 int HYDROData_Profile::NbPoints() const
321 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
322 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
325 void HYDROData_Profile::RemovePoints()
327 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
328 if ( !aProfileUZ.IsNull() )
330 aProfileUZ->RemoveSections();
335 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
339 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
340 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
342 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
343 aProfileUZ->AddPoint( 0, aPoint );
349 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
351 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
352 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
355 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints, bool IsConvertFromGlobal )
358 if ( thePoints.Length() < 2 )
361 gp_XY aFirstPoint, aLastPoint;
363 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
364 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
365 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
367 ProfilePoint aPoint = thePoints.Value( i );
368 if( IsConvertFromGlobal )
369 aDoc->Transform( aPoint, true );
371 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
374 aFirstPoint = aPointXY;
376 aLastPoint = aPointXY;
378 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
380 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
381 aProfileUZ->AddPoint( 0, aParPoint );
384 SetLeftPoint( aFirstPoint, false );//already converted to local CS
385 SetRightPoint( aLastPoint, false );
388 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints( bool IsConvertToGlobal ) const
390 ProfilePoints aResPoints;
392 gp_XY aFirstPoint, aLastPoint;
393 if ( !GetLeftPoint( aFirstPoint, IsConvertToGlobal ) ||
394 !GetRightPoint( aLastPoint, IsConvertToGlobal ) )
397 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
398 if ( aParametricPoints.Length() < 2 )
401 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
402 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
404 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
405 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
407 // Add first point as is
408 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
410 // Compute all other points
411 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
413 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
415 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
417 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
419 double aRatio = aParLen / ( aGeoDistance - aParLen );
421 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
422 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
424 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
425 aResPoints.Append( aCompPoint );
428 // Add last point as is
429 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
434 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
436 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
439 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
441 TCollection_AsciiString aRes;
443 Handle(TDataStd_AsciiString) anAsciiStr;
444 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
445 aRes = anAsciiStr->Get();
450 int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
451 const TCollection_AsciiString& theFileName,
452 NCollection_Sequence<int>& theBadProfilesIds )
454 if ( theDoc.IsNull() || theFileName.IsEmpty() )
457 OSD_File aFile( theFileName );
458 if ( !aFile.IsReadable() )
461 aFile.Open( OSD_ReadOnly, OSD_Protection() );
462 if ( !aFile.IsOpen() )
465 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
468 Handle(HYDROData_Profile) aNewProfile;
469 for ( ; !aFile.IsAtEnd(); ++aProfileId )
471 if ( aNewProfile.IsNull() )
472 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
474 bool anIsRead = false;
475 if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) )
477 aCreatedProfiles.Append( aNewProfile );
478 aNewProfile.Nullify();
482 theBadProfilesIds.Append( aProfileId );
486 if ( !aNewProfile.IsNull() )
487 aNewProfile->Remove();
492 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
494 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
496 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
497 aProfile->SetName( aProfileName );
499 aProfile->SetFilePath( theFileName );
501 aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
504 return aCreatedProfiles.Length();
507 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
513 // Try to open the file
514 OSD_File aFile( theFileName );
515 if ( !aFile.IsReadable() )
518 aFile.Open( OSD_ReadOnly, OSD_Protection() );
519 if ( !aFile.IsOpen() )
522 bool aRes = ImportFromFile( aFile, theIsRead );
530 SetFilePath( theFileName );
536 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
542 if ( !theFile.IsOpen() )
547 bool anIsParametric = false;
548 bool anIsGeoref = false;
550 HYDROData_ProfileUZ::PointsList aPointsUZ;
551 ProfilePoints aPointsXYZ;
553 double aPrevVal = -DBL_MAX;
554 while ( !theFile.IsAtEnd() )
556 Standard_Integer aNbRead = 0;
557 TCollection_AsciiString aLine;
558 theFile.ReadLine( aLine, 1024, aNbRead );
560 aLine.LeftAdjust(); aLine.RightAdjust();
561 if ( aLine.IsEmpty() )
563 if ( !anIsParametric && !anIsGeoref )
564 continue; // Definition is not started yet
566 break; // Next profile started
569 // Set flag of read status to true
573 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
574 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
575 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
577 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
578 aValY.IsEmpty() || !aValY.IsRealValue() )
584 if ( !anIsParametric && !anIsGeoref )
586 anIsParametric = aValZ.IsEmpty();
587 anIsGeoref = !aValZ.IsEmpty();
590 double aCoordX = aValX.RealValue();
591 double aCoordY = aValY.RealValue();
593 if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) ||
594 boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) )
597 if ( anIsParametric )
599 if ( aCoordX < aPrevVal )
601 // Move back readed line
602 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
606 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
607 aPointsUZ.Append( aPoint );
613 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
619 double aCoordZ = aValZ.RealValue();
620 if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) )
623 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
624 aPointsXYZ.Append( aPoint );
628 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
629 anIsGeoref && !aPointsXYZ.IsEmpty() );
632 // Update profile points
633 if ( anIsParametric )
635 SetParametricPoints( aPointsUZ );
637 else if ( anIsGeoref )
639 SetProfilePoints( aPointsXYZ, true );
648 void HYDROData_Profile::UpdateLocalCS( double theDx, double theDy )
650 gp_XY aDelta( theDx, theDy );
653 GetLeftPoint( aPnt, false );
655 SetLeftPoint( aPnt, false );
657 GetRightPoint( aPnt, false );
659 SetRightPoint( aPnt, false );
662 HYDROData_Profile::ProfilePoint HYDROData_Profile::GetBottomPoint() const
664 ProfilePoint aBottom;
666 // Get parametric points
667 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
668 if ( aParametricPoints.Length() < 1 ) {
672 // Calculate midvalue for U parameter
673 Standard_Real anUMidValue = aParametricPoints.First().X();
674 Standard_Real anUMinValue = anUMidValue;
675 Standard_Real anUMaxValue = anUMidValue;
677 for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
678 const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
679 Standard_Real anU = aParPoint.X();
681 if ( anU < anUMinValue ) {
683 } else if ( anU > anUMaxValue ) {
688 anUMidValue = ( anUMinValue + anUMaxValue ) / 2;
690 // Find index of the parametric point with minimal Z value
691 int aBottomIndex = 1;
692 HYDROData_IPolyline::Point aParBottom = aParametricPoints.First();
694 for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
695 const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
696 if ( aParPoint.Y() < aParBottom.Y() ) {
698 aParBottom = aParPoint;
699 } else if ( aParPoint.Y() == aParBottom.Y() ) {
700 // Check which point is neares to the U = 0.5
701 if ( fabs( aParPoint.X() - anUMidValue ) < fabs( aParBottom.X() - anUMidValue ) ) {
703 aParBottom = aParPoint;
708 // Find the corresponding profile point
709 ProfilePoints aProfilePoints = GetProfilePoints( false );
710 if ( aBottomIndex >= 1 && aBottomIndex <= aProfilePoints.Length() ) {
711 aBottom = aProfilePoints.Value( aBottomIndex );