1 /* tri2obj.c -- read in a .ele/.node file pair generated by the triangle
2 * program and output a simple Wavefront .obj file.
4 * Written by Curtis Olson, started October 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * (Log is kept at end of this file)
33 int nodecount, tricount;
34 double nodes[MAX_NODES][3];
35 int tris[MAX_TRIS][3];
36 int new_tris[MAX_TRIS][3];
39 /* Initialize a new mesh structure */
40 void triload(char *basename) {
41 char nodename[256], elename[256];
43 int dim, junk1, junk2;
46 strcpy(nodename, basename);
47 strcat(nodename, ".node");
48 strcpy(elename, basename);
49 strcat(elename, ".ele");
51 printf("Loading node file: %s ...\n", nodename);
52 if ( (node = fopen(nodename, "r")) == NULL ) {
53 printf("Cannot open file '%s'\n", nodename);
57 fscanf(node, "%d %d %d %d", &nodecount, &dim, &junk1, &junk2);
59 if ( nodecount > MAX_NODES - 1 ) {
60 printf("Error, too many nodes, need to increase array size\n");
63 printf(" Expecting %d nodes\n", nodecount);
66 for ( i = 1; i <= nodecount; i++ ) {
67 fscanf(node, "%d %lf %lf %lf %d\n", &junk1,
68 &nodes[i][0], &nodes[i][1], &nodes[i][2], &junk2);
69 /* printf("%d %.2f %.2f %.2f\n",
70 junk1, nodes[i][0], nodes[i][1], nodes[i][2]); */
75 printf("Loading element file: %s ...\n", elename);
76 if ( (ele = fopen(elename, "r")) == NULL ) {
77 printf("Cannot open file '%s'\n", elename);
81 fscanf(ele, "%d %d %d", &tricount, &junk1, &junk2);
83 if ( tricount > MAX_TRIS - 1 ) {
84 printf("Error, too many elements, need to increase array size\n");
87 printf(" Expecting %d elements\n", tricount);
90 for ( i = 1; i <= tricount; i++ ) {
91 fscanf(ele, "%d %d %d %d\n", &junk1,
92 &tris[i][0], &tris[i][1], &tris[i][2]);
93 /* printf("%d %d %d %d\n", junk1, tris[i][0], tris[i][1], tris[i][2]);*/
100 /* dump in WaveFront .obj format */
101 void dump_obj(char *basename) {
106 strcpy(objname, basename);
107 strcat(objname, ".obj");
109 printf("Dumping to file: %s ...\n", objname);
111 obj = fopen(objname, "w");
114 for ( i = 1; i <= nodecount; i++ ) {
115 fprintf(obj, "v %.2f %.2f %.2f\n",
116 nodes[i][0], nodes[i][1], nodes[i][2]);
120 for ( i = 1; i <= tricount; i++ ) {
121 fprintf(obj, "f %d %d %d\n", tris[i][0], tris[i][1], tris[i][2]);
127 int main(int argc, char **argv) {
130 strcpy(basename, argv[1]);
132 /* load the input data files */
135 /* dump in WaveFront .obj format */
143 /* Revision 1.1 1997/10/29 23:05:15 curt