rev |
line source |
nuclear@7
|
1 #ifndef GEOM_H_
|
nuclear@7
|
2 #define GEOM_H_
|
nuclear@7
|
3
|
nuclear@7
|
4 #include <vmath/vmath.h>
|
nuclear@7
|
5
|
nuclear@7
|
6 class GeomShape;
|
nuclear@7
|
7
|
nuclear@7
|
8 struct HitPoint {
|
nuclear@7
|
9 float dist;
|
nuclear@7
|
10 Vector3 pos;
|
nuclear@7
|
11 Vector3 normal;
|
nuclear@7
|
12 const GeomShape *shape;
|
nuclear@7
|
13 void *data;
|
nuclear@7
|
14 };
|
nuclear@7
|
15
|
nuclear@7
|
16 class GeomShape {
|
nuclear@8
|
17 public:
|
nuclear@8
|
18 enum Type { GEOM_UNKNOWN, GEOM_SPHERE, GEOM_PLANE, GEOM_AABOX };
|
nuclear@8
|
19
|
nuclear@7
|
20 protected:
|
nuclear@8
|
21 Matrix4x4 xform, inv_xform;
|
nuclear@8
|
22 Type type;
|
nuclear@7
|
23
|
nuclear@7
|
24 public:
|
nuclear@7
|
25 GeomShape();
|
nuclear@7
|
26 virtual ~GeomShape();
|
nuclear@7
|
27
|
nuclear@7
|
28 virtual Type get_type() const;
|
nuclear@7
|
29
|
nuclear@8
|
30 // apply an arbitrary transformation before testing intersections and collisions
|
nuclear@8
|
31 virtual void set_transform(const Matrix4x4 &m);
|
nuclear@8
|
32 virtual const Matrix4x4 &get_transform() const;
|
nuclear@8
|
33 virtual const Matrix4x4 &get_inv_transform() const;
|
nuclear@8
|
34
|
nuclear@8
|
35 virtual bool contains(const Vector3 &pt) const = 0;
|
nuclear@7
|
36 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0;
|
nuclear@7
|
37 virtual bool collide(const GeomShape *geom, HitPoint *hit = 0) const = 0;
|
nuclear@7
|
38
|
nuclear@7
|
39 virtual float distance(const Vector3 &pt) const = 0;
|
nuclear@7
|
40 virtual float distance_sq(const Vector3 &pt) const = 0;
|
nuclear@8
|
41
|
nuclear@8
|
42 // visualize the shape
|
nuclear@8
|
43 virtual void draw() const = 0;
|
nuclear@7
|
44 };
|
nuclear@7
|
45
|
nuclear@7
|
46 class Sphere : public GeomShape {
|
nuclear@7
|
47 public:
|
nuclear@7
|
48 Vector3 center;
|
nuclear@7
|
49 float radius;
|
nuclear@7
|
50
|
nuclear@7
|
51 Sphere();
|
nuclear@7
|
52 Sphere(const Vector3 &c, float rad);
|
nuclear@7
|
53
|
nuclear@8
|
54 bool contains(const Vector3 &pt) const;
|
nuclear@7
|
55 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
|
nuclear@7
|
56 bool collide(const GeomShape *geom, HitPoint *hit = 0) const;
|
nuclear@7
|
57
|
nuclear@7
|
58 float distance(const Vector3 &pt) const;
|
nuclear@7
|
59 float distance_sq(const Vector3 &pt) const;
|
nuclear@8
|
60
|
nuclear@8
|
61 void draw() const;
|
nuclear@7
|
62 };
|
nuclear@7
|
63
|
nuclear@7
|
64 class Plane : public GeomShape {
|
nuclear@7
|
65 public:
|
nuclear@7
|
66 Vector3 normal;
|
nuclear@7
|
67 float dist;
|
nuclear@7
|
68
|
nuclear@7
|
69 Plane();
|
nuclear@7
|
70 Plane(const Vector3 &n, float d);
|
nuclear@7
|
71 Plane(float a, float b, float c, float d);
|
nuclear@7
|
72 Plane(const Vector3 &pos, const Vector3 &norm);
|
nuclear@7
|
73 Plane(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3);
|
nuclear@7
|
74
|
nuclear@8
|
75 bool contains(const Vector3 &pt) const;
|
nuclear@7
|
76 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
|
nuclear@7
|
77 bool collide(const GeomShape *geom, HitPoint *hit = 0) const;
|
nuclear@7
|
78
|
nuclear@7
|
79 float distance(const Vector3 &pt) const;
|
nuclear@7
|
80 float distance_sq(const Vector3 &pt) const;
|
nuclear@8
|
81
|
nuclear@8
|
82 void draw() const;
|
nuclear@8
|
83 };
|
nuclear@8
|
84
|
nuclear@8
|
85 class AABox : public GeomShape {
|
nuclear@8
|
86 public:
|
nuclear@8
|
87 Vector3 min, max;
|
nuclear@8
|
88
|
nuclear@8
|
89 AABox();
|
nuclear@8
|
90 AABox(const Vector3 &min, const Vector3 &max);
|
nuclear@8
|
91
|
nuclear@8
|
92 void set_union(const GeomShape *obj1, const GeomShape *obj2);
|
nuclear@8
|
93 void set_intersection(const GeomShape *obj1, const GeomShape *obj2);
|
nuclear@8
|
94
|
nuclear@8
|
95 bool contains(const Vector3 &pt) const;
|
nuclear@8
|
96 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
|
nuclear@8
|
97 bool collide(const GeomShape *geom, HitPoint *hit = 0) const;
|
nuclear@8
|
98
|
nuclear@8
|
99 float distance(const Vector3 &pt) const;
|
nuclear@8
|
100 float distance_sq(const Vector3 &pt) const;
|
nuclear@8
|
101
|
nuclear@8
|
102 void draw() const;
|
nuclear@7
|
103 };
|
nuclear@7
|
104
|
nuclear@7
|
105 #endif // GEOM_H_
|