File: pc_patch_dimensional.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 (334 lines) | stat: -rw-r--r-- 9,058 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/***********************************************************************
 * pc_patch_dimensional.c
 *
 *  Pointclound patch handling. Create, get and set values from the
 *  dimensional PCPATCH structure.
 *
 *  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 <assert.h>
#include <math.h>

/*
typedef struct
{
        int type;
        int8_t readonly;
        const PCSCHEMA *schema;
        uint32_t npoints;
        double xmin, xmax, ymin, ymax;
        PCSTATS *stats;
        PCBYTES *bytes;
} PCPATCH_DIMENSIONAL;
*/

PCPATCH_DIMENSIONAL *
pc_patch_dimensional_clone(const PCPATCH_DIMENSIONAL *patch)
{
  PCPATCH_DIMENSIONAL *pdl = pcalloc(sizeof(PCPATCH_DIMENSIONAL));
  memcpy(pdl, patch, sizeof(PCPATCH_DIMENSIONAL));
  pdl->bytes = pcalloc(patch->schema->ndims * sizeof(PCBYTES));
  pdl->npoints = 0;
  pdl->stats = NULL;
  return pdl;
}

size_t pc_patch_dimensional_serialized_size(const PCPATCH_DIMENSIONAL *patch)
{
  PCPATCH_DIMENSIONAL *p = (PCPATCH_DIMENSIONAL *)patch;
  int i;
  size_t size = 0;
  for (i = 0; i < p->schema->ndims; i++)
  {
    size += pc_bytes_serialized_size(&(p->bytes[i]));
  }
  return size;
}

char *pc_patch_dimensional_to_string(const PCPATCH_DIMENSIONAL *pa)
{
  PCPATCH_UNCOMPRESSED *patch = pc_patch_uncompressed_from_dimensional(pa);
  char *str = pc_patch_uncompressed_to_string(patch);
  pc_patch_free((PCPATCH *)patch);
  return str;
}

PCPATCH_DIMENSIONAL *
pc_patch_dimensional_from_uncompressed(const PCPATCH_UNCOMPRESSED *pa)
{
  PCPATCH_DIMENSIONAL *pdl;
  const PCSCHEMA *schema;
  int i, j, ndims, npoints;

  assert(pa);
  npoints = pa->npoints;
  schema = pa->schema;
  ndims = schema->ndims;

  /* Cannot handle empty patches */
  if (npoints == 0)
    return NULL;

  /* Initialize dimensional */
  pdl = pcalloc(sizeof(PCPATCH_DIMENSIONAL));
  pdl->type = PC_DIMENSIONAL;
  pdl->readonly = PC_FALSE;
  pdl->schema = schema;
  pdl->npoints = npoints;
  pdl->bounds = pa->bounds;
  pdl->stats = pc_stats_clone(pa->stats);
  pdl->bytes = pcalloc(ndims * sizeof(PCBYTES));

  for (i = 0; i < ndims; i++)
  {
    PCDIMENSION *dim = pc_schema_get_dimension(schema, i);
    pdl->bytes[i] = pc_bytes_make(dim, npoints);
    for (j = 0; j < npoints; j++)
    {
      uint8_t *to = pdl->bytes[i].bytes + dim->size * j;
      uint8_t *from = pa->data + schema->size * j + dim->byteoffset;
      memcpy(to, from, dim->size);
    }
  }
  return pdl;
}

PCPATCH_DIMENSIONAL *
pc_patch_dimensional_compress(const PCPATCH_DIMENSIONAL *pdl,
                              PCDIMSTATS *pds_in)
{
  int i;
  int ndims;
  PCPATCH_DIMENSIONAL *pdl_compressed;
  PCDIMSTATS *pds = pds_in;

  assert(pdl);
  assert(pdl->schema);

  ndims = pdl->schema->ndims;

  if (!pds)
    pds = pc_dimstats_make(pdl->schema);

  /* Still sampling, update stats */
  if (pds->total_points < PCDIMSTATS_MIN_SAMPLE)
    pc_dimstats_update(pds, pdl);

  pdl_compressed = pcalloc(sizeof(PCPATCH_DIMENSIONAL));
  memcpy(pdl_compressed, pdl, sizeof(PCPATCH_DIMENSIONAL));
  pdl_compressed->bytes = pcalloc(ndims * sizeof(PCBYTES));
  pdl_compressed->stats = pc_stats_clone(pdl->stats);

  /* Compress each dimension as dictated by stats */
  for (i = 0; i < ndims; i++)
  {
    pdl_compressed->bytes[i] =
        pc_bytes_encode(pdl->bytes[i], pds->stats[i].recommended_compression);
  }

  if (pds != pds_in)
    pc_dimstats_free(pds);

  return pdl_compressed;
}

PCPATCH_DIMENSIONAL *
pc_patch_dimensional_decompress(const PCPATCH_DIMENSIONAL *pdl)
{
  int i;
  int ndims = pdl->schema->ndims;
  PCPATCH_DIMENSIONAL *pdl_decompressed;

  assert(pdl);
  assert(pdl->schema);

  pdl_decompressed = pcalloc(sizeof(PCPATCH_DIMENSIONAL));
  memcpy(pdl_decompressed, pdl, sizeof(PCPATCH_DIMENSIONAL));
  pdl_decompressed->bytes = pcalloc(ndims * sizeof(PCBYTES));
  pdl_decompressed->stats = pc_stats_clone(pdl->stats);

  /* Compress each dimension as dictated by stats */
  for (i = 0; i < ndims; i++)
  {
    pdl_decompressed->bytes[i] = pc_bytes_decode(pdl->bytes[i]);
  }

  return pdl_decompressed;
}

void pc_patch_dimensional_free(PCPATCH_DIMENSIONAL *pdl)
{
  int i;
  assert(pdl);
  assert(pdl->schema);

  pc_patch_free_stats((PCPATCH *)pdl);

  if (pdl->bytes)
  {
    for (i = 0; i < pdl->schema->ndims; i++)
      pc_bytes_free(pdl->bytes[i]);

    pcfree(pdl->bytes);
  }

  pcfree(pdl);
}

int pc_patch_dimensional_compute_extent(PCPATCH_DIMENSIONAL *pdl)
{
  double xmin, xmax, ymin, ymax, xavg, yavg;
  int rv;
  PCBYTES *pcb;

  assert(pdl);
  assert(pdl->schema);
  assert(pdl->schema->xdim);
  assert(pdl->schema->ydim);

  /* Get x extremes */
  pcb = &(pdl->bytes[pdl->schema->xdim->position]);
  rv = pc_bytes_minmax(pcb, &xmin, &xmax, &xavg);
  if (PC_FAILURE == rv)
    return PC_FAILURE;
  xmin = pc_value_scale_offset(xmin, pdl->schema->xdim);
  xmax = pc_value_scale_offset(xmax, pdl->schema->xdim);
  pdl->bounds.xmin = xmin;
  pdl->bounds.xmax = xmax;

  /* Get y extremes */
  pcb = &(pdl->bytes[pdl->schema->ydim->position]);
  rv = pc_bytes_minmax(pcb, &ymin, &ymax, &yavg);
  if (PC_FAILURE == rv)
    return PC_FAILURE;
  ymin = pc_value_scale_offset(ymin, pdl->schema->ydim);
  ymax = pc_value_scale_offset(ymax, pdl->schema->ydim);
  pdl->bounds.ymin = ymin;
  pdl->bounds.ymax = ymax;

  return PC_SUCCESS;
}

uint8_t *pc_patch_dimensional_to_wkb(const PCPATCH_DIMENSIONAL *patch,
                                     size_t *wkbsize)
{
  /*
  byte:     endianness (1 = NDR, 0 = XDR)
  uint32:   pcid (key to POINTCLOUD_SCHEMAS)
  uint32:   compression (0 = no compression, 1 = dimensional, 2 = lazperf)
  uint32:   npoints
  dimensions[]:  pcbytes (interpret relative to pcid and compressions)
  */
  int ndims = patch->schema->ndims;
  int i;
  uint8_t *buf;
  char endian = machine_endian();
  /* endian + pcid + compression + npoints + datasize */
  size_t size = 1 + 4 + 4 + 4 + pc_patch_dimensional_serialized_size(patch);
  uint8_t *wkb = pcalloc(size);
  uint32_t compression = patch->type;
  uint32_t npoints = patch->npoints;
  uint32_t pcid = patch->schema->pcid;
  wkb[0] = endian;                  /* Write endian flag */
  memcpy(wkb + 1, &pcid, 4);        /* Write PCID */
  memcpy(wkb + 5, &compression, 4); /* Write compression */
  memcpy(wkb + 9, &npoints, 4);     /* Write npoints */

  buf = wkb + 13;
  for (i = 0; i < ndims; i++)
  {
    size_t bsz;
    PCBYTES *pcb = &(patch->bytes[i]);
    // XXX        printf("pcb->(size=%d, interp=%d, npoints=%d,
    // compression=%d, readonly=%d)\n",pcb->size, pcb->interpretation,
    // pcb->npoints, pcb->compression, pcb->readonly);

    pc_bytes_serialize(pcb, buf, &bsz);
    buf += bsz;
  }

  if (wkbsize)
    *wkbsize = size;
  return wkb;
}

PCPATCH *pc_patch_dimensional_from_wkb(const PCSCHEMA *schema,
                                       const uint8_t *wkb, size_t wkbsize)
{
  /*
  byte:     endianness (1 = NDR, 0 = XDR)
  uint32:   pcid (key to POINTCLOUD_SCHEMAS)
  uint32:   compression (0 = no compression, 1 = dimensional, 2 = lazperf)
  uint32:   npoints
  dimensions[]:  dims (interpret relative to pcid and compressions)
  */
  static size_t hdrsz =
      1 + 4 + 4 + 4; /* endian + pcid + compression + npoints */
  PCPATCH_DIMENSIONAL *patch;
  uint8_t swap_endian = (wkb[0] != machine_endian());
  uint32_t npoints, ndims;
  const uint8_t *buf;
  int i;

  if (wkb_get_compression(wkb) != PC_DIMENSIONAL)
  {
    pcerror("%s: call with wkb that is not dimensionally compressed", __func__);
    return NULL;
  }

  npoints = wkb_get_npoints(wkb);
  ndims = schema->ndims;

  patch = pcalloc(sizeof(PCPATCH_DIMENSIONAL));
  patch->type = PC_DIMENSIONAL;
  patch->readonly = PC_FALSE;
  patch->schema = schema;
  patch->npoints = npoints;
  patch->bytes = pcalloc(ndims * sizeof(PCBYTES));
  patch->stats = NULL;

  buf = wkb + hdrsz;
  for (i = 0; i < ndims; i++)
  {
    PCBYTES *pcb = &(patch->bytes[i]);
    PCDIMENSION *dim = schema->dims[i];
    pc_bytes_deserialize(buf, dim, pcb, PC_FALSE /*readonly*/, swap_endian);
    pcb->npoints = npoints;
    buf += pc_bytes_serialized_size(pcb);
  }

  return (PCPATCH *)patch;
}

PCPATCH_DIMENSIONAL *pc_patch_dimensional_from_pointlist(const PCPOINTLIST *pdl)
{
  PCPATCH_UNCOMPRESSED *patch = pc_patch_uncompressed_from_pointlist(pdl);
  if (!patch)
    return NULL;
  PCPATCH_DIMENSIONAL *dimpatch = pc_patch_dimensional_from_uncompressed(patch);
  pc_patch_free((PCPATCH *)patch);
  return dimpatch;
}

/** get point n, 0-based, positive */
PCPOINT *pc_patch_dimensional_pointn(const PCPATCH_DIMENSIONAL *pdl, int n)
{
  assert(pdl);
  assert(pdl->schema);
  int i;
  int ndims = pdl->schema->ndims;
  PCPOINT *pt = pc_point_make(pdl->schema);
  uint8_t *buf = pt->data;
  for (i = 0; i < ndims; i++)
  {
    PCDIMENSION *dim = pc_schema_get_dimension(pdl->schema, i);
    pc_bytes_to_ptr(buf + dim->byteoffset, pdl->bytes[i], n);
  }

  return pt;
}