]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/hypersphere.h
Properly sort intersection points for complex shapes
[libs/math.git] / source / geometry / hypersphere.h
index 8b7d0a4cf91088f8e4e14028b5116698ca079fb5..cbec2d99af744e7182f4b5f0081379b059a14d95 100644 (file)
@@ -1,14 +1,21 @@
 #ifndef MSP_GEOMETRY_HYPERSPHERE_H_
 #define MSP_GEOMETRY_HYPERSPHERE_H_
 
+#include <cmath>
+#include <stdexcept>
 #include <msp/linal/vector.h>
 #include "hyperbox.h"
 #include "ray.h"
 #include "shape.h"
+#include "surfacepoint.h"
 
 namespace Msp {
 namespace Geometry {
 
+/**
+A shape consisting of the points within a specific distance from the origin.
+Two- and three-dimensional cases are Circle and Sphere, respectively.
+*/
 template<typename T, unsigned D>
 class HyperSphere: public Shape<T, D>
 {
@@ -16,7 +23,7 @@ private:
        T radius;
 
 public:
-       HyperSphere();
+       HyperSphere(): radius(1) { }
        explicit HyperSphere(T);
 
        virtual HyperSphere *clone() const;
@@ -24,18 +31,19 @@ public:
        T get_radius() const { return radius; }
 
        virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
+       virtual bool contains(const LinAl::Vector<T, D> &) const;
        virtual bool check_intersection(const Ray<T, D> &) const;
+       virtual unsigned get_max_ray_intersections() const { return 2; }
+       virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
 };
 
-template<typename T, unsigned D>
-inline HyperSphere<T, D>::HyperSphere():
-       radius(1)
-{ }
-
 template<typename T, unsigned D>
 inline HyperSphere<T, D>::HyperSphere(T r):
        radius(r)
-{ }
+{
+       if(r<=T(0))
+               throw std::invalid_argument("HyperSphere::HyperShpere");
+}
 
 template<typename T, unsigned D>
 inline HyperSphere<T, D> *HyperSphere<T, D>::clone() const
@@ -52,17 +60,52 @@ inline HyperBox<T, D> HyperSphere<T, D>::get_axis_aligned_bounding_box() const
        return HyperBox<T, D>(dimensions);
 }
 
+template<typename T, unsigned D>
+inline bool HyperSphere<T, D>::contains(const LinAl::Vector<T, D> &point) const
+{
+       return inner_product(point, point)<=radius*radius;
+}
+
 template<typename T, unsigned D>
 inline bool HyperSphere<T, D>::check_intersection(const Ray<T, D> &ray) const
 {
        T x = inner_product(ray.get_direction(), ray.get_start());
        if(x>0)
-               return inner_product(ray.get_start(), ray.get_start())<=radius*radius;
+               return contains(ray.get_start());
        else
+               return contains(ray.get_start()-ray.get_direction()*x);
+}
+
+template<typename T, unsigned D>
+inline unsigned HyperSphere<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
+{
+       using std::sqrt;
+
+       T mid = -inner_product(ray.get_direction(), ray.get_start());
+       LinAl::Vector<T, D> nearest = ray.get_start()+ray.get_direction()*mid;
+       T offset_sq = radius*radius-inner_product(nearest, nearest);
+       if(offset_sq<0)
+               return 0;
+       T offset = sqrt(offset_sq);
+
+       unsigned n = 0;
+       for(int i=-1; (n<size && i<=1); i+=2)
        {
-               LinAl::Vector<T, D> nearest = ray.get_start()-ray.get_direction()*x;
-               return inner_product(nearest, nearest)<=radius*radius;
+               T x = mid+offset*i;
+               if(ray.check_limits(x))
+               {
+                       if(points)
+                       {
+                               points[n].position = ray.get_start()+ray.get_direction()*x;
+                               points[n].normal = normalize(points[n].position);
+                               points[n].distance = x;
+                       }
+
+                       ++n;
+               }
        }
+
+       return n;
 }
 
 } // namespace Geometry