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>
33 #include <gp_Pnt2d.hxx>
37 #include <TColStd_Array1OfReal.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopoDS_Wire.hxx>
46 #include <TopExp_Explorer.hxx>
48 #include <TopTools_SequenceOfShape.hxx>
51 #include <QStringList>
53 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline3D,HYDROData_Object)
54 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
57 HYDROData_Polyline3D::HYDROData_Polyline3D()
58 : HYDROData_Object( Geom_3d )
62 HYDROData_Polyline3D::~HYDROData_Polyline3D()
66 QStringList HYDROData_Polyline3D::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
68 QStringList aResList = dumpObjectCreation( theTreatedObjects );
69 QString aPolylineName = GetObjPyName();
71 Handle(HYDROData_PolylineXY) aRefPolyline = GetPolylineXY();
72 setPythonReferenceObject( theTreatedObjects, aResList, aRefPolyline, "SetPolylineXY" );
74 Handle(HYDROData_ProfileUZ) aRefProfileUZ = GetProfileUZ();
75 if ( !aRefProfileUZ.IsNull() )
77 Handle(HYDROData_Profile) aProfile =
78 Handle(HYDROData_Profile)::DownCast( aRefProfileUZ->GetFatherObject() );
79 if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
81 QString aProfileName = aProfile->GetObjPyName();
82 if ( !aProfileName.isEmpty() )
84 aResList << QString( "%1.SetProfileUZ( %2.GetProfileUZ() );" )
85 .arg( aPolylineName ).arg( aProfileName );
91 Handle(HYDROData_IAltitudeObject) aRefBathymetry = GetAltitudeObject();
92 if ( !aRefBathymetry.IsNull() )
94 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
95 if ( !aChildProfileUZ.IsNull() )
97 Handle(HYDROData_Profile) aProfile =
98 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
99 if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
101 QString aProfileName = aProfile->GetObjPyName();
102 if ( !aProfileName.isEmpty() )
104 aResList << QString( "%1.SetChildProfileUZ( %2.GetProfileUZ() );" )
105 .arg( aPolylineName ).arg( aProfileName );
110 setPythonReferenceObject( theTreatedObjects, aResList, aRefBathymetry, "SetAltitudeObject" );
114 aResList << QString( "" );
115 aResList << QString( "%1.Update();" ).arg( aPolylineName );
116 aResList << QString( "" );
121 HYDROData_SequenceOfObjects HYDROData_Polyline3D::GetAllReferenceObjects() const
123 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
125 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
126 if ( !aPolylineXY.IsNull() )
127 aResSeq.Append( aPolylineXY );
129 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
130 if ( !aProfileUZ.IsNull() )
131 aResSeq.Append( aProfileUZ );
133 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
134 if ( !aChildProfileUZ.IsNull() )
135 aResSeq.Append( aChildProfileUZ );
140 TopoDS_Shape HYDROData_Polyline3D::GetTopShape() const
142 return getTopShape();
145 TopoDS_Shape HYDROData_Polyline3D::GetShape3D() const
150 void HYDROData_Polyline3D::Update()
152 HYDROData_Object::Update();
154 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
155 if ( aPolylineXY.IsNull() )
158 bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
159 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
160 HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
161 if ( aPolylinePoints.IsEmpty() )
164 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
166 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
167 if ( !anAltitude.IsNull() )
168 aProfileUZ = GetChildProfileUZ();
170 if ( aProfileUZ.IsNull() )
173 HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
174 if ( aProfilePoints.IsEmpty() )
177 const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
178 const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
180 const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
181 const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
183 double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
184 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
186 NCollection_Sequence<Polyline3DPoint> aResPoints;
188 // Add first point as is
189 aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
191 for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
193 const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
195 double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
197 double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
198 double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
200 Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
201 aResPoints.Append( aCompPoint );
204 // Add last point as is
205 aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
207 TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
208 SetTopShape( aResWire );
209 SetShape3D( aResWire );
212 QColor HYDROData_Polyline3D::DefaultFillingColor()
214 return QColor( Qt::transparent );
217 QColor HYDROData_Polyline3D::DefaultBorderColor()
219 return QColor( Qt::red );
222 QColor HYDROData_Polyline3D::getDefaultFillingColor() const
224 return DefaultFillingColor();
227 QColor HYDROData_Polyline3D::getDefaultBorderColor() const
229 return DefaultBorderColor();
232 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
233 const bool theIsUpdateProfile )
235 if ( thePolyline.IsNull() )
238 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
239 if ( IsEqual( aPrevPolyline, thePolyline ) )
242 SetReferenceObject( thePolyline, DataTag_PolylineXY );
244 // Update the child profile object
245 if ( theIsUpdateProfile )
246 updateChildProfilePoints();
248 // Indicate model of the need to update the polyline presentation
254 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
256 return Handle(HYDROData_PolylineXY)::DownCast(
257 GetReferenceObject( DataTag_PolylineXY ) );
260 void HYDROData_Polyline3D::RemovePolylineXY()
262 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
263 if ( aPrevPolyline.IsNull() )
266 ClearReferenceObjects( DataTag_PolylineXY );
268 // Indicate model of the need to update the polyline presentation
272 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
274 if ( theProfile.IsNull() )
277 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
278 if ( IsEqual( aPrevProfile, theProfile ) )
281 SetReferenceObject( theProfile, DataTag_ProfileUZ );
283 // Remove the bathymetry, because one altitude object can be presented at time
284 RemoveAltitudeObject();
286 // Indicate model of the need to update the polyline presentation
292 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
294 return Handle(HYDROData_ProfileUZ)::DownCast(
295 GetReferenceObject( DataTag_ProfileUZ ) );
298 void HYDROData_Polyline3D::RemoveProfileUZ()
300 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
301 if ( aPrevProfile.IsNull() )
304 ClearReferenceObjects( DataTag_ProfileUZ );
306 // Indicate model of the need to update the polyline presentation
310 bool HYDROData_Polyline3D::SetAltitudeObject(
311 const Handle(HYDROData_IAltitudeObject)& theAltitude )
313 Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
315 if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
318 if ( IsEqual( aPrevAltitude, theAltitude ) )
321 // Remove the u,z profile, because one altitude object can be presented at time
324 // Create the child profile object
325 updateChildProfilePoints();
331 void HYDROData_Polyline3D::RemoveAltitudeObject()
333 HYDROData_Object::RemoveAltitudeObject();
335 // Remove the child profile object
336 removeChildProfileUZ();
339 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
341 Handle(HYDROData_ProfileUZ) aProfileUZ =
342 Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
343 if ( !theIsCreate || !aProfileUZ.IsNull() )
346 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
347 if ( aDocument.IsNull() )
350 Handle(HYDROData_Profile) aProfile =
351 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
353 QString aProfilePref = GetName() + "_Profile";
354 QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
356 aProfile->SetName( aProfileName );
358 aProfileUZ = aProfile->GetProfileUZ();
360 HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
361 me->SetChildProfileUZ( aProfileUZ );
366 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
368 SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
371 HYDROData_IPolyline::PointsList generateProfileUZPoints(
372 const Handle(HYDROData_PolylineXY)& thePolyline,
373 const Handle(HYDROData_IAltitudeObject)& theAltitude )
375 HYDROData_IPolyline::PointsList aPointsList;
376 if ( thePolyline.IsNull() || theAltitude.IsNull() )
379 bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
380 HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
381 HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
382 if ( aPolylinePoints.IsEmpty() )
385 for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
387 const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
389 double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
390 double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
391 if( aPointDepth == theAltitude->GetInvalidAltitude() )
394 HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
395 aPointsList.Append( anAltitudePoint );
401 void HYDROData_Polyline3D::updateChildProfilePoints()
403 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
404 if ( anAltitude.IsNull() )
407 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
408 if ( aChildProfileUZ.IsNull() )
411 Handle(HYDROData_Profile) aProfile =
412 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
413 if ( aProfile.IsNull() )
416 HYDROData_IPolyline::PointsList aProfilePoints =
417 generateProfileUZPoints( GetPolylineXY(), anAltitude );
418 aProfile->SetParametricPoints( aProfilePoints );
423 void HYDROData_Polyline3D::removeChildProfileUZ()
425 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
426 if ( aChildProfileUZ.IsNull() )
429 ClearReferenceObjects( DataTag_ChildProfileUZ );
431 /* Uncomment if removing is requested
432 Handle(HYDROData_Profile) aProfile =
433 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
434 if ( !aProfile.IsNull() )
439 HYDROData_Polyline3D::Polyline3DPoints HYDROData_Polyline3D::GetPoints() const
441 Polyline3DPoints aPoints;
443 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
444 TopoDS_Wire aWire = TopoDS::Wire( GetShape3D() );
445 if ( aPolylineXY.IsNull() || aWire.IsNull() ) {
449 // Explode polyline on edges
450 TopTools_SequenceOfShape anEdges;
451 HYDROData_ShapesTool::ExploreShapeToShapes( aWire, TopAbs_EDGE, anEdges );
454 if ( !anEdges.IsEmpty() ) {
455 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
457 if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
458 // Get points from wire
459 /* Seems that intermediate vertices are duplicated
460 TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
461 for ( ; anExp.More(); anExp.Next() ) {
462 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
463 if ( !aVertex.IsNull() ) {
464 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
465 aPoints.Append( aPnt.XYZ() );
469 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
471 for ( ; anExp.More(); anExp.Next() ) {
472 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
473 if ( !anEdge.IsNull() ) {
474 TopoDS_Vertex aV1, aV2;
475 TopExp::Vertices( anEdge, aV1, aV2 );
477 gp_Pnt aPnt1 = BRep_Tool::Pnt( aV1 );
478 aPoints.Append( aPnt1.XYZ() );
481 gp_Pnt aPnt2 = BRep_Tool::Pnt( aV2 );
482 aPoints.Append( aPnt2.XYZ() );
488 // Get points from spline curve
489 Standard_Real aStart, anEnd;
490 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
491 Handle(Geom_BSplineCurve) aGeomSpline = Handle(Geom_BSplineCurve)::DownCast( aCurve );
493 if ( !aGeomSpline.IsNull() ) {
494 int aNbKnots = aGeomSpline->NbKnots();
496 TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
497 aGeomSpline->Knots( aSplineKnots );
499 for ( int i = 1; i <= aNbKnots; ++i ) {
500 const Standard_Real& aKnot = aSplineKnots.Value( i );
502 aGeomSpline->D0( aKnot, aPnt );
503 aPoints.Append( aPnt.XYZ() );