PostGIS  2.3.6-r@@SVN_REVISION@@
lwtriangle.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright (C) 2010 - Oslandia
22  *
23  **********************************************************************/
24 
25 
26 /* basic LWTRIANGLE manipulation */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "liblwgeom_internal.h"
32 #include "lwgeom_log.h"
33 
34 
35 
36 /* construct a new LWTRIANGLE.
37  * use SRID=SRID_UNKNOWN for unknown SRID (will have 8bit type's S = 0)
38  */
40 lwtriangle_construct(int srid, GBOX *bbox, POINTARRAY *points)
41 {
42  LWTRIANGLE *result;
43 
44  result = (LWTRIANGLE*) lwalloc(sizeof(LWTRIANGLE));
45  result->type = TRIANGLETYPE;
46 
47  result->flags = points->flags;
48  FLAGS_SET_BBOX(result->flags, bbox?1:0);
49 
50  result->srid = srid;
51  result->points = points;
52  result->bbox = bbox;
53 
54  return result;
55 }
56 
58 lwtriangle_construct_empty(int srid, char hasz, char hasm)
59 {
60  LWTRIANGLE *result = lwalloc(sizeof(LWTRIANGLE));
61  result->type = TRIANGLETYPE;
62  result->flags = gflags(hasz,hasm,0);
63  result->srid = srid;
64  result->points = ptarray_construct_empty(hasz, hasm, 1);
65  result->bbox = NULL;
66  return result;
67 }
68 
69 void lwtriangle_free(LWTRIANGLE *triangle)
70 {
71  if ( ! triangle ) return;
72 
73  if (triangle->bbox)
74  lwfree(triangle->bbox);
75 
76  if (triangle->points)
77  ptarray_free(triangle->points);
78 
79  lwfree(triangle);
80 }
81 
82 void printLWTRIANGLE(LWTRIANGLE *triangle)
83 {
84  if (triangle->type != TRIANGLETYPE)
85  lwerror("printLWTRIANGLE called with something else than a Triangle");
86 
87  lwnotice("LWTRIANGLE {");
88  lwnotice(" ndims = %i", (int)FLAGS_NDIMS(triangle->flags));
89  lwnotice(" SRID = %i", (int)triangle->srid);
90  printPA(triangle->points);
91  lwnotice("}");
92 }
93 
94 /* @brief Clone LWTRIANGLE object. Serialized point lists are not copied.
95  *
96  * @see ptarray_clone
97  */
98 LWTRIANGLE *
100 {
101  LWDEBUGF(2, "lwtriangle_clone called with %p", g);
102  return (LWTRIANGLE *)lwline_clone((const LWLINE *)g);
103 }
104 
105 void
107 {
108  if ( ptarray_isccw(triangle->points) )
109  ptarray_reverse(triangle->points);
110 }
111 
112 void
114 {
115  if( lwtriangle_is_empty(triangle) ) return;
116  ptarray_reverse(triangle->points);
117 }
118 
119 void
121 {
123 }
124 
125 /* check coordinate equality */
126 char
127 lwtriangle_same(const LWTRIANGLE *t1, const LWTRIANGLE *t2)
128 {
129  char r = ptarray_same(t1->points, t2->points);
130  LWDEBUGF(5, "returning %d", r);
131  return r;
132 }
133 
134 /*
135  * Construct a triangle from a LWLINE being
136  * the shell
137  * Pointarray from intput geom are cloned.
138  * Input line must have 4 points, and be closed.
139  */
140 LWTRIANGLE *
142 {
143  LWTRIANGLE *ret;
144  POINTARRAY *pa;
145 
146  if ( shell->points->npoints != 4 )
147  lwerror("lwtriangle_from_lwline: shell must have exactly 4 points");
148 
149  if ( (!FLAGS_GET_Z(shell->flags) && !ptarray_is_closed_2d(shell->points)) ||
150  (FLAGS_GET_Z(shell->flags) && !ptarray_is_closed_3d(shell->points)) )
151  lwerror("lwtriangle_from_lwline: shell must be closed");
152 
153  pa = ptarray_clone_deep(shell->points);
154  ret = lwtriangle_construct(shell->srid, NULL, pa);
155 
157  lwerror("lwtriangle_from_lwline: some points are repeated in triangle");
158 
159  return ret;
160 }
161 
162 char
164 {
165  char ret;
166  POINTARRAY *pa;
167 
168  pa = ptarray_remove_repeated_points(triangle->points, 0.0);
169  ret = ptarray_same(pa, triangle->points);
170  ptarray_free(pa);
171 
172  return ret;
173 }
174 
175 int lwtriangle_is_empty(const LWTRIANGLE *triangle)
176 {
177  if ( !triangle->points || triangle->points->npoints < 1 )
178  return LW_TRUE;
179  return LW_FALSE;
180 }
181 
185 double
186 lwtriangle_area(const LWTRIANGLE *triangle)
187 {
188  double area=0.0;
189  int i;
190  POINT2D p1;
191  POINT2D p2;
192 
193  if (! triangle->points->npoints) return area; /* empty triangle */
194 
195  for (i=0; i < triangle->points->npoints-1; i++)
196  {
197  getPoint2d_p(triangle->points, i, &p1);
198  getPoint2d_p(triangle->points, i+1, &p2);
199  area += ( p1.x * p2.y ) - ( p1.y * p2.x );
200  }
201 
202  area /= 2.0;
203 
204  return fabs(area);
205 }
206 
207 
208 double
210 {
211  if( triangle->points )
212  return ptarray_length(triangle->points);
213  else
214  return 0.0;
215 }
216 
217 double
219 {
220  if( triangle->points )
221  return ptarray_length_2d(triangle->points);
222  else
223  return 0.0;
224 }
int lwtriangle_is_empty(const LWTRIANGLE *triangle)
Definition: lwtriangle.c:175
POINTARRAY * ptarray_remove_repeated_points(const POINTARRAY *in, double tolerance)
Definition: ptarray.c:1499
POINTARRAY * points
Definition: liblwgeom.h:432
double lwtriangle_area(const LWTRIANGLE *triangle)
Find the area of the outer ring.
Definition: lwtriangle.c:186
char * r
Definition: cu_in_wkt.c:24
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:89
LWTRIANGLE * lwtriangle_construct_empty(int srid, char hasz, char hasm)
Definition: lwtriangle.c:58
void lwfree(void *mem)
Definition: lwutil.c:242
LWLINE * lwline_clone(const LWLINE *lwgeom)
Definition: lwline.c:102
int npoints
Definition: liblwgeom.h:370
int ptarray_is_closed_3d(const POINTARRAY *pa)
Definition: ptarray.c:707
void lwtriangle_release(LWTRIANGLE *lwtriangle)
Definition: lwtriangle.c:120
Datum area(PG_FUNCTION_ARGS)
LWTRIANGLE * lwtriangle_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwtriangle.c:40
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:330
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1645
POINT2D * p1
Definition: lwtree.h:33
#define TRIANGLETYPE
Definition: liblwgeom.h:97
LWTRIANGLE * lwtriangle_clone(const LWTRIANGLE *g)
Definition: lwtriangle.c:99
GBOX * bbox
Definition: liblwgeom.h:430
int32_t srid
Definition: liblwgeom.h:420
int ptarray_is_closed_2d(const POINTARRAY *pa)
Definition: ptarray.c:694
uint8_t type
Definition: liblwgeom.h:428
POINT2D * p2
Definition: lwtree.h:34
char lwtriangle_is_repeated_points(LWTRIANGLE *triangle)
Definition: lwtriangle.c:163
double x
Definition: liblwgeom.h:327
int ptarray_isccw(const POINTARRAY *pa)
Definition: ptarray.c:1026
void lwtriangle_free(LWTRIANGLE *triangle)
Definition: lwtriangle.c:69
#define LW_FALSE
Definition: liblwgeom.h:76
uint8_t flags
Definition: liblwgeom.h:368
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
void lwtriangle_reverse(LWTRIANGLE *triangle)
Definition: lwtriangle.c:113
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:634
LWGEOM * lwtriangle_as_lwgeom(const LWTRIANGLE *obj)
Definition: lwgeom.c:257
double y
Definition: liblwgeom.h:327
int getPoint2d_p(const POINTARRAY *pa, int n, POINT2D *point)
Definition: lwgeom_api.c:461
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
LWTRIANGLE * lwtriangle_from_lwline(const LWLINE *shell)
Definition: lwtriangle.c:141
uint8_t gflags(int hasz, int hasm, int geodetic)
Construct a new flags char.
Definition: g_util.c:145
void printLWTRIANGLE(LWTRIANGLE *triangle)
Definition: lwtriangle.c:82
double ptarray_length(const POINTARRAY *pts)
Find the 3d/2d length of the given POINTARRAY (depending on its dimensionality)
Definition: ptarray.c:1673
#define FLAGS_SET_BBOX(flags, value)
Definition: liblwgeom.h:147
void printPA(POINTARRAY *pa)
Definition: lwgeom_api.c:611
char ptarray_same(const POINTARRAY *pa1, const POINTARRAY *pa2)
Definition: ptarray.c:484
char lwtriangle_same(const LWTRIANGLE *t1, const LWTRIANGLE *t2)
Definition: lwtriangle.c:127
void ptarray_reverse(POINTARRAY *pa)
Definition: ptarray.c:343
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:385
double lwtriangle_perimeter_2d(const LWTRIANGLE *triangle)
Definition: lwtriangle.c:218
void * lwalloc(size_t size)
Definition: lwutil.c:227
void lwtriangle_force_clockwise(LWTRIANGLE *triangle)
Definition: lwtriangle.c:106
uint8_t flags
Definition: liblwgeom.h:418
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:151
double lwtriangle_perimeter(const LWTRIANGLE *triangle)
Definition: lwtriangle.c:209
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:102
int32_t srid
Definition: liblwgeom.h:431
uint8_t flags
Definition: liblwgeom.h:429
POINTARRAY * points
Definition: liblwgeom.h:421