Salome HOME
Merge remote-tracking branch 'origin/BR_LAND_COVER' into BR_v14_rc
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineOperator.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROData_PolylineOperator.h>
20 #include <HYDROData_Document.h>
21 #include <BRepBuilderAPI_MakeEdge2d.hxx>
22 #include <BRepBuilderAPI_MakeWire.hxx>
23 #include <TopoDS_Edge.hxx>
24 #include <TopoDS_Wire.hxx>
25
26 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
27 {
28   int aSize = theList.size();
29   int aNewSize = aSize + theList2.size();
30
31   if( aSize==aNewSize )
32     return;
33
34   theList.resize( aNewSize );
35   for( int i=aSize, j=0; i<aNewSize; i++, j++ )
36     theList[i] = theList2[j];
37 }
38
39
40 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
41                                         const TCollection_AsciiString& theNamePrefix,
42                                         const Handle( HYDROData_PolylineXY )& thePolyline,
43                                         const gp_Pnt2d& thePoint ) const
44 {
45   std::vector<gp_Pnt2d> aPointsList( 1 );
46   aPointsList[0] = thePoint;
47   std::vector<Handle( Geom2d_Curve )> aCurves = GetCurves( thePolyline );
48   bool isOK = true;
49   for( int i=0, n=aCurves.size(); i<n; i++ )
50   {
51     std::vector<Handle( Geom2d_Curve )> aCurvesList = Split( aCurves[i], aPointsList );
52     bool isLocalOK = CreatePolylines( theDoc, theNamePrefix, aCurvesList );
53     isOK = isOK && isLocalOK;
54   }
55   return isOK;
56 }
57
58 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
59                                         const TCollection_AsciiString& theNamePrefix,
60                                         const Handle( HYDROData_PolylineXY )& thePolyline,
61                                         const Handle( HYDROData_PolylineXY )& theTool ) const
62 {
63   std::vector<Handle( Geom2d_Curve )> aCurves = GetCurves( thePolyline );
64   std::vector<Handle( Geom2d_Curve )> aToolCurves = GetCurves( theTool );
65   bool isOK = true;
66   for( int i=0, n=aCurves.size(); i<n; i++ )
67     for( int j=0, m=aToolCurves.size(); j<m; j++ )
68     {
69       std::vector<gp_Pnt2d> aPointsList = Intersection( aCurves[i], aToolCurves[j] );
70       std::vector<Handle( Geom2d_Curve )> aCurvesList = Split( aCurves[i], aPointsList );
71       bool isLocalOK = CreatePolylines( theDoc, theNamePrefix, aCurvesList );
72       isOK = isOK && isLocalOK;
73     }
74   return isOK;
75 }
76
77 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
78                                         const TCollection_AsciiString& theNamePrefix,
79                                         const HYDROData_SequenceOfObjects& thePolylines )
80 {
81   int f = thePolylines.Lower(), l = thePolylines.Upper();
82   bool isOK = true;
83   std::vector<Handle( Geom2d_Curve )> anAllCurves;
84   for( int i=f; i<=l; i++ )
85   {
86     Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
87     std::vector<Handle( Geom2d_Curve )> aCurves = GetCurves( aPolyline );
88     append( anAllCurves, aCurves );
89   }
90
91   for( int i=0, n=anAllCurves.size(); i<n; i++ )
92   {
93     std::vector<gp_Pnt2d> aCompletePointsList;
94     for( int j=0; j<n; j++ )
95     {
96       if( i==j )
97         continue;
98       std::vector<gp_Pnt2d> aPointsList = Intersection( anAllCurves[i], anAllCurves[j] );
99       append( aCompletePointsList, aPointsList );
100     }
101     std::vector<Handle( Geom2d_Curve )> aCurvesList = Split( anAllCurves[i], aCompletePointsList );
102     bool isLocalOK = CreatePolylines( theDoc, theNamePrefix, aCurvesList );
103     isOK = isOK && isLocalOK;
104   }
105   return isOK;
106 }
107
108 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
109                                         const TCollection_AsciiString& theName,
110                                         const HYDROData_SequenceOfObjects& thePolylines,
111                                         bool isConnectByNewSegment )
112 {
113   //TODO
114
115   TopoDS_Shape aMergedPolyline;
116
117   Handle( HYDROData_PolylineXY ) aPolyline = 
118     Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
119   if( aPolyline.IsNull() )
120     return false;
121
122   aPolyline->SetShape( aMergedPolyline );
123   //TODO: set name
124
125   return true;
126 }
127
128 std::vector<Handle( Geom2d_Curve )> HYDROData_PolylineOperator::GetCurves( const Handle( HYDROData_PolylineXY )& thePolyline )
129 {
130   std::vector<Handle( Geom2d_Curve )> aResult;
131   //TODO
132   return aResult;
133 }
134
135 std::vector<gp_Pnt2d> HYDROData_PolylineOperator::Intersection( const Handle( Geom2d_Curve )& theCurve,
136                                                                 const Handle( Geom2d_Curve )& theTool )
137 {
138   std::vector<gp_Pnt2d> aResult;
139   //TODO
140   return aResult;
141 }
142
143 std::vector<Handle( Geom2d_Curve )> HYDROData_PolylineOperator::Split( const Handle( Geom2d_Curve )& theCurve,
144                                                                        const std::vector<gp_Pnt2d>& thePoints )
145 {
146   std::vector<Handle( Geom2d_Curve )> aResult;
147   //TODO
148   return aResult;
149 }
150
151 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
152                                                   const TCollection_AsciiString& theNamePrefix,
153                                                   const std::vector<Handle( Geom2d_Curve )>& theCurves )
154 {
155   if( theDoc.IsNull() )
156     return false;
157
158   int n = theCurves.size();
159   for( int i=0; i<n; i++ )
160   {
161     TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge2d( theCurves[i] ).Edge();
162     BRepBuilderAPI_MakeWire aMakeWire;
163     aMakeWire.Add( anEdge );
164
165     Handle( HYDROData_PolylineXY ) aPolyline = 
166       Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
167     if( aPolyline.IsNull() )
168       return false;
169
170     aPolyline->SetShape( aMakeWire.Wire() );
171     //TODO: set name
172   }
173   return true;
174 }