Merge changes from The Witness.

This commit is contained in:
castano
2012-07-20 16:19:03 +00:00
parent fea97461c5
commit 3b4fcd0369
36 changed files with 1073 additions and 178 deletions

View File

@ -8,6 +8,58 @@ using namespace nv;
// Clip the given segment against this box.
bool Box::clipSegment(const Vector3 & origin, const Vector3 & dir, float * t_near, float * t_far) const {
// Avoid aliasing.
float tnear = *t_near;
float tfar = *t_far;
// clip ray segment to box
for (int i = 0; i < 3; i++)
{
const float pos = origin.component[i] + tfar * dir.component[i];
const float dt = tfar - tnear;
if (dir.component[i] < 0) {
// clip end point
if (pos < minCorner.component[i]) {
tfar = tnear + dt * (origin.component[i] - minCorner.component[i]) / (origin.component[i] - pos);
}
// clip start point
if (origin.component[i] > maxCorner.component[i]) {
tnear = tnear + dt * (origin.component[i] - maxCorner.component[i]) / (tfar * dir.component[i]);
}
}
else {
// clip end point
if (pos > maxCorner.component[i]) {
tfar = tnear + dt * (maxCorner.component[i] - origin.component[i]) / (pos - origin.component[i]);
}
// clip start point
if (origin.component[i] < minCorner.component[i]) {
tnear = tnear + dt * (minCorner.component[i] - origin.component[i]) / (tfar * dir.component[i]);
}
}
if (tnear > tfar) {
// Clipped away.
return false;
}
}
// Return result.
*t_near = tnear;
*t_far = tfar;
return true;
}
float nv::distanceSquared(const Box &box, const Vector3 &point) {
Vector3 closest;
@ -64,3 +116,4 @@ bool nv::intersect(const Box & box, const Vector3 & p, const Vector3 & id, float
return true;
}

View File

@ -19,9 +19,9 @@ namespace nv
{
public:
Box();
Box(const Box & b);
Box(const Vector3 & mins, const Vector3 & maxs);
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) {}
Box & operator=(const Box & b);
@ -30,6 +30,9 @@ namespace nv
// Clear the bounds.
void clearBounds();
// min < max
bool isValid() const;
// Build a cube centered on center and with edge = 2*dist
void cube(const Vector3 & center, float dist);
@ -51,6 +54,9 @@ namespace nv
// Add a box to this box.
void addBoxToBounds(const Box & b);
// Add sphere to this box.
void addSphereToBounds(const Vector3 & p, float r);
// Translate box.
void translate(const Vector3 & v);
@ -72,6 +78,11 @@ namespace nv
// 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);
// Clip the given segment against this box.
bool clipSegment(const Vector3 & origin, const Vector3 & dir, float * t_near, float * t_far) const;
friend Stream & operator<< (Stream & s, Box & box);
const Vector3 & corner(int i) const { return (&minCorner)[i]; }

View File

@ -12,13 +12,13 @@
namespace nv
{
// Default ctor.
inline Box::Box() { };
//inline Box::Box() { };
// Copy ctor.
inline Box::Box(const Box & b) : minCorner(b.minCorner), maxCorner(b.maxCorner) { }
//inline Box::Box(const Box & b) : minCorner(b.minCorner), maxCorner(b.maxCorner) { }
// Init ctor.
inline Box::Box(const Vector3 & mins, const Vector3 & maxs) : minCorner(mins), maxCorner(maxs) { }
//inline Box::Box(const Vector3 & mins, const Vector3 & maxs) : minCorner(mins), maxCorner(maxs) { }
// Assignment operator.
inline Box & Box::operator=(const Box & b) { minCorner = b.minCorner; maxCorner = b.maxCorner; return *this; }
@ -30,6 +30,12 @@ namespace nv
maxCorner.set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
}
// min < max
inline bool Box::isValid() const
{
return minCorner.x <= maxCorner.x && minCorner.y <= maxCorner.y && minCorner.z <= maxCorner.z;
}
// Build a cube centered on center and with edge = 2*dist
inline void Box::cube(const Vector3 & center, float dist)
{
@ -62,7 +68,7 @@ namespace nv
if (axis == 0) return (maxCorner.x - minCorner.x) * 0.5f;
if (axis == 1) return (maxCorner.y - minCorner.y) * 0.5f;
if (axis == 2) return (maxCorner.z - minCorner.z) * 0.5f;
nvAssume(false);
nvUnreachable();
return 0.0f;
}
@ -80,6 +86,12 @@ namespace nv
maxCorner = max(maxCorner, b.maxCorner);
}
// Add sphere to this box.
inline void Box::addSphereToBounds(const Vector3 & p, float r) {
minCorner = min(minCorner, p - Vector3(r));
maxCorner = min(maxCorner, p + Vector3(r));
}
// Translate box.
inline void Box::translate(const Vector3 & v)
{

View File

@ -14,7 +14,7 @@ namespace nv
// Clamp color components.
inline Vector3 colorClamp(Vector3::Arg c)
{
return Vector3(clamp(c.x, 0.0f, 1.0f), clamp(c.y, 0.0f, 1.0f), clamp(c.z, 0.0f, 1.0f));
return Vector3(saturate(c.x), saturate(c.y), saturate(c.z));
}
// Clamp without allowing the hue to change.
@ -63,11 +63,10 @@ namespace nv
inline Color32 toColor32(Vector4::Arg v)
{
Color32 color;
color.r = uint8(clamp(v.x, 0.0f, 1.0f) * 255);
color.g = uint8(clamp(v.y, 0.0f, 1.0f) * 255);
color.b = uint8(clamp(v.z, 0.0f, 1.0f) * 255);
color.a = uint8(clamp(v.w, 0.0f, 1.0f) * 255);
color.r = uint8(saturate(v.x) * 255);
color.g = uint8(saturate(v.y) * 255);
color.b = uint8(saturate(v.z) * 255);
color.a = uint8(saturate(v.w) * 255);
return color;
}
@ -87,6 +86,13 @@ namespace nv
return sqrtf((2 + rmean)*r*r + 4*g*g + (3 - rmean)*b*b);
}
inline float hue(float r, float g, float b) {
float h = atan2f(sqrtf(3.0f)*(g-b), 2*r-g-b) * (1.0f / (2 * PI)) + 0.5f;
return h;
}
} // nv namespace
#endif // NV_MATH_COLOR_INL

View File

@ -1,4 +1,310 @@
// This code is in the public domain -- castanyo@yahoo.es
#include "Matrix.h"
#include "Matrix.inl"
#include "Vector.inl"
#include "nvcore/Array.inl"
#include <float.h>
using namespace nv;
// Given a matrix a[1..n][1..n], this routine replaces it by the LU decomposition of a rowwise
// permutation of itself. a and n are input. a is output, arranged as in equation (2.3.14) above;
// indx[1..n] is an output vector that records the row permutation effected by the partial
// pivoting; d is output as -1 depending on whether the number of row interchanges was even
// or odd, respectively. This routine is used in combination with lubksb to solve linear equations
// or invert a matrix.
static bool ludcmp(float **a, int n, int *indx, float *d)
{
const float TINY = 1.0e-20f;
Array<float> vv; // vv stores the implicit scaling of each row.
vv.resize(n);
*d = 1.0; // No row interchanges yet.
for (int i = 0; i < n; i++) { // Loop over rows to get the implicit scaling information.
float big = 0.0;
for (int j = 0; j < n; j++) {
big = max(big, fabsf(a[i][j]));
}
if (big == 0) {
return false; // Singular matrix
}
// No nonzero largest element.
vv[i] = 1.0f / big; // Save the scaling.
}
for (int j = 0; j < n; j++) { // This is the loop over columns of Crout's method.
for (int i = 0; i < j; i++) { // This is equation (2.3.12) except for i = j.
float sum = a[i][j];
for (int k = 0; k < i; k++) sum -= a[i][k]*a[k][j];
a[i][j] = sum;
}
int imax = -1;
float big = 0.0; // Initialize for the search for largest pivot element.
for (int i = j; i < n; i++) { // This is i = j of equation (2.3.12) and i = j+ 1 : : : N
float sum = a[i][j]; // of equation (2.3.13).
for (int k = 0; k < j; k++) {
sum -= a[i][k]*a[k][j];
}
a[i][j]=sum;
float dum = vv[i]*fabs(sum);
if (dum >= big) {
// Is the figure of merit for the pivot better than the best so far?
big = dum;
imax = i;
}
}
nvDebugCheck(imax != -1);
if (j != imax) { // Do we need to interchange rows?
for (int k = 0; k < n; k++) { // Yes, do so...
swap(a[imax][k], a[j][k]);
}
*d = -(*d); // ...and change the parity of d.
vv[imax]=vv[j]; // Also interchange the scale factor.
}
indx[j]=imax;
if (a[j][j] == 0.0) a[j][j] = TINY;
// If the pivot element is zero the matrix is singular (at least to the precision of the
// algorithm). For some applications on singular matrices, it is desirable to substitute
// TINY for zero.
if (j != n-1) { // Now, finally, divide by the pivot element.
float dum = 1.0f / a[j][j];
for (int i = j+1; i < n; i++) a[i][j] *= dum;
}
} // Go back for the next column in the reduction.
return true;
}
// Solves the set of n linear equations Ax = b. Here a[1..n][1..n] is input, not as the matrix
// A but rather as its LU decomposition, determined by the routine ludcmp. indx[1..n] is input
// as the permutation vector returned by ludcmp. b[1..n] is input as the right-hand side vector
// B, and returns with the solution vector X. a, n, and indx are not modified by this routine
// and can be left in place for successive calls with different right-hand sides b. This routine takes
// into account the possibility that b will begin with many zero elements, so it is efficient for use
// in matrix inversion.
static void lubksb(float **a, int n, int *indx, float b[])
{
int ii = 0;
for (int i=0; i<n; i++) { // When ii is set to a positive value, it will become
int ip = indx[i]; // the index of the first nonvanishing element of b. We now
float sum = b[ip]; // do the forward substitution, equation (2.3.6). The
b[ip] = b[i]; // only new wrinkle is to unscramble the permutation as we go.
if (ii != 0) {
for (int j = ii-1; j < i; j++) sum -= a[i][j]*b[j];
}
else if (sum != 0.0f) {
ii = i+1; // A nonzero element was encountered, so from now on we
}
b[i] = sum; // will have to do the sums in the loop above.
}
for (int i=n-1; i>=0; i--) { // Now we do the backsubstitution, equation (2.3.7).
float sum = b[i];
for (int j = i+1; j < n; j++) {
sum -= a[i][j]*b[j];
}
b[i] = sum/a[i][i]; // Store a component of the solution vector X.
} // All done!
}
bool nv::solveLU(const Matrix & A, const Vector4 & b, Vector4 * x)
{
nvDebugCheck(x != NULL);
float m[4][4];
float *a[4] = {m[0], m[1], m[2], m[3]};
int idx[4];
float d;
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
a[x][y] = A(x, y);
}
}
// Create LU decomposition.
if (!ludcmp(a, 4, idx, &d)) {
// Singular matrix.
return false;
}
// Init solution.
*x = b;
// Do back substitution.
lubksb(a, 4, idx, x->component);
return true;
}
bool nv::solveLU(const Matrix3 & A, const Vector3 & b, Vector3 * x)
{
nvDebugCheck(x != NULL);
float m[3][3];
float *a[3] = {m[0], m[1], m[2]};
int idx[3];
float d;
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
a[x][y] = A(x, y);
}
}
// Create LU decomposition.
if (!ludcmp(a, 3, idx, &d)) {
// Singular matrix.
return false;
}
// Init solution.
*x = b;
// Do back substitution.
lubksb(a, 3, idx, x->component);
return true;
}
bool nv::solveCramer(const Matrix & A, const Vector4 & b, Vector4 * x)
{
nvDebugCheck(x != NULL);
*x = transform(inverse(A), b);
return true; // @@ Return false if determinant(A) == 0 !
}
bool nv::solveCramer(const Matrix3 & A, const Vector3 & b, Vector3 * x)
{
nvDebugCheck(x != NULL);
const float det = A.determinant();
if (equal(det, 0.0f)) { // @@ Use input epsilon.
return false;
}
Matrix3 Ai = inverse(A);
*x = transform(Ai, b);
return true;
}
#if 0
// Copyright (C) 1999-2004 Michael Garland.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, provided that the above
// copyright notice(s) and this permission notice appear in all copies of
// the Software and that both the above copyright notice(s) and this
// permission notice appear in supporting documentation.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
// OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
// INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
// FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
// WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Except as contained in this notice, the name of a copyright holder
// shall not be used in advertising or otherwise to promote the sale, use
// or other dealings in this Software without prior written authorization
// of the copyright holder.
// Matrix inversion code for 4x4 matrices using Gaussian elimination
// with partial pivoting. This is a specialized version of a
// procedure originally due to Paul Heckbert <ph@cs.cmu.edu>.
//
// Returns determinant of A, and B=inverse(A)
// If matrix A is singular, returns 0 and leaves trash in B.
//
#define SWAP(a, b, t) {t = a; a = b; b = t;}
double invert(Mat4& B, const Mat4& m)
{
Mat4 A = m;
int i, j, k;
double max, t, det, pivot;
/*---------- forward elimination ----------*/
for (i=0; i<4; i++) /* put identity matrix in B */
for (j=0; j<4; j++)
B(i, j) = (double)(i==j);
det = 1.0;
for (i=0; i<4; i++) { /* eliminate in column i, below diag */
max = -1.;
for (k=i; k<4; k++) /* find pivot for column i */
if (fabs(A(k, i)) > max) {
max = fabs(A(k, i));
j = k;
}
if (max<=0.) return 0.; /* if no nonzero pivot, PUNT */
if (j!=i) { /* swap rows i and j */
for (k=i; k<4; k++)
SWAP(A(i, k), A(j, k), t);
for (k=0; k<4; k++)
SWAP(B(i, k), B(j, k), t);
det = -det;
}
pivot = A(i, i);
det *= pivot;
for (k=i+1; k<4; k++) /* only do elems to right of pivot */
A(i, k) /= pivot;
for (k=0; k<4; k++)
B(i, k) /= pivot;
/* we know that A(i, i) will be set to 1, so don't bother to do it */
for (j=i+1; j<4; j++) { /* eliminate in rows below i */
t = A(j, i); /* we're gonna zero this guy */
for (k=i+1; k<4; k++) /* subtract scaled row i from row j */
A(j, k) -= A(i, k)*t; /* (ignore k<=i, we know they're 0) */
for (k=0; k<4; k++)
B(j, k) -= B(i, k)*t;
}
}
/*---------- backward elimination ----------*/
for (i=4-1; i>0; i--) { /* eliminate in column i, above diag */
for (j=0; j<i; j++) { /* eliminate in rows above i */
t = A(j, i); /* we're gonna zero this guy */
for (k=0; k<4; k++) /* subtract scaled row i from row j */
B(j, k) -= B(i, k)*t;
}
}
return det;
}
#endif // 0

View File

@ -6,10 +6,15 @@
#include "Vector.h"
// - Matrices are stored in memory in *column major* order.
// - Points are to be though of as column vectors.
// - Transformation of a point p by a matrix M is: p' = M * p
namespace nv
{
enum identity_t { identity };
// 3x3 matrix.
class NVMATH_CLASS Matrix3
{
public:
@ -19,6 +24,8 @@ namespace nv
Matrix3(const Matrix3 & m);
Matrix3(Vector3::Arg v0, Vector3::Arg v1, Vector3::Arg v2);
float data(uint idx) const;
float & data(uint idx);
float get(uint row, uint col) const;
float operator()(uint row, uint col) const;
float & operator()(uint row, uint col);
@ -31,17 +38,22 @@ namespace nv
void operator+=(const Matrix3 & m);
void operator-=(const Matrix3 & m);
void scale(float s);
void scale(Vector3::Arg s);
float determinant() const;
private:
float m_data[9];
};
// Solve equation system using LU decomposition and back-substitution.
extern bool solveLU(const Matrix3 & m, const Vector3 & b, Vector3 * x);
// 4x4 transformation matrix.
// -# Matrices are stored in memory in column major order.
// -# Points are to be though of as column vectors.
// -# Transformation of a point p by a matrix M is: p' = M * p
// Solve equation system using Cramer's inverse.
extern bool solveCramer(const Matrix3 & A, const Vector3 & b, Vector3 * x);
// 4x4 matrix.
class NVMATH_CLASS Matrix
{
public:
@ -76,6 +88,12 @@ namespace nv
float m_data[16];
};
// Solve equation system using LU decomposition and back-substitution.
extern bool solveLU(const Matrix & m, const Vector4 & b, Vector4 * x);
// Solve equation system using Cramer's inverse.
extern bool solveCramer(const Matrix & A, const Vector4 & b, Vector4 * x);
} // nv namespace
#endif // NV_MATH_MATRIX_H

View File

@ -40,6 +40,16 @@ namespace nv
m_data[6] = v2.x; m_data[7] = v2.y; m_data[8] = v2.z;
}
inline float Matrix3::data(uint idx) const
{
nvDebugCheck(idx < 9);
return m_data[idx];
}
inline float & Matrix3::data(uint idx)
{
nvDebugCheck(idx < 9);
return m_data[idx];
}
inline float Matrix3::get(uint row, uint col) const
{
nvDebugCheck(row < 3 && col < 3);
@ -150,6 +160,29 @@ namespace nv
return mul(a, b);
}
// Transform the given 3d vector with the given matrix.
inline Vector3 transform(const Matrix3 & m, const Vector3 & p)
{
return Vector3(
p.x * m(0,0) + p.y * m(0,1) + p.z * m(0,2),
p.x * m(1,0) + p.y * m(1,1) + p.z * m(1,2),
p.x * m(2,0) + p.y * m(2,1) + p.z * m(2,2));
}
inline void Matrix3::scale(float s)
{
for (int i = 0; i < 9; i++) {
m_data[i] *= s;
}
}
inline void Matrix3::scale(Vector3::Arg s)
{
m_data[0] *= s.x; m_data[1] *= s.x; m_data[2] *= s.x;
m_data[3] *= s.y; m_data[4] *= s.y; m_data[5] *= s.y;
m_data[6] *= s.z; m_data[7] *= s.z; m_data[8] *= s.z;
}
inline float Matrix3::determinant() const
{
return
@ -161,6 +194,33 @@ namespace nv
get(0,0) * get(1,2) * get(2,1);
}
// Inverse using Cramer's rule.
inline Matrix3 inverse(const Matrix3 & m)
{
const float det = m.determinant();
if (equal(det, 0.0f, 0.0f)) {
return Matrix3(0);
}
Matrix3 r;
r.data(0) = - m.data(5) * m.data(7) + m.data(4) * m.data(8);
r.data(1) = + m.data(5) * m.data(6) - m.data(3) * m.data(8);
r.data(2) = - m.data(4) * m.data(6) + m.data(3) * m.data(7);
r.data(3) = + m.data(2) * m.data(7) - m.data(1) * m.data(8);
r.data(4) = - m.data(2) * m.data(6) + m.data(0) * m.data(8);
r.data(5) = + m.data(1) * m.data(6) - m.data(0) * m.data(7);
r.data(6) = - m.data(2) * m.data(4) + m.data(1) * m.data(5);
r.data(7) = + m.data(2) * m.data(3) - m.data(0) * m.data(5);
r.data(8) = - m.data(1) * m.data(3) + m.data(0) * m.data(4);
r.scale(1.0f / det);
return r;
}
inline Matrix::Matrix()
@ -470,6 +530,7 @@ namespace nv
return r;
}
// Inverse using Cramer's rule.
inline Matrix inverse(Matrix::Arg m)
{
Matrix r;

View File

@ -6,7 +6,7 @@
namespace nv
{
Plane transformPlane(const Matrix& m, Plane::Arg p)
Plane transformPlane(const Matrix & m, const Plane & p)
{
Vector3 newVec = transformVector(m, p.vector());
@ -16,7 +16,7 @@ namespace nv
return Plane(newVec, ptInPlane);
}
Vector3 planeIntersection(Plane::Arg a, Plane::Arg b, Plane::Arg c)
Vector3 planeIntersection(const Plane & a, const Plane & b, const Plane & c)
{
return dot(a.vector(), cross(b.vector(), c.vector())) * (
a.offset() * cross(b.vector(), c.vector()) +

View File

@ -14,31 +14,26 @@ namespace nv
class NVMATH_CLASS Plane
{
public:
typedef Plane const & Arg;
Plane();
Plane(float x, float y, float z, float w);
Plane(Vector4::Arg v);
Plane(Vector3::Arg v, float d);
Plane(Vector3::Arg normal, Vector3::Arg point);
Plane(const Vector4 & v);
Plane(const Vector3 & v, float d);
Plane(const Vector3 & normal, const Vector3 & point);
Plane(const Vector3 & v0, const Vector3 & v1, const Vector3 & v2);
const Plane & operator=(Plane::Arg v);
const Plane & operator=(const Plane & v);
Vector3 vector() const;
float offset() const;
const Vector4 & asVector() const;
Vector4 & asVector();
void operator*=(float s);
private:
Vector4 p;
Vector4 v;
};
Plane transformPlane(const Matrix&, Plane::Arg);
Plane transformPlane(const Matrix &, const Plane &);
Vector3 planeIntersection(Plane::Arg a, Plane::Arg b, Plane::Arg c);
Vector3 planeIntersection(const Plane & a, const Plane & b, const Plane & c);
} // nv namespace

View File

@ -10,37 +10,38 @@
namespace nv
{
inline Plane::Plane() {}
inline Plane::Plane(float x, float y, float z, float w) : p(x, y, z, w) {}
inline Plane::Plane(Vector4::Arg v) : p(v) {}
inline Plane::Plane(Vector3::Arg v, float d) : p(v, d) {}
inline Plane::Plane(Vector3::Arg normal, Vector3::Arg point) : p(normal, dot(normal, point)) {}
inline Plane::Plane(float x, float y, float z, float w) : v(x, y, z, w) {}
inline Plane::Plane(const Vector4 & v) : v(v) {}
inline Plane::Plane(const Vector3 & v, float d) : v(v, d) {}
inline Plane::Plane(const Vector3 & normal, const Vector3 & point) : v(normal, -dot(normal, point)) {}
inline Plane::Plane(const Vector3 & v0, const Vector3 & v1, const Vector3 & v2) {
Vector3 n = cross(v1-v0, v2-v0);
float d = -dot(n, v0);
v = Vector4(n, d);
}
inline const Plane & Plane::operator=(Plane::Arg v) { p = v.p; return *this; }
inline const Plane & Plane::operator=(const Plane & p) { v = p.v; return *this; }
inline Vector3 Plane::vector() const { return p.xyz(); }
inline float Plane::offset() const { return p.w; }
inline const Vector4 & Plane::asVector() const { return p; }
inline Vector4 & Plane::asVector() { return p; }
inline Vector3 Plane::vector() const { return v.xyz(); }
inline float Plane::offset() const { return v.w; }
// Normalize plane.
inline Plane normalize(Plane::Arg plane, float epsilon = NV_EPSILON)
inline Plane normalize(const Plane & plane, float epsilon = NV_EPSILON)
{
const float len = length(plane.vector());
nvDebugCheck(!isZero(len, epsilon));
const float inv = 1.0f / len;
return Plane(plane.asVector() * inv);
const float inv = isZero(len, epsilon) ? 0 : 1.0f / len;
return Plane(plane.v * inv);
}
// Get the signed distance from the given point to this plane.
inline float distance(Plane::Arg plane, Vector3::Arg point)
inline float distance(const Plane & plane, const Vector3 & point)
{
return dot(plane.vector(), point) - plane.offset();
return dot(plane.vector(), point) + plane.offset();
}
inline void Plane::operator*=(float s)
{
scale(p, s);
v *= s;
}
} // nv namespace

View File

@ -18,6 +18,8 @@ namespace nv
Vector2(float x, float y);
Vector2(Vector2::Arg v);
template <typename T> operator T() const { return T(x, y); }
const Vector2 & operator=(Vector2::Arg v);
const float * ptr() const;
@ -41,10 +43,6 @@ namespace nv
};
};
// Helpers to convert vector types. Assume T has x,y members and 2 argument constructor.
template <typename T> T to(Vector2::Arg v) { return T(v.x, v.y); }
class NVMATH_CLASS Vector3
{
public:
@ -56,6 +54,8 @@ namespace nv
Vector3(Vector2::Arg v, float z);
Vector3(Vector3::Arg v);
template <typename T> operator T() const { return T(x, y, z); }
const Vector3 & operator=(Vector3::Arg v);
Vector2 xy() const;
@ -82,10 +82,6 @@ namespace nv
};
};
// Helpers to convert vector types. Assume T has x,y,z members and 3 argument constructor.
template <typename T> T to(Vector3::Arg v) { return T(v.x, v.y, v.z); }
class NVMATH_CLASS Vector4
{
public:
@ -100,6 +96,8 @@ namespace nv
Vector4(Vector4::Arg v);
// Vector4(const Quaternion & v);
template <typename T> operator T() const { return T(x, y, z, w); }
const Vector4 & operator=(Vector4::Arg v);
Vector2 xy() const;
@ -127,10 +125,6 @@ namespace nv
};
};
// Helpers to convert vector types. Assume T has x,y,z members and 3 argument constructor.
template <typename T> T to(Vector4::Arg v) { return T(v.x, v.y, v.z, v.w); }
} // nv namespace
#endif // NV_MATH_VECTOR_H

View File

@ -6,6 +6,7 @@
#include "Vector.h"
#include "nvcore/Utils.h" // min, max
#include "nvcore/Hash.h" // hash
namespace nv
{
@ -419,6 +420,13 @@ namespace nv
return (v0.x * v1.y - v0.y * v1.x);
}
template <>
inline uint hash(const Vector2 & v, uint h)
{
return sdbmFloatHash(v.component, 2, h);
}
// Vector3
@ -617,6 +625,11 @@ namespace nv
return v - (2 * dot(v, n)) * n;
}
template <>
inline uint hash(const Vector3 & v, uint h)
{
return sdbmFloatHash(v.component, 3, h);
}
// Vector4
@ -765,6 +778,12 @@ namespace nv
return vf;
}
template <>
inline uint hash(const Vector4 & v, uint h)
{
return sdbmFloatHash(v.component, 4, h);
}
} // nv namespace
#endif // NV_MATH_VECTOR_INL

View File

@ -54,7 +54,7 @@
#define IS_NEGATIVE_FLOAT(x) (IR(x)&SIGN_BITMASK)
*/
inline double sqrt_assert(const double f)
extern "C" inline double sqrt_assert(const double f)
{
nvDebugCheck(f >= 0.0f);
return sqrt(f);
@ -66,7 +66,7 @@ inline float sqrtf_assert(const float f)
return sqrtf(f);
}
inline double acos_assert(const double f)
extern "C" inline double acos_assert(const double f)
{
nvDebugCheck(f >= -1.0f && f <= 1.0f);
return acos(f);
@ -78,7 +78,7 @@ inline float acosf_assert(const float f)
return acosf(f);
}
inline double asin_assert(const double f)
extern "C" inline double asin_assert(const double f)
{
nvDebugCheck(f >= -1.0f && f <= 1.0f);
return asin(f);
@ -98,6 +98,17 @@ inline float asinf_assert(const float f)
#define asin asin_assert
#define asinf asinf_assert
#if NV_CC_MSVC
NV_FORCEINLINE float log2f(float x)
{
nvCheck(x >= 0);
return logf(x) / logf(2.0f);
}
NV_FORCEINLINE float exp2f(float x)
{
return powf(2.0f, x);
}
#endif
namespace nv
{
@ -109,7 +120,7 @@ namespace nv
inline bool equal(const float f0, const float f1, const float epsilon = NV_EPSILON)
{
//return fabs(f0-f1) <= epsilon;
return fabs(f0-f1) <= epsilon * max(1.0f, fabsf(f0), fabsf(f1));
return fabs(f0-f1) <= epsilon * max3(1.0f, fabsf(f0), fabsf(f1));
}
inline bool isZero(const float f, const float epsilon = NV_EPSILON)
@ -154,18 +165,6 @@ namespace nv
return value;
}
#if NV_CC_MSVC
NV_FORCEINLINE float log2f(float x)
{
nvCheck(x >= 0);
return logf(x) / logf(2.0f);
}
NV_FORCEINLINE float exp2f(float x)
{
return powf(2.0f, x);
}
#endif
inline float lerp(float f0, float f1, float t)
{
const float s = 1.0f - t;
@ -211,8 +210,8 @@ namespace nv
// Eliminates negative zeros from a float array.
inline void floatCleanup(float * fp, int n)
{
nvDebugCheck(isFinite(*fp));
for (int i = 0; i < n; i++) {
//nvDebugCheck(isFinite(fp[i]));
union { float f; uint32 i; } x = { fp[i] };
if (x.i == 0x80000000) fp[i] = 0.0f;
}