5 * - implement distance functions
8 #include <gmath/gmath.h>
21 float dist; // parametric distance along the ray
22 Vec3 pos; // position of intersection (orig + dir * dist)
23 Vec3 normal; // normal at the point of intersection
25 const GeomObject *obj; // pointer to the intersected geom-object
26 void *data; // place to hang extra data
34 virtual ~GeomObject();
36 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0;
37 virtual bool contains(const Vec3 &pt) const = 0;
39 virtual float distance(const Vec3 &v) const = 0;
40 virtual float signed_distance(const Vec3 &v) const = 0;
43 class Sphere : public GeomObject {
49 Sphere(const Vec3 ¢er, float radius);
51 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
52 virtual bool contains(const Vec3 &pt) const;
54 virtual float distance(const Vec3 &v) const;
55 virtual float signed_distance(const Vec3 &v) const;
58 class AABox : public GeomObject {
63 AABox(const Vec3 &min, const Vec3 &max);
65 virtual Vec3 get_corner(int idx) const;
67 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
68 virtual bool contains(const Vec3 &pt) const;
70 virtual float distance(const Vec3 &v) const;
71 virtual float signed_distance(const Vec3 &v) const;
74 class Box : public AABox {
79 Box(const AABox &aabox, const Mat4 &xform);
80 Box(const Vec3 &min, const Vec3 &max);
81 Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform);
82 Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk);
83 Box(const Vec3 *varr, int vcount);
85 virtual Vec3 get_corner(int idx) const;
87 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
88 virtual bool contains(const Vec3 &pt) const;
90 virtual float distance(const Vec3 &v) const;
91 virtual float signed_distance(const Vec3 &v) const;
95 class Plane : public GeomObject {
100 Plane(const Vec3 &pt, const Vec3 &normal);
101 Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3);
102 Plane(const Vec3 &normal, float dist);
104 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
105 virtual bool contains(const Vec3 &pt) const;
107 virtual float distance(const Vec3 &v) const;
108 virtual float signed_distance(const Vec3 &v) const;
111 class Disc : public Plane {
116 Disc(const Vec3 &pt, const Vec3 &normal, float rad);
117 Disc(const Vec3 &normal, float dist, float rad);
119 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
120 //! true if the projection of pt to the plane is contained within the disc radius
121 virtual bool contains(const Vec3 &pt) const;
123 virtual float distance(const Vec3 &v) const;
124 virtual float signed_distance(const Vec3 &v) const;
127 //! project point to plane
128 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane);
130 //! calculate the bounding sphere of any object
131 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj);
132 //! calculate the bounding sphere of two objects
133 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b);
134 //! calculate the bounding sphere of multiple objects
135 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num);
136 //! calculate the bounding sphere of multiple points, optionally transformed by `xform`
137 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity);
139 //! calculate the bounding axis-aligned box of any object
140 bool calc_bounding_aabox(AABox *box, const GeomObject *obj);
141 //! calculate the bounding axis-aligned box of two objects
142 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b);
143 //! calculate the bounding axis-aligned box of multiple objects
144 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num);
145 //! calculate the bounding axis-aligned box of multiple points, optionally transformed by `xform`
146 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity);
148 //! calculate the bounding box of any object
149 bool calc_bounding_box(Box *box, const GeomObject *obj);
151 //! calculate the intersection plane of two spheres. false if there is no plane or infinite planes.
152 bool intersect_sphere_sphere(Plane *result, const Sphere &a, const Sphere &b);
153 //! calculate the intersection line of two planes. returns false if planes are exactly parallel.
154 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b);
155 /*! calculate the intesection circle of a plane and a sphere. returns false if they don't intersect.
158 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p);
159 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s);
162 //! calculate the intersection of two axis-aligned bounding boxes
163 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b);