line2.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_LINE2
00032 #define __VCGLIB_LINE2
00033 
00034 #include <vcg/space/point2.h>
00035 
00036 namespace vcg {
00037 
00047 template <class LineScalarType, bool NORM=false>
00048 class Line2
00049 {
00050 public: 
00051 
00053         typedef LineScalarType ScalarType;
00054 
00056         typedef Point2<LineScalarType> PointType;
00057 
00059         typedef Line2<LineScalarType,NORM> LineType;
00060 
00061 private:
00062 
00064         PointType _ori;
00065 
00067         PointType _dir;
00068 
00069 public:
00070 
00072 
00076 
00077   inline const PointType &Origin() const { return _ori; } 
00078   inline PointType &Origin() { return _ori; } 
00079   inline const PointType &Direction() const { return _dir; } 
00081         inline void SetOrigin( const PointType & ori )
00082         {       _ori=ori; }
00084         inline void SetDirection( const PointType & dir)
00085         {       _dir=dir; if (NORM) _dir.Normalize();  }
00087         inline void Set( const PointType & ori, const PointType & dir )
00088         {       SetOrigin(ori); SetDirection(dir); }
00090 
00092 
00094 
00095         Line2() {};
00097         Line2(const PointType &ori, const PointType &dir) {SetOrigin(ori); SetDirection(dir);};
00099 
00101         inline bool operator == ( LineType const & p ) const
00102         {       return _ori==p._ori && _dir==p._dir; }
00104         inline bool operator != ( LineType const & p ) const
00105         {       return _ori!=p._ori || _dir!=p._dir; }
00107         inline ScalarType Projection( const  PointType &p ) const
00108         { if (NORM) return ScalarType((p-_ori).dot(_dir));
00109                 else      return ScalarType((p-_ori).dot(_dir)/_dir.SquaredNorm());
00110         }
00112         static bool IsNormalized() {return NORM;};
00114         inline PointType P( const ScalarType t ) const
00115         { return _ori + _dir * t; }
00117         inline Line2<ScalarType,true> &Normalize()
00118         { if (!NORM) _dir.Normalize(); return *((Line2<ScalarType,true>*)this);}
00120         static Line2<ScalarType,true> &Normalize(LineType &p)
00121         { p.Normalize(); return *((Line2<ScalarType,true>*)(&p));}
00123         template <class Q, bool K>
00124         inline void Import( const Line2<Q,K> & b )
00125         { _ori.Import( b.Origin() );    _dir.Import( b.Direction() ); 
00126           if ((NORM) && (!K)) _dir.Normalize(); 
00127                 //printf("(=)%c->%c ",(!NORM)?'N':'n', NORM?'N':'n');
00128         }
00130         template <class Q, bool K>
00131         static LineType Construct( const Line2<Q,K> & b )
00132         { LineType res; res.Import(b);  return res;
00133         }       
00134         PointType ClosestPoint(const PointType & p) const{
00135         return P(Projection(p));
00136         }
00138         inline void Flip(){
00139                 _dir=-_dir;
00140         };
00141 
00143 
00150         inline Line2<ScalarType,false> operator + ( LineType const & p) const
00151         {return Line2<ScalarType,false> ( _ori+p.Origin(), _dir+p.Direction() );}
00152         inline Line2<ScalarType,false> operator - ( LineType const & p) const
00153         {return Line2<ScalarType,false> ( _ori-p.Origin(), _dir-p.Direction() );}
00154         inline Line2<ScalarType,false> operator * ( const ScalarType s ) const
00155         {return Line2<ScalarType,false> ( _ori*s, _dir*s );}
00156         inline Line2<ScalarType,false> operator / ( const ScalarType s ) const
00157         {ScalarType s0=((ScalarType)1.0)/s; return LineType( _ori*s0, _dir*s0 );}
00159 
00160 
00162 
00166 
00167         Line2 (const Line2<ScalarType,!NORM > &r) 
00168         { Import(r); };
00170         inline LineType & operator = ( Line2<ScalarType,!NORM> const &r) 
00171         { Import(r); return *this; };
00173 
00174 }; // end class definition
00175 
00176 typedef Line2<short>  Line2s;
00177 typedef Line2<int>        Line2i;
00178 typedef Line2<float>  Line2f;
00179 typedef Line2<double> Line2d;
00180 
00181 typedef Line2<short ,true> Line2sN;
00182 typedef Line2<int   ,true> Line2iN;
00183 typedef Line2<float ,true> Line2fN;
00184 typedef Line2<double,true> Line2dN;
00185 
00187 template <class ScalarType, bool NORM> 
00188 Point2<ScalarType> ClosestPoint( Line2<ScalarType,NORM> l, const Point2<ScalarType> & p) 
00189 {
00190         return l.P(l.Projection(p)); 
00191 }
00192 
00193 template <class ScalarType, bool NORM> 
00194 ScalarType Distance(const Line2<ScalarType, NORM> &l, 
00195                     const Point2<ScalarType> &p) {
00196   Point2<ScalarType> o = l.ClosestPoint(p);
00197   return (o - p).Norm();
00198 }
00199 
00200 template <class ScalarType, bool NORM> 
00201 vcg::Point2<ScalarType> Mirror(const vcg::Line2<ScalarType, NORM> &l, 
00202                                                            const vcg::Point2<ScalarType> &p) 
00203 {
00204   vcg::Point2<ScalarType> nearest=vcg::ClosestPoint<ScalarType,NORM>(l,p);
00205   vcg::Point2<ScalarType> dir=(nearest - p);
00206   nearest+=dir;
00207   return nearest;
00208 }
00209 
00212 } // end namespace
00213 #endif


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