Salome HOME
Feature #102: Display of Lambert coordinates.
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Utils.cxx
index 12d7ee7d84cf92d6a17a80aef4630e1eaf760122..2a29176e94c899b25fc16fff340a6ac7352b3fba 100644 (file)
@@ -18,6 +18,8 @@
 //
 
 #include "CurveCreator_Utils.h"
+#include "CurveCreator.hxx"
+#include "CurveCreator_UtilsICurve.hxx"
 
 #include <GEOMUtils.hxx>
 
@@ -27,6 +29,7 @@
 #include <TopoDS_Vertex.hxx>
 #include <TopoDS_Wire.hxx>
 #include <TopoDS_Edge.hxx>
+#include <TopoDS_Compound.hxx>
 
 #include <AIS_ListOfInteractive.hxx>
 #include <AIS_ListIteratorOfListOfInteractive.hxx>
 #include <Geom_Line.hxx>
 
 #include <TopExp_Explorer.hxx>
-#include <BRep_Tool.hxx>
 #include <GeomAPI_ProjectPointOnCurve.hxx>
 #include <SelectMgr_EntityOwner.hxx>
 
+#include <BRep_Tool.hxx>
+#include <BRep_Builder.hxx>
+#include <BRepBuilderAPI_MakeVertex.hxx>
+#include <BRepBuilderAPI_MakeEdge.hxx>
+#include <BRepBuilderAPI_MakeWire.hxx>
+
+#include <TColgp_HArray1OfPnt.hxx>
+#include <GeomAPI_Interpolate.hxx>
+
 #include <ProjLib.hxx>
 #include <ElSLib.hxx>
 
+#include "CurveCreator_ICurve.hxx"
+
 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
 const int    SCENE_PIXEL_TOLERANCE = 10;
 
@@ -71,43 +84,114 @@ gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) a
   return GEOMUtils::ConvertClickToPoint( x, y, aView );
 }
 
+void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
+                                         TopoDS_Shape& theShape )
+{
+  BRep_Builder aBuilder;
+  TopoDS_Compound aComp;
+  aBuilder.MakeCompound( aComp );
+  for( int iSection = 0 ; iSection < theCurve->getNbSections() ; iSection++ )
+  {
+    int theISection = iSection;
+
+    CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
+    int aPointSize = theCurve->getNbPoints( theISection );
+    bool aSectIsClosed = theCurve->isClosed( theISection );
+    bool isPolyline = aSectType == CurveCreator::Polyline;
+    int iPoint = 0;
+    gp_Pnt aPrevPoint, aPoint;
+    if ( aPointSize == 1 ) {
+      CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
+      TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
+      aBuilder.Add( aComp, aVertex );
+    }
+    else if ( aPointSize > 1 ) {
+      Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aPointSize);
+      int aHIndex = 1;
+
+      TopoDS_Edge aPointEdge;
+      TopoDS_Vertex aVertex;
+      CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
+      aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
+      aBuilder.Add( aComp, aVertex );
+      aHCurvePoints->SetValue(aHIndex++, aPoint);
+      aPrevPoint = aPoint;
+      iPoint++;
+      for( ; iPoint < aPointSize; iPoint++ ) {
+        CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
+        aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
+        aBuilder.Add( aComp, aVertex );
+        aHCurvePoints->SetValue(aHIndex++, aPoint);
+        if ( isPolyline ) {
+          aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
+          aBuilder.Add( aComp, aPointEdge );
+        }
+        aPrevPoint = aPoint;
+      }
+      if( aSectIsClosed && ( aPointSize > 2 ) ) {
+        CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
+        aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
+        aBuilder.Add( aComp, aVertex );
+        if ( isPolyline ) {
+          aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
+          aBuilder.Add( aComp, aPointEdge );
+        }
+      }
+      if( !isPolyline ) {
+        // compute BSpline
+        Handle(Geom_BSplineCurve) aBSplineCurve;
+        GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
+        aGBC.Perform();
+        if ( aGBC.IsDone() )
+          aBSplineCurve = aGBC.Curve();
+        TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
+        TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
+        aBuilder.Add( aComp, aWire );
+      }
+    }
+  }
+  theShape = aComp;
+}
+
+class ComparePnt
+{
+public:
+  ComparePnt( const gp_Pnt& thePoint ) : myPoint( thePoint) {};
+  ~ComparePnt() {}
+
+  bool operator < ( const ComparePnt& theOtherPoint ) const
+  {
+    bool isLess = myPoint.X() < theOtherPoint.myPoint.X();
+    if ( !isLess && myPoint.X() == theOtherPoint.myPoint.X() ) {
+      isLess = myPoint.Y() < theOtherPoint.myPoint.Y();
+      if ( !isLess && myPoint.Y() == theOtherPoint.myPoint.Y() )
+        isLess = myPoint.Z() < theOtherPoint.myPoint.Z();
+    }
+    return isLess;
+  }
+private:
+  gp_Pnt myPoint;
+};
+
 std::list<float> CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext )
 {
   std::list<float> aSelectedPoints;
 
   gp_Pnt aPnt;
+  std::map<ComparePnt, int> aPntMap;
+
   for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
     TopoDS_Vertex aVertex;
     TopoDS_Shape aShape = theContext->SelectedShape();
     if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
       aVertex = TopoDS::Vertex( theContext->SelectedShape() );
-    else {
-      Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
-      if ( !anOwner.IsNull() ) {
-        Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
-        if ( !anAIS.IsNull() ) {
-          Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
-          if ( !aPoint.IsNull() )
-            aVertex = TopoDS::Vertex( aPoint->Vertex() );
-        }
-        if ( aVertex.IsNull() ) {
-          // the following happens if there are no points in the current curve, there is only a shape
-          /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
-          if ( aBrepOwner.IsNull() )
-            continue;
-          if ( aBrepOwner->HasShape() ) {
-            const TopoDS_Shape& aShape = aBrepOwner->Shape();
-            if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
-            {
-              aVertex = TopoDS::Vertex( aShape );
-            }
-          }*/
-        }
-      }
-    }
+
     if ( aVertex.IsNull() )
       continue;
     aPnt = BRep_Tool::Pnt( aVertex );
+    if ( aPntMap.find( aPnt ) != aPntMap.end() )
+      continue;
+    aPntMap[aPnt] = 0;
     aSelectedPoints.push_back( aPnt.X() );
     aSelectedPoints.push_back( aPnt.Y() );
     aSelectedPoints.push_back( aPnt.Z() );
@@ -119,12 +203,12 @@ std::list<float> CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveCo
 // function : setLocalPointContext
 // purpose  : Open/close the viewer local context
 //=======================================================================
-//#define USE_COMPOUND
+//#define USE_GLOBAL_SELECTION
 void CurveCreator_Utils::setLocalPointContext(
                                               Handle(AIS_InteractiveContext) theContext,
                                               const bool theOpen )
 {
-#ifdef USE_COMPOUND
+#ifdef USE_GLOBAL_SELECTION
   return;
 #endif
   if ( !theContext )