Merge changes from The Witness.
parent
2075d740c9
commit
9489aed825
@ -1,460 +1,513 @@
|
||||
|
||||
#include "ErrorMetric.h"
|
||||
#include "FloatImage.h"
|
||||
#include "Filter.h"
|
||||
|
||||
#include "nvmath/Matrix.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
|
||||
#include <float.h> // FLT_MAX
|
||||
|
||||
using namespace nv;
|
||||
|
||||
float nv::rmsColorError(const FloatImage * ref, const FloatImage * img, bool alphaWeight)
|
||||
{
|
||||
if (!sameLayout(img, ref)) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img->componentCount() == 4);
|
||||
nvDebugCheck(ref->componentCount() == 4);
|
||||
|
||||
double mse = 0;
|
||||
|
||||
const uint count = img->pixelCount();
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
float r0 = ref->pixel(i + count * 0);
|
||||
float g0 = ref->pixel(i + count * 1);
|
||||
float b0 = ref->pixel(i + count * 2);
|
||||
float a0 = ref->pixel(i + count * 3);
|
||||
float r1 = img->pixel(i + count * 0);
|
||||
float g1 = img->pixel(i + count * 1);
|
||||
float b1 = img->pixel(i + count * 2);
|
||||
//float a1 = img->pixel(i + count * 3);
|
||||
|
||||
float r = r0 - r1;
|
||||
float g = g0 - g1;
|
||||
float b = b0 - b1;
|
||||
|
||||
float a = 1;
|
||||
if (alphaWeight) a = a0 * a0; // @@ a0*a1 or a0*a0 ?
|
||||
|
||||
mse += (r * r) * a;
|
||||
mse += (g * g) * a;
|
||||
mse += (b * b) * a;
|
||||
}
|
||||
|
||||
return float(sqrt(mse / count));
|
||||
}
|
||||
|
||||
float nv::rmsAlphaError(const FloatImage * ref, const FloatImage * img)
|
||||
{
|
||||
if (!sameLayout(img, ref)) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img->componentCount() == 4 && ref->componentCount() == 4);
|
||||
|
||||
double mse = 0;
|
||||
|
||||
const uint count = img->pixelCount();
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
float a0 = img->pixel(i + count * 3);
|
||||
float a1 = ref->pixel(i + count * 3);
|
||||
|
||||
float a = a0 - a1;
|
||||
|
||||
mse += a * a;
|
||||
}
|
||||
|
||||
return float(sqrt(mse / count));
|
||||
}
|
||||
|
||||
|
||||
float nv::averageColorError(const FloatImage * ref, const FloatImage * img, bool alphaWeight)
|
||||
{
|
||||
if (!sameLayout(img, ref)) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img->componentCount() == 4);
|
||||
nvDebugCheck(ref->componentCount() == 4);
|
||||
|
||||
double mae = 0;
|
||||
|
||||
const uint count = img->pixelCount();
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
float r0 = img->pixel(i + count * 0);
|
||||
float g0 = img->pixel(i + count * 1);
|
||||
float b0 = img->pixel(i + count * 2);
|
||||
//float a0 = img->pixel(i + count * 3);
|
||||
float r1 = ref->pixel(i + count * 0);
|
||||
float g1 = ref->pixel(i + count * 1);
|
||||
float b1 = ref->pixel(i + count * 2);
|
||||
float a1 = ref->pixel(i + count * 3);
|
||||
|
||||
float r = fabs(r0 - r1);
|
||||
float g = fabs(g0 - g1);
|
||||
float b = fabs(b0 - b1);
|
||||
|
||||
float a = 1;
|
||||
if (alphaWeight) a = a1;
|
||||
|
||||
mae += r * a;
|
||||
mae += g * a;
|
||||
mae += b * a;
|
||||
}
|
||||
|
||||
return float(mae / count);
|
||||
}
|
||||
|
||||
float nv::averageAlphaError(const FloatImage * ref, const FloatImage * img)
|
||||
{
|
||||
if (img == NULL || ref == NULL || img->width() != ref->width() || img->height() != ref->height()) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img->componentCount() == 4 && ref->componentCount() == 4);
|
||||
|
||||
double mae = 0;
|
||||
|
||||
const uint count = img->width() * img->height();
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
float a0 = img->pixel(i + count * 3);
|
||||
float a1 = ref->pixel(i + count * 3);
|
||||
|
||||
float a = a0 - a1;
|
||||
|
||||
mae += fabs(a);
|
||||
}
|
||||
|
||||
return float(mae / count);
|
||||
}
|
||||
|
||||
|
||||
// Color space conversions based on:
|
||||
// http://www.brucelindbloom.com/
|
||||
|
||||
// Assumes input is in *linear* sRGB color space.
|
||||
static Vector3 rgbToXyz(Vector3::Arg c)
|
||||
{
|
||||
Vector3 xyz;
|
||||
xyz.x = 0.412453f * c.x + 0.357580f * c.y + 0.180423f * c.z;
|
||||
xyz.y = 0.212671f * c.x + 0.715160f * c.y + 0.072169f * c.z;
|
||||
xyz.z = 0.019334f * c.x + 0.119193f * c.y + 0.950227f * c.z;
|
||||
return xyz;
|
||||
}
|
||||
|
||||
static Vector3 xyzToRgb(Vector3::Arg c)
|
||||
{
|
||||
Vector3 rgb;
|
||||
rgb.x = 3.2404542f * c.x - 1.5371385f * c.y - 0.4985314f * c.z;
|
||||
rgb.y = -0.9692660f * c.x + 1.8760108f * c.y + 0.0415560f * c.z;
|
||||
rgb.z = 0.0556434f * c.x - 0.2040259f * c.y + 1.0572252f * c.z;
|
||||
return rgb;
|
||||
}
|
||||
|
||||
static float toLinear(float f)
|
||||
{
|
||||
return powf(f, 2.2f);
|
||||
}
|
||||
|
||||
static float toGamma(float f)
|
||||
{
|
||||
// @@ Use sRGB space?
|
||||
return powf(f, 1.0f/2.2f);
|
||||
}
|
||||
|
||||
static Vector3 toLinear(Vector3::Arg c)
|
||||
{
|
||||
return Vector3(toLinear(c.x), toLinear(c.y), toLinear(c.z));
|
||||
}
|
||||
|
||||
static Vector3 toGamma(Vector3::Arg c)
|
||||
{
|
||||
return Vector3(toGamma(c.x), toGamma(c.y), toGamma(c.z));
|
||||
}
|
||||
|
||||
static float f(float t)
|
||||
{
|
||||
const float epsilon = powf(6.0f/29.0f, 3);
|
||||
|
||||
if (t > epsilon) {
|
||||
return powf(t, 1.0f/3.0f);
|
||||
}
|
||||
else {
|
||||
return 1.0f/3.0f * powf(29.0f/6.0f, 2) * t + 4.0f / 29.0f;
|
||||
}
|
||||
}
|
||||
|
||||
static float finv(float t)
|
||||
{
|
||||
if (t > 6.0f / 29.0f) {
|
||||
return 3.0f * powf(6.0f / 29.0f, 2) * (t - 4.0f / 29.0f);
|
||||
}
|
||||
else {
|
||||
return powf(t, 3.0f);
|
||||
}
|
||||
}
|
||||
|
||||
static Vector3 xyzToCieLab(Vector3::Arg c)
|
||||
{
|
||||
// Normalized white point.
|
||||
const float Xn = 0.950456f;
|
||||
const float Yn = 1.0f;
|
||||
const float Zn = 1.088754f;
|
||||
|
||||
float Xr = c.x / Xn;
|
||||
float Yr = c.y / Yn;
|
||||
float Zr = c.z / Zn;
|
||||
|
||||
float fx = f(Xr);
|
||||
float fy = f(Yr);
|
||||
float fz = f(Zr);
|
||||
|
||||
float L = 116 * fx - 16;
|
||||
float a = 500 * (fx - fy);
|
||||
float b = 200 * (fy - fz);
|
||||
|
||||
return Vector3(L, a, b);
|
||||
}
|
||||
|
||||
static Vector3 rgbToCieLab(Vector3::Arg c)
|
||||
{
|
||||
return xyzToCieLab(rgbToXyz(toLinear(c)));
|
||||
}
|
||||
|
||||
// h is hue-angle in radians
|
||||
static Vector3 cieLabToLCh(Vector3::Arg c)
|
||||
{
|
||||
return Vector3(c.x, sqrtf(c.y*c.y + c.z*c.z), atan2f(c.y, c.z));
|
||||
}
|
||||
|
||||
static void rgbToCieLab(const FloatImage * rgbImage, FloatImage * LabImage)
|
||||
{
|
||||
nvDebugCheck(rgbImage != NULL && LabImage != NULL);
|
||||
nvDebugCheck(rgbImage->width() == LabImage->width() && rgbImage->height() == LabImage->height());
|
||||
nvDebugCheck(rgbImage->componentCount() >= 3 && LabImage->componentCount() >= 3);
|
||||
|
||||
const uint w = rgbImage->width();
|
||||
const uint h = LabImage->height();
|
||||
|
||||
const float * R = rgbImage->channel(0);
|
||||
const float * G = rgbImage->channel(1);
|
||||
const float * B = rgbImage->channel(2);
|
||||
|
||||
float * L = LabImage->channel(0);
|
||||
float * a = LabImage->channel(1);
|
||||
float * b = LabImage->channel(2);
|
||||
|
||||
const uint count = w*h;
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
Vector3 Lab = rgbToCieLab(Vector3(R[i], G[i], B[i]));
|
||||
L[i] = Lab.x;
|
||||
a[i] = Lab.y;
|
||||
b[i] = Lab.z;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Assumes input images are in linear sRGB space.
|
||||
float nv::cieLabError(const FloatImage * img0, const FloatImage * img1)
|
||||
{
|
||||
if (!sameLayout(img0, img1)) return FLT_MAX;
|
||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||
|
||||
const float * r0 = img0->channel(0);
|
||||
const float * g0 = img0->channel(1);
|
||||
const float * b0 = img0->channel(2);
|
||||
|
||||
const float * r1 = img1->channel(0);
|
||||
const float * g1 = img1->channel(1);
|
||||
const float * b1 = img1->channel(2);
|
||||
|
||||
double error = 0.0f;
|
||||
|
||||
const uint count = img0->pixelCount();
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
Vector3 lab0 = rgbToCieLab(Vector3(r0[i], g0[i], b0[i]));
|
||||
Vector3 lab1 = rgbToCieLab(Vector3(r1[i], g1[i], b1[i]));
|
||||
|
||||
// @@ Measure Delta E.
|
||||
Vector3 delta = lab0 - lab1;
|
||||
|
||||
error += length(delta);
|
||||
}
|
||||
|
||||
return float(error / count);
|
||||
}
|
||||
|
||||
// Assumes input images are in linear sRGB space.
|
||||
float nv::cieLab94Error(const FloatImage * img0, const FloatImage * img1)
|
||||
{
|
||||
if (!sameLayout(img0, img1)) return FLT_MAX;
|
||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||
|
||||
const float kL = 1;
|
||||
const float kC = 1;
|
||||
const float kH = 1;
|
||||
const float k1 = 0.045f;
|
||||
const float k2 = 0.015f;
|
||||
|
||||
const float sL = 1;
|
||||
|
||||
const float * r0 = img0->channel(0);
|
||||
const float * g0 = img0->channel(1);
|
||||
const float * b0 = img0->channel(2);
|
||||
|
||||
const float * r1 = img1->channel(0);
|
||||
const float * g1 = img1->channel(1);
|
||||
const float * b1 = img1->channel(2);
|
||||
|
||||
double error = 0.0f;
|
||||
|
||||
const uint count = img0->pixelCount();
|
||||
for (uint i = 0; i < count; ++i)
|
||||
{
|
||||
Vector3 lab0 = rgbToCieLab(Vector3(r0[i], g0[i], b0[i]));
|
||||
Vector3 lch0 = cieLabToLCh(lab0);
|
||||
Vector3 lab1 = rgbToCieLab(Vector3(r1[i], g1[i], b1[i]));
|
||||
Vector3 lch1 = cieLabToLCh(lab1);
|
||||
|
||||
const float sC = 1 + k1*lch0.x;
|
||||
const float sH = 1 + k2*lch0.x;
|
||||
|
||||
// @@ Measure Delta E using the 1994 definition
|
||||
Vector3 labDelta = lab0 - lab1;
|
||||
Vector3 lchDelta = lch0 - lch1;
|
||||
|
||||
double deltaLsq = powf(lchDelta.x / (kL*sL), 2);
|
||||
double deltaCsq = powf(lchDelta.y / (kC*sC), 2);
|
||||
|
||||
// avoid possible sqrt of negative value by computing (deltaH/(kH*sH))^2
|
||||
double deltaHsq = powf(labDelta.y, 2) + powf(labDelta.z, 2) - powf(lchDelta.y, 2);
|
||||
deltaHsq /= powf(kH*sH, 2);
|
||||
|
||||
error += sqrt(deltaLsq + deltaCsq + deltaHsq);
|
||||
}
|
||||
|
||||
return float(error / count);
|
||||
}
|
||||
|
||||
float nv::spatialCieLabError(const FloatImage * img0, const FloatImage * img1)
|
||||
{
|
||||
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||
|
||||
uint w = img0->width();
|
||||
uint h = img0->height();
|
||||
uint d = img0->depth();
|
||||
|
||||
FloatImage lab0, lab1; // Original images in CIE-Lab space.
|
||||
lab0.allocate(3, w, h, d);
|
||||
lab1.allocate(3, w, h, d);
|
||||
|
||||
// Convert input images to CIE-Lab.
|
||||
rgbToCieLab(img0, &lab0);
|
||||
rgbToCieLab(img1, &lab1);
|
||||
|
||||
// @@ Convolve each channel by the corresponding filter.
|
||||
/*
|
||||
GaussianFilter LFilter(5);
|
||||
GaussianFilter aFilter(5);
|
||||
GaussianFilter bFilter(5);
|
||||
|
||||
lab0.convolve(0, LFilter);
|
||||
lab0.convolve(1, aFilter);
|
||||
lab0.convolve(2, bFilter);
|
||||
|
||||
lab1.convolve(0, LFilter);
|
||||
lab1.convolve(1, aFilter);
|
||||
lab1.convolve(2, bFilter);
|
||||
*/
|
||||
// @@ Measure Delta E between lab0 and lab1.
|
||||
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
|
||||
// Assumes input images are normal maps.
|
||||
float nv::averageAngularError(const FloatImage * img0, const FloatImage * img1)
|
||||
{
|
||||
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||
|
||||
uint w = img0->width();
|
||||
uint h = img0->height();
|
||||
|
||||
const float * x0 = img0->channel(0);
|
||||
const float * y0 = img0->channel(1);
|
||||
const float * z0 = img0->channel(2);
|
||||
|
||||
const float * x1 = img1->channel(0);
|
||||
const float * y1 = img1->channel(1);
|
||||
const float * z1 = img1->channel(2);
|
||||
|
||||
double error = 0.0f;
|
||||
|
||||
const uint count = w*h;
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
Vector3 n0 = Vector3(x0[i], y0[i], z0[i]);
|
||||
Vector3 n1 = Vector3(x1[i], y1[i], z1[i]);
|
||||
|
||||
n0 = 2.0f * n0 - Vector3(1);
|
||||
n1 = 2.0f * n1 - Vector3(1);
|
||||
|
||||
n0 = normalizeSafe(n0, Vector3(0), 0.0f);
|
||||
n1 = normalizeSafe(n1, Vector3(0), 0.0f);
|
||||
|
||||
error += acos(clamp(dot(n0, n1), -1.0f, 1.0f));
|
||||
}
|
||||
|
||||
return float(error / count);
|
||||
}
|
||||
|
||||
float nv::rmsAngularError(const FloatImage * img0, const FloatImage * img1)
|
||||
{
|
||||
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||
|
||||
uint w = img0->width();
|
||||
uint h = img0->height();
|
||||
|
||||
const float * x0 = img0->channel(0);
|
||||
const float * y0 = img0->channel(1);
|
||||
const float * z0 = img0->channel(2);
|
||||
|
||||
const float * x1 = img1->channel(0);
|
||||
const float * y1 = img1->channel(1);
|
||||
const float * z1 = img1->channel(2);
|
||||
|
||||
double error = 0.0f;
|
||||
|
||||
const uint count = w*h;
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
Vector3 n0 = Vector3(x0[i], y0[i], z0[i]);
|
||||
Vector3 n1 = Vector3(x1[i], y1[i], z1[i]);
|
||||
|
||||
n0 = 2.0f * n0 - Vector3(1);
|
||||
n1 = 2.0f * n1 - Vector3(1);
|
||||
|
||||
n0 = normalizeSafe(n0, Vector3(0), 0.0f);
|
||||
n1 = normalizeSafe(n1, Vector3(0), 0.0f);
|
||||
|
||||
float angle = acosf(clamp(dot(n0, n1), -1.0f, 1.0f));
|
||||
error += angle * angle;
|
||||
}
|
||||
|
||||
return float(sqrt(error / count));
|
||||
}
|
||||
|
||||
|
||||
#include "ErrorMetric.h"
|
||||
#include "FloatImage.h"
|
||||
#include "Filter.h"
|
||||
|
||||
#include "nvmath/Matrix.h"
|
||||
#include "nvmath/Vector.inl"
|
||||
|
||||
#include <float.h> // FLT_MAX
|
||||
|
||||
using namespace nv;
|
||||
|
||||
float nv::rmsColorError(const FloatImage * ref, const FloatImage * img, bool alphaWeight)
|
||||
{
|
||||
if (!sameLayout(img, ref)) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img->componentCount() == 4);
|
||||
nvDebugCheck(ref->componentCount() == 4);
|
||||
|
||||
double mse = 0;
|
||||
|
||||
const uint count = img->pixelCount();
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
float r0 = ref->pixel(i + count * 0);
|
||||
float g0 = ref->pixel(i + count * 1);
|
||||
float b0 = ref->pixel(i + count * 2);
|
||||
float a0 = ref->pixel(i + count * 3);
|
||||
float r1 = img->pixel(i + count * 0);
|
||||
float g1 = img->pixel(i + count * 1);
|
||||
float b1 = img->pixel(i + count * 2);
|
||||
//float a1 = img->pixel(i + count * 3);
|
||||
|
||||
float r = r0 - r1;
|
||||
float g = g0 - g1;
|
||||
float b = b0 - b1;
|
||||
|
||||
float a = 1;
|
||||
if (alphaWeight) a = a0 * a0; // @@ a0*a1 or a0*a0 ?
|
||||
|
||||
mse += (r * r) * a;
|
||||
mse += (g * g) * a;
|
||||
mse += (b * b) * a;
|
||||
}
|
||||
|
||||
return float(sqrt(mse / count));
|
||||
}
|
||||
|
||||
float nv::rmsAlphaError(const FloatImage * ref, const FloatImage * img)
|
||||
{
|
||||
if (!sameLayout(img, ref)) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img->componentCount() == 4 && ref->componentCount() == 4);
|
||||
|
||||
double mse = 0;
|
||||
|
||||
const uint count = img->pixelCount();
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
float a0 = img->pixel(i + count * 3);
|
||||
float a1 = ref->pixel(i + count * 3);
|
||||
|
||||
float a = a0 - a1;
|
||||
|
||||
mse += a * a;
|
||||
}
|
||||
|
||||
return float(sqrt(mse / count));
|
||||
}
|
||||
|
||||
|
||||
float nv::averageColorError(const FloatImage * ref, const FloatImage * img, bool alphaWeight)
|
||||
{
|
||||
if (!sameLayout(img, ref)) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img->componentCount() == 4);
|
||||
nvDebugCheck(ref->componentCount() == 4);
|
||||
|
||||
double mae = 0;
|
||||
|
||||
const uint count = img->pixelCount();
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
float r0 = img->pixel(i + count * 0);
|
||||
float g0 = img->pixel(i + count * 1);
|
||||
float b0 = img->pixel(i + count * 2);
|
||||
//float a0 = img->pixel(i + count * 3);
|
||||
float r1 = ref->pixel(i + count * 0);
|
||||
float g1 = ref->pixel(i + count * 1);
|
||||
float b1 = ref->pixel(i + count * 2);
|
||||
float a1 = ref->pixel(i + count * 3);
|
||||
|
||||
float r = fabs(r0 - r1);
|
||||
float g = fabs(g0 - g1);
|
||||
float b = fabs(b0 - b1);
|
||||
|
||||
float a = 1;
|
||||
if (alphaWeight) a = a1;
|
||||
|
||||
mae += r * a;
|
||||
mae += g * a;
|
||||
mae += b * a;
|
||||
}
|
||||
|
||||
return float(mae / count);
|
||||
}
|
||||
|
||||
float nv::averageAlphaError(const FloatImage * ref, const FloatImage * img)
|
||||
{
|
||||
if (img == NULL || ref == NULL || img->width() != ref->width() || img->height() != ref->height()) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img->componentCount() == 4 && ref->componentCount() == 4);
|
||||
|
||||
double mae = 0;
|
||||
|
||||
const uint count = img->width() * img->height();
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
float a0 = img->pixel(i + count * 3);
|
||||
float a1 = ref->pixel(i + count * 3);
|
||||
|
||||
float a = a0 - a1;
|
||||
|
||||
mae += fabs(a);
|
||||
}
|
||||
|
||||
return float(mae / count);
|
||||
}
|
||||
|
||||
|
||||
float nv::rmsBilinearColorError(const FloatImage * ref, const FloatImage * img, FloatImage::WrapMode wm, bool alphaWeight)
|
||||
{
|
||||
nvDebugCheck(img->componentCount() == 4);
|
||||
nvDebugCheck(ref->componentCount() == 4);
|
||||
|
||||
double mse = 0;
|
||||
|
||||
const uint w0 = ref->width();
|
||||
const uint h0 = ref->height();
|
||||
const uint d0 = ref->depth();
|
||||
|
||||
const uint w1 = img->width();
|
||||
const uint h1 = img->height();
|
||||
const uint d1 = img->depth();
|
||||
|
||||
for (uint z = 0; z < d0; z++) {
|
||||
for (uint y = 0; y < h0; y++) {
|
||||
for (uint x = 0; x < w0; x++) {
|
||||
float r0 = ref->pixel(0, x, y, z);
|
||||
float g0 = ref->pixel(1, x, y, z);
|
||||
float b0 = ref->pixel(2, x, y, z);
|
||||
float a0 = ref->pixel(3, x, y, z);
|
||||
|
||||
float fx = float(x) / w0;
|
||||
float fy = float(y) / h0;
|
||||
float fz = float(z) / d0;
|
||||
|
||||
float r1 = img->sampleLinear(0, fx, fy, fz, wm);
|
||||
float g1 = img->sampleLinear(1, fx, fy, fz, wm);
|
||||
float b1 = img->sampleLinear(2, fx, fy, fz, wm);
|
||||
float a1 = img->sampleLinear(2, fx, fy, fz, wm);
|
||||
|
||||
float dr = r0 - r1;
|
||||
float dg = g0 - g1;
|
||||
float db = b0 - b1;
|
||||
float da = a0 - a1;
|
||||
|
||||
float w = 1;
|
||||
if (alphaWeight) w = a0 * a0; // @@ a0*a1 or a0*a0 ?
|
||||
|
||||
mse += (dr * dr) * w;
|
||||
mse += (dg * dg) * w;
|
||||
mse += (db * db) * w;
|
||||
mse += (da * da);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int count = w0 * h0 * d0;
|
||||
return float(sqrt(mse / count));
|
||||
}
|
||||
|
||||
|
||||
// Color space conversions based on:
|
||||
// http://www.brucelindbloom.com/
|
||||
|
||||
// Assumes input is in *linear* sRGB color space.
|
||||
static Vector3 rgbToXyz(Vector3::Arg c)
|
||||
{
|
||||
Vector3 xyz;
|
||||
xyz.x = 0.412453f * c.x + 0.357580f * c.y + 0.180423f * c.z;
|
||||
xyz.y = 0.212671f * c.x + 0.715160f * c.y + 0.072169f * c.z;
|
||||
xyz.z = 0.019334f * c.x + 0.119193f * c.y + 0.950227f * c.z;
|
||||
return xyz;
|
||||
}
|
||||
|
||||
static Vector3 xyzToRgb(Vector3::Arg c)
|
||||
{
|
||||
Vector3 rgb;
|
||||
rgb.x = 3.2404542f * c.x - 1.5371385f * c.y - 0.4985314f * c.z;
|
||||
rgb.y = -0.9692660f * c.x + 1.8760108f * c.y + 0.0415560f * c.z;
|
||||
rgb.z = 0.0556434f * c.x - 0.2040259f * c.y + 1.0572252f * c.z;
|
||||
return rgb;
|
||||
}
|
||||
|
||||
static float toLinear(float f)
|
||||
{
|
||||
return powf(f, 2.2f);
|
||||
}
|
||||
|
||||
static float toGamma(float f)
|
||||
{
|
||||
// @@ Use sRGB space?
|
||||
return powf(f, 1.0f/2.2f);
|
||||
}
|
||||
|
||||
static Vector3 toLinear(Vector3::Arg c)
|
||||
{
|
||||
return Vector3(toLinear(c.x), toLinear(c.y), toLinear(c.z));
|
||||
}
|
||||
|
||||
static Vector3 toGamma(Vector3::Arg c)
|
||||
{
|
||||
return Vector3(toGamma(c.x), toGamma(c.y), toGamma(c.z));
|
||||
}
|
||||
|
||||
static float f(float t)
|
||||
{
|
||||
const float epsilon = powf(6.0f/29.0f, 3);
|
||||
|
||||
if (t > epsilon) {
|
||||
return powf(t, 1.0f/3.0f);
|
||||
}
|
||||
else {
|
||||
return 1.0f/3.0f * powf(29.0f/6.0f, 2) * t + 4.0f / 29.0f;
|
||||
}
|
||||
}
|
||||
|
||||
static float finv(float t)
|
||||
{
|
||||
if (t > 6.0f / 29.0f) {
|
||||
return 3.0f * powf(6.0f / 29.0f, 2) * (t - 4.0f / 29.0f);
|
||||
}
|
||||
else {
|
||||
return powf(t, 3.0f);
|
||||
}
|
||||
}
|
||||
|
||||
static Vector3 xyzToCieLab(Vector3::Arg c)
|
||||
{
|
||||
// Normalized white point.
|
||||
const float Xn = 0.950456f;
|
||||
const float Yn = 1.0f;
|
||||
const float Zn = 1.088754f;
|
||||
|
||||
float Xr = c.x / Xn;
|
||||
float Yr = c.y / Yn;
|
||||
float Zr = c.z / Zn;
|
||||
|
||||
float fx = f(Xr);
|
||||
float fy = f(Yr);
|
||||
float fz = f(Zr);
|
||||
|
||||
float L = 116 * fx - 16;
|
||||
float a = 500 * (fx - fy);
|
||||
float b = 200 * (fy - fz);
|
||||
|
||||
return Vector3(L, a, b);
|
||||
}
|
||||
|
||||
static Vector3 rgbToCieLab(Vector3::Arg c)
|
||||
{
|
||||
return xyzToCieLab(rgbToXyz(toLinear(c)));
|
||||
}
|
||||
|
||||
// h is hue-angle in radians
|
||||
static Vector3 cieLabToLCh(Vector3::Arg c)
|
||||
{
|
||||
return Vector3(c.x, sqrtf(c.y*c.y + c.z*c.z), atan2f(c.y, c.z));
|
||||
}
|
||||
|
||||
static void rgbToCieLab(const FloatImage * rgbImage, FloatImage * LabImage)
|
||||
{
|
||||
nvDebugCheck(rgbImage != NULL && LabImage != NULL);
|
||||
nvDebugCheck(rgbImage->width() == LabImage->width() && rgbImage->height() == LabImage->height());
|
||||
nvDebugCheck(rgbImage->componentCount() >= 3 && LabImage->componentCount() >= 3);
|
||||
|
||||
const uint w = rgbImage->width();
|
||||
const uint h = LabImage->height();
|
||||
|
||||
const float * R = rgbImage->channel(0);
|
||||
const float * G = rgbImage->channel(1);
|
||||
const float * B = rgbImage->channel(2);
|
||||
|
||||
float * L = LabImage->channel(0);
|
||||
float * a = LabImage->channel(1);
|
||||
float * b = LabImage->channel(2);
|
||||
|
||||
const uint count = w*h;
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
Vector3 Lab = rgbToCieLab(Vector3(R[i], G[i], B[i]));
|
||||
L[i] = Lab.x;
|
||||
a[i] = Lab.y;
|
||||
b[i] = Lab.z;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Assumes input images are in linear sRGB space.
|
||||
float nv::cieLabError(const FloatImage * img0, const FloatImage * img1)
|
||||
{
|
||||
if (!sameLayout(img0, img1)) return FLT_MAX;
|
||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||
|
||||
const float * r0 = img0->channel(0);
|
||||
const float * g0 = img0->channel(1);
|
||||
const float * b0 = img0->channel(2);
|
||||
|
||||
const float * r1 = img1->channel(0);
|
||||
const float * g1 = img1->channel(1);
|
||||
const float * b1 = img1->channel(2);
|
||||
|
||||
double error = 0.0f;
|
||||
|
||||
const uint count = img0->pixelCount();
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
Vector3 lab0 = rgbToCieLab(Vector3(r0[i], g0[i], b0[i]));
|
||||
Vector3 lab1 = rgbToCieLab(Vector3(r1[i], g1[i], b1[i]));
|
||||
|
||||
// @@ Measure Delta E.
|
||||
Vector3 delta = lab0 - lab1;
|
||||
|
||||
error += length(delta);
|
||||
}
|
||||
|
||||
return float(error / count);
|
||||
}
|
||||
|
||||
// Assumes input images are in linear sRGB space.
|
||||
float nv::cieLab94Error(const FloatImage * img0, const FloatImage * img1)
|
||||
{
|
||||
if (!sameLayout(img0, img1)) return FLT_MAX;
|
||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||
|
||||
const float kL = 1;
|
||||
const float kC = 1;
|
||||
const float kH = 1;
|
||||
const float k1 = 0.045f;
|
||||
const float k2 = 0.015f;
|
||||
|
||||
const float sL = 1;
|
||||
|
||||
const float * r0 = img0->channel(0);
|
||||
const float * g0 = img0->channel(1);
|
||||
const float * b0 = img0->channel(2);
|
||||
|
||||
const float * r1 = img1->channel(0);
|
||||
const float * g1 = img1->channel(1);
|
||||
const float * b1 = img1->channel(2);
|
||||
|
||||
double error = 0.0f;
|
||||
|
||||
const uint count = img0->pixelCount();
|
||||
for (uint i = 0; i < count; ++i)
|
||||
{
|
||||
Vector3 lab0 = rgbToCieLab(Vector3(r0[i], g0[i], b0[i]));
|
||||
Vector3 lch0 = cieLabToLCh(lab0);
|
||||
Vector3 lab1 = rgbToCieLab(Vector3(r1[i], g1[i], b1[i]));
|
||||
Vector3 lch1 = cieLabToLCh(lab1);
|
||||
|
||||
const float sC = 1 + k1*lch0.x;
|
||||
const float sH = 1 + k2*lch0.x;
|
||||
|
||||
// @@ Measure Delta E using the 1994 definition
|
||||
Vector3 labDelta = lab0 - lab1;
|
||||
Vector3 lchDelta = lch0 - lch1;
|
||||
|
||||
double deltaLsq = powf(lchDelta.x / (kL*sL), 2);
|
||||
double deltaCsq = powf(lchDelta.y / (kC*sC), 2);
|
||||
|
||||
// avoid possible sqrt of negative value by computing (deltaH/(kH*sH))^2
|
||||
double deltaHsq = powf(labDelta.y, 2) + powf(labDelta.z, 2) - powf(lchDelta.y, 2);
|
||||
deltaHsq /= powf(kH*sH, 2);
|
||||
|
||||
error += sqrt(deltaLsq + deltaCsq + deltaHsq);
|
||||
}
|
||||
|
||||
return float(error / count);
|
||||
}
|
||||
|
||||
float nv::spatialCieLabError(const FloatImage * img0, const FloatImage * img1)
|
||||
{
|
||||
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||
|
||||
uint w = img0->width();
|
||||
uint h = img0->height();
|
||||
uint d = img0->depth();
|
||||
|
||||
FloatImage lab0, lab1; // Original images in CIE-Lab space.
|
||||
lab0.allocate(3, w, h, d);
|
||||
lab1.allocate(3, w, h, d);
|
||||
|
||||
// Convert input images to CIE-Lab.
|
||||
rgbToCieLab(img0, &lab0);
|
||||
rgbToCieLab(img1, &lab1);
|
||||
|
||||
// @@ Convolve each channel by the corresponding filter.
|
||||
/*
|
||||
GaussianFilter LFilter(5);
|
||||
GaussianFilter aFilter(5);
|
||||
GaussianFilter bFilter(5);
|
||||
|
||||
lab0.convolve(0, LFilter);
|
||||
lab0.convolve(1, aFilter);
|
||||
lab0.convolve(2, bFilter);
|
||||
|
||||
lab1.convolve(0, LFilter);
|
||||
lab1.convolve(1, aFilter);
|
||||
lab1.convolve(2, bFilter);
|
||||
*/
|
||||
// @@ Measure Delta E between lab0 and lab1.
|
||||
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
|
||||
// Assumes input images are normal maps.
|
||||
float nv::averageAngularError(const FloatImage * img0, const FloatImage * img1)
|
||||
{
|
||||
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||
|
||||
uint w = img0->width();
|
||||
uint h = img0->height();
|
||||
|
||||
const float * x0 = img0->channel(0);
|
||||
const float * y0 = img0->channel(1);
|
||||
const float * z0 = img0->channel(2);
|
||||
|
||||
const float * x1 = img1->channel(0);
|
||||
const float * y1 = img1->channel(1);
|
||||
const float * z1 = img1->channel(2);
|
||||
|
||||
double error = 0.0f;
|
||||
|
||||
const uint count = w*h;
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
Vector3 n0 = Vector3(x0[i], y0[i], z0[i]);
|
||||
Vector3 n1 = Vector3(x1[i], y1[i], z1[i]);
|
||||
|
||||
n0 = 2.0f * n0 - Vector3(1);
|
||||
n1 = 2.0f * n1 - Vector3(1);
|
||||
|
||||
n0 = normalizeSafe(n0, Vector3(0), 0.0f);
|
||||
n1 = normalizeSafe(n1, Vector3(0), 0.0f);
|
||||
|
||||
error += acos(clamp(dot(n0, n1), -1.0f, 1.0f));
|
||||
}
|
||||
|
||||
return float(error / count);
|
||||
}
|
||||
|
||||
float nv::rmsAngularError(const FloatImage * img0, const FloatImage * img1)
|
||||
{
|
||||
if (img0 == NULL || img1 == NULL || img0->width() != img1->width() || img0->height() != img1->height()) {
|
||||
return FLT_MAX;
|
||||
}
|
||||
nvDebugCheck(img0->componentCount() == 4 && img1->componentCount() == 4);
|
||||
|
||||
uint w = img0->width();
|
||||
uint h = img0->height();
|
||||
|
||||
const float * x0 = img0->channel(0);
|
||||
const float * y0 = img0->channel(1);
|
||||
const float * z0 = img0->channel(2);
|
||||
|
||||
const float * x1 = img1->channel(0);
|
||||
const float * y1 = img1->channel(1);
|
||||
const float * z1 = img1->channel(2);
|
||||
|
||||
double error = 0.0f;
|
||||
|
||||
const uint count = w*h;
|
||||
for (uint i = 0; i < count; i++)
|
||||
{
|
||||
Vector3 n0 = Vector3(x0[i], y0[i], z0[i]);
|
||||
Vector3 n1 = Vector3(x1[i], y1[i], z1[i]);
|
||||
|
||||
n0 = 2.0f * n0 - Vector3(1);
|
||||
n1 = 2.0f * n1 - Vector3(1);
|
||||
|
||||
n0 = normalizeSafe(n0, Vector3(0), 0.0f);
|
||||
n1 = normalizeSafe(n1, Vector3(0), 0.0f);
|
||||
|
||||
float angle = acosf(clamp(dot(n0, n1), -1.0f, 1.0f));
|
||||
error += angle * angle;
|
||||
}
|
||||
|
||||
return float(sqrt(error / count));
|
||||
}
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,208 +1,208 @@
|
||||
// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
|
||||
//
|
||||
// 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, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// 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. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#include "NormalMap.h"
|
||||
#include "Filter.h"
|
||||
#include "FloatImage.h"
|
||||
#include "Image.h"
|
||||
|
||||
#include "nvmath/Color.inl"
|
||||
#include "nvmath/Vector.h"
|
||||
|
||||
#include "nvcore/Ptr.h"
|
||||
|
||||
#include <string.h> // memcpy
|
||||
|
||||
|
||||
using namespace nv;
|
||||
|
||||
// Create normal map using the given kernels.
|
||||
static FloatImage * createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, const Kernel2 * kdu, const Kernel2 * kdv)
|
||||
{
|
||||
nvDebugCheck(kdu != NULL);
|
||||
nvDebugCheck(kdv != NULL);
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
const uint w = img->width();
|
||||
const uint h = img->height();
|
||||
|
||||
AutoPtr<FloatImage> fimage(new FloatImage());
|
||||
fimage->allocate(4, w, h);
|
||||
|
||||
// Compute height and store in alpha channel:
|
||||
float * alphaChannel = fimage->channel(3);
|
||||
for(uint i = 0; i < w * h; i++)
|
||||
{
|
||||
Vector4 color = toVector4(img->pixel(i));
|
||||
alphaChannel[i] = dot(color, heightWeights);
|
||||
}
|
||||
|
||||
float heightScale = 1.0f / 16.0f; // @@ Use a user defined factor.
|
||||
|
||||
for(uint y = 0; y < h; y++)
|
||||
{
|
||||
for(uint x = 0; x < w; x++)
|
||||
{
|
||||
const float du = fimage->applyKernelXY(kdu, x, y, 0, 3, wm);
|
||||
const float dv = fimage->applyKernelXY(kdv, x, y, 0, 3, wm);
|
||||
|
||||
Vector3 n = normalize(Vector3(du, dv, heightScale));
|
||||
|
||||
fimage->pixel(0, x, y, 0) = 0.5f * n.x + 0.5f;
|
||||
fimage->pixel(1, x, y, 0) = 0.5f * n.y + 0.5f;
|
||||
fimage->pixel(2, x, y, 0) = 0.5f * n.z + 0.5f;
|
||||
}
|
||||
}
|
||||
|
||||
return fimage.release();
|
||||
}
|
||||
|
||||
|
||||
// Create normal map using the given kernels.
|
||||
static FloatImage * createNormalMap(const FloatImage * img, FloatImage::WrapMode wm, const Kernel2 * kdu, const Kernel2 * kdv)
|
||||
{
|
||||
nvDebugCheck(kdu != NULL);
|
||||
nvDebugCheck(kdv != NULL);
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
#pragma NV_MESSAGE("FIXME: Height scale parameter should go away. It should be a sensible value that produces good results when the heightmap is in the [0, 1] range.")
|
||||
const float heightScale = 1.0f / 16.0f;
|
||||
|
||||
const uint w = img->width();
|
||||
const uint h = img->height();
|
||||
|
||||
AutoPtr<FloatImage> img_out(new FloatImage());
|
||||
img_out->allocate(4, w, h);
|
||||
|
||||
for (uint y = 0; y < h; y++)
|
||||
{
|
||||
for (uint x = 0; x < w; x++)
|
||||
{
|
||||
const float du = img->applyKernelXY(kdu, x, y, 0, 3, wm);
|
||||
const float dv = img->applyKernelXY(kdv, x, y, 0, 3, wm);
|
||||
|
||||
Vector3 n = normalize(Vector3(du, dv, heightScale));
|
||||
|
||||
img_out->pixel(0, x, y, 0) = n.x;
|
||||
img_out->pixel(1, x, y, 0) = n.y;
|
||||
img_out->pixel(2, x, y, 0) = n.z;
|
||||
}
|
||||
}
|
||||
|
||||
// Copy alpha channel.
|
||||
/*for (uint y = 0; y < h; y++)
|
||||
{
|
||||
for (uint x = 0; x < w; x++)
|
||||
{
|
||||
|
||||
img_out->pixel(3, x, y, 0) = img->pixel(3, x, y, 0);
|
||||
}
|
||||
}*/
|
||||
memcpy(img_out->channel(3), img->channel(3), w * h * sizeof(float));
|
||||
|
||||
return img_out.release();
|
||||
}
|
||||
|
||||
|
||||
/// Create normal map using the given filter.
|
||||
FloatImage * nv::createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, NormalMapFilter filter /*= Sobel3x3*/)
|
||||
{
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
// Init the kernels.
|
||||
Kernel2 * kdu = NULL;
|
||||
Kernel2 * kdv = NULL;
|
||||
|
||||
switch(filter)
|
||||
{
|
||||
case NormalMapFilter_Sobel3x3:
|
||||
kdu = new Kernel2(3);
|
||||
break;
|
||||
case NormalMapFilter_Sobel5x5:
|
||||
kdu = new Kernel2(5);
|
||||
break;
|
||||
case NormalMapFilter_Sobel7x7:
|
||||
kdu = new Kernel2(7);
|
||||
break;
|
||||
case NormalMapFilter_Sobel9x9:
|
||||
kdu = new Kernel2(9);
|
||||
break;
|
||||
default:
|
||||
nvDebugCheck(false);
|
||||
};
|
||||
|
||||
kdu->initSobel();
|
||||
kdu->normalize();
|
||||
|
||||
kdv = new Kernel2(*kdu);
|
||||
kdv->transpose();
|
||||
|
||||
return ::createNormalMap(img, wm, heightWeights, kdu, kdv);
|
||||
}
|
||||
|
||||
|
||||
/// Create normal map combining multiple sobel filters.
|
||||
FloatImage * nv::createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, Vector4::Arg filterWeights)
|
||||
{
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
Kernel2 * kdu = NULL;
|
||||
Kernel2 * kdv = NULL;
|
||||
|
||||
kdu = new Kernel2(9);
|
||||
kdu->initBlendedSobel(filterWeights);
|
||||
kdu->normalize();
|
||||
|
||||
kdv = new Kernel2(*kdu);
|
||||
kdv->transpose();
|
||||
|
||||
return ::createNormalMap(img, wm, heightWeights, kdu, kdv);
|
||||
}
|
||||
|
||||
|
||||
FloatImage * nv::createNormalMap(const FloatImage * img, FloatImage::WrapMode wm, Vector4::Arg filterWeights)
|
||||
{
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
Kernel2 * kdu = NULL;
|
||||
Kernel2 * kdv = NULL;
|
||||
|
||||
kdu = new Kernel2(9);
|
||||
kdu->initBlendedSobel(filterWeights);
|
||||
kdu->normalize();
|
||||
|
||||
kdv = new Kernel2(*kdu);
|
||||
kdv->transpose();
|
||||
|
||||
return ::createNormalMap(img, wm, kdu, kdv);
|
||||
}
|
||||
|
||||
|
||||
/// Normalize the given image in place.
|
||||
void nv::normalizeNormalMap(FloatImage * img)
|
||||
{
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
img->normalize(0);
|
||||
}
|
||||
|
||||
// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
|
||||
//
|
||||
// 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, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// 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. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#include "NormalMap.h"
|
||||
#include "Filter.h"
|
||||
#include "FloatImage.h"
|
||||
#include "Image.h"
|
||||
|
||||
#include "nvmath/Color.inl"
|
||||
#include "nvmath/Vector.h"
|
||||
|
||||
#include "nvcore/Ptr.h"
|
||||
|
||||
#include <string.h> // memcpy
|
||||
|
||||
|
||||
using namespace nv;
|
||||
|
||||
// Create normal map using the given kernels.
|
||||
static FloatImage * createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, const Kernel2 * kdu, const Kernel2 * kdv)
|
||||
{
|
||||
nvDebugCheck(kdu != NULL);
|
||||
nvDebugCheck(kdv != NULL);
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
const uint w = img->width();
|
||||
const uint h = img->height();
|
||||
|
||||
AutoPtr<FloatImage> fimage(new FloatImage());
|
||||
fimage->allocate(4, w, h);
|
||||
|
||||
// Compute height and store in alpha channel:
|
||||
float * alphaChannel = fimage->channel(3);
|
||||
for(uint i = 0; i < w * h; i++)
|
||||
{
|
||||
Vector4 color = toVector4(img->pixel(i));
|
||||
alphaChannel[i] = dot(color, heightWeights);
|
||||
}
|
||||
|
||||
float heightScale = 1.0f / 16.0f; // @@ Use a user defined factor.
|
||||
|
||||
for(uint y = 0; y < h; y++)
|
||||
{
|
||||
for(uint x = 0; x < w; x++)
|
||||
{
|
||||
const float du = fimage->applyKernelXY(kdu, x, y, 0, 3, wm);
|
||||
const float dv = fimage->applyKernelXY(kdv, x, y, 0, 3, wm);
|
||||
|
||||
Vector3 n = normalize(Vector3(du, dv, heightScale));
|
||||
|
||||
fimage->pixel(0, x, y, 0) = 0.5f * n.x + 0.5f;
|
||||
fimage->pixel(1, x, y, 0) = 0.5f * n.y + 0.5f;
|
||||
fimage->pixel(2, x, y, 0) = 0.5f * n.z + 0.5f;
|
||||
}
|
||||
}
|
||||
|
||||
return fimage.release();
|
||||
}
|
||||
|
||||
|
||||
// Create normal map using the given kernels.
|
||||
static FloatImage * createNormalMap(const FloatImage * img, FloatImage::WrapMode wm, const Kernel2 * kdu, const Kernel2 * kdv)
|
||||
{
|
||||
nvDebugCheck(kdu != NULL);
|
||||
nvDebugCheck(kdv != NULL);
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
#pragma NV_MESSAGE("FIXME: Height scale parameter should go away. It should be a sensible value that produces good results when the heightmap is in the [0, 1] range.")
|
||||
const float heightScale = 1.0f / 16.0f;
|
||||
|
||||
const uint w = img->width();
|
||||
const uint h = img->height();
|
||||
|
||||
AutoPtr<FloatImage> img_out(new FloatImage());
|
||||
img_out->allocate(4, w, h);
|
||||
|
||||
for (uint y = 0; y < h; y++)
|
||||
{
|
||||
for (uint x = 0; x < w; x++)
|
||||
{
|
||||
const float du = img->applyKernelXY(kdu, x, y, 0, 3, wm);
|
||||
const float dv = img->applyKernelXY(kdv, x, y, 0, 3, wm);
|
||||
|
||||
Vector3 n = normalize(Vector3(du, dv, heightScale));
|
||||
|
||||
img_out->pixel(0, x, y, 0) = n.x;
|
||||
img_out->pixel(1, x, y, 0) = n.y;
|
||||
img_out->pixel(2, x, y, 0) = n.z;
|
||||
}
|
||||
}
|
||||
|
||||
// Copy alpha channel.
|
||||
/*for (uint y = 0; y < h; y++)
|
||||
{
|
||||
for (uint x = 0; x < w; x++)
|
||||
{
|
||||
|
||||
img_out->pixel(3, x, y, 0) = img->pixel(3, x, y, 0);
|
||||
}
|
||||
}*/
|
||||
memcpy(img_out->channel(3), img->channel(3), w * h * sizeof(float));
|
||||
|
||||
return img_out.release();
|
||||
}
|
||||
|
||||
|
||||
/// Create normal map using the given filter.
|
||||
FloatImage * nv::createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, NormalMapFilter filter /*= Sobel3x3*/)
|
||||
{
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
// Init the kernels.
|
||||
Kernel2 * kdu = NULL;
|
||||
Kernel2 * kdv = NULL;
|
||||
|
||||
switch(filter)
|
||||
{
|
||||
case NormalMapFilter_Sobel3x3:
|
||||
kdu = new Kernel2(3);
|
||||
break;
|
||||
case NormalMapFilter_Sobel5x5:
|
||||
kdu = new Kernel2(5);
|
||||
break;
|
||||
case NormalMapFilter_Sobel7x7:
|
||||
kdu = new Kernel2(7);
|
||||
break;
|
||||
case NormalMapFilter_Sobel9x9:
|
||||
kdu = new Kernel2(9);
|
||||
break;
|
||||
default:
|
||||
nvDebugCheck(false);
|
||||
};
|
||||
|
||||
kdu->initSobel();
|
||||
kdu->normalize();
|
||||
|
||||
kdv = new Kernel2(*kdu);
|
||||
kdv->transpose();
|
||||
|
||||
return ::createNormalMap(img, wm, heightWeights, kdu, kdv);
|
||||
}
|
||||
|
||||
|
||||
/// Create normal map combining multiple sobel filters.
|
||||
FloatImage * nv::createNormalMap(const Image * img, FloatImage::WrapMode wm, Vector4::Arg heightWeights, Vector4::Arg filterWeights)
|
||||
{
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
Kernel2 * kdu = NULL;
|
||||
Kernel2 * kdv = NULL;
|
||||
|
||||
kdu = new Kernel2(9);
|
||||
kdu->initBlendedSobel(filterWeights);
|
||||
kdu->normalize();
|
||||
|
||||
kdv = new Kernel2(*kdu);
|
||||
kdv->transpose();
|
||||
|
||||
return ::createNormalMap(img, wm, heightWeights, kdu, kdv);
|
||||
}
|
||||
|
||||
|
||||
FloatImage * nv::createNormalMap(const FloatImage * img, FloatImage::WrapMode wm, Vector4::Arg filterWeights)
|
||||
{
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
Kernel2 * kdu = NULL;
|
||||
Kernel2 * kdv = NULL;
|
||||
|
||||
kdu = new Kernel2(9);
|
||||
kdu->initBlendedSobel(filterWeights);
|
||||
kdu->normalize();
|
||||
|
||||
kdv = new Kernel2(*kdu);
|
||||
kdv->transpose();
|
||||
|
||||
return ::createNormalMap(img, wm, kdu, kdv);
|
||||
}
|
||||
|
||||
|
||||
/// Normalize the given image in place.
|
||||
void nv::normalizeNormalMap(FloatImage * img)
|
||||
{
|
||||
nvDebugCheck(img != NULL);
|
||||
|
||||
img->normalize(0);
|
||||
}
|
||||
|
||||
|
@ -1,441 +1,487 @@
|
||||
// This code is in the public domain -- castanyo@yahoo.es
|
||||
|
||||
#include "Matrix.inl"
|
||||
#include "Vector.inl"
|
||||
|
||||
#include "nvcore/Array.inl"
|
||||
|
||||
#include <float.h>
|
||||
|
||||
#if !NV_CC_MSVC && !NV_OS_ORBIS
|
||||
#include <alloca.h>
|
||||
#endif
|
||||
|
||||
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;
|
||||
|
||||
float * vv = (float*)alloca(sizeof(float) * n); // vv stores the implicit scaling of each row.
|
||||
|
||||
*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;
|
||||
}
|
||||
|
||||
// @@ Not tested.
|
||||
Matrix nv::inverseLU(const Matrix & A)
|
||||
{
|
||||
Vector4 Ai[4];
|
||||
|
||||
solveLU(A, Vector4(1, 0, 0, 0), &Ai[0]);
|
||||
solveLU(A, Vector4(0, 1, 0, 0), &Ai[1]);
|
||||
solveLU(A, Vector4(0, 0, 1, 0), &Ai[2]);
|
||||
solveLU(A, Vector4(0, 0, 0, 1), &Ai[3]);
|
||||
|
||||
return Matrix(Ai[0], Ai[1], Ai[2], Ai[3]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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(inverseCramer(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 = inverseCramer(A);
|
||||
|
||||
*x = transform(Ai, b);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Inverse using gaussian elimination. From Jon's code.
|
||||
Matrix nv::inverse(const Matrix & m) {
|
||||
|
||||
Matrix A = m;
|
||||
Matrix B(identity);
|
||||
|
||||
int i, j, k;
|
||||
float max, t, det, pivot;
|
||||
|
||||
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 B; /* 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));
|
||||
for (k=0; k<4; k++)
|
||||
swap(B(i, k), B(j, k));
|
||||
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 B;
|
||||
}
|
||||
|
||||
|
||||
Matrix3 nv::inverse(const Matrix3 & m) {
|
||||
|
||||
Matrix3 A = m;
|
||||
Matrix3 B(identity);
|
||||
|
||||
int i, j, k;
|
||||
float max, t, det, pivot;
|
||||
|
||||
det = 1.0;
|
||||
for (i=0; i<3; i++) { /* eliminate in column i, below diag */
|
||||
max = -1.;
|
||||
for (k=i; k<3; k++) /* find pivot for column i */
|
||||
if (fabs(A(k, i)) > max) {
|
||||
max = fabs(A(k, i));
|
||||
j = k;
|
||||
}
|
||||
if (max<=0.) return B; /* if no nonzero pivot, PUNT */
|
||||
if (j!=i) { /* swap rows i and j */
|
||||
for (k=i; k<3; k++)
|
||||
swap(A(i, k), A(j, k));
|
||||
for (k=0; k<3; k++)
|
||||
swap(B(i, k), B(j, k));
|
||||
det = -det;
|
||||
}
|
||||
pivot = A(i, i);
|
||||
det *= pivot;
|
||||
for (k=i+1; k<3; k++) /* only do elems to right of pivot */
|
||||
A(i, k) /= pivot;
|
||||
for (k=0; k<3; 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<3; j++) { /* eliminate in rows below i */
|
||||
t = A(j, i); /* we're gonna zero this guy */
|
||||
for (k=i+1; k<3; 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<3; k++)
|
||||
B(j, k) -= B(i, k)*t;
|
||||
}
|
||||
}
|
||||
|
||||
/*---------- backward elimination ----------*/
|
||||
|
||||
for (i=3-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<3; k++) /* subtract scaled row i from row j */
|
||||
B(j, k) -= B(i, k)*t;
|
||||
}
|
||||
}
|
||||
|
||||
return B;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#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
|
||||
|
||||
|
||||
|
||||
// This code is in the public domain -- castanyo@yahoo.es
|
||||
|
||||
#include "Matrix.inl"
|
||||
#include "Vector.inl"
|
||||
|
||||
#include "nvcore/Array.inl"
|
||||
|
||||
#include <float.h>
|
||||
|
||||
#if !NV_CC_MSVC && !NV_OS_ORBIS
|
||||
#include <alloca.h>
|
||||
#endif
|
||||
|
||||
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;
|
||||
|
||||
float * vv = (float*)alloca(sizeof(float) * n); // vv stores the implicit scaling of each row.
|
||||
|
||||
*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;
|
||||
}
|
||||
|
||||
// @@ Not tested.
|
||||
Matrix nv::inverseLU(const Matrix & A)
|
||||
{
|
||||
Vector4 Ai[4];
|
||||
|
||||
solveLU(A, Vector4(1, 0, 0, 0), &Ai[0]);
|
||||
solveLU(A, Vector4(0, 1, 0, 0), &Ai[1]);
|
||||
solveLU(A, Vector4(0, 0, 1, 0), &Ai[2]);
|
||||
solveLU(A, Vector4(0, 0, 0, 1), &Ai[3]);
|
||||
|
||||
return Matrix(Ai[0], Ai[1], Ai[2], Ai[3]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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::solveLU(const Matrix2 & A, const Vector2 & b, Vector2 * x)
|
||||
{
|
||||
nvDebugCheck(x != NULL);
|
||||
|
||||
float m[2][2];
|
||||
float *a[2] = {m[0], m[1]};
|
||||
int idx[2];
|
||||
float d;
|
||||
|
||||
for (int y = 0; y < 2; y++) {
|
||||
for (int x = 0; x < 2; x++) {
|
||||
a[x][y] = A(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
// Create LU decomposition.
|
||||
if (!ludcmp(a, 2, idx, &d)) {
|
||||
// Singular matrix.
|
||||
return false;
|
||||
}
|
||||
|
||||
// Init solution.
|
||||
*x = b;
|
||||
|
||||
// Do back substitution.
|
||||
lubksb(a, 2, idx, x->component);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool nv::solveCramer(const Matrix & A, const Vector4 & b, Vector4 * x)
|
||||
{
|
||||
nvDebugCheck(x != NULL);
|
||||
|
||||
*x = transform(inverseCramer(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 = inverseCramer(A);
|
||||
|
||||
*x = transform(Ai, b);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool nv::solveCramer(const Matrix2 & A, const Vector2 & b, Vector2 * x)
|
||||
{
|
||||
nvDebugCheck(x != NULL);
|
||||
|
||||
const float det = A.determinant();
|
||||
if (equal(det, 0.0f)) { // @@ Use input epsilon.
|
||||
return false;
|
||||
}
|
||||
|
||||
Matrix2 Ai = inverseCramer(A);
|
||||
|
||||
*x = transform(Ai, b);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Inverse using gaussian elimination. From Jon's code.
|
||||
Matrix nv::inverse(const Matrix & m) {
|
||||
|
||||
Matrix A = m;
|
||||
Matrix B(identity);
|
||||
|
||||
int i, j, k;
|
||||
float max, t, det, pivot;
|
||||
|
||||
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 B; /* 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));
|
||||
for (k=0; k<4; k++)
|
||||
swap(B(i, k), B(j, k));
|
||||
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 B;
|
||||
}
|
||||
|
||||
|
||||
Matrix3 nv::inverse(const Matrix3 & m) {
|
||||
|
||||
Matrix3 A = m;
|
||||
Matrix3 B(identity);
|
||||
|
||||
int i, j, k;
|
||||
float max, t, det, pivot;
|
||||
|
||||
det = 1.0;
|
||||
for (i=0; i<3; i++) { /* eliminate in column i, below diag */
|
||||
max = -1.;
|
||||
for (k=i; k<3; k++) /* find pivot for column i */
|
||||
if (fabs(A(k, i)) > max) {
|
||||
max = fabs(A(k, i));
|
||||
j = k;
|
||||
}
|
||||
if (max<=0.) return B; /* if no nonzero pivot, PUNT */
|
||||
if (j!=i) { /* swap rows i and j */
|
||||
for (k=i; k<3; k++)
|
||||
swap(A(i, k), A(j, k));
|
||||
for (k=0; k<3; k++)
|
||||
swap(B(i, k), B(j, k));
|
||||
det = -det;
|
||||
}
|
||||
pivot = A(i, i);
|
||||
det *= pivot;
|
||||
for (k=i+1; k<3; k++) /* only do elems to right of pivot */
|
||||
A(i, k) /= pivot;
|
||||
for (k=0; k<3; 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<3; j++) { /* eliminate in rows below i */
|
||||
t = A(j, i); /* we're gonna zero this guy */
|
||||
for (k=i+1; k<3; 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<3; k++)
|
||||
B(j, k) -= B(i, k)*t;
|
||||
}
|
||||
}
|
||||
|
||||
/*---------- backward elimination ----------*/
|
||||
|
||||
for (i=3-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<3; k++) /* subtract scaled row i from row j */
|
||||
B(j, k) -= B(i, k)*t;
|
||||
}
|
||||
}
|
||||
|
||||
return B;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#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
|
||||
|
||||
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,20 @@
|
||||
#include "nvcore/nvcore.h"
|
||||
|
||||
namespace nv {
|
||||
|
||||
class Vector3;
|
||||
class Vector4;
|
||||
|
||||
void decompress_etc(const void * input_block, Vector4 output_colors[16]);
|
||||
void decompress_eac(const void * input_block, Vector4 output_colors[16], int output_channel);
|
||||
void decompress_etc_eac(const void * input_block, Vector4 output_colors[16]);
|
||||
|
||||
float compress_etc1(Vector4 input_colors[16], float input_weights[16], const Vector3 & color_weights, void * output);
|
||||
float compress_etc2(Vector4 input_colors[16], float input_weights[16], const Vector3 & color_weights, void * output);
|
||||
float compress_etc2_a1(Vector4 input_colors[16], float input_weights[16], const Vector3 & color_weights, void * output);
|
||||
float compress_eac(Vector4 input_colors[16], float input_weights[16], int input_channel, int search_radius, bool use_11bit_mode, void * output);
|
||||
float compress_etc2_eac(Vector4 input_colors[16], float input_weights[16], const Vector3 & color_weights, void * output);
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue