nvidia-texture-tools/src/nvmath/Box.h

104 lines
2.7 KiB
C
Raw Normal View History

2007-04-17 08:49:19 +00:00
// This code is in the public domain -- castanyo@yahoo.es
2010-05-27 23:19:24 +00:00
#pragma once
2007-04-17 08:49:19 +00:00
#ifndef NV_MATH_BOX_H
#define NV_MATH_BOX_H
2010-05-27 23:19:24 +00:00
#include "Vector.h"
2007-04-17 08:49:19 +00:00
#include <float.h> // FLT_MAX
namespace nv
{
class Vector;
2010-05-27 23:19:24 +00:00
class Stream;
class Sphere;
// Axis Aligned Bounding Box.
2010-05-27 23:19:24 +00:00
class Box
{
public:
2012-07-20 16:19:03 +00:00
inline Box() {}
inline Box(const Box & b) : minCorner(b.minCorner), maxCorner(b.maxCorner) {}
inline Box(const Vector3 & mins, const Vector3 & maxs) : minCorner(mins), maxCorner(maxs) {}
2010-05-27 23:19:24 +00:00
Box & operator=(const Box & b);
2010-05-27 23:19:24 +00:00
operator const float * () const { return reinterpret_cast<const float *>(this); }
2010-05-27 23:19:24 +00:00
// Clear the bounds.
void clearBounds();
2011-01-08 04:54:06 +00:00
2012-07-20 16:19:03 +00:00
// min < max
bool isValid() const;
// Build a cube centered on center and with edge = 2*dist
void cube(const Vector3 & center, float dist);
// Build a box, given center and extents.
void setCenterExtents(const Vector3 & center, const Vector3 & extents);
// Get box center.
Vector3 center() const;
// Return extents of the box.
Vector3 extents() const;
// Return extents of the box.
float extents(uint axis) const;
2010-05-27 23:19:24 +00:00
// Add a point to this box.
void addPointToBounds(const Vector3 & p);
// Add a box to this box.
void addBoxToBounds(const Box & b);
2012-07-20 16:19:03 +00:00
// Add sphere to this box.
void addSphereToBounds(const Vector3 & p, float r);
// Translate box.
void translate(const Vector3 & v);
// Scale the box.
void scale(float s);
2010-05-27 23:19:24 +00:00
// Expand the box by a fixed amount.
void expand(float r);
// Get the area of the box.
float area() const;
// Get the volume of the box.
float volume() const;
// Return true if the box contains the given point.
bool contains(const Vector3 & p) const;
// Split the given box in 8 octants and assign the ith one to this box.
void setOctant(const Box & box, const Vector3 & center, int i);
2010-05-27 23:19:24 +00:00
2012-07-20 16:19:03 +00:00
// Clip the given segment against this box.
bool clipSegment(const Vector3 & origin, const Vector3 & dir, float * t_near, float * t_far) const;
2010-05-27 23:19:24 +00:00
friend Stream & operator<< (Stream & s, Box & box);
2012-02-14 16:31:25 +00:00
const Vector3 & corner(int i) const { return (&minCorner)[i]; }
2010-05-27 23:19:24 +00:00
Vector3 minCorner;
Vector3 maxCorner;
};
float distanceSquared(const Box &box, const Vector3 &point);
bool overlap(const Box &box, const Sphere &sphere);
2007-04-17 08:49:19 +00:00
2012-02-14 16:31:25 +00:00
// p is ray origin, id is inverse ray direction.
bool intersect(const Box & box, const Vector3 & p, const Vector3 & id, float * t);
2007-04-17 08:49:19 +00:00
} // nv namespace
#endif // NV_MATH_BOX_H