File: pc_val.c

package info (click to toggle)
pgpointcloud 1.2.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,892 kB
  • sloc: sql: 40,767; ansic: 11,045; xml: 935; makefile: 297; cpp: 282; perl: 248; python: 178; sh: 92
file content (225 lines) | stat: -rw-r--r-- 5,238 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/***********************************************************************
 * pc_val.c
 *
 *  Pointclound value handling. Create, get and set values.
 *
 *  PgSQL Pointcloud is free and open source software provided
 *  by the Government of Canada
 *  Copyright (c) 2013 Natural Resources Canada
 *
 ***********************************************************************/

#include "pc_api_internal.h"
#include <math.h>
#include <stdint.h>

double pc_value_unscale_unoffset(double val, const PCDIMENSION *dim)
{
  /* Offset value */
  if (dim->offset)
    val -= dim->offset;

  /* Scale value */
  if (dim->scale != 1)
    val /= dim->scale;

  return val;
}

double pc_value_scale_offset(double val, const PCDIMENSION *dim)
{
  /* Scale value */
  if (dim->scale != 1)
    val *= dim->scale;

  /* Offset value */
  if (dim->offset)
    val += dim->offset;

  return val;
}

double pc_value_from_ptr(const uint8_t *ptr, const PCDIMENSION *dim)
{
  double val = pc_double_from_ptr(ptr, dim->interpretation);
  return pc_value_scale_offset(val, dim);
}

double pc_double_from_ptr(const uint8_t *ptr, uint32_t interpretation)
{
  switch (interpretation)
  {
  case PC_UINT8:
  {
    uint8_t v;
    memcpy(&(v), ptr, sizeof(uint8_t));
    return (double)v;
  }
  case PC_UINT16:
  {
    uint16_t v;
    memcpy(&(v), ptr, sizeof(uint16_t));
    return (double)v;
  }
  case PC_UINT32:
  {
    uint32_t v;
    memcpy(&(v), ptr, sizeof(uint32_t));
    return (double)v;
  }
  case PC_UINT64:
  {
    uint64_t v;
    memcpy(&(v), ptr, sizeof(uint64_t));
    return (double)v;
  }
  case PC_INT8:
  {
    int8_t v;
    memcpy(&(v), ptr, sizeof(int8_t));
    return (double)v;
  }
  case PC_INT16:
  {
    int16_t v;
    memcpy(&(v), ptr, sizeof(int16_t));
    return (double)v;
  }
  case PC_INT32:
  {
    int32_t v;
    memcpy(&(v), ptr, sizeof(int32_t));
    return (double)v;
  }
  case PC_INT64:
  {
    int64_t v;
    memcpy(&(v), ptr, sizeof(int64_t));
    return (double)v;
  }
  case PC_FLOAT:
  {
    float v;
    memcpy(&(v), ptr, sizeof(float));
    return (double)v;
  }
  case PC_DOUBLE:
  {
    double v;
    memcpy(&(v), ptr, sizeof(double));
    return v;
  }
  default:
  {
    pcerror("unknown interpretation type %d encountered in pc_double_from_ptr",
            interpretation);
  }
  }
  return 0.0;
}

#define CLAMP(v, min, max, t, format)                                          \
  do                                                                           \
  {                                                                            \
    if (v > max)                                                               \
    {                                                                          \
      pcwarn("Value %g truncated to " format " to fit in " t, v, max);         \
      v = max;                                                                 \
    }                                                                          \
    else if (v < min)                                                          \
    {                                                                          \
      pcwarn("Value %g truncated to " format " to fit in " t, v, min);         \
      v = min;                                                                 \
    }                                                                          \
  } while (0)

int pc_double_to_ptr(uint8_t *ptr, uint32_t interpretation, double val)
{
  switch (interpretation)
  {
  case PC_UINT8:
  {
    uint8_t v;
    CLAMP(val, 0, UINT8_MAX, "uint8_t", "%u");
    v = (uint8_t)lround(val);
    memcpy(ptr, &(v), sizeof(uint8_t));
    break;
  }
  case PC_UINT16:
  {
    uint16_t v;
    CLAMP(val, 0, UINT16_MAX, "uint16_t", "%u");
    v = (uint16_t)lround(val);
    memcpy(ptr, &(v), sizeof(uint16_t));
    break;
  }
  case PC_UINT32:
  {
    uint32_t v;
    CLAMP(val, 0, UINT32_MAX, "uint32", "%u");
    v = (uint32_t)lround(val);
    memcpy(ptr, &(v), sizeof(uint32_t));
    break;
  }
  case PC_UINT64:
  {
    uint64_t v;
    CLAMP(val, 0, UINT64_MAX, "uint64", "%u");
    v = (uint64_t)lround(val);
    memcpy(ptr, &(v), sizeof(uint64_t));
    break;
  }
  case PC_INT8:
  {
    int8_t v;
    CLAMP(val, INT8_MIN, INT8_MAX, "int8", "%d");
    v = (int8_t)lround(val);
    memcpy(ptr, &(v), sizeof(int8_t));
    break;
  }
  case PC_INT16:
  {
    int16_t v;
    CLAMP(val, INT16_MIN, INT16_MAX, "int16", "%d");
    v = (int16_t)lround(val);
    memcpy(ptr, &(v), sizeof(int16_t));
    break;
  }
  case PC_INT32:
  {
    int32_t v;
    CLAMP(val, INT32_MIN, INT32_MAX, "int32", "%d");
    v = (int32_t)lround(val);
    memcpy(ptr, &(v), sizeof(int32_t));
    break;
  }
  case PC_INT64:
  {
    int64_t v;
    CLAMP(val, INT64_MIN, INT64_MAX, "int64", "%d");
    v = (int64_t)lround(val);
    memcpy(ptr, &(v), sizeof(int64_t));
    break;
  }
  case PC_FLOAT:
  {
    float v = (float)val;
    memcpy(ptr, &(v), sizeof(float));
    break;
  }
  case PC_DOUBLE:
  {
    double v = val;
    memcpy(ptr, &(v), sizeof(double));
    break;
  }
  default:
  {
    pcerror("unknown interpretation type %d encountered in pc_double_to_ptr",
            interpretation);
    return PC_FAILURE;
  }
  }

  return PC_SUCCESS;
}