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 "HYDRO_trace.hxx"
57 #include <QStringList>
59 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
62 HYDROData_Polyline3D::HYDROData_Polyline3D()
63 : HYDROData_Object( Geom_3d )
67 HYDROData_Polyline3D::~HYDROData_Polyline3D()
71 QStringList HYDROData_Polyline3D::DumpToPython( const QString& thePyScriptPath,
72 MapOfTreatedObjects& theTreatedObjects ) const
74 QStringList aResList = dumpObjectCreation( theTreatedObjects );
75 QString aPolylineName = GetObjPyName();
77 Handle(HYDROData_PolylineXY) aRefPolyline = GetPolylineXY();
78 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefPolyline, "SetPolylineXY" );
80 Handle(HYDROData_ProfileUZ) aRefProfileUZ = GetProfileUZ();
81 if ( !aRefProfileUZ.IsNull() )
83 Handle(HYDROData_Profile) aProfile =
84 Handle(HYDROData_Profile)::DownCast( aRefProfileUZ->GetFatherObject() );
85 if ( checkObjectPythonDefinition( thePyScriptPath, theTreatedObjects, aResList, aProfile ) )
87 QString aProfileName = aProfile->GetObjPyName();
88 if ( !aProfileName.isEmpty() )
90 aResList << QString( "%1.SetProfileUZ( %2.GetProfileUZ() )" )
91 .arg( aPolylineName ).arg( aProfileName );
97 Handle(HYDROData_IAltitudeObject) aRefBathymetry = GetAltitudeObject();
98 if ( !aRefBathymetry.IsNull() )
100 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
101 if ( !aChildProfileUZ.IsNull() )
103 Handle(HYDROData_Profile) aProfile =
104 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
105 if ( checkObjectPythonDefinition( thePyScriptPath, theTreatedObjects, aResList, aProfile ) )
107 QString aProfileName = aProfile->GetObjPyName();
108 if ( !aProfileName.isEmpty() )
110 aResList << QString( "%1.SetChildProfileUZ( %2.GetProfileUZ() )" )
111 .arg( aPolylineName ).arg( aProfileName );
116 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefBathymetry, "SetAltitudeObject" );
120 aResList << QString( "" );
121 aResList << QString( "%1.Update()" ).arg( aPolylineName );
122 aResList << QString( "" );
127 HYDROData_SequenceOfObjects HYDROData_Polyline3D::GetAllReferenceObjects() const
129 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
131 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
132 if ( !aPolylineXY.IsNull() )
133 aResSeq.Append( aPolylineXY );
135 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
136 if ( !aProfileUZ.IsNull() )
137 aResSeq.Append( aProfileUZ );
139 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
140 if ( !aChildProfileUZ.IsNull() )
141 aResSeq.Append( aChildProfileUZ );
146 void HYDROData_Polyline3D::Update()
148 HYDROData_Object::Update();
150 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
151 if ( aPolylineXY.IsNull() )
154 bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
155 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
156 HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
157 if ( aPolylinePoints.IsEmpty() )
160 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
162 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
163 if ( !anAltitude.IsNull() )
164 aProfileUZ = GetChildProfileUZ();
166 if ( aProfileUZ.IsNull() )
169 HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
170 if ( aProfilePoints.IsEmpty() )
173 const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
174 const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
176 const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
177 const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
179 double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
180 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
182 NCollection_Sequence<Polyline3DPoint> aResPoints;
184 // Add first point as is
185 aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
187 for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
189 const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
191 double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
193 double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
194 double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
196 Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
197 aResPoints.Append( aCompPoint );
200 // Add last point as is
201 aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
203 TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
204 SetTopShape( aResWire );
205 SetShape3D( aResWire );
208 QColor HYDROData_Polyline3D::DefaultFillingColor() const
210 return QColor( Qt::transparent );
213 QColor HYDROData_Polyline3D::DefaultBorderColor() const
215 return QColor( Qt::red );
218 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
219 const bool theIsUpdateProfile )
221 if ( thePolyline.IsNull() )
224 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
225 if ( IsEqual( aPrevPolyline, thePolyline ) )
228 SetReferenceObject( thePolyline, DataTag_PolylineXY );
230 // Update the child profile object
231 if ( theIsUpdateProfile )
232 updateChildProfilePoints();
234 // Indicate model of the need to update the polyline presentation
240 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
242 return Handle(HYDROData_PolylineXY)::DownCast(
243 GetReferenceObject( DataTag_PolylineXY ) );
246 void HYDROData_Polyline3D::RemovePolylineXY()
248 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
249 if ( aPrevPolyline.IsNull() )
252 ClearReferenceObjects( DataTag_PolylineXY );
254 // Indicate model of the need to update the polyline presentation
258 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
260 if ( theProfile.IsNull() )
263 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
264 if ( IsEqual( aPrevProfile, theProfile ) )
267 SetReferenceObject( theProfile, DataTag_ProfileUZ );
269 // Remove the bathymetry, because one altitude object can be presented at time
270 RemoveAltitudeObject();
272 // Indicate model of the need to update the polyline presentation
278 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
280 return Handle(HYDROData_ProfileUZ)::DownCast(
281 GetReferenceObject( DataTag_ProfileUZ ) );
284 void HYDROData_Polyline3D::RemoveProfileUZ()
286 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
287 if ( aPrevProfile.IsNull() )
290 ClearReferenceObjects( DataTag_ProfileUZ );
292 // Indicate model of the need to update the polyline presentation
296 bool HYDROData_Polyline3D::SetAltitudeObject(
297 const Handle(HYDROData_IAltitudeObject)& theAltitude )
299 Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
301 if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
304 if ( IsEqual( aPrevAltitude, theAltitude ) )
307 // Remove the u,z profile, because one altitude object can be presented at time
310 // Create the child profile object
311 updateChildProfilePoints();
317 void HYDROData_Polyline3D::RemoveAltitudeObject()
319 HYDROData_Object::RemoveAltitudeObject();
321 // Remove the child profile object
322 removeChildProfileUZ();
325 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
327 Handle(HYDROData_ProfileUZ) aProfileUZ =
328 Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
329 if ( !theIsCreate || !aProfileUZ.IsNull() )
332 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
333 if ( aDocument.IsNull() )
336 Handle(HYDROData_Profile) aProfile =
337 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
339 QString aProfilePref = GetName() + "_Profile";
340 QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
342 aProfile->SetName( aProfileName );
344 aProfileUZ = aProfile->GetProfileUZ();
346 HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
347 me->SetChildProfileUZ( aProfileUZ );
352 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
354 SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
357 HYDROData_IPolyline::PointsList generateProfileUZPoints(
358 const Handle(HYDROData_PolylineXY)& thePolyline,
359 const Handle(HYDROData_IAltitudeObject)& theAltitude )
361 HYDROData_IPolyline::PointsList aPointsList;
362 if ( thePolyline.IsNull() || theAltitude.IsNull() )
365 bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
366 HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
367 HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
368 if ( aPolylinePoints.IsEmpty() )
371 for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
373 const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
375 double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
376 double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
377 if( aPointDepth == theAltitude->GetInvalidAltitude() )
380 HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
381 aPointsList.Append( anAltitudePoint );
387 void HYDROData_Polyline3D::updateChildProfilePoints()
389 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
390 if ( anAltitude.IsNull() )
393 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
394 if ( aChildProfileUZ.IsNull() )
397 Handle(HYDROData_Profile) aProfile =
398 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
399 if ( aProfile.IsNull() )
402 HYDROData_IPolyline::PointsList aProfilePoints =
403 generateProfileUZPoints( GetPolylineXY(), anAltitude );
404 aProfile->SetParametricPoints( aProfilePoints );
409 void HYDROData_Polyline3D::removeChildProfileUZ()
411 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
412 if ( aChildProfileUZ.IsNull() )
415 ClearReferenceObjects( DataTag_ChildProfileUZ );
417 /* Uncomment if removing is requested
418 Handle(HYDROData_Profile) aProfile =
419 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
420 if ( !aProfile.IsNull() )
425 HYDROData_Polyline3D::Polyline3DPoints HYDROData_Polyline3D::GetPoints( double theEqDistance ) const
427 Polyline3DPoints aPoints;
429 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
430 TopoDS_Wire aWire = TopoDS::Wire( GetShape3D() );
431 if ( aPolylineXY.IsNull() || aWire.IsNull() ) {
435 // Explode polyline on edges
436 TopTools_SequenceOfShape anEdges;
437 HYDROData_ShapesTool::ExploreShapeToShapes( aWire, TopAbs_EDGE, anEdges );
440 if ( !anEdges.IsEmpty() ) {
441 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
443 if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
444 // Get points from wire
445 /* Seems that intermediate vertices are duplicated
446 TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
447 for ( ; anExp.More(); anExp.Next() ) {
448 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
449 if ( !aVertex.IsNull() ) {
450 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
451 aPoints.Append( aPnt.XYZ() );
455 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
457 for ( ; anExp.More(); anExp.Next() ) {
458 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
459 if ( !anEdge.IsNull() ) {
460 TopoDS_Vertex aV1, aV2;
461 TopExp::Vertices( anEdge, aV1, aV2 );
463 gp_Pnt aPnt1 = BRep_Tool::Pnt( aV1 );
464 aPoints.Append( aPnt1.XYZ() );
467 gp_Pnt aPnt2 = BRep_Tool::Pnt( aV2 );
468 aPoints.Append( aPnt2.XYZ() );
474 // Get points from spline curve
475 Standard_Real aStart, anEnd;
476 TopoDS_Edge anEdge = TopoDS::Edge( anEdges.First() );
477 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aStart, anEnd );
479 if( theEqDistance > 0 )
481 GeomAdaptor_Curve anAdaptorCurve( aCurve );
482 double aLength = GCPnts_AbscissaPoint::Length( anAdaptorCurve );
483 int aNbPoints = ceil( aLength / theEqDistance );
484 GCPnts_QuasiUniformAbscissa aDist( anAdaptorCurve, aNbPoints );
487 aNbPoints = aDist.NbPoints();
488 for( int i=1; i<=aNbPoints; i++ )
490 double p = aDist.Parameter( i );
492 aCurve->D0( p, aPnt );
493 aPoints.Append( aPnt.XYZ() );
499 Handle(Geom_BSplineCurve) aGeomSpline = Handle(Geom_BSplineCurve)::DownCast( aCurve );
501 if ( !aGeomSpline.IsNull() ) {
502 int aNbKnots = aGeomSpline->NbKnots();
504 TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
505 aGeomSpline->Knots( aSplineKnots );
507 for ( int i = 1; i <= aNbKnots; ++i ) {
508 const Standard_Real& aKnot = aSplineKnots.Value( i );
510 aGeomSpline->D0( aKnot, aPnt );
511 aPoints.Append( aPnt.XYZ() );