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 NCollection_Sequence<Polyline3DPoint> aResPoints = GetProfilePoints3D(false);
158 TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
159 SetTopShape( aResWire );
160 SetShape3D( aResWire );
163 HYDROData_Polyline3D::Polyline3DPoints HYDROData_Polyline3D::GetProfilePoints3D(bool IsConvertToGlobal)
165 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
166 if ( aPolylineXY.IsNull() )
167 return Polyline3DPoints();
169 HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
170 if ( aPolylinePoints.IsEmpty() )
171 return Polyline3DPoints();
173 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
175 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
176 if ( !anAltitude.IsNull() )
177 aProfileUZ = GetChildProfileUZ();
179 if ( aProfileUZ.IsNull() )
180 return Polyline3DPoints();
182 HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
183 if ( aProfilePoints.IsEmpty() )
184 return NCollection_Sequence<HYDROData_Polyline3D::Polyline3DPoint>();
186 const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
187 const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
189 const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
190 const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
192 double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
193 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
195 NCollection_Sequence<Polyline3DPoint> aResPoints;
197 // Add first point as is
198 aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
200 for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
202 const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
204 double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
206 double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
207 double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
209 Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
210 aResPoints.Append( aCompPoint );
213 // Add last point as is
214 aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
216 if (IsConvertToGlobal)
218 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
219 for (int i=1;i<=aResPoints.Size();i++)
221 gp_XYZ& point = aResPoints.ChangeValue(i);
222 aDoc->Transform( point, false );
228 QColor HYDROData_Polyline3D::DefaultFillingColor() const
230 return QColor( Qt::transparent );
233 QColor HYDROData_Polyline3D::DefaultBorderColor() const
235 return QColor( Qt::red );
238 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
239 const bool theIsUpdateProfile )
241 if ( thePolyline.IsNull() )
244 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
245 if ( IsEqual( aPrevPolyline, thePolyline ) )
248 SetReferenceObject( thePolyline, DataTag_PolylineXY );
250 // Update the child profile object
251 if ( theIsUpdateProfile )
252 updateChildProfilePoints();
254 // Indicate model of the need to update the polyline presentation
260 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
262 return Handle(HYDROData_PolylineXY)::DownCast(
263 GetReferenceObject( DataTag_PolylineXY ) );
266 void HYDROData_Polyline3D::RemovePolylineXY()
268 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
269 if ( aPrevPolyline.IsNull() )
272 ClearReferenceObjects( DataTag_PolylineXY );
274 // Indicate model of the need to update the polyline presentation
278 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
280 if ( theProfile.IsNull() )
283 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
284 if ( IsEqual( aPrevProfile, theProfile ) )
287 SetReferenceObject( theProfile, DataTag_ProfileUZ );
289 // Remove the bathymetry, because one altitude object can be presented at time
290 RemoveAltitudeObject();
292 // Indicate model of the need to update the polyline presentation
298 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
300 return Handle(HYDROData_ProfileUZ)::DownCast(
301 GetReferenceObject( DataTag_ProfileUZ ) );
304 void HYDROData_Polyline3D::RemoveProfileUZ()
306 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
307 if ( aPrevProfile.IsNull() )
310 ClearReferenceObjects( DataTag_ProfileUZ );
312 // Indicate model of the need to update the polyline presentation
316 bool HYDROData_Polyline3D::SetAltitudeObject(
317 const Handle(HYDROData_IAltitudeObject)& theAltitude )
319 Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
321 if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
324 if ( IsEqual( aPrevAltitude, theAltitude ) )
327 // Remove the u,z profile, because one altitude object can be presented at time
330 // Create the child profile object
331 updateChildProfilePoints();
337 void HYDROData_Polyline3D::RemoveAltitudeObject()
339 HYDROData_Object::RemoveAltitudeObject();
341 // Remove the child profile object
342 removeChildProfileUZ();
345 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
347 Handle(HYDROData_ProfileUZ) aProfileUZ =
348 Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
349 if ( !theIsCreate || !aProfileUZ.IsNull() )
352 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
353 if ( aDocument.IsNull() )
356 Handle(HYDROData_Profile) aProfile =
357 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
359 QString aProfilePref = GetName() + "_Profile";
360 QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
362 aProfile->SetName( aProfileName );
364 aProfileUZ = aProfile->GetProfileUZ();
366 HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
367 me->SetChildProfileUZ( aProfileUZ );
372 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
374 SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
377 HYDROData_IPolyline::PointsList generateProfileUZPoints(
378 const Handle(HYDROData_PolylineXY)& thePolyline,
379 const Handle(HYDROData_IAltitudeObject)& theAltitude )
381 HYDROData_IPolyline::PointsList aPointsList;
382 if ( thePolyline.IsNull() || theAltitude.IsNull() )
385 bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
386 HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
387 HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
388 if ( aPolylinePoints.IsEmpty() )
391 for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
393 const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
395 double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
396 double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
397 if( aPointDepth == theAltitude->GetInvalidAltitude() )
400 HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
401 aPointsList.Append( anAltitudePoint );
407 void HYDROData_Polyline3D::updateChildProfilePoints()
409 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
410 if ( anAltitude.IsNull() )
413 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
414 if ( aChildProfileUZ.IsNull() )
417 Handle(HYDROData_Profile) aProfile =
418 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
419 if ( aProfile.IsNull() )
422 HYDROData_IPolyline::PointsList aProfilePoints =
423 generateProfileUZPoints( GetPolylineXY(), anAltitude );
424 aProfile->SetParametricPoints( aProfilePoints );
429 void HYDROData_Polyline3D::removeChildProfileUZ()
431 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
432 if ( aChildProfileUZ.IsNull() )
435 ClearReferenceObjects( DataTag_ChildProfileUZ );
437 /* Uncomment if removing is requested
438 Handle(HYDROData_Profile) aProfile =
439 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
440 if ( !aProfile.IsNull() )
445 HYDROData_Polyline3D::Polyline3DPoints HYDROData_Polyline3D::GetPoints( double theEqDistance ) const
447 Polyline3DPoints aPoints;
449 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
450 TopoDS_Wire aWire = TopoDS::Wire( GetShape3D() );
451 if ( aPolylineXY.IsNull() || aWire.IsNull() ) {
455 // Explode polyline on edges
456 TopTools_SequenceOfShape anEdges;
457 HYDROData_ShapesTool::ExploreShapeToShapes( aWire, TopAbs_EDGE, anEdges );
460 if ( !anEdges.IsEmpty() ) {
461 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
463 if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
464 // Get points from wire
465 /* Seems that intermediate vertices are duplicated
466 TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
467 for ( ; anExp.More(); anExp.Next() ) {
468 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
469 if ( !aVertex.IsNull() ) {
470 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
471 aPoints.Append( aPnt.XYZ() );
475 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
477 for ( ; anExp.More(); anExp.Next() ) {
478 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
479 if ( !anEdge.IsNull() ) {
480 TopoDS_Vertex aV1, aV2;
481 TopExp::Vertices( anEdge, aV1, aV2 );
483 gp_Pnt aPnt1 = BRep_Tool::Pnt( aV1 );
484 aPoints.Append( aPnt1.XYZ() );
487 gp_Pnt aPnt2 = BRep_Tool::Pnt( aV2 );
488 aPoints.Append( aPnt2.XYZ() );
494 // Get points from spline curve
495 Standard_Real aStart, anEnd;
496 TopoDS_Edge anEdge = TopoDS::Edge( anEdges.First() );
497 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aStart, anEnd );
499 if( theEqDistance > 0 )
501 GeomAdaptor_Curve anAdaptorCurve( aCurve );
502 double aLength = GCPnts_AbscissaPoint::Length( anAdaptorCurve );
503 int aNbPoints = ceil( aLength / theEqDistance );
504 GCPnts_QuasiUniformAbscissa aDist( anAdaptorCurve, aNbPoints );
507 aNbPoints = aDist.NbPoints();
508 for( int i=1; i<=aNbPoints; i++ )
510 double p = aDist.Parameter( i );
512 aCurve->D0( p, aPnt );
513 aPoints.Append( aPnt.XYZ() );
519 Handle(Geom_BSplineCurve) aGeomSpline = Handle(Geom_BSplineCurve)::DownCast( aCurve );
521 if ( !aGeomSpline.IsNull() ) {
522 int aNbKnots = aGeomSpline->NbKnots();
524 TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
525 aGeomSpline->Knots( aSplineKnots );
527 for ( int i = 1; i <= aNbKnots; ++i ) {
528 const Standard_Real& aKnot = aSplineKnots.Value( i );
530 aGeomSpline->D0( aKnot, aPnt );
531 aPoints.Append( aPnt.XYZ() );