Salome HOME
48b9340f1593568871997f6892d3ea3375a6bc2a
[modules/hydro.git] / src / HYDROData / HYDROData_Tool.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_Tool.h"
20
21 #include "HYDROData_ArtificialObject.h"
22 #include "HYDROData_Image.h"
23 #include "HYDROData_Iterator.h"
24 #include "HYDROData_NaturalObject.h"
25 #include "HYDROData_ShapesGroup.h"
26
27 #include <QFile>
28 #include <QStringList>
29 #include <QTextStream>
30
31 #include <limits>
32 #include <gp_Pnt.hxx>
33 #include <gp_Pln.hxx>
34 #include <ElSLib.hxx>
35 #include <TopAbs_State.hxx>
36 #include <BRepAdaptor_Surface.hxx>
37 #include <BRepTopAdaptor_FClass2d.hxx>
38 #include <BRep_Tool.hxx>
39 #include <Geom_Curve.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Wire.hxx>
42 #include <TopExp_Explorer.hxx>
43
44 static int aMaxNameId = std::numeric_limits<int>::max();
45
46 void HYDROData_Tool::WriteStringsToFile( QFile&             theFile,
47                                          const QStringList& theStrings,
48                                          const QString&     theSep )
49 {
50   if ( !theFile.isOpen() || theStrings.isEmpty() )
51     return;
52   
53   QString aWriteStr = theStrings.join( theSep );
54   if ( aWriteStr.isEmpty() )
55     return;
56
57   QTextStream anOutStream( &theFile );
58   anOutStream << aWriteStr << theSep << theSep;
59 }
60
61 void HYDROData_Tool::SetMustBeUpdatedObjects(
62   const Handle(HYDROData_Document)& theDoc  )
63 {
64   bool anIsChanged = true;
65
66   // iterate until there is no changes because objects on all level of dependency must be updated
67   while ( anIsChanged )
68   {
69     anIsChanged = false;
70
71     HYDROData_Iterator anIter( theDoc );
72     for ( ; anIter.More(); anIter.Next() )
73     {
74       Handle(HYDROData_Entity) anObject = anIter.Current();
75       if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
76         continue;
77
78       HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
79       for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
80       {
81         Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
82         if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
83           continue;
84
85         anObject->SetToUpdate( true );
86         anIsChanged = true;
87         break;
88       }
89     }
90   }
91 }
92
93 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
94                                             const QString&                    thePrefix,
95                                             const QStringList&                theUsedNames,
96                                             const bool                        theIsTryToUsePurePrefix )
97 {
98   QStringList aNamesList( theUsedNames );
99
100   // Collect all used names in the document
101   HYDROData_Iterator anIter( theDoc );
102   for( ; anIter.More(); anIter.Next() )
103   {
104     Handle(HYDROData_Entity) anObject = anIter.Current();
105     if( anObject.IsNull() )
106       continue;
107
108     QString anObjName = anObject->GetName();
109     if ( anObjName.isEmpty() )
110       continue;
111
112     aNamesList.append( anObjName );
113   }
114
115   QString aName;
116
117   if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
118     aName = thePrefix;
119   } else {
120     int anId = 1;
121     while( anId < aMaxNameId )
122     {
123       aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
124
125       // check that there are no other objects with the same name in the document
126       if ( !aNamesList.contains( aName ) )
127         break;
128     }
129   }
130
131   return aName;
132 }
133
134 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
135 {
136   if ( theObject.IsNull() )
137     return false;
138   
139   return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
140          theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
141 }
142
143 void HYDROData_Tool::UpdateChildObjectName( const QString&                  theOldStr,
144                                             const QString&                  theNewStr,
145                                             const Handle(HYDROData_Entity)& theObject )
146 {
147   if ( theObject.IsNull() )
148     return;
149
150   QString anObjName = theObject->GetName();
151   if ( theOldStr.isEmpty() )
152   {
153     while ( anObjName.startsWith( '_' ) )
154       anObjName.remove( 0, 1 );
155
156     anObjName.prepend( theNewStr + "_" );
157   }
158   else if ( anObjName.startsWith( theOldStr ) )
159   {
160     anObjName.replace( 0, theOldStr.length(), theNewStr );
161   }
162   else
163     return;
164
165   theObject->SetName( anObjName );
166 }
167
168 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
169                                                const QString&             thePrefix )
170 {
171   QString aName = thePrefix;
172   if ( !theTreatedObjects.contains( aName ) )
173     return aName;
174
175   int anId = 1;
176   while( anId < aMaxNameId )
177   {
178     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
179
180     // check that there are no other objects with the same name
181     if ( !theTreatedObjects.contains( aName ) )
182       break;
183   }
184
185   return aName;
186 }
187 //======================================================================================================
188 TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& theXY, const TopoDS_Face& theFace )
189 {
190   TopAbs_State aState(TopAbs_UNKNOWN);
191   if(theFace.IsNull()) return aState;  
192   Standard_Real aTol = BRep_Tool::Tolerance(theFace);
193   BRepAdaptor_Surface Ads ( theFace, Standard_False );
194   Standard_Real toluv = Min ( Ads.UResolution(aTol), Ads.VResolution(aTol) ); 
195   const gp_Pln& aPlane = Ads.Surface().Plane();
196   gp_Pnt aPnt(theXY.X(), theXY.Y(), 0.);
197   Standard_Real aU1, aV1;
198   ElSLib::Parameters(aPlane,aPnt, aU1, aV1);
199   BRepTopAdaptor_FClass2d aClassifier( theFace, toluv ); 
200   aState = aClassifier.Perform( gp_Pnt2d(aU1, aV1), Standard_False );
201   return aState;
202 }
203
204 double HYDROData_Tool::GetAltitudeForEdge( const TopoDS_Edge& theEdge,
205                                            const gp_XY& thePoint,
206                                            double theParameterTolerance,
207                                            double theSquareDistanceTolerance,
208                                            double theInvalidAltitude )
209 {
210   double aFirst, aLast;
211   Handle(Geom_Curve) aCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
212   if( aCurve.IsNull() )
213     return theInvalidAltitude;
214
215   gp_Pnt aFirstPnt, aLastPnt;
216
217   aCurve->D0( aFirst, aFirstPnt );
218   aCurve->D0( aLast, aLastPnt );
219
220   gp_Pnt2d aFirstPnt2d( aFirstPnt.X(), aFirstPnt.Y() );
221   gp_Pnt2d aLastPnt2d( aLastPnt.X(), aLastPnt.Y() );
222
223   double aFirstDist = 0;
224   double aLastDist = aFirstPnt2d.SquareDistance( aLastPnt2d );
225   double aNecDist = aFirstPnt2d.SquareDistance( thePoint );
226
227   while( fabs( aLast - aFirst ) > theParameterTolerance )
228   {
229     double aMid = ( aFirst + aLast ) / 2;
230     gp_Pnt aMidPnt;
231     aCurve->D0( aMid, aMidPnt );
232     double aDist = aFirstPnt2d.SquareDistance( gp_Pnt2d( aMidPnt.X(), aMidPnt.Y() ) );
233
234     if( aDist < aNecDist )
235       aFirst = aMid;
236     else
237       aLast = aMid;
238   }
239
240   double aMid = ( aFirst + aLast ) / 2;
241   gp_Pnt aMidPnt;
242   aCurve->D0( aMid, aMidPnt );
243
244   gp_Pnt2d aMidPnt2d( aMidPnt.X(), aMidPnt.Y() );
245   if( aMidPnt2d.SquareDistance( thePoint ) < theSquareDistanceTolerance )
246     return aMidPnt.Z();
247   else
248     return theInvalidAltitude;
249 }
250
251 double HYDROData_Tool::GetAltitudeForWire( const TopoDS_Wire& theWire,
252                                            const gp_XY& thePoint,
253                                            double theParameterTolerance,
254                                            double theSquareDistanceTolerance,
255                                            double theInvalidAltitude )
256 {
257   TopExp_Explorer anExp( theWire, TopAbs_EDGE );
258   for( ; anExp.More(); anExp.Next() )
259   {
260     double anAltitude = GetAltitudeForEdge( TopoDS::Edge( anExp.Current() ), thePoint,
261       theParameterTolerance, theSquareDistanceTolerance, theInvalidAltitude );
262     if( anAltitude != theInvalidAltitude )
263       return anAltitude;
264   }
265   return theInvalidAltitude;
266 }
267
268 TopoDS_Shape HYDROData_Tool::getFirstShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups,
269                                                      const int                          theGroupId )
270 {
271   TopoDS_Shape aResShape;
272   if ( theGroupId < 1 || theGroupId > theGroups.Length() )
273     return aResShape;
274
275   Handle(HYDROData_ShapesGroup) aGroup =
276     Handle(HYDROData_ShapesGroup)::DownCast( theGroups.Value( theGroupId ) );
277   if ( aGroup.IsNull() )
278     return aResShape;
279
280   TopTools_SequenceOfShape aGroupShapes;
281   aGroup->GetShapes( aGroupShapes );
282
283   if ( !aGroupShapes.IsEmpty() )
284     aResShape = aGroupShapes.First();
285
286   return aResShape;
287 }
288
289 TCollection_ExtendedString HYDROData_Tool::toExtString( const QString& theStr )
290 {
291   TCollection_ExtendedString aRes;
292   if( !theStr.isEmpty() )
293   {
294           Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
295           memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
296           ((short*)extStr)[theStr.length()] = '\0';
297     aRes = TCollection_ExtendedString( extStr );
298           delete [] extStr;
299   }
300   return aRes;
301 }
302
303 QString HYDROData_Tool::toQString( const TCollection_ExtendedString& theStr )
304 {
305   return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
306 }