This seems like a more appropriate term for the operation.
--- /dev/null
+#ifndef MSP_GEOMETRY_AFFINETRANSFORM_H_
+#define MSP_GEOMETRY_AFFINETRANSFORM_H_
+
+#include <msp/linal/matrix.h>
+#include "angle.h"
+#include "boundingbox.h"
+#include "ray.h"
+
+namespace Msp {
+namespace Geometry {
+
+template<typename T, unsigned D>
+class AffineTransform;
+
+
+/**
+Helper class to provide specialized operations for AffineTransform.
+*/
+template<typename T, unsigned D>
+class AffineTransformOps
+{
+protected:
+ AffineTransformOps() { }
+};
+
+template<typename T>
+class AffineTransformOps<T, 2>
+{
+protected:
+ AffineTransformOps() { }
+
+public:
+ static AffineTransform<T, 2> rotation(const Angle<T> &);
+};
+
+template<typename T>
+class AffineTransformOps<T, 3>
+{
+protected:
+ AffineTransformOps() { }
+
+public:
+ static AffineTransform<T, 3> rotation(const Angle<T> &, const LinAl::Vector<T, 3> &);
+};
+
+
+/**
+An affine transformation in D dimensions. Affine transformations preserve
+straightness of lines and ratios of distances. Angles and distances themselves
+may change. Internally this is represented by a square matrix of size D+1.
+*/
+template<typename T, unsigned D>
+class AffineTransform: public AffineTransformOps<T, D>
+{
+ friend class AffineTransformOps<T, D>;
+
+private:
+ LinAl::Matrix<T, D+1, D+1> matrix;
+
+public:
+ AffineTransform();
+
+ static AffineTransform<T, D> translation(const LinAl::Vector<T, D> &);
+ static AffineTransform<T, D> scaling(const LinAl::Vector<T, D> &);
+ static AffineTransform<T, D> shear(const LinAl::Vector<T, D> &, const LinAl::Vector<T, D> &);
+
+ AffineTransform &operator*=(const AffineTransform &);
+ AffineTransform &invert();
+
+ const LinAl::Matrix<T, D+1, D+1> &get_matrix() const { return matrix; }
+ operator const LinAl::Matrix<T, D+1, D+1> &() const { return matrix; }
+
+ LinAl::Vector<T, D> transform(const LinAl::Vector<T, D> &) const;
+ LinAl::Vector<T, D> transform_linear(const LinAl::Vector<T, D> &) const;
+ Ray<T, D> transform(const Ray<T, D> &) const;
+ BoundingBox<T, D> transform(const BoundingBox<T, D> &) const;
+};
+
+template<typename T, unsigned D>
+inline AffineTransform<T, D>::AffineTransform()
+{
+ this->matrix = LinAl::Matrix<T, D+1, D+1>::identity();
+}
+
+
+template<typename T, unsigned D>
+AffineTransform<T, D> AffineTransform<T, D>::translation(const LinAl::Vector<T, D> &v)
+{
+ AffineTransform<T, D> r;
+ for(unsigned i=0; i<D; ++i)
+ r.matrix(i, D) = v[i];
+ return r;
+}
+
+template<typename T, unsigned D>
+AffineTransform<T, D> AffineTransform<T, D>::scaling(const LinAl::Vector<T, D> &factors)
+{
+ AffineTransform<T, D> r;
+ for(unsigned i=0; i<D; ++i)
+ r.matrix(i, i) = factors[i];
+ return r;
+}
+
+template<typename T, unsigned D>
+AffineTransform<T, D> AffineTransform<T, D>::shear(const LinAl::Vector<T, D> &normal, const LinAl::Vector<T, D> &shift)
+{
+ AffineTransform<T, D> r;
+ for(unsigned i=0; i<D; ++i)
+ for(unsigned j=0; j<D; ++j)
+ r.matrix(i, j) += normal[j]*shift[i];
+ return r;
+}
+
+template<typename T>
+AffineTransform<T, 2> AffineTransformOps<T, 2>::rotation(const Angle<T> &angle)
+{
+ AffineTransform<T, 2> r;
+ T c = cos(angle);
+ T s = sin(angle);
+ r.matrix(0, 0) = c;
+ r.matrix(0, 1) = -s;
+ r.matrix(1, 0) = s;
+ r.matrix(1, 1) = c;
+ return r;
+}
+
+template<typename T>
+AffineTransform<T, 3> AffineTransformOps<T, 3>::rotation(const Angle<T> &angle, const LinAl::Vector<T, 3> &axis)
+{
+ AffineTransform<T, 3> r;
+ LinAl::Vector<T, 3> axn = normalize(axis);
+ T c = cos(angle);
+ T s = sin(angle);
+ // http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
+ r.matrix(0, 0) = c+axn.x*axn.x*(1-c);
+ r.matrix(0, 1) = axn.x*axn.y*(1-c)-axn.z*s;
+ r.matrix(0, 2) = axn.x*axn.z*(1-c)+axn.y*s;
+ r.matrix(1, 0) = axn.y*axn.x*(1-c)+axn.z*s;
+ r.matrix(1, 1) = c+axn.y*axn.y*(1-c);
+ r.matrix(1, 2) = axn.y*axn.z*(1-c)-axn.x*s;
+ r.matrix(2, 0) = axn.z*axn.x*(1-c)-axn.y*s;
+ r.matrix(2, 1) = axn.z*axn.y*(1-c)+axn.x*s;
+ r.matrix(2, 2) = c+axn.z*axn.z*(1-c);
+ return r;
+}
+
+template<typename T, unsigned D>
+inline AffineTransform<T, D> &AffineTransform<T, D>::operator*=(const AffineTransform<T, D> &other)
+{
+ matrix *= other.get_matrix();
+ return *this;
+}
+
+template<typename T, unsigned D>
+inline AffineTransform<T, D> operator*(const AffineTransform<T, D> &at1, const AffineTransform<T, D> &at2)
+{
+ AffineTransform<T, D> r = at1;
+ return r *= at2;
+}
+
+template<typename T, unsigned D>
+inline AffineTransform<T, D> &AffineTransform<T, D>::invert()
+{
+ matrix.invert();
+ return *this;
+}
+
+template<typename T, unsigned D>
+inline AffineTransform<T, D> invert(const AffineTransform<T, D> &at)
+{
+ AffineTransform<T, D> r = at;
+ return r.invert();
+}
+
+template<typename T, unsigned D>
+inline LinAl::Vector<T, D> AffineTransform<T, D>::transform(const LinAl::Vector<T, D> &v) const
+{
+ return (matrix*compose(v, T(1))).template slice<D>(0);
+}
+
+template<typename T, unsigned D>
+inline LinAl::Vector<T, D> AffineTransform<T, D>::transform_linear(const LinAl::Vector<T, D> &v) const
+{
+ return (matrix*compose(v, T(0))).template slice<D>(0);
+}
+
+template<typename T, unsigned D>
+inline Ray<T, D> AffineTransform<T, D>::transform(const Ray<T, D> &ray) const
+{
+ LinAl::Vector<T, D> dir = transform_linear(ray.get_direction());
+ return Ray<T, D>(transform(ray.get_start()), dir, ray.get_limit()*dir.norm());
+}
+
+template<typename T, unsigned D>
+inline BoundingBox<T, D> AffineTransform<T, D>::transform(const BoundingBox<T, D> &bbox) const
+{
+ LinAl::Vector<T, D> min_pt;
+ LinAl::Vector<T, D> max_pt;
+ for(unsigned i=0; i<(1<<D); ++i)
+ {
+ LinAl::Vector<T, D> point;
+ for(unsigned j=0; j<D; ++j)
+ point[j] = ((i>>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<D; ++j)
+ {
+ min_pt[j] = std::min(min_pt[j], point[j]);
+ max_pt[j] = std::max(max_pt[j], point[j]);
+ }
+ }
+ }
+
+ return BoundingBox<T, D>(min_pt, max_pt);
+}
+
+} // namespace Geometry
+} // namespace Msp
+
+#endif
+++ /dev/null
-#ifndef MSP_GEOMETRY_AFFINETRANSFORMATION_H_
-#define MSP_GEOMETRY_AFFINETRANSFORMATION_H_
-
-#include <msp/linal/matrix.h>
-#include "angle.h"
-#include "boundingbox.h"
-#include "ray.h"
-
-namespace Msp {
-namespace Geometry {
-
-template<typename T, unsigned D>
-class AffineTransformation;
-
-
-/**
-Helper class to provide specialized operations for AffineTransformation.
-*/
-template<typename T, unsigned D>
-class AffineTransformationOps
-{
-protected:
- AffineTransformationOps() { }
-};
-
-template<typename T>
-class AffineTransformationOps<T, 2>
-{
-protected:
- AffineTransformationOps() { }
-
-public:
- static AffineTransformation<T, 2> rotation(const Angle<T> &);
-};
-
-template<typename T>
-class AffineTransformationOps<T, 3>
-{
-protected:
- AffineTransformationOps() { }
-
-public:
- static AffineTransformation<T, 3> rotation(const Angle<T> &, const LinAl::Vector<T, 3> &);
-};
-
-
-/**
-An affine transformation in D dimensions. Affine transformations preserve
-straightness of lines and ratios of distances. Angles and distances themselves
-may change. Internally this is represented by a square matrix of size D+1.
-*/
-template<typename T, unsigned D>
-class AffineTransformation: public AffineTransformationOps<T, D>
-{
- friend class AffineTransformationOps<T, D>;
-
-private:
- LinAl::Matrix<T, D+1, D+1> matrix;
-
-public:
- AffineTransformation();
-
- static AffineTransformation<T, D> translation(const LinAl::Vector<T, D> &);
- static AffineTransformation<T, D> scaling(const LinAl::Vector<T, D> &);
- static AffineTransformation<T, D> shear(const LinAl::Vector<T, D> &, const LinAl::Vector<T, D> &);
-
- AffineTransformation &operator*=(const AffineTransformation &);
- AffineTransformation &invert();
-
- const LinAl::Matrix<T, D+1, D+1> &get_matrix() const { return matrix; }
- operator const LinAl::Matrix<T, D+1, D+1> &() const { return matrix; }
-
- LinAl::Vector<T, D> transform(const LinAl::Vector<T, D> &) const;
- LinAl::Vector<T, D> transform_linear(const LinAl::Vector<T, D> &) const;
- Ray<T, D> transform(const Ray<T, D> &) const;
- BoundingBox<T, D> transform(const BoundingBox<T, D> &) const;
-};
-
-template<typename T, unsigned D>
-inline AffineTransformation<T, D>::AffineTransformation()
-{
- this->matrix = LinAl::Matrix<T, D+1, D+1>::identity();
-}
-
-
-template<typename T, unsigned D>
-AffineTransformation<T, D> AffineTransformation<T, D>::translation(const LinAl::Vector<T, D> &v)
-{
- AffineTransformation<T, D> r;
- for(unsigned i=0; i<D; ++i)
- r.matrix(i, D) = v[i];
- return r;
-}
-
-template<typename T, unsigned D>
-AffineTransformation<T, D> AffineTransformation<T, D>::scaling(const LinAl::Vector<T, D> &factors)
-{
- AffineTransformation<T, D> r;
- for(unsigned i=0; i<D; ++i)
- r.matrix(i, i) = factors[i];
- return r;
-}
-
-template<typename T, unsigned D>
-AffineTransformation<T, D> AffineTransformation<T, D>::shear(const LinAl::Vector<T, D> &normal, const LinAl::Vector<T, D> &shift)
-{
- AffineTransformation<T, D> r;
- for(unsigned i=0; i<D; ++i)
- for(unsigned j=0; j<D; ++j)
- r.matrix(i, j) += normal[j]*shift[i];
- return r;
-}
-
-template<typename T>
-AffineTransformation<T, 2> AffineTransformationOps<T, 2>::rotation(const Angle<T> &angle)
-{
- AffineTransformation<T, 2> r;
- T c = cos(angle);
- T s = sin(angle);
- r.matrix(0, 0) = c;
- r.matrix(0, 1) = -s;
- r.matrix(1, 0) = s;
- r.matrix(1, 1) = c;
- return r;
-}
-
-template<typename T>
-AffineTransformation<T, 3> AffineTransformationOps<T, 3>::rotation(const Angle<T> &angle, const LinAl::Vector<T, 3> &axis)
-{
- AffineTransformation<T, 3> r;
- LinAl::Vector<T, 3> axn = normalize(axis);
- T c = cos(angle);
- T s = sin(angle);
- // http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
- r.matrix(0, 0) = c+axn.x*axn.x*(1-c);
- r.matrix(0, 1) = axn.x*axn.y*(1-c)-axn.z*s;
- r.matrix(0, 2) = axn.x*axn.z*(1-c)+axn.y*s;
- r.matrix(1, 0) = axn.y*axn.x*(1-c)+axn.z*s;
- r.matrix(1, 1) = c+axn.y*axn.y*(1-c);
- r.matrix(1, 2) = axn.y*axn.z*(1-c)-axn.x*s;
- r.matrix(2, 0) = axn.z*axn.x*(1-c)-axn.y*s;
- r.matrix(2, 1) = axn.z*axn.y*(1-c)+axn.x*s;
- r.matrix(2, 2) = c+axn.z*axn.z*(1-c);
- return r;
-}
-
-template<typename T, unsigned D>
-inline AffineTransformation<T, D> &AffineTransformation<T, D>::operator*=(const AffineTransformation<T, D> &other)
-{
- matrix *= other.get_matrix();
- return *this;
-}
-
-template<typename T, unsigned D>
-inline AffineTransformation<T, D> operator*(const AffineTransformation<T, D> &at1, const AffineTransformation<T, D> &at2)
-{
- AffineTransformation<T, D> r = at1;
- return r *= at2;
-}
-
-template<typename T, unsigned D>
-inline AffineTransformation<T, D> &AffineTransformation<T, D>::invert()
-{
- matrix.invert();
- return *this;
-}
-
-template<typename T, unsigned D>
-inline AffineTransformation<T, D> invert(const AffineTransformation<T, D> &at)
-{
- AffineTransformation<T, D> r = at;
- return r.invert();
-}
-
-template<typename T, unsigned D>
-inline LinAl::Vector<T, D> AffineTransformation<T, D>::transform(const LinAl::Vector<T, D> &v) const
-{
- return (matrix*compose(v, T(1))).template slice<D>(0);
-}
-
-template<typename T, unsigned D>
-inline LinAl::Vector<T, D> AffineTransformation<T, D>::transform_linear(const LinAl::Vector<T, D> &v) const
-{
- return (matrix*compose(v, T(0))).template slice<D>(0);
-}
-
-template<typename T, unsigned D>
-inline Ray<T, D> AffineTransformation<T, D>::transform(const Ray<T, D> &ray) const
-{
- LinAl::Vector<T, D> dir = transform_linear(ray.get_direction());
- return Ray<T, D>(transform(ray.get_start()), dir, ray.get_limit()*dir.norm());
-}
-
-template<typename T, unsigned D>
-inline BoundingBox<T, D> AffineTransformation<T, D>::transform(const BoundingBox<T, D> &bbox) const
-{
- LinAl::Vector<T, D> min_pt;
- LinAl::Vector<T, D> max_pt;
- for(unsigned i=0; i<(1<<D); ++i)
- {
- LinAl::Vector<T, D> point;
- for(unsigned j=0; j<D; ++j)
- point[j] = ((i>>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<D; ++j)
- {
- min_pt[j] = std::min(min_pt[j], point[j]);
- max_pt[j] = std::max(max_pt[j], point[j]);
- }
- }
- }
-
- return BoundingBox<T, D>(min_pt, max_pt);
-}
-
-} // namespace Geometry
-} // namespace Msp
-
-#endif
-#include "affinetransformation.h"
+#include "affinetransform.h"
#include "angle.h"
#include "boundingbox.h"
#include "boundingsphere.h"
class TransformationLoader: public Loader<T, D>
{
protected:
- AffineTransformation<T, D> transformation;
+ AffineTransform<T, D> transformation;
public:
TransformationLoader();
if(offset.size()!=D)
throw std::invalid_argument("TransformationLoader::translate");
- transformation *= AffineTransformation<T, D>::translation(LinAl::Vector<T, D>(&offset[0]));
+ transformation *= AffineTransform<T, D>::translation(LinAl::Vector<T, D>(&offset[0]));
}
template<typename T, unsigned D>
LinAl::Vector<T, D> us;
for(unsigned i=0; i<D; ++i)
us[i] = s.front();
- transformation *= AffineTransformation<T, D>::scaling(us);
+ transformation *= AffineTransform<T, D>::scaling(us);
}
else
- transformation *= AffineTransformation<T, D>::scaling(LinAl::Vector<T, D>(&s[0]));
+ transformation *= AffineTransform<T, D>::scaling(LinAl::Vector<T, D>(&s[0]));
}
template<typename T, unsigned D>
if(s.size()!=2*D)
throw std::invalid_argument("TransformationLoader::shear");
- transformation *= AffineTransformation<T, D>::shear(LinAl::Vector<T, D>(&s[0]), LinAl::Vector<T, D>(&s[D]));
+ transformation *= AffineTransform<T, D>::shear(LinAl::Vector<T, D>(&s[0]), LinAl::Vector<T, D>(&s[D]));
}
template<typename T>
inline void ShapeLoader<TransformedShape<T, 2> >::rotate(T a)
{
- TransformationLoader<T, 2>::transformation *= AffineTransformation<T, 2>::rotation(Angle<T>::from_degrees(a));
+ TransformationLoader<T, 2>::transformation *= AffineTransform<T, 2>::rotation(Angle<T>::from_degrees(a));
}
template<typename T>
inline void ShapeLoader<TransformedShape<T, 3> >::rotate(T a, T x, T y, T z)
{
- TransformationLoader<T, 3>::transformation *= AffineTransformation<T, 3>::rotation(Angle<T>::from_degrees(a), LinAl::Vector<T, 3>(x, y, z));
+ TransformationLoader<T, 3>::transformation *= AffineTransform<T, 3>::rotation(Angle<T>::from_degrees(a), LinAl::Vector<T, 3>(x, y, z));
}
#ifndef MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
#define MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
-#include "affinetransformation.h"
+#include "affinetransform.h"
#include "shape.h"
namespace Msp {
{
private:
Shape<T, D> *shape;
- AffineTransformation<T, D> transformation;
- AffineTransformation<T, D> inverse_trans;
+ AffineTransform<T, D> transformation;
+ AffineTransform<T, D> inverse_trans;
public:
- TransformedShape(const Shape<T, D> &, const AffineTransformation<T, D> &);
+ TransformedShape(const Shape<T, D> &, const AffineTransform<T, D> &);
TransformedShape(const TransformedShape &);
TransformedShape &operator=(const TransformedShape &);
~TransformedShape();
virtual TransformedShape *clone() const;
const Shape<T, D> &get_shape() const { return *shape; }
- const AffineTransformation<T, D> &get_transformation() const { return transformation; }
+ const AffineTransform<T, D> &get_transformation() const { return transformation; }
virtual BoundingBox<T, D> get_axis_aligned_bounding_box(unsigned = 0) const;
virtual bool contains(const LinAl::Vector<T, D> &) const;
};
template<typename T, unsigned D>
-inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
+inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransform<T, D> &t):
shape(s.clone()),
transformation(t),
inverse_trans(invert(t))