Пример #1
0
/* returns 1 if read was unsuccesful, 2 if graph is empty, 0 otherwise */
int read(char *cmd)
{
  char *past_file = curr_file;
  if(strcmp("read", cmd) < 0){
    cmd = cmd + 5;
  } else {
    cmd = cmd + 2;
  }
  curr_file = strdup(cmd);

  if(fopen(curr_file, "r") == NULL){/* if file doesn't exit */
    free(curr_file);
    if(past_file != NULL){
      curr_file = strdup(past_file);
    } else {
      curr_file = NULL;
    }
    return 1;
  }

  /* parsing vertices */
  if(fgetc(fopen(curr_file, "r")) != '#'){/* if file isn't well formed */
    free(curr_file);                      /* NOTE: this isn't a very good test */
    if(past_file != NULL){
      curr_file = strdup(past_file);
    } else {
      curr_file = NULL;
    }
    return 2;
  }

  /* builds the machinery to perform dj calculate here */
  f = fopen(curr_file, "r");
  fscanf(f, "#vertices\n");
  if(t != NULL)
    htbl_free(t);
  t = htbl_init(79);
  sll *dj_sll = NULL;
  dj_sll = parse_vertices(t, f);

  if(dj_sll == NULL){
    printf("   -this is the empty graph\n");
    printf("   -there is no shortest distance to calculate\n");
    return 0;
  }

  size = dj_sll->i;
  dj_arr = sll_to_djtab_arr(dj_sll, size);

  /* parsing edges */
  fscanf(f, "edges\n");
  adj_mat = (edge_cost *)malloc(sizeof(edge_cost) * size * size);
  parse_edges(adj_mat, t, size, f);

  return 0;
}
Пример #2
0
bool MEDITParser::parse(const std::string& filename) {
    std::ifstream fin(filename.c_str());
    if (!fin.is_open()) {
        std::stringstream err_msg;
        err_msg << "failed to open file \"" << filename << "\"";
        throw IOError(err_msg.str());
    }

    bool success = true;
    success &= parse_header(fin);
    if (!success) {
        throw IOError("Error parsing file header.");
    }

    const size_t LINE_SIZE = 256;
    char line[LINE_SIZE];
    while (!fin.eof()) {
        std::string elem_type;
        fin >> elem_type;
        if (elem_type.size() == 0) continue;
        if (elem_type[0] == '#') {
            fin.getline(line, LINE_SIZE);
            continue;
        }
        if (elem_type == "Vertices") {
            success &= parse_vertices(fin);
        } else if (elem_type == "Triangles") {
            m_vertex_per_face = 3;
            success &= parse_faces(fin);
        } else if (elem_type == "Quadrilaterals") {
            m_vertex_per_face = 4;
            success &= parse_faces(fin);
        } else if (elem_type == "Tetrahedra") {
            if (m_faces.size() == 0) {
                m_vertex_per_face = 3;
            } else if (m_vertex_per_face != 3){
                success = false;
            }
            m_vertex_per_voxel = 4;
            success &= parse_voxels(fin);
        } else if (elem_type == "Hexahedra") {
            if (m_faces.size() == 0) {
                m_vertex_per_face = 4;
            } else if (m_vertex_per_face != 4){
                success = false;
            }
            m_vertex_per_voxel = 8;
            success &= parse_voxels(fin);
        } else if (elem_type == "End") {
            break;
        } else {
            if (elem_type != "Edges" &&
                elem_type != "Corners" &&
                elem_type != "RequiredVertices" &&
                elem_type != "Ridges" &&
                elem_type != "RequiredEdges" &&
                elem_type != "Normals" &&
                elem_type != "Tangents" &&
                elem_type != "NormalAtVertices" &&
                elem_type != "NormalAtTriangleVertices" &&
                elem_type != "NormalAtQuadrilateralVertices" &&
                elem_type != "TangentAtEdges" &&
                elem_type != "End") {
                success = false;
            } else {
                std::cerr << "Warning: Skipping " << elem_type
                    << " field" << std::endl;
                success &= skip_field(fin);
            }
        }
        if (!success) {
            std::stringstream err_msg;
            err_msg << "Error parsing \"" << elem_type << "\" field in " <<
                filename;
            throw IOError(err_msg.str());
        }
    }

    fin.close();
    return success;
}
Пример #3
0
int main(int argc, char *argv[])
{
	if (argc < 2) {
		fprintf(stderr, "Usage: %s [VERTEX DATA]\n", argv[0]);
		exit(-1);
	}

	do_init();

	body = (struct shape *)malloc(sizeof(struct shape));
	if (body == NULL) {
		fprintf(stderr, "Cannot malloc the body\n");
		exit(-1);
	}

	parse_vertices(argv[1]);
	/* ================================================================================ */

	int fromvert = 0;
	double range;
	int vlist[MAX_VERTEX];
	int len;

	for (; fromvert < body->vertices; fromvert++) {

		printf("Making triangles around vertex %d:\n", fromvert);

		get_closest_range_from(fromvert, &range);
		//printf("Two closest ranges from vertex %d are:\n%f\n%f\n", fromvert, r1, r2);

		select_vertices_in_range_from(vlist, range, fromvert, &len);
		//printf("%d vertices in range %f from vertex %d:\n", len, r1, fromvert);

/*
		int i, j;
		for (i=0, j=vlist[i]; i < len; i++, j=vlist[i])
		//printf("[vertex: %d] %f %f %f\n", vlist[i], 
		//	body->vertex[j].x, body->vertex[j].y, body->vertex[j].z);
*/
		make_triangles_around(fromvert, vlist, len);
	}

	/* */ 
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(600, 480);
	glutCreateWindow("3D shape render");

	gl_init();

	glutDisplayFunc(gl_display_loop);
	glutReshapeFunc(gl_resize);
	glutIdleFunc(gl_display_loop);

	glutKeyboardFunc(gl_get_butts);
	glutSpecialFunc(gl_get_more_butts);

	glutMainLoop();
	/* ================================================================================ */

	return 0;
}