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_HANDLE(HYDROData_Polyline3D,HYDROData_Object)
60 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
63 HYDROData_Polyline3D::HYDROData_Polyline3D()
64 : HYDROData_Object( Geom_3d )
68 HYDROData_Polyline3D::~HYDROData_Polyline3D()
72 QStringList HYDROData_Polyline3D::DumpToPython( const QString& thePyScriptPath,
73 MapOfTreatedObjects& theTreatedObjects ) const
75 QStringList aResList = dumpObjectCreation( theTreatedObjects );
76 QString aPolylineName = GetObjPyName();
78 Handle(HYDROData_PolylineXY) aRefPolyline = GetPolylineXY();
79 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefPolyline, "SetPolylineXY" );
81 Handle(HYDROData_ProfileUZ) aRefProfileUZ = GetProfileUZ();
82 if ( !aRefProfileUZ.IsNull() )
84 Handle(HYDROData_Profile) aProfile =
85 Handle(HYDROData_Profile)::DownCast( aRefProfileUZ->GetFatherObject() );
86 if ( checkObjectPythonDefinition( thePyScriptPath, theTreatedObjects, aResList, aProfile ) )
88 QString aProfileName = aProfile->GetObjPyName();
89 if ( !aProfileName.isEmpty() )
91 aResList << QString( "%1.SetProfileUZ( %2.GetProfileUZ() )" )
92 .arg( aPolylineName ).arg( aProfileName );
98 Handle(HYDROData_IAltitudeObject) aRefBathymetry = GetAltitudeObject();
99 if ( !aRefBathymetry.IsNull() )
101 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
102 if ( !aChildProfileUZ.IsNull() )
104 Handle(HYDROData_Profile) aProfile =
105 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
106 if ( checkObjectPythonDefinition( thePyScriptPath, theTreatedObjects, aResList, aProfile ) )
108 QString aProfileName = aProfile->GetObjPyName();
109 if ( !aProfileName.isEmpty() )
111 aResList << QString( "%1.SetChildProfileUZ( %2.GetProfileUZ() )" )
112 .arg( aPolylineName ).arg( aProfileName );
117 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefBathymetry, "SetAltitudeObject" );
121 aResList << QString( "" );
122 aResList << QString( "%1.Update()" ).arg( aPolylineName );
123 aResList << QString( "" );
128 HYDROData_SequenceOfObjects HYDROData_Polyline3D::GetAllReferenceObjects() const
130 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
132 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
133 if ( !aPolylineXY.IsNull() )
134 aResSeq.Append( aPolylineXY );
136 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
137 if ( !aProfileUZ.IsNull() )
138 aResSeq.Append( aProfileUZ );
140 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
141 if ( !aChildProfileUZ.IsNull() )
142 aResSeq.Append( aChildProfileUZ );
147 void HYDROData_Polyline3D::Update()
149 HYDROData_Object::Update();
151 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
152 if ( aPolylineXY.IsNull() )
155 bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
156 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
157 HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
158 if ( aPolylinePoints.IsEmpty() )
161 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
163 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
164 if ( !anAltitude.IsNull() )
165 aProfileUZ = GetChildProfileUZ();
167 if ( aProfileUZ.IsNull() )
170 HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
171 if ( aProfilePoints.IsEmpty() )
174 const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
175 const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
177 const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
178 const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
180 double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
181 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
183 NCollection_Sequence<Polyline3DPoint> aResPoints;
185 // Add first point as is
186 aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
188 for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
190 const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
192 double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
194 double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
195 double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
197 Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
198 aResPoints.Append( aCompPoint );
201 // Add last point as is
202 aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
204 TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
205 SetTopShape( aResWire );
206 SetShape3D( aResWire );
209 QColor HYDROData_Polyline3D::DefaultFillingColor() const
211 return QColor( Qt::transparent );
214 QColor HYDROData_Polyline3D::DefaultBorderColor() const
216 return QColor( Qt::red );
219 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
220 const bool theIsUpdateProfile )
222 if ( thePolyline.IsNull() )
225 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
226 if ( IsEqual( aPrevPolyline, thePolyline ) )
229 SetReferenceObject( thePolyline, DataTag_PolylineXY );
231 // Update the child profile object
232 if ( theIsUpdateProfile )
233 updateChildProfilePoints();
235 // Indicate model of the need to update the polyline presentation
241 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
243 return Handle(HYDROData_PolylineXY)::DownCast(
244 GetReferenceObject( DataTag_PolylineXY ) );
247 void HYDROData_Polyline3D::RemovePolylineXY()
249 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
250 if ( aPrevPolyline.IsNull() )
253 ClearReferenceObjects( DataTag_PolylineXY );
255 // Indicate model of the need to update the polyline presentation
259 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
261 if ( theProfile.IsNull() )
264 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
265 if ( IsEqual( aPrevProfile, theProfile ) )
268 SetReferenceObject( theProfile, DataTag_ProfileUZ );
270 // Remove the bathymetry, because one altitude object can be presented at time
271 RemoveAltitudeObject();
273 // Indicate model of the need to update the polyline presentation
279 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
281 return Handle(HYDROData_ProfileUZ)::DownCast(
282 GetReferenceObject( DataTag_ProfileUZ ) );
285 void HYDROData_Polyline3D::RemoveProfileUZ()
287 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
288 if ( aPrevProfile.IsNull() )
291 ClearReferenceObjects( DataTag_ProfileUZ );
293 // Indicate model of the need to update the polyline presentation
297 bool HYDROData_Polyline3D::SetAltitudeObject(
298 const Handle(HYDROData_IAltitudeObject)& theAltitude )
300 Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
302 if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
305 if ( IsEqual( aPrevAltitude, theAltitude ) )
308 // Remove the u,z profile, because one altitude object can be presented at time
311 // Create the child profile object
312 updateChildProfilePoints();
318 void HYDROData_Polyline3D::RemoveAltitudeObject()
320 HYDROData_Object::RemoveAltitudeObject();
322 // Remove the child profile object
323 removeChildProfileUZ();
326 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
328 Handle(HYDROData_ProfileUZ) aProfileUZ =
329 Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
330 if ( !theIsCreate || !aProfileUZ.IsNull() )
333 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
334 if ( aDocument.IsNull() )
337 Handle(HYDROData_Profile) aProfile =
338 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
340 QString aProfilePref = GetName() + "_Profile";
341 QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
343 aProfile->SetName( aProfileName );
345 aProfileUZ = aProfile->GetProfileUZ();
347 HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
348 me->SetChildProfileUZ( aProfileUZ );
353 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
355 SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
358 HYDROData_IPolyline::PointsList generateProfileUZPoints(
359 const Handle(HYDROData_PolylineXY)& thePolyline,
360 const Handle(HYDROData_IAltitudeObject)& theAltitude )
362 HYDROData_IPolyline::PointsList aPointsList;
363 if ( thePolyline.IsNull() || theAltitude.IsNull() )
366 bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
367 HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
368 HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
369 if ( aPolylinePoints.IsEmpty() )
372 for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
374 const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
376 double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
377 double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
378 if( aPointDepth == theAltitude->GetInvalidAltitude() )
381 HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
382 aPointsList.Append( anAltitudePoint );
388 void HYDROData_Polyline3D::updateChildProfilePoints()
390 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
391 if ( anAltitude.IsNull() )
394 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
395 if ( aChildProfileUZ.IsNull() )
398 Handle(HYDROData_Profile) aProfile =
399 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
400 if ( aProfile.IsNull() )
403 HYDROData_IPolyline::PointsList aProfilePoints =
404 generateProfileUZPoints( GetPolylineXY(), anAltitude );
405 aProfile->SetParametricPoints( aProfilePoints );
410 void HYDROData_Polyline3D::removeChildProfileUZ()
412 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
413 if ( aChildProfileUZ.IsNull() )
416 ClearReferenceObjects( DataTag_ChildProfileUZ );
418 /* Uncomment if removing is requested
419 Handle(HYDROData_Profile) aProfile =
420 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
421 if ( !aProfile.IsNull() )
426 HYDROData_Polyline3D::Polyline3DPoints HYDROData_Polyline3D::GetPoints( double theEqDistance ) const
428 Polyline3DPoints aPoints;
430 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
431 TopoDS_Wire aWire = TopoDS::Wire( GetShape3D() );
432 if ( aPolylineXY.IsNull() || aWire.IsNull() ) {
436 // Explode polyline on edges
437 TopTools_SequenceOfShape anEdges;
438 HYDROData_ShapesTool::ExploreShapeToShapes( aWire, TopAbs_EDGE, anEdges );
441 if ( !anEdges.IsEmpty() ) {
442 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
444 if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
445 // Get points from wire
446 /* Seems that intermediate vertices are duplicated
447 TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
448 for ( ; anExp.More(); anExp.Next() ) {
449 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
450 if ( !aVertex.IsNull() ) {
451 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
452 aPoints.Append( aPnt.XYZ() );
456 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
458 for ( ; anExp.More(); anExp.Next() ) {
459 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
460 if ( !anEdge.IsNull() ) {
461 TopoDS_Vertex aV1, aV2;
462 TopExp::Vertices( anEdge, aV1, aV2 );
464 gp_Pnt aPnt1 = BRep_Tool::Pnt( aV1 );
465 aPoints.Append( aPnt1.XYZ() );
468 gp_Pnt aPnt2 = BRep_Tool::Pnt( aV2 );
469 aPoints.Append( aPnt2.XYZ() );
475 // Get points from spline curve
476 Standard_Real aStart, anEnd;
477 TopoDS_Edge anEdge = TopoDS::Edge( anEdges.First() );
478 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aStart, anEnd );
480 if( theEqDistance > 0 )
482 GeomAdaptor_Curve anAdaptorCurve( aCurve );
483 double aLength = GCPnts_AbscissaPoint::Length( anAdaptorCurve );
484 int aNbPoints = ceil( aLength / theEqDistance );
485 GCPnts_QuasiUniformAbscissa aDist( anAdaptorCurve, aNbPoints );
488 aNbPoints = aDist.NbPoints();
489 for( int i=1; i<=aNbPoints; i++ )
491 double p = aDist.Parameter( i );
493 aCurve->D0( p, aPnt );
494 aPoints.Append( aPnt.XYZ() );
500 Handle(Geom_BSplineCurve) aGeomSpline = Handle(Geom_BSplineCurve)::DownCast( aCurve );
502 if ( !aGeomSpline.IsNull() ) {
503 int aNbKnots = aGeomSpline->NbKnots();
505 TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
506 aGeomSpline->Knots( aSplineKnots );
508 for ( int i = 1; i <= aNbKnots; ++i ) {
509 const Standard_Real& aKnot = aSplineKnots.Value( i );
511 aGeomSpline->D0( aKnot, aPnt );
512 aPoints.Append( aPnt.XYZ() );