| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#ifndef __VCGLIB_LINE2 |
| 32 |
#define __VCGLIB_LINE2 |
| 33 |
|
| 34 |
#include <vcg/space/point2.h> |
| 35 |
|
| 36 |
namespace vcg { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
template <class LineScalarType, bool NORM=false> |
| 48 |
class Line2 |
| 49 |
{ |
| 50 |
public: |
| 51 |
|
| 52 |
|
| 53 |
typedef LineScalarType ScalarType; |
| 54 |
|
| 55 |
|
| 56 |
typedef Point2<LineScalarType> PointType; |
| 57 |
|
| 58 |
|
| 59 |
typedef Line2<LineScalarType,NORM> LineType; |
| 60 |
|
| 61 |
private: |
| 62 |
|
| 63 |
|
| 64 |
PointType _ori; |
| 65 |
|
| 66 |
|
| 67 |
PointType _dir; |
| 68 |
|
| 69 |
public: |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
inline const PointType &Origin() const { return _ori; } |
| 78 |
inline PointType &Origin() { return _ori; } |
| 79 |
inline const PointType &Direction() const { return _dir; } |
| 80 |
|
| 81 |
inline void SetOrigin( const PointType & ori ) |
| 82 |
{ _ori=ori; } |
| 83 |
|
| 84 |
inline void SetDirection( const PointType & dir) |
| 85 |
{ _dir=dir; if (NORM) _dir.Normalize(); } |
| 86 |
|
| 87 |
inline void Set( const PointType & ori, const PointType & dir ) |
| 88 |
{ SetOrigin(ori); SetDirection(dir); } |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
Line2() {}; |
| 96 |
|
| 97 |
Line2(const PointType &ori, const PointType &dir) {SetOrigin(ori); SetDirection(dir);}; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
inline bool operator == ( LineType const & p ) const |
| 102 |
{ return _ori==p._ori && _dir==p._dir; } |
| 103 |
|
| 104 |
inline bool operator != ( LineType const & p ) const |
| 105 |
{ return _ori!=p._ori || _dir!=p._dir; } |
| 106 |
|
| 107 |
inline ScalarType Projection( const PointType &p ) const |
| 108 |
{ if (NORM) return ScalarType((p-_ori)*_dir); |
| 109 |
else return ScalarType((p-_ori)*_dir/_dir.SquaredNorm()); |
| 110 |
} |
| 111 |
|
| 112 |
static bool IsNormalized() {return NORM;}; |
| 113 |
|
| 114 |
inline PointType P( const ScalarType t ) const |
| 115 |
{ return _ori + _dir * t; } |
| 116 |
|
| 117 |
inline Line2<ScalarType,true> &Normalize() |
| 118 |
{ if (!NORM) _dir.Normalize(); return *((Line2<ScalarType,true>*)this);} |
| 119 |
|
| 120 |
static Line2<ScalarType,true> &Normalize(LineType &p) |
| 121 |
{ p.Normalize(); return *((Line2<ScalarType,true>*)(&p));} |
| 122 |
|
| 123 |
template <class Q, bool K> |
| 124 |
inline void Import( const Line2<Q,K> & b ) |
| 125 |
{ _ori.Import( b.Origin() ); _dir.Import( b.Direction() ); |
| 126 |
if ((NORM) && (!K)) _dir.Normalize(); |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
template <class Q, bool K> |
| 131 |
static LineType Construct( const Line2<Q,K> & b ) |
| 132 |
{ LineType res; res.Import(b); return res; |
| 133 |
} |
| 134 |
PointType ClosestPoint(const PointType & p) const{ |
| 135 |
return P(Projection(p)); |
| 136 |
} |
| 137 |
|
| 138 |
inline void Flip(){ |
| 139 |
_dir=-_dir; |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
inline Line2<ScalarType,false> operator + ( LineType const & p) const |
| 151 |
{return Line2<ScalarType,false> ( _ori+p.Origin(), _dir+p.Direction() );} |
| 152 |
inline Line2<ScalarType,false> operator - ( LineType const & p) const |
| 153 |
{return Line2<ScalarType,false> ( _ori-p.Origin(), _dir-p.Direction() );} |
| 154 |
inline Line2<ScalarType,false> operator * ( const ScalarType s ) const |
| 155 |
{return Line2<ScalarType,false> ( _ori*s, _dir*s );} |
| 156 |
inline Line2<ScalarType,false> operator / ( const ScalarType s ) const |
| 157 |
{ScalarType s0=((ScalarType)1.0)/s; return LineType( _ori*s0, _dir*s0 );} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
Line2 (const Line2<ScalarType,!NORM > &r) |
| 168 |
{ Import(r); }; |
| 169 |
|
| 170 |
inline LineType & operator = ( Line2<ScalarType,!NORM> const &r) |
| 171 |
{ Import(r); return *this; }; |
| 172 |
|
| 173 |
|
| 174 |
}; |
| 175 |
|
| 176 |
typedef Line2<short> Line2s; |
| 177 |
typedef Line2<int> Line2i; |
| 178 |
typedef Line2<float> Line2f; |
| 179 |
typedef Line2<double> Line2d; |
| 180 |
|
| 181 |
typedef Line2<short ,true> Line2sN; |
| 182 |
typedef Line2<int ,true> Line2iN; |
| 183 |
typedef Line2<float ,true> Line2fN; |
| 184 |
typedef Line2<double,true> Line2dN; |
| 185 |
|
| 186 |
|
| 187 |
template <class ScalarType, bool NORM> |
| 188 |
Point2<ScalarType> ClosestPoint( Line2<ScalarType,NORM> l, const Point2<ScalarType> & p) |
| 189 |
{ |
| 190 |
return l.P(l.Projection(p)); |
| 191 |
} |
| 192 |
|
| 193 |
template <class ScalarType, bool NORM> |
| 194 |
ScalarType Distance(const Line2<ScalarType, NORM> &l, |
| 195 |
const Point2<ScalarType> &p) { |
| 196 |
Point2<ScalarType> o = l.ClosestPoint(p); |
| 197 |
return (o - p).Norm(); |
| 198 |
} |
| 199 |
|
| 200 |
template <class ScalarType, bool NORM> |
| 201 |
vcg::Point2<ScalarType> Mirror(const vcg::Line2<ScalarType, NORM> &l, |
| 202 |
const vcg::Point2<ScalarType> &p) |
| 203 |
{ |
| 204 |
vcg::Point2<ScalarType> nearest=vcg::ClosestPoint<ScalarType,NORM>(l,p); |
| 205 |
vcg::Point2<ScalarType> dir=(nearest - p); |
| 206 |
nearest+=dir; |
| 207 |
return nearest; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
} |
| 213 |
#endif |