protected override unsafe void Compress3(byte *block) {
//
// cache some values
//
int count = colours.Count;
Vec3[] values = colours.Points;
//
// create a codebook
//
Vec3[] codes = new Vec3[3];
codes[0] = start;
codes[1] = end;
codes[2] = 0.5f * start + 0.5f * 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 < 3; ++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.WriteColourBlock3(start, end, indices, block);
//
// save the error
//
bestError = error;
}
}