Salome HOME
Additional parameter for splittiong of zones added.
[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 HYDROGUI_SplitZonesTool::SplitDataList
28 HYDROGUI_SplitZonesTool::SplitZones( const HYDROData_SequenceOfObjects& theZoneList,
29                                      const Handle(HYDROData_Polyline)&  thePolylie )
30 {
31   SplitDataList anOutputSplitDataList;
32
33   SplitDataList anInputSplitDataList;
34   for( int anIndex = 1, aLength = theZoneList.Length(); anIndex <= aLength; anIndex++ )
35   {
36     Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( anIndex ) );
37     if( !aZone.IsNull() )
38     {
39       SplitData aSplitData( aZone->GetPainterPath(), aZone->GetName() );
40       anInputSplitDataList.append( aSplitData );
41     }
42   }
43
44   SplitDataListIterator anInputIter( anInputSplitDataList );
45   while( anInputIter.hasNext() )
46   {
47     const SplitData& anInputSplitData = anInputIter.next();
48     if( anOutputSplitDataList.isEmpty() )
49       anOutputSplitDataList.append( anInputSplitData );
50     else
51     {
52       SplitDataList aSplitDataList;
53       SplitDataListIterator anOutputIter( anOutputSplitDataList );
54       while( anOutputIter.hasNext() )
55       {
56         const SplitData& anOutputSplitData = anOutputIter.next();
57
58         SplitDataList aList = SplitTwoData( anOutputSplitData, anInputSplitData );
59         aSplitDataList.append( aList );
60       }
61       anOutputSplitDataList = aSplitDataList;
62     }
63   }
64
65   /*
66   if( theZoneList.Length() != 2 )
67     return aSplitDataList;
68
69   Handle(HYDROData_Zone) aZone1 = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( 1 ) );
70   Handle(HYDROData_Zone) aZone2 = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( 2 ) );
71
72   SplitData aSplitData1( aZone1->GetPainterPath(), aZone1->GetName() );
73   SplitData aSplitData2( aZone2->GetPainterPath(), aZone2->GetName() );
74
75   aSplitDataList = SplitTwoData( aSplitData1, aSplitData2 );
76   */
77
78   return anOutputSplitDataList;
79 }
80
81 HYDROGUI_SplitZonesTool::SplitDataList
82 HYDROGUI_SplitZonesTool::SplitTwoData( const SplitData& theData1,
83                                        const SplitData& theData2 )
84 {
85   SplitDataList aSplitDataList;
86
87   const QPainterPath& aPath1 = theData1.Path;
88   const QPainterPath& aPath2 = theData2.Path;
89
90   const QStringList& aZoneNames1 = theData1.ZoneNames;
91   const QStringList& aZoneNames2 = theData2.ZoneNames;
92
93   QPainterPath anIntersection = aPath1.intersected( aPath2 );
94   if( anIntersection.isEmpty() )
95   {
96     aSplitDataList.append( theData1 );
97     aSplitDataList.append( theData2 );
98   }
99   else
100   {
101     aSplitDataList.append( SplitData( anIntersection, aZoneNames1 + aZoneNames2 ) );
102
103     QPainterPath aPath1Sub = aPath1.subtracted( aPath2 );
104     if( !aPath1Sub.isEmpty() )
105       aSplitDataList.append( ExtractSeparateData( SplitData( aPath1Sub, aZoneNames1 ) ) );
106
107     QPainterPath aPath2Sub = aPath2.subtracted( aPath1 );
108     if( !aPath2Sub.isEmpty() )
109       aSplitDataList.append( ExtractSeparateData( SplitData( aPath2Sub, aZoneNames2 ) ) );
110   }
111   return aSplitDataList;
112 }
113
114 HYDROGUI_SplitZonesTool::SplitDataList
115 HYDROGUI_SplitZonesTool::ExtractSeparateData( const SplitData& theData )
116 {
117   SplitDataList aSplitDataList;
118
119   const QPainterPath& aBasePath = theData.Path;
120   const QStringList& aBaseZoneNames = theData.ZoneNames;
121
122   QList<QPainterPath> aPathList;
123   for( int i = 0, n = aBasePath.elementCount(); i < n; i++ )
124   {
125     const QPainterPath::Element anElement = aBasePath.elementAt( i );
126     switch( anElement.type )
127     {
128       case QPainterPath::MoveToElement:
129         aPathList.append( QPainterPath( QPointF( anElement.x, anElement.y ) ) );
130         break;
131       case QPainterPath::LineToElement:
132         if( !aPathList.isEmpty() )
133         {
134           QPainterPath& aPath = aPathList.last();
135           aPath.lineTo( anElement.x, anElement.y );
136         }
137         break;
138       case QPainterPath::CurveToElement: // currently not supported
139       default:
140         break;
141     }
142   }
143
144   if( aPathList.size() == 2 )
145   {
146     const QPainterPath& aPath1 = aPathList.first();
147     const QPainterPath& aPath2 = aPathList.last();
148     if( aPath1.contains( aPath2 ) || aPath2.contains( aPath1 ) )
149     {
150       aSplitDataList.append( theData );
151       return aSplitDataList;
152     }
153   }
154
155   QListIterator<QPainterPath> anIter( aPathList );
156   while( anIter.hasNext() )
157   {
158     const QPainterPath& aPath = anIter.next();
159     aSplitDataList.append( SplitData( aPath, aBaseZoneNames ) );
160   }
161   return aSplitDataList;
162 }