PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/brlcad/tags/rel-7-0/src/off/off-g.c

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
C | 225 lines | 148 code | 47 blank | 30 comment | 35 complexity | 959058760f537e98df1b631ef73c459e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, Apache-2.0, AGPL-3.0, LGPL-3.0, GPL-3.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, 0BSD, BSD-3-Clause
  1. /*
  2. * O F F - G . C
  3. *
  4. * Program to convert from Digital Equipment Corporation's OFF
  5. * (Object File Format) to BRL-CAD NMG objects.
  6. * Inspired by Mike Markowski's jack-g Jack to NMG converter.
  7. *
  8. * Author -
  9. * Glenn Edward Durfee
  10. *
  11. * Source -
  12. * The U.S. Army Research Laboratory
  13. * Aberdeen Proving Ground, Maryland 21005-5066
  14. *
  15. * Distribution Status -
  16. * Public Domain, Distribution Unlimited
  17. *
  18. */
  19. #ifndef lint
  20. static const char RCSid[] = "@(#)$Header$ (BRL)";
  21. #endif
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "machine.h"
  25. #include "bu.h"
  26. #include "vmath.h"
  27. #include "bn.h"
  28. #include "nmg.h"
  29. #include "raytrace.h"
  30. #include "rtgeom.h"
  31. #include "wdb.h"
  32. static struct bn_tol tol;
  33. /*
  34. * R E A D _ F A C E S
  35. *
  36. * Reads the geometry from the the geometry file and creates the appropriate
  37. * vertices and faces.
  38. */
  39. int read_faces(struct model *m, FILE *fgeom)
  40. {
  41. int nverts, nfaces, nedges;
  42. register int i, j, fail=0;
  43. fastf_t *pts;
  44. struct vertex **verts;
  45. struct faceuse **outfaceuses;
  46. struct nmgregion *r;
  47. struct shell *s;
  48. /* Get numbers of vertices and faces, and grab the appropriate amount of memory */
  49. if (fscanf(fgeom, "%d %d %d", &nverts, &nfaces, &nedges) != 3)
  50. rt_bomb("Cannot read number of vertices, faces, edges.\n");
  51. pts = (fastf_t *) rt_malloc(sizeof(fastf_t) * 3 * nverts, "points list");
  52. verts = (struct vertex **) rt_malloc(sizeof(struct vertex *) * nverts, "vertices");
  53. outfaceuses = (struct faceuse **) rt_malloc(sizeof(struct faceuse *) * nfaces, "faceuses");
  54. /* Read in vertex geometry, store in geometry list */
  55. for (i = 0; i < nverts; i++) {
  56. if (fscanf(fgeom, "%lf %lf %lf", &pts[3*i], &pts[3*i+1], &pts[3*i+2]) != 3)
  57. rt_bomb("Not enough data points in geometry file.\n");
  58. verts[i] = (struct vertex *) 0;
  59. fscanf(fgeom, "%*[^\n]");
  60. }
  61. r = nmg_mrsv(m); /* Make region, empty shell, vertex. */
  62. s = RT_LIST_FIRST(shell, &r->s_hd);
  63. for (i = 0; i < nfaces; i++) { /* Read in each of the faces */
  64. struct vertex **vlist;
  65. int *pinds;
  66. if (fscanf(fgeom, "%d", &nedges) != 1) {
  67. fprintf(stderr, "Not enough faces in geometry file.\n");
  68. exit(1);
  69. }
  70. /* Grab memory for list for this face. */
  71. vlist = (struct vertex **) rt_malloc(sizeof(struct vertex *) * nedges, "vertex list");
  72. pinds = (int *) rt_malloc(sizeof(int) * nedges, "point indicies");
  73. for (j = 0; j < nedges; j++) { /* Read list of point indicies. */
  74. if (fscanf(fgeom, "%d", &pinds[j]) != 1) {
  75. fprintf(stderr, "Not enough points on face.\n");
  76. exit(1);
  77. }
  78. vlist[j] = verts[pinds[j]-1];
  79. }
  80. outfaceuses[i] = nmg_cface(s, vlist, nedges); /* Create face. */
  81. NMG_CK_FACEUSE(outfaceuses[i]);
  82. for (j = 0; j < nedges; j++) /* Save (possibly) newly created vertex structs. */
  83. verts[pinds[j]-1] = vlist[j];
  84. fscanf(fgeom, "%*[^\n]");
  85. rt_free((char *)vlist, "vertext list");
  86. rt_free((char *)pinds, "point indicies");
  87. }
  88. for (i = 0; i < nverts; i++)
  89. if (verts[i] != 0)
  90. nmg_vertex_gv(verts[i], &pts[3*i]);
  91. else
  92. fprintf(stderr, "Warning: vertex %d unused.\n", i+1);
  93. for (i = 0; i < nfaces; i++) {
  94. plane_t pl;
  95. fprintf(stderr, "planeeqning face %d.\n", i);
  96. if( nmg_loop_plane_area( RT_LIST_FIRST( loopuse , &outfaceuses[i]->lu_hd ) , pl ) < 0.0 )
  97. fail = 1;
  98. else
  99. nmg_face_g( outfaceuses[i] , pl );
  100. }
  101. if (fail) return (-1);
  102. nmg_gluefaces(outfaceuses, nfaces, &tol);
  103. nmg_region_a(r, &tol);
  104. rt_free((char *)pts, "points list");
  105. return (0);
  106. }
  107. int off2nmg(FILE *fpin, struct rt_wdb *fpout)
  108. {
  109. char title[64], geom_fname[64];
  110. char rname[67], sname[67];
  111. char buf[200], buf2[200];
  112. FILE *fgeom;
  113. struct model *m;
  114. title[0] = geom_fname[0] = '\0';
  115. fgets(buf, sizeof(buf), fpin);
  116. while (!feof(fpin)) { /* Retrieve the important data */
  117. if (sscanf(buf, "name %[^\n]s", buf2) > 0)
  118. strncpy(title, buf2, sizeof(title));
  119. /* if (sscanf(buf, "author %[^\n]s", buf2) > 0)
  120. strncpy(author, buf2, sizeof(author));
  121. */ if (sscanf(buf, "geometry %[^\n]s", buf2) > 0) {
  122. char dtype[40], format[40];
  123. if (sscanf(buf2, "%s %s %s", dtype, format, geom_fname) != 3)
  124. rt_bomb("Incomplete geometry field in input file.");
  125. if (strcmp(dtype, "indexed_poly") != 0)
  126. rt_bomb("Unknown geometry data type. Must be \"indexed_poly\".");
  127. }
  128. fgets(buf, sizeof(buf), fpin);
  129. }
  130. if (strlen(title) < (unsigned)1)
  131. fprintf(stderr, "Warning: no title\n");
  132. if (strlen(geom_fname) < (unsigned)1)
  133. rt_bomb("Error: no geometry filename.");
  134. if ((fgeom = fopen(geom_fname, "r")) == NULL) {
  135. fprintf(stderr, "off2nmg: cannot open %s (geometry description) for reading\n",
  136. geom_fname);
  137. exit(1);
  138. }
  139. m = nmg_mm();
  140. read_faces(m, fgeom);
  141. fclose(fgeom);
  142. strcpy(sname, "s."); strcat(sname, title);
  143. strcpy(rname, "r."); strcat(rname, title);
  144. mk_id(fpout, title);
  145. mk_nmg(fpout, sname, m);
  146. mk_comb1(fpout, rname, sname, 1);
  147. nmg_km(m);
  148. return (0);
  149. }
  150. int main(int argc, char **argv)
  151. {
  152. FILE *fpin;
  153. struct rt_wdb *fpout;
  154. tol.magic = RT_TOL_MAGIC; /* Copied from proc-db/nmgmodel.c */
  155. tol.dist = 0.01;
  156. tol.dist_sq = 0.01 * 0.01;
  157. tol.perp = 0.001;
  158. tol.para = 0.999;
  159. /* Get filenames and open the files. */
  160. if (argc != 3) {
  161. fprintf(stderr, "Usage: off-g file.off file.g\n");
  162. return 2;
  163. }
  164. if ((fpin = fopen(argv[1], "rt")) == NULL) {
  165. fprintf(stderr, "%s: cannot open %s for reading\n",
  166. argv[0], argv[1]);
  167. return (1);
  168. }
  169. if ((fpout = wdb_fopen(argv[2])) == NULL) {
  170. fprintf(stderr, "%s: cannot create %s\n",
  171. argv[0], argv[2]);
  172. return (1);
  173. }
  174. off2nmg(fpin, fpout);
  175. fclose(fpin);
  176. wdb_close(fpout);
  177. return (0);
  178. }