19 #if defined(WIN32) || defined(_WIN32) 30 #if defined(WIN32) || defined(_WIN32) 36 #if defined(WIN32) || defined(_WIN32) 38 # define snprintf _snprintf 43 #ifndef FLOATINGPOINT_PRECISION 44 # define FLOATINGPOINT_PRECISION 1 51 #if FLOATINGPOINT_PRECISION==1 53 # define VECTOR_EPSILON (1e-6f) 56 # define VECTOR_EPSILON (1e-10) 60 # define M_PI 3.1415926536 63 # define M_E 2.7182818284 81 inline Vector3D (
const float * v) : x((S)v[0]), y((S)v[1]), z((S)v[2]) {}
84 inline Vector3D (
const double * v) : x((S)v[0]), y((S)v[1]), z((S)v[2]) {}
90 inline Vector3D ( S vx, S vy, S vz) : x(vx), y(vy), z(vz) {}
169 return ( x<y ) ? ( ( x<z ) ? x:z ) : ( ( y<z ) ? y:z );
173 return ( x>y ) ? ( ( x>z ) ? x:z ) : ( ( y>z ) ? y:z );
178 return x==0 && y==0 && z==0;
215 inline Vector3D ( S vx, S vy, S vz, S vDummy) : x(vx), y(vy), z(vz) {}
228 return Vector3D<S> ( v1.x+v2.x, v1.y+v2.y, v1.z+v2.z );
231 template<
class S,
class S2>
236 template<
class S,
class S2>
244 return Vector3D<S> ( v1.x-v2.x, v1.y-v2.y, v1.z-v2.z );
247 template<
class S,
class S2>
252 template<
class S,
class S2>
260 return Vector3D<S> ( v1.x*v2.x, v1.y*v2.y, v1.z*v2.z );
263 template<
class S,
class S2>
268 template<
class S,
class S2>
276 return Vector3D<S> ( v1.x/v2.x, v1.y/v2.y, v1.z/v2.z );
279 template<
class S,
class S2>
284 template<
class S,
class S2>
292 return s1.x == s2.x && s1.y == s2.y && s1.z == s2.z;
298 return s1.x != s2.x || s1.y != s2.y || s1.z != s2.z;
308 return Vector3D<S>(std::min(s1.x,s2.x), std::min(s1.y,s2.y), std::min(s1.z,s2.z));
312 template<
class S,
class S2>
314 return Vector3D<S>(std::min(s1.x,s2), std::min(s1.y,s2), std::min(s1.z,s2));
318 template<
class S1,
class S>
320 return Vector3D<S>(std::min(s1,s2.x), std::min(s1,s2.y), std::min(s1,s2.z));
326 return Vector3D<S>(std::max(s1.x,s2.x), std::max(s1.y,s2.y), std::max(s1.z,s2.z));
330 template<
class S,
class S2>
332 return Vector3D<S>(std::max(s1.x,s2), std::max(s1.y,s2), std::max(s1.z,s2));
336 template<
class S1,
class S>
338 return Vector3D<S>(std::max(s1,s2.x), std::max(s1,s2.y), std::max(s1,s2.z));
344 return t.x*v.x + t.y*v.y + t.z*v.z;
351 ( ( t.y*v.z ) - ( t.z*v.y ) ),
352 ( ( t.z*v.x ) - ( t.x*v.z ) ),
353 ( ( t.x*v.y ) - ( t.y*v.x ) ) );
365 S sprod = dot (v, n);
366 return v - n * dot(v, n);
373 S l = v.x*v.x + v.y*v.y + v.z*v.z;
374 if ( l <= VECTOR_EPSILON*VECTOR_EPSILON )
return(0.);
375 return ( fabs ( l-1. ) < VECTOR_EPSILON*VECTOR_EPSILON ) ? 1. : sqrt ( l );
381 return v.x*v.x + v.y*v.y + v.z*v.z;
385 inline Real norm(
const Real v) {
return fabs(v); }
386 inline Real normSquare(
const Real v) {
return square(v); }
387 inline Real norm(
const int v) {
return abs(v); }
388 inline Real normSquare(
const int v) {
return square(v); }
393 S l = v.x*v.x + v.y*v.y + v.z*v.z;
394 if ( fabs ( l-1. ) < VECTOR_EPSILON*VECTOR_EPSILON )
396 else if ( l > VECTOR_EPSILON*VECTOR_EPSILON )
398 S fac = 1./sqrt ( l );
410 S l = v.x*v.x + v.y*v.y + v.z*v.z;
411 if ( fabs ( l-1. ) < VECTOR_EPSILON*VECTOR_EPSILON ) {
413 }
else if ( l > VECTOR_EPSILON*VECTOR_EPSILON ) {
430 int maxIndex= ( fabs ( v.x ) > fabs ( v.y ) ) ? 0 : 1;
431 maxIndex= ( fabs ( v[maxIndex] ) > fabs ( v.z ) ) ? maxIndex : 2;
436 o[ ( maxIndex+1 ) %3]= 1;
452 if ( fabs ( v.y ) < VECTOR_EPSILON )
454 else if ( fabs ( v.x ) < VECTOR_EPSILON && fabs ( v.z ) < VECTOR_EPSILON )
455 theta = ( v.y>=0 ) ? 0:M_PI;
457 theta = atan ( sqrt ( v.x*v.x+v.z*v.z ) /v.y );
458 if ( theta<0 ) theta+=M_PI;
460 if ( fabs ( v.x ) < VECTOR_EPSILON )
463 phi = atan ( v.z/v.x );
464 if ( phi<0 ) phi+=M_PI;
465 if ( fabs ( v.z ) < VECTOR_EPSILON )
466 phi = ( v.x>=0 ) ? 0 : M_PI;
481 Vector3D<S> nn= ( dot ( t, n ) > 0.0 ) ? ( n*-1.0 ) : n;
482 return ( t - nn * ( 2.0 * dot ( nn, t ) ) );
497 S n = -dot ( t, normal );
498 S tt = 1.0 + eta*eta* ( n*n-1.0 );
504 tt = eta*n - sqrt ( tt );
505 return ( t*eta + normal*tt );
513 snprintf ( buf,256,
"[%+4.6f,%+4.6f,%+4.6f]", (
double ) ( *
this ) [0], (
double ) ( *
this ) [1], (
double ) ( *
this ) [2] );
516 return std::string ( buf );
525 std::ostream& operator<< ( std::ostream& os, const Vector3D<S>& i ) {
536 is >> c >> i[0] >> dummy >> i[1] >> dummy >> i[2] >> c;
551 template<
class T>
inline Vec3 toVec3 ( T v ) {
552 return Vec3 ( v[0],v[1],v[2] );
556 template<
class T>
inline Vec3i toVec3i ( T v ) {
557 return Vec3i ( (
int ) v[0], (
int ) v[1], (
int ) v[2] );
561 template<
class T>
inline Vec3i toVec3i ( T v0, T v1, T v2 ) {
562 return Vec3i ( (
int ) v0, (
int ) v1, (
int ) v2 );
566 template<
class T>
inline Vec3i toVec3iRound ( T v ) {
567 return Vec3i ( (
int ) round ( v[0] ), (
int ) round ( v[1] ), (
int ) round ( v[2] ) );
571 template<
class T>
inline Vec3i toVec3iChecked ( T v ) {
573 for (
size_t i=0; i<3; i++) {
575 if (fabs(a-floor(a+0.5)) > 1e-5)
576 errMsg(
"argument is not an int, cannot convert");
577 ret[i] = (int) (a+0.5);
597 template<>
inline Vec3 clamp<Vec3>(
const Vec3& a,
const Vec3& b,
const Vec3& c) {
598 return Vec3 ( clamp(a.x, b.x, c.x),
599 clamp(a.y, b.y, c.y),
600 clamp(a.z, b.z, c.z) );
602 template<>
inline Vec3 safeDivide<Vec3>(
const Vec3 &a,
const Vec3& b) {
603 return Vec3(safeDivide(a.x,b.x), safeDivide(a.y,b.y), safeDivide(a.z,b.z));
605 template<>
inline Vec3 nmod<Vec3>(
const Vec3& a,
const Vec3& b) {
606 return Vec3(nmod(a.x,b.x),nmod(a.y,b.y),nmod(a.z,b.z));
Vector3D()
Constructor.
Definition: vectorbase.h:75
Definition: commonkernels.h:22
Vector3D(const Vector3D< S > &v)
Copy-Constructor.
Definition: vectorbase.h:78
const Vector3D< S > & operator=(const Vector3D< S > &v)
Assignment operator.
Definition: vectorbase.h:95
std::string toString() const
debug output vector to a string
S & operator[](unsigned int i)
access operator
Definition: vectorbase.h:182
Basic inlined vector class.
Definition: vectorbase.h:71
static const Vector3D< S > Zero
zero element
Definition: vectorbase.h:212
Vector3D(S vx, S vy, S vz)
Construct a vector from three Ss.
Definition: vectorbase.h:90
Vector3D< S > operator-() const
Negation operator.
Definition: vectorbase.h:163
Vector3D(S vx, S vy, S vz, S vDummy)
For compatibility with 4d vectors (discards 4th comp)
Definition: vectorbase.h:215
Vector3D(const double *v)
Copy-Constructor.
Definition: vectorbase.h:84
const Vector3D< S > & operator-=(const Vector3D< S > &v)
Assign and sub operator.
Definition: vectorbase.h:121
const Vector3D< S > & operator*=(const Vector3D< S > &v)
Assign and mult operator.
Definition: vectorbase.h:135
S max() const
Get biggest component.
Definition: vectorbase.h:172
bool isValid() const
test if nans are present
Vector3D(const float *v)
Copy-Constructor.
Definition: vectorbase.h:81
const Vector3D< S > & operator/=(const Vector3D< S > &v)
Assign and div operator.
Definition: vectorbase.h:149
S min() const
Get smallest component.
Definition: vectorbase.h:168
Vector3D(S v)
Construct a vector from one S.
Definition: vectorbase.h:87
bool empty()
Test if all components are zero.
Definition: vectorbase.h:177
const Vector3D< S > & operator+=(const Vector3D< S > &v)
Assign and add operator.
Definition: vectorbase.h:107