From 81787e9004377016236865e95b95707ed6cf1d0b Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 2 Mar 2008 12:23:48 +0000 Subject: initial (buggy) support for .map models --- src/math/Makefile.am | 4 ++-- src/math/mathlib.h | 3 ++- src/math/plane3f.cc | 32 ++++++++++++++++++++++++++++++++ src/math/plane3f.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/math/vector3f.cc | 19 +++++++++---------- src/math/vector3f.h | 2 +- 6 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 src/math/plane3f.cc create mode 100644 src/math/plane3f.h (limited to 'src/math') diff --git a/src/math/Makefile.am b/src/math/Makefile.am index c1f1f88..a622ba7 100644 --- a/src/math/Makefile.am +++ b/src/math/Makefile.am @@ -1,9 +1,9 @@ METASOURCES = AUTO -libmath_la_SOURCES = color.cc functions.cc vector3f.cc +libmath_la_SOURCES = color.cc functions.cc plane3f.cc vector3f.cc libmath_la_LDFLAGS = -avoid-version -no-undefined -lm noinst_LTLIBRARIES = libmath.la -noinst_HEADERS = color.h functions.h mathlib.h vector3f.h +noinst_HEADERS = color.h functions.h mathlib.h plane3f.h vector3f.h INCLUDES = -I$(top_srcdir)/src diff --git a/src/math/mathlib.h b/src/math/mathlib.h index ca5b7f4..b76c562 100644 --- a/src/math/mathlib.h +++ b/src/math/mathlib.h @@ -1,5 +1,5 @@ /* - math/math.h + math/mathlib.h This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ @@ -13,6 +13,7 @@ namespace math {} #include "math/vector3f.h" +#include "math/plane3f.h" #include "math/color.h" #include "math/functions.h" diff --git a/src/math/plane3f.cc b/src/math/plane3f.cc new file mode 100644 index 0000000..232bc98 --- /dev/null +++ b/src/math/plane3f.cc @@ -0,0 +1,32 @@ +/* + math/plane3f.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "math/plane3f.h" +#include + +namespace math +{ + +Plane3f::Plane3f(Vector3f const & point0, Vector3f const &point1, Vector3f const &point2) +{ + plane_point[0] = point0; + plane_point[1] = point1; + plane_point[2] = point2; + + plane_normal = crossproduct((plane_point[1] - plane_point[0]) , (plane_point[2] - plane_point[0]) ); + pd = -1 * (plane_normal.x * plane_point[0].x + plane_normal.y * plane_point[0].y + plane_normal.z * plane_point[0].z); +} + +Plane3f::Plane3f(Plane3f const & other) +{ + for (size_t i=0; i < 3; i++) + this->plane_point[i] = other.plane_point[i]; + + plane_normal = crossproduct((plane_point[1] - plane_point[0]) , (plane_point[2] - plane_point[0]) ); + pd = -1 * (plane_normal.x * plane_point[0].x + plane_normal.y * plane_point[0].y + plane_normal.z * plane_point[0].z); +} + +} diff --git a/src/math/plane3f.h b/src/math/plane3f.h new file mode 100644 index 0000000..ddaef16 --- /dev/null +++ b/src/math/plane3f.h @@ -0,0 +1,50 @@ +/* + math/plane3f.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_MATH_PLANE3F_H__ +#define __INCLUDED_MATH_PLANE3F_H__ + +#include "math/vector3f.h" + +namespace math +{ + +/** @brief A class representing a plane in 3d space + * all points p(x, y, z) on the plane satisfy the general equation + * x*a() + y*b() + z*c() + d() = 0 + */ +class Plane3f +{ +public: + /// a plane defined by 3 points in space + Plane3f(Vector3f const & point0, Vector3f const &point1, Vector3f const &point2); + /// copy constructor + Plane3f(Plane3f const & other); + + /// normal of the plane, not normalized to lenght 1 + inline Vector3f const & normal() const { return plane_normal; } + /// the points defining the plane. + /// @param i 0 <= i < 3 + inline Vector3f const & point(size_t i) const { return plane_point[i]; } + + /// first parameter of the general equation + inline float a() const { return plane_normal[0]; } + /// second parameter of the general equation + inline float b() const { return plane_normal[1]; } + /// third param of the general equation + inline float c() const { return plane_normal[2]; } + /// fourth parameter of the general equation + inline float d() const { return pd; } + +private: + Vector3f plane_point[3]; + Vector3f plane_normal; + float pd; +}; + +} + +#endif // __INCLUDED_MATH_PLANE3F_H__ diff --git a/src/math/vector3f.cc b/src/math/vector3f.cc index ddf46af..371ada1 100644 --- a/src/math/vector3f.cc +++ b/src/math/vector3f.cc @@ -141,15 +141,6 @@ Vector3f operator*(float scalar, const Vector3f& vector) return vector * scalar; } -Vector3f crossproduct(const Vector3f& first, const Vector3f& second) -{ - float x = first[1]*second[2] - first[2]*second[1]; - float y = first[2]*second[0] - first[0]*second[2]; - float z = first[0]*second[1] - first[1]*second[0]; - - return(Vector3f(x,y,z)); -} - std::ostream &operator<<(std::ostream & os, Vector3f const & vector) { os << vector[0] << " " << vector[1] << " " << vector[2]; @@ -163,6 +154,15 @@ std::istream &operator>>(std::istream & is, Vector3f & vector) return is; } +const Vector3f crossproduct(Vector3f const & first, Vector3f const& second) +{ + float x = first[1]*second[2] - first[2]*second[1]; + float y = first[2]*second[0] - first[0]*second[2]; + float z = first[0]*second[1] - first[1]*second[0]; + + return(Vector3f(x,y,z)); +} + float dotproduct(const Vector3f& first, const Vector3f& second) { float r = 0; @@ -170,5 +170,4 @@ float dotproduct(const Vector3f& first, const Vector3f& second) r += first[i] * second[i]; return (r); } - } // namespace math diff --git a/src/math/vector3f.h b/src/math/vector3f.h index d338f50..c2ebc6a 100644 --- a/src/math/vector3f.h +++ b/src/math/vector3f.h @@ -143,7 +143,7 @@ std::istream &operator>>(std::istream & is, Vector3f & vector); Vector3f operator*(float scalar, const Vector3f& vector); /// vector cross product -const Vector3f crossproduct(Vector3f const& first, Vector3f const second); +const Vector3f crossproduct(Vector3f const & first, Vector3f const & second); /// vetor dot product float dotproduct(const Vector3f& first, const Vector3f& second); -- cgit v1.2.3