2004-03-03 15:22:48 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
2016-06-13 07:29:25 +02:00
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
2004-03-03 15:22:48 +01:00
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
2008-10-29 01:05:44 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
2004-03-03 15:22:48 +01:00
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2004-03-07 23:45:32 +01:00
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
#ifndef VCG_USE_EIGEN
|
|
|
|
#include "deprecated_point3.h"
|
|
|
|
#else
|
2004-03-03 15:22:48 +01:00
|
|
|
|
|
|
|
#ifndef __VCGLIB_POINT3
|
|
|
|
#define __VCGLIB_POINT3
|
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
#include "../math/eigen.h"
|
2004-03-03 15:22:48 +01:00
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
namespace vcg{
|
2008-10-28 21:06:17 +01:00
|
|
|
template<typename Scalar> class Point3;
|
2008-10-27 20:35:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace Eigen{
|
2008-10-28 21:06:17 +01:00
|
|
|
|
|
|
|
template<typename Scalar> struct ei_traits<vcg::Point3<Scalar> > : ei_traits<Eigen::Matrix<Scalar,3,1> > {};
|
make point2 derived Eigen's Matrix, and a set of minimal fixes to make meshlab compile
with both old and new version. The fixes include:
- dot product: vec0 * vec1 => vec0.dot(vec1) (I added .dot() to the old Point classes too)
- Transpose: Transpose is an Eigen type, so we cannot keep it if Eigen is used. Therefore
I added a .tranpose() to old matrix classes, and modified most of the Transpose() to transpose()
both in vcg and meshlab. In fact, transpose() are free with Eigen, it simply returns a transpose
expression without copies. On the other be carefull: m = m.transpose() won't work as expected,
here me must evaluate to a temporary: m = m.transpose().eval(); However, this operation in very
rarely needed: you transpose at the same sime you set m, or you use m.transpose() directly.
- the last issue is Normalize which both modifies *this and return a ref to it. This behavior
don't make sense anymore when using expression template, e.g., in (a+b).Normalize(), the type
of a+b if not a Point (or whatever Vector types), it an expression of the addition of 2 points,
so we cannot modify the value of *this, since there is no value. Therefore I've already changed
all those .Normalize() of expressions to the Eigen's version .normalized().
- Finally I've changed the Zero to SetZero in the old Point classes too.
2008-10-28 01:59:46 +01:00
|
|
|
|
2008-10-29 01:05:44 +01:00
|
|
|
template<typename XprType> struct ei_to_vcgtype<XprType,3,1,0,3,1>
|
|
|
|
{ typedef vcg::Point3<typename XprType::Scalar> type; };
|
|
|
|
|
make point2 derived Eigen's Matrix, and a set of minimal fixes to make meshlab compile
with both old and new version. The fixes include:
- dot product: vec0 * vec1 => vec0.dot(vec1) (I added .dot() to the old Point classes too)
- Transpose: Transpose is an Eigen type, so we cannot keep it if Eigen is used. Therefore
I added a .tranpose() to old matrix classes, and modified most of the Transpose() to transpose()
both in vcg and meshlab. In fact, transpose() are free with Eigen, it simply returns a transpose
expression without copies. On the other be carefull: m = m.transpose() won't work as expected,
here me must evaluate to a temporary: m = m.transpose().eval(); However, this operation in very
rarely needed: you transpose at the same sime you set m, or you use m.transpose() directly.
- the last issue is Normalize which both modifies *this and return a ref to it. This behavior
don't make sense anymore when using expression template, e.g., in (a+b).Normalize(), the type
of a+b if not a Point (or whatever Vector types), it an expression of the addition of 2 points,
so we cannot modify the value of *this, since there is no value. Therefore I've already changed
all those .Normalize() of expressions to the Eigen's version .normalized().
- Finally I've changed the Zero to SetZero in the old Point classes too.
2008-10-28 01:59:46 +01:00
|
|
|
template<typename Scalar>
|
|
|
|
struct NumTraits<vcg::Point3<Scalar> > : NumTraits<Scalar>
|
|
|
|
{
|
|
|
|
enum {
|
|
|
|
ReadCost = 3,
|
|
|
|
AddCost = 3,
|
|
|
|
MulCost = 3
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
}
|
|
|
|
|
2004-03-03 15:22:48 +01:00
|
|
|
namespace vcg {
|
2004-10-13 14:45:51 +02:00
|
|
|
|
2008-10-28 21:06:17 +01:00
|
|
|
template<typename Scalar> class Box3;
|
|
|
|
|
2004-03-03 15:22:48 +01:00
|
|
|
/** \addtogroup space */
|
|
|
|
/*@{*/
|
2008-10-28 21:06:17 +01:00
|
|
|
/**
|
|
|
|
The templated class for representing a point in 3D space.
|
|
|
|
The class is templated over the ScalarType class that is used to represent coordinates. All the usual
|
|
|
|
operator overloading (* + - ...) is present.
|
|
|
|
*/
|
2008-10-27 20:35:17 +01:00
|
|
|
template <class _Scalar> class Point3 : public Eigen::Matrix<_Scalar,3,1>
|
2004-03-03 15:22:48 +01:00
|
|
|
{
|
2008-10-28 21:06:17 +01:00
|
|
|
//----------------------------------------
|
|
|
|
// template typedef part
|
|
|
|
// use it as follow: typename Point3<S>::Type instead of simply Point3<S>
|
|
|
|
//----------------------------------------
|
|
|
|
public:
|
|
|
|
typedef Eigen::Matrix<_Scalar,3,1> Type;
|
|
|
|
//----------------------------------------
|
|
|
|
// inheritence part
|
|
|
|
//----------------------------------------
|
|
|
|
private:
|
2008-10-27 20:35:17 +01:00
|
|
|
typedef Eigen::Matrix<_Scalar,3,1> _Base;
|
2004-03-03 15:22:48 +01:00
|
|
|
public:
|
2008-10-29 12:28:51 +01:00
|
|
|
|
|
|
|
using _Base::Construct;
|
2008-10-27 20:35:17 +01:00
|
|
|
_EIGEN_GENERIC_PUBLIC_INTERFACE(Point3,_Base);
|
|
|
|
VCG_EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Point3)
|
2004-03-03 15:22:48 +01:00
|
|
|
|
2008-10-29 01:05:44 +01:00
|
|
|
/** @name Standard Constructors and Initializers
|
2004-03-03 15:22:48 +01:00
|
|
|
No casting operators have been introduced to avoid automatic unattended (and costly) conversion between different point types
|
|
|
|
**/
|
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
inline Point3 () {}
|
|
|
|
inline Point3 ( const Scalar nx, const Scalar ny, const Scalar nz ) : Base(nx,ny,nz) {}
|
|
|
|
inline Point3 ( Point3 const & p ) : Base(p) {}
|
|
|
|
inline Point3 ( const Scalar nv[3] ) : Base(nv) {}
|
|
|
|
template<typename OtherDerived>
|
|
|
|
inline Point3(const Eigen::MatrixBase<OtherDerived>& other) : Base(other) {}
|
|
|
|
|
2004-03-03 15:22:48 +01:00
|
|
|
|
2008-10-28 21:06:17 +01:00
|
|
|
// this one is very useless
|
2008-10-29 01:05:44 +01:00
|
|
|
template <class Q>
|
2006-10-13 14:59:24 +02:00
|
|
|
static inline Point3 Construct( const Q & P0, const Q & P1, const Q & P2)
|
|
|
|
{
|
2008-10-27 20:35:17 +01:00
|
|
|
return Point3(Scalar(P0),Scalar(P1),Scalar(P2));
|
2006-10-13 14:59:24 +02:00
|
|
|
}
|
2008-10-28 21:06:17 +01:00
|
|
|
vcg::Box3<_Scalar> GetBBox(vcg::Box3<_Scalar> &bb) const;
|
2006-10-13 14:59:24 +02:00
|
|
|
|
2008-10-28 21:06:17 +01:00
|
|
|
}; // end class definition (Point3)
|
2004-03-03 15:22:48 +01:00
|
|
|
|
|
|
|
// Dot product preciso numericamente (solo double!!)
|
|
|
|
// Implementazione: si sommano i prodotti per ordine di esponente
|
|
|
|
// (prima le piu' grandi)
|
2008-10-27 20:35:17 +01:00
|
|
|
template<class Scalar>
|
|
|
|
double stable_dot ( Point3<Scalar> const & p0, Point3<Scalar> const & p1 )
|
2004-03-03 15:22:48 +01:00
|
|
|
{
|
2008-10-27 20:35:17 +01:00
|
|
|
Scalar k0 = p0.data()[0]*p1.data()[0];
|
|
|
|
Scalar k1 = p0.data()[1]*p1.data()[1];
|
|
|
|
Scalar k2 = p0.data()[2]*p1.data()[2];
|
2004-03-03 15:22:48 +01:00
|
|
|
|
|
|
|
int exp0,exp1,exp2;
|
|
|
|
|
|
|
|
frexp( double(k0), &exp0 );
|
|
|
|
frexp( double(k1), &exp1 );
|
|
|
|
frexp( double(k2), &exp2 );
|
|
|
|
|
|
|
|
if( exp0<exp1 )
|
|
|
|
{
|
|
|
|
if(exp0<exp2)
|
|
|
|
return (k1+k2)+k0;
|
|
|
|
else
|
|
|
|
return (k0+k1)+k2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(exp1<exp2)
|
|
|
|
return(k0+k2)+k1;
|
|
|
|
else
|
|
|
|
return (k0+k1)+k2;
|
|
|
|
}
|
2008-10-28 21:06:17 +01:00
|
|
|
}
|
2004-03-03 15:22:48 +01:00
|
|
|
|
|
|
|
/// Point(p) Edge(v1-v2) dist, q is the point in v1-v2 with min dist
|
2008-10-27 20:35:17 +01:00
|
|
|
template<class Scalar>
|
|
|
|
Scalar PSDist( const Point3<Scalar> & p,
|
|
|
|
const Point3<Scalar> & v1,
|
|
|
|
const Point3<Scalar> & v2,
|
|
|
|
Point3<Scalar> & q )
|
2004-03-03 15:22:48 +01:00
|
|
|
{
|
2008-10-27 20:35:17 +01:00
|
|
|
Point3<Scalar> e = v2-v1;
|
|
|
|
Scalar t = ((p-v1).dot(e))/e.SquaredNorm();
|
2004-03-03 15:22:48 +01:00
|
|
|
if(t<0) t = 0;
|
|
|
|
else if(t>1) t = 1;
|
|
|
|
q = v1+e*t;
|
|
|
|
return Distance(p,q);
|
|
|
|
}
|
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
template <class Scalar>
|
|
|
|
void GetUV( Point3<Scalar> &n,Point3<Scalar> &u, Point3<Scalar> &v, Point3<Scalar> up=(Point3<Scalar>(0,1,0)) )
|
2004-11-09 16:49:07 +01:00
|
|
|
{
|
|
|
|
n.Normalize();
|
|
|
|
const double LocEps=double(1e-7);
|
|
|
|
u=n^up;
|
|
|
|
double len = u.Norm();
|
|
|
|
if(len < LocEps)
|
|
|
|
{
|
|
|
|
if(fabs(n[0])<fabs(n[1])){
|
2008-10-27 20:35:17 +01:00
|
|
|
if(fabs(n[0])<fabs(n[2])) up=Point3<Scalar>(1,0,0); // x is the min
|
|
|
|
else up=Point3<Scalar>(0,0,1); // z is the min
|
2004-11-09 16:49:07 +01:00
|
|
|
}else {
|
2008-10-27 20:35:17 +01:00
|
|
|
if(fabs(n[1])<fabs(n[2])) up=Point3<Scalar>(0,1,0); // y is the min
|
|
|
|
else up=Point3<Scalar>(0,0,1); // z is the min
|
2004-11-09 16:49:07 +01:00
|
|
|
}
|
|
|
|
u=n^up;
|
|
|
|
}
|
|
|
|
u.Normalize();
|
|
|
|
v=n^u;
|
|
|
|
v.Normalize();
|
2008-10-27 20:35:17 +01:00
|
|
|
Point3<Scalar> uv=u^v;
|
2004-11-09 16:49:07 +01:00
|
|
|
}
|
|
|
|
|
2008-10-28 21:06:17 +01:00
|
|
|
/*@}*/
|
2005-11-09 17:11:55 +01:00
|
|
|
|
2004-03-03 15:22:48 +01:00
|
|
|
typedef Point3<short> Point3s;
|
|
|
|
typedef Point3<int> Point3i;
|
|
|
|
typedef Point3<float> Point3f;
|
|
|
|
typedef Point3<double> Point3d;
|
|
|
|
|
2008-10-28 21:06:17 +01:00
|
|
|
// typedef Eigen::Matrix<short ,3,1> Point3s;
|
|
|
|
// typedef Eigen::Matrix<int ,3,1> Point3i;
|
|
|
|
// typedef Eigen::Matrix<float ,3,1> Point3f;
|
|
|
|
// typedef Eigen::Matrix<double,3,1> Point3d;
|
|
|
|
// typedef Eigen::Matrix<short ,3,1> Vector3s;
|
|
|
|
// typedef Eigen::Matrix<int ,3,1> Vector3i;
|
|
|
|
// typedef Eigen::Matrix<float ,3,1> Vector3f;
|
|
|
|
// typedef Eigen::Matrix<double,3,1> Vector3d;
|
2004-10-13 14:45:51 +02:00
|
|
|
|
2004-03-03 15:22:48 +01:00
|
|
|
} // end namespace
|
2004-10-13 14:45:51 +02:00
|
|
|
|
2004-03-03 15:22:48 +01:00
|
|
|
#endif
|
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
#endif
|