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 Vec3 &min, const Vec3 &max);
80 Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk);
81 Box(const Vec3 *varr, int vcount);
83 virtual Vec3 get_corner(int idx) const;
85 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
86 virtual bool contains(const Vec3 &pt) const;
88 virtual float distance(const Vec3 &v) const;
89 virtual float signed_distance(const Vec3 &v) const;
93 class Plane : public GeomObject {
98 Plane(const Vec3 &pt, const Vec3 &normal);
99 Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3);
100 Plane(const Vec3 &normal, float dist);
102 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
103 virtual bool contains(const Vec3 &pt) const;
105 virtual float distance(const Vec3 &v) const;
106 virtual float signed_distance(const Vec3 &v) const;
109 class Disc : public Plane {
114 Disc(const Vec3 &pt, const Vec3 &normal, float rad);
115 Disc(const Vec3 &normal, float dist, float rad);
117 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
118 //! true if the projection of pt to the plane is contained within the disc radius
119 virtual bool contains(const Vec3 &pt) const;
121 virtual float distance(const Vec3 &v) const;
122 virtual float signed_distance(const Vec3 &v) const;
125 //! project point to plane
126 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane);
128 //! calculate the bounding sphere of any object
129 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj);
130 //! calculate the bounding sphere of two objects
131 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b);
132 //! calculate the bounding sphere of multiple objects
133 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num);
134 //! calculate the bounding sphere of multiple points, optionally transformed by `xform`
135 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity);
137 //! calculate the bounding axis-aligned box of any object
138 bool calc_bounding_aabox(AABox *box, const GeomObject *obj);
139 //! calculate the bounding axis-aligned box of two objects
140 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b);
141 //! calculate the bounding axis-aligned box of multiple objects
142 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num);
143 //! calculate the bounding axis-aligned box of multiple points, optionally transformed by `xform`
144 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity);
146 //! calculate the intersection plane of two spheres. false if there is no plane or infinite planes.
147 bool intersect_sphere_sphere(Plane *result, const Sphere &a, const Sphere &b);
148 //! calculate the intersection line of two planes. returns false if planes are exactly parallel.
149 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b);
150 /*! calculate the intesection circle of a plane and a sphere. returns false if they don't intersect.
153 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p);
154 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s);
157 //! calculate the intersection of two axis-aligned bounding boxes
158 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b);