Here's a small script that conveniently converts BRL-CAD databases
into gcode. The commandline looks like this:

./brlcad2gcode.sh  brlcad_database.g  brlcad_object_to_pick  file.ngc

"brlcad_object_to_pick" should be a "region" or something like this in the 
database
that you want to pick for conversion. Use the object that you'd use for 
raytracing, too.
If you don't know what I'm talking about, see brl-cad and mged docs :P

The script uses various tools to convert the raw 3D data into a 2D map
and then via image-to-gcode into gcode.
The CAD Z axis is defined as being the object height.

There are some patches to mesh2hmap needed. I posted them to the mesh2hmap 
bugzilla
and I also attached them to this mail.

Have fun. :)

-- 
Greetings, Michael.

Attachment: brlcad2gcode.sh
Description: application/shellscript

---
 src/mesh2hmap.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- mesh2hmap.orig/src/mesh2hmap.c
+++ mesh2hmap/src/mesh2hmap.c
@@ -33,7 +33,7 @@ typedef struct sub_grid {
 
 // 2D cross product of difference vectors.
 // returns: cross(v1-v0, v2-v0)
-inline float sub_cross2d(float *v0, float *v1, float *v2) 
+static inline float sub_cross2d(float *v0, float *v1, float *v2) 
 {
 	return ( (v1[0]-v0[0])*(v2[1]-v0[1]) - 
 		 (v1[1]-v0[1])*(v2[0]-v0[0]) );
---
 src/parser.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- mesh2hmap.orig/src/parser.c
+++ mesh2hmap/src/parser.c
@@ -111,7 +111,7 @@ int getword(FILE *fp, char *str, int len
 	assert(fp != NULL);
 	
 	int i = 0;
-	char ch;
+	int ch;
 	while ( (ch = fgetc(fp)) != EOF ) {
 		if ( isspace(ch) ) {
 			if ( i > 0 ) break; // End of word
---
 src/parser.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

--- mesh2hmap.orig/src/parser.c
+++ mesh2hmap/src/parser.c
@@ -37,7 +37,7 @@ void grab_vertices(FILE *fp, mesh_t *m) 
 	int length = 30;
 	char str[length+1]; // plus 1 for the '\0' char
 	int index = 0;
-	char ch;
+	int ch;
 
 	while( (ch = fgetc(fp)) != EOF && (ch != '}') ) {
 		// # = comment - chew to newline
@@ -73,7 +73,7 @@ void grab_polys(FILE *fp, mesh_t *m, int
 	int length = 30;
 	char str[length+1]; // plus 1 for the '\0' char
 	int index = 0;
-	char ch;
+	int ch;
 
 
 	while( (ch = fgetc(fp)) != ']' && (ch != '}') && (ch != EOF)) {
@@ -266,7 +266,7 @@ void vrml2mesh(mesh_t *mesh, const char 
 	// parser searches for 'coord', then 'point', then 'index',
 	// then 'coord', then back to 'coord'.
 
-	int i = 0; char ch;
+	int i = 0, ch;
 	int space;
 	while ( (ch = fgetc(fp)) != EOF ) {
 		if ( ch != current[i] ) { // no match, reset
@@ -384,7 +384,7 @@ void native2mesh(mesh_t *mesh, const cha
 	int length = 30;
 	char str[length+1]; // plus 1 for the '\0' char
 	int index = 0;
-	char ch;
+	int ch;
 
 
 	while( (ch = fgetc(fp)) != EOF ) {
---
 src/mesh2hmap.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- mesh2hmap.orig/src/mesh2hmap.c
+++ mesh2hmap/src/mesh2hmap.c
@@ -59,7 +59,7 @@ void get_grid_height(hmap_t *hmap, const
 	n[2] = (verts[1][0]-verts[0][0])*(verts[2][1]-verts[0][1]) -
 		(verts[1][1]-verts[0][1])*(verts[2][0]-verts[0][0]);
 
-	if ( n[2] == 0.0 ) { 
+	if ( fabs(n[2]) < 0.000001 ) { 
 		// Plane is vertical. Don't worry about it. If there
 		// is a grid point on this plane then it lies on the
 		// edge of another polygon that connects to this
---
 src/matrix.c    |   33 +++++++++++++++++++++++++++++++++
 src/mesh2hmap.c |   12 ++++++++----
 2 files changed, 41 insertions(+), 4 deletions(-)

--- mesh2hmap.orig/src/mesh2hmap.c
+++ mesh2hmap/src/mesh2hmap.c
@@ -22,6 +22,7 @@
 #include <math.h> // fmax, fmin, ceilf, floorf
 #include <stdlib.h> // malloc, free, exit
 #include <stdio.h> // perror()
+#include <errno.h>
 
 #include "mesh2hmap.h" // mesh_t, hmap_t
 #include "matrix.h" // fmatrix_create(), fmatrix_free, fmatrix_resize
@@ -169,10 +170,9 @@ void mesh2hmap(hmap_t *hmap, const mesh_
 	if (hmap->rows > 0) y_m_pix = y_max/hmap->rows;
 	else hmap->rows = ceilf(y_max/y_m_pix);
 
-	// limit matrix to 40Mb
-	if ( hmap->cols*hmap->rows > 10*1024*1024) {
-		perror("Error: requested image resolution too large. "
-		     "Maximum is 40Mb");
+	if ((unsigned long long)hmap->cols * (unsigned long long)hmap->rows > (0xFFFFFFFFull/sizeof(float))) {
+		errno = ENOMEM;
+		perror("Error: requested image resolution too large");
 		exit(1);
 	}
 	hmap->map = fmatrix_create(hmap->rows,hmap->cols);
@@ -182,6 +182,10 @@ void mesh2hmap(hmap_t *hmap, const mesh_
 	sub_grid_t sub_grid;
 	sub_grid.grid_row=(float*) malloc((size_t) (hmap->rows)*sizeof(float));
 	sub_grid.grid_col=(float*) malloc((size_t) (hmap->cols)*sizeof(float));
+	if (!sub_grid.grid_row || !sub_grid.grid_col) {
+		fprintf(stderr, "Grid allocation: Out of memory\n");
+		exit(1);
+	}
 
 
 	int j;
--- mesh2hmap.orig/src/matrix.c
+++ mesh2hmap/src/matrix.c
@@ -20,10 +20,17 @@
 
 #include <stdlib.h> // malloc, realloc, free
 #include <assert.h> // assert
+#include <stdio.h>
 
 #include "matrix.h"
 
 
+static void matrix_oom(void)
+{
+	fprintf(stderr, "Matrix allocation: Out of memory\n");
+	exit(1);
+}
+
 // ********************************************************************
 // FLOATING POINT MATRIX
 // ********************************************************************
@@ -33,7 +40,11 @@ fmatrix_t fmatrix_create(const int rows,
 	assert(cols >= 1);
 	
         fmatrix_t m = (float**) malloc((size_t) (rows)*sizeof(float*));
+	if (!m)
+		matrix_oom();
 	m[0] = (float*) malloc((size_t) (rows*cols)*sizeof(float));
+	if (!m[0])
+		matrix_oom();
 	int i; for(i = 1; i < rows; i++) m[i] = m[i-1] + cols;
 
         return m;
@@ -45,7 +56,11 @@ void fmatrix_resize(fmatrix_t *m, const 
 	assert(cols >= 1);
 
 	*m = (float**) realloc(*m, (size_t) rows*sizeof(float*));
+	if (!*m)
+		matrix_oom();
 	(*m)[0] = (float*) realloc((*m)[0],(size_t) (rows*cols)*sizeof(float));
+	if (!((*m)[0]))
+		matrix_oom();
 	int i; for(i = 1; i < rows; i++) (*m)[i] = (*m)[i-1] + cols;
 }
 
@@ -66,7 +81,11 @@ imatrix_t imatrix_create(const int rows,
 	assert(cols >= 1);
 	
         imatrix_t m = (int**) malloc((size_t) (rows)*sizeof(int*));
+	if (!m)
+		matrix_oom();
 	m[0] = (int*) malloc((size_t) (rows*cols)*sizeof(int));
+	if (!m[0])
+		matrix_oom();
 	int i; for(i = 1; i < rows; i++) m[i] = m[i-1] + cols;
 
         return m;
@@ -79,7 +98,11 @@ imatrix_t imatrix_resize(imatrix_t m, co
 	assert(cols >= 1);
 
 	m = (int**) realloc(m, (size_t) rows*sizeof(int*));
+	if (!m)
+		matrix_oom();
 	m[0] = (int*) realloc(m[0], (size_t) (rows*cols)*sizeof(int));
+	if (!m[0])
+		matrix_oom();
 	int i; for(i = 1; i < rows; i++) m[i] = m[i-1] + cols;
 
 	return m;
@@ -102,8 +125,12 @@ irowarray_t irowarray_create(const int r
 	assert(cols >= 1);
 	
 	irowarray_t r = (int**) malloc((size_t) rows*sizeof(int*));
+	if (!r)
+		matrix_oom();
 	int i; for(i = 0; i < rows; i++) {
 		r[i] = (int*) malloc((size_t) cols*sizeof(int));
+		if (!r[i])
+			matrix_oom();
 	}
 	return r;
 }
@@ -116,6 +143,8 @@ void irowarray_resize_row(irowarray_t r,
 	assert(r != NULL);
 
 	r[row] = (int*) realloc(r[row], (size_t) new_col*sizeof(int));
+	if (!r[row])
+		matrix_oom();
 }
 
 void irowarray_add_rows(irowarray_t *r, const int old_rows, 
@@ -125,8 +154,12 @@ void irowarray_add_rows(irowarray_t *r, 
 	assert(*r != NULL);
 	
 	*r = (irowarray_t) realloc(*r, (size_t) new_rows*sizeof(int*));
+	if (!(*r))
+		matrix_oom();
 	int i; for(i = old_rows; i < new_rows; i++) {
 		(*r)[i] = (int*) malloc((size_t) cols*sizeof(int));
+		if (!((*r)[i]))
+			matrix_oom();
 	}
 }
 
------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image 
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
Emc-users mailing list
Emc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/emc-users

Reply via email to