pointcloud/libpc/pc_point.c
Paul Ramsey 4626d99aa1 Add first cut at autoconf
Rename 'core' directory
2013-01-14 16:46:13 -08:00

142 lines
2.6 KiB
C

/***********************************************************************
* pc_point.c
*
* Pointclound point handling. Create, get and set values from the
* basic PCPOINT structure.
*
* Portions Copyright (c) 2012, OpenGeo
*
***********************************************************************/
#include "pc_api_internal.h"
PCPOINT *
pc_point_new(const PCSCHEMA *s)
{
size_t sz;
PCPOINT *pt;
if ( ! s )
{
pcerror("null schema passed into pc_point_new");
return NULL;
}
/* Width of the data area */
sz = pc_schema_get_size(s);
if ( ! sz )
{
pcerror("invalid size calculation in pc_point_new");
return NULL;
}
/* Make our own data area */
pt = pcalloc(sizeof(PCPOINT));
pt->data = pcalloc(sz);
/* Set up basic info */
pt->schema = s;
pt->readonly = PC_FALSE;
return pt;
};
PCPOINT *
pc_point_new_from_data(const PCSCHEMA *s, uint8_t *data)
{
size_t sz;
PCPOINT *pt;
if ( ! s )
{
pcerror("null schema passed into pc_point_new_from_data");
return NULL;
}
/* Reference the external data */
pt = pcalloc(sizeof(PCPOINT));
pt->data = data;
/* Set up basic info */
pt->schema = s;
pt->readonly = PC_TRUE;
return pt;
}
static double
pc_point_get_double(const PCPOINT *pt, const PCDIMENSION *d)
{
uint8_t *ptr;
double val;
/* Read raw value from byte buffer */
ptr = pt->data + d->byteoffset;
val = pc_double_from_ptr(ptr, d->interpretation);
/* Scale value */
if ( d->scale )
val *= d->scale;
/* Offset value */
if ( d->offset )
val += d->offset;
return val;
}
double
pc_point_get_double_by_name(const PCPOINT *pt, const char *name)
{
PCDIMENSION *d;
d = pc_schema_get_dimension_by_name(pt->schema, name);
return pc_point_get_double(pt, d);
}
double
pc_point_get_double_by_idx(const PCPOINT *pt, uint32_t idx)
{
PCDIMENSION *d;
d = pc_schema_get_dimension(pt->schema, idx);
return pc_point_get_double(pt, d);
}
int
pc_point_set_double(PCPOINT *pt, const PCDIMENSION *d, double val)
{
uint8_t *ptr;
/* Offset value */
if ( d->offset )
val -= d->offset;
/* Scale value */
if ( d->scale )
val /= d->scale;
/* Get pointer into byte buffer */
ptr = pt->data + d->byteoffset;
return pc_double_to_ptr(ptr, d->interpretation, val);
}
int
pc_point_set_double_by_idx(PCPOINT *pt, uint32_t idx, double val)
{
PCDIMENSION *d;
d = pc_schema_get_dimension(pt->schema, idx);
return pc_point_set_double(pt, d, val);
}
int
pc_point_set_double_by_name(PCPOINT *pt, const char *name, double val)
{
PCDIMENSION *d;
d = pc_schema_get_dimension_by_name(pt->schema, name);
return pc_point_set_double(pt, d, val);
}