From: Mikko Rasa Date: Fri, 24 May 2013 06:06:29 +0000 (+0300) Subject: Put ray and bounding box transformations in AffineTransformation X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=4bb4d03d5a6a0252083d6bc45b8a50fe7b504c51;p=libs%2Fmath.git Put ray and bounding box transformations in AffineTransformation This makes them easier to use elsewhere, in cases where creating a TransformedShape would be overkill. --- diff --git a/source/geometry/affinetransformation.h b/source/geometry/affinetransformation.h index 0e377ba..15bd926 100644 --- a/source/geometry/affinetransformation.h +++ b/source/geometry/affinetransformation.h @@ -3,6 +3,8 @@ #include #include "angle.h" +#include "boundingbox.h" +#include "ray.h" namespace Msp { namespace Geometry { @@ -70,6 +72,8 @@ public: LinAl::Vector transform(const LinAl::Vector &) const; LinAl::Vector transform_linear(const LinAl::Vector &) const; + Ray transform(const Ray &) const; + BoundingBox transform(const BoundingBox &) const; }; template @@ -180,6 +184,44 @@ inline LinAl::Vector AffineTransformation::transform_linear(const Li return LinAl::Vector(matrix*LinAl::Vector(v, T(0))); } +template +inline Ray AffineTransformation::transform(const Ray &ray) const +{ + LinAl::Vector dir = transform_linear(ray.get_direction()); + return Ray(transform(ray.get_start()), dir, ray.get_limit()*dir.norm()); +} + +template +inline BoundingBox AffineTransformation::transform(const BoundingBox &bbox) const +{ + LinAl::Vector min_pt; + LinAl::Vector max_pt; + for(unsigned i=0; i<(1< point; + for(unsigned j=0; j>j)&1 ? bbox.get_maximum_coordinate(j) : bbox.get_minimum_coordinate(j)); + + point = transform(point); + + if(i==0) + { + min_pt = point; + max_pt = point; + } + else + { + for(unsigned j=0; j(min_pt, max_pt); +} + } // namespace Geometry } // namespace Msp diff --git a/source/geometry/transformedshape.h b/source/geometry/transformedshape.h index f214e4b..6a14b0b 100644 --- a/source/geometry/transformedshape.h +++ b/source/geometry/transformedshape.h @@ -31,9 +31,6 @@ public: virtual BoundingBox get_axis_aligned_bounding_box() const; virtual bool contains(const LinAl::Vector &) const; -private: - Ray make_local_ray(const Ray &) const; -public: virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); } virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; }; @@ -76,34 +73,7 @@ inline TransformedShape *TransformedShape::clone() const template inline BoundingBox TransformedShape::get_axis_aligned_bounding_box() const { - BoundingBox inner_bbox = shape->get_axis_aligned_bounding_box(); - - LinAl::Vector min_pt; - LinAl::Vector max_pt; - for(unsigned i=0; i<(1< point; - for(unsigned j=0; j>j)&1 ? inner_bbox.get_maximum_coordinate(j) : inner_bbox.get_minimum_coordinate(j)); - - point = transformation.transform(point); - - if(i==0) - { - min_pt = point; - max_pt = point; - } - else - { - for(unsigned j=0; j(min_pt, max_pt); + return transformation.transform(shape->get_axis_aligned_bounding_box()); } template @@ -112,18 +82,10 @@ inline bool TransformedShape::contains(const LinAl::Vector &point) c return shape->contains(inverse_trans.transform(point)); } -template -inline Ray TransformedShape::make_local_ray(const Ray &ray) const -{ - LinAl::Vector local_dir = inverse_trans.transform_linear(ray.get_direction()); - float distortion = local_dir.norm(); - return Ray(inverse_trans.transform(ray.get_start()), local_dir, ray.get_limit()*distortion); -} - template inline unsigned TransformedShape::get_intersections(const Ray &ray, SurfacePoint *points, unsigned size) const { - Ray local_ray = make_local_ray(ray); + Ray local_ray = inverse_trans.transform(ray); unsigned count = shape->get_intersections(local_ray, points, size); if(points)