#include "stdafx.h"
#include "Sphere3D.h"
#include "GSLutilities.h"
CSphere3D::CSphere3D(void)
{
Center.X = 0.0;
Center.Y = 0.0;
Center.Z = 0.0;
Radius = 1.0;
}
CSphere3D::CSphere3D(double dX, double dY, double dZ, double dRadius)
{
Center.X = dX;
Center.Y = dY;
Center.Z = dZ;
Radius = dRadius;
}
CSphere3D::CSphere3D(CPoint3D& dOrigin, double dRadius)
{
Center = dOrigin;
Radius = dRadius;
}
CSphere3D::CSphere3D(const CSphere3D& ObjSrc)
{
Center = ObjSrc.Center;
Radius = ObjSrc.Radius;
}
const CSphere3D& CSphere3D::operator=(CSphere3D const& ObjSrc)
{
if ( this != &ObjSrc ) // Prevent self assignment
{
// Build the specific copy
Center = ObjSrc.Center;
Radius = ObjSrc.Radius;
}
return *this;
}
CSphere3D::~CSphere3D(void)
{
}
bool CSphere3D::IsIntersection(CLine3D &Line)
{
CPoint3D Diff;
Diff = Line.Origin - Center;
double a, b, c;
a = 1.0;
b = 2.0 * (CPoint3D::dotprod(Line.Origin,Line.UnitVersor) - CPoint3D::dotprod(Line.UnitVersor,Center));
c = CPoint3D::dotprod(Diff,Diff) - Radius*Radius;
double t1, t2;
t1 = t2 = 0.0;
return quadratic(a,b,c,t1,t2,0.0,100.0);
}
void CSphere3D::Intersection(CLine3D &Line, CPoint3D& P1, CPoint3D& P2)
{
if ( !IsIntersection(Line) )
return;
CPoint3D Diff;
Diff = Line.Origin - Center;
double a, b, c;
a = 1.0;
b = 2.0 * (CPoint3D::dotprod(Line.Origin,Line.UnitVersor) - CPoint3D::dotprod(Line.UnitVersor,Center));
c = CPoint3D::dotprod(Diff,Diff) - Radius*Radius;
double t1, t2;
t1 = t2 = 0.0;
if ( quadratic(a,b,c,t1,t2,0.0,100.0) )
{
P1 = Line.MoveTo(t1);
P2 = Line.MoveTo(t2);
}
}
bool CSphere3D::IsInside(CPoint3D& P)
{
bool bres = false;
if ( (P-Center).Length() < Radius )
bres = true;
return bres;
}
bool CSphere3D::IsOnSphere(CPoint3D& P)
{
bool bres = false;
if ( fabs((P-Center).Length() - Radius) <= 1.0e-15 )
bres = true;
return bres;
}
bool CSphere3D::IsOutside(CPoint3D& P)
{
bool bres = false;
if ( (P-Center).Length() > Radius )
bres = true;
return bres;
}
double CSphere3D::Volume()
{
double res = 0.0;
res = (4.0/3.0) * acos(-1.0) * Radius * Radius * Radius;
return res;
}
double CSphere3D::Surface()
{
double res = 0.0;
res = acos(-1.0) * Radius * Radius;
return res;
}
double CSphere3D::Diameter()
{
double res = 0.0;
res = 2.0 * Radius;
return res;
}