nuclear@7: #ifndef GEOM_H_ nuclear@7: #define GEOM_H_ nuclear@7: nuclear@7: #include nuclear@7: nuclear@7: class GeomShape; nuclear@7: nuclear@7: struct HitPoint { nuclear@7: float dist; nuclear@7: Vector3 pos; nuclear@7: Vector3 normal; nuclear@7: const GeomShape *shape; nuclear@7: void *data; nuclear@7: }; nuclear@7: nuclear@7: class GeomShape { nuclear@8: public: nuclear@8: enum Type { GEOM_UNKNOWN, GEOM_SPHERE, GEOM_PLANE, GEOM_AABOX }; nuclear@8: nuclear@7: protected: nuclear@8: Matrix4x4 xform, inv_xform; nuclear@8: Type type; nuclear@7: nuclear@7: public: nuclear@7: GeomShape(); nuclear@7: virtual ~GeomShape(); nuclear@7: nuclear@7: virtual Type get_type() const; nuclear@7: nuclear@8: // apply an arbitrary transformation before testing intersections and collisions nuclear@8: virtual void set_transform(const Matrix4x4 &m); nuclear@8: virtual const Matrix4x4 &get_transform() const; nuclear@8: virtual const Matrix4x4 &get_inv_transform() const; nuclear@8: nuclear@8: virtual bool contains(const Vector3 &pt) const = 0; nuclear@7: virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0; nuclear@7: virtual bool collide(const GeomShape *geom, HitPoint *hit = 0) const = 0; nuclear@7: nuclear@7: virtual float distance(const Vector3 &pt) const = 0; nuclear@7: virtual float distance_sq(const Vector3 &pt) const = 0; nuclear@8: nuclear@8: // visualize the shape nuclear@8: virtual void draw() const = 0; nuclear@7: }; nuclear@7: nuclear@7: class Sphere : public GeomShape { nuclear@7: public: nuclear@7: Vector3 center; nuclear@7: float radius; nuclear@7: nuclear@7: Sphere(); nuclear@7: Sphere(const Vector3 &c, float rad); nuclear@7: nuclear@8: bool contains(const Vector3 &pt) const; nuclear@7: bool intersect(const Ray &ray, HitPoint *hit = 0) const; nuclear@7: bool collide(const GeomShape *geom, HitPoint *hit = 0) const; nuclear@7: nuclear@7: float distance(const Vector3 &pt) const; nuclear@7: float distance_sq(const Vector3 &pt) const; nuclear@8: nuclear@8: void draw() const; nuclear@7: }; nuclear@7: nuclear@7: class Plane : public GeomShape { nuclear@7: public: nuclear@7: Vector3 normal; nuclear@7: float dist; nuclear@7: nuclear@7: Plane(); nuclear@7: Plane(const Vector3 &n, float d); nuclear@7: Plane(float a, float b, float c, float d); nuclear@7: Plane(const Vector3 &pos, const Vector3 &norm); nuclear@7: Plane(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3); nuclear@7: nuclear@8: bool contains(const Vector3 &pt) const; nuclear@7: bool intersect(const Ray &ray, HitPoint *hit = 0) const; nuclear@7: bool collide(const GeomShape *geom, HitPoint *hit = 0) const; nuclear@7: nuclear@7: float distance(const Vector3 &pt) const; nuclear@7: float distance_sq(const Vector3 &pt) const; nuclear@8: nuclear@8: void draw() const; nuclear@8: }; nuclear@8: nuclear@8: class AABox : public GeomShape { nuclear@8: public: nuclear@8: Vector3 min, max; nuclear@8: nuclear@8: AABox(); nuclear@8: AABox(const Vector3 &min, const Vector3 &max); nuclear@8: nuclear@8: void set_union(const GeomShape *obj1, const GeomShape *obj2); nuclear@8: void set_intersection(const GeomShape *obj1, const GeomShape *obj2); nuclear@8: nuclear@8: bool contains(const Vector3 &pt) const; nuclear@8: bool intersect(const Ray &ray, HitPoint *hit = 0) const; nuclear@8: bool collide(const GeomShape *geom, HitPoint *hit = 0) const; nuclear@8: nuclear@8: float distance(const Vector3 &pt) const; nuclear@8: float distance_sq(const Vector3 &pt) const; nuclear@8: nuclear@8: void draw() const; nuclear@7: }; nuclear@7: nuclear@7: #endif // GEOM_H_