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