📄 plyfile.cpp
字号:
while (*s1 && *s2)
if (*s1++ != *s2++)
return (0);
if (*s1 != *s2)
return (0);
else
return (1);
}
/******************************************************************************
Find an element from the element list of a given PLY object.
Entry:
plyfile - file id for PLY file
element - name of element we're looking for
Exit:
returns the element, or NULL if not found
******************************************************************************/
PlyElement *find_element(PlyFile *plyfile, char *element)
{
int i;
for (i = 0; i < plyfile->nelems; i++)
if (equal_strings (element, plyfile->elems[i]->name))
return (plyfile->elems[i]);
return (NULL);
}
/******************************************************************************
Find a property in the list of properties of a given element.
Entry:
elem - pointer to element in which we want to find the property
prop_name - name of property to find
Exit:
index - index to position in list
returns a pointer to the property, or NULL if not found
******************************************************************************/
PlyProperty *find_property(PlyElement *elem, char *prop_name, int *index)
{
int i;
for (i = 0; i < elem->nprops; i++)
if (equal_strings (prop_name, elem->props[i]->name)) {
*index = i;
return (elem->props[i]);
}
*index = -1;
return (NULL);
}
/******************************************************************************
Read an element from an ascii file.
Entry:
plyfile - file identifier
elem_ptr - pointer to element
******************************************************************************/
void ascii_get_element(PlyFile *plyfile, char *elem_ptr)
{
int i,j,k;
PlyElement *elem;
PlyProperty *prop;
char **words;
int nwords;
int which_word;
FILE *fp = plyfile->fp;
char *elem_data,*item;
char *item_ptr;
int item_size;
int int_val;
unsigned int uint_val;
double double_val;
int list_count;
int store_it;
char **store_array;
char *orig_line;
char *other_data;
int other_flag;
/* the kind of element we're reading currently */
elem = plyfile->which_elem;
/* do we need to setup for other_props? */
if (elem->other_offset != NO_OTHER_PROPS) {
char **ptr;
other_flag = 1;
/* make room for other_props */
other_data = (char *) myalloc (elem->other_size);
/* store pointer in user's structure to the other_props */
ptr = (char **) (elem_ptr + elem->other_offset);
*ptr = other_data;
}
else
other_flag = 0;
/* read in the element */
words = get_words (plyfile->fp, &nwords, &orig_line);
if (words == NULL) {
fprintf (stderr, "ply_get_element: unexpected end of file\n");
exit (-1);
}
which_word = 0;
for (j = 0; j < elem->nprops; j++) {
prop = elem->props[j];
store_it = (elem->store_prop[j] | other_flag);
/* store either in the user's structure or in other_props */
if (elem->store_prop[j])
elem_data = elem_ptr;
else
elem_data = other_data;
if (prop->is_list) { /* a list */
/* get and store the number of items in the list */
get_ascii_item (words[which_word++], prop->count_external,
&int_val, &uint_val, &double_val);
if (store_it) {
item = elem_data + prop->count_offset;
store_item(item, prop->count_internal, int_val, uint_val, double_val);
}
/* allocate space for an array of items and store a ptr to the array */
list_count = int_val;
item_size = ply_type_size[prop->internal_type];
store_array = (char **) (elem_data + prop->offset);
if (list_count == 0) {
if (store_it)
*store_array = NULL;
}
else {
if (store_it) {
item_ptr = (char *) myalloc (sizeof (char) * item_size * list_count);
item = item_ptr;
*store_array = item_ptr;
}
/* read items and store them into the array */
for (k = 0; k < list_count; k++) {
get_ascii_item (words[which_word++], prop->external_type,
&int_val, &uint_val, &double_val);
if (store_it) {
store_item (item, prop->internal_type,
int_val, uint_val, double_val);
item += item_size;
}
}
}
}
else { /* not a list */
get_ascii_item (words[which_word++], prop->external_type,
&int_val, &uint_val, &double_val);
if (store_it) {
item = elem_data + prop->offset;
store_item (item, prop->internal_type, int_val, uint_val, double_val);
}
}
}
free (words);
}
/******************************************************************************
Read an element from a binary file.
Entry:
plyfile - file identifier
elem_ptr - pointer to an element
******************************************************************************/
void binary_get_element(PlyFile *plyfile, char *elem_ptr)
{
int i,j,k;
PlyElement *elem;
PlyProperty *prop;
FILE *fp = plyfile->fp;
char *elem_data,*item;
char *item_ptr;
int item_size;
int int_val;
unsigned int uint_val;
double double_val;
int list_count;
int store_it;
char **store_array;
char *other_data;
int other_flag;
/* the kind of element we're reading currently */
elem = plyfile->which_elem;
/* do we need to setup for other_props? */
if (elem->other_offset != NO_OTHER_PROPS) {
char **ptr;
other_flag = 1;
/* make room for other_props */
other_data = (char *) myalloc (elem->other_size);
/* store pointer in user's structure to the other_props */
ptr = (char **) (elem_ptr + elem->other_offset);
*ptr = other_data;
}
else
other_flag = 0;
/* read in a number of elements */
for (j = 0; j < elem->nprops; j++) {
prop = elem->props[j];
store_it = (elem->store_prop[j] | other_flag);
/* store either in the user's structure or in other_props */
if (elem->store_prop[j])
elem_data = elem_ptr;
else
elem_data = other_data;
if (prop->is_list) { /* a list */
/* get and store the number of items in the list */
get_binary_item (fp, prop->count_external,
&int_val, &uint_val, &double_val);
if (store_it) {
item = elem_data + prop->count_offset;
store_item(item, prop->count_internal, int_val, uint_val, double_val);
}
/* allocate space for an array of items and store a ptr to the array */
list_count = int_val;
/* The "if" was added by Afra Zomorodian 8/22/95
* so that zipper won't crash reading plies that have additional
* properties.
*/
if (store_it) {
item_size = ply_type_size[prop->internal_type];
}
store_array = (char **) (elem_data + prop->offset);
if (list_count == 0) {
if (store_it)
*store_array = NULL;
}
else {
if (store_it) {
item_ptr = (char *) myalloc (sizeof (char) * item_size * list_count);
item = item_ptr;
*store_array = item_ptr;
}
/* read items and store them into the array */
for (k = 0; k < list_count; k++) {
get_binary_item (fp, prop->external_type,
&int_val, &uint_val, &double_val);
if (store_it) {
store_item (item, prop->internal_type,
int_val, uint_val, double_val);
item += item_size;
}
}
}
}
else { /* not a list */
get_binary_item (fp, prop->external_type,
&int_val, &uint_val, &double_val);
if (store_it) {
item = elem_data + prop->offset;
store_item (item, prop->internal_type, int_val, uint_val, double_val);
}
}
}
}
/******************************************************************************
Write to a file the word that represents a PLY data type.
Entry:
fp - file pointer
code - code for type
******************************************************************************/
void write_scalar_type (FILE *fp, int code)
{
/* make sure this is a valid code */
if (code <= PLY_START_TYPE || code >= PLY_END_TYPE) {
fprintf (stderr, "write_scalar_type: bad data code = %d\n", code);
exit (-1);
}
/* write the code to a file */
fprintf (fp, "%s", type_names[code]);
}
/******************************************************************************
Get a text line from a file and break it up into words.
IMPORTANT: The calling routine call "free" on the returned pointer once
finished with it.
Entry:
fp - file to read from
Exit:
nwords - number of words returned
orig_line - the original line of characters
returns a list of words from the line, or NULL if end-of-file
******************************************************************************/
char **get_words(FILE *fp, int *nwords, char **orig_line)
{
#define BIG_STRING 4096
int i,j;
static char str[BIG_STRING];
static char str_copy[BIG_STRING];
char **words;
int max_words = 10;
int num_words = 0;
char *ptr,*ptr2;
char *result;
words = (char **) myalloc (sizeof (char *) * max_words);
/* read in a line */
result = fgets (str, BIG_STRING, fp);
if (result == NULL) {
*nwords = 0;
*orig_line = NULL;
return (NULL);
}
/* convert line-feed and tabs into spaces */
/* (this guarentees that there will be a space before the */
/* null character at the end of the string) */
str[BIG_STRING-2] = ' ';
str[BIG_STRING-1] = '\0';
for (ptr = str, ptr2 = str_copy; *ptr != '\0'; ptr++, ptr2++) {
*ptr2 = *ptr;
if (*ptr == '\t') {
*ptr = ' ';
*ptr2 = ' ';
}
else if (*ptr == '\n') {
*ptr = ' ';
*ptr2 = '\0';
break;
}
}
/* find the words in the line */
ptr = str;
while (*ptr != '\0') {
/* jump over leading spaces */
while (*ptr == ' ')
ptr++;
/* break if we reach the end */
if (*ptr == '\0')
break;
/* save pointer to beginning of word */
if (num_words >= max_words) {
max_words += 10;
words = (char **) realloc (words, sizeof (char *) * max_words);
}
words[num_words++] = ptr;
/* jump over non-spaces */
while (*ptr != ' ')
ptr++;
/* place a null character here to mark the end of the word */
*ptr++ = '\0';
}
/* return the list of words */
*nwords = num_words;
*orig_line = str_copy;
return (words);
}
/******************************************************************************
Return the value of an item, given a pointer to it and its type.
Entry:
item - pointer to item
type - data type that "item" points to
Exit:
returns a double-precision float that contains the value of the item
******************************************************************************/
double get_item_value(char *item, int type)
{
unsigned char *puchar;
char *pchar;
short int *pshort;
unsigned short int *pushort;
int *pint;
unsigned int *puint;
float *pfloat;
double *pdouble;
int int_value;
unsigned int uint_value;
double double_value;
switch (type) {
case PLY_CHAR:
pchar = (char *) item;
int_value = *pchar;
return ((double) int_value);
case PLY_UCHAR:
puchar = (unsigned char *) item;
int_value = *puchar;
return ((double) int_value);
case PLY_SHORT:
pshort = (short int *) item;
int_value = *pshort;
return ((double) int_value);
case PLY_USHORT:
pushort = (unsigned short int *) item;
int_value = *pushort;
return ((double) int_value);
case PLY_INT:
pint = (int *) item;
int_value = *pint;
return ((double) int_value);
case PLY_UINT:
puint = (unsigned int *) item;
uint_value = *puint;
return ((double) uint_value);
case PLY_FLOAT:
pfloat = (float *) item;
double_value = *pfloat;
return (double_value);
case PLY_DOUBLE:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -