Salome HOME
Merge branch 'master' of https://git.salome-platform.org/git/modules/hydro
[modules/hydro.git] / src / HYDROData / HYDROData_Tool.cxx
index 333205a0e371be975b631e52c75e5e0991e767f0..7d1339241db68abd290f6b348fa52791943294ba 100644 (file)
@@ -1,38 +1,47 @@
+// Copyright (C) 2014-2015  EDF-R&D
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
-#include "HYDROData_Tool.h"
-
-#include "HYDROData_ArtificialObject.h"
-#include "HYDROData_Image.h"
-#include "HYDROData_Iterator.h"
-#include "HYDROData_NaturalObject.h"
-
-#include <TopTools_SequenceOfShape.hxx>
-#include <TopExp_Explorer.hxx>
-
+#include <HYDROData_Tool.h>
+#include <HYDROData_ArtificialObject.h>
+#include <HYDROData_Document.h>
+#include <HYDROData_Entity.h>
+#include <HYDROData_Iterator.h>
+#include <HYDROData_NaturalObject.h>
+#include <HYDROData_ShapesGroup.h>
+#include <QColor>
 #include <QFile>
 #include <QStringList>
 #include <QTextStream>
-
-#include <limits>
-#define CLASS2D 1
-#ifdef CLASS2D
-#include <gp_Pnt.hxx>
-#include <gp_Vec.hxx>
-#include <gp_Pln.hxx>
-#include <TopExp.hxx>
-#include <Geom_Plane.hxx>
 #include <BRep_Tool.hxx>
-#include <BRepBuilderAPI_FindPlane.hxx>
+#include <BRepAdaptor_Surface.hxx>
+#include <BRepTopAdaptor_FClass2d.hxx>
+#include <ElSLib.hxx>
+#include <Geom_Curve.hxx>
+#include <gp_Pln.hxx>
+#include <Quantity_Color.hxx>
+#include <TopExp_Explorer.hxx>
 #include <TopoDS.hxx>
-#include <TopoDS_Vertex.hxx>
-#include <TopoDS_Wire.hxx>
 #include <TopoDS_Face.hxx>
-#include <TopoDS_Iterator.hxx>
-#include <TopTools_MapOfShape.hxx>
-#include <TColgp_SequenceOfVec.hxx>
-#include <TopTools_ShapeMapHasher.hxx>
-#undef _NCollection_MapHasher
-#endif
+#include <TopoDS_Shape.hxx>
+#include <TopoDS_Wire.hxx>
+#include <limits>
+#include <math.h>
+
 static int aMaxNameId = std::numeric_limits<int>::max();
 
 void HYDROData_Tool::WriteStringsToFile( QFile&             theFile,
@@ -50,38 +59,6 @@ void HYDROData_Tool::WriteStringsToFile( QFile&             theFile,
   anOutStream << aWriteStr << theSep << theSep;
 }
 
-void HYDROData_Tool::SetMustBeUpdatedObjects(
-  const Handle(HYDROData_Document)& theDoc  )
-{
-  bool anIsChanged = true;
-
-  // iterate until there is no changes because objects on all level of dependency must be updated
-  while ( anIsChanged )
-  {
-    anIsChanged = false;
-
-    HYDROData_Iterator anIter( theDoc );
-    for ( ; anIter.More(); anIter.Next() )
-    {
-      Handle(HYDROData_Entity) anObject = anIter.Current();
-      if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
-        continue;
-
-      HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
-      for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
-      {
-        Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
-        if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
-          continue;
-
-        anObject->SetToUpdate( true );
-        anIsChanged = true;
-        break;
-      }
-    }
-  }
-}
-
 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
                                             const QString&                    thePrefix,
                                             const QStringList&                theUsedNames,
@@ -177,63 +154,196 @@ QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTre
   return aName;
 }
 //======================================================================================================
-// the Face to be: planer, whitout holes
-#ifdef CLASS2D
-TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& thePnt2d, const TopoDS_Face& theFace )
+TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& theXY, const TopoDS_Face& theFace )
 {
   TopAbs_State aState(TopAbs_UNKNOWN);
   if(theFace.IsNull()) return aState;  
+  Standard_Real aTol = BRep_Tool::Tolerance(theFace);
+  BRepAdaptor_Surface Ads ( theFace, Standard_False );
+  Standard_Real toluv = Min ( Ads.UResolution(aTol), Ads.VResolution(aTol) ); 
+  const gp_Pln& aPlane = Ads.Surface().Plane();
+  gp_Pnt aPnt(theXY.X(), theXY.Y(), 0.);
+  Standard_Real aU1, aV1;
+  ElSLib::Parameters(aPlane,aPnt, aU1, aV1);
+  BRepTopAdaptor_FClass2d aClassifier( theFace, toluv ); 
+  aState = aClassifier.Perform( gp_Pnt2d(aU1, aV1), Standard_False );
+  return aState;
+}
+
+double HYDROData_Tool::GetAltitudeForEdge( const TopoDS_Edge& theEdge,
+                                           const gp_XY& thePoint,
+                                           double theParameterTolerance,
+                                           double theSquareDistanceTolerance,
+                                           double theInvalidAltitude )
+{
+  double aFirst, aLast;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
+  if( aCurve.IsNull() )
+    return theInvalidAltitude;
+
+  gp_Pnt aFirstPnt, aLastPnt;
+
+  aCurve->D0( aFirst, aFirstPnt );
+  aCurve->D0( aLast, aLastPnt );
+
+  gp_Pnt2d aFirstPnt2d( aFirstPnt.X(), aFirstPnt.Y() );
+  gp_Pnt2d aLastPnt2d( aLastPnt.X(), aLastPnt.Y() );
+
+  double aFirstDist = 0;
+  double aLastDist = aFirstPnt2d.SquareDistance( aLastPnt2d );
+  double aNecDist = aFirstPnt2d.SquareDistance( thePoint );
+
+  while( fabs( aLast - aFirst ) > theParameterTolerance )
+  {
+    double aMid = ( aFirst + aLast ) / 2;
+    gp_Pnt aMidPnt;
+    aCurve->D0( aMid, aMidPnt );
+    double aDist = aFirstPnt2d.SquareDistance( gp_Pnt2d( aMidPnt.X(), aMidPnt.Y() ) );
 
-#ifdef CLASS2D    
-  TopoDS_Wire aWire; int nb(0);
-  TopoDS_Iterator it(theFace);
-  for(;it.More();it.Next()) {
-         aWire = TopoDS::Wire(it.Value());
-         nb++;
+    if( aDist < aNecDist )
+      aFirst = aMid;
+    else
+      aLast = aMid;
   }
-  if(nb > 1 || aWire.IsNull()) return aState;
-  gp_Pln aPlane;
-  gp_Vec aNormal;
-  BRepBuilderAPI_FindPlane fndPlane (theFace, Precision::Confusion());
-  if(fndPlane.Found())
-    aPlane = fndPlane.Plane()->Pln();
+
+  double aMid = ( aFirst + aLast ) / 2;
+  gp_Pnt aMidPnt;
+  aCurve->D0( aMid, aMidPnt );
+
+  gp_Pnt2d aMidPnt2d( aMidPnt.X(), aMidPnt.Y() );
+  if( aMidPnt2d.SquareDistance( thePoint ) < theSquareDistanceTolerance )
+    return aMidPnt.Z();
   else
-    return aState;
-  aNormal = gp_Vec(aPlane.Axis().Direction());
-  if(theFace.Orientation() == TopAbs_REVERSED) 
-    aNormal.Reverse();
-  gp_Pnt aPoint = gp_Pnt (thePnt2d.X(), thePnt2d.Y(), 0);
-  TColgp_SequenceOfVec aSeq;
-  TopTools_MapOfShape aMap;
-  it.Initialize(aWire);
-  for (;it.More(); it.Next()) {
-       const TopoDS_Vertex& aVx = TopExp::FirstVertex(TopoDS::Edge(it.Value()), Standard_True);
-       if(!aMap.Add(aVx))
-         continue;
-       const gp_Pnt& aCurPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVx));
-       if(aPoint.IsEqual(aCurPnt,Precision::Confusion())) {
-      aState = TopAbs_ON;
-         return aState;
-       }
-       gp_Vec aVec (aPoint, aCurPnt);
-       aSeq.Append(aVec);
+    return theInvalidAltitude;
+}
+
+double HYDROData_Tool::GetAltitudeForWire( const TopoDS_Wire& theWire,
+                                           const gp_XY& thePoint,
+                                           double theParameterTolerance,
+                                           double theSquareDistanceTolerance,
+                                           double theInvalidAltitude )
+{
+  TopExp_Explorer anExp( theWire, TopAbs_EDGE );
+  for( ; anExp.More(); anExp.Next() )
+  {
+    double anAltitude = GetAltitudeForEdge( TopoDS::Edge( anExp.Current() ), thePoint,
+      theParameterTolerance, theSquareDistanceTolerance, theInvalidAltitude );
+    if( anAltitude != theInvalidAltitude )
+      return anAltitude;
   }
-  Standard_Real anAng(0.0);
-  for(int i = 1;i < aSeq.Length();i++) {
-    const gp_Vec& aV1 = aSeq.Value(i);
-       const gp_Vec& aV2 = aSeq.Value(i+1);
-        anAng += aV1.AngleWithRef(aV2, aNormal);
+  return theInvalidAltitude;
+}
+
+TopoDS_Shape HYDROData_Tool::getFirstShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups,
+                                                     const int                          theGroupId )
+{
+  TopoDS_Shape aResShape;
+  if ( theGroupId < 1 || theGroupId > theGroups.Length() )
+    return aResShape;
+
+  Handle(HYDROData_ShapesGroup) aGroup =
+    Handle(HYDROData_ShapesGroup)::DownCast( theGroups.Value( theGroupId ) );
+  if ( aGroup.IsNull() )
+    return aResShape;
+
+  TopTools_SequenceOfShape aGroupShapes;
+  aGroup->GetShapes( aGroupShapes );
+
+  if ( !aGroupShapes.IsEmpty() )
+    aResShape = aGroupShapes.First();
+
+  return aResShape;
+}
+
+TCollection_ExtendedString HYDROData_Tool::toExtString( const QString& theStr )
+{
+  TCollection_ExtendedString aRes;
+  if( !theStr.isEmpty() )
+  {
+         Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
+         memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
+         ((short*)extStr)[theStr.length()] = '\0';
+    aRes = TCollection_ExtendedString( extStr );
+         delete [] extStr;
   }
-  anAng += aSeq.Value(aSeq.Length()).AngleWithRef(aSeq.Value(1), aNormal);
-  if(abs(anAng) > Precision::Angular())
-    aState = TopAbs_IN;
-  else
-        aState = TopAbs_OUT;
+  return aRes;
+}
+
+QString HYDROData_Tool::toQString( const TCollection_ExtendedString& theStr )
+{
+  return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
+}
+
+Quantity_Color HYDROData_Tool::toOccColor( const QColor& theColor )
+{
+  double r = theColor.red() / 255.0;
+  double g = theColor.green() / 255.0;
+  double b = theColor.blue() / 255.0;
 
+  return Quantity_Color( r, g, b, Quantity_TOC_RGB );
+}
+
+QColor HYDROData_Tool::toQtColor( const Quantity_Color& theColor )
+{
+  int r = 255 * theColor.Red();
+  int g = 255 * theColor.Green();
+  int b = 255 * theColor.Blue();
+  return QColor( r, g, b );
+}
+
+bool HYDROData_Tool::IsNan( double theValue )
+{
+#ifdef WIN32
+  return _isnan( theValue );
 #else
-  BRepTopAdaptor_FClass2d aClassifier( theFace, Precision::Confusion() ); 
-  aState = aClassifier.Perform( gp_Pnt2d( thePoint ), Standard_False );
+  return isnan( theValue );
 #endif
-  return aState;
 }
-#endif
\ No newline at end of file
+
+bool HYDROData_Tool::IsInf( double theValue )
+{
+#ifdef WIN32
+  return (!_finite( theValue  ) );
+#else
+  return isinf( theValue );
+#endif  
+}
+
+std::ostream& operator<<( std::ostream& theStream, const QString& theText )
+{
+  theStream << theText.toStdString();
+  return theStream;
+}
+
+std::ostream& operator<<( std::ostream& theStream, const QColor& theColor )
+{
+  theStream << "[" << theColor.red() << ", " << theColor.green() << ", " << theColor.blue() << "]";
+  return theStream;
+}
+
+std::ostream& operator<<( std::ostream& theStream, const TopoDS_Shape& theShape )
+{
+  theStream << "[" << theShape.TShape().operator->() << "]";
+  return theStream;
+}
+
+std::ostream& operator<<( std::ostream& theStream, const TopoDS_Face& theFace )
+{
+  theStream << "[" << theFace.TShape().operator->() << "]";
+  return theStream;
+}
+
+std::ostream& operator<<( std::ostream& theStream, const gp_XY& theXY )
+{
+  theStream << "(" << theXY.X() << "; " << theXY.Y() << ")";
+  return theStream;
+}
+
+bool operator == ( const gp_XY& thePoint1, const gp_XY& thePoint2 )
+{
+  const double EPS = 1E-3;
+  return
+    fabs( thePoint1.X() - thePoint2.X() ) < EPS &&
+    fabs( thePoint1.Y() - thePoint2.Y() ) < EPS;
+
+}