From d9acb6b27c7de61b85f8417f4c2fc31425fa0150 Mon Sep 17 00:00:00 2001 From: Bastian Rieck Date: Sun, 20 Feb 2011 14:59:53 +0100 Subject: [PATCH] Extended `v3ctor` class * Added distance calculation functions * Added perpendicular foot calculation functions * Added new constructor --- v3ctor.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ v3ctor.h | 7 +++++ 2 files changed, 94 insertions(+) diff --git a/v3ctor.cpp b/v3ctor.cpp index 4a48aac..d62c3cb 100644 --- a/v3ctor.cpp +++ b/v3ctor.cpp @@ -44,6 +44,17 @@ v3ctor::v3ctor() x = y = z = 0.0; } +/*! +* Initializes components with user-defined values. +*/ + +v3ctor::v3ctor(double x, double y, double z) +{ + this->x = x; + this->y = y; + this->z = z; +} + /*! * Adds two vectors. */ @@ -272,3 +283,79 @@ std::ostream& operator<<(std::ostream& o, const v3ctor& v) << v.y << " " << v.z << std::endl); } + +/*! +* Calculates the distance between a plane given by three points and +* another point. +* +* @param a First point in plane +* @param b Second point in plane +* @param c Third point in plane +* @param x Point for which the distance is to be determined +* +* @return Absolute distance value +*/ + +double distance_to_plane(const v3ctor& a, const v3ctor& b, const v3ctor& c, const v3ctor & x) +{ + v3ctor normal = ((b-a)|(c-a)).normalize(); + return(abs(normal*(x-a))); +} + +/*! +* Calculates the perpendicular foot of a point with respect to a plane +* given by three points. +* +* @param a First point in plane +* @param b Second point in plane +* @param c Third point in plane +* @param x Point for which the perpendicular foot is to be determined +* +* @return Position of perpendicular foot +*/ + +v3ctor perpendicular_foot(const v3ctor& a, const v3ctor& b, const v3ctor& c, const v3ctor& x) +{ + v3ctor normal = ((b-a)|(c-a)).normalize(); + double d = -(normal*a); + + return(normal*(-d)-normal*(x*normal)+x); +} + +/*! +* Calculates distance from a point to a line specified by two points. +* +* @param a First point on line +* @param b Second point on line +* @param x Point for which the distance is to be determined +* +* @return Absolute distance from point x to line given by a and b +*/ + +double distance_to_line(const v3ctor& a, const v3ctor& b, const v3ctor& x) +{ + double double_area = ((a-x)|(b-x)).length(); + double side_length = (a-b).length(); + + return(double_area/side_length); +} + +/*! +* Calculates the perpendicular foot of a point with respect to a line +* given by two points. +* +* @param a First point on line +* @param b Second point on line +* @param x Point for which the perpendicular foot is to be determined +* +* @return Position of perpendicular foot +*/ + +v3ctor perpendicular_foot(const v3ctor& a, const v3ctor& b, const v3ctor& x) +{ + double length = (b-a).length(); + double t = (a-x)*(b-a)/(length*length)*(-1.0); + + return(a+(b-a)*t); +} + diff --git a/v3ctor.h b/v3ctor.h index 82c8c52..da53aee 100644 --- a/v3ctor.h +++ b/v3ctor.h @@ -44,6 +44,7 @@ class v3ctor { public: v3ctor(); + v3ctor(double x, double y, double z); v3ctor operator+ (const v3ctor& b) const; v3ctor& operator+=(const v3ctor& b); @@ -71,4 +72,10 @@ class v3ctor double z; }; +double distance_to_plane(const v3ctor& a, const v3ctor& b, const v3ctor& c, const v3ctor & x); +v3ctor perpendicular_foot(const v3ctor& a, const v3ctor& b, const v3ctor& c, const v3ctor& x); + +double distance_to_line(const v3ctor& a, const v3ctor& b, const v3ctor& x); +v3ctor perpendicular_foot(const v3ctor& a, const v3ctor& b, const v3ctor& x); + #endif