1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_SplitZonesTool.h"
25 #include <HYDROData_Zone.h>
27 void printPath( const QString& thePrefix, const QPainterPath& thePath )
29 printf( "%s (n=%d) :", qPrintable( thePrefix ), thePath.elementCount() );
30 for( int i = 0, n = thePath.elementCount(); i < n; i++ )
32 const QPainterPath::Element anElement = thePath.elementAt( i );
33 switch( anElement.type )
35 case QPainterPath::MoveToElement:
36 printf( " M(%.0f,%.0f)", anElement.x, anElement.y );
38 case QPainterPath::LineToElement:
39 printf( " L(%.0f,%.0f)", anElement.x, anElement.y );
48 HYDROGUI_SplitZonesTool::SplitDataList
49 HYDROGUI_SplitZonesTool::SplitZones( const HYDROData_SequenceOfObjects& theZoneList,
50 const Handle(HYDROData_Polyline)& thePolylie )
52 SplitDataList anOutputSplitDataList;
54 SplitDataList anInputSplitDataList;
55 for( int anIndex = 1, aLength = theZoneList.Length(); anIndex <= aLength; anIndex++ )
57 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( anIndex ) );
60 SplitData aSplitData( aZone->GetPainterPath(), aZone->GetName() );
61 anInputSplitDataList.append( aSplitData );
65 SplitDataListIterator anInputIter( anInputSplitDataList );
66 while( anInputIter.hasNext() )
68 const SplitData& anInputSplitData = anInputIter.next();
69 if( anOutputSplitDataList.isEmpty() )
70 anOutputSplitDataList.append( anInputSplitData );
73 SplitDataList aSplitDataList;
74 SplitDataListIterator anOutputIter( anOutputSplitDataList );
75 while( anOutputIter.hasNext() )
77 const SplitData& anOutputSplitData = anOutputIter.next();
79 SplitDataList aList = SplitTwoData( anOutputSplitData, anInputSplitData );
80 aSplitDataList.append( aList );
82 anOutputSplitDataList = aSplitDataList;
87 if( theZoneList.Length() != 2 )
88 return aSplitDataList;
90 Handle(HYDROData_Zone) aZone1 = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( 1 ) );
91 Handle(HYDROData_Zone) aZone2 = Handle(HYDROData_Zone)::DownCast( theZoneList.Value( 2 ) );
93 SplitData aSplitData1( aZone1->GetPainterPath(), aZone1->GetName() );
94 SplitData aSplitData2( aZone2->GetPainterPath(), aZone2->GetName() );
96 aSplitDataList = SplitTwoData( aSplitData1, aSplitData2 );
99 return anOutputSplitDataList;
102 HYDROGUI_SplitZonesTool::SplitDataList
103 HYDROGUI_SplitZonesTool::SplitTwoData( const SplitData& theData1,
104 const SplitData& theData2 )
106 SplitDataList aSplitDataList;
108 const QPainterPath& aPath1 = theData1.Path;
109 const QPainterPath& aPath2 = theData2.Path;
110 printPath( "aPath1", aPath1 );
111 printPath( "aPath2", aPath2 );
113 const QStringList& aZoneNames1 = theData1.ZoneNames;
114 const QStringList& aZoneNames2 = theData2.ZoneNames;
116 QPainterPath anIntersection = aPath1.intersected( aPath2 );
117 printPath( "anIntersection", anIntersection );
118 anIntersection.closeSubpath();
119 printPath( "anIntersection after closing", anIntersection );
120 if( anIntersection.isEmpty() )
122 aSplitDataList.append( theData1 );
123 //aSplitDataList.append( theData2 );
127 aSplitDataList.append( SplitData( anIntersection, aZoneNames1 + aZoneNames2 ) );
129 QPainterPath aPath1Sub = aPath1.subtracted( aPath2 );
130 printPath( "aPath1Sub", aPath1Sub );
131 if( !aPath1Sub.isEmpty() )
132 aSplitDataList.append( ExtractSeparateData( SplitData( aPath1Sub, aZoneNames1 ) ) );
134 QPainterPath aPath2Sub = aPath2.subtracted( aPath1 );
135 printPath( "aPath2Sub", aPath2Sub );
136 if( !aPath2Sub.isEmpty() )
137 aSplitDataList.append( ExtractSeparateData( SplitData( aPath2Sub, aZoneNames2 ) ) );
139 return aSplitDataList;
142 HYDROGUI_SplitZonesTool::SplitDataList
143 HYDROGUI_SplitZonesTool::ExtractSeparateData( const SplitData& theData )
145 SplitDataList aSplitDataList;
147 const QPainterPath& aBasePath = theData.Path;
148 const QStringList& aBaseZoneNames = theData.ZoneNames;
150 QList<QPainterPath> aPathList;
151 for( int i = 0, n = aBasePath.elementCount(); i < n; i++ )
153 const QPainterPath::Element anElement = aBasePath.elementAt( i );
154 switch( anElement.type )
156 case QPainterPath::MoveToElement:
157 aPathList.append( QPainterPath( QPointF( anElement.x, anElement.y ) ) );
159 case QPainterPath::LineToElement:
160 if( !aPathList.isEmpty() )
162 QPainterPath& aPath = aPathList.last();
163 aPath.lineTo( anElement.x, anElement.y );
166 case QPainterPath::CurveToElement: // currently not supported
172 if( aPathList.size() == 2 )
174 const QPainterPath& aPath1 = aPathList.first();
175 const QPainterPath& aPath2 = aPathList.last();
176 if( aPath1.contains( aPath2 ) || aPath2.contains( aPath1 ) )
178 printf( "HOLE CASE!\n" );
179 aSplitDataList.append( theData );
180 return aSplitDataList;
184 QListIterator<QPainterPath> anIter( aPathList );
185 while( anIter.hasNext() )
187 const QPainterPath& aPath = anIter.next();
188 printPath( "aPath", aPath );
189 aSplitDataList.append( SplitData( aPath, aBaseZoneNames ) );
191 return aSplitDataList;