line3.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 $Log: not supported by cvs2svn $
00027 Revision 1.8  2004/05/14 03:14:04  ponchio
00028 Added Distance
00029 
00030 Revision 1.7  2004/05/10 10:58:35  ganovelli
00031 name of the constructor changed from LineType to Line3
00032 
00033 Revision 1.6  2004/03/11 11:47:20  tarini
00034 minor updates, corrections, added documentations, etc.
00035 
00036 Revision 1.5  2004/03/10 15:27:48  tarini
00037 added Normalized flag
00038 
00039 
00040 Revision 1.1  2004/03/08 16:15:48  tarini
00041 first version (tarini)
00042 
00043 ****************************************************************************/
00044 
00045 
00046 
00047 #ifndef __VCGLIB_LINE3
00048 #define __VCGLIB_LINE3
00049 
00050 #include <vcg/space/point3.h>
00051 
00052 namespace vcg {
00053 
00063 template <class LineScalarType, bool NORM=false>
00064 class Line3
00065 {
00066 public: 
00067 
00069         typedef LineScalarType ScalarType;
00070 
00072         typedef Point3<LineScalarType> PointType;
00073 
00075         typedef Line3<LineScalarType,NORM> LineType;
00076 
00077 private:
00078 
00080         PointType _ori;
00081 
00083         PointType _dir;
00084 
00085 public:
00086 
00088 
00092 
00093   inline const PointType &Origin() const { return _ori; } 
00094   inline PointType &Origin() { return _ori; } 
00095   inline const PointType &Direction() const { return _dir; } 
00097         inline void SetOrigin( const PointType & ori )
00098         {       _ori=ori; }
00100         inline void SetDirection( const PointType & dir)
00101         {       _dir=dir; if (NORM) _dir.Normalize();  }
00103         inline void Set( const PointType & ori, const PointType & dir )
00104         {       SetOrigin(ori); SetDirection(dir); }
00106 
00108 
00110 
00111         Line3() {};
00113         Line3(const PointType &ori, const PointType &dir) {SetOrigin(ori); SetDirection(dir);};
00115 
00117         inline bool operator == ( LineType const & p ) const
00118         {       return _ori==p._ori && _dir==p._dir; }
00120         inline bool operator != ( LineType const & p ) const
00121         {       return _ori!=p._ori || _dir!=p._dir; }
00123         inline ScalarType Projection( const  PointType &p ) const
00124         { if (NORM) return ScalarType((p-_ori).dot(_dir));
00125                 else      return ScalarType((p-_ori).dot(_dir)/_dir.SquaredNorm());
00126         }
00128         static bool IsNormalized() {return NORM;};
00130         inline PointType P( const ScalarType t ) const
00131         { return _ori + _dir * t; }
00133         inline Line3<ScalarType,true> &Normalize()
00134         { if (!NORM) _dir.Normalize(); return *((Line3<ScalarType,true>*)this);}
00136         static Line3<ScalarType,true> &Normalize(LineType &p)
00137         { p.Normalize(); return *((Line3<ScalarType,true>*)(&p));}
00139         template <class Q, bool K>
00140         inline void Import( const Line3<Q,K> & b )
00141         { _ori.Import( b.Origin() );    _dir.Import( b.Direction() ); 
00142           if ((NORM) && (!K)) _dir.Normalize(); 
00143                 //printf("(=)%c->%c ",(!NORM)?'N':'n', NORM?'N':'n');
00144         }
00146         template <class Q, bool K>
00147         static LineType Construct( const Line3<Q,K> & b )
00148         { LineType res; res.Import(b);  return res;
00149         }       
00150         PointType ClosestPoint(const PointType & p) const{
00151         return P(Projection(p));
00152         }
00154         inline void Flip(){
00155                 _dir=-_dir;
00156         };
00157 
00159 
00166         inline Line3<ScalarType,false> operator + ( LineType const & p) const
00167         {return Line3<ScalarType,false> ( _ori+p.Origin(), _dir+p.Direction() );}
00168         inline Line3<ScalarType,false> operator - ( LineType const & p) const
00169         {return Line3<ScalarType,false> ( _ori-p.Origin(), _dir-p.Direction() );}
00170         inline Line3<ScalarType,false> operator * ( const ScalarType s ) const
00171         {return Line3<ScalarType,false> ( _ori*s, _dir*s );}
00172         inline Line3<ScalarType,false> operator / ( const ScalarType s ) const
00173         {ScalarType s0=((ScalarType)1.0)/s; return LineType( _ori*s0, _dir*s0 );}
00175 
00176 
00178 
00182 
00183         Line3 (const Line3<ScalarType,!NORM > &r) 
00184         { Import(r); };
00186         inline LineType & operator = ( Line3<ScalarType,!NORM> const &r) 
00187         { Import(r); return *this; };
00189 
00190 }; // end class definition
00191 
00192 typedef Line3<short>  Line3s;
00193 typedef Line3<int>        Line3i;
00194 typedef Line3<float>  Line3f;
00195 typedef Line3<double> Line3d;
00196 
00197 typedef Line3<short ,true> Line3sN;
00198 typedef Line3<int   ,true> Line3iN;
00199 typedef Line3<float ,true> Line3fN;
00200 typedef Line3<double,true> Line3dN;
00201 
00203 template <class ScalarType, bool NORM> 
00204 Point3<ScalarType> ClosestPoint( Line3<ScalarType,NORM> l, const Point3<ScalarType> & p) 
00205 {
00206         return l.P(l.Projection(p)); 
00207 }
00208 
00209 template <class ScalarType, bool NORM> 
00210 ScalarType Distance(const Line3<ScalarType, NORM> &l, 
00211                     const Point3<ScalarType> &p) {
00212   Point3<ScalarType> o = l.ClosestPoint(p);
00213   return (o - p).Norm();
00214 }
00215 
00218 } // end namespace
00219 #endif


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