5 * - implement distance functions
8 #include <gmath/gmath.h>
22 float dist; // parametric distance along the ray
23 Vec3 pos; // position of intersection (orig + dir * dist)
24 Vec3 normal; // normal at the point of intersection
26 const GeomObject *obj; // pointer to the intersected geom-object
27 void *data; // place to hang extra data
35 virtual ~GeomObject();
37 virtual bool valid() const;
38 virtual void invalidate();
40 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0;
41 virtual bool contains(const Vec3 &pt) const = 0;
43 virtual float distance(const Vec3 &v) const;
44 virtual float signed_distance(const Vec3 &v) const;
46 virtual float distance_sq(const Vec3 &v) const = 0;
47 virtual float signed_distance_sq(const Vec3 &v) const = 0;
50 class Sphere : public GeomObject {
56 Sphere(const Vec3 ¢er, float radius);
58 virtual bool valid() const;
59 virtual void invalidate();
61 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
62 virtual bool contains(const Vec3 &pt) const;
64 virtual float distance_sq(const Vec3 &v) const;
65 virtual float signed_distance_sq(const Vec3 &v) const;
68 class AABox : public GeomObject {
73 AABox(const Vec3 &min, const Vec3 &max);
75 virtual bool valid() const;
76 virtual void invalidate();
78 virtual Vec3 get_corner(int idx) const;
79 virtual Plane get_plane(int pidx) const;
81 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
82 virtual bool contains(const Vec3 &pt) const;
84 virtual float distance_sq(const Vec3 &v) const;
85 virtual float signed_distance_sq(const Vec3 &v) const;
88 class Box : public AABox {
93 Box(const AABox &aabox, const Mat4 &xform);
94 Box(const Vec3 &min, const Vec3 &max);
95 Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform);
96 Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk);
97 Box(const Vec3 *varr, int vcount);
99 virtual void invalidate();
101 virtual Vec3 get_corner(int idx) const;
103 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
104 virtual bool contains(const Vec3 &pt) const;
106 virtual float distance_sq(const Vec3 &v) const;
107 virtual float signed_distance_sq(const Vec3 &v) const;
111 class Plane : public GeomObject {
116 Plane(const Vec3 &pt, const Vec3 &normal);
117 Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3);
118 Plane(const Vec3 &normal, float dist);
120 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
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;
126 virtual float distance_sq(const Vec3 &v) const;
127 virtual float signed_distance_sq(const Vec3 &v) const;
130 class Disc : public Plane {
135 Disc(const Vec3 &pt, const Vec3 &normal, float rad);
136 Disc(const Vec3 &normal, float dist, float rad);
138 virtual bool valid() const;
139 virtual void invalidate();
141 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
142 //! true if the projection of pt to the plane is contained within the disc radius
143 virtual bool contains(const Vec3 &pt) const;
145 virtual float distance_sq(const Vec3 &v) const;
146 virtual float signed_distance_sq(const Vec3 &v) const;
149 //! project point to plane
150 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane);
152 //! calculate the bounding sphere of any object
153 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj);
154 //! calculate the bounding sphere of two objects
155 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b);
156 //! calculate the bounding sphere of multiple objects
157 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num);
158 //! calculate the bounding sphere of multiple points, optionally transformed by `xform`
159 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity);
161 //! calculate the bounding axis-aligned box of any object
162 bool calc_bounding_aabox(AABox *box, const GeomObject *obj);
163 //! calculate the bounding axis-aligned box of two objects
164 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b);
165 //! calculate the bounding axis-aligned box of multiple objects
166 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num);
167 //! calculate the bounding axis-aligned box of multiple points, optionally transformed by `xform`
168 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity);
170 //! calculate the bounding box of any object
171 bool calc_bounding_box(Box *box, const GeomObject *obj);
173 //! calculate the intersection plane of two spheres. false if there is no plane or infinite planes.
174 bool intersect_sphere_sphere(Plane *result, const Sphere &a, const Sphere &b);
175 //! calculate the intersection line of two planes. returns false if planes are exactly parallel.
176 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b);
177 /*! calculate the intesection circle of a plane and a sphere. returns false if they don't intersect.
180 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p);
181 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s);
184 //! calculate the intersection of two axis-aligned bounding boxes
185 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b);
187 //! determine if a sphere and an axis-aligned box collide
188 bool collision_sphere_aabox(const Sphere &s, const AABox &b);