1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROData_Profile.h"
25 #include "HYDROData_Document.h"
26 #include "HYDROData_Iterator.h"
27 #include "HYDROData_Tool.h"
28 #include "HYDROData_PolylineXY.h"
30 #include <boost/math/special_functions/fpclassify.hpp>
32 #include <BRepBuilderAPI_MakeEdge.hxx>
33 #include <BRepBuilderAPI_MakeWire.hxx>
34 #include <BRepBuilderAPI_MakePolygon.hxx>
38 #include <gp_Pnt2d.hxx>
40 #include <TDataStd_AsciiString.hxx>
41 #include <TDataStd_RealArray.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Wire.hxx>
46 #include <OSD_File.hxx>
47 #include <OSD_Protection.hxx>
50 #include <QStringList>
52 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
53 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
55 HYDROData_Profile::HYDROData_Profile()
60 HYDROData_Profile::~HYDROData_Profile()
64 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
66 QStringList aResList = dumpObjectCreation( theTreatedObjects );
67 QString aProfileName = GetObjPyName();
69 //TCollection_AsciiString aFilePath = GetFilePath();
70 //if ( !aFilePath.IsEmpty() )
72 // aResList << QString( "%1.ImportFromFile( \"%2\" );" )
73 // .arg( aName ).arg( aFilePath.ToCString() );
76 bool anIsValidProfile = IsValid();
78 QStringList aPntsDefinition;
79 QString aPntsListName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "profile_points" );
81 QString aGap = QString().fill( ' ', aPntsListName.length() + 5 );
82 if ( anIsValidProfile )
84 HYDROData_Profile::ProfilePoints aPointsList = GetProfilePoints( true );
85 for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
87 const ProfilePoint& aPoint = aPointsList.Value( k );
88 aPntsDefinition << QString( aGap + "gp_XYZ( %1, %2, %3 )%4" )
89 .arg( aPoint.X() ).arg( aPoint.Y() ).arg( aPoint.Z() )
90 .arg( ( k < aNbPoints ? "," : "" ) );
95 HYDROData_IPolyline::PointsList aPointsList = GetParametricPoints();
96 for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
98 const HYDROData_IPolyline::Point& aPoint = aPointsList.Value( k );
99 aPntsDefinition << QString( aGap + "gp_XY( %1, %2 )%3" )
100 .arg( aPoint.X() ).arg( aPoint.Y() )
101 .arg( ( k < aNbPoints ? "," : "" ) );
105 if ( !aPntsDefinition.isEmpty() )
107 QString& aFirstStr = aPntsDefinition.first();
108 aFirstStr = aFirstStr.trimmed();
109 aFirstStr.prepend( QString( "%1 = [ " ).arg( aPntsListName ) );
111 aPntsDefinition.last().append( " ];" );
113 aResList << aPntsDefinition;
115 aResList << QString( "%1.%3( %2 );" )
116 .arg( aProfileName ).arg( aPntsListName )
117 .arg( anIsValidProfile ? "SetProfilePoints" : "SetParametricPoints" );
119 aResList << QString( "" );
122 // Set a polyline type if it is not default
123 Handle(HYDROData_ProfileUZ) aPrf = GetProfileUZ( false );
124 if ( !aPrf.IsNull() )
126 HYDROData_IPolyline::SectionType aSecType = aPrf->GetSectionType( 0 );
127 if ( aSecType != HYDROData_IPolyline::SECTION_POLYLINE )
129 aResList << QString( "%1.GetProfileUZ().SetSectionType( 0, %2 );" )
130 .arg( aProfileName ).arg( "HYDROData_IPolyline.SECTION_SPLINE" );
131 aResList << QString( "" );
135 aResList << QString( "%1.Update();" ).arg( aProfileName );
136 aResList << QString( "" );
141 TopoDS_Shape HYDROData_Profile::GetTopShape() const
145 gp_XY aFirstPoint, aLastPoint;
146 if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
149 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
150 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
152 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
153 TopoDS_Edge anEdge = aMakeEdge;
155 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
161 TopoDS_Shape HYDROData_Profile::GetShape3D() const
166 void HYDROData_Profile::Update()
168 HYDROData_Object::Update();
171 Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
172 if ( !aProfile.IsNull() )
174 ProfilePoints aProfilePoints = GetProfilePoints( false );
175 HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
177 aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
182 QColor HYDROData_Profile::DefaultFillingColor()
184 return QColor( Qt::transparent );
187 QColor HYDROData_Profile::DefaultBorderColor()
189 return QColor( Qt::black );
192 QColor HYDROData_Profile::getDefaultFillingColor() const
194 return DefaultFillingColor();
197 QColor HYDROData_Profile::getDefaultBorderColor() const
199 return DefaultBorderColor();
202 bool HYDROData_Profile::IsValid() const
204 gp_XY aFirstPoint, aLastPoint;
205 if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
208 int aNbPoints = NbPoints();
209 return aNbPoints > 1;
212 void HYDROData_Profile::SetLeftPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
214 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
215 if ( aLabel.IsNull() )
218 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
219 gp_XY aLPoint = theGPoint;
220 if( IsConvertFromGlobal )
221 aDoc->Transform( aLPoint, true );
223 Handle(TDataStd_RealArray) anArray;
224 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
225 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
227 anArray->SetValue( 0, aLPoint.X() );
228 anArray->SetValue( 1, aLPoint.Y() );
233 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint, bool IsConvertToGlobal ) const
235 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
236 if ( aLabel.IsNull() )
239 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
240 Handle(TDataStd_RealArray) anArray;
241 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
244 thePoint.SetX( anArray->Value( 0 ) );
245 thePoint.SetY( anArray->Value( 1 ) );
247 if( IsConvertToGlobal )
248 aDoc->Transform( thePoint, false );
253 void HYDROData_Profile::SetRightPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
255 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
257 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
258 gp_XY aLPoint = theGPoint;
259 if( IsConvertFromGlobal )
260 aDoc->Transform( aLPoint, true );
262 Handle(TDataStd_RealArray) anArray;
263 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
264 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
266 anArray->SetValue( 0, aLPoint.X() );
267 anArray->SetValue( 1, aLPoint.Y() );
272 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint, bool IsConvertToGlobal ) const
274 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
275 if ( aLabel.IsNull() )
278 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
279 Handle(TDataStd_RealArray) anArray;
280 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
283 thePoint.SetX( anArray->Value( 0 ) );
284 thePoint.SetY( anArray->Value( 1 ) );
286 if( IsConvertToGlobal )
287 aDoc->Transform( thePoint, false );
292 void HYDROData_Profile::Invalidate()
294 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
295 if ( !aFirstLabel.IsNull() )
296 aFirstLabel.ForgetAllAttributes();
298 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
299 if ( !aLastLabel.IsNull() )
300 aLastLabel.ForgetAllAttributes();
305 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
307 Handle(HYDROData_ProfileUZ) aProfileUZ;
309 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
310 if ( aLabel.IsNull() )
313 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
314 if ( aProfileUZ.IsNull() && theIsCreate )
316 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
317 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
323 int HYDROData_Profile::NbPoints() const
325 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
326 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
329 void HYDROData_Profile::RemovePoints()
331 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
332 if ( !aProfileUZ.IsNull() )
334 aProfileUZ->RemoveSections();
339 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
343 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
344 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
346 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
347 aProfileUZ->AddPoint( 0, aPoint );
353 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
355 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
356 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
359 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints, bool IsConvertFromGlobal )
362 if ( thePoints.Length() < 2 )
365 gp_XY aFirstPoint, aLastPoint;
367 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
368 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
369 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
371 ProfilePoint aPoint = thePoints.Value( i );
372 if( IsConvertFromGlobal )
373 aDoc->Transform( aPoint, true );
375 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
378 aFirstPoint = aPointXY;
380 aLastPoint = aPointXY;
382 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
384 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
385 aProfileUZ->AddPoint( 0, aParPoint );
388 SetLeftPoint( aFirstPoint, false );//already converted to local CS
389 SetRightPoint( aLastPoint, false );
392 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints( bool IsConvertToGlobal ) const
394 ProfilePoints aResPoints;
396 gp_XY aFirstPoint, aLastPoint;
397 if ( !GetLeftPoint( aFirstPoint, IsConvertToGlobal ) ||
398 !GetRightPoint( aLastPoint, IsConvertToGlobal ) )
401 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
402 if ( aParametricPoints.Length() < 2 )
405 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
406 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
408 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
409 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
411 // Add first point as is
412 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
414 // Compute all other points
415 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
417 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
419 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
421 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
423 double aRatio = aParLen / ( aGeoDistance - aParLen );
425 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
426 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
428 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
429 aResPoints.Append( aCompPoint );
432 // Add last point as is
433 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
438 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
440 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
443 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
445 TCollection_AsciiString aRes;
447 Handle(TDataStd_AsciiString) anAsciiStr;
448 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
449 aRes = anAsciiStr->Get();
454 int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
455 const TCollection_AsciiString& theFileName,
456 NCollection_Sequence<int>& theBadProfilesIds )
458 if ( theDoc.IsNull() || theFileName.IsEmpty() )
461 OSD_File aFile( theFileName );
462 if ( !aFile.IsReadable() )
465 aFile.Open( OSD_ReadOnly, OSD_Protection() );
466 if ( !aFile.IsOpen() )
469 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
472 Handle(HYDROData_Profile) aNewProfile;
473 for ( ; !aFile.IsAtEnd(); ++aProfileId )
475 if ( aNewProfile.IsNull() )
476 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
478 bool anIsRead = false;
479 if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) )
481 aCreatedProfiles.Append( aNewProfile );
482 aNewProfile.Nullify();
486 theBadProfilesIds.Append( aProfileId );
490 if ( !aNewProfile.IsNull() )
491 aNewProfile->Remove();
496 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
498 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
500 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
501 aProfile->SetName( aProfileName );
503 aProfile->SetFilePath( theFileName );
505 aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
508 return aCreatedProfiles.Length();
511 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
517 // Try to open the file
518 OSD_File aFile( theFileName );
519 if ( !aFile.IsReadable() )
522 aFile.Open( OSD_ReadOnly, OSD_Protection() );
523 if ( !aFile.IsOpen() )
526 bool aRes = ImportFromFile( aFile, theIsRead );
534 SetFilePath( theFileName );
540 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
546 if ( !theFile.IsOpen() )
551 bool anIsParametric = false;
552 bool anIsGeoref = false;
554 HYDROData_ProfileUZ::PointsList aPointsUZ;
555 ProfilePoints aPointsXYZ;
557 double aPrevVal = -DBL_MAX;
558 while ( !theFile.IsAtEnd() )
560 Standard_Integer aNbRead = 0;
561 TCollection_AsciiString aLine;
562 theFile.ReadLine( aLine, 1024, aNbRead );
564 aLine.LeftAdjust(); aLine.RightAdjust();
565 if ( aLine.IsEmpty() )
567 if ( !anIsParametric && !anIsGeoref )
568 continue; // Definition is not started yet
570 break; // Next profile started
573 // Set flag of read status to true
577 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
578 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
579 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
581 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
582 aValY.IsEmpty() || !aValY.IsRealValue() )
588 if ( !anIsParametric && !anIsGeoref )
590 anIsParametric = aValZ.IsEmpty();
591 anIsGeoref = !aValZ.IsEmpty();
594 double aCoordX = aValX.RealValue();
595 double aCoordY = aValY.RealValue();
597 if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) ||
598 boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) )
601 if ( anIsParametric )
603 if ( aCoordX < aPrevVal )
605 // Move back readed line
606 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
610 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
611 aPointsUZ.Append( aPoint );
617 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
623 double aCoordZ = aValZ.RealValue();
624 if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) )
627 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
628 aPointsXYZ.Append( aPoint );
632 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
633 anIsGeoref && !aPointsXYZ.IsEmpty() );
636 // Update profile points
637 if ( anIsParametric )
639 SetParametricPoints( aPointsUZ );
641 else if ( anIsGeoref )
643 SetProfilePoints( aPointsXYZ, true );
652 void HYDROData_Profile::UpdateLocalCS( double theDx, double theDy )
654 gp_XY aDelta( theDx, theDy );
657 GetLeftPoint( aPnt, false );
659 SetLeftPoint( aPnt, false );
661 GetRightPoint( aPnt, false );
663 SetRightPoint( aPnt, false );
666 HYDROData_Profile::ProfilePoint HYDROData_Profile::GetBottomPoint() const
668 ProfilePoint aBottom;
670 // Get parametric points
671 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
672 if ( aParametricPoints.Length() < 1 ) {
676 // Calculate midvalue for U parameter
677 Standard_Real anUMidValue = aParametricPoints.First().X();
678 Standard_Real anUMinValue = anUMidValue;
679 Standard_Real anUMaxValue = anUMidValue;
681 for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
682 const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
683 Standard_Real anU = aParPoint.X();
685 if ( anU < anUMinValue ) {
687 } else if ( anU > anUMaxValue ) {
692 anUMidValue = ( anUMinValue + anUMaxValue ) / 2;
694 // Find index of the parametric point with minimal Z value
695 int aBottomIndex = 1;
696 HYDROData_IPolyline::Point aParBottom = aParametricPoints.First();
698 for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
699 const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
700 if ( aParPoint.Y() < aParBottom.Y() ) {
702 aParBottom = aParPoint;
703 } else if ( aParPoint.Y() == aParBottom.Y() ) {
704 // Check which point is neares to the U = 0.5
705 if ( fabs( aParPoint.X() - anUMidValue ) < fabs( aParBottom.X() - anUMidValue ) ) {
707 aParBottom = aParPoint;
712 // Find the corresponding profile point
713 ProfilePoints aProfilePoints = GetProfilePoints( false );
714 if ( aBottomIndex >= 1 && aBottomIndex <= aProfilePoints.Length() ) {
715 aBottom = aProfilePoints.Value( aBottomIndex );