// This file is part of the Herezh++ application.
//
// The finite element software Herezh++ is dedicated to the field
// of mechanics for large transformations of solid structures.
// It is developed by Gérard Rio (APP: IDDN.FR.010.0106078.000.R.P.2006.035.20600)
// INSTITUT DE RECHERCHE DUPUY DE LÔME (IRDL) <https://www.irdl.fr/>.
//
// Herezh++ is distributed under GPL 3 license ou ultérieure.
//
// Copyright (C) 1997-2022 Université Bretagne Sud (France)
// AUTHOR : Gérard Rio
// E-MAIL  : gerardrio56@free.fr
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// For more information, please consult: <https://herezh.irdl.fr/>.

#include "Sphere.h"
#include "MathUtil.h"
#include "ParaGlob.h"

    // CONSTRUCTEURS :
// par defaut
Sphere::Sphere ()  :
  centre(3),rayon(0.)
   { if (ParaGlob::Dimension()!= 3)
	   { cout << "\nErreur : la dimension n'est pas 3 et on veut utiliser une sphere ???  ";
		 cout << "\nSphere::Sphere ()" << endl;
		 Sortie(1);
	    };
    };
// avec les datas
Sphere::Sphere ( const Coordonnee& B, const double& r):
  centre(B),rayon(r)
 { if (ParaGlob::Dimension()!= 3)
	   { cout << "\nErreur : la dimension n'est pas 3 et on veut utiliser une sphere ???  ";
		 cout << "\nSphere::Sphere (.." << endl;
		 Sortie(1);
	    };
   if (rayon < 0.)
   	{ cout << "\n erreur dans la construction d'une sphere, le rayon " << rayon
   	       << " est negatif !! ";
   	  Sortie(1);
   	};
   };
// avec la dimension
Sphere::Sphere (int dim):
  centre(dim),rayon(0.)
 { if (ParaGlob::Dimension()!= 3)
	   { cout << "\nErreur : la dimension n'est pas 3 et on veut utiliser une sphere ???  ";
		 cout << "\nSphere::Sphere (.." << endl;
		 Sortie(1);
	    };
   if (dim != 3)
	   { cout << "\nErreur : la dimension demandee n'est pas 3 et on veut utiliser une sphere ???  ";
		 cout << "\nSphere::Sphere (.." << endl;
		 Sortie(1);
	    };
   };
    // de copie
Sphere::Sphere ( const Sphere& a):
  centre(a.centre),rayon(a.rayon)
   {};

    // DESTRUCTEUR :
Sphere::~Sphere () {};

// surcharge des operator
Sphere& Sphere::operator = ( const Sphere & sph)
 { centre = sph.centre; rayon = sph.rayon; return *this;  };

// METHODES PUBLIQUES :

// change le centre de la sphere
void Sphere::Change_centre( const Coordonnee& B)
 {
    #ifdef MISE_AU_POINT
     if  (B.Dimension() != centre.Dimension())
	   { cout << "\nErreur : les dimensions du centre et du vecteur ne sont pas identique !";
	     cout <<"\ndim B = " <<B.Dimension() <<", dim centre =" << centre.Dimension();
		 cout << "\nSphere::Change_centre( const Coordonnee& B)" << endl;
		 Sortie(1);
		};
    #endif
    centre = B;
  };

// change le rayon de la sphere
void Sphere::Change_rayon( const double & r)
   {if (r < 0.)
     	{ cout << "\n erreur dans la construction d'une sphere, le rayon " << r
     	       << " est negatif !! "
     	       << "\n Sphere::Change_rayon( const double & r) ";
     	};
   	rayon  = r;
   };

// change toutes les donnees
void Sphere::change_donnees( const Coordonnee& B, const double& r)
 {
    #ifdef MISE_AU_POINT
     if  (B.Dimension() != centre.Dimension())
	   { cout << "\nErreur : les dimensions du centre et du vecteur ne sont pas identique !";
	     cout <<"\ndim B = " <<B.Dimension() <<", dim centre =" << centre.Dimension();
		 cout << "\nSphere::change_donnees( const Coordonnee& B, const double& r)" << endl;
		 Sortie(1);
		};
    #endif
    centre = B;
    if (r < 0.)
     	{ cout << "\n erreur dans la construction d'une sphere, le rayon " << r
     	       << " est negatif !! "
     	       << "\n Sphere::Change_rayon( const double & r) ";
		  Sortie(1);
     	};
   	rayon  = r;
  };

// calcul les deux intercections  M1 et M2  d'une droite avec la sphere,
// ramene  0 s'il n'y a pas d'intersection, ramene -1 si l'intersection
// ne peut pas etre calculee
// et 1 s'il y a deux points d'intercection
int Sphere::Intersection( const Droite & dr,Coordonnee& M1, Coordonnee& M2)
	{ // C le centre de la sphère, H la projection de C sur la droite
	  // M1 et M2 les deux intersections potentielles
	  const Coordonnee& A = dr.PointDroite(); // pour simplifier
	  const Coordonnee& U = dr.VecDroite();
	  Coordonnee CA = dr.PointDroite() - centre;
	  double ah = - CA * U;
	  Coordonnee CH = CA + ah * U;
	  double ch_2 = CH * CH;
	  if (sqrt(ch_2) > rayon)
	    return 0; // cas pas d'intersection
	  // sinon
	  double hm = sqrt(rayon*rayon - ch_2);
	  M1 = centre + CH + hm * U;
	  M2 = centre + CH - hm * U;
	  return 1;
	};

// calcul la distance d'un point à la sphere
double Sphere::Distance_a_sphere(const Coordonnee& M) const
	{ return Dabs((centre - M).Norme()-rayon);
	};

// projection d'un point M sur la parois de la sphère
// dans le cas où M = le centre de la sphère, la projection n'est pas
// possible, dans ce cas projection_ok = false en retour, sinon true
Coordonnee Sphere::Projete(const Coordonnee& M,bool& projection_ok) const
{ // on commence par calculer la projection du point sur l'axe
  Coordonnee CM = M-centre;
  double dist_centre = CM.Norme();
  if (dist_centre < ConstMath::pasmalpetit)
  	{projection_ok = false;// on signale que la projection n'est pas possible
  	 Coordonnee toto; // on fait l'opération en deux temps pour linux
     return (toto);// on ramène un point par défaut
  	}
  else
  	{projection_ok = true;
     Coordonnee P = centre + CM * (rayon / dist_centre );
     return P;
  	};
 };

// surcharge de l'operateur de lecture
istream & operator >> (istream & entree, Sphere & sph)
  { // vérification du type
    string nom;
    entree >> nom;
   #ifdef MISE_AU_POINT
    if (nom != "_sphere_")
		{	cout << "\nErreur, en lecture d'une instance  sphere "
		         << " on attendait _sphere_ et on a lue: " << nom ;
			cout << "istream & operator >> (istream & entree, Sphere & sph)\n";
			Sortie(1);
		};
   #endif
    // puis lecture des différents éléments
    entree >> nom >> sph.centre >> nom >> sph.rayon;
    return entree;
  };

// surcharge de l'operateur d'ecriture
ostream & operator << ( ostream & sort,const  Sphere & sph)
  { // tout d'abord un indicateur donnant le type
    sort << " _sphere_ " ;
    // puis les différents éléments
    sort << "\n C= " << sph.centre << " r= " << sph.rayon << " ";
    return sort;
  };