vcglib/apps/nexus/patch.cpp

387 lines
11 KiB
C++
Raw Normal View History

2005-02-08 13:43:03 +01:00
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program 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. *
* *
* This program 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 (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
2005-04-04 16:27:54 +02:00
Revision 1.13 2005/02/22 10:38:15 ponchio
Debug, cleaning and optimization.
2005-02-22 11:38:17 +01:00
Revision 1.12 2005/02/21 19:05:58 ponchio
i already fixed this bug. I hate you cvs.
Revision 1.11 2005/02/19 12:06:55 ponchio
Debug...
2005-02-19 13:06:55 +01:00
Revision 1.10 2005/02/19 10:45:05 ponchio
Patch generalized and small fixes.
2005-02-19 11:45:05 +01:00
Revision 1.9 2005/02/08 12:43:03 ponchio
Added copyright
2005-02-08 13:43:03 +01:00
****************************************************************************/
2004-09-17 17:25:59 +02:00
#include "patch.h"
2004-10-10 19:36:43 +02:00
#ifdef WIN32
#include "minilzo.108/minilzo.h"
#else
2004-10-10 19:19:42 +02:00
#include <lzo1x.h>
2004-10-10 19:36:43 +02:00
#endif
2004-10-10 19:19:42 +02:00
#include <iostream>
using namespace std;
2004-09-17 17:25:59 +02:00
using namespace nxs;
2004-10-10 19:36:43 +02:00
#ifdef WIN32
static double wrkmem[LZO1X_1_MEM_COMPRESS/sizeof(double) +1];
#else
2004-10-10 19:19:42 +02:00
static double wrkmem[LZO1X_999_MEM_COMPRESS/sizeof(double) +1];
2004-10-10 19:36:43 +02:00
#endif
2004-10-10 19:19:42 +02:00
2004-10-15 13:41:03 +02:00
2005-02-19 11:45:05 +01:00
Encodings Patch::encodings;
Encodings::Encodings() {
2005-04-04 16:27:54 +02:00
for(unsigned int i = 0; i < 17; i++) {
2005-02-19 11:45:05 +01:00
e[i].bytes = 0;
e[i].comps = 0;
e[i].pack = NULL;
e[i].unpack = NULL;
}
e[1].bytes = 1;
e[2].bytes = 2;
e[3].bytes = 4;
e[4].bytes = 8;
e[1].comps = e[2].comps = e[3].comps = e[4].comps = 1;
e[5].bytes = 1;
e[6].bytes = 2;
e[7].bytes = 4;
e[8].bytes = 8;
e[5].comps = e[6].comps = e[7].comps = e[8].comps = 2;
e[9].bytes = 1;
e[10].bytes = 2;
e[11].bytes = 4;
e[12].bytes = 8;
e[9].comps = e[10].comps = e[11].comps = e[12].comps = 3;
e[13].bytes = 1;
e[14].bytes = 2;
e[15].bytes = 4;
e[16].bytes = 8;
e[13].comps = e[14].comps = e[15].comps = e[16].comps = 4;
}
2005-02-22 11:38:17 +01:00
void pad8(unsigned int &s) {
if((s & 0x00000007) != 0) {
s>>=3; s++; s<<=3;
2005-02-19 11:45:05 +01:00
}
}
2004-10-01 18:54:57 +02:00
void pad(unsigned int &size) {
while(size&0x3) size++;
}
2004-09-17 17:25:59 +02:00
2004-10-11 18:04:18 +02:00
void shuffle(float *buffer, unsigned int size, unsigned int stride) {
float *tmp = new float[size];
unsigned int count = 0;
unsigned int nelem = size/stride;
for(unsigned int s = 0; s < stride; s++) {
float *ptr = buffer + s;
for(unsigned int i = 0; i < nelem; i++) {
tmp[count++] = *ptr;
ptr += stride;
}
}
memcpy(buffer, tmp, size * sizeof(float));
delete []tmp;
}
void unshuffle(float *buffer, unsigned int size, unsigned int stride) {
float *tmp = new float[size];
unsigned int count = 0;
unsigned int nelem = size/stride;
for(unsigned int s = 0; s < stride; s++) {
float *ptr = tmp + s;
for(unsigned int i = 0; i < nelem; i++) {
*ptr = buffer[count++];
ptr += stride;
}
}
memcpy(buffer, tmp, size * sizeof(float));
delete []tmp;
}
void subtract(float *buffer, unsigned int size) {
float p = buffer[0];
float q;
for(unsigned int i = 1; i < size; i++) {
q = buffer[i];
buffer[i] -= p;
p = q;
}
}
void unsubtract(float *buffer, unsigned int size) {
for(unsigned int i = 1; i < size; i++)
buffer[i] += buffer[i-1];
}
2005-02-19 11:45:05 +01:00
Patch::Patch(Signature &signature, char *s,
2004-09-17 17:25:59 +02:00
unsigned short nvert, unsigned short nface):
2005-02-19 11:45:05 +01:00
fstart(s) {
2004-09-17 17:25:59 +02:00
Init(signature, nvert, nface);
}
2005-02-19 11:45:05 +01:00
void Patch::Init(Signature &signature,
2004-09-17 17:25:59 +02:00
unsigned short nvert, unsigned short nface) {
nv = nvert;
nf = nface;
2005-02-19 11:45:05 +01:00
unsigned int offset = 0;
if(signature.face == Signature::TRIANGLES)
offset += nf * 3 * sizeof(unsigned short);
else if (signature.face == Signature::STRIPS)
offset += nf * sizeof(unsigned short);
else if (signature.face == Signature::TETRAS)
offset += nf * 4 * sizeof(unsigned short);
else if (signature.face == Signature::SLICE) {
assert(0);
//non lo so...
}
2005-02-22 11:38:17 +01:00
pad8(offset);
2005-02-19 11:45:05 +01:00
2005-02-22 11:38:17 +01:00
fstartc = fstart + offset;
2005-02-19 11:45:05 +01:00
offset += encodings[signature.fcolor].size(nf);
2005-02-22 11:38:17 +01:00
fstartn = fstart + offset;
2005-02-19 11:45:05 +01:00
offset += encodings[signature.fnorm].size(nf);
2005-02-22 11:38:17 +01:00
fstartt = fstart + offset;
2005-02-19 11:45:05 +01:00
offset += encodings[signature.ftext].size(nf);
2005-02-22 11:38:17 +01:00
fstartd = fstart + offset;
2005-02-19 11:45:05 +01:00
offset += encodings[signature.fdata].size(nf);
vstart = fstart + offset;
if(signature.vert == Signature::POINT3F)
offset += nv * sizeof(float) * 3;
else if(signature.vert == Signature::POINT4F)
offset += nv * sizeof(float) * 4;
else
assert(0);
2005-02-22 11:38:17 +01:00
pad8(offset);
2005-02-19 11:45:05 +01:00
2005-02-22 11:38:17 +01:00
vstartc = fstart + offset;
2005-02-19 11:45:05 +01:00
offset += encodings[signature.vcolor].size(nv);
2005-02-22 11:38:17 +01:00
vstartn = fstart + offset;
2005-02-19 11:45:05 +01:00
offset += encodings[signature.vnorm].size(nv);
2005-02-22 11:38:17 +01:00
vstartt = fstart + offset;
2005-02-19 11:45:05 +01:00
offset += encodings[signature.vtext].size(nv);
2005-02-22 11:38:17 +01:00
vstartd = fstart + offset;
2005-02-19 11:45:05 +01:00
offset += encodings[signature.vdata].size(nv);
/* if(signature & NXS_FACES)
2004-09-17 17:25:59 +02:00
vstart = (float *)(((char *)start) + nf * sizeof(unsigned short) * 3);
2004-09-30 02:27:42 +02:00
else if(signature & NXS_STRIP)
2004-09-17 17:25:59 +02:00
vstart = (float *)(((char *)start) + nf * sizeof(unsigned short));
else
vstart = (float *)start;
//align memory
if(((int)vstart) & 0x2) vstart = (float *)(((char *)vstart) + 2);
2004-10-01 18:54:57 +02:00
cstart = nv * 3;
if(signature & NXS_COLORS)
nstart = cstart + nv;
else
nstart = cstart;
if(signature & NXS_NORMALS_SHORT)
tstart = nstart + nv * 2;
else if(signature & NXS_NORMALS_FLOAT)
tstart = nstart + nv * 3;
else
tstart = nstart;
if(signature & NXS_TEXTURES_SHORT)
dstart = tstart + nv;
else if(signature & NXS_TEXTURES_FLOAT)
dstart = tstart + nv;
else
2005-02-19 11:45:05 +01:00
dstart = tstart;*/
2004-09-17 17:25:59 +02:00
}
2005-02-19 11:45:05 +01:00
unsigned int Patch::ChunkSize(Signature &signature,
2004-09-17 17:25:59 +02:00
unsigned short nvert,
2004-10-08 17:12:04 +02:00
unsigned short nface,
unsigned int chunk_size) {
2004-09-17 17:25:59 +02:00
unsigned int size = ByteSize(signature, nvert, nface);
2004-10-08 17:12:04 +02:00
size = (size/chunk_size + 1);
2004-09-17 17:25:59 +02:00
return size;
}
2005-02-19 11:45:05 +01:00
unsigned int Patch::ByteSize(Signature &signature,
2004-09-17 17:25:59 +02:00
unsigned short nvert,
unsigned short nface) {
2005-02-19 11:45:05 +01:00
2004-09-17 17:25:59 +02:00
unsigned int size = 0;
2005-02-19 11:45:05 +01:00
if(signature.face == Signature::TRIANGLES)
size += nface * 3 * sizeof(unsigned short);
else if (signature.face == Signature::STRIPS)
size += nface * sizeof(unsigned short);
2005-02-19 11:45:05 +01:00
else if (signature.face == Signature::TETRAS)
size += nface * 4 * sizeof(unsigned short);
else if (signature.face == Signature::SLICE) {
assert(0);
//non lo so...
}
2005-02-22 11:38:17 +01:00
pad8(size);
2005-02-19 11:45:05 +01:00
size += encodings[signature.fcolor].size(nface);
size += encodings[signature.fnorm].size(nface);
size += encodings[signature.ftext].size(nface);
size += encodings[signature.fdata].size(nface);
if(signature.vert == Signature::POINT3F)
size += nvert * sizeof(float) * 3;
else if(signature.vert == Signature::POINT4F)
size += nvert * sizeof(float) * 4;
else
assert(0);
2005-02-22 11:38:17 +01:00
pad8(size);
2005-02-19 11:45:05 +01:00
size += encodings[signature.vcolor].size(nvert);
size += encodings[signature.vnorm].size(nvert);
size += encodings[signature.vtext].size(nvert);
size += encodings[signature.vdata].size(nvert);
//this condition should really rarely happen but helps save space
//during construction
if(size < nface * 3 * sizeof(unsigned int))
size = nface * 3 * sizeof(unsigned int);
return size;
/* unsigned int size = 0;
2004-09-30 02:27:42 +02:00
if(signature & NXS_STRIP)
2004-09-17 17:25:59 +02:00
size += nface * sizeof(unsigned short);
2004-09-30 02:27:42 +02:00
else if(signature & NXS_FACES)
2004-09-17 17:25:59 +02:00
size += nface * 3 * sizeof(unsigned short);
//memory alignment
2004-10-01 18:54:57 +02:00
pad(size);
2004-09-17 17:25:59 +02:00
size += nvert * sizeof(vcg::Point3f);
2004-09-30 02:27:42 +02:00
if(signature & NXS_COLORS)
2004-09-17 17:25:59 +02:00
size += nvert * sizeof(unsigned int);
2004-09-30 02:27:42 +02:00
if(signature & NXS_NORMALS_SHORT)
2004-09-17 17:25:59 +02:00
size += nvert * 4 * sizeof(short);
2004-09-30 02:27:42 +02:00
if(signature & NXS_NORMALS_FLOAT)
2004-09-17 17:25:59 +02:00
size += nvert * 3 * sizeof(float);
2004-09-30 02:27:42 +02:00
if(signature & NXS_TEXTURES_SHORT)
2004-09-17 17:25:59 +02:00
size += nvert * 2 * sizeof(short);
2004-09-30 02:27:42 +02:00
if(signature & NXS_TEXTURES_FLOAT)
2004-09-17 17:25:59 +02:00
size += nvert * 2 * sizeof(float);
2004-09-30 02:27:42 +02:00
if(signature & NXS_DATA8)
2004-09-17 17:25:59 +02:00
size += nvert * sizeof(char);
2004-10-01 18:54:57 +02:00
pad(size);
2004-09-30 02:27:42 +02:00
if(signature & NXS_DATA16)
2004-09-17 17:25:59 +02:00
size += nvert * 2 * sizeof(char);
2004-10-01 18:54:57 +02:00
pad(size);
2004-09-30 02:27:42 +02:00
if(signature & NXS_DATA32)
2004-09-17 17:25:59 +02:00
size += nvert * 4 * sizeof(char);
2004-09-30 02:27:42 +02:00
if(signature & NXS_DATA64)
2004-09-17 17:25:59 +02:00
size += nvert * 8 * sizeof(char);
//this condition should really rarely happen but helps save space
//during construction
if(size < nface * 3 * sizeof(unsigned int))
size = nface * 3 * sizeof(unsigned int);
2005-02-19 11:45:05 +01:00
return size;*/
2004-09-17 17:25:59 +02:00
}
2004-10-10 19:19:42 +02:00
char *Patch::Compress(unsigned int ram_size, unsigned int &size) {
2004-10-10 19:36:43 +02:00
2004-10-11 18:04:18 +02:00
//lets use differences
// shuffle((float *)VertBegin(), nv * 3, 3);
// subtract((float *)VertBegin(), nv * 3);
2004-10-10 19:36:43 +02:00
2004-10-10 19:19:42 +02:00
//TODO use OVERLAP and test speed
//TODO fill chunk padding with zeroes?
size = ram_size + ram_size/64 + 23;
char *buffer = new char[size];
2004-10-10 19:36:43 +02:00
#ifdef WIN32
2005-02-19 11:45:05 +01:00
lzo1x_1_compress(((unsigned char *)fstart), ram_size,
2004-10-10 19:19:42 +02:00
(unsigned char *)buffer + sizeof(int), &size,
(char *)wrkmem);
2004-10-10 19:36:43 +02:00
#else
2005-02-19 11:45:05 +01:00
lzo1x_999_compress(((unsigned char *)fstart), ram_size,
2004-10-15 13:41:03 +02:00
(unsigned char *)buffer + sizeof(int), &size,
2004-10-10 19:36:43 +02:00
(char *)wrkmem);
2004-10-15 13:41:03 +02:00
lzo1x_optimize((unsigned char *)buffer + sizeof(int), size,
2005-02-19 11:45:05 +01:00
((unsigned char *)fstart), &ram_size,
2004-10-15 13:41:03 +02:00
NULL);
2004-10-10 19:36:43 +02:00
#endif
2004-10-10 19:19:42 +02:00
*(int *)buffer = size;
size += sizeof(int);
// memcpy(buffer, start, ram_size);
// size = ram_size;
2004-10-15 13:41:03 +02:00
2004-10-10 19:19:42 +02:00
return buffer;
2004-10-10 19:36:43 +02:00
2004-10-10 19:19:42 +02:00
}
void Patch::Decompress(unsigned int ram_size, void *src, unsigned int src_sz) {
2004-10-10 19:36:43 +02:00
2004-10-10 19:19:42 +02:00
unsigned int size = *(int *)src;
assert(size < src_sz + sizeof(int));
unsigned int dst_size = ram_size;
2004-10-10 19:36:43 +02:00
2004-10-10 19:19:42 +02:00
int ret = lzo1x_decompress_safe(((unsigned char *)src) + sizeof(int), size,
2005-02-19 11:45:05 +01:00
(unsigned char *)fstart, &dst_size, 0);
2004-10-10 19:19:42 +02:00
if(ret != 0) {
cerr << "Ret from decompress: " << ret << endl;
exit(-1);
}
assert(dst_size == ram_size);
//TODO add 3 to start... so we can use asm_fast decompressor
2004-10-11 18:04:18 +02:00
// unsubtract((float *)VertBegin(), nv * 3);
// unshuffle((float *)VertBegin(), nv * 3, 3);
2004-10-10 19:19:42 +02:00
}