⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ply_tk.cpp

📁 RBF平台
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*The interface routines for reading and writing PLY polygon files.Greg Turk---------------------------------------------------------------A PLY file contains a single polygonal _object_.An object is composed of lists of _elements_.  Typical elements arevertices, faces, edges and materials.Each type of element for a given object has one or more _properties_associated with the element type.  For instance, a vertex element mayhave as properties the floating-point values x,y,z and the three unsignedchars representing red, green and blue.-----------------------------------------------------------------------Copyright (c) 1998 Georgia Institute of Technology.  All rights reserved.     Permission to use, copy, modify and distribute this software and its   documentation for any purpose is hereby granted without fee, provided   that the above copyright notice and this permission notice appear in   all copies of this software and that you do not sell the software.     THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,   EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY   WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.   
Modified to C++ by Wu Xiaojun, 20, Jen,2005 in CUHK
*/#include "stdafx.h"#include <stdio.h>#include <stdlib.h>
#include <math.h>#include <string.h>#include "ply_tk.h"

Ply_tk::Ply_tk()
{
	nverts = 0;
	nfaces = 0;
	vlist = NULL;
	flist = NULL;

	//ply = NULL;
	//num_elems = 0;
	//elem_names = NULL;

	type_names[0] = "invalid";
	type_names[1] = "int8";
	type_names[2] = "int16";
	type_names[3] = "int32";
	type_names[4] = "uint8";
	type_names[5] = "uint16";
	type_names[6] = "uint32";
	type_names[7] = "float32";
	type_names[8] = "flaot64";

	old_type_names[0] = "invalid";
	old_type_names[1] = "char";
	old_type_names[2] = "short";
	old_type_names[3] = "int";
	old_type_names[4] = "uchar";
	old_type_names[5] = "ushort";
	old_type_names[6] = "uint";
	old_type_names[7] = "float";
	old_type_names[8] = "double";
	
	ply_type_size[0] = 0;
	ply_type_size[1] = 1;
	ply_type_size[2] = 2;
	ply_type_size[3] = 4;
	ply_type_size[4] = 1;
	ply_type_size[5] = 2;
	ply_type_size[6] = 4;
	ply_type_size[7] = 4;
	ply_type_size[8] = 8;

	rule_name_list[0].code = AVERAGE_RULE;
	rule_name_list[0].name = "avg";
	rule_name_list[1].code = RANDOM_RULE;
	rule_name_list[1].name = "rnd";
	rule_name_list[2].code = MINIMUM_RULE;
	rule_name_list[2].name = "max";
	rule_name_list[3].code = MAXIMUM_RULE;
	rule_name_list[3].name = "min";
	rule_name_list[4].code = MAJORITY_RULE;
	rule_name_list[4].name = "major";
	rule_name_list[5].code = SAME_RULE;
	rule_name_list[5].name = "same";
	rule_name_list[6].code = -1;
	rule_name_list[6].name = "end marker";

}


Ply_tk::~Ply_tk()
{
	if(vlist != NULL) free(vlist);
	if(vlist != NULL) free(flist);

}/*************//*  Writing  *//*************//******************************************************************************Given a file pointer, get ready to write PLY data to the file.Entry:  fp         - the given file pointer  nelems     - number of elements in object  elem_names - list of element names  file_type  - file type, either ascii or binaryExit:  returns a pointer to a PlyFile, used to refer to this file, or NULL if error******************************************************************************/PlyFile * Ply_tk::ply_write(  FILE *fp,  int nelems,  char **elem_names,  int file_type){  int i;  PlyFile *plyfile;  PlyElement *elem;  /* check for NULL file pointer */  if (fp == NULL)    return (NULL);  /* create a record for this object */  plyfile = (PlyFile *) myalloc (sizeof (PlyFile));  plyfile->file_type = file_type;  plyfile->num_comments = 0;  plyfile->num_obj_info = 0;  plyfile->num_elem_types = nelems;  plyfile->version = 1.0;  plyfile->fp = fp;  plyfile->other_elems = NULL;  /* tuck aside the names of the elements */  plyfile->elems = (PlyElement **) myalloc (sizeof (PlyElement *) * nelems);  for (i = 0; i < nelems; i++) {    elem = (PlyElement *) myalloc (sizeof (PlyElement));    plyfile->elems[i] = elem;    elem->name = strdup (elem_names[i]);    elem->num = 0;    elem->nprops = 0;  }  /* return pointer to the file descriptor */  return (plyfile);}/******************************************************************************Open a polygon file for writing.Entry:  filename   - name of file to read from  nelems     - number of elements in object  elem_names - list of element names  file_type  - file type, either ascii or binaryExit:  returns a file identifier, used to refer to this file, or NULL if error******************************************************************************/PlyFile * Ply_tk::open_for_writing_ply(char *filename, int nelems, char **elem_names, int file_type){  PlyFile *plyfile;  //PlyElement *elem;  char *name;  FILE *fp;  /* tack on the extension .ply, if necessary */  name = (char *) myalloc (sizeof (char) * (strlen (filename) + 5));  strcpy (name, filename);  if (strlen (name) < 4 ||      strcmp (name + strlen (name) - 4, ".ply") != 0)      strcat (name, ".ply");  /* open the file for writing */  fp = fopen (name, "w");  if (fp == NULL) {    return (NULL);  }  /* create the actual PlyFile structure */  plyfile = ply_write (fp, nelems, elem_names, file_type);  if (plyfile == NULL)    return (NULL);  /* return pointer to the file descriptor */  return (plyfile);}/******************************************************************************Describe an element, including its properties and how many will be writtento the file.Entry:  plyfile   - file identifier  elem_name - name of element that information is being specified about  nelems    - number of elements of this type to be written  nprops    - number of properties contained in the element  prop_list - list of properties******************************************************************************/void Ply_tk::element_layout_ply(PlyFile *plyfile, char *elem_name, int nelems, int nprops, PlyProperty *prop_list){  int i;  PlyElement *elem;  PlyProperty *prop;  /* look for appropriate element */  elem = find_element (plyfile, elem_name);  if (elem == NULL) {    fprintf(stderr,"element_layout_ply: can't find element '%s'\n",elem_name);    exit (-1);  }  elem->num = nelems;  /* copy the list of properties */  elem->nprops = nprops;  elem->props = (PlyProperty **) myalloc (sizeof (PlyProperty *) * nprops);  elem->store_prop = (char *) myalloc (sizeof (char) * nprops);  for (i = 0; i < nprops; i++) {    prop = (PlyProperty *) myalloc (sizeof (PlyProperty));    elem->props[i] = prop;    elem->store_prop[i] = NAMED_PROP;    copy_property (prop, &prop_list[i]);  }}/******************************************************************************Describe a property of an element.Entry:  plyfile   - file identifier  elem_name - name of element that information is being specified about  prop      - the new property******************************************************************************/void Ply_tk::ply_describe_property(PlyFile *plyfile, char *elem_name, PlyProperty *prop){  PlyElement *elem;  PlyProperty *elem_prop;  /* look for appropriate element */  elem = find_element (plyfile, elem_name);  if (elem == NULL) {    fprintf(stderr, "ply_describe_property: can't find element '%s'\n",            elem_name);    return;  }  /* create room for new property */  if (elem->nprops == 0) {    elem->props = (PlyProperty **) myalloc (sizeof (PlyProperty *));    elem->store_prop = (char *) myalloc (sizeof (char));    elem->nprops = 1;  }  else {    elem->nprops++;    elem->props = (PlyProperty **)                  realloc (elem->props, sizeof (PlyProperty *) * elem->nprops);    elem->store_prop = (char *)                  realloc (elem->store_prop, sizeof (char) * elem->nprops);  }  /* copy the new property */  elem_prop = (PlyProperty *) myalloc (sizeof (PlyProperty));  elem->props[elem->nprops - 1] = elem_prop;  elem->store_prop[elem->nprops - 1] = NAMED_PROP;  copy_property (elem_prop, prop);}/******************************************************************************State how many of a given element will be written.Entry:  plyfile   - file identifier  elem_name - name of element that information is being specified about  nelems    - number of elements of this type to be written******************************************************************************/void Ply_tk::element_count_ply(PlyFile *plyfile, char *elem_name, int nelems){  //int i;  PlyElement *elem;  //PlyProperty *prop;  /* look for appropriate element */  elem = find_element (plyfile, elem_name);  if (elem == NULL) {    fprintf(stderr,"element_count_ply: can't find element '%s'\n",elem_name);    exit (-1);  }  elem->num = nelems;}/******************************************************************************Signal that we've described everything a PLY file's header and that theheader should be written to the file.Entry:  plyfile - file identifier******************************************************************************/void Ply_tk::header_complete_ply(PlyFile *plyfile){  int i,j;  FILE *fp = plyfile->fp;  PlyElement *elem;  PlyProperty *prop;  fprintf (fp, "ply\n");  switch (plyfile->file_type) {    case PLY_ASCII:      fprintf (fp, "format ascii 1.0\n");      break;    case PLY_BINARY_BE:      fprintf (fp, "format binary_big_endian 1.0\n");      break;    case PLY_BINARY_LE:      fprintf (fp, "format binary_little_endian 1.0\n");      break;    default:      fprintf (stderr, "ply_header_complete: bad file type = %d\n",               plyfile->file_type);      exit (-1);  }  /* write out the comments */  for (i = 0; i < plyfile->num_comments; i++)    fprintf (fp, "comment %s\n", plyfile->comments[i]);  /* write out object information */  for (i = 0; i < plyfile->num_obj_info; i++)    fprintf (fp, "obj_info %s\n", plyfile->obj_info[i]);  /* write out information about each element */  for (i = 0; i < plyfile->num_elem_types; i++) {    elem = plyfile->elems[i];    fprintf (fp, "element %s %d\n", elem->name, elem->num);    /* write out each property */    for (j = 0; j < elem->nprops; j++) {      prop = elem->props[j];      if (prop->is_list == PLY_LIST) {        fprintf (fp, "property list ");        write_scalar_type (fp, prop->count_external);        fprintf (fp, " ");        write_scalar_type (fp, prop->external_type);        fprintf (fp, " %s\n", prop->name);      }      else if (prop->is_list == PLY_STRING) {        fprintf (fp, "property string");        fprintf (fp, " %s\n", prop->name);      }      else {        fprintf (fp, "property ");        write_scalar_type (fp, prop->external_type);        fprintf (fp, " %s\n", prop->name);      }    }  }  fprintf (fp, "end_header\n");}/******************************************************************************Specify which elements are going to be written.  This should be calledbefore a call to the routine ply_put_element().Entry:  plyfile   - file identifier  elem_name - name of element we're talking about******************************************************************************/void Ply_tk::put_element_setup_ply(PlyFile *plyfile, char *elem_name){  PlyElement *elem;  elem = find_element (plyfile, elem_name);  if (elem == NULL) {    fprintf(stderr, "put_element_setup_ply: can't find element '%s'\n", elem_name);    exit (-1);  }  plyfile->which_elem = elem;}/******************************************************************************Write an element to the file.  This routine assumes that we'rewriting the type of element specified in the last call to the routineput_element_setup_ply().Entry:  plyfile  - file identifier  elem_ptr - pointer to the element******************************************************************************/void Ply_tk::put_element_ply(PlyFile *plyfile, void *elem_ptr){  int j,k;  FILE *fp = plyfile->fp;  PlyElement *elem;  PlyProperty *prop;  char *item;  char *elem_data;  char **item_ptr;  int list_count;  int item_size;  int int_val;  unsigned int uint_val;  double double_val;  char **other_ptr;  elem = plyfile->which_elem;  elem_data = (char *) elem_ptr;  other_ptr = (char **) (((char *) elem_ptr) + elem->other_offset);  /* write out either to an ascii or binary file */  if (plyfile->file_type == PLY_ASCII) {    /* write an ascii file */    /* write out each property of the element */    for (j = 0; j < elem->nprops; j++) {      prop = elem->props[j];      if (elem->store_prop[j] == OTHER_PROP)        elem_data = *other_ptr;      else        elem_data = (char *) elem_ptr;      if (prop->is_list == PLY_LIST) {  /* list */        item = elem_data + prop->count_offset;        get_stored_item ((void *) item, prop->count_internal,                         &int_val, &uint_val, &double_val);        write_ascii_item (fp, int_val, uint_val, double_val,                          prop->count_external);        list_count = uint_val;        item_ptr = (char **) (elem_data + prop->offset);        item = item_ptr[0];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -