From 43f74f4627a8a83b29d4f055688b73bbb2f4f1aa Mon Sep 17 00:00:00 2001 From: mtarini Date: Mon, 8 Mar 2004 19:46:47 +0000 Subject: [PATCH] First Version (tarini) --- vcg/space/line3.h | 17 ++++-- vcg/space/ray3.h | 122 ++++++++++++++++++++++++++++++++++++++++ vcg/space/segment3.h | 129 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+), 5 deletions(-) create mode 100644 vcg/space/ray3.h create mode 100644 vcg/space/segment3.h diff --git a/vcg/space/line3.h b/vcg/space/line3.h index 4e99cf5f..6ecec6f0 100644 --- a/vcg/space/line3.h +++ b/vcg/space/line3.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.1 2004/03/08 16:15:48 tarini +first version (tarini) + ****************************************************************************/ @@ -79,14 +82,14 @@ public: Line3() {}; /// The (origin, direction) constructor LineType(const PointType &ori, const PointType &dir) { _ori=ori; _dir=dir; }; - /// Operator to compare two bounding box + /// Operator to compare two lines inline bool operator == ( LineType const & p ) const { return _ori==p._ori && _dir==p._dir; } - /// Operator to dispare two bounding box + /// Operator to dispare two lines inline bool operator != ( LineType const & p ) const { return _ori!=p._ori || _dir!=p._dir; } - /// initializes the bounding box - inline PointType Projection( const PointType &p ) const + /// Projects a point on the line + inline ScalarType Projection( const PointType &p ) const { ScalarType l = dire.SquaredNorm(); return ScalarType((p-_ori)*_dir/l); } ///set up of the line. @@ -100,7 +103,11 @@ public: { _dir.Normalize(); return *this;} static LineType &Normalize(LineType &p) { p.Normalize(); return p;} - + /// importer for different line types + template + inline void Import( const Line3 & b ) + { _ori.Import( b._ori); _dir.Import( b._dir); + } }; // end class definition diff --git a/vcg/space/ray3.h b/vcg/space/ray3.h new file mode 100644 index 00000000..35c799c1 --- /dev/null +++ b/vcg/space/ray3.h @@ -0,0 +1,122 @@ +/**************************************************************************** +* VCGLib o o * +* Visual and Computer Graphics Library o o * +* _ O _ * +* Copyright(C) 2004 \/)\/ * +* Visual Computing Lab /\/| * +* ISTI - Italian National Research Council | * +* \ * +* All rights reserved. * +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +/**************************************************************************** + History + +$Log: not supported by cvs2svn $ + + +****************************************************************************/ + + + +#ifndef __VCGLIB_RAY3 +#define __VCGLIB_RAY3 + +#include + +namespace vcg { + +/** \addtogroup space */ +/*@{*/ +/** +Templated class for 3D ray. + This is the class for a semi-line in 3D space. A ray is stored just as origin and direction (Point3). + @param RayScalarType (template parameter) Specifies the type of scalar used to represent coords. +*/ +template +class Ray3 +{ +public: + + /// The scalar type + typedef RayScalarType ScalarType; + + /// The point type + typedef Point3 PointType; + + /// The point type + typedef Ray3 RayType; + +private: + + /// Origin + PointType _ori; + + /// Direction (not necessarily normalized) + PointType _dir; + +public: + + /// Members to access the origin, direction + inline const PointType &Ori() const { return _ori; } + inline const PointType &Dir() const { return _dir; } + inline PointType &Ori() { return _ori; } + inline PointType &Dir() { return _dir; } + /// The empty constructor + Ray3() {}; + /// The (a,b) constructor + RayType(const PointType &a, const PointType &b) { _p0=a; _p1=b; }; + /// Operator to compare two rays + inline bool operator == ( LineType const & p ) const + { return _ori==p._ori && _dir==p._dir; } + /// Operator to dispare two rays + inline bool operator != ( LineType const & p ) const + { return _ori!=p._ori || _dir!=p._dir; } + /// Projects a point on the ray (returns distance) + /// projection exists iff result > 0 + inline ScalarType Projection( const PointType &p ) const + { ScalarType l = dire.SquaredNorm(); + return ScalarType((p-_ori)*_dir/l); } + ///set up of the line. + void Set( const PointType & ori, const PointType & dir ) + { _ori = ori; _dir=dir } + /// calculates the point of parameter t on the ray. + /// if t>0, point is on the ray + inline PointType P( const ScalarType t ) const + { return orig + dire * t; } + /// normalizes direction field + LineType &Normalize() + { _dir.Normalize(); return *this;} + static LineType &Normalize(LineType &p) + { p.Normalize(); return p;} + /// importer for different Ray types + template + inline void Import( const Ray3 & b ) + { _ori.Import( b._ori); _dir.Import( b._dir); + } + +}; // end class definition + + + +typedef Ray3 Ray3s; +typedef Ray3 Ray3i; +typedef Ray3 Ray3f; +typedef Ray3 Ray3d; + + +/*@}*/ + +} // end namespace +#endif \ No newline at end of file diff --git a/vcg/space/segment3.h b/vcg/space/segment3.h new file mode 100644 index 00000000..8f6a3305 --- /dev/null +++ b/vcg/space/segment3.h @@ -0,0 +1,129 @@ +/**************************************************************************** +* VCGLib o o * +* Visual and Computer Graphics Library o o * +* _ O _ * +* Copyright(C) 2004 \/)\/ * +* Visual Computing Lab /\/| * +* ISTI - Italian National Research Council | * +* \ * +* All rights reserved. * +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +/**************************************************************************** + History + +$Log: not supported by cvs2svn $ + + +****************************************************************************/ + + + +#ifndef __VCGLIB_SEGMENT3 +#define __VCGLIB_SEGMENT3 + +#include +#include + +namespace vcg { + +/** \addtogroup space */ +/*@{*/ +/** +Templated class for 3D segment. + This is the class for a segment in 3D space. A Segment is stored just as its two extrema (Point3). + @param SegmentScalarType (template parameter) Specifies the type of scalar used to represent coords. +*/ +template +class Segment3 +{ +public: + + /// The scalar type + typedef SegmentScalarType ScalarType; + + /// The point type + typedef Point3 PointType; + + /// The point type + typedef Segment3 SegmentType; + +private: + + /// _extrema + PointType _p0,_p1; + +public: + + /// Members to access either extrema + inline const PointType &P0() const { return _p0; } + inline const PointType &P1() const { return _p1; } + inline PointType &P0() { return _p0; } + inline PointType &P1() { return _p1; } + /// The empty constructor + Segment3() {}; + /// The (a,b) constructor + SegmentType(const PointType &a, const PointType &b) { _p0=a; _p1=b; }; + /// Operator to compare two bounding box + inline bool operator == ( SegmentType const & p ) const + { return _p0==p._p0 && _p1==p._p1; } + /// Operator to dispare two bounding box + inline bool operator != ( SegmentType const & p ) const + { return _p0!=p._p0 || _p1!=p._p1; } + /// initializes the bounding box + void Set( const PointType &a, const PointType &b) + { _p0=a; _p1=b;} + /// calculates the point of parameter t on the segment. + /// if t is in [0..1] returned point is inside the segment + inline PointType P( const ScalarType t ) const + { return _p0 + (_p1 - _p0) * t; } + /// return the middle point + inline PointType MidPoint( ) const + { return ( _p0 + _p1) / ScalarType(2.0) ; } + /// return the bounding box + inline Box3 BBox( ) const + { Box3 t; + if (_p0[0]<_p1[0]) { t.min[0]=_p0[0];t.max[0]=_p1[0];} else { t.min[0]=_p1[0];t.max[0]=_p0[0];} + 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 + SegmentType &Length() + { return (_p0 - _p1).Norm(); } + /// return 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 + inline void Import( const Segment3 & b ) + { _p0.Import( b._p0); _p1.Import( b._p1); + } + +}; // end class definition + + + +typedef Segment3 Segment3s; +typedef Segment3 Segment3i; +typedef Segment3 Segment3f; +typedef Segment3 Segment3d; + + +/*@}*/ + +} // end namespace +#endif \ No newline at end of file