Esempio n. 1
0
int main()
{
  std::cerr << "### There should be three errors following this line...\n";
  assert(! read("data/read_test/bug_1.xyz"));
  assert(! read("data/read_test/bug_2.xyz"));
  assert(! read("data/read_test/bug_3.xyz"));
  std::cerr << "### ... Done. Now, there should not be any error.\n";

  assert(read("data/read_test/ok_1.xyz"));
  assert(read("data/read_test/ok_2.xyz"));
  assert(read("data/read_test/ok_3.xyz"));

  std::vector<PointVectorPair> pv_pairs;

  read("data/read_test/ok_2.xyz", pv_pairs);
  assert(pv_pairs.size() == 4);
  assert(pv_pairs[0] == std::make_pair(Point_3(2,3,4), Vector_3(4,4,2)));
  assert(pv_pairs[1] == std::make_pair(Point_3(3,4,6), Vector_3(0,0,0))); 
  assert(pv_pairs[2] == std::make_pair(Point_3(3,6,7), Vector_3(3,5,6)));
  assert(pv_pairs[3] == std::make_pair(Point_3(1,3,4), Vector_3(4,6,8)));

  pv_pairs.clear();

  assert(read_off("data/read_test/ok_1.off", pv_pairs));
  assert(pv_pairs.size() == 4);
  assert(pv_pairs[0] == std::make_pair(Point_3(3,2,0), Vector_3(1,2,3)));
  assert(pv_pairs[1] == std::make_pair(Point_3(1,2,3), Vector_3(0,0,0))); 
  assert(pv_pairs[2] == std::make_pair(Point_3(4,5,6), Vector_3(0,0,0)));
  assert(pv_pairs[3] == std::make_pair(Point_3(7,8,9), Vector_3(0,0,0)));

  pv_pairs.clear ();
  assert(read_ply("data/read_test/simple.ply", pv_pairs));
  assert(pv_pairs[0] == std::make_pair(Point_3(1,1,1), Vector_3(2,2,2)));
  assert(pv_pairs[1] == std::make_pair(Point_3(3,3,3), Vector_3(4,4,4)));
  assert(pv_pairs[2] == std::make_pair(Point_3(5,5,5), Vector_3(6,6,6)));

  pv_pairs.clear ();
  assert(read_ply("data/read_test/simple_ascii.ply", pv_pairs));
  assert(pv_pairs[0] == std::make_pair(Point_3(1,1,1), Vector_3(2,2,2)));
  assert(pv_pairs[1] == std::make_pair(Point_3(3,3,3), Vector_3(4,4,4)));
  assert(pv_pairs[2] == std::make_pair(Point_3(5,5,5), Vector_3(6,6,6)));

  pv_pairs.clear ();
  assert(read_ply("data/read_test/simple_with_flag.ply", pv_pairs));
  assert(pv_pairs[0] == std::make_pair(Point_3(1,1,1), Vector_3(2,2,2)));
  assert(pv_pairs[1] == std::make_pair(Point_3(3,3,3), Vector_3(4,4,4)));
  assert(pv_pairs[2] == std::make_pair(Point_3(5,5,5), Vector_3(6,6,6)));

  return 0;
}  
Esempio n. 2
0
returnPLY loadPly(FILE* f) 
{

  PlyFile* input;

  // get the ply structure and open the file
  input = read_ply(f);

  // read in the data
  store_ply(input, 
	    &vertices, &faces, 
	    &vertexcount, &facecount,
	    &vertexnormals, &facenormals);

  // close the file
  close_ply(input);
  printf("vertexcount: %d\n", vertexcount);

  /*find_center(cx, cy, cz, x_min, x_max, 
	      y_min, y_max, z_min, z_max); 
  printf("geometry center = %f %f %f \n", cx, cy, cz); 
  printf("geometry bound = x: %f %f y: %f %f z: %f %f\n", 
	 x_min, x_max, y_min, y_max, z_min, z_max); */

  returnPLY _returnPLY(vertices,faces,vertexcount,facecount,vertexnormals, facenormals);

  return _returnPLY;
}
Esempio n. 3
0
read_file()
{
  int i,j;
  int elem_count;
  char *elem_name;

  /*** Read in the original PLY object ***/

  in_ply = read_ply (stdin);

  for (i = 0; i < in_ply->num_elem_types; i++) {

    /* prepare to read the i'th list of elements */
    elem_name = setup_element_read_ply (in_ply, i, &elem_count);

    if (equal_strings ("vertex", elem_name)) {

      /* create a vertex list to hold all the vertices */
      vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count);
      nverts = elem_count;

      /* set up for getting vertex elements */

      setup_property_ply (in_ply, &vert_props[0]);
      setup_property_ply (in_ply, &vert_props[1]);
      setup_property_ply (in_ply, &vert_props[2]);
      vert_other = get_other_properties_ply (in_ply, 
					     offsetof(Vertex,other_props));

      /* grab all the vertex elements */
      for (j = 0; j < elem_count; j++) {
        vlist[j] = (Vertex *) malloc (sizeof (Vertex));
        get_element_ply (in_ply, (void *) vlist[j]);
      }
    }
    else
      get_other_element_ply (in_ply);
  }

  close_ply (in_ply);
}
Esempio n. 4
0
int open_ply(const char* filename)
{
	/* bounding box */
	int I;
	struct triangle_t* t,**cursor;
	float x1=+1e18f,x2=-1e18f;
	float y1=+1e18f,y2=-1e18f;
	float z1=+1e18f,z2=-1e18f;

	PlyProperty vert_prop[3]={
		{    "x", Float32, Float32,  0, 0, 0, 0, 0},
		{    "y", Float32, Float32,  4, 0, 0, 0, 0},
		{    "z", Float32, Float32,  8, 0, 0, 0, 0}
	};

	PlyProperty   face_prop	={"vertex_indices", Int32, Int32, 4,PLY_LIST, Int32, Int32, 0};

	PlyProperty** props=NULL;
	int numvertices,numproperties,numtriangles,numstrips,numfaces;
	int clockwise;
	struct {int nverts,*verts;} face;
	int h,k;
	float maxdim;
	PlyFile* ply=0;

	FILE* file = fopen( filename, "rb" );

	if (!file)
		return 0;

	ply = read_ply(file);

	if (!ply)
	{
		fclose(file);
		return 0;
	}

	cursor=&(mesh.triangles);

	props = get_element_description_ply(ply, "vertex", &numvertices, &numproperties);
	
	mesh.vertices=(struct vertex_t*)malloc(sizeof(struct vertex_t)*numvertices);
	mesh.numvertices=numvertices;

	get_element_setup_ply(ply, "vertex", 3,vert_prop);
	
	for( k=0;k<numvertices; ++k) 
	{
		struct vertex_t* v=mesh.vertices+k;
		get_element_ply( ply, (void*)v);
		
		x1=min2(x1,v->x);x2=max2(x2,v->x);
		y1=min2(y1,v->y);y2=max2(y2,v->y);
		z1=min2(z1,v->z);z2=max2(z2,v->z);
	}

	/* normalize to unit box to [-1,+1],[-1,+1],[-1,+1] mantaining proportions */
	maxdim=max2(x2-x1,y2-y1);
	maxdim=max2(maxdim,z2-z1);

	for( k=0;k<numvertices; ++k) 
	{
		struct vertex_t* v=mesh.vertices+k;
		v->x=2*((v->x-x1)/maxdim-0.5f);
		v->y=2*((v->y-y1)/maxdim-0.5f);
		v->z=2*((v->z-z1)/maxdim-0.5f);
	}

	numtriangles=0;

	if (props = get_element_description_ply( ply, "face", &numfaces, &numproperties))
	{
		get_element_setup_ply( ply, "face", 1, &face_prop);
		
		for(h=0; h<numfaces; h++ ) 
		{
			get_element_ply( ply, (void*)&face);
						
			for(k=2; k<face.nverts;++k) 
			{
				t=(struct triangle_t*)malloc(sizeof(struct triangle_t));
				t->i0=face.verts[0  ];
				t->i1=face.verts[k-1];
				t->i2=face.verts[k  ];

				t->next=0;
				(*cursor)=t;
				cursor=&(t->next);
			}
			
			free(face.verts);
		}
	}
	else
	{
		props = get_element_description_ply( ply, "tristrips", &numstrips, &numproperties);
		
		get_element_setup_ply(ply,"tristrips",1,&face_prop);

		for(k=0; k<numstrips;++k ) 
		{
			get_element_ply( ply, (void*)&face);
			clockwise=1;

			for (I=2;I< face.nverts; I++) 
			{
				if (face.verts[I] == -1) 
					{
						I += 2;
						clockwise = 1;
				}
				else 
				{			
					t=(struct triangle_t*)malloc(sizeof(struct triangle_t));
					
					t->i0=face.verts[I-2];
					t->i1=face.verts[I-1];
					t->i2=face.verts[I  ];

					if (!clockwise) swap_int(t->i1,t->i2);

					t->next=0;
					(*cursor)=t;
					cursor=&(t->next);
					clockwise = 1-clockwise;
				}
			}
			
			free(face.verts);
		}
	}
	
	//close_ply( ply );
	fclose(file);

	set_normals();
	return 1; /* ok */
}
Esempio n. 5
0
void
read_file(void)
{
  int i,j;
  int elem_count;
  char *elem_name;
  PlyFile *in_ply;

  /*** Read in the original PLY object ***/

  in_ply  = read_ply (stdin);

  for (i = 0; i < in_ply->num_elem_types; i++) {

    /* prepare to read the i'th list of elements */
    elem_name = setup_element_read_ply (in_ply, i, &elem_count);

    if (equal_strings ("vertex", elem_name)) {

      /* create a vertex list to hold all the vertices */
      vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count);
      nverts = elem_count;

      /* set up for getting vertex elements */

      setup_property_ply (in_ply, &vert_props[0]);
      setup_property_ply (in_ply, &vert_props[1]);
      setup_property_ply (in_ply, &vert_props[2]);

      for (j = 0; j < in_ply->elems[i]->nprops; j++) {
	PlyProperty *prop;
	prop = in_ply->elems[i]->props[j];
	if (equal_strings ("r", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[3]);
	  per_vertex_color = 1;
	}
	if (equal_strings ("g", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[4]);
	  per_vertex_color = 1;
	}
	if (equal_strings ("b", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[5]);
	  per_vertex_color = 1;
	}
	if (equal_strings ("nx", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[6]);
	  has_normals = 1;
	}
	if (equal_strings ("ny", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[7]);
	  has_normals = 1;
	}
	if (equal_strings ("nz", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[8]);
	  has_normals = 1;
	}
      }

      vert_other = get_other_properties_ply (in_ply, 
					     offsetof(Vertex,other_props));

      /* grab all the vertex elements */
      for (j = 0; j < elem_count; j++) {
        vlist[j] = (Vertex *) malloc (sizeof (Vertex));
	vlist[j]->r = 1;
	vlist[j]->g = 1;
	vlist[j]->b = 1;
        get_element_ply (in_ply, (void *) vlist[j]);
      }
    }
    else if (equal_strings ("face", elem_name)) {

      /* create a list to hold all the face elements */
      flist = (Face **) malloc (sizeof (Face *) * elem_count);
      nfaces = elem_count;

      /* set up for getting face elements */

      setup_property_ply (in_ply, &face_props[0]);
      face_other = get_other_properties_ply (in_ply, 
					     offsetof(Face,other_props));

      /* grab all the face elements */
      for (j = 0; j < elem_count; j++) {
        flist[j] = (Face *) malloc (sizeof (Face));
        get_element_ply (in_ply, (void *) flist[j]);
      }
    }
    else
      get_other_element_ply (in_ply);
  }

  close_ply (in_ply);
  free_ply (in_ply);
}
Esempio n. 6
0
main(int argc, char *argv[])
{
  int i,j,k;
  PlyFile *in_ply;
  PlyFile *out_ply;
  int nelems;
  char **elist;
  int file_type;
  float version;
  int nprops;
  int num_elems;
  PlyProperty **plist;
  PlyProperty **plist_copy;
  PlyProperty *prop;
  char *elem_name;
  char *data;
  int offset;
  int *offset_list;
  int **lists;
  int *list_count;
  char **list_ptr;
  int verbose_flag = 0;

#ifndef WRITE_ASCII
#ifndef WRITE_BINARY

  fprintf (stderr, "'%s' compiled incorrectly.\n", argv[0]);
  fprintf (stderr,
           "Must have WRITE_ASCII or WRITE_BINARY defined during compile.\n");
  exit (-1);

#endif
#endif

  /* maybe print out help message */

#ifdef WRITE_ASCII
  if (argc > 2 || (argc == 2 && !equal_strings (argv[1], "-p"))) {
    fprintf (stderr, "usage: %s [flags] <infile >outfile\n", argv[0]);
    fprintf (stderr, "          -p (print element labels)\n");
    exit (0);
  }
#endif

#ifdef WRITE_BINARY
  if (argc > 1) {
    fprintf (stderr, "usage: %s <infile >outfile\n", argv[0]);
    exit (0);
  }
#endif

  if (argc == 2 && equal_strings (argv[1], "-p"))
    verbose_flag = 1;

  /* open the input and output files */

  in_ply  = read_ply (stdin);
  elist = get_element_list_ply (in_ply, &nelems);

#ifdef WRITE_ASCII
  out_ply = write_ply (stdout, nelems, elist, PLY_ASCII);
#endif

#ifdef WRITE_BINARY
  out_ply = write_ply (stdout, nelems, elist, PLY_BINARY_BE);
#endif

  /* allocate space for various lists that keep track of the elements */

  plist_copy = (PlyProperty **) malloc (sizeof (PlyProperty *) * nelems);
  offset_list = (int *) malloc (sizeof (int) * nelems);
  lists = (int **) malloc (sizeof (int *) * nelems);
  list_count = (int *) malloc (sizeof (int));

  /* go through each kind of element that we learned is in the file */
  /* and come up with a list that has offsets for all properties */

  for (i = 0; i < nelems; i++) {

    /* get the description of the element */
    elem_name = elist[i];
    plist = get_element_description_ply(in_ply, elem_name, &num_elems, &nprops);

    /* make room for a list of the lists in an element, so that */
    /* we can later easily free up the space created by malloc'ed lists */

    list_count[i] = 0;
    lists[i] = (int *) malloc (sizeof (int) * nprops);

    /* set up pointers into data */

    offset = 0;
    for (j = 0; j < nprops; j++) {
      plist[j]->offset = offset;
      offset += 8;
      if (plist[j]->is_list) {
        plist[j]->count_offset = offset;
        lists[i][list_count[i]] = offset - 8;
        list_count[i]++;
        offset += 8;
      }
    }

    offset_list[i] = offset;

    /* copy the property list */

    plist_copy[i] = (PlyProperty *) malloc (sizeof (PlyProperty) * nprops);
    prop = plist_copy[i];

    for (j = 0; j < nprops; j++) {
      prop->name = plist[j]->name;
      prop->external_type = plist[j]->external_type;
      prop->internal_type = plist[j]->external_type;
      prop->offset = plist[j]->offset;
      prop->is_list = plist[j]->is_list;
      prop->count_external = plist[j]->count_external;
      prop->count_internal = plist[j]->count_external;
      prop->count_offset = plist[j]->count_offset;
      prop++;
    }

    element_layout_ply (out_ply, elem_name, num_elems, nprops, plist_copy[i]);
  }

  /* copy the comments and obj_info */

  copy_comments_ply (out_ply, in_ply);
  copy_obj_info_ply (out_ply, in_ply);

  /* finish the header for the output file */
  header_complete_ply (out_ply);

  /* copy all the element information */

  for (i = 0; i < nelems; i++) {

    /* get the description of the element */
    elem_name = elist[i];
    plist = get_element_description_ply(in_ply, elem_name, &num_elems, &nprops);

    /* allocate space for an element */
    data = (char *) malloc (8 * offset_list[i]);

    /* set up for getting elements */
    get_element_setup_ply (in_ply, elem_name, nprops, plist_copy[i]);
    put_element_setup_ply (out_ply, elem_name);

    /* possibly print out name of element */
    if (verbose_flag)
      fprintf (out_ply->fp, "%s:\n", elem_name);

    /* copy all the elements */

    if (list_count[i]) {
      /* need to free the lists */
      for (j = 0; j < num_elems; j++) {
        get_element_ply (in_ply, (void *) data);
        put_element_ply (out_ply, (void *) data);
        for (k = 0; k < list_count[i]; k++) {
          list_ptr = (char **) (data + lists[i][k]);
          free (*list_ptr);
        }
      }
    }
    else {
      /* no lists */
      for (j = 0; j < num_elems; j++) {
        get_element_ply (in_ply, (void *) data);
        put_element_ply (out_ply, (void *) data);
      }
    }

  }

  /* close the PLY files */

  close_ply (in_ply);
  close_ply (out_ply);
}
Esempio n. 7
0
int _tmain(int argc, _TCHAR* argv[])
{
#ifdef WIN32
	_fmode = _O_BINARY;
#endif
	printf("This is QSplatMake version %s.\n", QSPLATMAKE_VERSION);

	// Parse command-line params
	//strncasecmp(char*s1,char*s2,size_t n):compare first n chars of s1 and s2
	//ignore case and return 0 if equal else if s1>s2 return >0 else if s1<s2 return <0;
	if ((argc < 4) ||
		!strncasecmp(argv[1], "-h", 2) ||
		!strncasecmp(argv[1], "--h", 3))
		usage(argv[0]);
	bool is_pointfile = false;
	int i = strncasecmp(argv[1], "point" , 5);
	if(!strncasecmp(argv[1], "point" , 5))
		is_pointfile = true;
	const char *infilename = argv[2];
	const char *outfilename = argv[3];
	float threshold = 0;
	printf("argv2:%s\n",infilename);
	printf("argv3:%s\n",outfilename);

	if (argc >= 5 && !strncasecmp(argv[1], "-m", 2)) {
		threshold = atof(argv[2]);
		infilename = argv[3];
		outfilename = argv[4];
	}


	// Read the .ply file
	int numleaves, numfaces;
	face *faces;
	bool havecolor;
	QTree_Node *leaves;
	std::string comments;

	if (!read_ply(infilename, numleaves, leaves, numfaces, faces, havecolor, comments)) {
		fprintf(stderr, "Couldn't read input file %s\n", infilename);
		exit(1);
	}
	if (numleaves < 4) {
		fprintf(stderr, "Ummm...  That's an awfully small mesh you've got there...\n");
		exit(1);
	}
	if(!is_pointfile)
		if (numfaces < 4) {
			fprintf(stderr, "Ummm... I need a *mesh* as input.  That means triangles 'n stuff...\n");
			exit(1);
		}
	
	// Compute per-vertex normals
	if(!is_pointfile)
	{
		find_normals(numleaves, leaves, numfaces, faces);
		// Merge nodes
		merge_nodes(numleaves, leaves, numfaces, faces, havecolor, threshold);

		// Compute initial splat sizes
		find_splat_sizes(numleaves, leaves, numfaces, faces);
	}
	else
	{
		compute_splat_sizes(numleaves, leaves);
	}
	
	

	// Don't need face data any more
	delete [] faces;

	// Initialize the tree
	QTree qt(numleaves, leaves, havecolor);

	// Build the tree...
	qt.BuildTree();

	// ... and write it out
	qt.Write(outfilename, comments);
	system("PAUSE");
	return 0;
}
int main(int argc, char** argv) {
    parse_args(argc,argv);
    auto mesh = read_ply(filename_ply, flipface, normalizesize, flipyz);
    Serializer::write_json(mesh, filename_igl, false);
}
Esempio n. 9
0
bool
TrisetObject::loadPLY(QString flnm)
{
  m_position = Vec(0,0,0);
  m_scale = Vec(1,1,1);

  typedef struct Vertex {
    float x,y,z;
    float r,g,b;
    float nx,ny,nz;
    void *other_props;       /* other properties */
  } Vertex;

  typedef struct Face {
    unsigned char nverts;    /* number of vertex indices in list */
    int *verts;              /* vertex index list */
    void *other_props;       /* other properties */
  } Face;

  PlyProperty vert_props[] = { /* list of property information for a vertex */
    {"x", Float32, Float32, offsetof(Vertex,x), 0, 0, 0, 0},
    {"y", Float32, Float32, offsetof(Vertex,y), 0, 0, 0, 0},
    {"z", Float32, Float32, offsetof(Vertex,z), 0, 0, 0, 0},
    {"red", Float32, Float32, offsetof(Vertex,r), 0, 0, 0, 0},
    {"green", Float32, Float32, offsetof(Vertex,g), 0, 0, 0, 0},
    {"blue", Float32, Float32, offsetof(Vertex,b), 0, 0, 0, 0},
    {"nx", Float32, Float32, offsetof(Vertex,nx), 0, 0, 0, 0},
    {"ny", Float32, Float32, offsetof(Vertex,ny), 0, 0, 0, 0},
    {"nz", Float32, Float32, offsetof(Vertex,nz), 0, 0, 0, 0},
  };

  PlyProperty face_props[] = { /* list of property information for a face */
    {"vertex_indices", Int32, Int32, offsetof(Face,verts),
     1, Uint8, Uint8, offsetof(Face,nverts)},
  };


  /*** the PLY object ***/

  int nverts,nfaces;
  Vertex **vlist;
  Face **flist;

  PlyOtherProp *vert_other,*face_other;

  bool per_vertex_color = false;
  bool has_normals = false;

  int i,j;
  int elem_count;
  char *elem_name;
  PlyFile *in_ply;


  /*** Read in the original PLY object ***/
  FILE *fp = fopen(flnm.toAscii().data(), "rb");

  in_ply  = read_ply (fp);

  for (i = 0; i < in_ply->num_elem_types; i++) {

    /* prepare to read the i'th list of elements */
    elem_name = setup_element_read_ply (in_ply, i, &elem_count);


    if (equal_strings ("vertex", elem_name)) {

      /* create a vertex list to hold all the vertices */
      vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count);
      nverts = elem_count;

      /* set up for getting vertex elements */

      setup_property_ply (in_ply, &vert_props[0]);
      setup_property_ply (in_ply, &vert_props[1]);
      setup_property_ply (in_ply, &vert_props[2]);

      for (j = 0; j < in_ply->elems[i]->nprops; j++) {
	PlyProperty *prop;
	prop = in_ply->elems[i]->props[j];
	if (equal_strings ("r", prop->name) ||
	    equal_strings ("red", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[3]);
	  per_vertex_color = true;
	}
	if (equal_strings ("g", prop->name) ||
	    equal_strings ("green", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[4]);
	  per_vertex_color = true;
	}
	if (equal_strings ("b", prop->name) ||
	    equal_strings ("blue", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[5]);
	  per_vertex_color = true;
	}
	if (equal_strings ("nx", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[6]);
	  has_normals = true;
	}
	if (equal_strings ("ny", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[7]);
	  has_normals = true;
	}
	if (equal_strings ("nz", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[8]);
	  has_normals = true;
	}
      }

      vert_other = get_other_properties_ply (in_ply, 
					     offsetof(Vertex,other_props));

      /* grab all the vertex elements */
      for (j = 0; j < elem_count; j++) {
        vlist[j] = (Vertex *) malloc (sizeof (Vertex));
        get_element_ply (in_ply, (void *) vlist[j]);
      }
    }
    else if (equal_strings ("face", elem_name)) {

      /* create a list to hold all the face elements */
      flist = (Face **) malloc (sizeof (Face *) * elem_count);
      nfaces = elem_count;

      /* set up for getting face elements */

      setup_property_ply (in_ply, &face_props[0]);
      face_other = get_other_properties_ply (in_ply, 
					     offsetof(Face,other_props));

      /* grab all the face elements */
      for (j = 0; j < elem_count; j++) {
        flist[j] = (Face *) malloc (sizeof (Face));
        get_element_ply (in_ply, (void *) flist[j]);
      }
    }
    else
      get_other_element_ply (in_ply);
  }

  close_ply (in_ply);
  free_ply (in_ply);
  
  if (Global::volumeType() == Global::DummyVolume)
    {
      float minX, maxX;
      float minY, maxY;
      float minZ, maxZ;
      minX = maxX = vlist[0]->x;
      minY = maxY = vlist[0]->y;
      minZ = maxZ = vlist[0]->z;
      for(int i=0; i<nverts; i++)
	{
	  minX = qMin(minX, vlist[i]->x);
	  maxX = qMax(maxX, vlist[i]->x);
	  minY = qMin(minY, vlist[i]->y);
	  maxY = qMax(maxY, vlist[i]->y);
	  minZ = qMin(minZ, vlist[i]->z);
	  maxZ = qMax(maxZ, vlist[i]->z);
	}
      minX = floor(minX);
      minY = floor(minY);
      minZ = floor(minZ);
      maxX = ceil(maxX);
      maxY = ceil(maxY);
      maxZ = ceil(maxZ);
      int h = maxX-minX+1;
      int w = maxY-minY+1;
      int d = maxZ-minZ+1;

      m_nX = d;
      m_nY = w;
      m_nZ = h;
      m_position = Vec(-minX, -minY, -minZ);

//      bool ok;
//      QString text = QInputDialog::getText(0,
//					   "Please enter grid size",
//					   "Grid Size",
//					   QLineEdit::Normal,
//					   QString("%1 %2 %3").\
//					   arg(d).arg(w).arg(h),
//					   &ok);
//      if (!ok || text.isEmpty())
//	{
//	  QMessageBox::critical(0, "Cannot load PLY", "No grid");
//	  return false;
//	}
//      
//      int nx=0;
//      int ny=0;
//      int nz=0;
//      QStringList gs = text.split(" ", QString::SkipEmptyParts);
//      if (gs.count() > 0) nx = gs[0].toInt();
//      if (gs.count() > 1) ny = gs[1].toInt();
//      if (gs.count() > 2) nz = gs[2].toInt();
//      if (nx > 0 && ny > 0 && nz > 0)
//	{
//	  m_nX = nx;
//	  m_nY = ny;
//	  m_nZ = nz;
//	}
//      else
//	{
//	  QMessageBox::critical(0, "Cannot load triset", "No grid");
//	  return false;
//	}
//
//      if (d == m_nX && w == m_nY && h == m_nZ)
//	m_position = Vec(-minX, -minY, -minZ);
    }
  else
    {
      Vec dim = VolumeInformation::volumeInformation(0).dimensions;
      m_nZ = dim.x;
      m_nY = dim.y;
      m_nX = dim.z;
    }

  m_vertices.resize(nverts);
  for(int i=0; i<nverts; i++)
    m_vertices[i] = Vec(vlist[i]->x,
			vlist[i]->y,
			vlist[i]->z);


  m_normals.clear();
  if (has_normals)
    {
      m_normals.resize(nverts);
      for(int i=0; i<nverts; i++)
	m_normals[i] = Vec(vlist[i]->nx,
			   vlist[i]->ny,
			   vlist[i]->nz);
    }

  m_vcolor.clear();
  if (per_vertex_color)
    {
      m_vcolor.resize(nverts);
      for(int i=0; i<nverts; i++)
	m_vcolor[i] = Vec(vlist[i]->r/255.0f,
			  vlist[i]->g/255.0f,
			  vlist[i]->b/255.0f);
    }

  // only triangles considered
  int ntri=0;
  for (int i=0; i<nfaces; i++)
    {
      if (flist[i]->nverts >= 3)
	ntri++;
    }
  m_triangles.resize(3*ntri);

  int tri=0;
  for(int i=0; i<nfaces; i++)
    {
      if (flist[i]->nverts >= 3)
	{
	  m_triangles[3*tri+0] = flist[i]->verts[0];
	  m_triangles[3*tri+1] = flist[i]->verts[1];
	  m_triangles[3*tri+2] = flist[i]->verts[2];
	  tri++;
	}
    }



  m_tvertices.resize(nverts);
  m_tnormals.resize(nverts);
  m_texValues.resize(nverts);


  Vec bmin = m_vertices[0];
  Vec bmax = m_vertices[0];
  for(int i=0; i<nverts; i++)
    {
      bmin = StaticFunctions::minVec(bmin, m_vertices[i]);
      bmax = StaticFunctions::maxVec(bmax, m_vertices[i]);
    }
  m_centroid = (bmin + bmax)/2;

  m_enclosingBox[0] = Vec(bmin.x, bmin.y, bmin.z);
  m_enclosingBox[1] = Vec(bmax.x, bmin.y, bmin.z);
  m_enclosingBox[2] = Vec(bmax.x, bmax.y, bmin.z);
  m_enclosingBox[3] = Vec(bmin.x, bmax.y, bmin.z);
  m_enclosingBox[4] = Vec(bmin.x, bmin.y, bmax.z);
  m_enclosingBox[5] = Vec(bmax.x, bmin.y, bmax.z);
  m_enclosingBox[6] = Vec(bmax.x, bmax.y, bmax.z);
  m_enclosingBox[7] = Vec(bmin.x, bmax.y, bmax.z);


  m_pointStep = qMax(1, nverts/50000);

//  QMessageBox::information(0, "", QString("%1 %2 %3\n%4 %5").	\
//			   arg(m_nX).arg(m_nY).arg(m_nZ).	\
//			   arg(m_vertices.count()).		\
//			   arg(m_triangles.count()/3));

  m_fileName = flnm;

  return true;
}
Esempio n. 10
0
bool
MeshGenerator::loadPLY(QString flnm)
{
  PlyProperty vert_props[] = { /* list of property information for a vertex */
    {"x", Float32, Float32, offsetof(Vertex,x), 0, 0, 0, 0},
    {"y", Float32, Float32, offsetof(Vertex,y), 0, 0, 0, 0},
    {"z", Float32, Float32, offsetof(Vertex,z), 0, 0, 0, 0},
    {"nx", Float32, Float32, offsetof(Vertex,nx), 0, 0, 0, 0},
    {"ny", Float32, Float32, offsetof(Vertex,ny), 0, 0, 0, 0},
    {"nz", Float32, Float32, offsetof(Vertex,nz), 0, 0, 0, 0},
    {"red", Uint8, Uint8, offsetof(Vertex,r), 0, 0, 0, 0},
    {"green", Uint8, Uint8, offsetof(Vertex,g), 0, 0, 0, 0},
    {"blue", Uint8, Uint8, offsetof(Vertex,b), 0, 0, 0, 0},
  };

  PlyProperty face_props[] = { /* list of property information for a face */
    {"vertex_indices", Int32, Int32, offsetof(Face,verts),
     1, Uint8, Uint8, offsetof(Face,nverts)},
  };


  /*** the PLY object ***/
  PlyOtherProp *vert_other,*face_other;

  bool per_vertex_color = false;
  bool has_normals = false;

  int i,j;
  int elem_count;
  char *elem_name;
  PlyFile *in_ply;


  /*** Read in the original PLY object ***/
  FILE *fp = fopen(flnm.toLatin1().data(), "rb");

  in_ply  = read_ply (fp);

  for (i = 0; i < in_ply->num_elem_types; i++) {

    /* prepare to read the i'th list of elements */
    elem_name = setup_element_read_ply (in_ply, i, &elem_count);


    if (equal_strings ("vertex", elem_name)) {

      /* create a vertex list to hold all the vertices */
      m_vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count);
      m_nverts = elem_count;

      /* set up for getting vertex elements */

      setup_property_ply (in_ply, &vert_props[0]);
      setup_property_ply (in_ply, &vert_props[1]);
      setup_property_ply (in_ply, &vert_props[2]);

      for (j = 0; j < in_ply->elems[i]->nprops; j++) {
	PlyProperty *prop;
	prop = in_ply->elems[i]->props[j];
	if (equal_strings ("r", prop->name) ||
	    equal_strings ("red", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[3]);
	  per_vertex_color = true;
	}
	if (equal_strings ("g", prop->name) ||
	    equal_strings ("green", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[4]);
	  per_vertex_color = true;
	}
	if (equal_strings ("b", prop->name) ||
	    equal_strings ("blue", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[5]);
	  per_vertex_color = true;
	}
	if (equal_strings ("nx", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[6]);
	  has_normals = true;
	}
	if (equal_strings ("ny", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[7]);
	  has_normals = true;
	}
	if (equal_strings ("nz", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[8]);
	  has_normals = true;
	}
      }

      /* grab all the vertex elements */
      for (j = 0; j < elem_count; j++) {
        m_vlist[j] = (Vertex *) malloc (sizeof (Vertex));
        get_element_ply (in_ply, (void *) m_vlist[j]);
      }
    }
    else if (equal_strings ("face", elem_name)) {

      /* create a list to hold all the face elements */
      m_flist = (Face **) malloc (sizeof (Face *) * elem_count);
      m_nfaces = elem_count;

      /* set up for getting face elements */

      setup_property_ply (in_ply, &face_props[0]);

      /* grab all the face elements */
      for (j = 0; j < elem_count; j++) {
        m_flist[j] = (Face *) malloc (sizeof (Face));
        get_element_ply (in_ply, (void *) m_flist[j]);
      }
    }
    else
      get_other_element_ply (in_ply);
  }

  close_ply (in_ply);
  free_ply (in_ply);
  

  vcolor = new float[m_nverts*3];
  for(int i=0; i<m_nverts; i++)
    {
      vcolor[3*i+0] = m_vlist[i]->r/255.0f;
      vcolor[3*i+1] = m_vlist[i]->g/255.0f;
      vcolor[3*i+2] = m_vlist[i]->b/255.0f;
    }

  QMessageBox::information(0, "", QString("Loaded input mesh : vertices %1  faces %2").\
			   arg(m_nverts).\
			   arg(m_nfaces));

  return true;
}
Esempio n. 11
0
read_file()
{
  int i,j;
  int elem_count;
  char *elem_name;

  /*** Read in the original PLY object ***/

  in_ply = read_ply (stdin);

  /* examine each element type that is in the file (vertex, face) */

  for (i = 0; i < in_ply->num_elem_types; i++) {

    /* prepare to read the i'th list of elements */
    elem_name = setup_element_read_ply (in_ply, i, &elem_count);

    if (equal_strings ("vertex", elem_name)) {

      /* create a vertex list to hold all the vertices */
      vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count);
      nverts = elem_count;

      /* set up for getting vertex elements */
      /* (we want x,y,z) */

      setup_property_ply (in_ply, &vert_props[0]);
      setup_property_ply (in_ply, &vert_props[1]);
      setup_property_ply (in_ply, &vert_props[2]);

      /* we also want normal information if it is there (nx,ny,nz) */

      for (j = 0; j < in_ply->elems[i]->nprops; j++) {
	PlyProperty *prop;
	prop = in_ply->elems[i]->props[j];
	if (equal_strings ("nx", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[3]);
	  has_nx = 1;
	}
	if (equal_strings ("ny", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[4]);
	  has_ny = 1;
	}
	if (equal_strings ("nz", prop->name)) {
	  setup_property_ply (in_ply, &vert_props[5]);
	  has_nz = 1;
	}
      }

      /* also grab anything else that we don't need to know about */

      vert_other = get_other_properties_ply (in_ply, 
					     offsetof(Vertex,other_props));

      /* grab the vertex elements and store them in our list */

      for (j = 0; j < elem_count; j++) {
        vlist[j] = (Vertex *) malloc (sizeof (Vertex));
        get_element_ply (in_ply, (void *) vlist[j]);
      }
    }
    else if (equal_strings ("face", elem_name)) {

      /* create a list to hold all the face elements */
      flist = (Face **) malloc (sizeof (Face *) * elem_count);
      nfaces = elem_count;

      /* set up for getting face elements */
      /* (all we need are vertex indices) */

      setup_property_ply (in_ply, &face_props[0]);
      face_other = get_other_properties_ply (in_ply, 
					     offsetof(Face,other_props));

      /* grab all the face elements and place them in our list */

      for (j = 0; j < elem_count; j++) {
        flist[j] = (Face *) malloc (sizeof (Face));
        get_element_ply (in_ply, (void *) flist[j]);
      }
    }
    else  /* all non-vertex and non-face elements are grabbed here */
      get_other_element_ply (in_ply);
  }

  /* close the file */
  /* (we won't free up the memory for in_ply because we will use it */
  /*  to help describe the file that we will write out) */

  close_ply (in_ply);
}
Esempio n. 12
0
int CTriangleObj::_loadPLYFile(FILE *fp)
{
	Vector3i*& m_pTriangle = (Vector3i*&)m_pPolygon;
	int i, j, elem_count, nverts, nfaces;
	char *elem_name, buff[200];
	Vertex vlist;
	Face flist;
	PlyOtherProp *vert_other,*face_other;
	PlyProperty vert_props[] = { /* list of property information for a vertex */
		{"x", Float32, Float32, offsetof(Vertex,x), 0, 0, 0, 0},
		{"y", Float32, Float32, offsetof(Vertex,y), 0, 0, 0, 0},
		{"z", Float32, Float32, offsetof(Vertex,z), 0, 0, 0, 0},
		{"nx", Float32, Float32, offsetof(Vertex,nx), 0, 0, 0, 0},
		{"ny", Float32, Float32, offsetof(Vertex,ny), 0, 0, 0, 0},
		{"nz", Float32, Float32, offsetof(Vertex,nz), 0, 0, 0, 0},
	};
	char *elem_names[] = { /* list of the kinds of elements in the user's object */
		"vertex", "face"
	};

	PlyProperty face_props[] = { /* list of property information for a face */
		{"vertex_indices", Int32, Int32, offsetof(Face,verts), 1, Uint8, Uint8, offsetof(Face,nverts)},
	};

	/*** Read in the original PLY object ***/
	Free();
	PlyFile *in_ply = read_ply (fp);
	if (in_ply==NULL) return 0;

	for (i = 0; i < in_ply->num_elem_types; i++){
		/* prepare to read the i'th list of elements */
		elem_name = setup_element_read_ply (in_ply, i, &elem_count);
		if (equal_strings ("vertex", elem_name)){
			/* create a vertex list to hold all the vertices */
			m_nVertexCount = nverts = elem_count;
			m_pVertex = new Vector3d [m_nVertexCount];
			assert(m_pVertex!=NULL);
			/* set up for getting vertex elements */
			setup_property_ply (in_ply, &vert_props[0]);
			setup_property_ply (in_ply, &vert_props[1]);
			setup_property_ply (in_ply, &vert_props[2]);
			vert_other = get_other_properties_ply (in_ply, offsetof(Vertex,other_props));
			/* grab all the vertex elements */
			for (j = 0; j < elem_count; j++) {
				get_element_ply (in_ply, (void *) &vlist);
				Vector3d *p = &m_pVertex[j];
				p->x = vlist.x;
				p->y = vlist.y;
				p->z = vlist.z;
			}
		}
		else if (equal_strings ("face", elem_name)) {
			m_nPolygonCount = nfaces = elem_count;
			/* create a list to hold all the face elements */
			m_pTriangle = new Vector3i[m_nPolygonCount];
			assert(m_pTriangle!=NULL);
			/* set up for getting face elements */
			setup_property_ply (in_ply, &face_props[0]);
			face_other = get_other_properties_ply (in_ply, offsetof(Face,other_props));
			/* grab all the face elements */
			for (j = 0; j < elem_count; j++) {
				get_element_ply (in_ply, (void *) &flist);
				if (flist.nverts !=3){
					sprintf(buff, "Found a face with %d vertices!\n", (int)flist.nverts);
					puts(buff);
				}
				Vector3i *ptri = &m_pTriangle[j];
				ptri->x = flist.verts[0];
				ptri->y = flist.verts[2];
				ptri->z = flist.verts[1];
			}
		}
		else if (equal_strings ("tristrips", elem_name)) {
			int *ibuff = _readTriangleStrip(fp, elem_count);
			m_pTriangle=_parseStrip2Triangles(ibuff, elem_count, m_nPolygonCount);
			delete [] ibuff;
		}
		else{
			get_other_element_ply (in_ply);
		}
	}

	close_ply (in_ply);
	return 1;
}