#ifndef GIM_TRI_COLLISION_H_INCLUDED #define GIM_TRI_COLLISION_H_INCLUDED /*! \file gim_tri_collision.h \author Francisco León Nájera */ /* ----------------------------------------------------------------------------- This source file is part of GIMPACT Library. For the latest info, see http://gimpact.sourceforge.net/ Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371. email: projectileman@yahoo.com This library is free software; you can redistribute it and/or modify it under the terms of EITHER: (1) The GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The text of the GNU Lesser General Public License is included with this library in the file GIMPACT-LICENSE-LGPL.TXT. (2) The BSD-style license that is included with this library in the file GIMPACT-LICENSE-BSD.TXT. This library 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 files GIMPACT-LICENSE-LGPL.TXT and GIMPACT-LICENSE-BSD.TXT for more details. ----------------------------------------------------------------------------- */ /*! \addtogroup GEOMETRIC_OPERATIONS */ //! @{ #define MAX_TRI_CLIPPING 8 //! Clips a polygon by a plane #define PLANE_CLIP_POLYGON(plane,polygon_points,polygon_point_count,clipped,clipped_count,max_clipped) \ { \ clipped_count = 0; \ GUINT _i, _vi, _prevclassif=32000, _classif; \ GREAL _d; \ for(_i=0;_i<=polygon_point_count;_i++) \ { \ _vi = _i%polygon_point_count; \ _d = DISTANCE_PLANE_POINT(plane,polygon_points[_vi]); \ _classif = _d>G_EPSILON ?1:0; \ if(_classif == 0) \ { \ if(_prevclassif==1) \ {\ if(clipped_count u*axe1[i1] + ((vecproj[i2] - u*axe1[i2])/axe2[i2])*axe2[i1] = vecproj[i1] --> u*axe1[i1] + vecproj[i2]*axe2[i1]/axe2[i2] - u*axe1[i2]*axe2[i1]/axe2[i2] = vecproj[i1] --> u*(axe1[i1] - axe1[i2]*axe2[i1]/axe2[i2]) = vecproj[i1] - vecproj[i2]*axe2[i1]/axe2[i2] --> u*((axe1[i1]*axe2[i2] - axe1[i2]*axe2[i1])/axe2[i2]) = (vecproj[i1]*axe2[i2] - vecproj[i2]*axe2[i1])/axe2[i2] --> u*(axe1[i1]*axe2[i2] - axe1[i2]*axe2[i1]) = vecproj[i1]*axe2[i2] - vecproj[i2]*axe2[i1] --> u = (vecproj[i1]*axe2[i2] - vecproj[i2]*axe2[i1]) /(axe1[i1]*axe2[i2] - axe1[i2]*axe2[i1]) if 0.0<= u+v <=1.0 then they are inside of triangle */ #define TRIANGLE_GET_UVPARAMETERS(point,vec1,vec2,vec3,tri_plane,u,v,outside)\ {\ vec3f _axe1, _axe2, _vecproj;\ VEC_DIFF(_axe1,vec2,vec1);\ VEC_DIFF(_axe2,vec3,vec1);\ VEC_DIFF(_vecproj,point,vec1);\ GUINT _i1,_i2;\ PLANE_MINOR_AXES(tri_plane, _i1, _i2);\ if(fabsf(_axe2[_i2])G_EPSILON)\ {\ outside = 1;\ }\ else\ {\ outside = 0;\ }\ }\ }\ //! Finds the collision of a ray and a triangle. #define RAY_TRIANGLE_INTERSECTION(vOrigin,vDir,vec1,vec2,vec3,tri_plane,pout,u,v,tparam,tmax,does_intersect)\ {\ RAY_PLANE_COLLISION(tri_plane,vDir,vOrigin,pout,tparam,does_intersect);\ if(does_intersect != 0)\ {\ if(tparam<-G_EPSILON||tparam>tmax+G_EPSILON)\ {\ does_intersect = 0;\ }\ else\ {\ TRIANGLE_GET_UVPARAMETERS(pout,vec1,vec2,vec3,tri_plane,u,v,does_intersect);\ does_intersect = !does_intersect;\ }\ }\ }\ //! @} #endif // GIM_TRI_COLLISION_H_INCLUDED