2007-11-04 03:34:51 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <memory.h>
|
|
|
|
#include "animcomp.h"
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void *SafeMalloc( size_t n, char *desc );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float *matrix;
|
|
|
|
float *delta;
|
|
|
|
float *best;
|
|
|
|
float *comp;
|
|
|
|
float *tcomp;
|
|
|
|
float *bestcomp;
|
|
|
|
float *frames;
|
|
|
|
float *base;
|
|
|
|
|
|
|
|
int MatWidth;
|
|
|
|
int MatHeight;
|
|
|
|
int CFrameSize;
|
|
|
|
int nFrames;
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void AnimCompressInit( int nframes,int nVerts,int CompressedFrameSize ){
|
|
|
|
nFrames = nframes;
|
|
|
|
MatWidth = nVerts * 3;
|
|
|
|
MatHeight = CompressedFrameSize;
|
|
|
|
CFrameSize = CompressedFrameSize;
|
|
|
|
matrix = (float *)SafeMalloc( MatWidth * MatHeight * sizeof( float ), "AnimCompressInit" );
|
|
|
|
best = (float *)SafeMalloc( MatWidth * MatHeight * sizeof( float ), "AnimCompressInit" );
|
|
|
|
delta = (float *)SafeMalloc( MatWidth * MatHeight * sizeof( float ), "AnimCompressInit" );
|
|
|
|
comp = (float *)SafeMalloc( CFrameSize * nFrames * sizeof( float ), "AnimCompressInit" );
|
|
|
|
tcomp = (float *)SafeMalloc( CFrameSize * nFrames * sizeof( float ), "AnimCompressInit" );
|
|
|
|
bestcomp = (float *)SafeMalloc( CFrameSize * nFrames * sizeof( float ), "AnimCompressInit" );
|
|
|
|
base = (float *)SafeMalloc( MatWidth * sizeof( float ), "AnimCompressInit" );
|
|
|
|
frames = (float *)SafeMalloc( MatWidth * nFrames * sizeof( float ), "AnimCompressInit" );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void AnimSetFrame( int frame,int index,float x,float y,float z ){
|
|
|
|
frames[frame * MatWidth + index * 3] = x;
|
|
|
|
frames[frame * MatWidth + index * 3 + 1] = y;
|
|
|
|
frames[frame * MatWidth + index * 3 + 2] = z;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
typedef struct
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
float val;
|
|
|
|
} SORTP;
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define F_RANDOM ( ( (float)rand() ) / (float)RAND_MAX )
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
extern void DOsvd( float *a,float *res,float *comp,float *values,int nframes,int framesize,int compressedsize );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void AnimCompressDoit(){
|
2007-11-04 03:34:51 +00:00
|
|
|
float compression;
|
|
|
|
float *rescale;
|
|
|
|
float *ans;
|
|
|
|
float maxdev;
|
|
|
|
float avedev;
|
|
|
|
float tmp;
|
|
|
|
int j,k,l,numave;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < MatWidth; k++ )
|
|
|
|
base[k] = 0.0f;
|
|
|
|
for ( j = 0; j < nFrames; j++ )
|
|
|
|
for ( k = 0; k < MatWidth; k++ )
|
|
|
|
base[k] += frames[j * MatWidth + k];
|
|
|
|
tmp = 1.0f / (float)nFrames;
|
|
|
|
for ( k = 0; k < MatWidth; k++ )
|
|
|
|
base[k] *= tmp;
|
|
|
|
for ( j = 0; j < nFrames; j++ )
|
|
|
|
for ( k = 0; k < MatWidth; k++ )
|
|
|
|
frames[j * MatWidth + k] -= base[k];
|
|
|
|
|
|
|
|
ans = (float *)SafeMalloc( sizeof( float ) * MatWidth, "AnimCompressDoit" );
|
|
|
|
rescale = (float *)SafeMalloc( sizeof( float ) * CFrameSize, "AnimCompressDoit" );
|
|
|
|
DOsvd( frames,best,bestcomp,rescale,nFrames,MatWidth,MatHeight );
|
|
|
|
avedev = 0.0;
|
|
|
|
for ( l = 0; l < CFrameSize; l++ )
|
|
|
|
avedev += rescale[l];
|
|
|
|
for ( l = 0; l < CFrameSize; l++ )
|
|
|
|
printf( "%3.1f ",100.0f * rescale[l] / avedev );
|
|
|
|
printf( "\n" );
|
|
|
|
for ( j = 0; j < nFrames; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( l = 0; l < CFrameSize; l++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
bestcomp[j * CFrameSize + l] = 0.0;
|
|
|
|
for ( k = 0; k < MatWidth; k++ )
|
|
|
|
bestcomp[j * CFrameSize + l] += best[l * MatWidth + k] * frames[j * MatWidth + k];
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
numave = 0;
|
|
|
|
avedev = 0.0;
|
|
|
|
maxdev = 0.0;
|
|
|
|
for ( j = 0; j < nFrames; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < MatWidth; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
ans[k] = 0.0;
|
|
|
|
for ( l = 0; l < CFrameSize; l++ )
|
|
|
|
ans[k] += best[l * MatWidth + k] * bestcomp[j * CFrameSize + l];
|
|
|
|
ans[k] -= frames[j * MatWidth + k];
|
|
|
|
tmp = (float)fabs( ans[k] );
|
|
|
|
if ( tmp > maxdev ) {
|
|
|
|
maxdev = tmp;
|
|
|
|
}
|
|
|
|
avedev += tmp;
|
2007-11-04 03:34:51 +00:00
|
|
|
numave++;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
avedev /= (float)numave;
|
|
|
|
printf( "%f Max Deviation (inches) %f Ave Dev. (inches)\n",maxdev,avedev );
|
|
|
|
printf( "%d bytes original size\n",MatWidth * nFrames );
|
|
|
|
printf( "%d bytes of overhead\n",MatWidth * MatHeight );
|
|
|
|
printf( "%d bytes/frame * %d frames = %d bytes\n",CFrameSize,nFrames,CFrameSize * nFrames );
|
|
|
|
compression = (float)( MatWidth * MatHeight + CFrameSize * nFrames + MatWidth );
|
|
|
|
compression /= (float)( MatWidth * nFrames );
|
|
|
|
printf( "Overall compression = %f %%\n",100.0f - 100.0f * compression );
|
|
|
|
compression = (float)( CFrameSize );
|
|
|
|
compression /= (float)( MatWidth );
|
|
|
|
printf( "frame size compression = %f %%\n",100.0f - 100.0f * compression );
|
|
|
|
free( rescale );
|
|
|
|
free( ans );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void AnimCompressToBytes( float *trans,float *scale,char *mat,char *ccomp,unsigned char *cbase,float *cscale,float *coffset,float *bmin,float *bmax ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int k,l,nv,j;
|
|
|
|
float maxdev;
|
|
|
|
float avedev;
|
|
|
|
float tmp;
|
|
|
|
int numave;
|
|
|
|
float t,mx;
|
|
|
|
float *ans;
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
nv = MatWidth / 3;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
trans[0] = 1E30f;
|
|
|
|
scale[0] = -1E30f;
|
|
|
|
trans[1] = 1E30f;
|
|
|
|
scale[1] = -1E30f;
|
|
|
|
trans[2] = 1E30f;
|
|
|
|
scale[2] = -1E30f;
|
|
|
|
for ( k = 0; k < MatWidth; k += 3 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( base[k] > scale[0] ) {
|
|
|
|
scale[0] = base[k];
|
|
|
|
}
|
|
|
|
if ( base[k] < trans[0] ) {
|
|
|
|
trans[0] = base[k];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( base[k + 1] > scale[1] ) {
|
|
|
|
scale[1] = base[k + 1];
|
|
|
|
}
|
|
|
|
if ( base[k + 1] < trans[1] ) {
|
|
|
|
trans[1] = base[k + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( base[k + 2] > scale[2] ) {
|
|
|
|
scale[2] = base[k + 2];
|
|
|
|
}
|
|
|
|
if ( base[k + 2] < trans[2] ) {
|
|
|
|
trans[2] = base[k + 2];
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
scale[0] -= trans[0];
|
|
|
|
scale[1] -= trans[1];
|
|
|
|
scale[2] -= trans[2];
|
|
|
|
scale[0] /= 255.0f;
|
|
|
|
scale[1] /= 255.0f;
|
|
|
|
scale[2] /= 255.0f;
|
|
|
|
for ( k = 0; k < MatWidth; k += 3 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
t = ( base[k] - trans[0] ) / scale[0];
|
|
|
|
if ( t < 0.0f ) {
|
|
|
|
t = 0.0f;
|
|
|
|
}
|
|
|
|
if ( t > 255.0f ) {
|
|
|
|
t = 255.0f;
|
|
|
|
}
|
|
|
|
cbase[k] = (unsigned char)t;
|
|
|
|
|
|
|
|
t = ( base[k + 1] - trans[1] ) / scale[1];
|
|
|
|
if ( t < 0.0f ) {
|
|
|
|
t = 0.0f;
|
|
|
|
}
|
|
|
|
if ( t > 255.0f ) {
|
|
|
|
t = 255.0f;
|
|
|
|
}
|
|
|
|
cbase[k + 1] = (unsigned char)t;
|
|
|
|
|
|
|
|
t = ( base[k + 2] - trans[2] ) / scale[2];
|
|
|
|
if ( t < 0.0f ) {
|
|
|
|
t = 0.0f;
|
|
|
|
}
|
|
|
|
if ( t > 255.0f ) {
|
|
|
|
t = 255.0f;
|
|
|
|
}
|
|
|
|
cbase[k + 2] = (unsigned char)t;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( l = 0; l < MatHeight; l++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
mx = 0.0;
|
|
|
|
for ( k = 0; k < MatWidth; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fabs( best[l * MatWidth + k] ) > mx ) {
|
|
|
|
mx = (float)fabs( best[l * MatWidth + k] );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( mx > 1E-8 ) {
|
|
|
|
mx /= 127.0f;
|
|
|
|
coffset[l] = 1E30f;
|
|
|
|
cscale[l] = -1E30f;
|
|
|
|
for ( j = 0; j < nFrames; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
bestcomp[j * MatHeight + l] *= mx;
|
|
|
|
if ( bestcomp[j * MatHeight + l] > cscale[l] ) {
|
|
|
|
cscale[l] = bestcomp[j * MatHeight + l];
|
|
|
|
}
|
|
|
|
if ( bestcomp[j * MatHeight + l] < coffset[l] ) {
|
|
|
|
coffset[l] = bestcomp[j * MatHeight + l];
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
cscale[l] -= coffset[l];
|
|
|
|
if ( cscale[l] > 1E-10 ) {
|
|
|
|
for ( j = 0; j < nFrames; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
tmp = 254.0f * ( bestcomp[j * MatHeight + l] - coffset[l] ) / cscale[l] - 127.0f;
|
|
|
|
if ( tmp > 127.0f ) {
|
|
|
|
tmp = 127.0f;
|
|
|
|
}
|
|
|
|
if ( tmp < -127.0f ) {
|
|
|
|
tmp = -127.0f;
|
|
|
|
}
|
|
|
|
ccomp[j * MatHeight + l] = (char)floor( tmp + 0.5 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
coffset[l] += cscale[l] * 127.0f / 254.0f;
|
|
|
|
cscale[l] /= 254.0f;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
cscale[l] = 1.0f;
|
|
|
|
coffset[l] = 0.0f;
|
|
|
|
for ( j = 0; j < nFrames; j++ )
|
|
|
|
ccomp[j * MatHeight + l] = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
mx = 1.0f / mx;
|
|
|
|
for ( k = 0; k < MatWidth; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
tmp = best[l * MatWidth + k] * mx;
|
|
|
|
if ( tmp > 127.0f ) {
|
|
|
|
tmp = 127.0f;
|
|
|
|
}
|
|
|
|
if ( tmp < -127.0f ) {
|
|
|
|
tmp = -127.0f;
|
|
|
|
}
|
|
|
|
mat[k * MatHeight + l] = (char)floor( tmp + 0.5 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
cscale[l] = 1.0f;
|
|
|
|
coffset[l] = 0.0f;
|
|
|
|
for ( j = 0; j < nFrames; j++ )
|
|
|
|
ccomp[j * MatHeight + l] = 0;
|
|
|
|
for ( k = 0; k < MatWidth; k++ )
|
|
|
|
mat[k * MatHeight + l] = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
bmin[0] = 1E30f;
|
|
|
|
bmin[1] = 1E30f;
|
|
|
|
bmin[2] = 1E30f;
|
|
|
|
bmax[0] = -1E30f;
|
|
|
|
bmax[1] = -1E30f;
|
|
|
|
bmax[2] = -1E30f;
|
|
|
|
numave = 0;
|
|
|
|
avedev = 0.0;
|
|
|
|
maxdev = 0.0;
|
|
|
|
ans = (float *)SafeMalloc( sizeof( float ) * MatWidth, "AnimCompressToBytes" );
|
|
|
|
for ( j = 0; j < nFrames; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < MatWidth; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
ans[k] = 0.0;
|
|
|
|
for ( l = 0; l < CFrameSize; l++ )
|
|
|
|
ans[k] += (float)( mat[l + k * MatHeight] ) * ( (float)( ccomp[j * CFrameSize + l] ) * cscale[l] + coffset[l] );
|
|
|
|
ans[k] += (float)( cbase[k] ) * scale[k % 3] + trans[k % 3];
|
|
|
|
tmp = (float)fabs( ans[k] - frames[j * MatWidth + k] - base[k] );
|
|
|
|
if ( tmp > maxdev ) {
|
|
|
|
maxdev = tmp;
|
|
|
|
}
|
|
|
|
avedev += tmp;
|
2007-11-04 03:34:51 +00:00
|
|
|
numave++;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bmin[k % 3] > ans[k] ) {
|
|
|
|
bmin[k % 3] = ans[k];
|
|
|
|
}
|
|
|
|
if ( bmax[k % 3] < ans[k] ) {
|
|
|
|
bmax[k % 3] = ans[k];
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
avedev /= (float)numave;
|
|
|
|
printf( "%f Max Deviation (inches) %f Ave Dev. (inches)\n",maxdev,avedev );
|
|
|
|
free( ans );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void AnimCompressGetMatrix( float *mat ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int k,l;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < MatWidth; k++ )
|
|
|
|
for ( l = 0; l < MatHeight; l++ )
|
|
|
|
mat[k * MatHeight + l] = best[l * MatWidth + k];
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void AnimCompressGetFrames( float *mat ){
|
|
|
|
memcpy( mat,bestcomp,CFrameSize * nFrames * sizeof( float ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void AnimCompressGetBase( int i,float *x,float *y,float *z ){
|
|
|
|
*x = base[i * 3];
|
|
|
|
*y = base[i * 3 + 1];
|
|
|
|
*z = base[i * 3 + 2];
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void AnimCompressEnd(){
|
|
|
|
free( matrix );
|
|
|
|
free( best );
|
|
|
|
free( delta );
|
|
|
|
free( comp );
|
|
|
|
free( tcomp );
|
|
|
|
free( bestcomp );
|
|
|
|
free( base );
|
|
|
|
free( frames );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|