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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
|
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004-2016 \/)\/ *
* 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 $
Revision 1.9 2006/12/20 15:23:52 ganovelli
using of locally defined variable removed
Revision 1.8 2006/04/11 08:10:05 zifnab1974
changes necessary for gcc 3.4.5 on linux 64bit.
Revision 1.7 2005/12/12 11:22:32 ganovelli
compiled with gcc
Revision 1.6 2005/01/12 11:25:52 ganovelli
corrected Point<3
Revision 1.5 2004/10/20 16:45:21 ganovelli
first compiling version (MC,INtel,gcc)
Revision 1.4 2004/04/29 10:47:06 ganovelli
some siyntax error corrected
Revision 1.3 2004/04/05 12:36:43 tarini
unified version: PointBase version, with no guards "(N==3)"
Revision 1.1 2004/03/16 03:07:38 tarini
"dimensionally unified" version: first commit
****************************************************************************/
#ifndef __VCGLIB_POINT
#define __VCGLIB_POINT
#include <assert.h>
#include <vcg/math/base.h>
#include <vcg/space/space.h>
namespace vcg {
namespace ndim{
//template <int N, class S>
//class Point;
/** \addtogroup space */
/*@{*/
/**
The templated class for representing a point in R^N space.
The class is templated over the ScalarType class that is used to represent coordinates.
PointBase provides the interface and the common operators for points
of any dimensionality.
*/
template <int N, class S>
class Point
{
public:
typedef S ScalarType;
typedef VoidType ParamType;
typedef Point<N,S> PointType;
enum {Dimension=N};
protected:
/// The only data member. Hidden to user.
S _v[N];
public:
//@{
/** @name Standard Constructors and Initializers
No casting operators have been introduced to avoid automatic unattended (and costly) conversion between different PointType types
**/
inline Point () { };
// inline Point ( const S nv[N] );
/// Padding function: give a default 0 value to all the elements that are not in the [0..2] range.
/// Useful for managing in a consistent way object that could have point2 / point3 / point4
inline S Ext( const int i ) const
{
if(i>=0 && i<=N) return _v[i];
else return 0;
}
/// importer for points with different scalar type and-or dimensionality
template <int N2, class S2>
inline void Import( const Point<N2,S2> & b )
{
_v[0] = ScalarType(b[0]);
_v[1] = ScalarType(b[1]);
if (N>2) { if (N2>2) _v[2] = ScalarType(b[2]); else _v[2] = 0;};
if (N>3) { if (N2>3) _v[3] = ScalarType(b[3]); else _v[3] = 0;};
}
/// constructor for points with different scalar type and-or dimensionality
template <int N2, class S2>
static inline PointType Construct( const Point<N2,S2> & b )
{
PointType p; p.Import(b);
return p;
}
/// importer for homogeneous points
template <class S2>
inline void ImportHomo( const Point<N-1,S2> & b )
{
_v[0] = ScalarType(b[0]);
_v[1] = ScalarType(b[1]);
if (N>2) { _v[2] = ScalarType(_v[2]); };
_v[N-1] = 1.0;
}
/// constructor for homogeneus point.
template <int N2, class S2>
static inline PointType Construct( const Point<N-1,S2> & b )
{
PointType p; p.ImportHomo(b);
return p;
}
//@}
//@{
/** @name Data Access.
access to data is done by overloading of [] or explicit naming of coords (x,y,z)**/
inline S & operator [] ( const int i )
{
assert(i>=0 && i<N);
return _v[i];
}
inline const S & operator [] ( const int i ) const
{
assert(i>=0 && i<N);
return _v[i];
}
inline const S &X() const { return _v[0]; }
inline const S &Y() const { return _v[1]; }
inline const S &Z() const { static_assert(N>2); return _v[2]; }
/// W is in any case the last coordinate.
/// (in a 2D point, W() == Y(). In a 3D point, W()==Z()
/// in a 4D point, W() is a separate component)
inline const S &W() const { return _v[N-1]; }
inline S &X() { return _v[0]; }
inline S &Y() { return _v[1]; }
inline S &Z() { static_assert(N>2); return _v[2]; }
inline S &W() { return _v[N-1]; }
inline const S * V() const
{
return _v;
}
inline S * V()
{
return _v;
}
inline S & V( const int i )
{
assert(i>=0 && i<N);
return _v[i];
}
inline const S & V( const int i ) const
{
assert(i>=0 && i<N);
return _v[i];
}
//@}
//@{
/** @name Linearity for points
**/
/// sets a PointType to Zero
inline void SetZero()
{
for(unsigned int ii = 0; ii < Dimension;++ii)
V(ii) = S();
}
inline PointType operator + ( PointType const & p) const
{
PointType res;
for(unsigned int ii = 0; ii < Dimension;++ii)
res[ii] = V(ii) + p[ii];
return res;
}
inline PointType operator - ( PointType const & p) const
{
PointType res;
for(unsigned int ii = 0; ii < Dimension;++ii)
res[ii] = V(ii) - p[ii];
return res;
}
inline PointType operator * ( const S s ) const
{
PointType res;
for(unsigned int ii = 0; ii < Dimension;++ii)
res[ii] = V(ii) * s;
return res;
}
inline PointType operator / ( const S s ) const
{
PointType res;
for(unsigned int ii = 0; ii < Dimension;++ii)
res[ii] = V(ii) / s;
return res;
}
inline PointType & operator += ( PointType const & p)
{
for(unsigned int ii = 0; ii < Dimension;++ii)
V(ii) += p[ii];
return *this;
}
inline PointType & operator -= ( PointType const & p)
{
for(unsigned int ii = 0; ii < Dimension;++ii)
V(ii) -= p[ii];
return *this;
}
inline PointType & operator *= ( const S s )
{
for(unsigned int ii = 0; ii < Dimension;++ii)
V(ii) *= s;
return *this;
}
inline PointType & operator /= ( const S s )
{
for(unsigned int ii = 0; ii < Dimension;++ii)
V(ii) *= s;
return *this;
}
inline PointType operator - () const
{
PointType res;
for(unsigned int ii = 0; ii < Dimension;++ii)
res[ii] = - V(ii);
return res;
}
//@}
//@{
/** @name Dot products (cross product "%" is defined olny for 3D points)
**/
/// Dot product
inline S operator * ( PointType const & p ) const;
/// slower version, more stable (double precision only)
inline S StableDot ( const PointType & p ) const;
//@}
//@{
/** @name Norms
**/
/// Euclidean norm
inline S Norm() const;
/// Euclidean norm, static version
template <class PT> static S Norm(const PT &p );
/// Squared Euclidean norm
inline S SquaredNorm() const;
/// Squared Euclidean norm, static version
template <class PT> static S SquaredNorm(const PT &p );
/// Normalization (division by norm)
inline PointType & Normalize();
/// Normalization (division by norm), static version
template <class PT> static PointType & Normalize(const PT &p);
/// Homogeneous normalization (division by W)
inline PointType & HomoNormalize();
/// norm infinity: largest absolute value of compoenet
inline S NormInfinity() const;
/// norm 1: sum of absolute values of components
inline S NormOne() const;
//@}
/// Signed area operator
/// a % b returns the signed area of the parallelogram inside a and b
inline S operator % ( PointType const & p ) const;
/// the sum of the components
inline S Sum() const;
/// returns the biggest component
inline S Max() const;
/// returns the smallest component
inline S Min() const;
/// returns the index of the biggest component
inline int MaxI() const;
/// returns the index of the smallest component
inline int MinI() const;
/// Per component scaling
inline PointType & Scale( const PointType & p );
/// Convert to polar coordinates
void ToPolar( S & ro, S & tetha, S & fi ) const
{
ro = Norm();
tetha = (S)atan2( _v[1], _v[0] );
fi = (S)acos( _v[2]/ro );
}
//@{
/** @name Comparison Operators.
Lexicographic order.
**/
inline bool operator == ( PointType const & p ) const;
inline bool operator != ( PointType const & p ) const;
inline bool operator < ( PointType const & p ) const;
inline bool operator > ( PointType const & p ) const;
inline bool operator <= ( PointType const & p ) const;
inline bool operator >= ( PointType const & p ) const;
//@}
//@{
/** @name
Glocal to Local and viceversa
(provided for uniformity with other spatial classes. trivial for points)
**/
inline PointType LocalToGlobal(ParamType p) const{
return *this; }
inline ParamType GlobalToLocal(PointType /*p*/) const{
return ParamType(); }
//@}
}; // end class definition
template <class S>
class Point2 : public Point<2,S> {
public:
typedef S ScalarType;
typedef Point2 PointType;
using Point<2,S>::_v;
using Point<2,S>::V;
using Point<2,S>::W;
//@{
/** @name Special members for 2D points. **/
/// default
inline Point2 (){}
/// yx constructor
inline Point2 ( const S a, const S b){
_v[0]=a; _v[1]=b; };
/// unary orthogonal operator (2D equivalent of cross product)
/// returns orthogonal vector (90 deg left)
inline Point2 operator ~ () const {
return Point2 ( -_v[2], _v[1] );
}
/// returns the angle with X axis (radiants, in [-PI, +PI] )
inline ScalarType &Angle(){
return math::Atan2(_v[1],_v[0]);}
/// transform the point in cartesian coords into polar coords
inline Point2 & ToPolar(){
ScalarType t = Angle();
_v[0] = Norm();
_v[1] = t;
return *this;}
/// transform the point in polar coords into cartesian coords
inline Point2 & ToCartesian() {
ScalarType l = _v[0];
_v[0] = (ScalarType)(l*math::Cos(_v[1]));
_v[1] = (ScalarType)(l*math::Sin(_v[1]));
return *this;}
/// rotates the point of an angle (radiants, counterclockwise)
inline Point2 & Rotate( const ScalarType rad ){
ScalarType t = _v[0];
ScalarType s = math::Sin(rad);
ScalarType c = math::Cos(rad);
_v[0] = _v[0]*c - _v[1]*s;
_v[1] = t *s + _v[1]*c;
return *this;}
//@}
//@{
/** @name Implementation of standard functions for 3D points **/
inline void Zero(){
_v[0]=0; _v[1]=0; };
inline Point2 ( const S nv[2] ){
_v[0]=nv[0]; _v[1]=nv[1]; };
inline Point2 operator + ( Point2 const & p) const {
return Point2( _v[0]+p._v[0], _v[1]+p._v[1]); }
inline Point2 operator - ( Point2 const & p) const {
return Point2( _v[0]-p._v[0], _v[1]-p._v[1]); }
inline Point2 operator * ( const S s ) const {
return Point2( _v[0]*s, _v[1]*s ); }
inline Point2 operator / ( const S s ) const {
S t=1.0/s;
return Point2( _v[0]*t, _v[1]*t ); }
inline Point2 operator - () const {
return Point2 ( -_v[0], -_v[1] ); }
inline Point2 & operator += ( Point2 const & p ) {
_v[0] += p._v[0]; _v[1] += p._v[1]; return *this; }
inline Point2 & operator -= ( Point2 const & p ) {
_v[0] -= p._v[0]; _v[1] -= p._v[1]; return *this; }
inline Point2 & operator *= ( const S s ) {
_v[0] *= s; _v[1] *= s; return *this; }
inline Point2 & operator /= ( const S s ) {
S t=1.0/s; _v[0] *= t; _v[1] *= t; return *this; }
inline S Norm() const {
return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] );}
template <class PT> static S Norm(const PT &p ) {
return math::Sqrt( p.V(0)*p.V(0) + p.V(1)*p.V(1) );}
inline S SquaredNorm() const {
return ( _v[0]*_v[0] + _v[1]*_v[1] );}
template <class PT> static S SquaredNorm(const PT &p ) {
return ( p.V(0)*p.V(0) + p.V(1)*p.V(1) );}
inline S operator * ( Point2 const & p ) const {
return ( _v[0]*p._v[0] + _v[1]*p._v[1]) ; }
inline bool operator == ( Point2 const & p ) const {
return _v[0]==p._v[0] && _v[1]==p._v[1] ;}
inline bool operator != ( Point2 const & p ) const {
return _v[0]!=p._v[0] || _v[1]!=p._v[1] ;}
inline bool operator < ( Point2 const & p ) const{
return (_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<p._v[0]); }
inline bool operator > ( Point2 const & p ) const {
return (_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>p._v[0]); }
inline bool operator <= ( Point2 const & p ) {
return (_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<=p._v[0]); }
inline bool operator >= ( Point2 const & p ) const {
return (_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>=p._v[0]); }
inline Point2 & Normalize() {
PointType n = Norm(); if(n!=0.0) { n=1.0/n; _v[0]*=n; _v[1]*=n;} return *this;}
inline Point2 & HomoNormalize(){
if (_v[2]!=0.0) { _v[0] /= W(); W()=1.0; } return *this;}
inline S NormInfinity() const {
return math::Max( math::Abs(_v[0]), math::Abs(_v[1]) ); }
inline S NormOne() const {
return math::Abs(_v[0])+ math::Abs(_v[1]);}
inline S operator % ( Point2 const & p ) const {
return _v[0] * p._v[1] - _v[1] * p._v[0]; }
inline S Sum() const {
return _v[0]+_v[1];}
inline S Max() const {
return math::Max( _v[0], _v[1] ); }
inline S Min() const {
return math::Min( _v[0], _v[1] ); }
inline int MaxI() const {
return (_v[0] < _v[1]) ? 1:0; };
inline int MinI() const {
return (_v[0] > _v[1]) ? 1:0; };
inline PointType & Scale( const PointType & p ) {
_v[0] *= p._v[0]; _v[1] *= p._v[1]; return *this; }
inline S StableDot ( const PointType & p ) const {
return _v[0]*p._v[0] +_v[1]*p._v[1]; }
//@}
};
template <typename S>
class Point3 : public Point<3,S> {
public:
typedef S ScalarType;
typedef Point3<S> PointType;
using Point<3,S>::_v;
using Point<3,S>::V;
using Point<3,S>::W;
//@{
/** @name Special members for 3D points. **/
/// default
inline Point3 ():Point<3,S>(){}
/// yxz constructor
inline Point3 ( const S a, const S b, const S c){
_v[0]=a; _v[1]=b; _v[2]=c; };
/// Cross product for 3D points
inline PointType operator ^ ( PointType const & p ) const {
return Point3 (
_v[1]*p._v[2] - _v[2]*p._v[1],
_v[2]*p._v[0] - _v[0]*p._v[2],
_v[0]*p._v[1] - _v[1]*p._v[0] );
}
//@}
//@{
/** @name Implementation of standard functions for 3D points **/
inline void Zero(){
_v[0]=0; _v[1]=0; _v[2]=0; };
inline Point3 ( const S nv[3] ){
_v[0]=nv[0]; _v[1]=nv[1]; _v[2]=nv[2]; };
inline Point3 operator + ( Point3 const & p) const{
return Point3( _v[0]+p._v[0], _v[1]+p._v[1], _v[2]+p._v[2]); }
inline Point3 operator - ( Point3 const & p) const {
return Point3( _v[0]-p._v[0], _v[1]-p._v[1], _v[2]-p._v[2]); }
inline Point3 operator * ( const S s ) const {
return Point3( _v[0]*s, _v[1]*s , _v[2]*s ); }
inline Point3 operator / ( const S s ) const {
S t=1.0/s;
return Point3( _v[0]*t, _v[1]*t , _v[2]*t ); }
inline Point3 operator - () const {
return Point3 ( -_v[0], -_v[1] , -_v[2] ); }
inline Point3 & operator += ( Point3 const & p ) {
_v[0] += p._v[0]; _v[1] += p._v[1]; _v[2] += p._v[2]; return *this; }
inline Point3 & operator -= ( Point3 const & p ) {
_v[0] -= p._v[0]; _v[1] -= p._v[1]; _v[2] -= p._v[2]; return *this; }
inline Point3 & operator *= ( const S s ) {
_v[0] *= s; _v[1] *= s; _v[2] *= s; return *this; }
inline Point3 & operator /= ( const S s ) {
S t=1.0/s; _v[0] *= t; _v[1] *= t; _v[2] *= t; return *this; }
inline S Norm() const {
return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );}
template <class PT> static S Norm(const PT &p ) {
return math::Sqrt( p.V(0)*p.V(0) + p.V(1)*p.V(1) + p.V(2)*p.V(2) );}
inline S SquaredNorm() const {
return ( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );}
template <class PT> static S SquaredNorm(const PT &p ) {
return ( p.V(0)*p.V(0) + p.V(1)*p.V(1) + p.V(2)*p.V(2) );}
inline S operator * ( PointType const & p ) const {
return ( _v[0]*p._v[0] + _v[1]*p._v[1] + _v[2]*p._v[2]) ; }
inline bool operator == ( PointType const & p ) const {
return _v[0]==p._v[0] && _v[1]==p._v[1] && _v[2]==p._v[2] ;}
inline bool operator != ( PointType const & p ) const {
return _v[0]!=p._v[0] || _v[1]!=p._v[1] || _v[2]!=p._v[2] ;}
inline bool operator < ( PointType const & p ) const{
return (_v[2]!=p._v[2])?(_v[2]< p._v[2]):
(_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<p._v[0]); }
inline bool operator > ( PointType const & p ) const {
return (_v[2]!=p._v[2])?(_v[2]> p._v[2]):
(_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>p._v[0]); }
inline bool operator <= ( PointType const & p ) {
return (_v[2]!=p._v[2])?(_v[2]< p._v[2]):
(_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<=p._v[0]); }
inline bool operator >= ( PointType const & p ) const {
return (_v[2]!=p._v[2])?(_v[2]> p._v[2]):
(_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>=p._v[0]); }
inline PointType & Normalize() {
S n = Norm();
if(n!=0.0) {
n=S(1.0)/n;
_v[0]*=n; _v[1]*=n; _v[2]*=n; }
return *this;}
inline PointType & HomoNormalize(){
if (_v[2]!=0.0) { _v[0] /= W(); _v[1] /= W(); W()=1.0; }
return *this;}
inline S NormInfinity() const {
return math::Max( math::Max( math::Abs(_v[0]), math::Abs(_v[1]) ),
math::Abs(_v[3]) ); }
inline S NormOne() const {
return math::Abs(_v[0])+ math::Abs(_v[1])+math::Max(math::Abs(_v[2]));}
inline S operator % ( PointType const & p ) const {
S t = (*this)*p; /* Area, general formula */
return math::Sqrt( SquaredNorm() * p.SquaredNorm() - (t*t) );};
inline S Sum() const {
return _v[0]+_v[1]+_v[2];}
inline S Max() const {
return math::Max( math::Max( _v[0], _v[1] ), _v[2] ); }
inline S Min() const {
return math::Min( math::Min( _v[0], _v[1] ), _v[2] ); }
inline int MaxI() const {
int i= (_v[0] < _v[1]) ? 1:0; if (_v[i] < _v[2]) i=2; return i;};
inline int MinI() const {
int i= (_v[0] > _v[1]) ? 1:0; if (_v[i] > _v[2]) i=2; return i;};
inline PointType & Scale( const PointType & p ) {
_v[0] *= p._v[0]; _v[1] *= p._v[1]; _v[2] *= p._v[2]; return *this; }
inline S StableDot ( const PointType & p ) const {
S k0=_v[0]*p._v[0], k1=_v[1]*p._v[1], k2=_v[2]*p._v[2];
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;
}
//@}
};
template <typename S>
class Point4 : public Point<4,S> {
public:
typedef S ScalarType;
typedef Point4<S> PointType;
using Point<3,S>::_v;
using Point<3,S>::V;
using Point<3,S>::W;
//@{
/** @name Special members for 4D points. **/
/// default
inline Point4 (){}
/// xyzw constructor
//@}
inline Point4 ( const S a, const S b, const S c, const S d){
_v[0]=a; _v[1]=b; _v[2]=c; _v[3]=d; };
//@{
/** @name Implementation of standard functions for 3D points **/
inline void Zero(){
_v[0]=0; _v[1]=0; _v[2]=0; _v[3]=0; };
inline Point4 ( const S nv[4] ){
_v[0]=nv[0]; _v[1]=nv[1]; _v[2]=nv[2]; _v[3]=nv[3]; };
inline Point4 operator + ( Point4 const & p) const {
return Point4( _v[0]+p._v[0], _v[1]+p._v[1], _v[2]+p._v[2], _v[3]+p._v[3] ); }
inline Point4 operator - ( Point4 const & p) const {
return Point4( _v[0]-p._v[0], _v[1]-p._v[1], _v[2]-p._v[2], _v[3]-p._v[3] ); }
inline Point4 operator * ( const S s ) const {
return Point4( _v[0]*s, _v[1]*s , _v[2]*s , _v[3]*s ); }
inline PointType operator ^ ( PointType const & p ) const {
assert(0);
return *this;
}
inline Point4 operator / ( const S s ) const {
S t=1.0/s;
return Point4( _v[0]*t, _v[1]*t , _v[2]*t , _v[3]*t ); }
inline Point4 operator - () const {
return Point4 ( -_v[0], -_v[1] , -_v[2] , -_v[3] ); }
inline Point4 & operator += ( Point4 const & p ) {
_v[0] += p._v[0]; _v[1] += p._v[1]; _v[2] += p._v[2]; _v[3] += p._v[3]; return *this; }
inline Point4 & operator -= ( Point4 const & p ) {
_v[0] -= p._v[0]; _v[1] -= p._v[1]; _v[2] -= p._v[2]; _v[3] -= p._v[3]; return *this; }
inline Point4 & operator *= ( const S s ) {
_v[0] *= s; _v[1] *= s; _v[2] *= s; _v[3] *= s; return *this; }
inline Point4 & operator /= ( const S s ) {
S t=1.0/s; _v[0] *= t; _v[1] *= t; _v[2] *= t; _v[3] *= t; return *this; }
inline S Norm() const {
return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3] );}
template <class PT> static S Norm(const PT &p ) {
return math::Sqrt( p.V(0)*p.V(0) + p.V(1)*p.V(1) + p.V(2)*p.V(2) + p.V(3)*p.V(3) );}
inline S SquaredNorm() const {
return ( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3] );}
template <class PT> static S SquaredNorm(const PT &p ) {
return ( p.V(0)*p.V(0) + p.V(1)*p.V(1) + p.V(2)*p.V(2) + p.V(3)*p.V(3) );}
inline S operator * ( PointType const & p ) const {
return ( _v[0]*p._v[0] + _v[1]*p._v[1] + _v[2]*p._v[2] + _v[3]*p._v[3] ); }
inline bool operator == ( PointType const & p ) const {
return _v[0]==p._v[0] && _v[1]==p._v[1] && _v[2]==p._v[2] && _v[3]==p._v[3];}
inline bool operator != ( PointType const & p ) const {
return _v[0]!=p._v[0] || _v[1]!=p._v[1] || _v[2]!=p._v[2] || _v[3]!=p._v[3];}
inline bool operator < ( PointType const & p ) const{
return (_v[3]!=p._v[3])?(_v[3]< p._v[3]) : (_v[2]!=p._v[2])?(_v[2]< p._v[2]):
(_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<p._v[0]); }
inline bool operator > ( PointType const & p ) const {
return (_v[3]!=p._v[3])?(_v[3]> p._v[3]) : (_v[2]!=p._v[2])?(_v[2]> p._v[2]):
(_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>p._v[0]); }
inline bool operator <= ( PointType const & p ) {
return (_v[3]!=p._v[3])?(_v[3]< p._v[3]) : (_v[2]!=p._v[2])?(_v[2]< p._v[2]):
(_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<=p._v[0]); }
inline bool operator >= ( PointType const & p ) const {
return (_v[3]!=p._v[3])?(_v[3]> p._v[3]) : (_v[2]!=p._v[2])?(_v[2]> p._v[2]):
(_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>=p._v[0]); }
inline PointType & Normalize() {
PointType n = Norm(); if(n!=0.0) { n=1.0/n; _v[0]*=n; _v[1]*=n; _v[2]*=n; _v[3]*=n; }
return *this;};
template <class PT> PointType & Normalize(const PT &p){
PointType n = Norm(); if(n!=0.0) { n=1.0/n; V(0)*=n; V(1)*=n; V(2)*=n; V(3)*=n; }
return *this;};
inline PointType & HomoNormalize(){
if (_v[3]!=0.0) { _v[0] /= W(); _v[1] /= W(); _v[2] /= W(); W()=1.0; }
return *this;};
inline S NormInfinity() const {
return math::Max( math::Max( math::Abs(_v[0]), math::Abs(_v[1]) ),
math::Max( math::Abs(_v[2]), math::Abs(_v[3]) ) ); }
inline S NormOne() const {
return math::Abs(_v[0])+ math::Abs(_v[1])+math::Max(math::Abs(_v[2]),math::Abs(_v[3]));}
inline S operator % ( PointType const & p ) const {
S t = (*this)*p; /* Area, general formula */
return math::Sqrt( SquaredNorm() * p.SquaredNorm() - (t*t) );};
inline S Sum() const {
return _v[0]+_v[1]+_v[2]+_v[3];}
inline S Max() const {
return math::Max( math::Max( _v[0], _v[1] ), math::Max( _v[2], _v[3] )); }
inline S Min() const {
return math::Min( math::Min( _v[0], _v[1] ), math::Min( _v[2], _v[3] )); }
inline int MaxI() const {
int i= (_v[0] < _v[1]) ? 1:0; if (_v[i] < _v[2]) i=2; if (_v[i] < _v[3]) i=3;
return i;};
inline int MinI() const {
int i= (_v[0] > _v[1]) ? 1:0; if (_v[i] > _v[2]) i=2; if (_v[i] > _v[3]) i=3;
return i;};
inline PointType & Scale( const PointType & p ) {
_v[0] *= p._v[0]; _v[1] *= p._v[1]; _v[2] *= p._v[2]; _v[3] *= p._v[3]; return *this; }
inline S StableDot ( const PointType & p ) const {
S k0=_v[0]*p._v[0], k1=_v[1]*p._v[1], k2=_v[2]*p._v[2], k3=_v[3]*p._v[3];
int exp0,exp1,exp2,exp3;
frexp( double(k0), &exp0 );frexp( double(k1), &exp1 );
frexp( double(k2), &exp2 );frexp( double(k3), &exp3 );
if (exp0>exp1) { std::swap(k0,k1); std::swap(exp0,exp1); }
if (exp2>exp3) { std::swap(k2,k3); std::swap(exp2,exp3); }
if (exp0>exp2) { std::swap(k0,k2); std::swap(exp0,exp2); }
if (exp1>exp3) { std::swap(k1,k3); std::swap(exp1,exp3); }
if (exp2>exp3) { std::swap(k2,k3); std::swap(exp2,exp3); }
return ( (k0 + k1) + k2 ) +k3; }
//@}
};
template <class S>
inline S Angle( Point3<S> const & p1, Point3<S> const & p2 )
{
S w = p1.Norm()*p2.Norm();
if(w==0) return -1;
S t = (p1*p2)/w;
if(t>1) t = 1;
else if(t<-1) t = -1;
return (S) acos(t);
}
// versione uguale alla precedente ma che assume che i due vettori siano unitari
template <class S>
inline S AngleN( Point3<S> const & p1, Point3<S> const & p2 )
{
S w = p1*p2;
if(w>1)
w = 1;
else if(w<-1)
w=-1;
return (S) acos(w);
}
template <int N,class S>
inline S Norm( Point<N,S> const & p )
{
return p.Norm();
}
template <int N,class S>
inline S SquaredNorm( Point<N,S> const & p )
{
return p.SquaredNorm();
}
template <int N,class S>
inline Point<N,S> & Normalize( Point<N,S> & p )
{
p.Normalize();
return p;
}
template <int N, class S>
inline S Distance( Point<N,S> const & p1,Point<N,S> const & p2 )
{
return (p1-p2).Norm();
}
template <int N, class S>
inline S SquaredDistance( Point<N,S> const & p1,Point<N,S> const & p2 )
{
return (p1-p2).SquaredNorm();
}
//template <typename S>
//struct Point2:public Point<2,S>{
// inline Point2(){};
// inline Point2(Point<2,S> const & p):Point<2,S>(p){} ;
// inline Point2( const S a, const S b):Point<2,S>(a,b){};
//};
//
//template <typename S>
//struct Point3:public Point3<S> {
// inline Point3(){};
// inline Point3(Point3<S> const & p):Point3<S> (p){}
// inline Point3( const S a, const S b, const S c):Point3<S> (a,b,c){};
//};
//
//
//template <typename S>
//struct Point4:public Point4<S>{
// inline Point4(){};
// inline Point4(Point4<S> const & p):Point4<S>(p){}
// inline Point4( const S a, const S b, const S c, const S d):Point4<S>(a,b,c,d){};
//};
typedef Point2<short> Point2s;
typedef Point2<int> Point2i;
typedef Point2<float> Point2f;
typedef Point2<double> Point2d;
typedef Point2<short> Vector2s;
typedef Point2<int> Vector2i;
typedef Point2<float> Vector2f;
typedef Point2<double> Vector2d;
typedef Point3<short> Point3s;
typedef Point3<int> Point3i;
typedef Point3<float> Point3f;
typedef Point3<double> Point3d;
typedef Point3<short> Vector3s;
typedef Point3<int> Vector3i;
typedef Point3<float> Vector3f;
typedef Point3<double> Vector3d;
typedef Point4<short> Point4s;
typedef Point4<int> Point4i;
typedef Point4<float> Point4f;
typedef Point4<double> Point4d;
typedef Point4<short> Vector4s;
typedef Point4<int> Vector4i;
typedef Point4<float> Vector4f;
typedef Point4<double> Vector4d;
/*@}*/
//added only for backward compatibility
template<unsigned int N,typename S>
struct PointBase : Point<N,S>
{
PointBase()
:Point<N,S>()
{
}
};
} // end namespace ndim
} // end namespace vcg
#endif
|