From ef25fe4a5b02715015bd02b2bd955d88e1ec3978 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 27 Feb 2009 22:43:49 +0000 Subject: [PATCH] added Subtract, including Union and Subtract in Shape.h git-svn-id: svn://anubis/fart/trunk@158 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- shapes/Shape.h | 2 ++ shapes/Subtract.cc | 66 ++++++++++++++++++++++++++++++++++++++++++++++ shapes/Subtract.h | 20 ++++++++++++++ shapes/Union.cc | 1 - 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 shapes/Subtract.cc create mode 100644 shapes/Subtract.h diff --git a/shapes/Shape.h b/shapes/Shape.h index c7c09cb..babb0a3 100644 --- a/shapes/Shape.h +++ b/shapes/Shape.h @@ -124,6 +124,8 @@ class Shape #include "Intersect.h" #include "Plane.h" #include "Sphere.h" +#include "Subtract.h" +#include "Union.h" #endif diff --git a/shapes/Subtract.cc b/shapes/Subtract.cc new file mode 100644 index 0000000..9d9d0e5 --- /dev/null +++ b/shapes/Subtract.cc @@ -0,0 +1,66 @@ + +#include "Subtract.h" +#include +using namespace std; + +Subtract::Subtract(refptr shape1, refptr shape2) +{ + m_shape1 = shape1; + m_shape2 = shape2; +} + +Shape::IntersectionList Subtract::intersect(refptr _this, const Ray & ray) +{ + IntersectionList res1 = m_shape1->intersect(m_shape1, ray); + IntersectionList res2 = m_shape2->intersect(m_shape2, ray); + BoolIntersectionList merged(res1, res2, ray.getOrigin()); + + IntersectionList res; + bool in1 = false, in2 = false; + bool in_bool = false; + + for (int i = 0, sz = merged.size(); i < sz; i++) + { + Vector normal = merged[i].intersection.shape->getNormalAt( + merged[i].intersection.vector); + double dot = - (ray.getDirection() % normal); + bool front = dot > 0.0; + bool left = merged[i].left; + if (front) + { + if (left) + in1 = true; + else + in2 = true; + if (!in_bool && in1 && !in2) + { + /* we found an intersection point with the boolean object */ + in_bool = true; + res.add( merged[i].intersection ); + } + } + else + { + if (left) + in1 = false; + else + in2 = false; + if (in_bool && !(in1 && !in2)) + { + /* we found an intersection point with the boolean object */ + res.add( merged[i].intersection ); + in_bool = false; + } + } + } + + return res; +} + +Vector Subtract::getNormalAt(const Vector & pt) +{ + /* this should not be called */ + cerr << __FILE__ << ": " << __LINE__ << + ": error: Subtract::getNormalAt() was called!" << endl; + return Vector(0, 0, 0); +} diff --git a/shapes/Subtract.h b/shapes/Subtract.h new file mode 100644 index 0000000..fb44642 --- /dev/null +++ b/shapes/Subtract.h @@ -0,0 +1,20 @@ + +#ifndef SUBTRACT_H +#define SUBTRACT_H SUBTRACT_H + +#include "Shape.h" + +class Subtract : public Shape +{ + public: + Subtract(refptr shape1, refptr shape2); + IntersectionList intersect(refptr _this, const Ray & ray); + Vector getNormalAt(const Vector & pt); + + protected: + refptr m_shape1; + refptr m_shape2; +}; + +#endif + diff --git a/shapes/Union.cc b/shapes/Union.cc index a3257f9..902872f 100644 --- a/shapes/Union.cc +++ b/shapes/Union.cc @@ -19,7 +19,6 @@ Shape::IntersectionList Union::intersect(refptr _this, const Ray & ray) bool in1 = false, in2 = false; bool in_bool = false; - /* TODO: finish */ for (int i = 0, sz = merged.size(); i < sz; i++) { Vector normal = merged[i].intersection.shape->getNormalAt(