5db1996d09c5743d4a4fa59debaa7db5dc18ee7c
[laserbrain_demo] / src / geom.h
1 #ifndef GEOMOBJ_H_
2 #define GEOMOBJ_H_
3
4 /* TODO:
5  * - implement distance functions
6  */
7
8 #include <gmath/gmath.h>
9
10 enum GeomObjectType {
11         GOBJ_UNKNOWN,
12         GOBJ_SPHERE,
13         GOBJ_AABOX,
14         GOBJ_BOX,
15         GOBJ_PLANE,
16         GOBJ_DISC
17 };
18
19 class GeomObject;
20
21 struct HitPoint {
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
25         Ray ray, local_ray;
26         const GeomObject *obj;  // pointer to the intersected geom-object
27         void *data;                     // place to hang extra data
28 };
29
30 class GeomObject {
31 public:
32         GeomObjectType type;
33
34         GeomObject();
35         virtual ~GeomObject();
36
37         virtual bool valid() const;
38         virtual void invalidate();
39
40         virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0;
41         virtual bool contains(const Vec3 &pt) const = 0;
42
43         virtual float distance(const Vec3 &v) const = 0;
44         virtual float signed_distance(const Vec3 &v) const = 0;
45 };
46
47 class Sphere : public GeomObject {
48 public:
49         Vec3 center;
50         float radius;
51
52         Sphere();
53         Sphere(const Vec3 &center, float radius);
54
55         virtual bool valid() const;
56         virtual void invalidate();
57
58         virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
59         virtual bool contains(const Vec3 &pt) const;
60
61         virtual float distance(const Vec3 &v) const;
62         virtual float signed_distance(const Vec3 &v) const;
63 };
64
65 class AABox : public GeomObject {
66 public:
67         Vec3 min, max;
68
69         AABox();
70         AABox(const Vec3 &min, const Vec3 &max);
71
72         virtual bool valid() const;
73         virtual void invalidate();
74
75         virtual Vec3 get_corner(int idx) const;
76
77         virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
78         virtual bool contains(const Vec3 &pt) const;
79
80         virtual float distance(const Vec3 &v) const;
81         virtual float signed_distance(const Vec3 &v) const;
82 };
83
84 class Box : public AABox {
85 public:
86         Mat4 xform;
87
88         Box();
89         Box(const AABox &aabox, const Mat4 &xform);
90         Box(const Vec3 &min, const Vec3 &max);
91         Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform);
92         Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk);
93         Box(const Vec3 *varr, int vcount);
94
95         virtual void invalidate();
96
97         virtual Vec3 get_corner(int idx) const;
98
99         virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
100         virtual bool contains(const Vec3 &pt) const;
101
102         virtual float distance(const Vec3 &v) const;
103         virtual float signed_distance(const Vec3 &v) const;
104 };
105
106
107 class Plane : public GeomObject {
108 public:
109         Vec3 pt, normal;
110
111         Plane();
112         Plane(const Vec3 &pt, const Vec3 &normal);
113         Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3);
114         Plane(const Vec3 &normal, float dist);
115
116         virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
117         virtual bool contains(const Vec3 &pt) const;
118
119         virtual float distance(const Vec3 &v) const;
120         virtual float signed_distance(const Vec3 &v) const;
121 };
122
123 class Disc : public Plane {
124 public:
125         float radius;
126
127         Disc();
128         Disc(const Vec3 &pt, const Vec3 &normal, float rad);
129         Disc(const Vec3 &normal, float dist, float rad);
130
131         virtual bool valid() const;
132         virtual void invalidate();
133
134         virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const;
135         //! true if the projection of pt to the plane is contained within the disc radius
136         virtual bool contains(const Vec3 &pt) const;
137
138         virtual float distance(const Vec3 &v) const;
139         virtual float signed_distance(const Vec3 &v) const;
140 };
141
142 //! project point to plane
143 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane);
144
145 //! calculate the bounding sphere of any object
146 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj);
147 //! calculate the bounding sphere of two objects
148 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b);
149 //! calculate the bounding sphere of multiple objects
150 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num);
151 //! calculate the bounding sphere of multiple points, optionally transformed by `xform`
152 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity);
153
154 //! calculate the bounding axis-aligned box of any object
155 bool calc_bounding_aabox(AABox *box, const GeomObject *obj);
156 //! calculate the bounding axis-aligned box of two objects
157 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b);
158 //! calculate the bounding axis-aligned box of multiple objects
159 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num);
160 //! calculate the bounding axis-aligned box of multiple points, optionally transformed by `xform`
161 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity);
162
163 //! calculate the bounding box of any object
164 bool calc_bounding_box(Box *box, const GeomObject *obj);
165
166 //! calculate the intersection plane of two spheres. false if there is no plane or infinite planes.
167 bool intersect_sphere_sphere(Plane *result, const Sphere &a, const Sphere &b);
168 //! calculate the intersection line of two planes. returns false if planes are exactly parallel.
169 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b);
170 /*! calculate the intesection circle of a plane and a sphere. returns false if they don't intersect.
171  * \{
172  */
173 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p);
174 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s);
175 //! \}
176
177 //! calculate the intersection of two axis-aligned bounding boxes
178 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b);
179
180 #endif  // GEOMOBJ_H_