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_Polyline3D.h"
21 #include "HYDROData_IAltitudeObject.h"
22 #include "HYDROData_Document.h"
23 #include "HYDROData_PolylineXY.h"
24 #include "HYDROData_Profile.h"
25 #include "HYDROData_ProfileUZ.h"
26 #include "HYDROData_ShapesTool.h"
27 #include "HYDROData_Tool.h"
29 #include <BRep_Tool.hxx>
31 #include <Geom_BSplineCurve.hxx>
32 #include <GeomAdaptor_Curve.hxx>
33 #include <GCPnts_QuasiUniformAbscissa.hxx>
34 #include <GCPnts_AbscissaPoint.hxx>
36 #include <gp_Pnt2d.hxx>
40 #include <TColStd_Array1OfReal.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopoDS_Wire.hxx>
49 #include <TopExp_Explorer.hxx>
51 #include <TopTools_SequenceOfShape.hxx>
54 #include <QStringList>
56 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline3D,HYDROData_Object)
57 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
60 HYDROData_Polyline3D::HYDROData_Polyline3D()
61 : HYDROData_Object( Geom_3d )
65 HYDROData_Polyline3D::~HYDROData_Polyline3D()
69 QStringList HYDROData_Polyline3D::DumpToPython( const QString& thePyScriptPath,
70 MapOfTreatedObjects& theTreatedObjects ) const
72 QStringList aResList = dumpObjectCreation( theTreatedObjects );
73 QString aPolylineName = GetObjPyName();
75 Handle(HYDROData_PolylineXY) aRefPolyline = GetPolylineXY();
76 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefPolyline, "SetPolylineXY" );
78 Handle(HYDROData_ProfileUZ) aRefProfileUZ = GetProfileUZ();
79 if ( !aRefProfileUZ.IsNull() )
81 Handle(HYDROData_Profile) aProfile =
82 Handle(HYDROData_Profile)::DownCast( aRefProfileUZ->GetFatherObject() );
83 if ( checkObjectPythonDefinition( thePyScriptPath, theTreatedObjects, aResList, aProfile ) )
85 QString aProfileName = aProfile->GetObjPyName();
86 if ( !aProfileName.isEmpty() )
88 aResList << QString( "%1.SetProfileUZ( %2.GetProfileUZ() )" )
89 .arg( aPolylineName ).arg( aProfileName );
95 Handle(HYDROData_IAltitudeObject) aRefBathymetry = GetAltitudeObject();
96 if ( !aRefBathymetry.IsNull() )
98 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
99 if ( !aChildProfileUZ.IsNull() )
101 Handle(HYDROData_Profile) aProfile =
102 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
103 if ( checkObjectPythonDefinition( thePyScriptPath, theTreatedObjects, aResList, aProfile ) )
105 QString aProfileName = aProfile->GetObjPyName();
106 if ( !aProfileName.isEmpty() )
108 aResList << QString( "%1.SetChildProfileUZ( %2.GetProfileUZ() )" )
109 .arg( aPolylineName ).arg( aProfileName );
114 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefBathymetry, "SetAltitudeObject" );
118 aResList << QString( "" );
119 aResList << QString( "%1.Update()" ).arg( aPolylineName );
120 aResList << QString( "" );
125 HYDROData_SequenceOfObjects HYDROData_Polyline3D::GetAllReferenceObjects() const
127 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
129 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
130 if ( !aPolylineXY.IsNull() )
131 aResSeq.Append( aPolylineXY );
133 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
134 if ( !aProfileUZ.IsNull() )
135 aResSeq.Append( aProfileUZ );
137 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
138 if ( !aChildProfileUZ.IsNull() )
139 aResSeq.Append( aChildProfileUZ );
144 void HYDROData_Polyline3D::Update()
146 HYDROData_Object::Update();
148 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
149 if ( aPolylineXY.IsNull() )
152 bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
153 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
154 HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
155 if ( aPolylinePoints.IsEmpty() )
158 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
160 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
161 if ( !anAltitude.IsNull() )
162 aProfileUZ = GetChildProfileUZ();
164 if ( aProfileUZ.IsNull() )
167 HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
168 if ( aProfilePoints.IsEmpty() )
171 const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
172 const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
174 const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
175 const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
177 double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
178 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
180 NCollection_Sequence<Polyline3DPoint> aResPoints;
182 // Add first point as is
183 aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
185 for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
187 const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
189 double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
191 double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
192 double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
194 Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
195 aResPoints.Append( aCompPoint );
198 // Add last point as is
199 aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
201 TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
202 SetTopShape( aResWire );
203 SetShape3D( aResWire );
206 QColor HYDROData_Polyline3D::DefaultFillingColor() const
208 return QColor( Qt::transparent );
211 QColor HYDROData_Polyline3D::DefaultBorderColor() const
213 return QColor( Qt::red );
216 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
217 const bool theIsUpdateProfile )
219 if ( thePolyline.IsNull() )
222 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
223 if ( IsEqual( aPrevPolyline, thePolyline ) )
226 SetReferenceObject( thePolyline, DataTag_PolylineXY );
228 // Update the child profile object
229 if ( theIsUpdateProfile )
230 updateChildProfilePoints();
232 // Indicate model of the need to update the polyline presentation
238 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
240 return Handle(HYDROData_PolylineXY)::DownCast(
241 GetReferenceObject( DataTag_PolylineXY ) );
244 void HYDROData_Polyline3D::RemovePolylineXY()
246 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
247 if ( aPrevPolyline.IsNull() )
250 ClearReferenceObjects( DataTag_PolylineXY );
252 // Indicate model of the need to update the polyline presentation
256 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
258 if ( theProfile.IsNull() )
261 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
262 if ( IsEqual( aPrevProfile, theProfile ) )
265 SetReferenceObject( theProfile, DataTag_ProfileUZ );
267 // Remove the bathymetry, because one altitude object can be presented at time
268 RemoveAltitudeObject();
270 // Indicate model of the need to update the polyline presentation
276 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
278 return Handle(HYDROData_ProfileUZ)::DownCast(
279 GetReferenceObject( DataTag_ProfileUZ ) );
282 void HYDROData_Polyline3D::RemoveProfileUZ()
284 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
285 if ( aPrevProfile.IsNull() )
288 ClearReferenceObjects( DataTag_ProfileUZ );
290 // Indicate model of the need to update the polyline presentation
294 bool HYDROData_Polyline3D::SetAltitudeObject(
295 const Handle(HYDROData_IAltitudeObject)& theAltitude )
297 Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
299 if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
302 if ( IsEqual( aPrevAltitude, theAltitude ) )
305 // Remove the u,z profile, because one altitude object can be presented at time
308 // Create the child profile object
309 updateChildProfilePoints();
315 void HYDROData_Polyline3D::RemoveAltitudeObject()
317 HYDROData_Object::RemoveAltitudeObject();
319 // Remove the child profile object
320 removeChildProfileUZ();
323 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
325 Handle(HYDROData_ProfileUZ) aProfileUZ =
326 Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
327 if ( !theIsCreate || !aProfileUZ.IsNull() )
330 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
331 if ( aDocument.IsNull() )
334 Handle(HYDROData_Profile) aProfile =
335 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
337 QString aProfilePref = GetName() + "_Profile";
338 QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
340 aProfile->SetName( aProfileName );
342 aProfileUZ = aProfile->GetProfileUZ();
344 HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
345 me->SetChildProfileUZ( aProfileUZ );
350 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
352 SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
355 HYDROData_IPolyline::PointsList generateProfileUZPoints(
356 const Handle(HYDROData_PolylineXY)& thePolyline,
357 const Handle(HYDROData_IAltitudeObject)& theAltitude )
359 HYDROData_IPolyline::PointsList aPointsList;
360 if ( thePolyline.IsNull() || theAltitude.IsNull() )
363 bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
364 HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
365 HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
366 if ( aPolylinePoints.IsEmpty() )
369 for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
371 const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
373 double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
374 double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
375 if( aPointDepth == theAltitude->GetInvalidAltitude() )
378 HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
379 aPointsList.Append( anAltitudePoint );
385 void HYDROData_Polyline3D::updateChildProfilePoints()
387 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
388 if ( anAltitude.IsNull() )
391 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
392 if ( aChildProfileUZ.IsNull() )
395 Handle(HYDROData_Profile) aProfile =
396 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
397 if ( aProfile.IsNull() )
400 HYDROData_IPolyline::PointsList aProfilePoints =
401 generateProfileUZPoints( GetPolylineXY(), anAltitude );
402 aProfile->SetParametricPoints( aProfilePoints );
407 void HYDROData_Polyline3D::removeChildProfileUZ()
409 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
410 if ( aChildProfileUZ.IsNull() )
413 ClearReferenceObjects( DataTag_ChildProfileUZ );
415 /* Uncomment if removing is requested
416 Handle(HYDROData_Profile) aProfile =
417 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
418 if ( !aProfile.IsNull() )
423 HYDROData_Polyline3D::Polyline3DPoints HYDROData_Polyline3D::GetPoints( double theEqDistance ) const
425 Polyline3DPoints aPoints;
427 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
428 TopoDS_Wire aWire = TopoDS::Wire( GetShape3D() );
429 if ( aPolylineXY.IsNull() || aWire.IsNull() ) {
433 // Explode polyline on edges
434 TopTools_SequenceOfShape anEdges;
435 HYDROData_ShapesTool::ExploreShapeToShapes( aWire, TopAbs_EDGE, anEdges );
438 if ( !anEdges.IsEmpty() ) {
439 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
441 if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
442 // Get points from wire
443 /* Seems that intermediate vertices are duplicated
444 TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
445 for ( ; anExp.More(); anExp.Next() ) {
446 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
447 if ( !aVertex.IsNull() ) {
448 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
449 aPoints.Append( aPnt.XYZ() );
453 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
455 for ( ; anExp.More(); anExp.Next() ) {
456 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
457 if ( !anEdge.IsNull() ) {
458 TopoDS_Vertex aV1, aV2;
459 TopExp::Vertices( anEdge, aV1, aV2 );
461 gp_Pnt aPnt1 = BRep_Tool::Pnt( aV1 );
462 aPoints.Append( aPnt1.XYZ() );
465 gp_Pnt aPnt2 = BRep_Tool::Pnt( aV2 );
466 aPoints.Append( aPnt2.XYZ() );
472 // Get points from spline curve
473 Standard_Real aStart, anEnd;
474 TopoDS_Edge anEdge = TopoDS::Edge( anEdges.First() );
475 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aStart, anEnd );
477 if( theEqDistance > 0 )
479 GeomAdaptor_Curve anAdaptorCurve( aCurve );
480 double aLength = GCPnts_AbscissaPoint::Length( anAdaptorCurve );
481 int aNbPoints = ceil( aLength / theEqDistance );
482 GCPnts_QuasiUniformAbscissa aDist( anAdaptorCurve, aNbPoints );
485 aNbPoints = aDist.NbPoints();
486 for( int i=1; i<=aNbPoints; i++ )
488 double p = aDist.Parameter( i );
490 aCurve->D0( p, aPnt );
491 aPoints.Append( aPnt.XYZ() );
497 Handle(Geom_BSplineCurve) aGeomSpline = Handle(Geom_BSplineCurve)::DownCast( aCurve );
499 if ( !aGeomSpline.IsNull() ) {
500 int aNbKnots = aGeomSpline->NbKnots();
502 TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
503 aGeomSpline->Knots( aSplineKnots );
505 for ( int i = 1; i <= aNbKnots; ++i ) {
506 const Standard_Real& aKnot = aSplineKnots.Value( i );
508 aGeomSpline->D0( aKnot, aPnt );
509 aPoints.Append( aPnt.XYZ() );