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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
28 #include <QStringList>
29 #include <QTextStream>
30 #include <BRep_Tool.hxx>
31 #include <BRepAdaptor_Surface.hxx>
32 #include <BRepTopAdaptor_FClass2d.hxx>
34 #include <Geom_Curve.hxx>
36 #include <Quantity_Color.hxx>
37 #include <TopExp_Explorer.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Wire.hxx>
45 static int aMaxNameId = std::numeric_limits<int>::max();
47 void HYDROData_Tool::WriteStringsToFile( QFile& theFile,
48 const QStringList& theStrings,
49 const QString& theSep )
51 if ( !theFile.isOpen() || theStrings.isEmpty() )
54 QString aWriteStr = theStrings.join( theSep );
55 if ( aWriteStr.isEmpty() )
58 QTextStream anOutStream( &theFile );
59 anOutStream << aWriteStr << theSep << theSep;
62 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
63 const QString& thePrefix,
64 const QStringList& theUsedNames,
65 const bool theIsTryToUsePurePrefix )
67 QStringList aNamesList( theUsedNames );
69 // Collect all used names in the document
70 HYDROData_Iterator anIter( theDoc );
71 for( ; anIter.More(); anIter.Next() )
73 Handle(HYDROData_Entity) anObject = anIter.Current();
74 if( anObject.IsNull() )
77 QString anObjName = anObject->GetName();
78 if ( anObjName.isEmpty() )
81 aNamesList.append( anObjName );
86 if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
90 while( anId < aMaxNameId )
92 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
94 // check that there are no other objects with the same name in the document
95 if ( !aNamesList.contains( aName ) )
103 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
105 if ( theObject.IsNull() )
108 return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
109 theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
112 void HYDROData_Tool::UpdateChildObjectName( const QString& theOldStr,
113 const QString& theNewStr,
114 const Handle(HYDROData_Entity)& theObject )
116 if ( theObject.IsNull() )
119 QString anObjName = theObject->GetName();
120 if ( theOldStr.isEmpty() )
122 while ( anObjName.startsWith( '_' ) )
123 anObjName.remove( 0, 1 );
125 anObjName.prepend( theNewStr + "_" );
127 else if ( anObjName.startsWith( theOldStr ) )
129 anObjName.replace( 0, theOldStr.length(), theNewStr );
134 theObject->SetName( anObjName );
137 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
138 const QString& thePrefix )
140 QString aName = thePrefix;
141 if ( !theTreatedObjects.contains( aName ) )
145 while( anId < aMaxNameId )
147 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
149 // check that there are no other objects with the same name
150 if ( !theTreatedObjects.contains( aName ) )
156 //======================================================================================================
157 TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& theXY, const TopoDS_Face& theFace )
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 );
173 double HYDROData_Tool::GetAltitudeForEdge( const TopoDS_Edge& theEdge,
174 const gp_XY& thePoint,
175 double theParameterTolerance,
176 double theSquareDistanceTolerance,
177 double theInvalidAltitude )
179 double aFirst, aLast;
180 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
181 if( aCurve.IsNull() )
182 return theInvalidAltitude;
184 gp_Pnt aFirstPnt, aLastPnt;
186 aCurve->D0( aFirst, aFirstPnt );
187 aCurve->D0( aLast, aLastPnt );
189 gp_Pnt2d aFirstPnt2d( aFirstPnt.X(), aFirstPnt.Y() );
190 gp_Pnt2d aLastPnt2d( aLastPnt.X(), aLastPnt.Y() );
192 double aFirstDist = 0;
193 double aLastDist = aFirstPnt2d.SquareDistance( aLastPnt2d );
194 double aNecDist = aFirstPnt2d.SquareDistance( thePoint );
196 while( fabs( aLast - aFirst ) > theParameterTolerance )
198 double aMid = ( aFirst + aLast ) / 2;
200 aCurve->D0( aMid, aMidPnt );
201 double aDist = aFirstPnt2d.SquareDistance( gp_Pnt2d( aMidPnt.X(), aMidPnt.Y() ) );
203 if( aDist < aNecDist )
209 double aMid = ( aFirst + aLast ) / 2;
211 aCurve->D0( aMid, aMidPnt );
213 gp_Pnt2d aMidPnt2d( aMidPnt.X(), aMidPnt.Y() );
214 if( aMidPnt2d.SquareDistance( thePoint ) < theSquareDistanceTolerance )
217 return theInvalidAltitude;
220 double HYDROData_Tool::GetAltitudeForWire( const TopoDS_Wire& theWire,
221 const gp_XY& thePoint,
222 double theParameterTolerance,
223 double theSquareDistanceTolerance,
224 double theInvalidAltitude )
226 TopExp_Explorer anExp( theWire, TopAbs_EDGE );
227 for( ; anExp.More(); anExp.Next() )
229 double anAltitude = GetAltitudeForEdge( TopoDS::Edge( anExp.Current() ), thePoint,
230 theParameterTolerance, theSquareDistanceTolerance, theInvalidAltitude );
231 if( anAltitude != theInvalidAltitude )
234 return theInvalidAltitude;
237 TopoDS_Shape HYDROData_Tool::getFirstShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups,
238 const int theGroupId )
240 TopoDS_Shape aResShape;
241 if ( theGroupId < 1 || theGroupId > theGroups.Length() )
244 Handle(HYDROData_ShapesGroup) aGroup =
245 Handle(HYDROData_ShapesGroup)::DownCast( theGroups.Value( theGroupId ) );
246 if ( aGroup.IsNull() )
249 TopTools_SequenceOfShape aGroupShapes;
250 aGroup->GetShapes( aGroupShapes );
252 if ( !aGroupShapes.IsEmpty() )
253 aResShape = aGroupShapes.First();
258 TCollection_ExtendedString HYDROData_Tool::toExtString( const QString& theStr )
260 TCollection_ExtendedString aRes;
261 if( !theStr.isEmpty() )
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 );
272 QString HYDROData_Tool::toQString( const TCollection_ExtendedString& theStr )
274 return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
277 Quantity_Color HYDROData_Tool::toOccColor( const QColor& theColor )
279 double r = theColor.red() / 255.0;
280 double g = theColor.green() / 255.0;
281 double b = theColor.blue() / 255.0;
283 return Quantity_Color( r, g, b, Quantity_TOC_RGB );
286 QColor HYDROData_Tool::toQtColor( const Quantity_Color& theColor )
288 int r = 255 * theColor.Red();
289 int g = 255 * theColor.Green();
290 int b = 255 * theColor.Blue();
291 return QColor( r, g, b );
294 bool HYDROData_Tool::IsNan( double theValue )
297 return _isnan( theValue );
299 return isnan( theValue );
303 bool HYDROData_Tool::IsInf( double theValue )
306 return (!_finite( theValue ) );
308 return isinf( theValue );
312 std::ostream& operator<<( std::ostream& theStream, const QString& theText )
314 theStream << theText.toStdString();
318 std::ostream& operator<<( std::ostream& theStream, const QColor& theColor )
320 theStream << "[" << theColor.red() << ", " << theColor.green() << ", " << theColor.blue() << "]";
324 std::ostream& operator<<( std::ostream& theStream, const TopoDS_Shape& theShape )
326 theStream << "[" << theShape.TShape().operator->() << "]";
330 std::ostream& operator<<( std::ostream& theStream, const TopoDS_Face& theFace )
332 theStream << "[" << theFace.TShape().operator->() << "]";
336 std::ostream& operator<<( std::ostream& theStream, const gp_XY& theXY )
338 theStream << "(" << theXY.X() << "; " << theXY.Y() << ")";
342 bool operator == ( const gp_XY& thePoint1, const gp_XY& thePoint2 )
344 const double EPS = 1E-3;
346 fabs( thePoint1.X() - thePoint2.X() ) < EPS &&
347 fabs( thePoint1.Y() - thePoint2.Y() ) < EPS;