X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=laserbrain_demo;a=blobdiff_plain;f=src%2Fgeom.h;h=0d8dd003658fa44c4cccbd32dda21586788a04b0;hp=e110193ceb9e81968c7a36a019c15383bcfce342;hb=2f5ee2007d258d947f2efab3bf3460479fe34813;hpb=b7c92831285013b2a0783bccaf3d900545ebb5ba diff --git a/src/geom.h b/src/geom.h index e110193..0d8dd00 100644 --- a/src/geom.h +++ b/src/geom.h @@ -1,16 +1,32 @@ #ifndef GEOMOBJ_H_ #define GEOMOBJ_H_ +/* TODO: + * - implement distance functions + */ + #include enum GeomObjectType { GOBJ_UNKNOWN, GOBJ_SPHERE, GOBJ_AABOX, - GOBJ_PLANE + GOBJ_BOX, + GOBJ_PLANE, + GOBJ_DISC +}; + +enum { + AABOX_PLANE_PX, + AABOX_PLANE_NX, + AABOX_PLANE_PY, + AABOX_PLANE_NY, + AABOX_PLANE_PZ, + AABOX_PLANE_NZ }; class GeomObject; +class Plane; struct HitPoint { float dist; // parametric distance along the ray @@ -23,16 +39,22 @@ struct HitPoint { class GeomObject { public: + GeomObjectType type; + + GeomObject(); virtual ~GeomObject(); - virtual GeomObjectType get_type() const = 0; - virtual void set_union(const GeomObject *obj1, const GeomObject *obj2) = 0; - virtual void set_intersection(const GeomObject *obj1, const GeomObject *obj2) = 0; + virtual bool valid() const; + virtual void invalidate(); virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0; virtual bool contains(const Vec3 &pt) const = 0; - virtual float distance(const Vec3 &v) const = 0; + virtual float distance(const Vec3 &v) const; + virtual float signed_distance(const Vec3 &v) const; + + virtual float distance_sq(const Vec3 &v) const = 0; + virtual float signed_distance_sq(const Vec3 &v) const = 0; }; class Sphere : public GeomObject { @@ -43,15 +65,14 @@ public: Sphere(); Sphere(const Vec3 ¢er, float radius); - GeomObjectType get_type() const; - - void set_union(const GeomObject *obj1, const GeomObject *obj2); - void set_intersection(const GeomObject *obj1, const GeomObject *obj2); + virtual bool valid() const; + virtual void invalidate(); - bool intersect(const Ray &ray, HitPoint *hit = 0) const; - bool contains(const Vec3 &pt) const; + virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const; + virtual bool contains(const Vec3 &pt) const; - float distance(const Vec3 &v) const; + virtual float distance_sq(const Vec3 &v) const; + virtual float signed_distance_sq(const Vec3 &v) const; }; class AABox : public GeomObject { @@ -61,17 +82,42 @@ public: AABox(); AABox(const Vec3 &min, const Vec3 &max); - GeomObjectType get_type() const; + virtual bool valid() const; + virtual void invalidate(); - void set_union(const GeomObject *obj1, const GeomObject *obj2); - void set_intersection(const GeomObject *obj1, const GeomObject *obj2); + virtual Vec3 get_corner(int idx) const; + virtual Plane get_plane(int pidx) const; - bool intersect(const Ray &ray, HitPoint *hit = 0) const; - bool contains(const Vec3 &pt) const; + virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const; + virtual bool contains(const Vec3 &pt) const; - float distance(const Vec3 &v) const; + virtual float distance_sq(const Vec3 &v) const; + virtual float signed_distance_sq(const Vec3 &v) const; }; +class Box : public AABox { +public: + Mat4 xform; + + Box(); + Box(const AABox &aabox, const Mat4 &xform); + Box(const Vec3 &min, const Vec3 &max); + Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform); + Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk); + Box(const Vec3 *varr, int vcount); + + virtual void invalidate(); + + virtual Vec3 get_corner(int idx) const; + + virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const; + virtual bool contains(const Vec3 &pt) const; + + virtual float distance_sq(const Vec3 &v) const; + virtual float signed_distance_sq(const Vec3 &v) const; +}; + + class Plane : public GeomObject { public: Vec3 pt, normal; @@ -81,15 +127,74 @@ public: Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3); Plane(const Vec3 &normal, float dist); - GeomObjectType get_type() const; + virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const; + virtual bool contains(const Vec3 &pt) const; - void set_union(const GeomObject *obj1, const GeomObject *obj2); - void set_intersection(const GeomObject *obj1, const GeomObject *obj2); + virtual float distance(const Vec3 &v) const; + virtual float signed_distance(const Vec3 &v) const; - bool intersect(const Ray &ray, HitPoint *hit = 0) const; - bool contains(const Vec3 &pt) const; + virtual float distance_sq(const Vec3 &v) const; + virtual float signed_distance_sq(const Vec3 &v) const; +}; - float distance(const Vec3 &v) const; +class Disc : public Plane { +public: + float radius; + + Disc(); + Disc(const Vec3 &pt, const Vec3 &normal, float rad); + Disc(const Vec3 &normal, float dist, float rad); + + virtual bool valid() const; + virtual void invalidate(); + + virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const; + //! true if the projection of pt to the plane is contained within the disc radius + virtual bool contains(const Vec3 &pt) const; + + virtual float distance_sq(const Vec3 &v) const; + virtual float signed_distance_sq(const Vec3 &v) const; }; +//! project point to plane +Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane); + +//! calculate the bounding sphere of any object +bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj); +//! calculate the bounding sphere of two objects +bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b); +//! calculate the bounding sphere of multiple objects +bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num); +//! calculate the bounding sphere of multiple points, optionally transformed by `xform` +bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity); + +//! calculate the bounding axis-aligned box of any object +bool calc_bounding_aabox(AABox *box, const GeomObject *obj); +//! calculate the bounding axis-aligned box of two objects +bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b); +//! calculate the bounding axis-aligned box of multiple objects +bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num); +//! calculate the bounding axis-aligned box of multiple points, optionally transformed by `xform` +bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform = Mat4::identity); + +//! calculate the bounding box of any object +bool calc_bounding_box(Box *box, const GeomObject *obj); + +//! calculate the intersection plane of two spheres. false if there is no plane or infinite planes. +bool intersect_sphere_sphere(Plane *result, const Sphere &a, const Sphere &b); +//! calculate the intersection line of two planes. returns false if planes are exactly parallel. +bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b); +/*! calculate the intesection circle of a plane and a sphere. returns false if they don't intersect. + * \{ + */ +bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p); +bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s); +//! \} + +//! calculate the intersection of two axis-aligned bounding boxes +bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b); + +//! determine if a sphere and an axis-aligned box collide +bool collision_sphere_aabox(const Sphere &s, const AABox &b); + #endif // GEOMOBJ_H_