示例#1
0
double
circle::intersect(ray const& r) const
{
	double t;
	point3d v1, v2;
	v1=normalize(p1_-center_);
	v2=normalize(p2_-center_);
	point3d n=normalenvektor(v1, v2);
	double s=scaleproduct(n, center_);
	point3d dir=normalize(r.getDir());
	if (scaleproduct(n, dir)!=0)
	{
		double temp=(scaleproduct(n, r.getOrigin())+s)/scaleproduct(n, dir);
		point3d schnitt=r.getOrigin()+temp*dir;
		//std::cout<<"schnitt: "<<schnitt<<std::endl;
        if (is_inside(schnitt))
		{
		    //std::cout<<"Schnittpunkt mit dem Kreis"<<std::endl;
			return t=temp;
		}
		else
		{
		    //std::cout<<"Schnittpunkt mit Ebene aber nicht mit Kreis"<<std::endl;
			return t=-1;
		}

	}
	else
	{
	    //std::cout<<"Kein Schnittpunkt mit Kreisebene"<<std::endl;
		return t=-1;
	}
}
示例#2
0
文件: sphere.hpp 项目: mojocorp/gtl
        //! Intersect ray and sphere, returning true if there is an intersection.
        bool intersect(const ray<Type>& r, Type& tmin, Type& tmax) const
        {
            const vec3<Type> r_to_s = r.getOrigin() - m_center;

            //Compute A, B and C coefficients
            const Type A = r.getDirection().sqrLength();
            const Type B = 2.0f * r_to_s.dot(r.getDirection());
            const Type C = r_to_s.sqrLength() - m_radius * m_radius;

            //Find discriminant
            Type disc = B * B - 4.0 * A * C;

            // if discriminant is negative there are no real roots
            if (disc < 0.0)
                return false;

            disc = (Type)std::sqrt((double)disc);

            tmin = (-B + disc) / (2.0 * A);
            tmax = (-B - disc) / (2.0 * A);

            // check if we're inside it
            if ((tmin < 0.0 && tmax > 0) || (tmin > 0 && tmax < 0))
                return false;

            if (tmin > tmax)
                std::swap(tmin, tmax);

            return (tmin > 0);
        }