From f36d1e007abaf207d1ebc4ff8613a6153f9ca8bf Mon Sep 17 00:00:00 2001 From: mtarini Date: Thu, 11 Mar 2004 11:47:20 +0000 Subject: [PATCH] minor updates, corrections, added documentations, etc. --- vcg/space/line3.h | 103 +++++++++++++++++++++++++++++++------------ vcg/space/ray3.h | 96 ++++++++++++++++++++++++++++++---------- vcg/space/segment3.h | 38 +++++++++++----- 3 files changed, 175 insertions(+), 62 deletions(-) diff --git a/vcg/space/line3.h b/vcg/space/line3.h index b3f2c554..5f18c7c5 100644 --- a/vcg/space/line3.h +++ b/vcg/space/line3.h @@ -24,12 +24,13 @@ History $Log: not supported by cvs2svn $ +Revision 1.5 2004/03/10 15:27:48 tarini +added Normalized flag + Revision 1.1 2004/03/08 16:15:48 tarini first version (tarini) - - ****************************************************************************/ @@ -61,31 +62,48 @@ public: /// The point type typedef Point3 PointType; - /// The point type + /// The line type typedef Line3 LineType; private: - /// Origingin + /// Origin PointType _ori; - /// Directionection (not necessarily normalized) + /// Direction (not necessarily normalized, unless so specified by NORM) PointType _dir; public: - /// Members to access the origin, direction +//@{ + /** @name Members to access the origin or direction + Direction() cannot be assigned directly. + Use SetDirection() or Set() instead. + **/ + /// inline const PointType &Origin() const { return _ori; } - inline const PointType &Direction() const { return _dir; } inline PointType &Origin() { return _ori; } - inline PointType &Direction() { - assert(!IsNormalized()); // Directionection can't be set for NORMALIZED Lines! Use SetDirection instead! - return _dir; - } - /// The empty constructor + inline const PointType &Direction() const { return _dir; } + /// sets the origin + inline void SetOrigin( const PointType & ori ) + { _ori=ori; } + /// sets the direction + inline void SetDirection( const PointType & dir) + { _dir=dir; if (NORM) _dir.Normalize(); } + /// sets origin and direction. + inline void Set( const PointType & ori, const PointType & dir ) + { SetOrigin(ori); SetDirection(dir); } +//@} + +//@{ + /** @name Constructors + **/ + /// The empty constructor Line3() {}; /// The (origin, direction) constructor - LineType(const PointType &ori, const PointType &dir) {SetOrigin(ori); SetDirection(dir);}; + Line3(const PointType &ori, const PointType &dir) {SetOrigin(ori); SetDirection(dir);}; +//@} + /// Operator to compare two lines inline bool operator == ( LineType const & p ) const { return _ori==p._ori && _dir==p._dir; } @@ -97,31 +115,29 @@ public: { if (NORM) return ScalarType((p-_ori)*_dir); else return ScalarType((p-_ori)*_dir/_dir.SquaredNorm()); } - inline bool IsNormalized() const {return NORM;}; - ///set the origin - inline void SetOrigin( const PointType & ori ) - { _ori=ori; } - ///set the direction - inline void SetDirection( const PointType & dir) - { _dir=dir; if (NORM) _dir.Normalize(); } - ///set both the origina and direction. - inline void Set( const PointType & ori, const PointType & dir ) - { SetOrigin(ori); SetDirection(dir); } + /// returns wheter this type is normalized or not + static bool IsNormalized() {return NORM;}; /// calculates the point of parameter t on the line. inline PointType P( const ScalarType t ) const { return _ori + _dir * t; } /// normalizes direction field (returns a Normalized Line) - Line3 &Normalize() + inline Line3 &Normalize() { if (!NORM) _dir.Normalize(); return *((Line3*)this);} /// normalizes direction field (returns a Normalized Line) - static version static Line3 &Normalize(LineType &p) { p.Normalize(); return *((Line3*)(&p));} - /// importer for different line types + /// importer for different line types (with any scalar type or normalization beaviour) template inline void Import( const Line3 & b ) - { _ori.Import( b.Origin()); _dir.Import( b.Direction()); - if ((NORM) && (!K)) _dir.Normalize(); + { _ori.Import( b.Origin() ); _dir.Import( b.Direction() ); + if ((NORM) && (!K)) _dir.Normalize(); + //printf("(=)%c->%c ",(!NORM)?'N':'n', NORM?'N':'n'); } + /// constructs a new line importing it from an existing one + template + static LineType Construct( const Line3 & b ) + { LineType res; res.Import(b); return res; + } PointType ClosestPoint(const PointType & p) const{ return P(Projection(p)); } @@ -129,6 +145,39 @@ public: inline void Flip(){ _dir=-_dir; }; + +//@{ + /** @name Linearity for 3d lines + (operators +, -, *, /) so a line can be set as a linear combination + of several lines. Note that the result of any operation returns + a non-normalized line; however, the command r0 = r1*a + r2*b is licit + even if r0,r1,r2 are normalized lines, as the normalization will + take place within the final assignement operation. + **/ + inline Line3 operator + ( LineType const & p) const + {return Line3 ( _ori+p.Origin(), _dir+p.Direction() );} + inline Line3 operator - ( LineType const & p) const + {return Line3 ( _ori-p.Origin(), _dir-p.Direction() );} + inline Line3 operator * ( const ScalarType s ) const + {return Line3 ( _ori*s, _dir*s );} + inline Line3 operator / ( const ScalarType s ) const + {ScalarType s0=((ScalarType)1.0)/s; return LineType( _ori*s0, _dir*s0 );} +//@} + + +//@{ + /** @name Automatic normalized to non-normalized + "Line3dN r0 = r1" is equivalent to + "Line3dN r0 = r1.Normalize()" if r1 is a Line3d + **/ + /// copy constructor that takes opposite beaviour + LineType (const Line3 &r) + { Import(r); }; + /// assignment + inline LineType & operator = ( Line3 const &r) + { Import(r); return *this; }; +//@} + }; // end class definition typedef Line3 Line3s; diff --git a/vcg/space/ray3.h b/vcg/space/ray3.h index 78f9adfe..43aed84a 100644 --- a/vcg/space/ray3.h +++ b/vcg/space/ray3.h @@ -25,8 +25,8 @@ $Log: not supported by cvs2svn $ - - +Revision 1.3 2004/03/10 15:27:18 tarini +first version ****************************************************************************/ @@ -59,31 +59,48 @@ public: /// The point type typedef Point3 PointType; - /// The point type + /// The ray type typedef Ray3 RayType; private: - /// Origingin + /// Origin PointType _ori; - /// Directionection (not necessarily normalized) + /// Direction (not necessarily normalized, unless so specified by NORM) PointType _dir; public: - /// Members to access the origin, direction +//@{ + /** @name Members to access the origin or direction + Direction() cannot be assigned directly. + Use SetDirection() or Set() instead. + **/ + /// inline const PointType &Origin() const { return _ori; } - inline const PointType &Direction() const { return _dir; } inline PointType &Origin() { return _ori; } - inline PointType &Direction() { - assert(!IsNormalized()); // Directionection can't be set for NORMALIZED Rays! Use SetDirection instead! - return _dir; - } - /// The empty constructor + inline const PointType &Direction() const { return _dir; } + /// sets the origin + inline void SetOrigin( const PointType & ori ) + { _ori=ori; } + /// sets the direction + inline void SetDirection( const PointType & dir) + { _dir=dir; if (NORM) _dir.Normalize(); } + /// sets origin and direction. + inline void Set( const PointType & ori, const PointType & dir ) + { SetOrigin(ori); SetDirection(dir); } +//@} + +//@{ + /** @name Constructors + **/ + /// The empty constructor Ray3() {}; /// The (origin, direction) constructor RayType(const PointType &ori, const PointType &dir) {SetOrigin(ori); SetDirection(dir);}; +//@} + /// Operator to compare two rays inline bool operator == ( RayType const & p ) const { return _ori==p._ori && _dir==p._dir; } @@ -95,16 +112,8 @@ public: { if (NORM) return ScalarType((p-_ori)*_dir); else return ScalarType((p-_ori)*_dir/_dir.SquaredNorm()); } - inline bool IsNormalized() const {return NORM;}; - ///set the origin - inline void SetOrigin( const PointType & ori ) - { _ori=ori; } - ///set the direction - inline void SetDirection( const PointType & dir) - { _dir=dir; if (NORM) _dir.Normalize(); } - ///set both the origina and direction. - inline void Set( const PointType & ori, const PointType & dir ) - { SetOrigin(ori); SetDirection(dir); } + /// returns wheter this type is normalized or not + static bool IsNormalized() {return NORM;}; /// calculates the point of parameter t on the ray. inline PointType P( const ScalarType t ) const { return _ori + _dir * t; } @@ -114,12 +123,18 @@ public: /// normalizes direction field (returns a Normalized Ray) - static version static Ray3 &Normalize(RayType &p) { p.Normalize(); return *((Ray3*)(&p));} - /// importer for different ray types + /// importer for different ray types (with any scalar type or normalization beaviour) template inline void Import( const Ray3 & b ) { _ori.Import( b.Origin() ); _dir.Import( b.Direction() ); - if ((NORM) && (!K)) _dir.Normalize(); + if ((NORM) && (!K)) _dir.Normalize(); + //printf("(=)%c->%c ",(!NORM)?'N':'n', NORM?'N':'n'); } + /// constructs a new ray importing it from an existing one + template + static RayType Construct( const Ray3 & b ) + { RayType res; res.Import(b); return res; + } PointType ClosestPoint(const PointType & p) const{ return P(Projection(p)); } @@ -127,6 +142,39 @@ public: inline void Flip(){ _dir=-_dir; }; + +//@{ + /** @name Linearity for 3d rays + (operators +, -, *, /) so a ray can be set as a linear combination + of several rays. Note that the result of any operation returns + a non-normalized ray; however, the command r0 = r1*a + r2*b is licit + even if r0,r1,r2 are normalized rays, as the normalization will + take place within the final assignement operation. + **/ + inline Ray3 operator + ( RayType const & p) const + {return Ray3 ( _ori+p.Origin(), _dir+p.Direction() );} + inline Ray3 operator - ( RayType const & p) const + {return Ray3 ( _ori-p.Origin(), _dir-p.Direction() );} + inline Ray3 operator * ( const ScalarType s ) const + {return Ray3 ( _ori*s, _dir*s );} + inline Ray3 operator / ( const ScalarType s ) const + {ScalarType s0=((ScalarType)1.0)/s; return RayType( _ori*s0, _dir*s0 );} +//@} + + +//@{ + /** @name Automatic normalized to non-normalized + "Ray3dN r0 = r1" is equivalent to + "Ray3dN r0 = r1.Normalize()" if r1 is a Ray3d + **/ + /// copy constructor that takes opposite beaviour + RayType (const Ray3 &r) + { Import(r); }; + /// assignment + inline RayType & operator = ( Ray3 const &r) + { Import(r); return *this; }; +//@} + }; // end class definition typedef Ray3 Ray3s; diff --git a/vcg/space/segment3.h b/vcg/space/segment3.h index ad8f3cef..61d2b66d 100644 --- a/vcg/space/segment3.h +++ b/vcg/space/segment3.h @@ -24,11 +24,10 @@ History $Log: not supported by cvs2svn $ + Revision 1.1 2004/03/08 19:46:47 tarini First Version (tarini) - - ****************************************************************************/ @@ -78,13 +77,13 @@ public: Segment3() {}; /// The (a,b) constructor SegmentType(const PointType &a, const PointType &b) { _p0=a; _p1=b; }; - /// Operator to compare two bounding box + /// Operator to compare segments inline bool operator == ( SegmentType const & p ) const { return _p0==p._p0 && _p1==p._p1; } - /// Operator to dispare two bounding box + /// Operator to dispare segments inline bool operator != ( SegmentType const & p ) const { return _p0!=p._p0 || _p1!=p._p1; } - /// initializes the bounding box + /// initializes the segment with its extrema void Set( const PointType &a, const PointType &b) { _p0=a; _p1=b;} /// calculates the point of parameter t on the segment. @@ -101,20 +100,36 @@ public: 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];} 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];} return t; } - /// return lenght + /// returns segment length SegmentType &Length() { return (_p0 - _p1).Norm(); } - /// return squared lenght + /// return segment squared lenght SegmentType &SquaredLength() { return (_p0 - _p1).SquaredNorm(); } /// flips: a-b becomes b-a void Flip() { PointType t=_p0; _p0=_p1; _p1=t; } - template /// importer for different line types + template inline void Import( const Segment3 & b ) - { _p0.Import( b._p0); _p1.Import( b._p1); + { _p0.Import( b.P0() ); _p1.Import( b.P1() ); } + /// copy constructor (builds a new segment importing an existing one) + template + static SegmentType Construct( const Segment3 & b ) + { return SegmentType(PointType::Construct(b.P0()), PointType::Construct(b.P1()));} + +//@{ + /** @name Linearity for 3d segments (operators +, -, *, /) **/ + inline SegmentType operator + ( SegmentType const & p) const + {return SegmentType( _p0+p.P0(), _p1+p.P1() );} + inline SegmentType operator - ( SegmentType const & p) const + {return SegmentType( _p0-p.P0(), _p1-p.P1() );} + inline SegmentType operator * ( const ScalarType s ) const + {return SegmentType( _p0*s, _p1*s );} + inline SegmentType operator / ( const ScalarType s ) const + {ScalarType s0=((ScalarType)1.0)/s; return SegmentType( _p0*s0, _p1*s0 );} +//@} }; // end class definition @@ -129,8 +144,9 @@ template Point3 ClosestPoint( Segment3 s, const Point3 & p) { ScalarType t = s.Projection(p); - clamp (); if (s<0) s=0; - return r.P(t); + if (s<0) return s.P0(); + if (s>1) return s.P0(); + return s.P(t); } /*@}*/