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 History 00025 00026 $Log: not supported by cvs2svn $ 00027 Revision 1.7 2006/06/06 14:35:31 zifnab1974 00028 Changes for compilation on linux AMD64. Some remarks: Linux filenames are case-sensitive. _fileno and _filelength do not exist on linux 00029 00030 Revision 1.6 2006/03/07 16:39:38 pietroni 00031 compiled and corrected ClosestPoint function 00032 00033 Revision 1.5 2004/05/13 23:39:47 ponchio 00034 SegmentType -> Segment3 in constructor (g++ complained) 00035 00036 Revision 1.4 2004/05/08 14:07:50 ganovelli 00037 return type of length and squaredlength corrected 00038 00039 Revision 1.3 2004/03/11 11:47:20 tarini 00040 minor updates, corrections, added documentations, etc. 00041 00042 00043 Revision 1.1 2004/03/08 19:46:47 tarini 00044 First Version (tarini) 00045 00046 ****************************************************************************/ 00047 00048 00049 00050 #ifndef __VCGLIB_SEGMENT3 00051 #define __VCGLIB_SEGMENT3 00052 00053 #include <vcg/space/point3.h> 00054 #include <vcg/space/line3.h> 00055 #include <vcg/space/box3.h> 00056 00057 namespace vcg { 00058 00066 template <class SegmentScalarType > 00067 class Segment3 00068 { 00069 public: 00070 00072 typedef SegmentScalarType ScalarType; 00073 00075 typedef Point3<SegmentScalarType> PointType; 00076 00078 typedef Segment3<SegmentScalarType> SegmentType; 00079 00080 private: 00081 00083 PointType _p0,_p1; 00084 00085 public: 00086 00088 inline const PointType &P0() const { return _p0; } 00089 inline const PointType &P1() const { return _p1; } 00090 inline PointType &P0() { return _p0; } 00091 inline PointType &P1() { return _p1; } 00092 00093 inline PointType &operator[] (const int i) {return i==0?_p0:_p1;} 00094 inline const PointType &operator[] (const int i) const {return i==0?_p0:_p1;} 00095 00097 Segment3() {}; 00099 Segment3(const PointType &a, const PointType &b) { _p0=a; _p1=b; }; 00101 inline bool operator == ( SegmentType const & p ) const 00102 { return _p0==p._p0 && _p1==p._p1; } 00104 inline bool operator != ( SegmentType const & p ) const 00105 { return _p0!=p._p0 || _p1!=p._p1; } 00107 void Set( const PointType &a, const PointType &b) 00108 { _p0=a; _p1=b;} 00111 inline PointType Lerp( const ScalarType t ) const 00112 { return _p0 + (_p1 - _p0) * t; } 00114 inline PointType MidPoint( ) const 00115 { return ( _p0 + _p1) / ScalarType(2.0) ; } 00116 inline PointType Direction( ) const 00117 { return ( _p1 - _p0) ; } 00118 inline PointType NormalizedDirection( ) const 00119 { return ( _p1 - _p0).Normalize() ; } 00121 inline Box3<ScalarType> BBox( ) const 00122 { Box3<ScalarType> t; 00123 if (_p0[0]<_p1[0]) { t.min[0]=_p0[0];t.max[0]=_p1[0];} else { t.min[0]=_p1[0];t.max[0]=_p0[0];} 00124 if (_p0[1]<_p1[1]) { t.min[1]=_p0[1];t.max[1]=_p1[1];} else { t.min[1]=_p1[1];t.max[1]=_p0[1];} 00125 if (_p0[2]<_p1[2]) { t.min[2]=_p0[2];t.max[2]=_p1[2];} else { t.min[2]=_p1[2];t.max[2]=_p0[2];} 00126 return t; } 00128 ScalarType Length() const 00129 { return (_p0 - _p1).Norm(); } 00131 ScalarType SquaredLength() const 00132 { return (_p0 - _p1).SquaredNorm(); } 00134 void Flip() 00135 { PointType t=_p0; _p0=_p1; _p1=t; } 00137 template <class Q> 00138 inline void Import( const Segment3<Q> & b ) 00139 { _p0.Import( b.P0() ); _p1.Import( b.P1() ); 00140 } 00142 template <class Q> 00143 static SegmentType Construct( const Segment3<Q> & b ) 00144 { return SegmentType(PointType::Construct(b.P0()), PointType::Construct(b.P1()));} 00145 00147 00148 inline SegmentType operator + ( SegmentType const & p) const 00149 {return SegmentType( _p0+p.P0(), _p1+p.P1() );} 00150 inline SegmentType operator - ( SegmentType const & p) const 00151 {return SegmentType( _p0-p.P0(), _p1-p.P1() );} 00152 inline SegmentType operator * ( const ScalarType s ) const 00153 {return SegmentType( _p0*s, _p1*s );} 00154 inline SegmentType operator / ( const ScalarType s ) const 00155 {ScalarType s0=((ScalarType)1.0)/s; return SegmentType( _p0*s0, _p1*s0 );} 00157 00158 }; // end class definition 00159 00160 00161 00162 typedef Segment3<short> Segment3s; 00163 typedef Segment3<int> Segment3i; 00164 typedef Segment3<float> Segment3f; 00165 typedef Segment3<double> Segment3d; 00166 00168 //* Computes the minimum distance between a segment and a point 00169 //* @param[in] segment The input segment 00170 //* @param[in] p The input point 00171 //* @return The distance between the segment and the point p 00172 //*/ 00173 //template < class ScalarType > 00174 //ScalarType SquaredDistance(Segment3< ScalarType > &segment, Point3< ScalarType > &p) 00175 //{ 00176 // typedef typename vcg::Point3< ScalarType > Point3t; 00177 // 00178 // Point3t dir = (segment.P1()-segment.P0()).Normalize(); 00179 // ScalarType h = dir * (p-segment.P0()); 00180 // if (h<=ScalarType(0.0)) return vcg::SquaredDistance<ScalarType>(p, segment.P0()); 00181 // else if (h>=segment.Length()) return vcg::SquaredDistance<ScalarType>(p, segment.P1()); 00182 // else 00183 // { 00184 // dir = segment.P0() + dir*h; 00185 // return vcg::SquaredDistance<ScalarType>(p, dir); 00186 // } 00187 //}; //end of Distance method 00188 // 00189 //template <class ScalarType> 00190 //Point3<ScalarType> ClosestPoint( Segment3<ScalarType> s, const Point3<ScalarType> & p) 00191 //{ 00192 // vcg::Line3<ScalarType> l; 00193 // l.Set(s.P0(),s.P1()-s.P0()); 00194 // l.Normalize(); 00195 // Point3<ScalarType> clos=vcg::ClosestPoint<ScalarType,true>(l,p) ;//attention to call 00196 // vcg::Box3<ScalarType> b; 00197 // b.Add(s.P0()); 00198 // b.Add(s.P1()); 00199 // if (b.IsIn(clos)) 00200 // return clos; 00201 // else 00202 // { 00203 // ScalarType d0=(s.P0()-p).Norm(); 00204 // ScalarType d1=(s.P1()-p).Norm(); 00205 // if (d0<d1) 00206 // return (s.P0()); 00207 // else 00208 // return (s.P1()); 00209 // } 00210 // /*ScalarType t = s.Projection(p); 00211 // if (s<0) return s.P0(); 00212 // if (s>1) return s.P0(); 00213 // return s.P(t);*/ 00214 //} 00215 00218 } // end namespace 00219 #endif