We have to calculate the vector on the special direction firstly.

``````void CalculateVecComponent(PointStruct originVec, PointStruct &axis0, PointStruct &vecOnAxis)
{
double cosTheta = originVec.Dot( axis0 );
return axis0*cosTheta;
}
``````

## Project point on line

``````Point GetProjectPtOnDir(Point pt, Point dir, Point startPt)
{
dir.Unit();
Point ptVector = pt - startPt;
double cosTheta = ptVector.Dot( dir );
Point resultPt = startPt + dir * cosTheta;
return resultPt;
}
``````

## Project point on plane

``````Point ProjectPtOnPlane(Point point, Point projectDir, Point planeOrigin, Point planeNormal)
{
double point1[3];
double len = 100;
point1[0] = point[0] - projectDir[0] * len;
point1[1] = point[1] - projectDir[1] * len;
point1[2] = point[2] - projectDir[2] * len;
double point2[3];
point2[0] = point[0] + projectDir[0] * len;
point2[1] = point[1] + projectDir[1] * len;
point2[2] = point[2] + projectDir[2] * len;

double interSection[3];
double t;

vtkPlane::IntersectWithLine(point1, point2, planeNormal.point, planeOrigin.point, t, interSection);
Point resultPt;
resultPt.SetPoint(interSection);

return resultPt;
}
``````

Here is struct of point we used in the above functions.

point.hpp

``````#ifndef POINT_HPP
#define POINT_HPP

#include <iostream>
#include <math.h>
using namespace std;

class Point
{
public:
double point[3];
Point() {}
Point(double x, double y, double z) { point[0]=x; point[1]=y; point[2]=z;}
Point & SetPoint(double p[]) { point[0]=p[0]; point[1]=p[1];  point[2]=p[2];  return *this; }
double operator[](int i) const { return point[i]; }

Point operator+(double v) { return Point(point[0]+v, point[1]+v, point[2]+v); }
Point operator-(double v) { return Point(point[0]-v, point[1]-v, point[2]-v); }
Point operator*(double v) { return Point(point[0]*v, point[1]*v, point[2]*v); }
Point operator/(double v) { return Point(point[0]/v, point[1]/v, point[2]/v); }
Point operator-() { return Point(-point[0], -point[1], -point[2]); }
Point operator/=(double v) { point[0]/=v; point[1]/=v; point[2]/=v;   return *this; }
double Dot(const Point &p) { return point[0]*p.point[0]+point[1]*p.point[1]+point[2]*p.point[2]; }
double Length() { return sqrt(point[0]*point[0]+point[1]*point[1]+point[2]*point[2]); }
Point Unit() { return (operator/=(Length())); }

friend Point operator*(double v,Point &p) { return p*v; };
friend Point operator/( Point &p, double v) { return p*(1./v); }
friend Point operator+(const Point &p1, const Point &p2) { return Point(p1[0]+p2[0],p1[1]+p2[1],p1[2]+p2[2]); }
friend Point operator-(const Point &p1, const Point &p2) { return Point(p1[0]-p2[0],p1[1]-p2[1],p1[2]-p2[2]); }
friend Point operator^(const Point &p1, const Point &p2) { return Point(p1[1]*p2[2]-p1[2]*p2[1],p1[2]*p2[0]-p1[0]*p2[2],p1[0]*p2[1]-p1[1]*p2[0]); }
friend bool operator==( Point &p1,  Point &p2)
{
bool notEqual = (p1 != p2);
return !notEqual;
}
friend bool operator!=( Point &p1,  Point &p2)
{
if( fabs( p1[0] - p2[0] ) > 1e-6 ||
fabs( p1[1] - p2[1] ) > 1e-6 ||
fabs( p1[2] - p2[2] ) > 1e-6 )
{
return true;
}
return false;
}

bool operator<( const Point &p2 ) const
{
return ( point[0] <=  p2[0] && point[1] <= p2[1] && point[2] <= p2[2] );
}
};

#endif //POINT_HPP
``````
Categories: VTK

Article Rating
Subscribe
Notify of

Inline Feedbacks