1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/*
* Copyright 2005 Sandia Corporation.
* Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
* license for use of this work by or on behalf of the
* U.S. Government. Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that this Notice and any
* statement of authorship are reproduced on all copies.
*/
#include "vtkMath.h"
#ifndef ABS
#define ABS(x) ((x) < 0 ? -(x) : (x))
#endif
//=============================================================================
// Helpful class for storing and using color triples.
class Triple {
public:
Triple() {};
Triple(double a, double b, double c) {
data[0] = a; data[1] = b; data[2] = c;
}
const double *operator()() const { return data; }
double *operator()() { return data; }
const double &operator[](int i) const { return data[i]; }
double &operator[](int i) { return data[i]; }
bool operator==(const Triple &triple) const {
return *this == triple.data;
}
bool operator==(const double *triple) const {
return ( (this->data[0] - triple[0] <= 0.01*ABS(data[0])+0.02)
&& (this->data[0] - triple[0] >= -0.01*ABS(data[0])-0.02)
&& (this->data[1] - triple[1] <= 0.01*ABS(data[1])+0.02)
&& (this->data[1] - triple[1] >= -0.01*ABS(data[1])-0.02)
&& (this->data[2] - triple[2] <= 0.01*ABS(data[2])+0.02)
&& (this->data[2] - triple[2] >= -0.01*ABS(data[2])-0.02) );
}
bool operator!=(const Triple &triple) const {
return *this != triple.data;
}
bool operator!=(const double *triple) const {
return !(*this == triple);
}
private:
double data[3];
};
static ostream &operator<<(ostream &os, const Triple t)
{
os << t[0] << ", " << t[1] << ", " << t[2];
return os;
}
//=============================================================================
// Function for comparing colors. Each value should be equivalent in the
// respective color space.
static int TestColorConvert(const Triple &rgb, const Triple &hsv,
const Triple &xyz, const Triple &lab);
int TestMath(int,char *[])
{
int testIntValue;
testIntValue = vtkMath::Factorial(5);
if ( testIntValue != 120 )
{
vtkGenericWarningMacro("Factorial(5) = "<<testIntValue<<" != 120");
return 1;
}
testIntValue = vtkMath::Binomial(8,3);
if ( testIntValue != 56 )
{
vtkGenericWarningMacro("Binomial(8,3) = "<<testIntValue<<" != 56");
return 1;
}
testIntValue = vtkMath::Binomial(5,3);
if ( testIntValue != 10 )
{
vtkGenericWarningMacro("Binomial(5,3) = "<<testIntValue<<" != 10");
return 1;
}
// Solving x(x - 10^-4)^2 = 0 illustrates how the Tartaglia-Cardan solver
// filters some numerical noise by noticing there is a double root (that
// SolveCubic does not notice).
double c[] = { 1., -2.e-4, 1.e-8, 0.};
#if 0
double r1, r2, r3;
int nr;
testIntValue = vtkMath::SolveCubic( c[0], c[1], c[2], c[3], &r1, &r2, &r3, &nr );
if ( testIntValue != 3 )
{
vtkGenericWarningMacro("SolveCubic returned "<<testIntValue<<" != 3");
return 1;
}
#endif // 0
double r[3];
int m[3];
testIntValue = vtkMath::TartagliaCardanSolve( c, r, m );
if ( testIntValue != 2 )
{
vtkGenericWarningMacro("TartagliaCardanSolve returned "<<testIntValue<<" != 2");
return 1;
}
// Test color conversion.
int colorsPassed = 1;
colorsPassed &= TestColorConvert(Triple(1.0, 1.0, 1.0), // RGB
Triple(0.0, 0.0, 1.0), // HSV (H ambiguous)
Triple(95.047, 100.0, 108.883), // XYZ
Triple(100.0, 0.0, 0.0)); // L*ab
colorsPassed &= TestColorConvert(Triple(0.5, 0.5, 0.0), // RGB
Triple(1.0/6.0, 1.0, 0.5), // HSV
Triple(16.48, 19.86, 2.96), // XYZ
Triple(51.68, -12.89, 56.52)); // L*ab
colorsPassed &= TestColorConvert(Triple(0.25, 0.25, 0.5), // RGB
Triple(2.0/3.0, 0.5, 0.5), // HSV
Triple(7.78, 6.27, 21.05), // XYZ
Triple(30.07, 18.51, -36.21)); // L*ab
colorsPassed &= TestColorConvert(Triple(0.0, 0.0, 0.0), // RGB
Triple(0.0, 0.0, 0.0), // HSV (H&S ambiguous)
Triple(0.0, 0.0, 0.0), // XYZ
Triple(0.0, 0.0, 0.0)); // L*ab
if (!colorsPassed)
{
return 1;
}
return 0;
}
static int TestColorConvert(const Triple &rgb, const Triple &hsv,
const Triple &xyz, const Triple &lab)
{
cout << "Ensuring the following colors are consistent: " << endl;
cout << " RGB: " << rgb << endl;
cout << " HSV: " << hsv << endl;
cout << " CIE XYZ: " << xyz << endl;
cout << " CIE-L*ab: " << lab << endl;
Triple result1;
double *result2;
#define COMPARE(testname, target, dest) \
if (target != dest) \
{ \
vtkGenericWarningMacro(<< "Incorrect " #testname " conversion. Got " \
<< dest << " expected " << target); \
return 0; \
}
// Test conversion between RGB and HSV.
vtkMath::RGBToHSV(rgb(), result1());
COMPARE(RGBToHSV, hsv, result1);
vtkMath::HSVToRGB(hsv(), result1());
COMPARE(HSVToRGB, rgb, result1);
result2 = vtkMath::RGBToHSV(rgb());
COMPARE(RGBToHSV, hsv, result2);
result2 = vtkMath::HSVToRGB(hsv());
COMPARE(HSVToRGB, rgb, result2);
vtkMath::RGBToHSV(rgb[0], rgb[1], rgb[2],
&result1[0], &result1[1], &result1[2]);
COMPARE(RGBToHSV, hsv, result1);
vtkMath::HSVToRGB(hsv[0], hsv[1], hsv[2],
&result1[0], &result1[1], &result1[2]);
COMPARE(HSVToRGB, rgb, result1);
// Test conversion between RGB and XYZ.
vtkMath::RGBToXYZ(rgb(), result1());
COMPARE(RGBToXYZ, xyz, result1);
vtkMath::XYZToRGB(xyz(), result1());
COMPARE(XYZToRGB, rgb, result1);
result2 = vtkMath::RGBToXYZ(rgb());
COMPARE(RGBToXYZ, xyz, result2);
result2 = vtkMath::XYZToRGB(xyz());
COMPARE(XYZToRGB, rgb, result2);
vtkMath::RGBToXYZ(rgb[0], rgb[1], rgb[2],
&result1[0], &result1[1], &result1[2]);
COMPARE(RGBToXYZ, xyz, result1);
vtkMath::XYZToRGB(xyz[0], xyz[1], xyz[2],
&result1[0], &result1[1], &result1[2]);
COMPARE(XYZToRGB, rgb, result1);
// Test conversion between Lab and XYZ.
vtkMath::LabToXYZ(lab(), result1());
COMPARE(LabToXYZ, xyz, result1);
vtkMath::XYZToLab(xyz(), result1());
COMPARE(XYZToLab, lab, result1);
result2 = vtkMath::LabToXYZ(lab());
COMPARE(LabToXYZ, xyz, result2);
result2 = vtkMath::XYZToLab(xyz());
COMPARE(XYZToLab, lab, result2);
vtkMath::LabToXYZ(lab[0], lab[1], lab[2],
&result1[0], &result1[1], &result1[2]);
COMPARE(LabToXYZ, xyz, result1);
vtkMath::XYZToLab(xyz[0], xyz[1], xyz[2],
&result1[0], &result1[1], &result1[2]);
COMPARE(XYZToLab, lab, result1);
// Test conversion between Lab and RGB.
vtkMath::LabToRGB(lab(), result1());
COMPARE(LabToRGB, rgb, result1);
vtkMath::RGBToLab(rgb(), result1());
COMPARE(RGBToLab, lab, result1);
result2 = vtkMath::LabToRGB(lab());
COMPARE(LabToRGB, rgb, result2);
result2 = vtkMath::RGBToLab(rgb());
COMPARE(RGBToLab, lab, result2);
vtkMath::LabToRGB(lab[0], lab[1], lab[2],
&result1[0], &result1[1], &result1[2]);
COMPARE(LabToRGB, rgb, result1);
vtkMath::RGBToLab(rgb[0], rgb[1], rgb[2],
&result1[0], &result1[1], &result1[2]);
COMPARE(RGBToLab, lab, result1);
return 1;
}
|