File: lwout_x3d.c

package info (click to toggle)
postgis 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 70,052 kB
  • sloc: ansic: 162,204; sql: 93,950; xml: 53,121; cpp: 12,646; perl: 5,658; sh: 5,369; makefile: 3,434; python: 1,205; yacc: 447; lex: 151; pascal: 58
file content (565 lines) | stat: -rw-r--r-- 15,709 bytes parent folder | download | duplicates (5)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
 * http://postgis.net
 *
 * PostGIS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * PostGIS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PostGIS.  If not, see <http://www.gnu.org/licenses/>.
 *
 **********************************************************************
 *
 * Copyright 2011-2017 Arrival 3D, Regina Obe
 *
 **********************************************************************/

/**
* @file X3D output routines.
*
**********************************************************************/

#include "lwout_x3d.h"

/*
 * VERSION X3D 3.0.2 http://www.web3d.org/specifications/x3d-3.0.dtd
 */
/* takes a GEOMETRY and returns a X3D representation */
lwvarlena_t *
lwgeom_to_x3d3(const LWGEOM *geom, int precision, int opts, const char *defid)
{
	stringbuffer_t *sb;
	int rv;

	/* Empty varlena for empties */
	if( lwgeom_is_empty(geom) )
	{
		lwvarlena_t *v = lwalloc(LWVARHDRSZ);
		LWSIZE_SET(v->size, LWVARHDRSZ);
		return v;
	}

	sb = stringbuffer_create();
	rv = lwgeom_to_x3d3_sb(geom, precision, opts, defid, sb);

	if ( rv == LW_FAILURE )
	{
		stringbuffer_destroy(sb);
		return NULL;
	}

	lwvarlena_t *v = stringbuffer_getvarlenacopy(sb);
	stringbuffer_destroy(sb);

	return v;
}
/* takes a GEOMETRY and appends to string buffer the x3d output */
static int
lwgeom_to_x3d3_sb(const LWGEOM *geom, int precision, int opts, const char *defid, stringbuffer_t *sb)
{
	int type = geom->type;

	switch (type)
	{
	case POINTTYPE:
		return asx3d3_point_sb((LWPOINT *)geom, precision, opts, defid, sb);

	case LINETYPE:
		return asx3d3_line_sb((LWLINE *)geom, precision, opts, defid, sb);

	case POLYGONTYPE:
	{
		/** We might change this later, but putting a polygon in an indexed face set
		* seems like the simplest way to go so treat just like a mulitpolygon
		*/
		LWCOLLECTION *tmp = (LWCOLLECTION*)lwgeom_as_multi(geom);
		asx3d3_multi_sb(tmp, precision, opts, defid, sb);
		lwcollection_free(tmp);
		return LW_SUCCESS;
	}

	case TRIANGLETYPE:
		return asx3d3_triangle_sb((LWTRIANGLE *)geom, precision, opts, defid, sb);

	case MULTIPOINTTYPE:
	case MULTILINETYPE:
	case MULTIPOLYGONTYPE:
		return asx3d3_multi_sb((LWCOLLECTION *)geom, precision, opts, defid, sb);

	case POLYHEDRALSURFACETYPE:
		return asx3d3_psurface_sb((LWPSURFACE *)geom, precision, opts, defid, sb);

	case TINTYPE:
		return asx3d3_tin_sb((LWTIN *)geom, precision, opts, defid, sb);

	case COLLECTIONTYPE:
		return asx3d3_collection_sb((LWCOLLECTION *)geom, precision, opts, defid, sb);

	default:
		lwerror("lwgeom_to_x3d3: '%s' geometry type not supported", lwtype_name(type));
		return LW_FAILURE;
	}
}

static int
asx3d3_point_sb(const LWPOINT *point,
		int precision,
		int opts,
		__attribute__((__unused__)) const char *defid,
		stringbuffer_t *sb)
{
	/** for point we just output the coordinates **/
	return ptarray_to_x3d3_sb(point->point, precision, opts, 0, sb);
}

static int
asx3d3_line_coords_sb(const LWLINE *line, int precision, int opts, stringbuffer_t *sb)
{
	return ptarray_to_x3d3_sb(line->points, precision, opts, lwline_is_closed(line), sb);
}

/* Calculate the coordIndex property of the IndexedLineSet for the multilinestring
and add to string buffer */
static int
asx3d3_mline_coordindex_sb(const LWMLINE *mgeom, stringbuffer_t *sb)
{
	LWLINE *geom;
	uint32_t i, j, k, si;
	POINTARRAY *pa;
	uint32_t np;

	j = 0;
	for (i=0; i < mgeom->ngeoms; i++)
	{
		geom = (LWLINE *) mgeom->geoms[i];
		pa = geom->points;
		np = pa->npoints;
		si = j;  /* start index of first point of linestring */
		for (k=0; k < np ; k++)
		{
			if (k)
			{
				stringbuffer_aprintf(sb, " ");
			}
			/** if the linestring is closed, we put the start point index
			*   for the last vertex to denote use first point
			*    and don't increment the index **/
			if (!lwline_is_closed(geom) || k < (np -1) )
			{
				stringbuffer_aprintf(sb, "%u", j);
				j += 1;
			}
			else
			{
				stringbuffer_aprintf(sb,"%u", si);
			}
		}
		if (i < (mgeom->ngeoms - 1) )
		{
			stringbuffer_aprintf(sb, " -1 "); /* separator for each linestring */
		}
	}
	return LW_SUCCESS;
}

/* Calculate the coordIndex property of the IndexedLineSet for a multipolygon
    This is not ideal -- would be really nice to just share this function with psurf,
    but I'm not smart enough to do that yet*/
static int
asx3d3_mpoly_coordindex_sb(const LWMPOLY *psur, stringbuffer_t *sb)
{
	LWPOLY *patch;
	uint32_t i, j, k, l;
	uint32_t np;
	j = 0;
	for (i=0; i<psur->ngeoms; i++)
	{
		patch = (LWPOLY *) psur->geoms[i];
		for (l=0; l < patch->nrings; l++)
		{
			np = patch->rings[l]->npoints - 1;
			for (k=0; k < np ; k++)
			{
				if (k)
				{
					stringbuffer_aprintf(sb,  " ");
				}
				stringbuffer_aprintf(sb,  "%d", (j + k));
			}
			j += k;
			if (l < (patch->nrings - 1) )
			{
				/** @todo TODO: Decide the best way to render holes
				*  Evidently according to my X3D expert the X3D consortium doesn't really
				*  support holes and it's an issue of argument among many that feel it should. He thinks CAD x3d extensions to spec might.
				*  What he has done and others developing X3D exports to simulate a hole is to cut around it.
				*  So if you have a donut, you would cut it into half and have 2 solid polygons.  Not really sure the best way to handle this.
				*  For now will leave it as polygons stacked on top of each other -- which is what we are doing here and perhaps an option
				*  to color differently.  It's not ideal but the alternative sounds complicated.
				**/
				stringbuffer_aprintf(sb,  " -1 "); /* separator for each inner ring. Ideally we should probably triangulate and cut around as others do */
			}
		}
		if (i < (psur->ngeoms - 1) )
		{
			stringbuffer_aprintf(sb,  " -1 "); /* separator for each subgeom */
		}
	}
	return LW_SUCCESS;
}

/** Return the linestring as an X3D LineSet */
static int
asx3d3_line_sb(const LWLINE *line,
	       int precision,
	       int opts,
	       __attribute__((__unused__)) const char *defid,
	       stringbuffer_t *sb)
{

	/* int dimension=2; */
	POINTARRAY *pa;


	/* if (FLAGS_GET_Z(line->flags)) dimension = 3; */

	pa = line->points;
	stringbuffer_aprintf(sb, "<LineSet %s vertexCount='%d'>", defid, pa->npoints);

	if ( X3D_USE_GEOCOORDS(opts) ) stringbuffer_aprintf(sb, "<GeoCoordinate geoSystem='\"GD\" \"WE\" \"%s\"' point='", ( (opts & LW_X3D_FLIP_XY) ? "latitude_first" : "longitude_first") );
	else
		stringbuffer_aprintf(sb, "<Coordinate point='");

	ptarray_to_x3d3_sb(line->points, precision, opts, lwline_is_closed((LWLINE *) line), sb);


	stringbuffer_aprintf(sb, "' />");

	return stringbuffer_aprintf(sb, "</LineSet>");
}

/** Compute the X3D coordinates of the polygon and add to string buffer **/
static int
asx3d3_poly_sb(const LWPOLY *poly,
	       int precision,
	       int opts,
	       __attribute__((__unused__)) int is_patch,
	       __attribute__((__unused__)) const char *defid,
	       stringbuffer_t *sb)
{
	uint32_t i;
	for (i=0; i<poly->nrings; i++)
	{
		if (i) stringbuffer_aprintf(sb, " "); /* inner ring points start */
		ptarray_to_x3d3_sb(poly->rings[i], precision, opts, 1, sb);
	}
	return LW_SUCCESS;
}

static int
asx3d3_triangle_sb(const LWTRIANGLE *triangle,
		   int precision,
		   int opts,
		   __attribute__((__unused__)) const char *defid,
		   stringbuffer_t *sb)
{
	return  ptarray_to_x3d3_sb(triangle->points, precision, opts, 1, sb);
}


/*
 * Don't call this with single-geoms inspected!
 */
static int
asx3d3_multi_sb(const LWCOLLECTION *col, int precision, int opts, const char *defid, stringbuffer_t *sb)
{
	char *x3dtype;
	uint32_t i;
	int dimension=2;

	if (FLAGS_GET_Z(col->flags)) dimension = 3;
	LWGEOM *subgeom;
	x3dtype="";


	switch (col->type)
	{
        case MULTIPOINTTYPE:
            x3dtype = "PointSet";
            if ( dimension == 2 ){ /** Use Polypoint2D instead **/
                x3dtype = "Polypoint2D";
                stringbuffer_aprintf(sb, "<%s %s point='", x3dtype, defid);
            }
            else {
                stringbuffer_aprintf(sb, "<%s %s>", x3dtype, defid);
            }
            break;
        case MULTILINETYPE:
            x3dtype = "IndexedLineSet";
            stringbuffer_aprintf(sb, "<%s %s coordIndex='", x3dtype, defid);
            asx3d3_mline_coordindex_sb((const LWMLINE *)col, sb);
            stringbuffer_aprintf(sb, "'>");
            break;
        case MULTIPOLYGONTYPE:
            x3dtype = "IndexedFaceSet";
            stringbuffer_aprintf(sb, "<%s %s convex='false' coordIndex='", x3dtype, defid);
            asx3d3_mpoly_coordindex_sb((const LWMPOLY *)col, sb);
            stringbuffer_aprintf(sb, "'>");
            break;
        default:
            lwerror("asx3d3_multi_buf: '%s' geometry type not supported", lwtype_name(col->type));
            return 0;
    }
    if (dimension == 3){
		if ( X3D_USE_GEOCOORDS(opts) )
			stringbuffer_aprintf(sb, "<GeoCoordinate geoSystem='\"GD\" \"WE\" \"%s\"' point='", ((opts & LW_X3D_FLIP_XY) ? "latitude_first" : "longitude_first") );
		else
        	stringbuffer_aprintf(sb, "<Coordinate point='");
    }

	for (i=0; i<col->ngeoms; i++)
	{
		subgeom = col->geoms[i];
		if (subgeom->type == POINTTYPE)
		{
			asx3d3_point_sb((LWPOINT *)subgeom, precision, opts, defid, sb);
			stringbuffer_aprintf(sb, " ");
		}
		else if (subgeom->type == LINETYPE)
		{
			asx3d3_line_coords_sb((LWLINE*)subgeom, precision, opts, sb);
			stringbuffer_aprintf(sb, " ");
		}
		else if (subgeom->type == POLYGONTYPE)
		{
			asx3d3_poly_sb((LWPOLY *)subgeom, precision, opts, 0, defid, sb);
			stringbuffer_aprintf(sb, " ");
		}
	}

	/* Close outmost tag */
	if (dimension == 3){
		stringbuffer_aprintf(sb, "' /></%s>", x3dtype);
	}
	else { stringbuffer_aprintf(sb, "' />"); }
	return LW_SUCCESS;

}

/*
 * Don't call this with single-geoms inspected!
 */
static int
asx3d3_psurface_sb(const LWPSURFACE *psur, int precision, int opts, const char *defid, stringbuffer_t *sb)
{
	uint32_t i;
	uint32_t j;
	uint32_t k;
	uint32_t np;
	LWPOLY *patch;

	/* Open outmost tag */
	stringbuffer_aprintf(sb, "<IndexedFaceSet convex='false' %s coordIndex='",defid);

	j = 0;
	for (i=0; i<psur->ngeoms; i++)
	{
		patch = (LWPOLY *) psur->geoms[i];
		np = patch->rings[0]->npoints - 1;
		for (k=0; k < np ; k++)
		{
			if (k)
			{
				stringbuffer_aprintf(sb, " ");
			}
			stringbuffer_aprintf(sb,"%d", (j + k));
		}
		if (i < (psur->ngeoms - 1) )
		{
			stringbuffer_aprintf(sb, " -1 "); /* separator for each subgeom */
		}
		j += k;
	}

	if ( X3D_USE_GEOCOORDS(opts) )
		stringbuffer_aprintf(sb, "'><GeoCoordinate geoSystem='\"GD\" \"WE\" \"%s\"' point='",
			( (opts & LW_X3D_FLIP_XY) ? "latitude_first" : "longitude_first") );
	else stringbuffer_aprintf(sb, "'><Coordinate point='");

	for (i=0; i<psur->ngeoms; i++)
	{
		asx3d3_poly_sb(psur->geoms[i], precision, opts, 1, defid, sb);
		if (i < (psur->ngeoms - 1) )
		{
			stringbuffer_aprintf(sb, " "); /* only add a trailing space if its not the last polygon in the set */
		}
	}

	/* Close outmost tag */
	return stringbuffer_aprintf(sb, "' /></IndexedFaceSet>");
}

/*
 * Computes X3D representation of TIN (as IndexedTriangleSet and adds to string buffer)
 */
static int
asx3d3_tin_sb(const LWTIN *tin, int precision, int opts, const char *defid, stringbuffer_t *sb)
{
	uint32_t i;
	uint32_t k;
	/* int dimension=2; */

	stringbuffer_aprintf(sb,"<IndexedTriangleSet %s index='",defid);
	k = 0;
	/** Fill in triangle index **/
	for (i=0; i<tin->ngeoms; i++)
	{
		stringbuffer_aprintf(sb, "%d %d %d", k, (k+1), (k+2));
		if (i < (tin->ngeoms - 1) )
		{
			stringbuffer_aprintf(sb, " ");
		}
		k += 3;
	}

	if ( X3D_USE_GEOCOORDS(opts) ) stringbuffer_aprintf(sb, "'><GeoCoordinate geoSystem='\"GD\" \"WE\" \"%s\"' point='", ( (opts & LW_X3D_FLIP_XY) ? "latitude_first" : "longitude_first") );
	else stringbuffer_aprintf(sb, "'><Coordinate point='");

	for (i=0; i<tin->ngeoms; i++)
	{
		asx3d3_triangle_sb(tin->geoms[i], precision, opts, defid, sb);
		if (i < (tin->ngeoms - 1) )
		{
			stringbuffer_aprintf(sb, " ");
		}
	}

	/* Close outmost tag */

	return stringbuffer_aprintf(sb, "'/></IndexedTriangleSet>");
}

static int
asx3d3_collection_sb(const LWCOLLECTION *col, int precision, int opts, const char *defid, stringbuffer_t *sb)
{
	uint32_t i;
	LWGEOM *subgeom;

	/* Open outmost tag */
	/** @TODO: if collection should be grouped, we'll wrap in a group tag.  Still needs cleanup
	 * like the shapes should really be in a transform **/
#ifdef PGIS_X3D_OUTERMOST_TAGS
	stringbuffer_aprintf(sb, "<%sGroup>", defid);
#endif

	for (i=0; i<col->ngeoms; i++)
	{
		subgeom = col->geoms[i];
		stringbuffer_aprintf(sb, "<Shape%s>", defid);
		if ( subgeom->type == POINTTYPE )
		{
			asx3d3_point_sb((LWPOINT *)subgeom, precision, opts, defid, sb);
		}
		else if ( subgeom->type == LINETYPE )
		{
			asx3d3_line_sb((LWLINE *)subgeom, precision, opts, defid, sb);
		}
		else if ( subgeom->type == POLYGONTYPE )
		{
			asx3d3_poly_sb((LWPOLY *)subgeom, precision, opts, 0, defid, sb);
		}
		else if ( subgeom->type == TINTYPE )
		{
			asx3d3_tin_sb((LWTIN *)subgeom, precision, opts, defid, sb);
		}
		else if ( subgeom->type == POLYHEDRALSURFACETYPE )
		{
			asx3d3_psurface_sb((LWPSURFACE *)subgeom, precision, opts, defid, sb);
		}
		else if ( lwgeom_is_collection(subgeom) )
		{
			if ( subgeom->type == COLLECTIONTYPE )
				asx3d3_collection_sb((LWCOLLECTION *)subgeom, precision, opts, defid, sb);
			else
				asx3d3_multi_sb((LWCOLLECTION *)subgeom, precision, opts, defid, sb);
		}
		else
			lwerror("asx3d3_collection_buf: unknown geometry type");

		stringbuffer_aprintf(sb, "</Shape>");
	}

	/* Close outmost tag */
#ifdef PGIS_X3D_OUTERMOST_TAGS
	stringbuffer_aprintf(sb,  "</%sGroup>", defid);
#endif

	return LW_SUCCESS;
}

/** In X3D3, coordinates are separated by a space separator
 */
static int
ptarray_to_x3d3_sb(POINTARRAY *pa, int precision, int opts, int is_closed, stringbuffer_t *sb )
{
	uint32_t i;
	char x[OUT_DOUBLE_BUFFER_SIZE];
	char y[OUT_DOUBLE_BUFFER_SIZE];
	char z[OUT_DOUBLE_BUFFER_SIZE];

	if ( ! FLAGS_GET_Z(pa->flags) )
	{
		for (i=0; i<pa->npoints; i++)
		{
			/** Only output the point if it is not the last point of a closed object or it is a non-closed type **/
			if ( !is_closed || i < (pa->npoints - 1) )
			{
				POINT2D pt;
				getPoint2d_p(pa, i, &pt);

				lwprint_double(pt.x, precision, x);
				lwprint_double(pt.y, precision, y);

				if ( i ) stringbuffer_append_len(sb," ",1);

				if ( ( opts & LW_X3D_FLIP_XY) )
					stringbuffer_aprintf(sb, "%s %s", y, x);
				else
					stringbuffer_aprintf(sb, "%s %s", x, y);
			}
		}
	}
	else
	{
		for (i=0; i<pa->npoints; i++)
		{
			/** Only output the point if it is not the last point of a closed object or it is a non-closed type **/
			if ( !is_closed || i < (pa->npoints - 1) )
			{
				POINT4D pt;
				getPoint4d_p(pa, i, &pt);

				lwprint_double(pt.x, precision, x);
				lwprint_double(pt.y, precision, y);
				lwprint_double(pt.z, precision, z);

				if ( i ) stringbuffer_append_len(sb," ",1);

				if ( ( opts & LW_X3D_FLIP_XY) )
					stringbuffer_aprintf(sb, "%s %s %s", y, x, z);
				else
					stringbuffer_aprintf(sb, "%s %s %s", x, y, z);
			}
		}
	}

	return LW_SUCCESS;
}