Multimodale-Visualisierung

3D Vektor Header

Hilfsmittel für geometrische Berechnungen:

C++ Klasse, die einen 3D Vektor kapselt und entsprechende Operatoren für inneres Produkt, Skalarprodukt etc. bereitstellt. Implementierung als Klasse v3d in Header File v3d.h:

#ifndef V3D_H
#define V3D_H

#include <math.h>

// 3D double vector
//  definition of components via constructor v3d(x,y,z)
//  access to components x/y/z via . component selector
//  supplies vector operators + - * dot and cross product
//  supplies getters for length and normalization
class v3d
   {
   public:

   // default constructor
   v3d() {}

   // copy constructor
   v3d(const v3d &v) {x=v.x; y=v.y; z=v.z;}

   // component-wise constructor
   v3d(const double vx,const double vy,const double vz) {x=vx; y=vy; z=vz;}

   // destructor
   ~v3d() {}

   // get vector length
   double length() const {return(sqrt(x*x+y*y+z*z));}

   // get squared vector length
   double length2() const {return(x*x+y*y+z*z);}

   // normalization to unit length
   v3d normalize() const;

   // vector components
   double x,y,z;
   };

// addition of two vectors
inline v3d operator + (const v3d &a,const v3d &b)
   {return(v3d(a.x+b.x,a.y+b.y,a.z+b.z));}

// subtraction of two vectors
inline v3d operator - (const v3d &a,const v3d &b)
   {return(v3d(a.x-b.x,a.y-b.y,a.z-b.z));}

// negation of a vector
inline v3d operator - (const v3d &v)
   {return(v3d(-v.x,-v.y,-v.z));}

// left-hand side scalar multiplication
inline v3d operator * (const double a,const v3d &b)
   {return(v3d(a*b.x,a*b.y,a*b.z));}

// right-hand side scalar multiplication
inline v3d operator * (const v3d &a,const double b)
   {return(v3d(a.x*b,a.y*b,a.z*b));}

// right-hand side scalar division
inline v3d operator / (const v3d &a,const double b)
   {return(v3d(a.x/b,a.y/b,a.z/b));}

// dot product
inline double operator * (const v3d &a,const v3d &b)
   {return(a.x*b.x+a.y*b.y+a.z*b.z);}

// cross product (0,0,-1)/(-1,0,0)=(0,1,0)
inline v3d operator / (const v3d &a,const v3d &b)
   {return(v3d(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x));}

// comparison
inline int operator == (const v3d &a,const v3d &b)
   {return(a.x==b.x && a.y==b.y && a.z==b.z);}

// negated comparison
inline int operator != (const v3d &a,const v3d &b)
   {return(a.x!=b.x || a.y!=b.y || a.z!=b.z);}

// normalization to unit length
inline v3d v3d::normalize()
   {
   double l2=length2();
   if (l2>0.0 && l2!=1.0) return(*this/sqrt(l2));
   return(*this);
   }

#endif

Damit schreibt sich die Addition zweier Punkte c=a+b in C++:

v3d a(1,0,0),b(0,1,0);
v3d c=a+b;

Und damit ist zum Beispiel der geometrische Abstand eines Punktes p von einer Ebene mit dem Aufsatzpunkt o und der Normale n:

v3d o(1,0,0),n(1,1,1);
v3d p(0,0,1);

n=n.normalize();
double d=(p-o)*n;


Options: