Add some inline files to reduce amount of code parsed.
This commit is contained in:
parent
cbf4b2e3bf
commit
94401919b8
@ -5,6 +5,7 @@
|
||||
#include "FloatImage.h"
|
||||
|
||||
#include "nvmath/Box.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
#include "nvcore/Utils.h" // swap
|
||||
|
||||
#include <string.h> // memcpy
|
||||
|
@ -5,6 +5,7 @@
|
||||
#define NV_IMAGE_COLORBLOCK_H
|
||||
|
||||
#include "nvmath/Color.h"
|
||||
#include "nvmath/Vector.h"
|
||||
|
||||
namespace nv
|
||||
{
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "Filter.h"
|
||||
|
||||
#include "nvmath/Matrix.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
|
||||
#include <float.h> // FLT_MAX
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "Image.h"
|
||||
|
||||
#include "nvmath/Color.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
#include "nvmath/Matrix.h"
|
||||
|
||||
#include "nvcore/Utils.h" // max
|
||||
|
@ -26,7 +26,7 @@
|
||||
#include "FloatImage.h"
|
||||
#include "Image.h"
|
||||
|
||||
#include "nvmath/Color.h"
|
||||
#include "nvmath/Color.inl"
|
||||
|
||||
#include "nvcore/Ptr.h"
|
||||
|
||||
|
@ -17,6 +17,7 @@ http://www.efg2.com/Lab/Library/ImageProcessing/DHALF.TXT
|
||||
#include "PixelFormat.h"
|
||||
|
||||
#include "nvmath/Color.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
|
||||
#include "nvcore/Utils.h" // swap
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
// This code is in the public domain -- castanyo@yahoo.es
|
||||
|
||||
#include "nvmath/Box.h"
|
||||
#include "nvmath/Sphere.h"
|
||||
#include "Box.h"
|
||||
#include "Box.inl"
|
||||
//#include "Sphere.h"
|
||||
|
||||
using namespace nv;
|
||||
|
||||
@ -25,6 +26,6 @@ float nv::distanceSquared(const Box &box, const Vector3 &point) {
|
||||
return lengthSquared(point - closest);
|
||||
}
|
||||
|
||||
bool nv::overlap(const Box &box, const Sphere &sphere) {
|
||||
/*bool nv::overlap(const Box &box, const Sphere &sphere) {
|
||||
return distanceSquared(box, sphere.center) < sphere.radius * sphere.radius;
|
||||
}
|
||||
}*/
|
||||
|
146
src/nvmath/Box.h
146
src/nvmath/Box.h
@ -10,141 +10,67 @@
|
||||
|
||||
namespace nv
|
||||
{
|
||||
class Vector;
|
||||
class Stream;
|
||||
class Sphere;
|
||||
|
||||
/// Axis Aligned Bounding Box.
|
||||
// Axis Aligned Bounding Box.
|
||||
class Box
|
||||
{
|
||||
public:
|
||||
|
||||
/// Default ctor.
|
||||
Box() { };
|
||||
Box();
|
||||
Box(const Box & b);
|
||||
Box(const Vector3 & mins, const Vector3 & maxs);
|
||||
|
||||
/// Copy ctor.
|
||||
Box(const Box & b) : minCorner(b.minCorner), maxCorner(b.maxCorner) { }
|
||||
Box & operator=(const Box & b);
|
||||
|
||||
/// Init ctor.
|
||||
Box(Vector3::Arg mins, Vector3::Arg maxs) : minCorner(mins), maxCorner(maxs) { }
|
||||
|
||||
// Assignment operator.
|
||||
Box & operator=(const Box & b) { minCorner = b.minCorner; maxCorner = b.maxCorner; return *this; }
|
||||
|
||||
// Cast operators.
|
||||
operator const float * () const { return reinterpret_cast<const float *>(this); }
|
||||
|
||||
/// Clear the bounds.
|
||||
void clearBounds()
|
||||
{
|
||||
minCorner.set(FLT_MAX, FLT_MAX, FLT_MAX);
|
||||
maxCorner.set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
|
||||
}
|
||||
// Clear the bounds.
|
||||
void clearBounds();
|
||||
|
||||
/// Build a cube centered on center and with edge = 2*dist
|
||||
void cube(Vector3::Arg center, float dist)
|
||||
{
|
||||
setCenterExtents(center, Vector3(dist, dist, dist));
|
||||
}
|
||||
// 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(Vector3::Arg center, Vector3::Arg extents)
|
||||
{
|
||||
minCorner = center - extents;
|
||||
maxCorner = center + extents;
|
||||
}
|
||||
// Build a box, given center and extents.
|
||||
void setCenterExtents(const Vector3 & center, const Vector3 & extents);
|
||||
|
||||
/// Get box center.
|
||||
Vector3 center() const
|
||||
{
|
||||
return (minCorner + maxCorner) * 0.5f;
|
||||
}
|
||||
// Get box center.
|
||||
Vector3 center() const;
|
||||
|
||||
/// Return extents of the box.
|
||||
Vector3 extents() const
|
||||
{
|
||||
return (maxCorner - minCorner) * 0.5f;
|
||||
}
|
||||
// Return extents of the box.
|
||||
Vector3 extents() const;
|
||||
|
||||
/// Return extents of the box.
|
||||
scalar extents(uint axis) const
|
||||
{
|
||||
nvDebugCheck(axis < 3);
|
||||
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);
|
||||
return 0.0f;
|
||||
}
|
||||
// Return extents of the box.
|
||||
scalar extents(uint axis) const;
|
||||
|
||||
/// Add a point to this box.
|
||||
void addPointToBounds(Vector3::Arg p)
|
||||
{
|
||||
minCorner = min(minCorner, p);
|
||||
maxCorner = max(maxCorner, p);
|
||||
}
|
||||
// Add a point to this box.
|
||||
void addPointToBounds(const Vector3 & p);
|
||||
|
||||
/// Add a box to this box.
|
||||
void addBoxToBounds(const Box & b)
|
||||
{
|
||||
minCorner = min(minCorner, b.minCorner);
|
||||
maxCorner = max(maxCorner, b.maxCorner);
|
||||
}
|
||||
// Add a box to this box.
|
||||
void addBoxToBounds(const Box & b);
|
||||
|
||||
/// Translate box.
|
||||
void translate(Vector3::Arg v)
|
||||
{
|
||||
minCorner += v;
|
||||
maxCorner += v;
|
||||
}
|
||||
// Translate box.
|
||||
void translate(const Vector3 & v);
|
||||
|
||||
/// Scale the box.
|
||||
void scale(float s)
|
||||
{
|
||||
minCorner *= s;
|
||||
maxCorner *= s;
|
||||
}
|
||||
// Scale the box.
|
||||
void scale(float s);
|
||||
|
||||
// Expand the box by a fixed amount.
|
||||
void expand(float r) {
|
||||
minCorner -= Vector3(r,r,r);
|
||||
maxCorner += Vector3(r,r,r);
|
||||
}
|
||||
void expand(float r);
|
||||
|
||||
/// Get the area of the box.
|
||||
float area() const
|
||||
{
|
||||
const Vector3 d = extents();
|
||||
return 8.0f * (d.x*d.y + d.x*d.z + d.y*d.z);
|
||||
}
|
||||
// Get the area of the box.
|
||||
float area() const;
|
||||
|
||||
// Get the volume of the box.
|
||||
float volume() const;
|
||||
|
||||
/// Get the volume of the box.
|
||||
float volume() const
|
||||
{
|
||||
Vector3 d = extents();
|
||||
return 8.0f * (d.x * d.y * d.z);
|
||||
}
|
||||
// Return true if the box contains the given point.
|
||||
bool contains(const Vector3 & p) const;
|
||||
|
||||
/// Return true if the box contains the given point.
|
||||
bool contains(Vector3::Arg p) const
|
||||
{
|
||||
return
|
||||
minCorner.x < p.x && minCorner.y < p.y && minCorner.z < p.z &&
|
||||
maxCorner.x > p.x && maxCorner.y > p.y && maxCorner.z > p.z;
|
||||
}
|
||||
|
||||
/// Split the given box in 8 octants and assign the ith one to this box.
|
||||
void setOctant(const Box & box, Vector3::Arg center, int i)
|
||||
{
|
||||
minCorner = box.minCorner;
|
||||
maxCorner = box.maxCorner;
|
||||
|
||||
if (i & 4) minCorner.x = center.x;
|
||||
else maxCorner.x = center.x;
|
||||
if (i & 2) minCorner.y = center.y;
|
||||
else maxCorner.y = center.y;
|
||||
if (i & 1) minCorner.z = center.z;
|
||||
else maxCorner.z = center.z;
|
||||
}
|
||||
// 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);
|
||||
|
||||
friend Stream & operator<< (Stream & s, Box & box);
|
||||
|
||||
|
142
src/nvmath/Box.inl
Normal file
142
src/nvmath/Box.inl
Normal file
@ -0,0 +1,142 @@
|
||||
// This code is in the public domain -- castanyo@yahoo.es
|
||||
|
||||
#pragma once
|
||||
#ifndef NV_MATH_BOX_INL
|
||||
#define NV_MATH_BOX_INL
|
||||
|
||||
#include "Box.h"
|
||||
#include "Vector.inl"
|
||||
|
||||
#include <float.h> // FLT_MAX
|
||||
|
||||
namespace nv
|
||||
{
|
||||
// Default ctor.
|
||||
Box::Box() { };
|
||||
|
||||
// Copy ctor.
|
||||
Box::Box(const Box & b) : minCorner(b.minCorner), maxCorner(b.maxCorner) { }
|
||||
|
||||
// Init ctor.
|
||||
Box::Box(const Vector3 & mins, const Vector3 & maxs) : minCorner(mins), maxCorner(maxs) { }
|
||||
|
||||
// Assignment operator.
|
||||
Box & Box::operator=(const Box & b) { minCorner = b.minCorner; maxCorner = b.maxCorner; return *this; }
|
||||
|
||||
// Clear the bounds.
|
||||
void Box::clearBounds()
|
||||
{
|
||||
minCorner.set(FLT_MAX, FLT_MAX, FLT_MAX);
|
||||
maxCorner.set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
|
||||
}
|
||||
|
||||
// Build a cube centered on center and with edge = 2*dist
|
||||
void Box::cube(const Vector3 & center, float dist)
|
||||
{
|
||||
setCenterExtents(center, Vector3(dist, dist, dist));
|
||||
}
|
||||
|
||||
// Build a box, given center and extents.
|
||||
void Box::setCenterExtents(const Vector3 & center, const Vector3 & extents)
|
||||
{
|
||||
minCorner = center - extents;
|
||||
maxCorner = center + extents;
|
||||
}
|
||||
|
||||
// Get box center.
|
||||
Vector3 Box::center() const
|
||||
{
|
||||
return (minCorner + maxCorner) * 0.5f;
|
||||
}
|
||||
|
||||
// Return extents of the box.
|
||||
Vector3 Box::extents() const
|
||||
{
|
||||
return (maxCorner - minCorner) * 0.5f;
|
||||
}
|
||||
|
||||
// Return extents of the box.
|
||||
scalar Box::extents(uint axis) const
|
||||
{
|
||||
nvDebugCheck(axis < 3);
|
||||
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);
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
// Add a point to this box.
|
||||
void Box::addPointToBounds(const Vector3 & p)
|
||||
{
|
||||
minCorner = min(minCorner, p);
|
||||
maxCorner = max(maxCorner, p);
|
||||
}
|
||||
|
||||
// Add a box to this box.
|
||||
void Box::addBoxToBounds(const Box & b)
|
||||
{
|
||||
minCorner = min(minCorner, b.minCorner);
|
||||
maxCorner = max(maxCorner, b.maxCorner);
|
||||
}
|
||||
|
||||
// Translate box.
|
||||
void Box::translate(const Vector3 & v)
|
||||
{
|
||||
minCorner += v;
|
||||
maxCorner += v;
|
||||
}
|
||||
|
||||
// Scale the box.
|
||||
void Box::scale(float s)
|
||||
{
|
||||
minCorner *= s;
|
||||
maxCorner *= s;
|
||||
}
|
||||
|
||||
// Expand the box by a fixed amount.
|
||||
void Box::expand(float r) {
|
||||
minCorner -= Vector3(r,r,r);
|
||||
maxCorner += Vector3(r,r,r);
|
||||
}
|
||||
|
||||
// Get the area of the box.
|
||||
float Box::area() const
|
||||
{
|
||||
const Vector3 d = extents();
|
||||
return 8.0f * (d.x*d.y + d.x*d.z + d.y*d.z);
|
||||
}
|
||||
|
||||
// Get the volume of the box.
|
||||
float Box::volume() const
|
||||
{
|
||||
Vector3 d = extents();
|
||||
return 8.0f * (d.x * d.y * d.z);
|
||||
}
|
||||
|
||||
// Return true if the box contains the given point.
|
||||
bool Box::contains(const Vector3 & p) const
|
||||
{
|
||||
return
|
||||
minCorner.x < p.x && minCorner.y < p.y && minCorner.z < p.z &&
|
||||
maxCorner.x > p.x && maxCorner.y > p.y && maxCorner.z > p.z;
|
||||
}
|
||||
|
||||
// Split the given box in 8 octants and assign the ith one to this box.
|
||||
void Box::setOctant(const Box & box, const Vector3 & center, int i)
|
||||
{
|
||||
minCorner = box.minCorner;
|
||||
maxCorner = box.maxCorner;
|
||||
|
||||
if (i & 4) minCorner.x = center.x;
|
||||
else maxCorner.x = center.x;
|
||||
if (i & 2) minCorner.y = center.y;
|
||||
else maxCorner.y = center.y;
|
||||
if (i & 1) minCorner.z = center.z;
|
||||
else maxCorner.z = center.z;
|
||||
}
|
||||
|
||||
} // nv namespace
|
||||
|
||||
|
||||
#endif // NV_MATH_BOX_INL
|
4
src/nvmath/Color.cpp
Normal file
4
src/nvmath/Color.cpp
Normal file
@ -0,0 +1,4 @@
|
||||
// This code is in the public domain -- castanyo@yahoo.es
|
||||
|
||||
#include "Color.h"
|
||||
#include "Color.inl"
|
@ -4,8 +4,7 @@
|
||||
#ifndef NV_MATH_COLOR_H
|
||||
#define NV_MATH_COLOR_H
|
||||
|
||||
#include "nvcore/Debug.h"
|
||||
#include "nvmath/Vector.h"
|
||||
#include "nvmath.h"
|
||||
|
||||
namespace nv
|
||||
{
|
||||
@ -119,83 +118,6 @@ 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));
|
||||
}
|
||||
|
||||
/// Clamp without allowing the hue to change.
|
||||
inline Vector3 colorNormalize(Vector3::Arg c)
|
||||
{
|
||||
float scale = 1.0f;
|
||||
if (c.x > scale) scale = c.x;
|
||||
if (c.y > scale) scale = c.y;
|
||||
if (c.z > scale) scale = c.z;
|
||||
return c / scale;
|
||||
}
|
||||
|
||||
/// Convert Color32 to Color16.
|
||||
inline Color16 toColor16(Color32 c)
|
||||
{
|
||||
Color16 color;
|
||||
// rrrrrggggggbbbbb
|
||||
// rrrrr000gggggg00bbbbb000
|
||||
// color.u = (c.u >> 3) & 0x1F;
|
||||
// color.u |= (c.u >> 5) & 0x7E0;
|
||||
// color.u |= (c.u >> 8) & 0xF800;
|
||||
|
||||
color.r = c.r >> 3;
|
||||
color.g = c.g >> 2;
|
||||
color.b = c.b >> 3;
|
||||
return color;
|
||||
}
|
||||
|
||||
|
||||
/// Promote 16 bit color to 32 bit using regular bit expansion.
|
||||
inline Color32 toColor32(Color16 c)
|
||||
{
|
||||
Color32 color;
|
||||
// c.u = ((col0.u << 3) & 0xf8) | ((col0.u << 5) & 0xfc00) | ((col0.u << 8) & 0xf80000);
|
||||
// c.u |= (c.u >> 5) & 0x070007;
|
||||
// c.u |= (c.u >> 6) & 0x000300;
|
||||
|
||||
color.b = (c.b << 3) | (c.b >> 2);
|
||||
color.g = (c.g << 2) | (c.g >> 4);
|
||||
color.r = (c.r << 3) | (c.r >> 2);
|
||||
color.a = 0xFF;
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
inline Vector4 toVector4(Color32 c)
|
||||
{
|
||||
const float scale = 1.0f / 255.0f;
|
||||
return Vector4(c.r * scale, c.g * scale, c.b * scale, c.a * scale);
|
||||
}
|
||||
|
||||
|
||||
inline float perceptualColorDistance(Vector3::Arg c0, Vector3::Arg c1)
|
||||
{
|
||||
float rmean = (c0.x + c1.x) * 0.5f;
|
||||
float r = c1.x - c0.x;
|
||||
float g = c1.y - c0.y;
|
||||
float b = c1.z - c0.z;
|
||||
return sqrtf((2 + rmean)*r*r + 4*g*g + (3 - rmean)*b*b);
|
||||
}
|
||||
|
||||
} // nv namespace
|
||||
|
||||
#endif // NV_MATH_COLOR_H
|
||||
|
92
src/nvmath/Color.inl
Normal file
92
src/nvmath/Color.inl
Normal file
@ -0,0 +1,92 @@
|
||||
// This code is in the public domain -- castanyo@yahoo.es
|
||||
|
||||
#pragma once
|
||||
#ifndef NV_MATH_COLOR_INL
|
||||
#define NV_MATH_COLOR_INL
|
||||
|
||||
#include "Color.h"
|
||||
#include "Vector.inl"
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
/// Clamp without allowing the hue to change.
|
||||
inline Vector3 colorNormalize(Vector3::Arg c)
|
||||
{
|
||||
float scale = 1.0f;
|
||||
if (c.x > scale) scale = c.x;
|
||||
if (c.y > scale) scale = c.y;
|
||||
if (c.z > scale) scale = c.z;
|
||||
return c / scale;
|
||||
}
|
||||
|
||||
/// Convert Color32 to Color16.
|
||||
inline Color16 toColor16(Color32 c)
|
||||
{
|
||||
Color16 color;
|
||||
// rrrrrggggggbbbbb
|
||||
// rrrrr000gggggg00bbbbb000
|
||||
// color.u = (c.u >> 3) & 0x1F;
|
||||
// color.u |= (c.u >> 5) & 0x7E0;
|
||||
// color.u |= (c.u >> 8) & 0xF800;
|
||||
|
||||
color.r = c.r >> 3;
|
||||
color.g = c.g >> 2;
|
||||
color.b = c.b >> 3;
|
||||
return color;
|
||||
}
|
||||
|
||||
|
||||
/// Promote 16 bit color to 32 bit using regular bit expansion.
|
||||
inline Color32 toColor32(Color16 c)
|
||||
{
|
||||
Color32 color;
|
||||
// c.u = ((col0.u << 3) & 0xf8) | ((col0.u << 5) & 0xfc00) | ((col0.u << 8) & 0xf80000);
|
||||
// c.u |= (c.u >> 5) & 0x070007;
|
||||
// c.u |= (c.u >> 6) & 0x000300;
|
||||
|
||||
color.b = (c.b << 3) | (c.b >> 2);
|
||||
color.g = (c.g << 2) | (c.g >> 4);
|
||||
color.r = (c.r << 3) | (c.r >> 2);
|
||||
color.a = 0xFF;
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
inline Vector4 toVector4(Color32 c)
|
||||
{
|
||||
const float scale = 1.0f / 255.0f;
|
||||
return Vector4(c.r * scale, c.g * scale, c.b * scale, c.a * scale);
|
||||
}
|
||||
|
||||
|
||||
inline float perceptualColorDistance(Vector3::Arg c0, Vector3::Arg c1)
|
||||
{
|
||||
float rmean = (c0.x + c1.x) * 0.5f;
|
||||
float r = c1.x - c0.x;
|
||||
float g = c1.y - c0.y;
|
||||
float b = c1.z - c0.z;
|
||||
return sqrtf((2 + rmean)*r*r + 4*g*g + (3 - rmean)*b*b);
|
||||
}
|
||||
|
||||
} // nv namespace
|
||||
|
||||
#endif // NV_MATH_COLOR_INL
|
@ -1,6 +1,9 @@
|
||||
// This code is in the public domain -- Ignacio Castaño <castano@gmail.com>
|
||||
|
||||
#include "Fitting.h"
|
||||
#include "Vector.inl"
|
||||
#include "Plane.inl"
|
||||
|
||||
#include "nvcore/Utils.h" // max, swap
|
||||
|
||||
#include <float.h> // FLT_MAX
|
||||
|
@ -4,27 +4,28 @@
|
||||
#ifndef NV_MATH_FITTING_H
|
||||
#define NV_MATH_FITTING_H
|
||||
|
||||
#include "nvmath/nvmath.h"
|
||||
#include "nvmath/Vector.h"
|
||||
#include "nvmath/Plane.h"
|
||||
#include "nvmath.h"
|
||||
|
||||
namespace nv
|
||||
{
|
||||
class Vector3;
|
||||
class Plane;
|
||||
|
||||
namespace Fit
|
||||
{
|
||||
Vector3 computeCentroid(int n, const Vector3 * points);
|
||||
Vector3 computeCentroid(int n, const Vector3 * points, const float * weights, Vector3::Arg metric);
|
||||
Vector3 computeCentroid(int n, const Vector3 * points, const float * weights, const Vector3 & metric);
|
||||
|
||||
Vector3 computeCovariance(int n, const Vector3 * points, float * covariance);
|
||||
Vector3 computeCovariance(int n, const Vector3 * points, const float * weights, Vector3::Arg metric, float * covariance);
|
||||
Vector3 computeCovariance(int n, const Vector3 * points, const float * weights, const Vector3 & metric, float * covariance);
|
||||
|
||||
Vector3 computePrincipalComponent(int n, const Vector3 * points);
|
||||
Vector3 computePrincipalComponent(int n, const Vector3 * points, const float * weights, Vector3::Arg metric);
|
||||
Vector3 computePrincipalComponent(int n, const Vector3 * points, const float * weights, const Vector3 & metric);
|
||||
|
||||
Plane bestPlane(int n, const Vector3 * points);
|
||||
|
||||
// Returns number of clusters [1-4].
|
||||
int compute4Means(int n, const Vector3 * points, const float * weights, Vector3::Arg metric, Vector3 * cluster);
|
||||
int compute4Means(int n, const Vector3 * points, const float * weights, const Vector3 & metric, Vector3 * cluster);
|
||||
}
|
||||
|
||||
} // nv namespace
|
||||
|
@ -1,6 +1,7 @@
|
||||
// This code is in the public domain -- castanyo@yahoo.es
|
||||
|
||||
#include "Plane.h"
|
||||
#include "Plane.inl"
|
||||
#include "Matrix.h"
|
||||
|
||||
namespace nv
|
||||
|
@ -11,7 +11,6 @@ namespace nv
|
||||
{
|
||||
class Matrix;
|
||||
|
||||
|
||||
class NVMATH_CLASS Plane
|
||||
{
|
||||
public:
|
||||
@ -37,40 +36,6 @@ namespace nv
|
||||
Vector4 p;
|
||||
};
|
||||
|
||||
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 const Plane & Plane::operator=(Plane::Arg v) { p = v.p; return *this; }
|
||||
|
||||
inline Vector3 Plane::vector() const { return p.xyz(); }
|
||||
inline scalar Plane::offset() const { return p.w; }
|
||||
|
||||
inline const Vector4 & Plane::asVector() const { return p; }
|
||||
inline Vector4 & Plane::asVector() { return p; }
|
||||
|
||||
// Normalize plane.
|
||||
inline Plane normalize(Plane::Arg 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);
|
||||
}
|
||||
|
||||
// Get the signed distance from the given point to this plane.
|
||||
inline float distance(Plane::Arg plane, Vector3::Arg point)
|
||||
{
|
||||
return dot(plane.vector(), point) - plane.offset();
|
||||
}
|
||||
|
||||
inline void Plane::operator*=(scalar s)
|
||||
{
|
||||
scale(p, s);
|
||||
}
|
||||
|
||||
Plane transformPlane(const Matrix&, Plane::Arg);
|
||||
|
||||
Vector3 planeIntersection(Plane::Arg a, Plane::Arg b, Plane::Arg c);
|
||||
|
48
src/nvmath/Plane.inl
Normal file
48
src/nvmath/Plane.inl
Normal file
@ -0,0 +1,48 @@
|
||||
// This code is in the public domain -- Ignacio Castaño <castano@gmail.com>
|
||||
|
||||
#pragma once
|
||||
#ifndef NV_MATH_PLANE_INL
|
||||
#define NV_MATH_PLANE_INL
|
||||
|
||||
#include "Plane.h"
|
||||
#include "Vector.inl"
|
||||
|
||||
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 const Plane & Plane::operator=(Plane::Arg v) { p = v.p; return *this; }
|
||||
|
||||
inline Vector3 Plane::vector() const { return p.xyz(); }
|
||||
inline scalar Plane::offset() const { return p.w; }
|
||||
|
||||
inline const Vector4 & Plane::asVector() const { return p; }
|
||||
inline Vector4 & Plane::asVector() { return p; }
|
||||
|
||||
// Normalize plane.
|
||||
inline Plane normalize(Plane::Arg 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);
|
||||
}
|
||||
|
||||
// Get the signed distance from the given point to this plane.
|
||||
inline float distance(Plane::Arg plane, Vector3::Arg point)
|
||||
{
|
||||
return dot(plane.vector(), point) - plane.offset();
|
||||
}
|
||||
|
||||
inline void Plane::operator*=(scalar s)
|
||||
{
|
||||
scale(p, s);
|
||||
}
|
||||
|
||||
} // nv namespace
|
||||
|
||||
#endif // NV_MATH_PLANE_H
|
4
src/nvmath/Vector.cpp
Normal file
4
src/nvmath/Vector.cpp
Normal file
@ -0,0 +1,4 @@
|
||||
// This code is in the public domain -- castanyo@yahoo.es
|
||||
|
||||
#include "Vector.h"
|
||||
#include "Vector.inl"
|
@ -5,7 +5,6 @@
|
||||
#define NV_MATH_VECTOR_H
|
||||
|
||||
#include "nvmath.h"
|
||||
#include "nvcore/Utils.h" // min, max
|
||||
|
||||
namespace nv
|
||||
{
|
||||
@ -136,702 +135,6 @@ namespace nv
|
||||
template <typename T> T to(Vector4::Arg v) { return T(v.x, v.y, v.z, v.w); }
|
||||
|
||||
|
||||
|
||||
// Vector2
|
||||
|
||||
inline Vector2::Vector2() {}
|
||||
inline Vector2::Vector2(scalar f) : x(f), y(f) {}
|
||||
inline Vector2::Vector2(scalar x, scalar y) : x(x), y(y) {}
|
||||
inline Vector2::Vector2(Vector2::Arg v) : x(v.x), y(v.y) {}
|
||||
|
||||
inline const Vector2 & Vector2::operator=(Vector2::Arg v)
|
||||
{
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline const scalar * Vector2::ptr() const
|
||||
{
|
||||
return &x;
|
||||
}
|
||||
|
||||
inline void Vector2::set(scalar x, scalar y)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
inline Vector2 Vector2::operator-() const
|
||||
{
|
||||
return Vector2(-x, -y);
|
||||
}
|
||||
|
||||
inline void Vector2::operator+=(Vector2::Arg v)
|
||||
{
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
}
|
||||
|
||||
inline void Vector2::operator-=(Vector2::Arg v)
|
||||
{
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
}
|
||||
|
||||
inline void Vector2::operator*=(scalar s)
|
||||
{
|
||||
x *= s;
|
||||
y *= s;
|
||||
}
|
||||
|
||||
inline void Vector2::operator*=(Vector2::Arg v)
|
||||
{
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
}
|
||||
|
||||
inline bool operator==(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return a.x == b.x && a.y == b.y;
|
||||
}
|
||||
inline bool operator!=(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return a.x != b.x || a.y != b.y;
|
||||
}
|
||||
|
||||
|
||||
// Vector3
|
||||
|
||||
inline Vector3::Vector3() {}
|
||||
inline Vector3::Vector3(scalar f) : x(f), y(f), z(f) {}
|
||||
inline Vector3::Vector3(scalar x, scalar y, scalar z) : x(x), y(y), z(z) {}
|
||||
inline Vector3::Vector3(Vector2::Arg v, scalar z) : x(v.x), y(v.y), z(z) {}
|
||||
inline Vector3::Vector3(Vector3::Arg v) : x(v.x), y(v.y), z(v.z) {}
|
||||
|
||||
inline const Vector3 & Vector3::operator=(Vector3::Arg v)
|
||||
{
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
z = v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline Vector2 Vector3::xy() const
|
||||
{
|
||||
return Vector2(x, y);
|
||||
}
|
||||
|
||||
inline const scalar * Vector3::ptr() const
|
||||
{
|
||||
return &x;
|
||||
}
|
||||
|
||||
inline void Vector3::set(scalar x, scalar y, scalar z)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
inline Vector3 Vector3::operator-() const
|
||||
{
|
||||
return Vector3(-x, -y, -z);
|
||||
}
|
||||
|
||||
inline void Vector3::operator+=(Vector3::Arg v)
|
||||
{
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
z += v.z;
|
||||
}
|
||||
|
||||
inline void Vector3::operator-=(Vector3::Arg v)
|
||||
{
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
z -= v.z;
|
||||
}
|
||||
|
||||
inline void Vector3::operator*=(scalar s)
|
||||
{
|
||||
x *= s;
|
||||
y *= s;
|
||||
z *= s;
|
||||
}
|
||||
|
||||
inline void Vector3::operator/=(scalar s)
|
||||
{
|
||||
float is = 1.0f / s;
|
||||
x *= is;
|
||||
y *= is;
|
||||
z *= is;
|
||||
}
|
||||
|
||||
inline void Vector3::operator*=(Vector3::Arg v)
|
||||
{
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
z *= v.z;
|
||||
}
|
||||
|
||||
inline bool operator==(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return a.x == b.x && a.y == b.y && a.z == b.z;
|
||||
}
|
||||
inline bool operator!=(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return a.x != b.x || a.y != b.y || a.z != b.z;
|
||||
}
|
||||
|
||||
|
||||
// Vector4
|
||||
|
||||
inline Vector4::Vector4() {}
|
||||
inline Vector4::Vector4(scalar f) : x(f), y(f), z(f), w(f) {}
|
||||
inline Vector4::Vector4(scalar x, scalar y, scalar z, scalar w) : x(x), y(y), z(z), w(w) {}
|
||||
inline Vector4::Vector4(Vector2::Arg v, scalar z, scalar w) : x(v.x), y(v.y), z(z), w(w) {}
|
||||
inline Vector4::Vector4(Vector2::Arg v, Vector2::Arg u) : x(v.x), y(v.y), z(u.x), w(u.y) {}
|
||||
inline Vector4::Vector4(Vector3::Arg v, scalar w) : x(v.x), y(v.y), z(v.z), w(w) {}
|
||||
inline Vector4::Vector4(Vector4::Arg v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
|
||||
|
||||
inline const Vector4 & Vector4::operator=(const Vector4 & v)
|
||||
{
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
z = v.z;
|
||||
w = v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vector2 Vector4::xy() const
|
||||
{
|
||||
return Vector2(x, y);
|
||||
}
|
||||
|
||||
inline Vector2 Vector4::zw() const
|
||||
{
|
||||
return Vector2(z, w);
|
||||
}
|
||||
|
||||
inline Vector3 Vector4::xyz() const
|
||||
{
|
||||
return Vector3(x, y, z);
|
||||
}
|
||||
|
||||
inline const scalar * Vector4::ptr() const
|
||||
{
|
||||
return &x;
|
||||
}
|
||||
|
||||
inline void Vector4::set(scalar x, scalar y, scalar z, scalar w)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
this->w = w;
|
||||
}
|
||||
|
||||
inline Vector4 Vector4::operator-() const
|
||||
{
|
||||
return Vector4(-x, -y, -z, -w);
|
||||
}
|
||||
|
||||
inline void Vector4::operator+=(Vector4::Arg v)
|
||||
{
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
z += v.z;
|
||||
w += v.w;
|
||||
}
|
||||
|
||||
inline void Vector4::operator-=(Vector4::Arg v)
|
||||
{
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
z -= v.z;
|
||||
w -= v.w;
|
||||
}
|
||||
|
||||
inline void Vector4::operator*=(scalar s)
|
||||
{
|
||||
x *= s;
|
||||
y *= s;
|
||||
z *= s;
|
||||
w *= s;
|
||||
}
|
||||
|
||||
inline void Vector4::operator*=(Vector4::Arg v)
|
||||
{
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
z *= v.z;
|
||||
w *= v.w;
|
||||
}
|
||||
|
||||
inline bool operator==(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
|
||||
}
|
||||
inline bool operator!=(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return a.x != b.x || a.y != b.y || a.z != b.z || a.w != b.w;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Functions
|
||||
|
||||
|
||||
// Vector2
|
||||
|
||||
inline Vector2 add(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return Vector2(a.x + b.x, a.y + b.y);
|
||||
}
|
||||
inline Vector2 operator+(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return add(a, b);
|
||||
}
|
||||
|
||||
inline Vector2 sub(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return Vector2(a.x - b.x, a.y - b.y);
|
||||
}
|
||||
inline Vector2 operator-(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return sub(a, b);
|
||||
}
|
||||
|
||||
inline Vector2 scale(Vector2::Arg v, scalar s)
|
||||
{
|
||||
return Vector2(v.x * s, v.y * s);
|
||||
}
|
||||
|
||||
inline Vector2 scale(Vector2::Arg v, Vector2::Arg s)
|
||||
{
|
||||
return Vector2(v.x * s.x, v.y * s.y);
|
||||
}
|
||||
|
||||
inline Vector2 operator*(Vector2::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector2 operator*(Vector2::Arg v1, Vector2::Arg v2)
|
||||
{
|
||||
return Vector2(v1.x*v2.x, v1.y*v2.y);
|
||||
}
|
||||
|
||||
inline Vector2 operator*(scalar s, Vector2::Arg v)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector2 operator/(Vector2::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, 1.0f/s);
|
||||
}
|
||||
|
||||
inline scalar dot(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y;
|
||||
}
|
||||
|
||||
inline scalar lengthSquared(Vector2::Arg v)
|
||||
{
|
||||
return v.x * v.x + v.y * v.y;
|
||||
}
|
||||
|
||||
inline scalar length(Vector2::Arg v)
|
||||
{
|
||||
return sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline scalar inverseLength(Vector2::Arg v)
|
||||
{
|
||||
return 1.0f / sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline bool isNormalized(Vector2::Arg v, float epsilon = NV_NORMAL_EPSILON)
|
||||
{
|
||||
return equal(length(v), 1, epsilon);
|
||||
}
|
||||
|
||||
inline Vector2 normalize(Vector2::Arg v, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
nvDebugCheck(!isZero(l, epsilon));
|
||||
Vector2 n = scale(v, 1.0f / l);
|
||||
nvDebugCheck(isNormalized(n));
|
||||
return n;
|
||||
}
|
||||
|
||||
inline Vector2 normalizeSafe(Vector2::Arg v, Vector2::Arg fallback, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
if (isZero(l, epsilon)) {
|
||||
return fallback;
|
||||
}
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
// Safe, branchless normalization from Andy Firth. All error checking ommitted.
|
||||
// http://altdevblogaday.com/2011/08/21/practical-flt-point-tricks/
|
||||
inline Vector2 normalizeFast(Vector2::Arg v)
|
||||
{
|
||||
const float very_small_float = 1.0e-037f;
|
||||
float l = very_small_float + length(v);
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
inline bool equal(Vector2::Arg v1, Vector2::Arg v2, float epsilon = NV_EPSILON)
|
||||
{
|
||||
return equal(v1.x, v2.x, epsilon) && equal(v1.y, v2.y, epsilon);
|
||||
}
|
||||
|
||||
inline Vector2 min(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return Vector2(min(a.x, b.x), min(a.y, b.y));
|
||||
}
|
||||
|
||||
inline Vector2 max(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return Vector2(max(a.x, b.x), max(a.y, b.y));
|
||||
}
|
||||
|
||||
inline bool isValid(Vector2::Arg v)
|
||||
{
|
||||
return isFinite(v.x) && isFinite(v.y);
|
||||
}
|
||||
|
||||
inline Vector2 validate(Vector2::Arg v, Vector2::Arg fallback = Vector2(0.0f))
|
||||
{
|
||||
if (!isValid(v)) return fallback;
|
||||
Vector2 vf = v;
|
||||
nv::floatCleanup(vf.component, 2);
|
||||
return vf;
|
||||
}
|
||||
|
||||
inline float triangleArea(Vector2::Arg a, Vector2::Arg b, Vector2::Arg c)
|
||||
{
|
||||
Vector2 v0 = a - c;
|
||||
Vector2 v1 = b - c;
|
||||
|
||||
return (v0.x * v1.y - v0.y * v1.x);
|
||||
}
|
||||
|
||||
|
||||
// Vector3
|
||||
|
||||
inline Vector3 add(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
|
||||
}
|
||||
inline Vector3 add(Vector3::Arg a, float b)
|
||||
{
|
||||
return Vector3(a.x + b, a.y + b, a.z + b);
|
||||
}
|
||||
inline Vector3 operator+(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return add(a, b);
|
||||
}
|
||||
inline Vector3 operator+(Vector3::Arg a, float b)
|
||||
{
|
||||
return add(a, b);
|
||||
}
|
||||
|
||||
inline Vector3 sub(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
|
||||
}
|
||||
inline Vector3 sub(Vector3::Arg a, float b)
|
||||
{
|
||||
return Vector3(a.x - b, a.y - b, a.z - b);
|
||||
}
|
||||
inline Vector3 operator-(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return sub(a, b);
|
||||
}
|
||||
inline Vector3 operator-(Vector3::Arg a, float b)
|
||||
{
|
||||
return sub(a, b);
|
||||
}
|
||||
|
||||
inline Vector3 cross(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
|
||||
}
|
||||
|
||||
inline Vector3 scale(Vector3::Arg v, scalar s)
|
||||
{
|
||||
return Vector3(v.x * s, v.y * s, v.z * s);
|
||||
}
|
||||
|
||||
inline Vector3 scale(Vector3::Arg v, Vector3::Arg s)
|
||||
{
|
||||
return Vector3(v.x * s.x, v.y * s.y, v.z * s.z);
|
||||
}
|
||||
|
||||
inline Vector3 operator*(Vector3::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector3 operator*(scalar s, Vector3::Arg v)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector3 operator*(Vector3::Arg v, Vector3::Arg s)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector3 operator/(Vector3::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, 1.0f/s);
|
||||
}
|
||||
|
||||
/*inline Vector3 add_scaled(Vector3::Arg a, Vector3::Arg b, scalar s)
|
||||
{
|
||||
return Vector3(a.x + b.x * s, a.y + b.y * s, a.z + b.z * s);
|
||||
}*/
|
||||
|
||||
inline Vector3 lerp(Vector3::Arg v1, Vector3::Arg v2, scalar t)
|
||||
{
|
||||
const scalar s = 1.0f - t;
|
||||
return Vector3(v1.x * s + t * v2.x, v1.y * s + t * v2.y, v1.z * s + t * v2.z);
|
||||
}
|
||||
|
||||
inline scalar dot(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
inline scalar lengthSquared(Vector3::Arg v)
|
||||
{
|
||||
return v.x * v.x + v.y * v.y + v.z * v.z;
|
||||
}
|
||||
|
||||
inline scalar length(Vector3::Arg v)
|
||||
{
|
||||
return sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline scalar inverseLength(Vector3::Arg v)
|
||||
{
|
||||
return 1.0f / sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline bool isNormalized(Vector3::Arg v, float epsilon = NV_NORMAL_EPSILON)
|
||||
{
|
||||
return equal(length(v), 1, epsilon);
|
||||
}
|
||||
|
||||
inline Vector3 normalize(Vector3::Arg v, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
nvDebugCheck(!isZero(l, epsilon));
|
||||
Vector3 n = scale(v, 1.0f / l);
|
||||
nvDebugCheck(isNormalized(n));
|
||||
return n;
|
||||
}
|
||||
|
||||
inline Vector3 normalizeSafe(Vector3::Arg v, Vector3::Arg fallback, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
if (isZero(l, epsilon)) {
|
||||
return fallback;
|
||||
}
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
// Safe, branchless normalization from Andy Firth. All error checking ommitted.
|
||||
// http://altdevblogaday.com/2011/08/21/practical-flt-point-tricks/
|
||||
inline Vector3 normalizeFast(Vector3::Arg v)
|
||||
{
|
||||
const float very_small_float = 1.0e-037f;
|
||||
float l = very_small_float + length(v);
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
inline bool equal(Vector3::Arg v1, Vector3::Arg v2, float epsilon = NV_EPSILON)
|
||||
{
|
||||
return equal(v1.x, v2.x, epsilon) && equal(v1.y, v2.y, epsilon) && equal(v1.z, v2.z, epsilon);
|
||||
}
|
||||
|
||||
inline Vector3 min(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return Vector3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
|
||||
}
|
||||
|
||||
inline Vector3 max(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return Vector3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
|
||||
}
|
||||
|
||||
inline Vector3 clamp(Vector3::Arg v, float min, float max)
|
||||
{
|
||||
return Vector3(clamp(v.x, min, max), clamp(v.y, min, max), clamp(v.z, min, max));
|
||||
}
|
||||
|
||||
inline Vector3 floor(Vector3::Arg v)
|
||||
{
|
||||
return Vector3(floorf(v.x), floorf(v.y), floorf(v.z));
|
||||
}
|
||||
|
||||
inline Vector3 ceil(Vector3::Arg v)
|
||||
{
|
||||
return Vector3(ceilf(v.x), ceilf(v.y), ceilf(v.z));
|
||||
}
|
||||
|
||||
inline bool isValid(Vector3::Arg v)
|
||||
{
|
||||
return isFinite(v.x) && isFinite(v.y) && isFinite(v.z);
|
||||
}
|
||||
|
||||
inline Vector3 validate(Vector3::Arg v, Vector3::Arg fallback = Vector3(0.0f))
|
||||
{
|
||||
if (!isValid(v)) return fallback;
|
||||
Vector3 vf = v;
|
||||
nv::floatCleanup(vf.component, 3);
|
||||
return vf;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Vector4
|
||||
|
||||
inline Vector4 add(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
|
||||
}
|
||||
inline Vector4 operator+(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return add(a, b);
|
||||
}
|
||||
|
||||
inline Vector4 sub(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
|
||||
}
|
||||
inline Vector4 operator-(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return sub(a, b);
|
||||
}
|
||||
|
||||
inline Vector4 scale(Vector4::Arg v, scalar s)
|
||||
{
|
||||
return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
|
||||
}
|
||||
|
||||
inline Vector4 scale(Vector4::Arg v, Vector4::Arg s)
|
||||
{
|
||||
return Vector4(v.x * s.x, v.y * s.y, v.z * s.z, v.w * s.w);
|
||||
}
|
||||
|
||||
inline Vector4 operator*(Vector4::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector4 operator*(scalar s, Vector4::Arg v)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector4 operator/(Vector4::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, 1.0f/s);
|
||||
}
|
||||
|
||||
inline Vector4 add_scaled(Vector4::Arg a, Vector4::Arg b, scalar s)
|
||||
{
|
||||
return Vector4(a.x + b.x * s, a.y + b.y * s, a.z + b.z * s, a.w + b.w * s);
|
||||
}
|
||||
|
||||
inline scalar dot(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
||||
}
|
||||
|
||||
inline scalar lengthSquared(Vector4::Arg v)
|
||||
{
|
||||
return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
|
||||
}
|
||||
|
||||
inline scalar length(Vector4::Arg v)
|
||||
{
|
||||
return sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline scalar inverseLength(Vector4::Arg v)
|
||||
{
|
||||
return 1.0f / sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline bool isNormalized(Vector4::Arg v, float epsilon = NV_NORMAL_EPSILON)
|
||||
{
|
||||
return equal(length(v), 1, epsilon);
|
||||
}
|
||||
|
||||
inline Vector4 normalize(Vector4::Arg v, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
nvDebugCheck(!isZero(l, epsilon));
|
||||
Vector4 n = scale(v, 1.0f / l);
|
||||
nvDebugCheck(isNormalized(n));
|
||||
return n;
|
||||
}
|
||||
|
||||
inline Vector4 normalizeSafe(Vector4::Arg v, Vector4::Arg fallback, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
if (isZero(l, epsilon)) {
|
||||
return fallback;
|
||||
}
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
// Safe, branchless normalization from Andy Firth. All error checking ommitted.
|
||||
// http://altdevblogaday.com/2011/08/21/practical-flt-point-tricks/
|
||||
inline Vector4 normalizeFast(Vector4::Arg v)
|
||||
{
|
||||
const float very_small_float = 1.0e-037f;
|
||||
float l = very_small_float + length(v);
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
inline bool equal(Vector4::Arg v1, Vector4::Arg v2, float epsilon = NV_EPSILON)
|
||||
{
|
||||
return equal(v1.x, v2.x, epsilon) && equal(v1.y, v2.y, epsilon) && equal(v1.z, v2.z, epsilon) && equal(v1.w, v2.w, epsilon);
|
||||
}
|
||||
|
||||
inline Vector4 min(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return Vector4(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
|
||||
}
|
||||
|
||||
inline Vector4 max(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return Vector4(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w));
|
||||
}
|
||||
|
||||
inline bool isValid(Vector4::Arg v)
|
||||
{
|
||||
return isFinite(v.x) && isFinite(v.y) && isFinite(v.z) && isFinite(v.w);
|
||||
}
|
||||
|
||||
inline Vector4 validate(Vector4::Arg v, Vector4::Arg fallback = Vector4(0.0f))
|
||||
{
|
||||
if (!isValid(v)) return fallback;
|
||||
Vector4 vf = v;
|
||||
nv::floatCleanup(vf.component, 4);
|
||||
return vf;
|
||||
}
|
||||
|
||||
} // nv namespace
|
||||
|
||||
#endif // NV_MATH_VECTOR_H
|
||||
|
717
src/nvmath/Vector.inl
Normal file
717
src/nvmath/Vector.inl
Normal file
@ -0,0 +1,717 @@
|
||||
// This code is in the public domain -- castanyo@yahoo.es
|
||||
|
||||
#pragma once
|
||||
#ifndef NV_MATH_VECTOR_INL
|
||||
#define NV_MATH_VECTOR_INL
|
||||
|
||||
#include "Vector.h"
|
||||
#include "nvcore/Utils.h" // min, max
|
||||
|
||||
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); }
|
||||
|
||||
// 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); }
|
||||
|
||||
// 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); }
|
||||
|
||||
|
||||
// Vector2
|
||||
inline Vector2::Vector2() {}
|
||||
inline Vector2::Vector2(scalar f) : x(f), y(f) {}
|
||||
inline Vector2::Vector2(scalar x, scalar y) : x(x), y(y) {}
|
||||
inline Vector2::Vector2(Vector2::Arg v) : x(v.x), y(v.y) {}
|
||||
|
||||
inline const Vector2 & Vector2::operator=(Vector2::Arg v)
|
||||
{
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline const scalar * Vector2::ptr() const
|
||||
{
|
||||
return &x;
|
||||
}
|
||||
|
||||
inline void Vector2::set(scalar x, scalar y)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
inline Vector2 Vector2::operator-() const
|
||||
{
|
||||
return Vector2(-x, -y);
|
||||
}
|
||||
|
||||
inline void Vector2::operator+=(Vector2::Arg v)
|
||||
{
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
}
|
||||
|
||||
inline void Vector2::operator-=(Vector2::Arg v)
|
||||
{
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
}
|
||||
|
||||
inline void Vector2::operator*=(scalar s)
|
||||
{
|
||||
x *= s;
|
||||
y *= s;
|
||||
}
|
||||
|
||||
inline void Vector2::operator*=(Vector2::Arg v)
|
||||
{
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
}
|
||||
|
||||
inline bool operator==(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return a.x == b.x && a.y == b.y;
|
||||
}
|
||||
inline bool operator!=(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return a.x != b.x || a.y != b.y;
|
||||
}
|
||||
|
||||
|
||||
// Vector3
|
||||
inline Vector3::Vector3() {}
|
||||
inline Vector3::Vector3(scalar f) : x(f), y(f), z(f) {}
|
||||
inline Vector3::Vector3(scalar x, scalar y, scalar z) : x(x), y(y), z(z) {}
|
||||
inline Vector3::Vector3(Vector2::Arg v, scalar z) : x(v.x), y(v.y), z(z) {}
|
||||
inline Vector3::Vector3(Vector3::Arg v) : x(v.x), y(v.y), z(v.z) {}
|
||||
|
||||
inline const Vector3 & Vector3::operator=(Vector3::Arg v)
|
||||
{
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
z = v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline Vector2 Vector3::xy() const
|
||||
{
|
||||
return Vector2(x, y);
|
||||
}
|
||||
|
||||
inline const scalar * Vector3::ptr() const
|
||||
{
|
||||
return &x;
|
||||
}
|
||||
|
||||
inline void Vector3::set(scalar x, scalar y, scalar z)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
inline Vector3 Vector3::operator-() const
|
||||
{
|
||||
return Vector3(-x, -y, -z);
|
||||
}
|
||||
|
||||
inline void Vector3::operator+=(Vector3::Arg v)
|
||||
{
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
z += v.z;
|
||||
}
|
||||
|
||||
inline void Vector3::operator-=(Vector3::Arg v)
|
||||
{
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
z -= v.z;
|
||||
}
|
||||
|
||||
inline void Vector3::operator*=(scalar s)
|
||||
{
|
||||
x *= s;
|
||||
y *= s;
|
||||
z *= s;
|
||||
}
|
||||
|
||||
inline void Vector3::operator/=(scalar s)
|
||||
{
|
||||
float is = 1.0f / s;
|
||||
x *= is;
|
||||
y *= is;
|
||||
z *= is;
|
||||
}
|
||||
|
||||
inline void Vector3::operator*=(Vector3::Arg v)
|
||||
{
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
z *= v.z;
|
||||
}
|
||||
|
||||
inline bool operator==(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return a.x == b.x && a.y == b.y && a.z == b.z;
|
||||
}
|
||||
inline bool operator!=(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return a.x != b.x || a.y != b.y || a.z != b.z;
|
||||
}
|
||||
|
||||
|
||||
// Vector4
|
||||
inline Vector4::Vector4() {}
|
||||
inline Vector4::Vector4(scalar f) : x(f), y(f), z(f), w(f) {}
|
||||
inline Vector4::Vector4(scalar x, scalar y, scalar z, scalar w) : x(x), y(y), z(z), w(w) {}
|
||||
inline Vector4::Vector4(Vector2::Arg v, scalar z, scalar w) : x(v.x), y(v.y), z(z), w(w) {}
|
||||
inline Vector4::Vector4(Vector2::Arg v, Vector2::Arg u) : x(v.x), y(v.y), z(u.x), w(u.y) {}
|
||||
inline Vector4::Vector4(Vector3::Arg v, scalar w) : x(v.x), y(v.y), z(v.z), w(w) {}
|
||||
inline Vector4::Vector4(Vector4::Arg v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
|
||||
|
||||
inline const Vector4 & Vector4::operator=(const Vector4 & v)
|
||||
{
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
z = v.z;
|
||||
w = v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vector2 Vector4::xy() const
|
||||
{
|
||||
return Vector2(x, y);
|
||||
}
|
||||
|
||||
inline Vector2 Vector4::zw() const
|
||||
{
|
||||
return Vector2(z, w);
|
||||
}
|
||||
|
||||
inline Vector3 Vector4::xyz() const
|
||||
{
|
||||
return Vector3(x, y, z);
|
||||
}
|
||||
|
||||
inline const scalar * Vector4::ptr() const
|
||||
{
|
||||
return &x;
|
||||
}
|
||||
|
||||
inline void Vector4::set(scalar x, scalar y, scalar z, scalar w)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
this->w = w;
|
||||
}
|
||||
|
||||
inline Vector4 Vector4::operator-() const
|
||||
{
|
||||
return Vector4(-x, -y, -z, -w);
|
||||
}
|
||||
|
||||
inline void Vector4::operator+=(Vector4::Arg v)
|
||||
{
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
z += v.z;
|
||||
w += v.w;
|
||||
}
|
||||
|
||||
inline void Vector4::operator-=(Vector4::Arg v)
|
||||
{
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
z -= v.z;
|
||||
w -= v.w;
|
||||
}
|
||||
|
||||
inline void Vector4::operator*=(scalar s)
|
||||
{
|
||||
x *= s;
|
||||
y *= s;
|
||||
z *= s;
|
||||
w *= s;
|
||||
}
|
||||
|
||||
inline void Vector4::operator*=(Vector4::Arg v)
|
||||
{
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
z *= v.z;
|
||||
w *= v.w;
|
||||
}
|
||||
|
||||
inline bool operator==(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
|
||||
}
|
||||
inline bool operator!=(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return a.x != b.x || a.y != b.y || a.z != b.z || a.w != b.w;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Functions
|
||||
|
||||
|
||||
// Vector2
|
||||
|
||||
inline Vector2 add(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return Vector2(a.x + b.x, a.y + b.y);
|
||||
}
|
||||
inline Vector2 operator+(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return add(a, b);
|
||||
}
|
||||
|
||||
inline Vector2 sub(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return Vector2(a.x - b.x, a.y - b.y);
|
||||
}
|
||||
inline Vector2 operator-(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return sub(a, b);
|
||||
}
|
||||
|
||||
inline Vector2 scale(Vector2::Arg v, scalar s)
|
||||
{
|
||||
return Vector2(v.x * s, v.y * s);
|
||||
}
|
||||
|
||||
inline Vector2 scale(Vector2::Arg v, Vector2::Arg s)
|
||||
{
|
||||
return Vector2(v.x * s.x, v.y * s.y);
|
||||
}
|
||||
|
||||
inline Vector2 operator*(Vector2::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector2 operator*(Vector2::Arg v1, Vector2::Arg v2)
|
||||
{
|
||||
return Vector2(v1.x*v2.x, v1.y*v2.y);
|
||||
}
|
||||
|
||||
inline Vector2 operator*(scalar s, Vector2::Arg v)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector2 operator/(Vector2::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, 1.0f/s);
|
||||
}
|
||||
|
||||
inline scalar dot(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y;
|
||||
}
|
||||
|
||||
inline scalar lengthSquared(Vector2::Arg v)
|
||||
{
|
||||
return v.x * v.x + v.y * v.y;
|
||||
}
|
||||
|
||||
inline scalar length(Vector2::Arg v)
|
||||
{
|
||||
return sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline scalar inverseLength(Vector2::Arg v)
|
||||
{
|
||||
return 1.0f / sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline bool isNormalized(Vector2::Arg v, float epsilon = NV_NORMAL_EPSILON)
|
||||
{
|
||||
return equal(length(v), 1, epsilon);
|
||||
}
|
||||
|
||||
inline Vector2 normalize(Vector2::Arg v, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
nvDebugCheck(!isZero(l, epsilon));
|
||||
Vector2 n = scale(v, 1.0f / l);
|
||||
nvDebugCheck(isNormalized(n));
|
||||
return n;
|
||||
}
|
||||
|
||||
inline Vector2 normalizeSafe(Vector2::Arg v, Vector2::Arg fallback, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
if (isZero(l, epsilon)) {
|
||||
return fallback;
|
||||
}
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
// Safe, branchless normalization from Andy Firth. All error checking ommitted.
|
||||
// http://altdevblogaday.com/2011/08/21/practical-flt-point-tricks/
|
||||
inline Vector2 normalizeFast(Vector2::Arg v)
|
||||
{
|
||||
const float very_small_float = 1.0e-037f;
|
||||
float l = very_small_float + length(v);
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
inline bool equal(Vector2::Arg v1, Vector2::Arg v2, float epsilon = NV_EPSILON)
|
||||
{
|
||||
return equal(v1.x, v2.x, epsilon) && equal(v1.y, v2.y, epsilon);
|
||||
}
|
||||
|
||||
inline Vector2 min(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return Vector2(min(a.x, b.x), min(a.y, b.y));
|
||||
}
|
||||
|
||||
inline Vector2 max(Vector2::Arg a, Vector2::Arg b)
|
||||
{
|
||||
return Vector2(max(a.x, b.x), max(a.y, b.y));
|
||||
}
|
||||
|
||||
inline bool isValid(Vector2::Arg v)
|
||||
{
|
||||
return isFinite(v.x) && isFinite(v.y);
|
||||
}
|
||||
|
||||
inline Vector2 validate(Vector2::Arg v, Vector2::Arg fallback = Vector2(0.0f))
|
||||
{
|
||||
if (!isValid(v)) return fallback;
|
||||
Vector2 vf = v;
|
||||
nv::floatCleanup(vf.component, 2);
|
||||
return vf;
|
||||
}
|
||||
|
||||
inline float triangleArea(Vector2::Arg a, Vector2::Arg b, Vector2::Arg c)
|
||||
{
|
||||
Vector2 v0 = a - c;
|
||||
Vector2 v1 = b - c;
|
||||
|
||||
return (v0.x * v1.y - v0.y * v1.x);
|
||||
}
|
||||
|
||||
|
||||
// Vector3
|
||||
|
||||
inline Vector3 add(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
|
||||
}
|
||||
inline Vector3 add(Vector3::Arg a, float b)
|
||||
{
|
||||
return Vector3(a.x + b, a.y + b, a.z + b);
|
||||
}
|
||||
inline Vector3 operator+(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return add(a, b);
|
||||
}
|
||||
inline Vector3 operator+(Vector3::Arg a, float b)
|
||||
{
|
||||
return add(a, b);
|
||||
}
|
||||
|
||||
inline Vector3 sub(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
|
||||
}
|
||||
inline Vector3 sub(Vector3::Arg a, float b)
|
||||
{
|
||||
return Vector3(a.x - b, a.y - b, a.z - b);
|
||||
}
|
||||
inline Vector3 operator-(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return sub(a, b);
|
||||
}
|
||||
inline Vector3 operator-(Vector3::Arg a, float b)
|
||||
{
|
||||
return sub(a, b);
|
||||
}
|
||||
|
||||
inline Vector3 cross(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
|
||||
}
|
||||
|
||||
inline Vector3 scale(Vector3::Arg v, scalar s)
|
||||
{
|
||||
return Vector3(v.x * s, v.y * s, v.z * s);
|
||||
}
|
||||
|
||||
inline Vector3 scale(Vector3::Arg v, Vector3::Arg s)
|
||||
{
|
||||
return Vector3(v.x * s.x, v.y * s.y, v.z * s.z);
|
||||
}
|
||||
|
||||
inline Vector3 operator*(Vector3::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector3 operator*(scalar s, Vector3::Arg v)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector3 operator*(Vector3::Arg v, Vector3::Arg s)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector3 operator/(Vector3::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, 1.0f/s);
|
||||
}
|
||||
|
||||
/*inline Vector3 add_scaled(Vector3::Arg a, Vector3::Arg b, scalar s)
|
||||
{
|
||||
return Vector3(a.x + b.x * s, a.y + b.y * s, a.z + b.z * s);
|
||||
}*/
|
||||
|
||||
inline Vector3 lerp(Vector3::Arg v1, Vector3::Arg v2, scalar t)
|
||||
{
|
||||
const scalar s = 1.0f - t;
|
||||
return Vector3(v1.x * s + t * v2.x, v1.y * s + t * v2.y, v1.z * s + t * v2.z);
|
||||
}
|
||||
|
||||
inline scalar dot(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
inline scalar lengthSquared(Vector3::Arg v)
|
||||
{
|
||||
return v.x * v.x + v.y * v.y + v.z * v.z;
|
||||
}
|
||||
|
||||
inline scalar length(Vector3::Arg v)
|
||||
{
|
||||
return sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline scalar inverseLength(Vector3::Arg v)
|
||||
{
|
||||
return 1.0f / sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline bool isNormalized(Vector3::Arg v, float epsilon = NV_NORMAL_EPSILON)
|
||||
{
|
||||
return equal(length(v), 1, epsilon);
|
||||
}
|
||||
|
||||
inline Vector3 normalize(Vector3::Arg v, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
nvDebugCheck(!isZero(l, epsilon));
|
||||
Vector3 n = scale(v, 1.0f / l);
|
||||
nvDebugCheck(isNormalized(n));
|
||||
return n;
|
||||
}
|
||||
|
||||
inline Vector3 normalizeSafe(Vector3::Arg v, Vector3::Arg fallback, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
if (isZero(l, epsilon)) {
|
||||
return fallback;
|
||||
}
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
// Safe, branchless normalization from Andy Firth. All error checking ommitted.
|
||||
// http://altdevblogaday.com/2011/08/21/practical-flt-point-tricks/
|
||||
inline Vector3 normalizeFast(Vector3::Arg v)
|
||||
{
|
||||
const float very_small_float = 1.0e-037f;
|
||||
float l = very_small_float + length(v);
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
inline bool equal(Vector3::Arg v1, Vector3::Arg v2, float epsilon = NV_EPSILON)
|
||||
{
|
||||
return equal(v1.x, v2.x, epsilon) && equal(v1.y, v2.y, epsilon) && equal(v1.z, v2.z, epsilon);
|
||||
}
|
||||
|
||||
inline Vector3 min(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return Vector3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
|
||||
}
|
||||
|
||||
inline Vector3 max(Vector3::Arg a, Vector3::Arg b)
|
||||
{
|
||||
return Vector3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
|
||||
}
|
||||
|
||||
inline Vector3 clamp(Vector3::Arg v, float min, float max)
|
||||
{
|
||||
return Vector3(clamp(v.x, min, max), clamp(v.y, min, max), clamp(v.z, min, max));
|
||||
}
|
||||
|
||||
inline Vector3 floor(Vector3::Arg v)
|
||||
{
|
||||
return Vector3(floorf(v.x), floorf(v.y), floorf(v.z));
|
||||
}
|
||||
|
||||
inline Vector3 ceil(Vector3::Arg v)
|
||||
{
|
||||
return Vector3(ceilf(v.x), ceilf(v.y), ceilf(v.z));
|
||||
}
|
||||
|
||||
inline bool isValid(Vector3::Arg v)
|
||||
{
|
||||
return isFinite(v.x) && isFinite(v.y) && isFinite(v.z);
|
||||
}
|
||||
|
||||
inline Vector3 validate(Vector3::Arg v, Vector3::Arg fallback = Vector3(0.0f))
|
||||
{
|
||||
if (!isValid(v)) return fallback;
|
||||
Vector3 vf = v;
|
||||
nv::floatCleanup(vf.component, 3);
|
||||
return vf;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Vector4
|
||||
|
||||
inline Vector4 add(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
|
||||
}
|
||||
inline Vector4 operator+(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return add(a, b);
|
||||
}
|
||||
|
||||
inline Vector4 sub(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
|
||||
}
|
||||
inline Vector4 operator-(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return sub(a, b);
|
||||
}
|
||||
|
||||
inline Vector4 scale(Vector4::Arg v, scalar s)
|
||||
{
|
||||
return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
|
||||
}
|
||||
|
||||
inline Vector4 scale(Vector4::Arg v, Vector4::Arg s)
|
||||
{
|
||||
return Vector4(v.x * s.x, v.y * s.y, v.z * s.z, v.w * s.w);
|
||||
}
|
||||
|
||||
inline Vector4 operator*(Vector4::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector4 operator*(scalar s, Vector4::Arg v)
|
||||
{
|
||||
return scale(v, s);
|
||||
}
|
||||
|
||||
inline Vector4 operator/(Vector4::Arg v, scalar s)
|
||||
{
|
||||
return scale(v, 1.0f/s);
|
||||
}
|
||||
|
||||
inline Vector4 add_scaled(Vector4::Arg a, Vector4::Arg b, scalar s)
|
||||
{
|
||||
return Vector4(a.x + b.x * s, a.y + b.y * s, a.z + b.z * s, a.w + b.w * s);
|
||||
}
|
||||
|
||||
inline scalar dot(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
||||
}
|
||||
|
||||
inline scalar lengthSquared(Vector4::Arg v)
|
||||
{
|
||||
return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
|
||||
}
|
||||
|
||||
inline scalar length(Vector4::Arg v)
|
||||
{
|
||||
return sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline scalar inverseLength(Vector4::Arg v)
|
||||
{
|
||||
return 1.0f / sqrtf(lengthSquared(v));
|
||||
}
|
||||
|
||||
inline bool isNormalized(Vector4::Arg v, float epsilon = NV_NORMAL_EPSILON)
|
||||
{
|
||||
return equal(length(v), 1, epsilon);
|
||||
}
|
||||
|
||||
inline Vector4 normalize(Vector4::Arg v, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
nvDebugCheck(!isZero(l, epsilon));
|
||||
Vector4 n = scale(v, 1.0f / l);
|
||||
nvDebugCheck(isNormalized(n));
|
||||
return n;
|
||||
}
|
||||
|
||||
inline Vector4 normalizeSafe(Vector4::Arg v, Vector4::Arg fallback, float epsilon = NV_EPSILON)
|
||||
{
|
||||
float l = length(v);
|
||||
if (isZero(l, epsilon)) {
|
||||
return fallback;
|
||||
}
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
// Safe, branchless normalization from Andy Firth. All error checking ommitted.
|
||||
// http://altdevblogaday.com/2011/08/21/practical-flt-point-tricks/
|
||||
inline Vector4 normalizeFast(Vector4::Arg v)
|
||||
{
|
||||
const float very_small_float = 1.0e-037f;
|
||||
float l = very_small_float + length(v);
|
||||
return scale(v, 1.0f / l);
|
||||
}
|
||||
|
||||
inline bool equal(Vector4::Arg v1, Vector4::Arg v2, float epsilon = NV_EPSILON)
|
||||
{
|
||||
return equal(v1.x, v2.x, epsilon) && equal(v1.y, v2.y, epsilon) && equal(v1.z, v2.z, epsilon) && equal(v1.w, v2.w, epsilon);
|
||||
}
|
||||
|
||||
inline Vector4 min(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return Vector4(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
|
||||
}
|
||||
|
||||
inline Vector4 max(Vector4::Arg a, Vector4::Arg b)
|
||||
{
|
||||
return Vector4(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w));
|
||||
}
|
||||
|
||||
inline bool isValid(Vector4::Arg v)
|
||||
{
|
||||
return isFinite(v.x) && isFinite(v.y) && isFinite(v.z) && isFinite(v.w);
|
||||
}
|
||||
|
||||
inline Vector4 validate(Vector4::Arg v, Vector4::Arg fallback = Vector4(0.0f))
|
||||
{
|
||||
if (!isValid(v)) return fallback;
|
||||
Vector4 vf = v;
|
||||
nv::floatCleanup(vf.component, 4);
|
||||
return vf;
|
||||
}
|
||||
|
||||
} // nv namespace
|
||||
|
||||
#endif // NV_MATH_VECTOR_INL
|
@ -26,6 +26,7 @@
|
||||
|
||||
#include "ClusterFit.h"
|
||||
#include "nvmath/Fitting.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
#include "nvimage/ColorBlock.h"
|
||||
|
||||
#include <float.h> // FLT_MAX
|
||||
|
@ -26,7 +26,7 @@
|
||||
|
||||
#include "nvimage/DirectDrawSurface.h"
|
||||
|
||||
#include "nvmath/Vector.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
|
||||
#include "nvcore/Array.h"
|
||||
#include "nvcore/StrLib.h"
|
||||
@ -474,7 +474,7 @@ Vector3 CubeSurface::Private::applyCosinePowerFilter(const Vector3 & filterDir,
|
||||
// Focal point in polar coordinates:
|
||||
Vector2 Fp = toPolar(F);
|
||||
nvCheck(Fp.y >= 0.0f); // top
|
||||
nvCheck(Fp.y <= PI/2); // horizon
|
||||
//nvCheck(Fp.y <= PI/2); // horizon @@ We should cull this earlier.
|
||||
|
||||
// If this is an ellipse:
|
||||
if (Fp.y + coneAngle < PI/2) {
|
||||
|
@ -22,15 +22,16 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#include "InputOptions.h"
|
||||
|
||||
#include "nvmath/Vector.inl"
|
||||
|
||||
#include "nvcore/Utils.h" // nextPowerOfTwo
|
||||
#include "nvcore/Memory.h"
|
||||
|
||||
#include <string.h> // memcpy
|
||||
|
||||
#include <nvcore/Utils.h> // nextPowerOfTwo
|
||||
#include <nvcore/Memory.h>
|
||||
|
||||
#include <nvmath/Color.h>
|
||||
|
||||
#include "nvtt.h"
|
||||
#include "InputOptions.h"
|
||||
|
||||
using namespace nv;
|
||||
using namespace nvtt;
|
||||
|
@ -22,17 +22,19 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#include <nvcore/Utils.h> // swap
|
||||
|
||||
#include <nvmath/Color.h>
|
||||
#include <nvmath/Fitting.h>
|
||||
|
||||
#include <nvimage/ColorBlock.h>
|
||||
#include <nvimage/BlockDXT.h>
|
||||
|
||||
#include "QuickCompressDXT.h"
|
||||
#include "OptimalCompressDXT.h"
|
||||
|
||||
#include "nvimage/ColorBlock.h"
|
||||
#include "nvimage/BlockDXT.h"
|
||||
|
||||
#include "nvmath/Color.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
#include "nvmath/Fitting.h"
|
||||
|
||||
#include "nvcore/Utils.h" // swap
|
||||
|
||||
|
||||
|
||||
using namespace nv;
|
||||
using namespace QuickCompress;
|
||||
|
@ -24,7 +24,7 @@
|
||||
|
||||
#include "Surface.h"
|
||||
|
||||
#include "nvmath/Vector.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
#include "nvmath/Matrix.h"
|
||||
#include "nvmath/Color.h"
|
||||
#include "nvmath/Half.h"
|
||||
|
@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations
|
||||
// Utility and common routines
|
||||
|
||||
#include "utils.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
#include <math.h>
|
||||
|
||||
using namespace nv;
|
||||
|
@ -18,7 +18,7 @@ See the License for the specific language governing permissions and limitations
|
||||
#include "zoh.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include "nvmath/Vector.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
#include "nvmath/Fitting.h"
|
||||
|
||||
#include <string.h> // strlen
|
||||
|
@ -43,6 +43,7 @@ See the License for the specific language governing permissions and limitations
|
||||
#include "utils.h"
|
||||
|
||||
#include "nvmath/Fitting.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
|
||||
#include <string.h> // strlen
|
||||
#include <float.h> // FLT_MAX
|
||||
|
@ -27,6 +27,7 @@
|
||||
|
||||
#include "nvcore/Debug.h"
|
||||
#include "nvmath/Color.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
#include "nvimage/Image.h"
|
||||
#include "nvimage/ColorBlock.h"
|
||||
#include "nvimage/BlockDXT.h"
|
||||
|
@ -40,9 +40,12 @@ int main(int argc, char *argv[])
|
||||
// Init context.
|
||||
nvtt::Context context;
|
||||
|
||||
const char * fileName = "envmap.dds";
|
||||
if (argc > 1) fileName = argv[1];
|
||||
|
||||
// Load cubemap.
|
||||
nvtt::CubeSurface envmap;
|
||||
if (!envmap.load("envmap.dds", 0)) {
|
||||
if (!envmap.load(fileName, 0)) {
|
||||
printf("Error loading envmap.dds\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
@ -21,18 +21,19 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#include <nvcore/StrLib.h>
|
||||
#include <nvcore/StdStream.h>
|
||||
#include "cmdline.h"
|
||||
|
||||
#include <nvimage/Image.h>
|
||||
#include <nvimage/DirectDrawSurface.h>
|
||||
#include "nvmath/Color.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
|
||||
#include <nvmath/Color.h>
|
||||
#include <nvmath/Vector.h>
|
||||
#include "nvimage/Image.h"
|
||||
#include "nvimage/DirectDrawSurface.h"
|
||||
|
||||
#include "nvcore/StrLib.h"
|
||||
#include "nvcore/StdStream.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "cmdline.h"
|
||||
|
||||
static bool loadImage(nv::Image & image, const char * fileName)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user