예제 #1
0
파일: element.c 프로젝트: Zabrane/SPOCP
char *
element2str( element_t *e )
{
    octet_t *o;
    char *tmp;

    o = oct_new( 512, NULL);
    element_print( o, e );
    tmp = oct2strdup(o, 0);
    oct_free(o);

    return tmp;
}
예제 #2
0
파일: element.c 프로젝트: Zabrane/SPOCP
static void
element_set_reduce( element_t *ep )
{
    varr_t      *va;
    size_t      i;
    element_t   *te;
#ifdef XYDEBUG
    octet_t     *op;
    char        *tmp;

    op = oct_new( 512, NULL);
    element_print( op, ep );
    tmp = oct2strdup( op, 0 );
    traceLog(LOG_DEBUG,"Reducing: [%s]", tmp );
    Free( tmp );
#endif

    varr_rm_dup( ep->e.set, P_element_cmp, P_element_free ); 

#ifdef XYDEBUG
    op->len = 0;
    element_print( op, ep );
    tmp = oct2strdup( op, 0 );
    traceLog(LOG_DEBUG, "1:st Reduction to [%s]", tmp);
    Free(tmp);
#endif

    va = ep->e.set;
    if( va->n > 1 ) {
        for( i = 0; i < va->n; i++) {
            if( va->arr[i] == 0 ) continue;

            te = (element_t *) va->arr[i];
            va->arr[i] = element_reduce(te);
        }
    }

#ifdef XYDEBUG
    op->len = 0;
    element_print( op, ep );
    tmp = oct2strdup( op, 0 );
    traceLog(LOG_DEBUG, "2:nd Reduction to [%s]", tmp);
    Free(tmp);
    oct_free( op );
#endif

}
예제 #3
0
void gen_oct(MESH *m, char *filename)
{
	// place each point into the oct-tree
	OctTree *tree = oct_new(100000, 500000);
	int depth = 6;
	float total = (float)(B_EDGE-1) / (float)B_EDGE;
	
	for(int i=0; i<m->nv; i++)
	{
		F3MULS(m->v[i], m->v[i], total);
		int leaf = oct_leaf(tree, &m->v[i], depth);
		ListInt2 *tmp = malloc(sizeof(ListInt2));
		tmp->x = i;
		tmp->y = tree->brick[leaf].nv;
		tmp->next = tree->brick[leaf].v;
		tree->brick[leaf].v = tmp;
		tree->brick[leaf].nv++;
	}
	depth++;

	printf("TreeBlock/Brick = (%d %d)\n",
			tree->blockcount, tree->brickcount);

//	tree->brickcount ++;
	unsigned int buf_size = 16*B_CUBE;
	float4 (*bricktex)[B_EDGE][B_EDGE] = malloc(buf_size);
	memset(bricktex, 0, buf_size);
	int3 index;
	int3 itmp;
	int id, idt;
	ListInt2 *tmp;


	printf("Generating Voxel bricks.\n");
	int lastbrickcount = tree->brickcount;
	for(int i=0; i<lastbrickcount; i++)
	{
//		printf("Brick #%d. %d\n", i, tree->brick[i].nv);
		tmp = tree->brick[i].v;
		if(tmp)
		for(int j=0; j<tree->brick[i].nv && tmp; j++) //while(tmp)
		{
			index.x = ((int)(m->v[tmp->x].x * (7<<depth)) % (B_SIZE-1))+1;
			index.y = ((int)(m->v[tmp->x].y * (7<<depth)) % (B_SIZE-1))+1;
			index.z = ((int)(m->v[tmp->x].z * (7<<depth)) % (B_SIZE-1))+1;
			id = i;
			itmp.x = id % B_COUNT;
			idt = (id-itmp.x) / B_COUNT;
			itmp.y = idt % B_COUNT;
			itmp.z = (idt-itmp.y) / B_COUNT;
			F3MULS(itmp, itmp, (B_SIZE));
			F3ADD(index, index, itmp);
			F3COPY(bricktex[index.z][index.y][index.x], m->n[tmp->x]);
			bricktex[index.z][index.y][index.x].w = 1.0f;
			tmp = tmp->next;
		}

	}

	printf("Copy neighbour voxels.\n");
	for(int i=1; i<tree->brickcount; i++)
	{
		float size = tree->location[i].w;
		int3 d,s;
		d.x = i % B_COUNT;
		idt = (i-d.x) / B_COUNT;
		d.y = idt % B_COUNT;
		d.z = (idt-d.y) / B_COUNT;
		F3MULS(d, d, B_SIZE);

		int src;
		float3 pos;

		F3COPY(pos, tree->location[i]);
		pos.x += size*1.5;
		pos.y += size * 0.5;
		pos.z += size * 0.5;
		src = oct_read(tree, &pos);
		if(src)
		{
			s.x = src % B_COUNT;
			idt = (src-s.x) / B_COUNT;
			s.y = idt % B_COUNT;
			s.z = (idt-s.y) / B_COUNT;
			F3MULS(s, s, B_SIZE);

			for(int x=0; x<B_SIZE; x++)
			for(int y=0; y<B_SIZE; y++)
			{
		bricktex[s.z+x][s.y+y][s.x] = bricktex[d.z+x][d.y+y][d.x+7];
			}
		}

		F3COPY(pos, tree->location[i]);
		pos.x += size*0.5;
		pos.y += size*1.5;
		pos.z += size*0.5;
		src = oct_read(tree, &pos);
		if(src)
		{
			s.x = src % B_COUNT;
			idt = (src-s.x) / B_COUNT;
			s.y = idt % B_COUNT;
			s.z = (idt-s.y) / B_COUNT;
			F3MULS(s, s, B_SIZE);

			for(int x=1; x<B_SIZE; x++)
			for(int y=1; y<B_SIZE; y++)
			{
		bricktex[s.z+x][s.y][s.x+y] = bricktex[d.z+x][d.y+7][d.x+y];
			}
		}

		F3COPY(pos, tree->location[i]);
		pos.x += size*0.5;
		pos.y += size*0.5;
		pos.z += size*1.5;
		src = oct_read(tree, &pos);
		if(src)
		{
			s.x = src % B_COUNT;
			idt = (src-s.x) / B_COUNT;
			s.y = idt % B_COUNT;
			s.z = (idt-s.y) / B_COUNT;
			F3MULS(s, s, B_SIZE);

			for(int x=1; x<B_SIZE; x++)
			for(int y=1; y<B_SIZE; y++)
			{
			bricktex[s.z][s.y+x][s.x+y] = bricktex[d.z+7][d.y+x][d.x+y];
			}
		}


		F3COPY(pos, tree->location[i]);
		pos.x += size*1.5;
		pos.y += size*1.5;
		pos.z += size*0.5;
		src = oct_read(tree, &pos);
		if(src)
		{
			s.x = src % B_COUNT;
			idt = (src-s.x) / B_COUNT;
			s.y = idt % B_COUNT;
			s.z = (idt-s.y) / B_COUNT;
			F3MULS(s, s, B_SIZE);

			for(int x=1; x<B_SIZE; x++)
			{
			bricktex[s.z+x][s.y][s.x] = bricktex[d.z+x][d.y+7][d.x+7];
			}
		}


		F3COPY(pos, tree->location[i]);
		pos.x += size*1.5;
		pos.y += size*0.5;
		pos.z += size*1.5;
		src = oct_read(tree, &pos);
		if(src)
		{
			s.x = src % B_COUNT;
			idt = (src-s.x) / B_COUNT;
			s.y = idt % B_COUNT;
			s.z = (idt-s.y) / B_COUNT;
			F3MULS(s, s, B_SIZE);

			for(int x=1; x<B_SIZE; x++)
			{
			bricktex[s.z][s.y+x][s.x] = bricktex[d.z+7][d.y+x][d.x+7];
			}
		}

		F3COPY(pos, tree->location[i]);
		pos.x += size*0.5;
		pos.y += size*1.5;
		pos.z += size*1.5;
		src = oct_read(tree, &pos);
		if(src)
		{
			s.x = src % B_COUNT;
			idt = (src-s.x) / B_COUNT;
			s.y = idt % B_COUNT;
			s.z = (idt-s.y) / B_COUNT;
			F3MULS(s, s, B_SIZE);

			for(int x=1; x<B_SIZE; x++)
			{
			bricktex[s.z][s.y][s.x+x] = bricktex[d.z+7][d.y+7][d.x+x];
			}
		}

		F3COPY(pos, tree->location[i]);
		pos.x += size*1.5;
		pos.y += size*1.5;
		pos.z += size*1.5;
		src = oct_read(tree, &pos);
		if(src)
		{
			s.x = src % B_COUNT;
			idt = (src-s.x) / B_COUNT;
			s.y = idt % B_COUNT;
			s.z = (idt-s.y) / B_COUNT;
			F3MULS(s, s, B_SIZE);

			bricktex[s.z][s.y][s.x] = bricktex[d.z+7][d.y+7][d.x+7];
		}

	}
	printf("TreeBlock/Brick = (%d %d)\n",
			tree->blockcount, tree->brickcount);
	printf("Walking the normals.\n");
	for(int i=1; i<tree->brickcount; i++)
	{
		

	}

	printf("Writing \"%s\".\n", filename);
	FILE *fptr = fopen(filename, "wb");
	int zero = 0;
	fwrite("VOCT", 4, 1, fptr);
	fwrite(&tree->blockcount, 4, 1, fptr);
	fwrite(&tree->brickcount, 4, 1, fptr);
	fwrite(&zero, 4, 1, fptr);
	fwrite(tree->block, tree->blockcount, sizeof(OctBlock), fptr);

	z_stream strm = { .zalloc=Z_NULL, .zfree=Z_NULL, .opaque=Z_NULL };
	int ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
	if(ret != Z_OK)
	{
		printf("deflateInit() failed.\n");
		return;
	}
#define CHUNK (256 * 1024)
	unsigned char in[CHUNK];
	unsigned char out[CHUNK];
	int row = sizeof(float4)*B_SIZE;

	int have, inoff = 0;
	for(int i=0; i<tree->brickcount; i++)
	{
		int3 boff;
		int itmp = i % B_COUNT;
		boff.x = itmp;
		itmp = (i-boff.x) / B_COUNT;
		boff.y = itmp % B_COUNT;
		boff.z = (itmp-boff.y) / B_COUNT;
		F3MULS(boff, boff, B_SIZE);
		for(int z=0; z < B_SIZE; z++)
		for(int y=0; y < B_SIZE; y++)
		{
			memcpy(in+inoff, &bricktex[boff.z+z][boff.y+y][boff.x], row);
			inoff += row;
			if(inoff+row > CHUNK)
			{
				strm.next_in = in;
				strm.avail_in = inoff;
				do{
					strm.avail_out = CHUNK;
					strm.next_out = out;
					ret = deflate(&strm, Z_NO_FLUSH);
//					assert(ret != Z_STREAM_ERROR);
					have = CHUNK - strm.avail_out;
					fwrite(out, have, 1, fptr);
				} while (strm.avail_out == 0);
//				assert(strm.avail_in == 0);
				inoff = 0;
			}
//		fwrite(&bricktex[boff.z+z][boff.y+y][boff.x],
//				sizeof(float4)*B_SIZE, 1, fptr);
		}
	}
	strm.next_in = in;
	strm.avail_in = inoff;
	do{
		strm.avail_out = CHUNK;
		strm.next_out = out;
		ret = deflate(&strm, Z_FINISH);
//		assert(ret != Z_STREAM_ERROR);
		have = CHUNK - strm.avail_out;
		fwrite(out, have, 1, fptr);
	} while (strm.avail_out == 0);
//	assert(strm.avail_in == 0);
	deflateEnd(&strm);

//	fwrite(bricktex, 16*B_CUBE, 1, fptr);
//	fwrite(bricktex[B_EDGE], 16*B_CUBE, 1, fptr);
	fclose(fptr);
}
예제 #4
0
파일: read.c 프로젝트: Zabrane/SPOCP
int
read_rules(srv_t * srv, char *file, dbcmd_t * dbc)
{
	FILE           *fp;
	char           *sp, *tmp;
	int             n = 0, f = 0, r;
	octet_t         *op;
	octarr_t       *oa = 0;
	ruleset_t      *rs = 0, *trs, *prs;
	spocp_result_t  rc = SPOCP_SUCCESS;
	spocp_charbuf_t	*buf;
	spocp_chunk_t	*chunk = 0, *ck;
	spocp_chunkwrap_t   *cw;
	spocp_ruledef_t	rdef;
	struct stat	statbuf;

	if ((fp = fopen(file, "r")) == 0) {
		LOG(SPOCP_EMERG) traceLog(LOG_ERR,"couldn't open rule file \"%s\"",
					  file);
		op = oct_new( 256, NULL);
		sp = getcwd(op->val, op->size);
		traceLog(LOG_ERR,"I'm in \"%s\"", sp);
		oct_free(op);
		return -1;
	}

	stat( file, &statbuf);

	srv->mtime = statbuf.st_mtime;
 
	/*
	 * The default ruleset should already be set 
	 */

	if (srv->root == 0) {
		srv->root = rs = ruleset_new(0);
	} else
		rs = srv->root;

	if (rs->db == 0)
		rs->db = db_new();

	buf = charbuf_new( fp, BUFSIZ );

	if (get_more(buf) == 0) return 0;

	/*
	 * have to escape CR since fgets stops reading when it hits a newline
	 * NUL also has to be escaped since I have problem otherwise finding
	 * the length of the 'string'. '\' hex hex is probably going to be the 
	 * choice 
	 */
	while (rc == SPOCP_SUCCESS ) {
	    cw = get_object( buf, 0 );
	    if (cw->status == 0) {
	        Free(cw);
	        break;
	    }
	    else if (cw->status == -1) {
	        rc = SPOCP_LOCAL_ERROR;
            Free(cw);
            break;
        }
        else {
            chunk = cw->chunk;
            Free(cw);
        }
	    
		if (oct2strcmp(chunk->val, ";include ") == 0) {	/* include
								 * file */
			ck = chunk->next;
			tmp = oct2strdup( ck->val, 0 ) ;
			LOG(SPOCP_DEBUG) traceLog(LOG_DEBUG,"include directive \"%s\"",
						  tmp);
			if ((rc = read_rules(srv, tmp, dbc)) < 0) {
				traceLog(LOG_ERR,"Include problem");
			}
		}
		else if (*chunk->val->val == '/' || *chunk->val->val == '(') {
			trs = rs;
			if (*chunk->val->val == '/') {
#ifdef AVLUS
				oct_print(LOG_INFO,"ruleset", chunk->val);
#endif
				if ((trs = ruleset_find( chunk->val, rs)) == NULL) {
					octet_t oct;

					octln( &oct, chunk->val);
					rs = ruleset_create(chunk->val, rs);
					trs = ruleset_find(&oct, rs);
					trs->db = db_new();
				}

				ck = chunk->next;
			}
			else {
				ck = chunk;
			}

			ruledef_return( &rdef, ck ) ;
			if( rdef.rule ) {
				op = chunk2sexp( rdef.rule ) ;
				oa = octarr_add(oa, op) ;
				LOG(SPOCP_DEBUG) {
					traceLog(LOG_DEBUG,"We've got a rule");
				}
			}
			
			if( rdef.bcond) {
				op = chunk2sexp( rdef.bcond ) ;
				oa = octarr_add(oa, op) ;
				LOG(SPOCP_DEBUG) {
					traceLog(LOG_DEBUG,"We've got a boundary condition");
				}
			}