Salome HOME
Modify creation of curves: 1) using QDockWidget instead of QDialog; 2) selection...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_SplitZonesTool.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_SplitZonesTool.h"
24
25 #include <HYDROData_Zone.h>
26
27 void printPath( const QString& thePrefix, const QPainterPath& thePath )
28 {
29   printf( "%s (n=%d) :", qPrintable( thePrefix ), thePath.elementCount() );
30   for( int i = 0, n = thePath.elementCount(); i < n; i++ )
31   {
32     const QPainterPath::Element anElement = thePath.elementAt( i );
33     switch( anElement.type )
34     {
35       case QPainterPath::MoveToElement:
36         printf( " M(%.0f,%.0f)", anElement.x, anElement.y );
37         break;
38       case QPainterPath::LineToElement:
39         printf( " L(%.0f,%.0f)", anElement.x, anElement.y );
40         break;
41       default:
42         break;
43     }
44   }
45   printf( "\n" );
46 }
47
48 HYDROGUI_SplitZonesTool::SplitDataList
49 HYDROGUI_SplitZonesTool::SplitZones( const HYDROData_SequenceOfObjects& theZoneList,
50                                      const Handle(HYDROData_Polyline)&  thePolylie )
51 {
52   SplitDataList anOutputSplitDataList;
53
54   SplitDataList anInputSplitDataList;
55   for( int anIndex = 1, aLength = theZoneList.Length(); anIndex <= aLength; anIndex++ )
56   {
57     Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( anIndex ) );
58     if( !aZone.IsNull() )
59     {
60       SplitData aSplitData( aZone->GetPainterPath(), aZone->GetName() );
61       anInputSplitDataList.append( aSplitData );
62     }
63   }
64
65   SplitDataListIterator anInputIter( anInputSplitDataList );
66   while( anInputIter.hasNext() )
67   {
68     const SplitData& anInputSplitData = anInputIter.next();
69     if( anOutputSplitDataList.isEmpty() )
70       anOutputSplitDataList.append( anInputSplitData );
71     else
72     {
73       SplitDataList aSplitDataList;
74       SplitDataListIterator anOutputIter( anOutputSplitDataList );
75       while( anOutputIter.hasNext() )
76       {
77         const SplitData& anOutputSplitData = anOutputIter.next();
78
79         SplitDataList aList = SplitTwoData( anOutputSplitData, anInputSplitData );
80         aSplitDataList.append( aList );
81       }
82       anOutputSplitDataList = aSplitDataList;
83     }
84   }
85
86   /*
87   if( theZoneList.Length() != 2 )
88     return aSplitDataList;
89
90   Handle(HYDROData_Zone) aZone1 = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( 1 ) );
91   Handle(HYDROData_Zone) aZone2 = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( 2 ) );
92
93   SplitData aSplitData1( aZone1->GetPainterPath(), aZone1->GetName() );
94   SplitData aSplitData2( aZone2->GetPainterPath(), aZone2->GetName() );
95
96   aSplitDataList = SplitTwoData( aSplitData1, aSplitData2 );
97   */
98
99   return anOutputSplitDataList;
100 }
101
102 HYDROGUI_SplitZonesTool::SplitDataList
103 HYDROGUI_SplitZonesTool::SplitTwoData( const SplitData& theData1,
104                                        const SplitData& theData2 )
105 {
106   SplitDataList aSplitDataList;
107
108   const QPainterPath& aPath1 = theData1.Path;
109   const QPainterPath& aPath2 = theData2.Path;
110   printPath( "aPath1", aPath1 );
111   printPath( "aPath2", aPath2 );
112
113   const QStringList& aZoneNames1 = theData1.ZoneNames;
114   const QStringList& aZoneNames2 = theData2.ZoneNames;
115
116   QPainterPath anIntersection = aPath1.intersected( aPath2 );
117   printPath( "anIntersection", anIntersection );
118   anIntersection.closeSubpath();
119   printPath( "anIntersection after closing", anIntersection );
120   if( anIntersection.isEmpty() )
121   {
122     aSplitDataList.append( theData1 );
123     //aSplitDataList.append( theData2 );
124   }
125   else
126   {
127     aSplitDataList.append( SplitData( anIntersection, aZoneNames1 + aZoneNames2 ) );
128
129     QPainterPath aPath1Sub = aPath1.subtracted( aPath2 );
130     printPath( "aPath1Sub", aPath1Sub );
131     if( !aPath1Sub.isEmpty() )
132       aSplitDataList.append( ExtractSeparateData( SplitData( aPath1Sub, aZoneNames1 ) ) );
133
134     QPainterPath aPath2Sub = aPath2.subtracted( aPath1 );
135     printPath( "aPath2Sub", aPath2Sub );
136     if( !aPath2Sub.isEmpty() )
137       aSplitDataList.append( ExtractSeparateData( SplitData( aPath2Sub, aZoneNames2 ) ) );
138   }
139   return aSplitDataList;
140 }
141
142 HYDROGUI_SplitZonesTool::SplitDataList
143 HYDROGUI_SplitZonesTool::ExtractSeparateData( const SplitData& theData )
144 {
145   SplitDataList aSplitDataList;
146
147   const QPainterPath& aBasePath = theData.Path;
148   const QStringList& aBaseZoneNames = theData.ZoneNames;
149
150   QList<QPainterPath> aPathList;
151   for( int i = 0, n = aBasePath.elementCount(); i < n; i++ )
152   {
153     const QPainterPath::Element anElement = aBasePath.elementAt( i );
154     switch( anElement.type )
155     {
156       case QPainterPath::MoveToElement:
157         aPathList.append( QPainterPath( QPointF( anElement.x, anElement.y ) ) );
158         break;
159       case QPainterPath::LineToElement:
160         if( !aPathList.isEmpty() )
161         {
162           QPainterPath& aPath = aPathList.last();
163           aPath.lineTo( anElement.x, anElement.y );
164         }
165         break;
166       case QPainterPath::CurveToElement: // currently not supported
167       default:
168         break;
169     }
170   }
171
172   if( aPathList.size() == 2 )
173   {
174     const QPainterPath& aPath1 = aPathList.first();
175     const QPainterPath& aPath2 = aPathList.last();
176     if( aPath1.contains( aPath2 ) || aPath2.contains( aPath1 ) )
177     {
178       printf( "HOLE CASE!\n" );
179       aSplitDataList.append( theData );
180       return aSplitDataList;
181     }
182   }
183
184   QListIterator<QPainterPath> anIter( aPathList );
185   while( anIter.hasNext() )
186   {
187     const QPainterPath& aPath = anIter.next();
188     printPath( "aPath", aPath );
189     aSplitDataList.append( SplitData( aPath, aBaseZoneNames ) );
190   }
191   return aSplitDataList;
192 }