PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/brlcad/branches/STABLE/src/conv/off/off-g.c

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
C | 244 lines | 146 code | 48 blank | 50 comment | 36 complexity | 8c9ad16691e4e98bd6d343dce9e1e848 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. /* O F F - G . C
  2. * BRL-CAD
  3. *
  4. * Copyright (c) 2004-2012 United States Government as represented by
  5. * the U.S. Army Research Laboratory.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * version 2.1 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this file; see the file named COPYING for more
  18. * information.
  19. */
  20. /** @file off-g.c
  21. *
  22. * Program to convert from Digital Equipment Corporation's OFF
  23. * (Object File Format) to BRL-CAD NMG objects.
  24. * Inspired by Mike Markowski's jack-g Jack to NMG converter.
  25. *
  26. * Author -
  27. * Glenn Edward Durfee
  28. */
  29. #include "common.h"
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include "bu.h"
  34. #include "vmath.h"
  35. #include "bn.h"
  36. #include "nmg.h"
  37. #include "raytrace.h"
  38. #include "rtgeom.h"
  39. #include "wdb.h"
  40. static struct bn_tol tol;
  41. /*
  42. * R E A D _ F A C E S
  43. *
  44. * Reads the geometry from the the geometry file and creates the appropriate
  45. * vertices and faces.
  46. */
  47. int read_faces(struct model *m, FILE *fgeom)
  48. {
  49. int nverts, nfaces, nedges;
  50. int i, j, fail=0;
  51. fastf_t *pts;
  52. struct vertex **verts;
  53. struct faceuse **outfaceuses;
  54. struct nmgregion *r;
  55. struct shell *s;
  56. size_t ret;
  57. /* Get numbers of vertices and faces, and grab the appropriate amount of memory */
  58. if (fscanf(fgeom, "%d %d %d", &nverts, &nfaces, &nedges) != 3)
  59. bu_exit(1, "Cannot read number of vertices, faces, edges.\n");
  60. pts = (fastf_t *) bu_malloc(sizeof(fastf_t) * 3 * nverts, "points list");
  61. verts = (struct vertex **) bu_malloc(sizeof(struct vertex *) * nverts, "vertices");
  62. outfaceuses = (struct faceuse **) bu_malloc(sizeof(struct faceuse *) * nfaces, "faceuses");
  63. /* Read in vertex geometry, store in geometry list */
  64. for (i = 0; i < nverts; i++) {
  65. if (fscanf(fgeom, "%lf %lf %lf", &pts[3*i], &pts[3*i+1], &pts[3*i+2]) != 3)
  66. bu_exit(1, "Not enough data points in geometry file.\n");
  67. verts[i] = (struct vertex *) 0;
  68. ret = fscanf(fgeom, "%*[^\n]");
  69. if (ret > 0)
  70. bu_log("unknown parsing error\n");
  71. }
  72. r = nmg_mrsv(m); /* Make region, empty shell, vertex. */
  73. s = BU_LIST_FIRST(shell, &r->s_hd);
  74. for (i = 0; i < nfaces; i++) {
  75. /* Read in each of the faces */
  76. struct vertex **vlist;
  77. int *pinds;
  78. if (fscanf(fgeom, "%d", &nedges) != 1) {
  79. bu_exit(1, "Not enough faces in geometry file.\n");
  80. }
  81. /* Grab memory for list for this face. */
  82. vlist = (struct vertex **) bu_malloc(sizeof(struct vertex *) * nedges, "vertex list");
  83. pinds = (int *) bu_malloc(sizeof(int) * nedges, "point indicies");
  84. for (j = 0; j < nedges; j++) {
  85. /* Read list of point indicies. */
  86. if (fscanf(fgeom, "%d", &pinds[j]) != 1) {
  87. bu_exit(1, "Not enough points on face.\n");
  88. }
  89. vlist[j] = verts[pinds[j]-1];
  90. }
  91. outfaceuses[i] = nmg_cface(s, vlist, nedges); /* Create face. */
  92. NMG_CK_FACEUSE(outfaceuses[i]);
  93. for (j = 0; j < nedges; j++) /* Save (possibly) newly created vertex structs. */
  94. verts[pinds[j]-1] = vlist[j];
  95. ret = fscanf(fgeom, "%*[^\n]");
  96. if (ret > 0)
  97. bu_log("unknown parsing error\n");
  98. bu_free((char *)vlist, "vertext list");
  99. bu_free((char *)pinds, "point indicies");
  100. }
  101. for (i = 0; i < nverts; i++)
  102. if (verts[i] != 0)
  103. nmg_vertex_gv(verts[i], &pts[3*i]);
  104. else
  105. fprintf(stderr, "Warning: vertex %d unused.\n", i+1);
  106. for (i = 0; i < nfaces; i++) {
  107. plane_t pl;
  108. fprintf(stderr, "planeeqning face %d.\n", i);
  109. if ( nmg_loop_plane_area( BU_LIST_FIRST( loopuse, &outfaceuses[i]->lu_hd ), pl ) < 0.0 )
  110. fail = 1;
  111. else
  112. nmg_face_g( outfaceuses[i], pl );
  113. }
  114. if (fail) return -1;
  115. nmg_gluefaces(outfaceuses, nfaces, &tol);
  116. nmg_region_a(r, &tol);
  117. bu_free((char *)pts, "points list");
  118. return 0;
  119. }
  120. int off2nmg(FILE *fpin, struct rt_wdb *fpout)
  121. {
  122. char title[64], geom_fname[64];
  123. char rname[67], sname[67];
  124. char buf[200], buf2[200];
  125. FILE *fgeom;
  126. struct model *m;
  127. title[0] = geom_fname[0] = '\0';
  128. bu_fgets(buf, sizeof(buf), fpin);
  129. while (!feof(fpin)) {
  130. /* Retrieve the important data */
  131. if (sscanf(buf, "name %[^\n]s", buf2) > 0)
  132. bu_strlcpy(title, buf2, sizeof(title));
  133. if (sscanf(buf, "geometry %200[^\n]s", buf2) > 0) {
  134. char dtype[40], format[40];
  135. if (sscanf(buf2, "%40s %40s %64s", dtype, format, geom_fname) != 3)
  136. bu_exit(1, "Incomplete geometry field in input file.");
  137. if (!BU_STR_EQUAL(dtype, "indexed_poly"))
  138. bu_exit(1, "Unknown geometry data type. Must be \"indexed_poly\".");
  139. }
  140. bu_fgets(buf, sizeof(buf), fpin);
  141. }
  142. if (strlen(title) < (unsigned)1)
  143. fprintf(stderr, "Warning: no title\n");
  144. if (strlen(geom_fname) < (unsigned)1)
  145. bu_exit(1, "ERROR: no geometry filename given");
  146. if ((fgeom = fopen(geom_fname, "rb")) == NULL) {
  147. bu_exit(1, "off2nmg: cannot open %s (geometry description) for reading\n",
  148. geom_fname);
  149. }
  150. m = nmg_mm();
  151. read_faces(m, fgeom);
  152. fclose(fgeom);
  153. snprintf(sname, 67, "s.%s", title);
  154. snprintf(rname, 67, "r.%s", title);
  155. mk_id(fpout, title);
  156. mk_nmg(fpout, sname, m);
  157. mk_comb1(fpout, rname, sname, 1);
  158. nmg_km(m);
  159. return 0;
  160. }
  161. int main(int argc, char **argv)
  162. {
  163. FILE *fpin;
  164. struct rt_wdb *fpout;
  165. bu_setprogname(argv[0]);
  166. tol.magic = BN_TOL_MAGIC; /* Copied from proc-db/nmgmodel.c */
  167. tol.dist = 0.01;
  168. tol.dist_sq = 0.01 * 0.01;
  169. tol.perp = 0.001;
  170. tol.para = 0.999;
  171. /* Get filenames and open the files. */
  172. if (argc != 3) {
  173. bu_exit(2, "Usage: off-g file.off file.g\n");
  174. }
  175. if ((fpin = fopen(argv[1], "r")) == NULL) {
  176. bu_exit(1, "%s: cannot open %s for reading\n",
  177. argv[0], argv[1]);
  178. }
  179. if ((fpout = wdb_fopen(argv[2])) == NULL) {
  180. bu_exit(1, "%s: cannot create %s\n",
  181. argv[0], argv[2]);
  182. }
  183. off2nmg(fpin, fpout);
  184. fclose(fpin);
  185. wdb_close(fpout);
  186. return 0;
  187. }
  188. /*
  189. * Local Variables:
  190. * mode: C
  191. * tab-width: 8
  192. * indent-tabs-mode: t
  193. * c-file-style: "stroustrup"
  194. * End:
  195. * ex: shiftwidth=4 tabstop=8
  196. */