5 * - implement distance functions
8 #include <gmath/gmath.h>
32 float dist; // parametric distance along the ray
33 Vec3 pos; // position of intersection (orig + dir * dist)
34 Vec3 normal; // normal at the point of intersection
36 const GeomObject *obj; // pointer to the intersected geom-object
37 void *data; // place to hang extra data
45 virtual ~GeomObject();
47 virtual bool valid() const;
48 virtual void invalidate();
50 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0;
51 virtual bool contains(const Vec3 &pt) const = 0;
53 virtual float distance(const Vec3 &v) const;
54 virtual float signed_distance(const Vec3 &v) const;
56 virtual float distance_sq(const Vec3 &v) const = 0;
57 virtual float signed_distance_sq(const Vec3 &v) const = 0;
60 class Sphere : public GeomObject {
66 Sphere(const Vec3 ¢er, float radius);
68 virtual bool valid() const;
69 virtual void invalidate();
71 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
72 virtual bool contains(const Vec3 &pt) const;
74 virtual float distance_sq(const Vec3 &v) const;
75 virtual float signed_distance_sq(const Vec3 &v) const;
78 class AABox : public GeomObject {
83 AABox(const Vec3 &min, const Vec3 &max);
85 virtual bool valid() const;
86 virtual void invalidate();
88 virtual Vec3 get_corner(int idx) const;
89 virtual Plane get_plane(int pidx) const;
91 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
92 virtual bool contains(const Vec3 &pt) const;
94 virtual float distance_sq(const Vec3 &v) const;
95 virtual float signed_distance_sq(const Vec3 &v) const;
98 class Box : public AABox {
103 Box(const AABox &aabox, const Mat4 &xform);
104 Box(const Vec3 &min, const Vec3 &max);
105 Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform);
106 Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk);
107 Box(const Vec3 *varr, int vcount);
109 virtual void invalidate();
111 virtual Vec3 get_corner(int idx) const;
113 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
114 virtual bool contains(const Vec3 &pt) const;
116 virtual float distance_sq(const Vec3 &v) const;
117 virtual float signed_distance_sq(const Vec3 &v) const;
121 class Plane : public GeomObject {
126 Plane(const Vec3 &pt, const Vec3 &normal);
127 Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3);
128 Plane(const Vec3 &normal, float dist);
130 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
131 virtual bool contains(const Vec3 &pt) const;
133 virtual float distance(const Vec3 &v) const;
134 virtual float signed_distance(const Vec3 &v) const;
136 virtual float distance_sq(const Vec3 &v) const;
137 virtual float signed_distance_sq(const Vec3 &v) const;
140 class Disc : public Plane {
145 Disc(const Vec3 &pt, const Vec3 &normal, float rad);
146 Disc(const Vec3 &normal, float dist, float rad);
148 virtual bool valid() const;
149 virtual void invalidate();
151 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
152 //! true if the projection of pt to the plane is contained within the disc radius
153 virtual bool contains(const Vec3 &pt) const;
155 virtual float distance_sq(const Vec3 &v) const;
156 virtual float signed_distance_sq(const Vec3 &v) const;
159 //! project point to plane
160 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane);
162 //! calculate the bounding sphere of any object
163 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj);
164 //! calculate the bounding sphere of two objects
165 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b);
166 //! calculate the bounding sphere of multiple objects
167 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num);
168 //! calculate the bounding sphere of multiple points, optionally transformed by `xform`
169 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity);
171 //! calculate the bounding axis-aligned box of any object
172 bool calc_bounding_aabox(AABox *box, const GeomObject *obj);
173 //! calculate the bounding axis-aligned box of two objects
174 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b);
175 //! calculate the bounding axis-aligned box of multiple objects
176 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num);
177 //! calculate the bounding axis-aligned box of multiple points, optionally transformed by `xform`
178 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity);
180 //! calculate the bounding box of any object
181 bool calc_bounding_box(Box *box, const GeomObject *obj);
183 //! calculate the intersection plane of two spheres. false if there is no plane or infinite planes.
184 bool intersect_sphere_sphere(Plane *result, const Sphere &a, const Sphere &b);
185 //! calculate the intersection line of two planes. returns false if planes are exactly parallel.
186 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b);
187 /*! calculate the intesection circle of a plane and a sphere. returns false if they don't intersect.
190 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p);
191 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s);
194 //! calculate the intersection of two axis-aligned bounding boxes
195 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b);
197 //! determine if a sphere and an axis-aligned box collide
198 bool collision_sphere_aabox(const Sphere &s, const AABox &b);