protected override unsafe void Compress4(byte *block) {
//
// cache some values
//
int count = colours.Count;
Vec3[] values = colours.Points;
//
// create a codebook
//
Vec3[] codes = new Vec3[4];
codes[0] = start;
codes[1] = end;
codes[2] = 2.0f / 3.0f * start + 1.0f / 3.0f * end;
codes[3] = 1.0f / 3.0f * start + 2.0f / 3.0f * end;
//
// match each point to the closest code
//
byte[] closest = new byte[16];
float error = 0.0f;
for(int i = 0; i < count; ++i) {
//
// find the closest code
//
float dist = Single.MaxValue;
int idx = 0;
for(int j = 0; j < 4; ++j) {
float d = Vec3.LengthSquared(metric * (values[i] - codes[j]));
if (d < dist) {
dist = d;
idx = j;
}
}
//
// save the index
//
closest[i] = (byte)idx;
//
// accumulate the error
//
error += dist;
}
//
// save this scheme if it wins
//
if (error < bestError) {
//
// remap the indices
//
byte[] indices = new byte[16];
colours.RemapIndices(closest, indices);
//
// save the block
//
ColourBlock.WriteColourBlock4(start, end, indices, block);
//
// save the error
//
bestError = error;
}
}