File: cu_pc_point.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 (323 lines) | stat: -rw-r--r-- 10,644 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
/***********************************************************************
 * cu_pc_schema.c
 *
 *        Testing for the schema API functions
 *
 * Portions Copyright (c) 2012, OpenGeo
 *
 ***********************************************************************/

#include "CUnit/Basic.h"
#include "cu_tester.h"

/* GLOBALS ************************************************************/

static PCSCHEMA *schema = NULL;
static PCSCHEMA *schema_xy = NULL;
static PCSCHEMA *schema_xyz = NULL;
static PCSCHEMA *schema_xym = NULL;
static PCSCHEMA *schema_xyzm = NULL;
static const char *xmlfile = "data/simple-schema.xml";
static const char *xmlfile_xy = "data/simple-schema-xy.xml";
static const char *xmlfile_xyz = "data/simple-schema-xyz.xml";
static const char *xmlfile_xym = "data/simple-schema-xym.xml";
static const char *xmlfile_xyzm = "data/simple-schema-xyzm.xml";

// SIMPLE SCHEMA
// int32_t x
// int32_t y
// int32_t z
// int16_t intensity

/* Setup/teardown for this suite */
static int init_suite(void)
{
  char *xmlstr;

  xmlstr = file_to_str(xmlfile);
  schema = pc_schema_from_xml(xmlstr);
  pcfree(xmlstr);
  if (!schema)
    return 1;

  xmlstr = file_to_str(xmlfile_xy);
  schema_xy = pc_schema_from_xml(xmlstr);
  pcfree(xmlstr);
  if (!schema_xy)
    return 1;

  xmlstr = file_to_str(xmlfile_xyz);
  schema_xyz = pc_schema_from_xml(xmlstr);
  pcfree(xmlstr);
  if (!schema_xyz)
    return 1;

  xmlstr = file_to_str(xmlfile_xym);
  schema_xym = pc_schema_from_xml(xmlstr);
  pcfree(xmlstr);
  if (!schema_xym)
    return 1;

  xmlstr = file_to_str(xmlfile_xyzm);
  schema_xyzm = pc_schema_from_xml(xmlstr);
  pcfree(xmlstr);
  if (!schema_xyzm)
    return 1;
  return 0;
}

static int clean_suite(void)
{
  pc_schema_free(schema);
  pc_schema_free(schema_xy);
  pc_schema_free(schema_xyz);
  pc_schema_free(schema_xym);
  pc_schema_free(schema_xyzm);
  return 0;
}

/* TESTS **************************************************************/

static void test_point_hex_inout()
{
  // byte:     endianness (1 = NDR, 0 = XDR)
  // uint32:   pcid (key to POINTCLOUD_SCHEMAS)
  // uchar[]:  pointdata (interpret relative to pcid)

  double d;
  char *hexbuf = "00000000010000000100000002000000030004";
  size_t hexsize = strlen(hexbuf);
  uint8_t *wkb = pc_bytes_from_hexbytes(hexbuf, hexsize);
  PCPOINT *pt = pc_point_from_wkb(schema, wkb, hexsize / 2);
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "X", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, 0.01, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Y", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, 0.02, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Z", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, 0.03, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Intensity", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, 4, 0.0001);
  CU_ASSERT_FAILURE(pc_point_get_double_by_name(pt, "M", &d));
  pc_point_free(pt);
  pcfree(wkb);

  hexbuf = "01010000000100000002000000030000000500";
  hexsize = strlen(hexbuf);
  wkb = pc_bytes_from_hexbytes(hexbuf, hexsize);
  pt = pc_point_from_wkb(schema, wkb, hexsize / 2);
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "X", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, 0.01, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Y", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, 0.02, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Z", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, 0.03, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Intensity", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, 5, 0.0001);
  CU_ASSERT_FAILURE(pc_point_get_double_by_name(pt, "M", &d));
  pc_point_free(pt);
  pcfree(wkb);
}

static void test_point_access()
{
  PCPOINT *pt;
  double a1, a2, a3, a4, b1, b2, b3, b4;
  int idx = 0;
  double *allvals;

  pt = pc_point_make(schema);
  CU_ASSERT_PTR_NOT_NULL(pt);

  /* One at a time */
  idx = 0;
  a1 = 1.5;
  CU_ASSERT_SUCCESS(pc_point_set_double_by_index(pt, idx, a1));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_index(pt, idx, &b1));
  // printf("d1=%g, d2=%g\n", a1, b1);
  CU_ASSERT_DOUBLE_EQUAL(a1, b1, 0.0000001);

  idx = 2;
  a2 = 1501500.12;
  CU_ASSERT_SUCCESS(pc_point_set_double_by_index(pt, idx, a2));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_index(pt, idx, &b2));
  CU_ASSERT_DOUBLE_EQUAL(a2, b2, 0.0000001);

  a3 = 91;
  CU_ASSERT_SUCCESS(pc_point_set_double_by_name(pt, "Intensity", a3));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Intensity", &b3));
  CU_ASSERT_DOUBLE_EQUAL(a3, b3, 0.0000001);

  pc_point_free(pt);

  /* All at once */
  pt = pc_point_make(schema);
  a1 = 1.5;
  a2 = 1501500.12;
  a3 = 91;
  a4 = 200;
  CU_ASSERT_SUCCESS(pc_point_set_double_by_index(pt, 0, a1));
  CU_ASSERT_SUCCESS(pc_point_set_double_by_index(pt, 1, a2));
  CU_ASSERT_SUCCESS(pc_point_set_double_by_name(pt, "Intensity", a3));
  CU_ASSERT_SUCCESS(pc_point_set_double_by_name(pt, "Z", a4));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_index(pt, 0, &b1));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_index(pt, 1, &b2));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Intensity", &b3));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Z", &b4));
  CU_ASSERT_DOUBLE_EQUAL(a1, b1, 0.0000001);
  CU_ASSERT_DOUBLE_EQUAL(a2, b2, 0.0000001);
  CU_ASSERT_DOUBLE_EQUAL(a3, b3, 0.0000001);
  CU_ASSERT_DOUBLE_EQUAL(a4, b4, 0.0000001);

  /* as a double array */
  pc_point_set_double_by_index(pt, 0, a1);
  pc_point_set_double_by_index(pt, 1, a2);
  pc_point_set_double_by_index(pt, 2, a3);
  pc_point_set_double_by_index(pt, 3, a4);
  allvals = pc_point_to_double_array(pt);
  CU_ASSERT_DOUBLE_EQUAL(allvals[0], a1, 0.0000001);
  CU_ASSERT_DOUBLE_EQUAL(allvals[1], a2, 0.0000001);
  // printf("allvals[2]:%g\n", allvals[2]);
  CU_ASSERT_DOUBLE_EQUAL(allvals[2], a3, 0.0000001);
  // printf("allvals[3]:%g\n", allvals[3]);
  CU_ASSERT_DOUBLE_EQUAL(allvals[3], a4, 0.0000001);
  pcfree(allvals);

  pc_point_free(pt);
}

static void test_point_xyzm()
{
  PCPOINT *pt;
  double x = 1;
  double y = 40;
  double z = 160;
  double m = 640;
  double d;

  pt = pc_point_make(schema_xyz);
  CU_ASSERT_PTR_NOT_NULL(pt);

  CU_ASSERT_SUCCESS(pc_point_set_x(pt, x));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "X", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, x, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_x(pt, &d));
  CU_ASSERT_DOUBLE_EQUAL(d, x, 0.000001);

  CU_ASSERT_SUCCESS(pc_point_set_y(pt, y));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Y", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, y, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_y(pt, &d));
  CU_ASSERT_DOUBLE_EQUAL(d, y, 0.000001);

  CU_ASSERT_SUCCESS(pc_point_set_z(pt, z));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Z", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, z, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_z(pt, &d));
  CU_ASSERT_DOUBLE_EQUAL(d, z, 0.000001);

  CU_ASSERT_FAILURE(pc_point_set_m(pt, m));
  CU_ASSERT_FAILURE(pc_point_get_double_by_name(pt, "M", &d));
  CU_ASSERT_FAILURE(pc_point_get_m(pt, &d));

  pc_point_free(pt);

  pt = pc_point_make(schema_xyzm);
  CU_ASSERT_PTR_NOT_NULL(pt);

  CU_ASSERT_SUCCESS(pc_point_set_x(pt, x));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "X", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, x, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_x(pt, &d));
  CU_ASSERT_DOUBLE_EQUAL(d, x, 0.000001);

  CU_ASSERT_SUCCESS(pc_point_set_y(pt, y));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Y", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, y, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_y(pt, &d));
  CU_ASSERT_DOUBLE_EQUAL(d, y, 0.000001);

  CU_ASSERT_SUCCESS(pc_point_set_z(pt, z));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "Z", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, z, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_z(pt, &d));
  CU_ASSERT_DOUBLE_EQUAL(d, z, 0.000001);

  CU_ASSERT_SUCCESS(pc_point_set_m(pt, m));
  CU_ASSERT_SUCCESS(pc_point_get_double_by_name(pt, "M", &d));
  CU_ASSERT_DOUBLE_EQUAL(d, m, 0.000001);
  CU_ASSERT_SUCCESS(pc_point_get_m(pt, &d));
  CU_ASSERT_DOUBLE_EQUAL(d, m, 0.000001);

  pc_point_free(pt);
}

void test_point_geometry_bytes(const PCSCHEMA *s, size_t expectedgeomwkbsize,
                               const char *pthexbytes,
                               const char *expectedgeomhexbytes)
{
  /* point
  byte:     endianness (1 = NDR, 0 = XDR)
  uint32:   pcid (key to POINTCLOUD_SCHEMAS)
  uchar[]:  data (interpret relative to pcid)
  */

  /* geometry
  byte:     endianness (1 = NDR, 0 = XDR)
  uint32:   point type (XYZ=01000080, XYM=01000040, XY=01000000,
  XYZM=010000C0) double[]: XY(Z?)(M?) coordinates
  */

  PCPOINT *pt;
  uint8_t *ptwkb, *geomwkb;
  char *geomhexbytes;
  size_t pthexsize, geomwkbsize;

  pthexsize = strlen(pthexbytes);
  ptwkb = pc_bytes_from_hexbytes(pthexbytes, pthexsize);
  pt = pc_point_from_wkb(s, ptwkb, pthexsize / 2);
  CU_ASSERT_PTR_NOT_NULL(pt);
  geomwkb = pc_point_to_geometry_wkb(pt, &geomwkbsize);
  CU_ASSERT_EQUAL(geomwkbsize, expectedgeomwkbsize);
  geomhexbytes = pc_hexbytes_from_bytes(geomwkb, geomwkbsize);
  CU_ASSERT_STRING_EQUAL(geomhexbytes, expectedgeomhexbytes);

  pcfree(geomhexbytes);
  pcfree(geomwkb);
  pc_point_free(pt);
  pcfree(ptwkb);
}

static void test_point_geometry()
{
  // pt XYI = 1 2 3, scale = 1 2 1, geom XY = 1 4
  test_point_geometry_bytes(schema_xy, 5 + 2 * 8,
                            "000000000100000001000000020003",
                            "0101000000000000000000F03F0000000000001040");

  // pt XYZI = 1 2 3 4, scale = 1 2 4 1, geom XYZ = 1 2 3
  test_point_geometry_bytes(
      schema_xyz, 5 + 3 * 8, "00000000010000000100000002000000030004",
      "0101000080000000000000F03F00000000000010400000000000002840");

  // pt XYMI = 1 2 3 4, scale = 1 2 4 1, geom XYM = 1 4 12
  test_point_geometry_bytes(
      schema_xym, 5 + 3 * 8, "00000000010000000100000002000000030004",
      "0101000040000000000000F03F00000000000010400000000000002840");

  // pt XYZMI = 1 2 3 4 5, scale = 1 2 4 8 1, geom XYZM = 1 4 12 32
  test_point_geometry_bytes(schema_xyzm, 5 + 4 * 8,
                            "0000000001000000010000000200000003000000040005",
                            "01010000C0000000000000F03F000000000000104000000000"
                            "000028400000000000004040");
}

/* REGISTER ***********************************************************/

CU_TestInfo point_tests[] = {
    PC_TEST(test_point_hex_inout), PC_TEST(test_point_access),
    PC_TEST(test_point_xyzm), PC_TEST(test_point_geometry), CU_TEST_INFO_NULL};

CU_SuiteInfo point_suite = {.pName = "point",
                            .pInitFunc = init_suite,
                            .pCleanupFunc = clean_suite,
                            .pTests = point_tests};