📄 ply.c
字号:
type - data type to write out
******************************************************************************/
void write_binary_item(
FILE *fp,
int int_val,
unsigned int uint_val,
double double_val,
int type
)
{
unsigned char uchar_val;
char char_val;
unsigned short ushort_val;
short short_val;
float float_val;
switch (type) {
case Int8:
char_val = int_val;
fwrite (&char_val, 1, 1, fp);
break;
case Int16:
short_val = int_val;
fwrite (&short_val, 2, 1, fp);
break;
case Int32:
fwrite (&int_val, 4, 1, fp);
break;
case Uint8:
uchar_val = uint_val;
fwrite (&uchar_val, 1, 1, fp);
break;
case Uint16:
ushort_val = uint_val;
fwrite (&ushort_val, 2, 1, fp);
break;
case Uint32:
fwrite (&uint_val, 4, 1, fp);
break;
case Float32:
float_val = (float)double_val;
fwrite (&float_val, 4, 1, fp);
break;
case Float64:
fwrite (&double_val, 8, 1, fp);
break;
default:
fprintf (stderr, "write_binary_item: bad type = %d\n", type);
exit (-1);
}
}
/******************************************************************************
Write out an item to a file as ascii characters.
Entry:
fp - file to write to
int_val - integer version of item
uint_val - unsigned integer version of item
double_val - double-precision float version of item
type - data type to write out
******************************************************************************/
void write_ascii_item(
FILE *fp,
int int_val,
unsigned int uint_val,
double double_val,
int type
)
{
switch (type) {
case Int8:
case Int16:
case Int32:
fprintf (fp, "%d ", int_val);
break;
case Uint8:
case Uint16:
case Uint32:
fprintf (fp, "%u ", uint_val);
break;
case Float32:
case Float64:
fprintf (fp, "%g ", double_val);
break;
default:
fprintf (stderr, "write_ascii_item: bad type = %d\n", type);
exit (-1);
}
}
/******************************************************************************
Get the value of an item that is in memory, and place the result
into an integer, an unsigned integer and a double.
Entry:
ptr - pointer to the item
type - data type supposedly in the item
Exit:
int_val - integer value
uint_val - unsigned integer value
double_val - double-precision floating point value
******************************************************************************/
void get_stored_item(
void *ptr,
int type,
int *int_val,
unsigned int *uint_val,
double *double_val
)
{
switch (type) {
case Int8:
*int_val = *((char *) ptr);
*uint_val = *int_val;
*double_val = *int_val;
break;
case Uint8:
*uint_val = *((unsigned char *) ptr);
*int_val = *uint_val;
*double_val = *uint_val;
break;
case Int16:
*int_val = *((short int *) ptr);
*uint_val = *int_val;
*double_val = *int_val;
break;
case Uint16:
*uint_val = *((unsigned short int *) ptr);
*int_val = *uint_val;
*double_val = *uint_val;
break;
case Int32:
*int_val = *((int *) ptr);
*uint_val = *int_val;
*double_val = *int_val;
break;
case Uint32:
*uint_val = *((unsigned int *) ptr);
*int_val = *uint_val;
*double_val = *uint_val;
break;
case Float32:
*double_val = *((float *) ptr);
*int_val = (int)*double_val;
*uint_val = (unsigned int)*double_val;
break;
case Float64:
*double_val = *((double *) ptr);
*int_val = (int)*double_val;
*uint_val = (unsigned int)*double_val;
break;
default:
fprintf (stderr, "get_stored_item: bad type = %d\n", type);
exit (-1);
}
}
/******************************************************************************
Get the value of an item from a binary file, and place the result
into an integer, an unsigned integer and a double.
Entry:
fp - file to get item from
type - data type supposedly in the word
Exit:
int_val - integer value
uint_val - unsigned integer value
double_val - double-precision floating point value
******************************************************************************/
void get_binary_item(
FILE *fp,
int type,
int *int_val,
unsigned int *uint_val,
double *double_val
)
{
char c[8];
void *ptr;
ptr = (void *) c;
switch (type) {
case Int8:
fread (ptr, 1, 1, fp);
*int_val = *((char *) ptr);
*uint_val = *int_val;
*double_val = *int_val;
break;
case Uint8:
fread (ptr, 1, 1, fp);
*uint_val = *((unsigned char *) ptr);
*int_val = *uint_val;
*double_val = *uint_val;
break;
case Int16:
fread (ptr, 2, 1, fp);
*int_val = *((short int *) ptr);
*uint_val = *int_val;
*double_val = *int_val;
break;
case Uint16:
fread (ptr, 2, 1, fp);
*uint_val = *((unsigned short int *) ptr);
*int_val = *uint_val;
*double_val = *uint_val;
break;
case Int32:
fread (ptr, 4, 1, fp);
*int_val = *((int *) ptr);
*uint_val = *int_val;
*double_val = *int_val;
break;
case Uint32:
fread (ptr, 4, 1, fp);
*uint_val = *((unsigned int *) ptr);
*int_val = *uint_val;
*double_val = *uint_val;
break;
case Float32:
fread (ptr, 4, 1, fp);
*double_val = *((float *) ptr);
*int_val = (int)*double_val;
*uint_val = (unsigned int)*double_val;
break;
case Float64:
fread (ptr, 8, 1, fp);
*double_val = *((double *) ptr);
*int_val = (int)*double_val;
*uint_val = (unsigned int)*double_val;
break;
default:
fprintf (stderr, "get_binary_item: bad type = %d\n", type);
exit (-1);
}
}
/******************************************************************************
Extract the value of an item from an ascii word, and place the result
into an integer, an unsigned integer and a double.
Entry:
word - word to extract value from
type - data type supposedly in the word
Exit:
int_val - integer value
uint_val - unsigned integer value
double_val - double-precision floating point value
******************************************************************************/
void get_ascii_item(
char *word,
int type,
int *int_val,
unsigned int *uint_val,
double *double_val
)
{
switch (type) {
case Int8:
case Uint8:
case Int16:
case Uint16:
case Int32:
*int_val = atoi (word);
*uint_val = *int_val;
*double_val = *int_val;
break;
case Uint32:
*uint_val = strtoul (word, (char **) NULL, 10);
*int_val = *uint_val;
*double_val = *uint_val;
break;
case Float32:
case Float64:
*double_val = atof (word);
*int_val = (int) *double_val;
*uint_val = (unsigned int) *double_val;
break;
default:
fprintf (stderr, "get_ascii_item: bad type = %d\n", type);
exit (-1);
}
}
/******************************************************************************
Store a value into a place being pointed to, guided by a data type.
Entry:
item - place to store value
type - data type
int_val - integer version of value
uint_val - unsigned integer version of value
double_val - double version of value
Exit:
item - pointer to stored value
******************************************************************************/
void store_item (
char *item,
int type,
int int_val,
unsigned int uint_val,
double double_val
)
{
unsigned char *puchar;
short int *pshort;
unsigned short int *pushort;
int *pint;
unsigned int *puint;
float *pfloat;
double *pdouble;
switch (type) {
case Int8:
*item = int_val;
break;
case Uint8:
puchar = (unsigned char *) item;
*puchar = uint_val;
break;
case Int16:
pshort = (short *) item;
*pshort = int_val;
break;
case Uint16:
pushort = (unsigned short *) item;
*pushort = uint_val;
break;
case Int32:
pint = (int *) item;
*pint = int_val;
break;
case Uint32:
puint = (unsigned int *) item;
*puint = uint_val;
break;
case Float32:
pfloat = (float *) item;
*pfloat = (float)double_val;
break;
case Float64:
pdouble = (double *) item;
*pdouble = double_val;
break;
default:
fprintf (stderr, "store_item: bad type = %d\n", type);
exit (-1);
}
}
/******************************************************************************
Add an element to a PLY file descriptor.
Entry:
plyfile - PLY file descriptor
words - list of words describing the element
nwords - number of words in the list
******************************************************************************/
void add_element (PlyFile *plyfile, char **words, int nwords)
{
PlyElement *elem;
/* create the new element */
elem = (PlyElement *) myalloc (sizeof (PlyElement));
elem->name = strdup (words[1]);
elem->num = atoi (words[2]);
elem->nprops = 0;
/* make room for new element in the object's list of elements */
if (plyfile->num_elem_types == 0)
plyfile->elems = (PlyElement **) myalloc (sizeof (PlyElement *));
else
plyfile->elems = (PlyElement **) realloc (plyfile->elems,
sizeof (PlyElement *) * (plyfile->num_elem_types + 1));
/* add the new element to the object's list */
plyfile->elems[plyfile->num_elem_types] = elem;
plyfile->num_elem_types++;
}
/******************************************************************************
Return the type of a property, given the name of the property.
Entry:
name - name of property type
Exit:
returns integer code for property, or 0 if not found
******************************************************************************/
int get_prop_type(char *type_name)
{
int i;
/* try to match the type name */
for (i = StartType + 1; i < EndType; i++)
if (equal_strings (type_name, type_names[i]))
return (i);
/* see if we can match an old type name */
for (i = StartType + 1; i < EndType; i++)
if (equal_strings (type_name, old_type_names[i]))
return (i);
/* if we get here, we didn't find the type */
return (0);
}
/******************************************************************************
Add a property to a PLY file descriptor.
Entry:
plyfile - PLY file descriptor
words - list of words describing the property
nwords - number of words in the list
******************************************************************************/
void add_property (PlyFile *plyfile, char **words, int nwords)
{
PlyProperty *prop;
PlyElement *elem;
/* create the new property */
prop = (PlyProperty *) myalloc (sizeof (PlyProperty));
if (equal_strings (words[1], "list")) { /* list */
prop->count_external = get_prop_type (words[2]);
prop->external_type = get_prop_type (words[3]);
prop->name = strdup (words[4]);
prop->is_list = PLY_LIST;
}
else if (equal_strings (words[1], "string")) { /* string */
prop->count_external = Int8;
prop->external_type = Int8;
prop->name = strdup (words[2]);
prop->is_list = PLY_STRING;
}
else { /* scalar */
prop->external_type = get_prop_type (words[1]);
prop->name = strdup (words[2]);
prop->is_list = PLY_SCALAR;
}
/* add this
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -