Code style cleanup in intern/dualcon.

This commit is contained in:
Nicholas Bishop 2012-05-08 22:11:05 +00:00
parent d9ce1cda94
commit 56342f222f
12 changed files with 3585 additions and 3925 deletions

@ -24,7 +24,7 @@
#define __DUALCON_H__ #define __DUALCON_H__
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef float (*DualConCo)[3]; typedef float (*DualConCo)[3];
@ -35,11 +35,11 @@ typedef struct DualConInput {
DualConCo co; DualConCo co;
int co_stride; int co_stride;
int totco; int totco;
DualConFaces faces; DualConFaces faces;
int face_stride; int face_stride;
int totface; int totface;
float min[3], max[3]; float min[3], max[3];
} DualConInput; } DualConInput;
@ -64,32 +64,32 @@ typedef enum {
} DualConMode; } DualConMode;
/* Usage: /* Usage:
*
The three callback arguments are used for creating the output * The three callback arguments are used for creating the output
mesh. The alloc_output callback takes the total number of vertices * mesh. The alloc_output callback takes the total number of vertices
and faces (quads) that will be in the output. It should allocate * and faces (quads) that will be in the output. It should allocate
and return a structure to hold the output mesh. The add_vert and * and return a structure to hold the output mesh. The add_vert and
add_quad callbacks will then be called for each new vertex and * add_quad callbacks will then be called for each new vertex and
quad, and the callback should add the new mesh elements to the * quad, and the callback should add the new mesh elements to the
structure. * structure.
*/ */
void *dualcon(const DualConInput *input_mesh, void *dualcon(const DualConInput *input_mesh,
/* callbacks for output */ /* callbacks for output */
DualConAllocOutput alloc_output, DualConAllocOutput alloc_output,
DualConAddVert add_vert, DualConAddVert add_vert,
DualConAddQuad add_quad, DualConAddQuad add_quad,
/* flags and settings to control the remeshing /* flags and settings to control the remeshing
algorithm */ * algorithm */
DualConFlags flags, DualConFlags flags,
DualConMode mode, DualConMode mode,
float threshold, float threshold,
float hermite_num, float hermite_num,
float scale, float scale,
int depth); int depth);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* __DUALCON_H__ */ #endif /* __DUALCON_H__ */

@ -36,31 +36,26 @@
// 3d point with integer coordinates // 3d point with integer coordinates
typedef struct typedef struct {
{
int x, y, z; int x, y, z;
} Point3i; } Point3i;
typedef struct typedef struct {
{
Point3i begin; Point3i begin;
Point3i end; Point3i end;
} BoundingBox; } BoundingBox;
// triangle that points to three vertices // triangle that points to three vertices
typedef struct typedef struct {
{ float vt[3][3];
float vt[3][3] ;
} Triangle; } Triangle;
// 3d point with float coordinates // 3d point with float coordinates
typedef struct typedef struct {
{
float x, y, z; float x, y, z;
} Point3f; } Point3f;
typedef struct typedef struct {
{
Point3f begin; Point3f begin;
Point3f end; Point3f end;
} BoundingBoxf; } BoundingBoxf;

@ -43,19 +43,19 @@
class VirtualMemoryAllocator class VirtualMemoryAllocator
{ {
public: public:
virtual void * allocate( ) = 0 ; virtual void *allocate( ) = 0;
virtual void deallocate( void * obj ) = 0 ; virtual void deallocate(void *obj) = 0;
virtual void destroy( ) = 0 ; virtual void destroy( ) = 0;
virtual void printInfo( ) = 0 ; virtual void printInfo( ) = 0;
virtual int getAllocated( ) = 0 ; virtual int getAllocated( ) = 0;
virtual int getAll( ) = 0 ; virtual int getAll( ) = 0;
virtual int getBytes( ) = 0 ; virtual int getBytes( ) = 0;
}; };
/** /**
* Dynamic memory allocator - allows allocation/deallocation * Dynamic memory allocator - allows allocation/deallocation
* *
* Note: there are 4 bytes overhead for each allocated yet unused object. * Note: there are 4 bytes overhead for each allocated yet unused object.
*/ */
template < int N > template < int N >
@ -63,157 +63,157 @@ class MemoryAllocator : public VirtualMemoryAllocator
{ {
private: private:
/// Constants /// Constants
int HEAP_UNIT, HEAP_MASK ; int HEAP_UNIT, HEAP_MASK;
/// Data array /// Data array
UCHAR ** data ; UCHAR **data;
/// Allocation stack /// Allocation stack
UCHAR *** stack ; UCHAR ***stack;
/// Number of data blocks /// Number of data blocks
int datablocknum ; int datablocknum;
/// Number of stack blocks /// Number of stack blocks
int stackblocknum ; int stackblocknum;
/// Size of stack /// Size of stack
int stacksize ; int stacksize;
/// Number of available objects on stack /// Number of available objects on stack
int available ; int available;
/** /**
* Allocate a memory block * Allocate a memory block
*/ */
void allocateDataBlock ( ) void allocateDataBlock( )
{
// Allocate a data block
datablocknum += 1;
data = ( UCHAR ** )realloc(data, sizeof (UCHAR *) * datablocknum);
data[datablocknum - 1] = ( UCHAR * )malloc(HEAP_UNIT * N);
// Update allocation stack
for (int i = 0; i < HEAP_UNIT; i++)
{ {
// Allocate a data block stack[0][i] = (data[datablocknum - 1] + i * N);
datablocknum += 1 ;
data = ( UCHAR ** )realloc( data, sizeof ( UCHAR * ) * datablocknum ) ;
data[ datablocknum - 1 ] = ( UCHAR * )malloc( HEAP_UNIT * N ) ;
// Update allocation stack
for ( int i = 0 ; i < HEAP_UNIT ; i ++ )
{
stack[ 0 ][ i ] = ( data[ datablocknum - 1 ] + i * N ) ;
}
available = HEAP_UNIT ;
} }
available = HEAP_UNIT;
}
/** /**
* Allocate a stack block, to store more deallocated objects * Allocate a stack block, to store more deallocated objects
*/ */
void allocateStackBlock( ) void allocateStackBlock( )
{ {
// Allocate a stack block // Allocate a stack block
stackblocknum += 1 ; stackblocknum += 1;
stacksize += HEAP_UNIT ; stacksize += HEAP_UNIT;
stack = ( UCHAR *** )realloc( stack, sizeof ( UCHAR ** ) * stackblocknum ) ; stack = ( UCHAR *** )realloc(stack, sizeof (UCHAR * *) * stackblocknum);
stack[ stackblocknum - 1 ] = ( UCHAR ** )malloc( HEAP_UNIT * sizeof ( UCHAR * ) ) ; stack[stackblocknum - 1] = ( UCHAR ** )malloc(HEAP_UNIT * sizeof (UCHAR *) );
} }
public: public:
/** /**
* Constructor * Constructor
*/ */
MemoryAllocator( ) MemoryAllocator( )
{
HEAP_UNIT = 1 << HEAP_BASE;
HEAP_MASK = (1 << HEAP_BASE) - 1;
data = ( UCHAR ** )malloc(sizeof(UCHAR *) );
data[0] = ( UCHAR * )malloc(HEAP_UNIT * N);
datablocknum = 1;
stack = ( UCHAR *** )malloc(sizeof (UCHAR * *) );
stack[0] = ( UCHAR ** )malloc(HEAP_UNIT * sizeof (UCHAR *) );
stackblocknum = 1;
stacksize = HEAP_UNIT;
available = HEAP_UNIT;
for (int i = 0; i < HEAP_UNIT; i++)
{ {
HEAP_UNIT = 1 << HEAP_BASE ; stack[0][i] = (data[0] + i * N);
HEAP_MASK = ( 1 << HEAP_BASE ) - 1 ; }
}
data = ( UCHAR ** )malloc( sizeof( UCHAR * ) ) ; /**
data[ 0 ] = ( UCHAR * )malloc( HEAP_UNIT * N ) ; * Destructor
datablocknum = 1 ; */
void destroy( )
{
int i;
for (i = 0; i < datablocknum; i++)
{
free(data[i]);
}
for (i = 0; i < stackblocknum; i++)
{
free(stack[i]);
}
free(data);
free(stack);
}
stack = ( UCHAR *** )malloc( sizeof ( UCHAR ** ) ) ; /**
stack[ 0 ] = ( UCHAR ** )malloc( HEAP_UNIT * sizeof ( UCHAR * ) ) ; * Allocation method
stackblocknum = 1 ; */
stacksize = HEAP_UNIT ; void *allocate( )
available = HEAP_UNIT ; {
if (available == 0)
for ( int i = 0 ; i < HEAP_UNIT ; i ++ ) {
{ allocateDataBlock( );
stack[ 0 ][ i ] = ( data[ 0 ] + i * N ) ;
}
} }
/** // printf("Allocating %d\n", header[ allocated ]) ;
* Destructor available--;
*/ return (void *)stack[available >> HEAP_BASE][available & HEAP_MASK];
void destroy( ) }
/**
* De-allocation method
*/
void deallocate(void *obj)
{
if (available == stacksize)
{ {
int i ; allocateStackBlock( );
for ( i = 0 ; i < datablocknum ; i ++ )
{
free( data[ i ] ) ;
}
for ( i = 0 ; i < stackblocknum ; i ++ )
{
free( stack[ i ] ) ;
}
free( data ) ;
free( stack ) ;
} }
/** // printf("De-allocating %d\n", ( obj - data ) / N ) ;
* Allocation method stack[available >> HEAP_BASE][available & HEAP_MASK] = (UCHAR *)obj;
*/ available++;
void * allocate ( ) // printf("%d %d\n", allocated, header[ allocated ]) ;
{ }
if ( available == 0 )
{
allocateDataBlock ( ) ;
}
// printf("Allocating %d\n", header[ allocated ]) ; /**
available -- ; * Print information
return (void*)stack[ available >> HEAP_BASE ][ available & HEAP_MASK ] ; */
} void printInfo( )
{
printf("Bytes: %d Used: %d Allocated: %d Maxfree: %d\n", getBytes(), getAllocated(), getAll(), stacksize);
}
/** /**
* De-allocation method * Query methods
*/ */
void deallocate ( void * obj ) int getAllocated( )
{ {
if ( available == stacksize ) return HEAP_UNIT * datablocknum - available;
{ };
allocateStackBlock ( ) ;
}
// printf("De-allocating %d\n", ( obj - data ) / N ) ; int getAll( )
stack[ available >> HEAP_BASE ][ available & HEAP_MASK ] = (UCHAR*)obj ; {
available ++ ; return HEAP_UNIT * datablocknum;
// printf("%d %d\n", allocated, header[ allocated ]) ; };
}
/** int getBytes( )
* Print information {
*/ return N;
void printInfo ( ) };
{
printf("Bytes: %d Used: %d Allocated: %d Maxfree: %d\n", getBytes(), getAllocated(), getAll(), stacksize ) ;
}
/**
* Query methods
*/
int getAllocated( )
{
return HEAP_UNIT * datablocknum - available ;
};
int getAll( )
{
return HEAP_UNIT * datablocknum ;
};
int getBytes( )
{
return N ;
};
}; };
#endif #endif

@ -33,31 +33,32 @@
class ModelReader class ModelReader
{ {
public: public:
/// Constructor /// Constructor
ModelReader(){} ; ModelReader(){
};
/// Get next triangle /// Get next triangle
virtual Triangle* getNextTriangle( ) = 0 ; virtual Triangle *getNextTriangle( ) = 0;
virtual int getNextTriangle( int t[3] ) = 0 ; virtual int getNextTriangle(int t[3]) = 0;
/// Get bounding box /// Get bounding box
virtual float getBoundingBox ( float origin[3] ) = 0 ; virtual float getBoundingBox(float origin[3]) = 0;
/// Get number of triangles /// Get number of triangles
virtual int getNumTriangles ( ) = 0 ; virtual int getNumTriangles( ) = 0;
/// Get storage size /// Get storage size
virtual int getMemory ( ) = 0 ; virtual int getMemory( ) = 0;
/// Reset file reading location /// Reset file reading location
virtual void reset( ) = 0 ; virtual void reset( ) = 0;
/// For explicit vertex models /// For explicit vertex models
virtual int getNumVertices( ) = 0 ; virtual int getNumVertices( ) = 0;
virtual void getNextVertex( float v[3] ) = 0 ; virtual void getNextVertex(float v[3]) = 0;
virtual void printInfo ( ) = 0 ; virtual void printInfo( ) = 0;
}; };

@ -37,18 +37,15 @@ const int vertmap[8][3] = {
const int centmap[3][3][3][2] = { const int centmap[3][3][3][2] = {
{{{0, 0}, {0, 1}, {1, 1}}, {{{0, 0}, {0, 1}, {1, 1}},
{{0, 2}, {0, 3}, {1, 3}}, {{0, 2}, {0, 3}, {1, 3}},
{{2, 2}, {2, 3}, {3, 3}} {{2, 2}, {2, 3}, {3, 3}}},
},
{{{0, 4}, {0, 5}, {1, 5}}, {{{0, 4}, {0, 5}, {1, 5}},
{{0, 6}, {0, 7}, {1, 7}}, {{0, 6}, {0, 7}, {1, 7}},
{{2, 6}, {2, 7}, {3, 7}} {{2, 6}, {2, 7}, {3, 7}}},
},
{{{4, 4}, {4, 5}, {5, 5}}, {{{4, 4}, {4, 5}, {5, 5}},
{{4, 6}, {4, 7}, {5, 7}}, {{4, 6}, {4, 7}, {5, 7}},
{{6, 6}, {6, 7}, {7, 7}} {{6, 6}, {6, 7}, {7, 7}}}
}
}; };
const int edgemap[12][2] = { const int edgemap[12][2] = {

File diff suppressed because it is too large Load Diff

@ -23,83 +23,81 @@
#ifndef QUEUE_H #ifndef QUEUE_H
#define QUEUE_H #define QUEUE_H
struct gridQueueEle struct gridQueueEle {
{
int x, y, z; int x, y, z;
UCHAR dir ; UCHAR dir;
gridQueueEle* next ; gridQueueEle *next;
}; };
class GridQueue class GridQueue
{ {
gridQueueEle* head ; gridQueueEle *head;
gridQueueEle* tail ; gridQueueEle *tail;
int numEles ; int numEles;
public: public:
GridQueue( ) GridQueue( )
{
head = NULL;
tail = NULL;
numEles = 0;
}
gridQueueEle *getHead( )
{
return head;
}
int getNumElements( )
{
return numEles;
}
void pushQueue(int st[3], int dir)
{
gridQueueEle *ele = new gridQueueEle;
ele->x = st[0];
ele->y = st[1];
ele->z = st[2];
ele->dir = (UCHAR) dir;
ele->next = NULL;
if (head == NULL)
{ {
head = NULL ; head = ele;
tail = NULL ; }
numEles = 0 ; else {
tail->next = ele;
}
tail = ele;
numEles++;
}
int popQueue(int st[3], int& dir)
{
if (head == NULL)
{
return 0;
} }
gridQueueEle* getHead( ) st[0] = head->x;
st[1] = head->y;
st[2] = head->z;
dir = (int) (head->dir);
gridQueueEle *temp = head;
head = head->next;
delete temp;
if (head == NULL)
{ {
return head ; tail = NULL;
} }
numEles--;
int getNumElements( ) return 1;
{ }
return numEles ;
}
void pushQueue( int st[3], int dir )
{
gridQueueEle* ele = new gridQueueEle ;
ele->x = st[0] ;
ele->y = st[1] ;
ele->z = st[2] ;
ele->dir = (UCHAR) dir ;
ele->next = NULL ;
if ( head == NULL )
{
head = ele ;
}
else
{
tail->next = ele ;
}
tail = ele ;
numEles ++ ;
}
int popQueue( int st[3], int& dir )
{
if ( head == NULL )
{
return 0 ;
}
st[0] = head->x ;
st[1] = head->y ;
st[2] = head->z ;
dir = (int) (head->dir) ;
gridQueueEle* temp = head ;
head = head->next ;
delete temp ;
if ( head == NULL )
{
tail = NULL ;
}
numEles -- ;
return 1 ;
}
}; };

@ -29,18 +29,18 @@
class Cubes class Cubes
{ {
public: public:
/// Get number of triangles /// Get number of triangles
int getNumTriangle(int mask) int getNumTriangle(int mask)
{ {
return marching_cubes_numtri[mask]; return marching_cubes_numtri[mask];
} }
/// Get a triangle /// Get a triangle
void getTriangle(int mask, int index, int indices[3] ) void getTriangle(int mask, int index, int indices[3])
{ {
for(int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
indices[i] = marching_cubes_tris[mask][index][i]; indices[i] = marching_cubes_tris[mask][index][i];
} }
}; };
#endif #endif

@ -40,166 +40,169 @@ void veccopy(float dst[3], const float src[3])
} }
#define GET_FACE(_mesh, _n) \ #define GET_FACE(_mesh, _n) \
(*(DualConFaces)(((char*)(_mesh)->faces) + ((_n) * (_mesh)->face_stride))) (*(DualConFaces)(((char *)(_mesh)->faces) + ((_n) * (_mesh)->face_stride)))
#define GET_CO(_mesh, _n) \ #define GET_CO(_mesh, _n) \
(*(DualConCo)(((char*)(_mesh)->co) + ((_n) * (_mesh)->co_stride))) (*(DualConCo)(((char *)(_mesh)->co) + ((_n) * (_mesh)->co_stride)))
class DualConInputReader : public ModelReader class DualConInputReader : public ModelReader
{ {
private: private:
const DualConInput *input_mesh; const DualConInput *input_mesh;
int tottri, curface, offset; int tottri, curface, offset;
float min[3], max[3], maxsize; float min[3], max[3], maxsize;
float scale; float scale;
public: public:
DualConInputReader(const DualConInput *mesh, float _scale) DualConInputReader(const DualConInput *mesh, float _scale)
: input_mesh(mesh), scale(_scale) : input_mesh(mesh), scale(_scale)
{ {
reset(); reset();
}
void reset()
{
tottri = 0;
curface = 0;
offset = 0;
maxsize = 0;
/* initialize tottri */
for (int i = 0; i < input_mesh->totface; i++)
tottri += GET_FACE(input_mesh, i)[3] ? 2 : 1;
veccopy(min, input_mesh->min);
veccopy(max, input_mesh->max);
/* initialize maxsize */
for (int i = 0; i < 3; i++) {
float d = max[i] - min[i];
if (d > maxsize)
maxsize = d;
} }
void reset() /* redo the bounds */
for (int i = 0; i < 3; i++)
{ {
tottri = 0; min[i] = (max[i] + min[i]) / 2 - maxsize / 2;
curface = 0; max[i] = (max[i] + min[i]) / 2 + maxsize / 2;
}
for (int i = 0; i < 3; i++)
min[i] -= maxsize * (1 / scale - 1) / 2;
maxsize *= 1 / scale;
}
Triangle *getNextTriangle()
{
if (curface == input_mesh->totface)
return 0;
Triangle *t = new Triangle();
unsigned int *f = GET_FACE(input_mesh, curface);
if (offset == 0) {
veccopy(t->vt[0], GET_CO(input_mesh, f[0]));
veccopy(t->vt[1], GET_CO(input_mesh, f[1]));
veccopy(t->vt[2], GET_CO(input_mesh, f[2]));
}
else {
veccopy(t->vt[0], GET_CO(input_mesh, f[2]));
veccopy(t->vt[1], GET_CO(input_mesh, f[3]));
veccopy(t->vt[2], GET_CO(input_mesh, f[0]));
}
if (offset == 0 && f[3])
offset++;
else {
offset = 0; offset = 0;
maxsize = 0; curface++;
/* initialize tottri */
for(int i = 0; i < input_mesh->totface; i++)
tottri += GET_FACE(input_mesh, i)[3] ? 2 : 1;
veccopy(min, input_mesh->min);
veccopy(max, input_mesh->max);
/* initialize maxsize */
for(int i = 0; i < 3; i++) {
float d = max[i] - min[i];
if(d > maxsize)
maxsize = d;
}
/* redo the bounds */
for(int i = 0; i < 3; i++)
{
min[i] = (max[i] + min[i]) / 2 - maxsize / 2;
max[i] = (max[i] + min[i]) / 2 + maxsize / 2;
}
for(int i = 0; i < 3; i++)
min[i] -= maxsize * (1 / scale - 1) / 2;
maxsize *= 1 / scale;
} }
Triangle* getNextTriangle() /* remove triangle if it contains invalid coords */
{ for (int i = 0; i < 3; i++) {
if(curface == input_mesh->totface) const float *co = t->vt[i];
return 0; if (isnan(co[0]) || isnan(co[1]) || isnan(co[2])) {
delete t;
Triangle* t = new Triangle(); return getNextTriangle();
unsigned int *f = GET_FACE(input_mesh, curface);
if(offset == 0) {
veccopy(t->vt[0], GET_CO(input_mesh, f[0]));
veccopy(t->vt[1], GET_CO(input_mesh, f[1]));
veccopy(t->vt[2], GET_CO(input_mesh, f[2]));
} }
else {
veccopy(t->vt[0], GET_CO(input_mesh, f[2]));
veccopy(t->vt[1], GET_CO(input_mesh, f[3]));
veccopy(t->vt[2], GET_CO(input_mesh, f[0]));
}
if(offset == 0 && f[3])
offset++;
else {
offset = 0;
curface++;
}
/* remove triangle if it contains invalid coords */
for(int i = 0; i < 3; i++) {
const float *co = t->vt[i];
if(isnan(co[0]) || isnan(co[1]) || isnan(co[2])) {
delete t;
return getNextTriangle();
}
}
return t;
} }
int getNextTriangle(int t[3]) return t;
{ }
if(curface == input_mesh->totface)
return 0;
unsigned int *f = GET_FACE(input_mesh, curface);
if(offset == 0) {
t[0] = f[0];
t[1] = f[1];
t[2] = f[2];
}
else {
t[0] = f[2];
t[1] = f[3];
t[2] = f[0];
}
if(offset == 0 && f[3]) int getNextTriangle(int t[3])
offset++; {
else { if (curface == input_mesh->totface)
offset = 0; return 0;
curface++;
}
return 1; unsigned int *f = GET_FACE(input_mesh, curface);
if (offset == 0) {
t[0] = f[0];
t[1] = f[1];
t[2] = f[2];
}
else {
t[0] = f[2];
t[1] = f[3];
t[2] = f[0];
} }
int getNumTriangles() if (offset == 0 && f[3])
{ offset++;
return tottri; else {
offset = 0;
curface++;
} }
int getNumVertices() return 1;
{ }
return input_mesh->totco;
}
float getBoundingBox(float origin[3]) int getNumTriangles()
{ {
veccopy(origin, min); return tottri;
return maxsize ; }
}
/* output */ int getNumVertices()
void getNextVertex(float v[3]) {
{ return input_mesh->totco;
/* not used */ }
}
/* stubs */ float getBoundingBox(float origin[3])
void printInfo() {} {
int getMemory() { return sizeof(DualConInputReader); } veccopy(origin, min);
return maxsize;
}
/* output */
void getNextVertex(float v[3])
{
/* not used */
}
/* stubs */
void printInfo() {
}
int getMemory() {
return sizeof(DualConInputReader);
}
}; };
void *dualcon(const DualConInput *input_mesh, void *dualcon(const DualConInput *input_mesh,
/* callbacks for output */ /* callbacks for output */
DualConAllocOutput alloc_output, DualConAllocOutput alloc_output,
DualConAddVert add_vert, DualConAddVert add_vert,
DualConAddQuad add_quad, DualConAddQuad add_quad,
DualConFlags flags, DualConFlags flags,
DualConMode mode, DualConMode mode,
float threshold, float threshold,
float hermite_num, float hermite_num,
float scale, float scale,
int depth) int depth)
{ {
DualConInputReader r(input_mesh, scale); DualConInputReader r(input_mesh, scale);
Octree o(&r, alloc_output, add_vert, add_quad, Octree o(&r, alloc_output, add_vert, add_quad,
flags, mode, depth, threshold, hermite_num); flags, mode, depth, threshold, hermite_num);
o.scanConvert(); o.scanConvert();
return o.getOutputMesh(); return o.getOutputMesh();
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff