// File:	Extrema_GenExtSS.cxx
// Created:	Tue Jul 18 08:21:34 1995
// Author:	Modelistation
//		<model@metrox>

#include <Extrema_GenLocateExtSS.ixx>

#include <Extrema_FuncExtSS.hxx>
#include <math_Vector.hxx>
#include <math_FunctionSetRoot.hxx>
#include <math_NewtonFunctionSetRoot.hxx>

//=======================================================================
//function : Extrema_GenLocateExtSS
//purpose  : 
//=======================================================================

 Extrema_GenLocateExtSS::Extrema_GenLocateExtSS()
{
}

//=======================================================================
//function : Extrema_GenLocateExtSS
//purpose  : 
//=======================================================================

 Extrema_GenLocateExtSS::Extrema_GenLocateExtSS(const Adaptor3d_Surface& S1, 
						const Adaptor3d_Surface& S2, 
						const Standard_Real U1, 
						const Standard_Real V1, 
						const Standard_Real U2, 
						const Standard_Real V2, 
						const Standard_Real Tol1, 
						const Standard_Real Tol2)
{
  Perform(S1,S2,U1,V1,U2,V2,Tol1,Tol2);
}

//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

void Extrema_GenLocateExtSS::Perform(const Adaptor3d_Surface& S1, 
				     const Adaptor3d_Surface& S2, 
				     const Standard_Real U1, 
				     const Standard_Real V1, 
				     const Standard_Real U2, 
				     const Standard_Real V2, 
				     const Standard_Real Tol1, 
				     const Standard_Real Tol2)
{
  myDone = Standard_False;

  Standard_Real Uinf1, Usup1, Vinf1, Vsup1;
  Uinf1 = S1.FirstUParameter();
  Usup1 = S1.LastUParameter();
  Vinf1 = S1.FirstVParameter();
  Vsup1 = S1.LastVParameter();

  if ((U1 < Uinf1) || (U1 > Usup1) ||
      (V1 < Vinf1) || (V1 > Vsup1)) { Standard_DomainError::Raise(); }

  Standard_Real Uinf2, Usup2, Vinf2, Vsup2;
  Uinf2 = S2.FirstUParameter();
  Usup2 = S2.LastUParameter();
  Vinf2 = S2.FirstVParameter();
  Vsup2 = S2.LastVParameter();

  if ((U2 < Uinf2) || (U2 > Usup2) ||
      (V2 < Vinf2) || (V2 > Vsup2)) { Standard_DomainError::Raise(); }

  Extrema_FuncExtSS F (S1,S2);
  math_Vector Tol(1, 4), Start(1, 4), BInf(1, 4), BSup(1, 4);
  Tol(1) = Tol1;
  Tol(2) = Tol1;
  Tol(3) = Tol2;
  Tol(4) = Tol2;
  Start(1) = U1;
  Start(2) = V1;
  Start(3) = U2;
  Start(4) = V2;
  BInf(1) = Uinf1;
  BInf(2) = Vinf1;
  BInf(3) = Uinf2;
  BInf(4) = Vinf2;
  BSup(1) = Usup1;
  BSup(2) = Vsup1;
  BSup(3) = Usup2;
  BSup(4) = Vsup2;

  Standard_Integer option = 1;
  if (option == 1) {
    math_FunctionSetRoot SR1 (F, Start,Tol, BInf, BSup);
    if (!SR1.IsDone()) { return; }

  }
  else {
    math_NewtonFunctionSetRoot SR (F, Tol, 1.e-12);
    SR.Perform(F, Start, BInf, BSup);
    if (!SR.IsDone()) { return; }
  }

  myValue = F.Value(1);
  myPoint1 = F.PointOnS1(1);
  myPoint2 = F.PointOnS2(1);
  myDone = Standard_True;

}

//=======================================================================
//function : IsDone
//purpose  : 
//=======================================================================

Standard_Boolean Extrema_GenLocateExtSS::IsDone() const 
{
  return myDone;
}

//=======================================================================
//function : Value
//purpose  : 
//=======================================================================

Standard_Real Extrema_GenLocateExtSS::Value() const 
{
  if (!IsDone()) { StdFail_NotDone::Raise(); }
  return myValue;
}

//=======================================================================
//function : PointOnS1
//purpose  : 
//=======================================================================

const Extrema_POnSurf& Extrema_GenLocateExtSS::PointOnS1() const 
{
  if (!IsDone()) { StdFail_NotDone::Raise(); }
  return myPoint1;
}

//=======================================================================
//function : PointOnS2
//purpose  : 
//=======================================================================

const Extrema_POnSurf& Extrema_GenLocateExtSS::PointOnS2() const 
{
  if (!IsDone()) { StdFail_NotDone::Raise(); }
  return myPoint2;
}

