//#####################################################################
// Function Intersection
//#####################################################################
template<class TV> bool DYADIC_IMPLICIT_OBJECT<TV>::
Intersection(RAY<TV>& ray,const T thickness,const ARRAY<T>& phi_nodes,const bool verbose) const
{
    bool save_semi_infinite=ray.semi_infinite;T save_t_max=ray.t_max;int save_aggregate=ray.aggregate_id;

    T t_start,t_end; 
    bool exit_intersection=false;int exit_aggregate=0;T exit_t_max=0;
    int intersect_box=INTERSECTION::Intersects(ray,box,thickness);
    int outside_box=box.Outside(ray.endpoint,thickness);

    if(outside_box && !intersect_box) return false; // missed the box
    else if(outside_box){ // intersected the box from the outside
        TV point=ray.Point(ray.t_max); // intersection point with the box
        if((*this)(point) <= 0) return true; // test the levelset right at the box
        point=box.Thickened(-4*thickness).Clamp(point); // moves the point inside the box
        if((*this)(point) <= 0) return true; // level set is on the edge of the box
        else{ // level set is not on the edge of the box
            t_start=ray.t_max; // intersection with the box
            ray.semi_infinite=save_semi_infinite;ray.t_max=save_t_max;ray.aggregate_id=save_aggregate; // box intersection doesn't count
            RAY<TV> new_ray(point,ray.direction);INTERSECTION::Intersects(new_ray,box,thickness);
            if(ray.semi_infinite) t_end=t_start+new_ray.t_max;
            else t_end=min(t_start+new_ray.t_max,ray.t_max);}}
    else if(!intersect_box){t_start=0;t_end=ray.t_max;} // intersected some object inside the box
    else{ // intersects the box from inside
        t_start=0;t_end=ray.t_max;
        exit_intersection=true;exit_t_max=ray.t_max;exit_aggregate=ray.aggregate_id; // save for exiting rays
        ray.semi_infinite=save_semi_infinite;ray.t_max=save_t_max;ray.aggregate_id=save_aggregate;} // box intersection doesn't count

    // set up marching
    DYADIC_IMPLICIT_OBJECT_ON_A_RAY<T_GRID> implicit_surface_on_a_ray(*this,ray);
    ITERATIVE_SOLVER<T> iterative_solver;iterative_solver.tolerance=Iterative_Solver_Tolerance<T>()*thickness;
    // start marching
    T t1=t_start+thickness;
    
    TV p1(ray.Point(t1));T_CELL* current_cell=levelset.grid.Leaf_Cell(p1);
    T phi1=levelset.Phi_From_Close_Cell(current_cell,p1,&phi_nodes),t2=t1+Integration_Step(phi1);
    // march through the line segment
    while(t2 <= t_end){
        TV p2(ray.Point(t2));
        if(!levelset.grid.Inside_Cell(current_cell,p2)){
            if(phi1<current_cell->DX().x*20) // 20 is a hack to avoid looking for neighbors
                current_cell=levelset.grid.Leaf_Cell_By_Neighbor_Path(current_cell,p2);
            else current_cell=levelset.grid.Leaf_Cell(p2);}
        else if(current_cell->Has_Children()) current_cell=levelset.grid.Leaf_Cell(p2);
        T phi2=levelset.Phi_From_Close_Cell(current_cell,p2,&phi_nodes);
        if(LEVELSET_UTILITIES<T>::Interface(phi1,phi2)){
            T_CELL* base_cell=levelset.grid.Base_Cell_By_Neighbor_Path(current_cell,p1);assert(base_cell);
            implicit_surface_on_a_ray.phi_nodes=&phi_nodes;
            implicit_surface_on_a_ray.last_block=new BLOCK_DYADIC<T_GRID>(levelset.grid,base_cell); // optimization
            ray.semi_infinite=false;ray.t_max=iterative_solver.Bisection_Secant_Root(implicit_surface_on_a_ray,t1,t2);ray.aggregate_id=-1;
            if(implicit_surface_on_a_ray.last_block){delete implicit_surface_on_a_ray.last_block;implicit_surface_on_a_ray.last_block=0;}
            return true;}
        else{t1=t2;phi1=phi2;t2=t1+Integration_Step(phi1);}} 
    // check the last piece of the line segment
    t2=t_end;T phi2=(*this)(ray.Point(t_end));
    if(LEVELSET_UTILITIES<T>::Interface(phi1,phi2)){ray.semi_infinite=false;ray.t_max=iterative_solver.Bisection_Secant_Root(implicit_surface_on_a_ray,t1,t2);ray.aggregate_id=-1;return true;}    
    else if(exit_intersection && phi2 <= 0){ray.semi_infinite=false;ray.t_max=exit_t_max; ray.aggregate_id=exit_aggregate; return true;} // exiting ray
    else return false;
}
//#####################################################################
// Function Closest_Non_Intersecting_Point
//#####################################################################
template<class T> bool Closest_Non_Intersecting_Point(RAY<VECTOR<T,3> >& ray,const TRIANGULATED_SURFACE<T>& surface, const T thickness_over_two)
{
    assert(surface.hierarchy);assert(surface.bounding_box);assert(surface.triangle_list);
    bool hit=false;T start_t,end_t;if(!INTERSECTION::Get_Intersection_Range(ray,*surface.bounding_box,start_t,end_t))return false;
    RANGE<VECTOR<T,3> > ray_box(ray.Point(start_t));ray_box.Enlarge_To_Include_Point(ray.Point(end_t));
    ARRAY<int> triangles_to_check;surface.hierarchy->Intersection_List(ray_box,triangles_to_check,4*thickness_over_two);
    for(int i=1;i<=triangles_to_check.m;i++){int k=triangles_to_check(i);if(INTERSECTION::Closest_Non_Intersecting_Point(ray,(*surface.triangle_list)(k),thickness_over_two)){ray.aggregate_id=k;hit=true;}}
    return hit;
}
//#####################################################################
// Function Intersects
//#####################################################################
template<class T> bool Rectangle_Intersects(RAY<VECTOR<T,3> >& ray,const PLANE<T>& plane,const PLANE<T>& bounding_plane1,const PLANE<T>& bounding_plane2,const PLANE<T>& bounding_plane3,const PLANE<T>& bounding_plane4,const T thickness_over_two)
{
    RAY<VECTOR<T,3> > ray_temp;ray.Save_Intersection_Information(ray_temp);
    if(INTERSECTION::Intersects(ray,plane,thickness_over_two)){
        VECTOR<T,3> point=ray.Point(ray.t_max);
        if(!bounding_plane1.Outside(point,thickness_over_two) && !bounding_plane2.Outside(point,thickness_over_two) && !bounding_plane3.Outside(point,thickness_over_two)
            && !bounding_plane4.Outside(point,thickness_over_two)) return true;
        else ray.Restore_Intersection_Information(ray_temp);}
    return false;
}
Example #4
0
void wiSPTree::getVisible(Node* node, RAY& frustum, CulledList& objects, int type){
	if(!node) return;
	int contain_type = frustum.intersects(node->box);
	if(!contain_type) return;
	else {
		for(Cullable* object : node->objects)
			if(frustum.intersects(object->bounds)){
				//object->lastSquaredDistMulThousand=(long)(wiMath::DistanceEstimated(object->bounds.getCenter(),frustum.center)*1000);
				objects.insert(object);
			}
		if(node->count){
			for (unsigned int i = 0; i<node->children.size(); ++i)
				getVisible(node->children[i],frustum,objects,type);
		}
	}
}
Example #5
0
void MOUSE::CalculateMappos(TERRAIN &terrain)
{
	//Get Mouse Ray
	D3DXMATRIX world;
	D3DXMatrixIdentity(&world);
	m_pDevice->SetTransform(D3DTS_WORLD, &world);
	RAY mRay = GetRay();

	float minDistance = 10000.0f;
	for(int i=0;i<(int)terrain.m_patches.size();i++)
	{
		if(mRay.Intersect(terrain.m_patches[i]->m_BBox) > 0.0f)
		{
			// Collect only the closest intersection
			BOOL hit;
			DWORD dwFace;
			float hitU, hitV, dist;
			D3DXIntersect(terrain.m_patches[i]->m_pMesh, &mRay.org, &mRay.dir, &hit, &dwFace, &hitU, &hitV, &dist, NULL, NULL);

			if(hit && dist < minDistance)
			{
				minDistance = dist;
				int tiles = dwFace / 2;		//Two faces to each map tile
				int tilesPerRow = terrain.m_patches[i]->m_mapRect.right - terrain.m_patches[i]->m_mapRect.left;
				int y = tiles / tilesPerRow, x = tiles - y * tilesPerRow;

				if(dwFace % 2 == 0)		//Hit upper left face
				{
					if(hitU > 0.5f)x++;
					else if(hitV > 0.5f)y++;
				}
				else					//Hit lower right face
				{
					if(hitU + hitV < 0.5f)y++;
					else if(hitU > 0.5f)x++;
					else {x++;y++;}
				}

				m_mappos.Set(terrain.m_patches[i]->m_mapRect.left + x, terrain.m_patches[i]->m_mapRect.top + y);
				m_ballPos = terrain.GetWorldPos(m_mappos);
			}			
		}
	}	
}
bool CTRIANGLE::Intersect( const RAY &aRay, HITINFO &aHitInfo ) const
{
    //!TODO: precalc this, improove it
#define ku s_modulo[m_k + 1]
#define kv s_modulo[m_k + 2]

    const SFVEC3F &O = aRay.m_Origin;
    const SFVEC3F &D = aRay.m_Dir;
    const SFVEC3F &A = m_vertex[0];

    const float lnd = 1.0f / (D[m_k] + m_nu * D[ku] + m_nv * D[kv]);
    const float t = (m_nd - O[m_k] - m_nu * O[ku] - m_nv * O[kv]) * lnd;

    if( !( (aHitInfo.m_tHit > t) && (t > 0.0f) ) )
        return false;

    const float hu = O[ku] + t * D[ku] - A[ku];
    const float hv = O[kv] + t * D[kv] - A[kv];
    const float beta = hv * m_bnu + hu * m_bnv;

    if( beta < 0.0f )
        return false;

    const float gamma = hu * m_cnu + hv * m_cnv;

    if( gamma < 0 )
        return false;

    const float v = gamma;
    const float u = beta;

    if( (u + v) > 1.0f )
        return false;

    if( glm::dot( D, m_n ) > 0.0f )
        return false;

    aHitInfo.m_tHit = t;
    aHitInfo.m_HitPoint = aRay.at( t );

    // interpolate vertex normals with UVW using Gouraud's shading
    aHitInfo.m_HitNormal = glm::normalize( (1.0f - u - v) * m_normal[0] +
                                            u * m_normal[1] +
                                            v * m_normal[2] );

    m_material->PerturbeNormal( aHitInfo.m_HitNormal, aRay, aHitInfo );

    aHitInfo.pHitObject = this;

    return true;
#undef ku
#undef kv
}
Example #7
0
void wiSPTree::getVisible(Node* node, RAY& frustum, CulledList& objects, SortType sort, CullStrictness type){
	if(!node) return;
	int contain_type = frustum.intersects(node->box);
	if(!contain_type) return;
	else {
		for(Cullable* object : node->objects)
			if(frustum.intersects(object->bounds)){

#ifdef SORT_SPTREE_CULL
				object->lastSquaredDistMulThousand=(long)(wiMath::Distance(object->bounds.getCenter(),frustum.origin)*1000);
				if (sort == SP_TREE_SORT_PAINTER)
					object->lastSquaredDistMulThousand *= -1;
#endif

				objects.insert(object);
			}
		if(node->count){
			for (unsigned int i = 0; i<node->children.size(); ++i)
				getVisible(node->children[i],frustum,objects, sort,type);
		}
	}
}
bool CVCYLINDER::Intersect( const RAY &aRay, HITINFO &aHitInfo ) const
{
    // Based on:
    // http://www.cs.utah.edu/~lha/Code%206620%20/Ray4/Cylinder.cpp
    // Ray-sphere intersection: geometric
    // /////////////////////////////////////////////////////////////////////////
    const double OCx_Start = aRay.m_Origin.x - m_center.x;
    const double OCy_Start = aRay.m_Origin.y - m_center.y;

    const double p_dot_p = OCx_Start * OCx_Start + OCy_Start * OCy_Start;

    const double a = (double)aRay.m_Dir.x * (double)aRay.m_Dir.x +
                     (double)aRay.m_Dir.y * (double)aRay.m_Dir.y;
    const double b = (double)aRay.m_Dir.x * (double)OCx_Start +
                     (double)aRay.m_Dir.y * (double)OCy_Start;
    const double c = p_dot_p - m_radius_squared;

    const float delta = (float)(b * b - a * c);

    bool hitResult = false;

    if( delta > FLT_EPSILON )
    {
        const float inv_a = 1.0 / a;

        const float sdelta = sqrtf( delta );
        const float t = (-b - sdelta) * inv_a;
        const float z = aRay.m_Origin.z + t * aRay.m_Dir.z;

        if( (z >= m_bbox.Min().z) &&
            (z <= m_bbox.Max().z) )
        {
            if( t < aHitInfo.m_tHit )
            {
                hitResult = true;
                aHitInfo.m_tHit = t;
            }
        }

        if( !hitResult )
        {
            const float t1 = (-b + sdelta) * inv_a;
            const float z1 = aRay.m_Origin.z + t1 * aRay.m_Dir.z;

            if( (z1 > m_bbox.Min().z ) &&
                (z1 < m_bbox.Max().z ) )
            {
                if( t1 < aHitInfo.m_tHit )
                {
                    hitResult = true;
                    aHitInfo.m_tHit = t1;
                }
            }
        }
    }

    if( hitResult )
    {
        aHitInfo.m_HitPoint = aRay.at( aHitInfo.m_tHit );

        const SFVEC2F hitPoint2D = SFVEC2F( aHitInfo.m_HitPoint.x,
                                            aHitInfo.m_HitPoint.y );

        aHitInfo.m_HitNormal = SFVEC3F( -(hitPoint2D.x - m_center.x) * m_inv_radius,
                                        -(hitPoint2D.y - m_center.y) * m_inv_radius,
                                        0.0f );

        m_material->PerturbeNormal( aHitInfo.m_HitNormal, aRay, aHitInfo );

        aHitInfo.pHitObject = this;
    }

    return hitResult;
}
Example #9
0
HRESULT APPLICATION::Render()
{
    // Clear the viewport
    m_pDevice->Clear(0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0L );
	
    // Begin the scene 
    if(SUCCEEDED(m_pDevice->BeginScene()))
    {
		for(int i=0;i<(int)m_objects.size();i++)
			m_objects[i].Render();
		
		//Find intersecting object closest to the camera
		D3DXMATRIX identity;
		D3DXMatrixIdentity(&identity);

		int object = -1;
		float bestDist = 100000.0f;
		for(int i=0;i<(int)m_objects.size() - 1;i++)
		{
			float dist = -1.0f; 
			
			//Do intersection tests
			if(m_intersectType == 0)
			{
				m_pDevice->SetTransform(D3DTS_WORLD, &m_objects[i].m_meshInstance.GetWorldMatrix());
				RAY ray = m_mouse.GetRay();
				dist = ray.Intersect(m_objects[i].m_meshInstance);
			}
			else if(m_intersectType == 1)
			{				
				m_pDevice->SetTransform(D3DTS_WORLD, &identity);
				RAY ray = m_mouse.GetRay();
				dist = ray.Intersect(m_objects[i].m_BBox);
			}
			else if(m_intersectType == 2)
			{
				m_pDevice->SetTransform(D3DTS_WORLD, &identity);
				RAY ray = m_mouse.GetRay();
				dist = ray.Intersect(m_objects[i].m_BSphere);
			}

			m_objects[i].RenderBoundingVolume(m_intersectType);

			if(dist >= 0.0f && dist < bestDist)
			{
				object = i;
				bestDist = dist;
			}
		}

		//Write m_mouse text
		if(object != -1)
		{
			RECT mr[] = {{m_mouse.x + 2, m_mouse.y + 24, 0, 0}, {m_mouse.x, m_mouse.y + 22, 0, 0}};
			m_pFontMouse->DrawText(NULL, m_objects[object].m_name.c_str(), -1, &mr[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
			m_pFontMouse->DrawText(NULL, m_objects[object].m_name.c_str(), -1, &mr[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffff0000);
		}

		RECT r[] = {{10, 10, 0, 0}, {10, 30, 0, 0}, {10, 50, 0, 0}};
		m_pFont->DrawText(NULL, "Mouse Wheel: Change Camera Radius", -1, &r[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		m_pFont->DrawText(NULL, "Arrows: Change Camera Angle", -1, &r[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		m_pFont->DrawText(NULL, "Space: Change Bounding Volume", -1, &r[2], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);

		RECT rc = {500, 10, 0, 0};
		if(m_intersectType == 0)
			m_pFont->DrawText(NULL, "Mesh Intersection Test", -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		else if(m_intersectType == 1)
			m_pFont->DrawText(NULL, "Box Intersection Test", -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		else if(m_intersectType == 2)
			m_pFont->DrawText(NULL, "Sphere Intersection Test", -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		
		//Draw m_mouse
		m_mouse.Paint();

        // End the scene.
		m_pDevice->EndScene();
		m_pDevice->Present(0, 0, 0, 0);
    }

	return S_OK;
}
//#####################################################################
// Function Intersection
//#####################################################################
template<class TV> bool IMPLICIT_OBJECT<TV>::
Intersection(RAY<TV>& ray,const T thickness) const
{
    bool save_semi_infinite=ray.semi_infinite;T save_t_max=ray.t_max;int save_aggregate=ray.aggregate_id;

    T t_start,t_end;
    bool exit_intersection=false;int exit_aggregate=0;T exit_t_max=0;
    int intersect_box=INTERSECTION::Intersects(ray,box,thickness);
    int outside_box=box.Outside(ray.endpoint,thickness);

    if(outside_box && !intersect_box) return false; // missed the box
    else if(outside_box){ // intersected the box from the outside
        TV point=ray.Point(ray.t_max); // intersection point with the box
        point=box.Thickened(-4*thickness).Clamp(point); // moves the point inside the box
        if((*this)(point) <= 0) return true; // level set is on the edge of the box
        else{ // level set is not on the edge of the box
            t_start=ray.t_max; // intersection with the box
            ray.semi_infinite=save_semi_infinite;ray.t_max=save_t_max;ray.aggregate_id=save_aggregate; // box intersection doesn't count
            RAY<TV> new_ray(point,ray.direction);INTERSECTION::Intersects(new_ray,box,thickness);
            if(ray.semi_infinite) t_end=t_start+new_ray.t_max;
            else t_end=min(t_start+new_ray.t_max,ray.t_max);}}
    else if(!intersect_box){t_start=0;t_end=ray.t_max;} // intersected some object inside the box
    else{ // intersects the box from inside
        t_start=0;t_end=ray.t_max;
        exit_intersection=true;exit_t_max=ray.t_max;exit_aggregate=ray.aggregate_id; // save for exiting rays
        ray.semi_infinite=save_semi_infinite;ray.t_max=save_t_max;ray.aggregate_id=save_aggregate;} // box intersection doesn't count

    if(!use_secondary_interpolation){
        // set up marching
        IMPLICIT_OBJECT_ON_A_RAY<IMPLICIT_OBJECT> implicit_surface_on_a_ray(*this,ray);
        ITERATIVE_SOLVER<T> iterative_solver;iterative_solver.tolerance=Iterative_Solver_Tolerance<T>()*thickness;
        // start marching
        T t1=t_start+thickness,phi1=(*this)(ray.Point(t1)),t2=t1+Integration_Step(phi1);
        // march through the line segment
        while(t2 <= t_end){
            T phi2=(*this)(ray.Point(t2));
            if(LEVELSET_UTILITIES<T>::Interface(phi1,phi2)){ray.semi_infinite=false;ray.t_max=iterative_solver.Bisection_Secant_Root(implicit_surface_on_a_ray,t1,t2);ray.aggregate_id=-1;return true;}
            else{t1=t2;phi1=phi2;t2=t1+Integration_Step(phi1);}}
        // check the last piece of the line segment
        t2=t_end;T phi2=(*this)(ray.Point(t_end));
        if(LEVELSET_UTILITIES<T>::Interface(phi1,phi2)){ray.semi_infinite=false;ray.t_max=iterative_solver.Bisection_Secant_Root(implicit_surface_on_a_ray,t1,t2);ray.aggregate_id=-1;return true;}
        else if(exit_intersection && phi2 <= 0){ray.semi_infinite=false;ray.t_max=exit_t_max;ray.aggregate_id=exit_aggregate;return true;} // exiting ray
        else return false;}
    else{ // use_secondary_interpolation
        // set up marching
        //IMPLICIT_OBJECT_ON_A_RAY_SECONDARY_INTERPOLATION<T> implicit_surface_on_a_ray(*this,ray); // TODO: we should probably be using this instead
        IMPLICIT_OBJECT_ON_A_RAY<IMPLICIT_OBJECT> implicit_surface_on_a_ray(*this,ray);
        ITERATIVE_SOLVER<T> iterative_solver;iterative_solver.tolerance=Iterative_Solver_Tolerance<T>()*thickness;
        // start marching
        T t1=t_start+thickness,phi1=(*this)(ray.Point(t1)),t2=t1+Integration_Step(phi1);
        // march through the line segment

        while(t2 <= t_end){
            T phi2=(*this)(ray.Point(t2));
            if(LEVELSET_UTILITIES<T>::Interface(phi1,phi2)){
                phi1=this->Phi_Secondary(ray.Point(t1));
                phi2=this->Phi_Secondary(ray.Point(t2));
                if(LEVELSET_UTILITIES<T>::Interface(phi1,phi2)){
                    ray.semi_infinite=false;
                    ray.t_max=iterative_solver.Bisection_Secant_Root(implicit_surface_on_a_ray,t1,t2);
                    ray.aggregate_id=-1;return true;}
                else{t1=t2;phi1=phi2;t2=t1+Integration_Step(phi1);}}
            else{t1=t2;phi1=phi2;t2=t1+Integration_Step(phi1);}}

        // check the last piece of the line segment
        t2=t_end;T phi2=(*this)(ray.Point(t2));
        if(LEVELSET_UTILITIES<T>::Interface(phi1,phi2)){
            phi1=this->Phi_Secondary(ray.Point(t1));
            phi2=this->Phi_Secondary(ray.Point(t2));
            if(LEVELSET_UTILITIES<T>::Interface(phi1,phi2)){
                ray.semi_infinite=false;
                ray.t_max=iterative_solver.Bisection_Secant_Root(implicit_surface_on_a_ray,t1,t2);
                ray.aggregate_id=-1;return true;}}
        if(exit_intersection && phi2 <= 0){ray.semi_infinite=false;ray.t_max=exit_t_max;ray.aggregate_id=exit_aggregate;return true;} // exiting ray
        else return false;}
}
Example #11
0
bool CROUNDSEG::Intersect( const RAY &aRay, HITINFO &aHitInfo ) const
{
    // Top / Botton plane
    // /////////////////////////////////////////////////////////////////////////
    float zPlanePos = aRay.m_dirIsNeg[2]? m_bbox.Max().z : m_bbox.Min().z;

    float tPlane    = ( zPlanePos - aRay.m_Origin.z) * aRay.m_InvDir.z;

    if( ( tPlane >= aHitInfo.m_tHit ) || ( tPlane < FLT_EPSILON ) )
        return false;   // Early exit

    SFVEC2F planeHitPoint2d( aRay.m_Origin.x + aRay.m_Dir.x * tPlane,
                             aRay.m_Origin.y + aRay.m_Dir.y * tPlane );

    float dSquared = m_segment.DistanceToPointSquared( planeHitPoint2d );

    if( dSquared <= m_radius_squared )
    {
        if( tPlane < aHitInfo.m_tHit )
        {
            aHitInfo.m_tHit = tPlane;
            //aHitInfo.m_HitPoint = SFVEC3F( planeHitPoint2d.x,
            //                               planeHitPoint2d.y,
            //                               aRay.m_Origin.z + aRay.m_Dir.z * tPlane );
            aHitInfo.m_HitNormal = SFVEC3F( 0.0f,
                                            0.0f,
                                            aRay.m_dirIsNeg[2]? 1.0f: -1.0f );
            aHitInfo.pHitObject = this;

            return true;
        }

        return false;
    }

    // Test LEFT / RIGHT plane
    // /////////////////////////////////////////////////////////////////////////
    float normal_dot_ray = glm::dot( m_plane_dir_right, aRay.m_Dir );

    if( normal_dot_ray < 0.0f ) // If the dot is neg, the it hits the plane
    {
        const float n_dot_ray_origin = glm::dot( m_plane_dir_right,
                                       m_center_right - aRay.m_Origin );
        const float t = n_dot_ray_origin / normal_dot_ray;

        if( t > 0.0f )
        {
            const SFVEC3F hitP = aRay.at( t );

            const SFVEC3F v = hitP - m_center_right;
            const float len = glm::dot( v, v );

            if( (len <= m_seglen_over_two_squared) &&
                    (hitP.z >= m_bbox.Min().z) &&
                    (hitP.z <= m_bbox.Max().z) )
            {
                if( t < aHitInfo.m_tHit )
                {
                    aHitInfo.m_tHit = t;
                    //aHitInfo.m_HitPoint = hitP;
                    aHitInfo.m_HitNormal = SFVEC3F( m_plane_dir_right.x,
                                                    m_plane_dir_right.y,
                                                    0.0f );
                    aHitInfo.pHitObject = this;

                    return true;
                }

                return false;
            }
        }
    }
    else
    {
        normal_dot_ray = glm::dot( m_plane_dir_left, aRay.m_Dir );

        if( normal_dot_ray < 0.0f ) // If the dot is neg, the it hits the plane
        {
            const float n_dot_ray_origin = glm::dot( m_plane_dir_left,
                                           m_center_left - aRay.m_Origin );
            const float t = n_dot_ray_origin / normal_dot_ray;

            if( t > 0.0f )
            {
                const SFVEC3F hitP = aRay.at( t );

                const SFVEC3F v = hitP - m_center_left;
                const float len = glm::dot( v, v );

                if( (len <= m_seglen_over_two_squared) &&
                        (hitP.z >= m_bbox.Min().z) &&
                        (hitP.z <= m_bbox.Max().z) )
                {
                    if( t < aHitInfo.m_tHit )
                    {
                        aHitInfo.m_tHit = t;
                        //aHitInfo.m_HitPoint = hitP;
                        aHitInfo.m_HitNormal = SFVEC3F( m_plane_dir_left.x,
                                                        m_plane_dir_left.y,
                                                        0.0f );
                        aHitInfo.pHitObject = this;

                        return true;
                    }

                    return false;
                }
            }
        }
    }

    // Based on:
    // http://www.cs.utah.edu/~lha/Code%206620%20/Ray4/Cylinder.cpp

    // Ray-sphere intersection: geometric
    // /////////////////////////////////////////////////////////////////////////

    const double OCx_Start = aRay.m_Origin.x - m_segment.m_Start.x;
    const double OCy_Start = aRay.m_Origin.y - m_segment.m_Start.y;

    const double p_dot_p_Start = OCx_Start * OCx_Start + OCy_Start * OCy_Start;

    const double a = (double)aRay.m_Dir.x * (double)aRay.m_Dir.x +
                     (double)aRay.m_Dir.y * (double)aRay.m_Dir.y;

    const double b_Start = (double)aRay.m_Dir.x * (double)OCx_Start +
                           (double)aRay.m_Dir.y * (double)OCy_Start;

    const double c_Start = p_dot_p_Start - m_radius_squared;

    const float delta_Start = (float)(b_Start * b_Start - a * c_Start);

    if( delta_Start > FLT_EPSILON )
    {
        const float sdelta = sqrtf( delta_Start );
        const float t = (-b_Start - sdelta) / a;
        const float z = aRay.m_Origin.z + t * aRay.m_Dir.z;

        if( (z >= m_bbox.Min().z) &&
                (z <= m_bbox.Max().z) )
        {
            if( t < aHitInfo.m_tHit )
            {
                aHitInfo.m_tHit = t;
                SFVEC2F hitPoint2D = aRay.at2D( t );
                //aHitInfo.m_HitPoint = aRay.at( t );
                aHitInfo.m_HitNormal = SFVEC3F(
                                           (hitPoint2D.x - m_segment.m_Start.x) * m_inv_radius,
                                           (hitPoint2D.y - m_segment.m_Start.y) * m_inv_radius,
                                           0.0f );
                aHitInfo.pHitObject = this;

                return true;
            }

            return false;
        }
    }

    const double OCx_End = aRay.m_Origin.x - m_segment.m_End.x;
    const double OCy_End = aRay.m_Origin.y - m_segment.m_End.y;

    const double p_dot_p_End = OCx_End * OCx_End + OCy_End * OCy_End;

    const double b_End = (double)aRay.m_Dir.x * (double)OCx_End +
                         (double)aRay.m_Dir.y * (double)OCy_End;

    const double c_End = p_dot_p_End - m_radius_squared;

    const float delta_End = (float)(b_End * b_End - a * c_End);

    if( delta_End > FLT_EPSILON )
    {
        const float sdelta = sqrtf( delta_End );
        const float t = (-b_End - sdelta) / a;
        const float z = aRay.m_Origin.z + t * aRay.m_Dir.z;

        if( (z >= m_bbox.Min().z) &&
                (z <= m_bbox.Max().z) )
        {
            if( t < aHitInfo.m_tHit )
            {
                aHitInfo.m_tHit = t;
                //aHitInfo.m_HitPoint = aRay.at( t );
                const SFVEC2F hitPoint2D = aRay.at2D( t );

                aHitInfo.m_HitNormal = SFVEC3F(
                                           (hitPoint2D.x - m_segment.m_End.x) * m_inv_radius,
                                           (hitPoint2D.y - m_segment.m_End.y) * m_inv_radius,
                                           0.0f );
                aHitInfo.pHitObject = this;

                return true;
            }

            return false;
        }
    }

    return false;
}
Example #12
0
bool CROUNDSEG::IntersectP( const RAY &aRay, float aMaxDistance ) const
{
    // Top / Botton plane
    // /////////////////////////////////////////////////////////////////////////
    const float zPlanePos = aRay.m_dirIsNeg[2]? m_bbox.Max().z : m_bbox.Min().z;

    const float tPlane    = ( zPlanePos - aRay.m_Origin.z) * aRay.m_InvDir.z;

    if( ( tPlane >= aMaxDistance) || ( tPlane < FLT_EPSILON ) )
        return false;   // Early exit

    const SFVEC2F planeHitPoint2d( aRay.m_Origin.x + aRay.m_Dir.x * tPlane,
                                   aRay.m_Origin.y + aRay.m_Dir.y * tPlane );

    const float dSquared = m_segment.DistanceToPointSquared( planeHitPoint2d );

    if( dSquared <= m_radius_squared )
    {
        if( tPlane < aMaxDistance )
            return true;

        return false;
    }

    // Since the IntersectP is used for shadows, we are simplifying the test
    // intersection and only consider the top/bottom plane of the segment
    return false;
#if 0
    // Test LEFT / RIGHT plane
    // /////////////////////////////////////////////////////////////////////////
    float normal_dot_ray = glm::dot( m_plane_dir_right, aRay.m_Dir );

    if( normal_dot_ray < 0.0f ) // If the dot is neg, the it hits the plane
    {
        float n_dot_ray_origin = glm::dot( m_plane_dir_right,
                                           m_center_right - aRay.m_Origin );
        float t = n_dot_ray_origin / normal_dot_ray;

        if( t > 0.0f )
        {
            SFVEC3F hitP = aRay.at( t );

            SFVEC3F v = hitP - m_center_right;
            float len = glm::dot( v, v );

            if( (len <= m_seglen_over_two_squared) &&
                    (hitP.z >= m_bbox.Min().z) && (hitP.z <= m_bbox.Max().z) )
            {
                if( t < aMaxDistance )
                    return true;

                return false;
            }
        }
    }
    else
    {
        normal_dot_ray = glm::dot( m_plane_dir_left, aRay.m_Dir );

        if( normal_dot_ray < 0.0f ) // If the dot is neg, the it hits the plane
        {
            const float n_dot_ray_origin = glm::dot( m_plane_dir_left,
                                           m_center_left - aRay.m_Origin );
            const float t = n_dot_ray_origin / normal_dot_ray;

            if( t > 0.0f )
            {
                SFVEC3F hitP = aRay.at( t );

                SFVEC3F v = hitP - m_center_left;
                float len = glm::dot( v, v );

                if( (len <= m_seglen_over_two_squared) &&
                        (hitP.z >= m_bbox.Min().z) && (hitP.z <= m_bbox.Max().z) )
                {
                    if( t < aMaxDistance )
                        return true;

                    return false;
                }
            }
        }
    }

    // Based on:
    // http://www.cs.utah.edu/~lha/Code%206620%20/Ray4/Cylinder.cpp

    // Ray-sphere intersection: geometric
    // /////////////////////////////////////////////////////////////////////////

    double OCx_Start = aRay.m_Origin.x - m_segment.m_Start.x;
    double OCy_Start = aRay.m_Origin.y - m_segment.m_Start.y;

    double p_dot_p_Start = OCx_Start * OCx_Start + OCy_Start * OCy_Start;

    double a = (double)aRay.m_Dir.x * (double)aRay.m_Dir.x +
               (double)aRay.m_Dir.y * (double)aRay.m_Dir.y;

    double b_Start = (double)aRay.m_Dir.x * (double)OCx_Start +
                     (double)aRay.m_Dir.y * (double)OCy_Start;

    double c_Start = p_dot_p_Start - m_radius_squared;

    float delta_Start = (float)(b_Start * b_Start - a * c_Start);

    if( delta_Start > FLT_EPSILON )
    {
        float sdelta = sqrtf( delta_Start );
        float t = (-b_Start - sdelta) / a;
        float z = aRay.m_Origin.z + t * aRay.m_Dir.z;

        if( (z >= m_bbox.Min().z) &&
                (z <= m_bbox.Max().z) )
        {
            if( t < aMaxDistance )
                return true;

            return false;
        }
    }

    double OCx_End = aRay.m_Origin.x - m_segment.m_End.x;
    double OCy_End = aRay.m_Origin.y - m_segment.m_End.y;

    double p_dot_p_End = OCx_End * OCx_End + OCy_End * OCy_End;


    double b_End = (double)aRay.m_Dir.x * (double)OCx_End +
                   (double)aRay.m_Dir.y * (double)OCy_End;

    double c_End = p_dot_p_End - m_radius_squared;

    float delta_End = (float)(b_End * b_End - a * c_End);

    if( delta_End > FLT_EPSILON )
    {
        float sdelta = sqrtf( delta_End );
        float t = (-b_End - sdelta) / a;
        float z = aRay.m_Origin.z + t * aRay.m_Dir.z;

        if( (z >= m_bbox.Min().z) &&
                (z <= m_bbox.Max().z) )
        {
            if( t < aMaxDistance )
                return true;

            return false;
        }
    }

    return false;
#endif
}