00001 /**************************************************************************** 00002 * VCGLib o o * 00003 * Visual and Computer Graphics Library o o * 00004 * _ O _ * 00005 * Copyright(C) 2004 \/)\/ * 00006 * Visual Computing Lab /\/| * 00007 * ISTI - Italian National Research Council | * 00008 * \ * 00009 * All rights reserved. * 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 * This program is distributed in the hope that it will be useful, * 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * 00020 * for more details. * 00021 * * 00022 ****************************************************************************/ 00023 /****************************************************************************/ 00024 00025 #ifndef __VCG_DISTANCE2 00026 #define __VCG_DISTANCE2 00027 00028 #include <limits> 00029 #include <vcg/space/segment2.h> 00030 #include <vcg/space/intersection2.h> 00031 00032 namespace vcg { 00033 00034 /* 00035 * Computes the minimum distance between a two segments 00036 * @param[in] S0 The input segment0 00037 * @param[in] S1 The input segment1 00038 * return the distance between the two segments 00039 */ 00040 template<class ScalarType> 00041 ScalarType Segment2DSegment2DDistance(const vcg::Segment2<ScalarType> &S0, 00042 const vcg::Segment2<ScalarType> &S1, 00043 vcg::Point2<ScalarType> &p_clos0, 00044 vcg::Point2<ScalarType> &p_clos1) 00045 { 00046 //first test if they intersect 00047 vcg::Point2<ScalarType> IntPoint; 00048 if (vcg:: SegmentSegmentIntersection(S0,S1,IntPoint)) 00049 { 00050 p_clos0=IntPoint; 00051 p_clos1=IntPoint; 00052 return 0; 00053 } 00054 vcg::Point2<ScalarType> Pclos0=ClosestPoint(S0,S1.P0()); 00055 vcg::Point2<ScalarType> Pclos1=ClosestPoint(S0,S1.P1()); 00056 vcg::Point2<ScalarType> Pclos2=ClosestPoint(S1,S0.P0()); 00057 vcg::Point2<ScalarType> Pclos3=ClosestPoint(S1,S0.P1()); 00058 ScalarType d0=(Pclos0-S1.P0()).Norm(); 00059 ScalarType d1=(Pclos1-S1.P1()).Norm(); 00060 ScalarType d2=(Pclos2-S0.P0()).Norm(); 00061 ScalarType d3=(Pclos3-S0.P1()).Norm(); 00062 00063 //then return the minimuim distance 00064 if ((d0<d1)&&(d0<d2)&&(d0<d3)) 00065 { 00066 p_clos0=Pclos0; 00067 p_clos1=S1.P0(); 00068 return d0; 00069 } 00070 if ((d1<d0)&&(d1<d2)&&(d1<d3)) 00071 { 00072 p_clos0=Pclos1; 00073 p_clos1=S1.P1(); 00074 return d1; 00075 } 00076 if ((d2<d0)&&(d2<d1)&&(d2<d3)) 00077 { 00078 p_clos0=S0.P0(); 00079 p_clos1=Pclos2; 00080 return d2; 00081 } 00082 else 00083 { 00084 p_clos0=S0.P1(); 00085 p_clos1=Pclos3; 00086 return d3; 00087 } 00088 } 00089 00090 00091 } 00092 00093 #endif