// Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
//
// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
//
// 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
//

// File:        GEOMAlgo_ShellSolid.cxx
// Created:     Wed Jan 12 12:49:45 2005
// Author:      Peter KURNEV
//              <pkv@irinox>
//
#include <GEOMAlgo_ShellSolid.hxx>

#include <Standard_Failure.hxx>

#include <gp_Pnt2d.hxx>
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>

#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Solid.hxx>

#include <BRep_Tool.hxx>
#include <BRepTools.hxx>

#include <TopTools_ListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopExp_Explorer.hxx>

#include <BOPTools_AlgoTools.hxx>

#include <TopTools_DataMapOfShapeListOfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <IntTools_Context.hxx>
#include <BOPDS_DS.hxx>
#include <BOPAlgo_Builder.hxx>

#include <GEOMAlgo_AlgoTools.hxx>

#include <Basics_OCCTVersion.hxx>

/////////////////////////////////////////////////////////////////////////
//=======================================================================
//class : GEOMAlgo_ShellSolidBuilder
//purpose  : 
//=======================================================================
class GEOMAlgo_ShellSolidBuilder : public BOPAlgo_Builder {
 public:
  Standard_EXPORT
    GEOMAlgo_ShellSolidBuilder();

  Standard_EXPORT
    virtual ~GEOMAlgo_ShellSolidBuilder();

 protected:
  Standard_EXPORT
#if OCC_VERSION_LARGE < 0x07070000
    virtual void PerformInternal(const BOPAlgo_PaveFiller& theFiller);
#else
    virtual void PerformInternal(const BOPAlgo_PaveFiller& theFiller,
                                 const Message_ProgressRange& theRange);
#endif
};

//=======================================================================
//function : GEOMAlgo_ShellSolidBuilder
//purpose  : 
//=======================================================================
GEOMAlgo_ShellSolidBuilder::GEOMAlgo_ShellSolidBuilder()
:
  BOPAlgo_Builder()
{
}
//=======================================================================
//function : ~GEOMAlgo_ShellSolidBuilder
//purpose  : 
//=======================================================================
GEOMAlgo_ShellSolidBuilder::~GEOMAlgo_ShellSolidBuilder()
{
}
//=======================================================================
//function : PerformInternal
//purpose  : 
//=======================================================================
#if OCC_VERSION_LARGE < 0x07070000
void GEOMAlgo_ShellSolidBuilder::PerformInternal(const BOPAlgo_PaveFiller& theFiller)
#else
void GEOMAlgo_ShellSolidBuilder::PerformInternal(const BOPAlgo_PaveFiller& theFiller,
                                                 const Message_ProgressRange& theRange)
#endif
{
  myPaveFiller=(BOPAlgo_PaveFiller*)&theFiller;
  myDS=myPaveFiller->PDS();
  myContext=myPaveFiller->Context();
  //
  // 1. CheckData
  CheckData();
  if (HasErrors()) {
    return;
  }
  //
  // 2. Prepare
  Prepare();
  if (HasErrors()) {
    return;
  }

#if OCC_VERSION_LARGE >= 0x07070000
  Message_ProgressScope aPS(theRange, "Building the result of Boolean operation", 100);
  //
  BOPAlgo_PISteps aSteps (PIOperation_Last);
  analyzeProgress (100, aSteps);
#endif

  // 3. Fill Images
  // 3.1 Vertice
#if OCC_VERSION_LARGE < 0x07070000
  FillImagesVertices();
#else
  FillImagesVertices(aPS.Next(aSteps.GetStep(PIOperation_TreatVertices)));
#endif
  if (HasErrors()) {
    return;
  }
  //
  BuildResult(TopAbs_VERTEX);
  if (HasErrors()) {
    return;
  }
  // 3.2 Edges
#if OCC_VERSION_LARGE < 0x07070000
  FillImagesEdges();
#else
  FillImagesEdges(aPS.Next(aSteps.GetStep(PIOperation_TreatEdges)));
#endif
  if (HasErrors()) {
    return;
  }
  //
  BuildResult(TopAbs_EDGE);
  if (HasErrors()) {
    return;
  } 
  //
  // 3.3 Wires
#if OCC_VERSION_LARGE < 0x07070000
  FillImagesContainers(TopAbs_WIRE);
#else
  FillImagesContainers(TopAbs_WIRE, aPS.Next(aSteps.GetStep(PIOperation_TreatWires)));
#endif
  if (HasErrors()) {
    return;
  }
  //
  BuildResult(TopAbs_WIRE);
  if (HasErrors()) {
    return;
  }
  
  // 3.4 Faces
#if OCC_VERSION_LARGE < 0x07070000
  FillImagesFaces();
#else
  FillImagesFaces(aPS.Next(aSteps.GetStep(PIOperation_TreatFaces)));
#endif
  if (HasErrors()) {
    return;
  }
  //
  BuildResult(TopAbs_FACE);
  if (HasErrors()) {
    return;
  }
}
/////////////////////////////////////////////////////////////////////////
//=======================================================================
//function : GEOMAlgo_ShellSolid
//purpose  :
//=======================================================================
GEOMAlgo_ShellSolid::GEOMAlgo_ShellSolid()
:
  GEOMAlgo_ShapeSolid()
{
}
//=======================================================================
//function : ~
//purpose  :
//=======================================================================
GEOMAlgo_ShellSolid::~GEOMAlgo_ShellSolid()
{
}
//=======================================================================
// function:
// purpose:
//=======================================================================
void GEOMAlgo_ShellSolid::Perform()
{
  //
  try {
    Standard_Integer aNbArgs, iRank, iErr, iBeg, iEnd, i, aNbSp;
    Standard_Real aTol;
    TopAbs_ShapeEnum aType;
    TopAbs_State aState;
    gp_Pnt aP;
    gp_Pnt2d aP2D;
    TopoDS_Face aF;
    //
    myLSIN.Clear();
    myLSOUT.Clear();
    myLSON.Clear();
    //
    aTol=1.e-7;
    //
    if (myDSFiller==NULL) {
      myErrorStatus=10;
      return;
    }
    if(myDSFiller->HasErrors()) {
      myErrorStatus=11;
      return;
    }
    //
    const BOPDS_DS& aDS=myDSFiller->DS();
    BOPDS_DS* pDS=(BOPDS_DS*)&aDS;
    const TopTools_ListOfShape& aLS=pDS->Arguments();
    //
    aNbArgs=aLS.Extent();
    if (aNbArgs!=2) {
      myErrorStatus=13;
      return;
    }
    //
    iRank=-1;
    const TopoDS_Shape& aObj=aLS.First();
    if (aObj.ShapeType()==TopAbs_SHELL) {
      iRank=0;
    }
    const TopoDS_Shape& aTool=aLS.Last();
    if (aTool.ShapeType()==TopAbs_SHELL) {
      iRank=1;
    }
    //
    if (iRank==-1) {
      myErrorStatus=14;
      return;
    }
    //
    Handle(IntTools_Context) aCtx=myDSFiller->Context();
    const BOPDS_IndexRange& aRange=pDS->Range(iRank);
    aRange.Indices(iBeg, iEnd);
    const TopoDS_Solid& aSolid=(!iRank) ? *((TopoDS_Solid*)&aTool) : *((TopoDS_Solid*)&aObj);
    //BRepClass3d_SolidClassifier& aSC=aCtx->SolidClassifier(aSolid);
    //
    //------------------------------ShellSolidBuilder
    GEOMAlgo_ShellSolidBuilder aSSB;
    //
    aSSB.PerformWithFiller(*myDSFiller);
    iErr=aSSB.HasErrors();
    if (iErr) {
      myErrorStatus=15;
      return;
    }
    //
    const TopTools_DataMapOfShapeListOfShape& aImages=aSSB.Images();
    //
    //-------------------------------
    for (i=iBeg; i<=iEnd; ++i) {
      const TopoDS_Shape& aS=pDS->Shape(i);
      aType=aS.ShapeType();
      if (aType!=TopAbs_FACE) {
        continue;
      }
      //
      aState=TopAbs_UNKNOWN;
      aF=*((TopoDS_Face*)&aS);
      //
      if (!aImages.IsBound(aS)) {
        iErr=GEOMAlgo_AlgoTools::PntInFace(aF, aP, aP2D);
        if (iErr) {
          myErrorStatus=16;
          return;
        }
        //
        aState=BOPTools_AlgoTools::ComputeState(aP, aSolid, aTol, aCtx);
      }
      else {
        const TopTools_ListOfShape& aLSp=aImages.Find(aS);
        aNbSp=aLSp.Extent();
        if (aNbSp>0) {
          continue;
        }
        //
        if (aNbSp==1) {
          aF=*((TopoDS_Face*)&aLSp.First());
        }
        //
        iErr=GEOMAlgo_AlgoTools::PntInFace(aF, aP, aP2D);
        if (iErr) {
          myErrorStatus=16;
          return;
        }
        //
        aState=BOPTools_AlgoTools::ComputeState(aP, aSolid, aTol, aCtx);
      }
      //----------
      if (aState==TopAbs_ON) {
        myLSON.Append(aF);
      }
      else if (aState==TopAbs_OUT) {
        myLSOUT.Append(aF);
      }
      else if (aState==TopAbs_IN) {
        myLSIN.Append(aF);
      } 
      //----------
    }//for (i=iBeg; i<=iEnd; ++i) {
    
  }// try
  catch (Standard_Failure&) {
    myErrorStatus=12;
  }
}