/
colournormalfit.cpp
514 lines (402 loc) · 14.5 KB
/
colournormalfit.cpp
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* -----------------------------------------------------------------------------
Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
Copyright (c) 2012 Niels Fröhling niels@paradice-insight.us
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 "colournormalfit.h"
#include "colourset.h"
#include "colourblock.h"
#include "inlineables.cpp"
namespace squish {
/* *****************************************************************************
*/
#if !defined(SQUISH_USE_PRE)
ColourNormalFit::ColourNormalFit(ColourSet const* colours, int flags)
: ColourFit(colours, flags)
{
cQuantizer3<5,6,5> q = cQuantizer3<5,6,5>();
// cache some values
int const count = m_colours->GetCount();
Vec3 const* values = m_colours->GetPoints();
Scr3 const* weights = m_colours->GetWeights();
#ifdef FEATURE_NORMALFIT_PROJECT
Sym3x3 covariance;
Vec3 centroid;
Vec3 principle;
// get the covariance matrix
if (m_colours->IsUnweighted())
ComputeWeightedCovariance3(covariance, centroid, count, values, Vec3(1.0f));
else
ComputeWeightedCovariance3(covariance, centroid, count, values, Vec3(1.0f), weights);
// compute the principle component
GetPrincipleComponent(covariance, principle);
// get the min and max normal as the codebook endpoints
Vec3 start(127.5f, 127.5f, 255.0f);
Vec3 end(127.5f, 127.5f, 255.0f);
if (count > 0) {
#undef FEATURE_NORMALFIT_PROJECT_NEAREST
#ifdef FEATURE_NORMALFIT_PROJECT_NEAREST
const Vec3 scale = Vec3( 1.0f / 0.5f);
const Vec3 offset = Vec3(-1.0f * 0.5f);
const Vec3 scalei = Vec3( 1.0f * 0.5f);
Vec3 centroidn = (scale * (offset + centroid));
Vec3 rec = Reciprocal(principle);
Scr3 min, max;
Vec3 chk;
// http://geomalgorithms.com/a07-_distance.html
// compute the line parameters of the two closest points
min = Scr3( FLT_MAX);
max = Scr3(-FLT_MAX);
for (int i = 0; i < count; ++i) {
Vec3 valuenorm = Normalize(scale * (offset + values[i]));
Vec3 u = principle;//L1.P1 - L1.P0;
Vec3 v = valuenorm;//L2.P1 - L2.P0;
Vec3 w = centroidn;//L1.P0 - L2.P0;
Scr3 a = Dot(u, u); // always >= 0
Scr3 b = Dot(u, v);
Scr3 c = Dot(v, v); // always >= 0
Scr3 d = Dot(u, w);
Scr3 e = Dot(v, w);
Scr3 D = a * c - b * b; // always >= 0
Scr3 sc, tc;
// compute the line parameters of the two closest points
if (D < Scr3(0.00001f)) { // the lines are almost parallel
sc = Scr3(0.0f); // use the largest denominator
tc = (b > c
? d * Reciprocal(b)
: e * Reciprocal(c)
);
}
else {
D = Reciprocal(D);
sc = (b * e - c * d) * D;
tc = (a * e - b * d) * D;
}
// one dimension of the principle axis is 1
// the maximum magnitude the principle axis
// can move in the [-1,+1] cube is 1.41*2
// without leaving the cube's boundaries
sc = Min(sc, Scr3( 2.82842712474619f));
sc = Max(sc, Scr3(-2.82842712474619f));
min = Min(min, sc);
max = Max(max, sc);
}
start = centroidn + principle * min;
end = centroidn + principle * max;
start = Normalize(start);
end = Normalize(end );
start = (start * scalei) - offset;
end = (end * scalei) - offset;
#else
// compute the projection
GetPrincipleProjection(start, end, principle, centroid, count, values);
#endif
#else
Scr3 min, max;
// compute the normal
start = end = values[0];
min = max = Dot(values[0], principle);
for (int i = 1; i < count; ++i) {
Scr3 val = Dot(values[i], principle);
if (val < min) {
start = values[i];
min = val;
}
else if (val > max) {
end = values[i];
max = val;
}
}
#endif
}
// snap floating-point-values to the integer-lattice and save
m_start_candidate = q.SnapToLattice(start);
m_end_candidate = q.SnapToLattice(end );
}
void ColourNormalFit::kMeans3()
{
const Vec3 scale = Vec3( 1.0f / 0.5f);
const Vec3 offset = Vec3(-1.0f * 0.5f);
const Vec3 scalei = Vec3( 1.0f * 0.5f);
// cache some values
int const count = m_colours->GetCount();
Vec3 const* values = m_colours->GetPoints();
Scr3 const* freq = m_colours->GetWeights();
cQuantizer3<5,6,5> q = cQuantizer3<5,6,5>();
Vec3 c_start = m_start, c_end = m_end;
Vec3 l_start = m_start, l_end = m_end;
Scr3 berror = Scr3(DEVIANCE_MAXSUM);
int trie = 1 + (m_flags & kColourIterativeClusterFits) / kColourClusterFit;
do {
Vec3 means[3];
means[0] = Vec3(0.0f);
means[1] = Vec3(0.0f);
means[2] = Vec3(0.0f);
// create a codebook
// resolve "metric * (value - code)" to "metric * value - metric * code"
Vec3 codes[3]; Codebook3n(codes, c_start, c_end);
Scr3 merror = Scr3(DEVIANCE_BASE);
for (int i = 0; i < count; ++i) {
int idx = 0;
// find the closest code
Vec3 value = Normalize(scale * (offset + values[i]));
Scr3 dist; MinDeviance3<true>(dist, idx, value, codes);
// accumulate the error
AddDeviance(dist, merror, freq[i]);
// accumulate the mean
means[idx] += value * freq[i];
}
if (berror > merror) {
berror = merror;
m_start = c_start;
m_end = c_end;
}
means[0] = (Normalize(means[0]) * scalei) - offset;
means[1] = (Normalize(means[1]) * scalei) - offset;
l_start = c_start;
l_end = c_end;
c_start = q.SnapToLattice(means[0]);
c_end = q.SnapToLattice(means[1]);
} while(--trie && !(CompareAllEqualTo(c_start, l_start) && CompareAllEqualTo(c_end, l_end)));
}
void ColourNormalFit::kMeans4()
{
const Vec3 scale = Vec3( 1.0f / 0.5f);
const Vec3 offset = Vec3(-1.0f * 0.5f);
const Vec3 scalei = Vec3( 1.0f * 0.5f);
// cache some values
int const count = m_colours->GetCount();
Vec3 const* values = m_colours->GetPoints();
Scr3 const* freq = m_colours->GetWeights();
cQuantizer3<5,6,5> q = cQuantizer3<5,6,5>();
Vec3 c_start = m_start, c_end = m_end;
Vec3 l_start = m_start, l_end = m_end;
Scr3 berror = Scr3(DEVIANCE_MAXSUM);
int trie = 1 + (m_flags & kColourIterativeClusterFits) / kColourClusterFit;
do {
Vec3 means[4];
means[0] = Vec3(0.0f);
means[1] = Vec3(0.0f);
means[2] = Vec3(0.0f);
means[3] = Vec3(0.0f);
// create a codebook
Vec3 codes[4]; Codebook4n(codes, c_start, c_end);
Scr3 merror = Scr3(DEVIANCE_BASE);
for (int i = 0; i < count; ++i) {
int idx = 0;
// find the closest code
Vec3 value = Normalize(scale * (offset + values[i]));
Scr3 dist; MinDeviance4<true>(dist, idx, value, codes);
// accumulate the error
AddDeviance(dist, merror, freq[i]);
// accumulate the mean
means[idx] += value * freq[i];
}
if (berror > merror) {
berror = merror;
m_start = c_start;
m_end = c_end;
}
means[0] = (Normalize(means[0]) * scalei) - offset;
means[1] = (Normalize(means[1]) * scalei) - offset;
l_start = c_start;
l_end = c_end;
c_start = q.SnapToLattice(means[0]);
c_end = q.SnapToLattice(means[1]);
} while(--trie && !(CompareAllEqualTo(c_start, l_start) && CompareAllEqualTo(c_end, l_end)));
}
void ColourNormalFit::Permute3()
{
const Vec3 scale = Vec3( 1.0f / 0.5f);
const Vec3 offset = Vec3(-1.0f * 0.5f);
const Vec3 scalei = Vec3( 1.0f * 0.5f);
// cache some values
int const count = m_colours->GetCount();
Vec3 const* values = m_colours->GetPoints();
Scr3 const* freq = m_colours->GetWeights();
cQuantizer3<5,6,5> q = cQuantizer3<5,6,5>();
Scr3 berror = Scr3(DEVIANCE_MAXSUM);
Vec3 c_start = m_start;
Vec3 c_end = m_end;
Scr3 l_start = LengthSquared(Normalize(scale * (offset + m_start)));
Scr3 l_end = LengthSquared(Normalize(scale * (offset + m_end)));
Vec3 q_start = Reciprocal(q.grid + Vec3(1.0f));
Vec3 q_end = q_start;
// adjust offset towards sphere-boundary
if (!(l_start < Scr3(1.0f)))
q_start = Vec3(0.0f) - q_start;
if (!(l_end < Scr3(1.0f)))
q_end = Vec3(0.0f) - q_end;
int trie = 0x3F;
do {
// permute end-points +-1 towards sphere-boundary
Vec3 p_start = q_start & Vec3(!(trie & 0x01), !(trie & 0x02), !(trie & 0x04));
Vec3 p_end = q_end & Vec3(!(trie & 0x08), !(trie & 0x10), !(trie & 0x20));
p_start = q.SnapToLattice(c_start + p_start);
p_end = q.SnapToLattice(c_end + p_end);
// create a codebook
// resolve "metric * (value - code)" to "metric * value - metric * code"
Vec3 codes[3]; Codebook3n(codes, p_start, p_end);
Scr3 merror = Scr3(DEVIANCE_BASE);
for (int i = 0; i < count; ++i) {
// find the closest code
Vec3 value = Normalize(scale * (offset + values[i]));
Scr3 dist; MinDeviance3<false>(dist, i, value, codes);
// accumulate the error
AddDeviance(dist, merror, freq[i]);
}
if (berror > merror) {
berror = merror;
m_start = p_start;
m_end = p_end;
}
} while(--trie);
}
void ColourNormalFit::Permute4()
{
const Vec3 scale = Vec3( 1.0f / 0.5f);
const Vec3 offset = Vec3(-1.0f * 0.5f);
const Vec3 scalei = Vec3( 1.0f * 0.5f);
// cache some values
int const count = m_colours->GetCount();
Vec3 const* values = m_colours->GetPoints();
Scr3 const* freq = m_colours->GetWeights();
cQuantizer3<5,6,5> q = cQuantizer3<5,6,5>();
Scr3 berror = Scr3(DEVIANCE_MAXSUM);
Vec3 c_start = m_start;
Vec3 c_end = m_end;
Scr3 l_start = LengthSquared(Normalize(scale * (offset + m_start)));
Scr3 l_end = LengthSquared(Normalize(scale * (offset + m_end)));
Vec3 q_start = Reciprocal(q.grid + Vec3(1.0f));
Vec3 q_end = q_start;
// adjust offset towards sphere-boundary
if (!(l_start < Scr3(1.0f)))
q_start = Vec3(0.0f) - q_start;
if (!(l_end < Scr3(1.0f)))
q_end = Vec3(0.0f) - q_end;
int trie = 0x3F;
do {
// permute end-points +-1 towards sphere-boundary
Vec3 p_start = q_start & Vec3(!(trie & 0x01), !(trie & 0x02), !(trie & 0x04));
Vec3 p_end = q_end & Vec3(!(trie & 0x08), !(trie & 0x10), !(trie & 0x20));
p_start = q.SnapToLattice(c_start + p_start);
p_end = q.SnapToLattice(c_end + p_end);
// create a codebook
Vec3 codes[4]; Codebook4n(codes, p_start, p_end);
Scr3 merror = Scr3(DEVIANCE_BASE);
for (int i = 0; i < count; ++i) {
// find the closest code
Vec3 value = Normalize(scale * (offset + values[i]));
Scr3 dist; MinDeviance4<false>(dist, i, value, codes);
// accumulate the error
AddDeviance(dist, merror, freq[i]);
}
if (berror > merror) {
berror = merror;
m_start = p_start;
m_end = p_end;
}
} while(--trie);
}
void ColourNormalFit::Compress3(void* block)
{
const Vec3 scale = Vec3( 1.0f / 0.5f);
const Vec3 offset = Vec3(-1.0f * 0.5f);
// cache some values
int const count = m_colours->GetCount();
Vec3 const* values = m_colours->GetPoints();
Scr3 const* freq = m_colours->GetWeights();
// use a fitting algorithm
m_start = m_start_candidate;
m_end = m_end_candidate;
if ((m_flags & kColourIterativeClusterFits))
kMeans3();
//if ((m_flags & kColourIterativeClusterFits) >= (kColourIterativeClusterFit))
// Permute3();
// create a codebook
// resolve "metric * (value - code)" to "metric * value - metric * code"
Vec3 codes[3]; Codebook3n(codes, m_start, m_end);
// match each point to the closest code
u8 closest[16];
Scr3 error = Scr3(DEVIANCE_BASE);
for (int i = 0; i < count; ++i) {
int idx = 0;
// find the closest code
Vec3 value = Normalize(scale * (offset + values[i]));
Scr3 dist; MinDeviance3<true>(dist, idx, value, codes);
// accumulate the error
AddDeviance(dist, error, freq[i]);
// save the index
closest[i] = (u8)idx;
}
// save this scheme if it wins
if (error < m_besterror) {
// save the error
m_besterror = error;
// remap the indices
u8 indices[16]; m_colours->RemapIndices(closest, indices);
// save the block
WriteColourBlock3(m_start, m_end, indices, block);
}
}
void ColourNormalFit::Compress4(void* block)
{
const Vec3 scale = Vec3( 1.0f / 0.5f);
const Vec3 offset = Vec3(-1.0f * 0.5f);
// cache some values
int const count = m_colours->GetCount();
Vec3 const* values = m_colours->GetPoints();
Scr3 const* freq = m_colours->GetWeights();
// use a fitting algorithm
m_start = m_start_candidate;
m_end = m_end_candidate;
if (m_flags & kColourIterativeClusterFits)
kMeans4();
//if ((m_flags & kColourIterativeClusterFits) >= (kColourIterativeClusterFit))
// Permute4();
// create a codebook
Vec3 codes[4]; Codebook4n(codes, m_start, m_end);
// match each point to the closest code
u8 closest[16];
Scr3 error = Scr3(DEVIANCE_BASE);
for (int i = 0; i < count; ++i) {
int idx = 0;
// find the closest code
Vec3 value = Normalize(scale * (offset + values[i]));
Scr3 dist; MinDeviance4<true>(dist, idx, value, codes);
// accumulate the error
AddDeviance(dist, error, freq[i]);
// save the index
closest[i] = (u8)idx;
}
// save this scheme if it wins
if (error < m_besterror) {
// save the error
m_besterror = error;
// remap the indices
u8 indices[16]; m_colours->RemapIndices(closest, indices);
// save the block
WriteColourBlock4(m_start, m_end, indices, block);
}
}
#endif
/* *****************************************************************************
*/
#if defined(SQUISH_USE_AMP) || defined(SQUISH_USE_COMPUTE)
#endif
} // namespace squish