From 27cea5e70242c85439c07592ebea743fae148ec4 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 27 Feb 2009 18:34:08 +0000 Subject: [PATCH] added Union shape based on Intersect git-svn-id: svn://anubis/fart/trunk@156 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- shapes/Intersect.cc | 1 - shapes/Union.cc | 67 +++++++++++++++++++++++++++++++++++++++++++++ shapes/Union.h | 20 ++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 shapes/Union.cc create mode 100644 shapes/Union.h diff --git a/shapes/Intersect.cc b/shapes/Intersect.cc index 9ad50c8..f142771 100644 --- a/shapes/Intersect.cc +++ b/shapes/Intersect.cc @@ -19,7 +19,6 @@ Shape::IntersectionList Intersect::intersect(refptr _this, const Ray & ra 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( diff --git a/shapes/Union.cc b/shapes/Union.cc new file mode 100644 index 0000000..4f1eca4 --- /dev/null +++ b/shapes/Union.cc @@ -0,0 +1,67 @@ + +#include "Union.h" +#include +using namespace std; + +Union::Union(refptr shape1, refptr shape2) +{ + m_shape1 = shape1; + m_shape2 = shape2; +} + +Shape::IntersectionList Union::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; + + /* TODO: finish */ + 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 (in_bool && in1 && in2) + { + /* we found an intersection point with the boolean object */ + res.add( merged[i].intersection ); + } + if (left) + in1 = false; + else + in2 = false; + in_bool = false; + } + } + + return res; +} + +Vector Union::getNormalAt(const Vector & pt) +{ + /* this should not be called */ + cerr << __FILE__ << ": " << __LINE__ << + ": error: Union::getNormalAt() was called!" << endl; + return Vector(0, 0, 0); +} diff --git a/shapes/Union.h b/shapes/Union.h new file mode 100644 index 0000000..bf9a9fb --- /dev/null +++ b/shapes/Union.h @@ -0,0 +1,20 @@ + +#ifndef UNION_H +#define UNION_H UNION_H + +#include "Shape.h" + +class Union : public Shape +{ + public: + Union(refptr shape1, refptr shape2); + IntersectionList intersect(refptr _this, const Ray & ray); + Vector getNormalAt(const Vector & pt); + + protected: + refptr m_shape1; + refptr m_shape2; +}; + +#endif +