From bcbec65a8f4b7aa84ba242da7515f9dca6356009 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 24 Feb 2009 03:04:26 +0000 Subject: [PATCH] converted shapes/Intersect::intersect() to using BoolIntersectionList git-svn-id: svn://anubis/fart/trunk@152 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- main/Scene.cc | 4 +-- shapes/Intersect.cc | 2 +- shapes/Shape.cc | 39 ++++++++++++++--------------- shapes/Shape.h | 60 ++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 78 insertions(+), 27 deletions(-) diff --git a/main/Scene.cc b/main/Scene.cc index 9270054..e296fd6 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -177,8 +177,8 @@ vector Scene::getRayHits(const Ray & ray) i < num_results; i++) { - refptr shape = intersections[i].first; - const Vector & isect_point = intersections[i].second; + refptr shape = intersections[i].shape; + const Vector & isect_point = intersections[i].vector; Vector normal = shape->getNormalAt(isect_point); double dot = normal % ray.getDirection(); double intersect_dist = (isect_point - ray.getOrigin()).mag(); diff --git a/shapes/Intersect.cc b/shapes/Intersect.cc index cce50ab..3d58ba7 100644 --- a/shapes/Intersect.cc +++ b/shapes/Intersect.cc @@ -11,7 +11,7 @@ Shape::IntersectionList Intersect::intersect(refptr _this, const Ray & ra { IntersectionList res1 = m_shape1->intersect(m_shape1, ray); IntersectionList res2 = m_shape2->intersect(m_shape2, ray); - IntersectionList merged = res1.merge(res2, ray.getOrigin()); + BoolIntersectionList merged(res1, res2, ray.getOrigin()); IntersectionList res; bool in1 = false, in2 = false; diff --git a/shapes/Shape.cc b/shapes/Shape.cc index 4ed75c8..5211c00 100755 --- a/shapes/Shape.cc +++ b/shapes/Shape.cc @@ -15,45 +15,44 @@ Shape::~Shape() { } -class IntersectionComparator : public std::binary_function +class BoolIntersectionComparator + : public std::binary_function { public: - IntersectionComparator(const Vector & refPoint) + BoolIntersectionComparator(const Vector & refPoint) { m_refPoint = refPoint; } - bool operator()(const Shape::Intersection & i1, - const Shape::Intersection & i2) const + bool operator()(const Shape::BoolIntersection & i1, + const Shape::BoolIntersection & i2) const { - return (m_refPoint.dist_to(i1.second) - < m_refPoint.dist_to(i2.second)); + return (m_refPoint.dist_to(i1.intersection.vector) + < m_refPoint.dist_to(i2.intersection.vector)); } protected: Vector m_refPoint; }; -Shape::IntersectionList -Shape::IntersectionList::merge(const IntersectionList & other, - const Vector & startPoint) +Shape::BoolIntersectionList::BoolIntersectionList(const IntersectionList & l1, + const IntersectionList & l2, + const Vector & startPoint) { - Shape::IntersectionList result; - - for (size_t i = 0, sz = m_intersections.size(); + for (size_t i = 0, sz = l1.size(); i < sz; i++) { - result.add(m_intersections[i]); + m_intersections.push_back( BoolIntersection(l1[i], true) ); } - for (size_t i = 0, sz = other.m_intersections.size(); + for (size_t i = 0, sz = l2.size(); i < sz; i++) { - result.add(other.m_intersections[i]); + m_intersections.push_back( BoolIntersection(l2[i], false) ); } - sort(result.begin(), result.end(), IntersectionComparator(startPoint)); - - return result; + sort(m_intersections.begin(), + m_intersections.end(), + BoolIntersectionComparator(startPoint)); } diff --git a/shapes/Shape.h b/shapes/Shape.h index 7116d7f..8f48ed6 100644 --- a/shapes/Shape.h +++ b/shapes/Shape.h @@ -14,7 +14,30 @@ class Shape { public: - typedef std::pair< refptr , Vector > Intersection; + class Intersection + { + public: + Intersection() {}; + Intersection(refptr shape, const Vector & vector) + { + this->shape = shape; + this->vector = vector; + } + refptr shape; + Vector vector; + }; + class BoolIntersection + { + public: + BoolIntersection() {}; + BoolIntersection(const Intersection & intersection, bool left) + { + this->intersection = intersection; + this->left = left; + } + Intersection intersection; + bool left; + }; class IntersectionList { public: @@ -26,7 +49,11 @@ class Shape { return m_intersections[i]; } - size_t size() { return m_intersections.size(); } + const Intersection & operator[](int i) const + { + return m_intersections[i]; + } + size_t size() const { return m_intersections.size(); } std::vector::iterator begin() { return m_intersections.begin(); @@ -35,11 +62,36 @@ class Shape { return m_intersections.end(); } - IntersectionList merge(const IntersectionList & other, - const Vector & startPoint); protected: std::vector< Intersection > m_intersections; }; + class BoolIntersectionList + { + public: + BoolIntersectionList(const IntersectionList & l1, + const IntersectionList & l2, + const Vector & startPoint); + BoolIntersection & operator[](int i) + { + return m_intersections[i]; + } + const BoolIntersection & operator[](int i) const + { + return m_intersections[i]; + } + size_t size() const { return m_intersections.size(); } + std::vector::iterator begin() + { + return m_intersections.begin(); + } + std::vector::iterator end() + { + return m_intersections.end(); + } + + protected: + std::vector< BoolIntersection > m_intersections; + }; Shape(); virtual ~Shape();