segment2.h
Go to the documentation of this file.
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 
00027 ****************************************************************************/
00028 
00029 
00030 
00031 #ifndef __VCGLIB_SEGMENT2
00032 #define __VCGLIB_SEGMENT2
00033 
00034 #include <vcg/space/point2.h>
00035 #include <vcg/space/line2.h>
00036 #include <vcg/space/box2.h>
00037 
00038 namespace vcg {
00039 
00047 template <class SegmentScalarType >
00048 class Segment2
00049 {
00050 public:
00051 
00053         typedef SegmentScalarType ScalarType;
00054 
00056         typedef Point2<SegmentScalarType> PointType;
00057 
00059         typedef Segment2<SegmentScalarType> SegmentType;
00060 
00061 private:
00062 
00064         PointType _p0,_p1;
00065 
00066 public:
00067 
00069   inline const PointType &P0() const { return _p0; }
00070   inline const PointType &P1() const { return _p1; }
00071   inline PointType &P0() { return _p0; }
00072   inline PointType &P1() { return _p1; }
00074     Segment2() {};
00076     Segment2(const PointType &a, const PointType &b) { _p0=a; _p1=b; };
00078     inline bool operator == ( SegmentType const & p ) const
00079     {   return _p0==p._p0 && _p1==p._p1; }
00081     inline bool operator != ( SegmentType const & p ) const
00082     {   return _p0!=p._p0 || _p1!=p._p1; }
00084     void Set( const PointType &a, const PointType &b)
00085     {   _p0=a; _p1=b;}
00088     inline PointType Lerp( const ScalarType t ) const
00089     { return _p0 + (_p1 - _p0) * t; }
00091     inline PointType MidPoint( ) const
00092     { return ( _p0 +  _p1) / ScalarType(2.0) ; }
00094     inline Box2<ScalarType> BBox( ) const
00095     {
00096       Box2<ScalarType> t;
00097       t.Add(_p0);
00098       t.Add(_p1);
00099       return t;
00100     }
00102     ScalarType Length()
00103     { return (_p0 - _p1).Norm(); }
00104 
00106         ScalarType Length() const
00107         { return (_p0 - _p1).Norm(); }
00108 
00110         ScalarType SquaredLength()
00111         { return (_p0 - _p1).SquaredNorm(); }
00113         void Flip()
00114         { PointType t=_p0; _p0=_p1; _p1=t; }
00116         template <class Q>
00117         inline void Import( const Segment2<Q> & b )
00118         { _p0.Import( b.P0() ); _p1.Import( b.P1() );
00119         }
00121         template <class Q>
00122         static SegmentType Construct( const Segment2<Q> & b )
00123         { return SegmentType(PointType::Construct(b.P0()), PointType::Construct(b.P1()));}
00124 
00126 
00127         inline SegmentType operator + ( SegmentType const & p) const
00128         {return SegmentType( _p0+p.P0(), _p1+p.P1() );}
00129         inline SegmentType operator - ( SegmentType const & p) const
00130         {return SegmentType( _p0-p.P0(), _p1-p.P1() );}
00131         inline SegmentType operator * ( const ScalarType s ) const
00132         {return SegmentType( _p0*s, _p1*s );}
00133         inline SegmentType operator / ( const ScalarType s ) const
00134         {ScalarType s0=((ScalarType)1.0)/s; return SegmentType( _p0*s0, _p1*s0 );}
00136 
00137 }; // end class definition
00138 
00139 
00140 
00141 typedef Segment2<short>  Segment2s;
00142 typedef Segment2<int>    Segment2i;
00143 typedef Segment2<float>  Segment2f;
00144 typedef Segment2<double> Segment2d;
00145 
00146 template <class ScalarType>
00147 Point2<ScalarType> ClosestPoint( Segment2<ScalarType> s, const Point2<ScalarType> & p)
00148 {
00149         vcg::Line2<ScalarType, true> l;
00150         l.Set(s.P0(),s.P1()-s.P0());
00151         ScalarType t = l.Projection(p);
00152         Point2<ScalarType> clos = l.P(t);
00153         ScalarType length = s.Length();
00154         if (t <= 0)
00155                 return s.P0();
00156         else if (t >= length)
00157                 return s.P1();
00158         else
00159                 return clos;
00160 }
00161 
00162 template <class S>
00163 class PointSegment2DEPFunctor {
00164 public:
00165     typedef S ScalarType;
00166     typedef Point2<ScalarType> QueryType;
00167     static inline const Point2<ScalarType> &  Pos(const QueryType & qt)  {return qt;}
00168 
00169     template <class EdgeType, class ScalarType>
00170     inline bool operator () (const EdgeType & e, const Point2<ScalarType> & p,
00171                              ScalarType & minDist, Point2<ScalarType> & q)
00172     {
00173         const Point2<typename EdgeType::ScalarType> fp = Point2<typename EdgeType::ScalarType>::Construct(p);
00174         Point2<typename EdgeType::ScalarType> fq;
00175         fq=ClosestPoint(e,fp);
00176         ScalarType currD = (ScalarType)(fp-fq).Norm();
00177 
00178         if (currD>minDist)return false;
00179 
00180         minDist=currD;
00181         q = Point2<ScalarType>::Construct(fq);
00182         return true;
00183     }
00184 };
00185 
00186 
00187 } // end namespace
00188 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:35:12