From 1ad08d99984d1801a3b7605ebdec83b171aed7bb Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 7 Mar 2009 19:17:47 +0000 Subject: [PATCH] cleaning up shapes/Cyl a bit git-svn-id: svn://anubis/fart/trunk@190 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- shapes/Cyl.cc | 28 ++++++++++++++++++---------- shapes/Cyl.h | 1 + 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/shapes/Cyl.cc b/shapes/Cyl.cc index daab4b8..79cb68c 100644 --- a/shapes/Cyl.cc +++ b/shapes/Cyl.cc @@ -17,6 +17,8 @@ Cyl::Cyl(double bottom_radius, double top_radius, double height) if (m_height == 0.0) m_height = 1.0; m_slope = (m_top_radius - m_bottom_radius) / m_height; /* rise over run */ + if ( ! FP_EQUAL(m_slope, 0.0) ) + m_inv_slope = -1.0 / m_slope; } Shape::IntersectionList Cyl::intersect(refptr _this, const Ray & ray) @@ -34,7 +36,7 @@ Shape::IntersectionList Cyl::intersect(refptr _this, const Ray & ray) { Vector isect_point = ray_inv[solutions.results[0]]; if (isect_point[0]*isect_point[0] + isect_point[1]*isect_point[1] - < m_bottom_radius_2) + <= m_bottom_radius_2) { res.add(Intersection(_this, m_transform.transform_point(isect_point))); @@ -52,7 +54,7 @@ Shape::IntersectionList Cyl::intersect(refptr _this, const Ray & ray) { Vector isect_point = ray_inv[solutions.results[0]]; if (isect_point[0]*isect_point[0] + isect_point[1]*isect_point[1] - < m_top_radius_2) + <= m_top_radius_2) { res.add(Intersection(_this, m_transform.transform_point(isect_point))); @@ -97,7 +99,7 @@ Shape::IntersectionList Cyl::intersect(refptr _this, const Ray & ray) if (solutions.results[i] >= 0.0) { Vector isect_point = ray_inv[solutions.results[i]]; - if (isect_point[2] >= 0.0 && isect_point[2] <= m_height) + if (isect_point[2] > 0.0 && isect_point[2] < m_height) { res.add(Intersection(_this, m_transform.transform_point(isect_point))); @@ -124,19 +126,25 @@ Vector Cyl::getNormalAt(const Vector & pt) } else { - if (m_slope == 0.0) + if ( FP_EQUAL(m_slope, 0.0) ) { normal = Vector(local_pt[0], local_pt[1], 0.0); - normal.normalize(); } else { - double norm_slope = -1.0 / m_slope; - double z = sqrt(local_pt[0]*local_pt[0] + local_pt[1]*local_pt[1]) - * norm_slope; - normal = Vector(local_pt[0], local_pt[1], z); - normal.normalize(); + double x = local_pt[0]; + double y = local_pt[1]; + double dist = sqrt( local_pt[0] * local_pt[0] + + local_pt[1] * local_pt[1] ); + if (dist > 0.0) + { + double scale = 1.0 / dist; + x *= scale; + y *= scale; + } + normal = Vector(x, y, m_inv_slope); } + normal.normalize(); } return m_transform.transform_normal(normal); diff --git a/shapes/Cyl.h b/shapes/Cyl.h index eeef786..e1eddf0 100644 --- a/shapes/Cyl.h +++ b/shapes/Cyl.h @@ -18,6 +18,7 @@ class Cyl : public Shape double m_top_radius_2; double m_height; double m_slope; + double m_inv_slope; }; #endif