PostGIS  2.5.7dev-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_in_place(triangle->points);
110 }
111 
112 int
114 {
115  return !ptarray_isccw(triangle->points);
116 }
117 
118 void
120 {
122 }
123 
124 /* check coordinate equality */
125 char
126 lwtriangle_same(const LWTRIANGLE *t1, const LWTRIANGLE *t2)
127 {
128  char r = ptarray_same(t1->points, t2->points);
129  LWDEBUGF(5, "returning %d", r);
130  return r;
131 }
132 
133 static char
135 {
136  char ret;
137  POINTARRAY *pa;
138 
139  pa = ptarray_remove_repeated_points(triangle->points, 0.0);
140  ret = ptarray_same(pa, triangle->points);
141  ptarray_free(pa);
142 
143  return ret;
144 }
145 
146 /*
147  * Construct a triangle from a LWLINE being
148  * the shell
149  * Pointarray from input geom is cloned.
150  * Input line must have 4 points, and be closed.
151  */
152 LWTRIANGLE *
154 {
155  LWTRIANGLE *ret;
156  POINTARRAY *pa;
157 
158  if ( shell->points->npoints != 4 )
159  lwerror("lwtriangle_from_lwline: shell must have exactly 4 points");
160 
161  if ( (!FLAGS_GET_Z(shell->flags) && !ptarray_is_closed_2d(shell->points)) ||
162  (FLAGS_GET_Z(shell->flags) && !ptarray_is_closed_3d(shell->points)) )
163  lwerror("lwtriangle_from_lwline: shell must be closed");
164 
165  pa = ptarray_clone_deep(shell->points);
166  ret = lwtriangle_construct(shell->srid, NULL, pa);
167 
169  lwerror("lwtriangle_from_lwline: some points are repeated in triangle");
170 
171  return ret;
172 }
173 
174 int lwtriangle_is_empty(const LWTRIANGLE *triangle)
175 {
176  if ( !triangle->points || triangle->points->npoints < 1 )
177  return LW_TRUE;
178  return LW_FALSE;
179 }
180 
184 double
185 lwtriangle_area(const LWTRIANGLE *triangle)
186 {
187  double area=0.0;
188  uint32_t i;
189  POINT2D p1;
190  POINT2D p2;
191 
192  if (! triangle->points->npoints) return area; /* empty triangle */
193 
194  for (i=0; i < triangle->points->npoints-1; i++)
195  {
196  getPoint2d_p(triangle->points, i, &p1);
197  getPoint2d_p(triangle->points, i+1, &p2);
198  area += ( p1.x * p2.y ) - ( p1.y * p2.x );
199  }
200 
201  area /= 2.0;
202 
203  return fabs(area);
204 }
205 
206 
207 double
209 {
210  if( triangle->points )
211  return ptarray_length(triangle->points);
212  else
213  return 0.0;
214 }
215 
216 double
218 {
219  if( triangle->points )
220  return ptarray_length_2d(triangle->points);
221  else
222  return 0.0;
223 }
char * r
Definition: cu_in_wkt.c:24
uint8_t gflags(int hasz, int hasm, int geodetic)
Construct a new flags char.
Definition: g_util.c:145
#define LW_FALSE
Definition: liblwgeom.h:77
int ptarray_is_closed_3d(const POINTARRAY *pa)
Definition: ptarray.c:708
#define FLAGS_SET_BBOX(flags, value)
Definition: liblwgeom.h:148
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:628
void printPA(POINTARRAY *pa)
Definition: lwgeom_api.c:506
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:348
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1689
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:140
void lwfree(void *mem)
Definition: lwutil.c:244
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:152
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:328
#define TRIANGLETYPE
Definition: liblwgeom.h:98
void * lwalloc(size_t size)
Definition: lwutil.c:229
int ptarray_is_closed_2d(const POINTARRAY *pa)
Definition: ptarray.c:695
LWGEOM * lwtriangle_as_lwgeom(const LWTRIANGLE *obj)
Definition: lwgeom.c:325
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:459
void ptarray_reverse_in_place(POINTARRAY *pa)
Definition: ptarray.c:341
LWLINE * lwline_clone(const LWLINE *lwgeom)
Definition: lwline.c:102
double ptarray_length(const POINTARRAY *pts)
Find the 3d/2d length of the given POINTARRAY (depending on its dimensionality)
Definition: ptarray.c:1717
char ptarray_same(const POINTARRAY *pa1, const POINTARRAY *pa2)
Definition: ptarray.c:478
POINTARRAY * ptarray_remove_repeated_points(const POINTARRAY *in, double tolerance)
Definition: ptarray.c:1446
int ptarray_isccw(const POINTARRAY *pa)
Definition: ptarray.c:1028
Datum area(PG_FUNCTION_ARGS)
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
double lwtriangle_area(const LWTRIANGLE *triangle)
Find the area of the outer ring.
Definition: lwtriangle.c:185
double lwtriangle_perimeter_2d(const LWTRIANGLE *triangle)
Definition: lwtriangle.c:217
LWTRIANGLE * lwtriangle_clone(const LWTRIANGLE *g)
Definition: lwtriangle.c:99
int lwtriangle_is_clockwise(LWTRIANGLE *triangle)
Definition: lwtriangle.c:113
double lwtriangle_perimeter(const LWTRIANGLE *triangle)
Definition: lwtriangle.c:208
LWTRIANGLE * lwtriangle_from_lwline(const LWLINE *shell)
Definition: lwtriangle.c:153
char lwtriangle_same(const LWTRIANGLE *t1, const LWTRIANGLE *t2)
Definition: lwtriangle.c:126
void lwtriangle_force_clockwise(LWTRIANGLE *triangle)
Definition: lwtriangle.c:106
static char lwtriangle_is_repeated_points(LWTRIANGLE *triangle)
Definition: lwtriangle.c:134
int lwtriangle_is_empty(const LWTRIANGLE *triangle)
Definition: lwtriangle.c:174
void lwtriangle_free(LWTRIANGLE *triangle)
Definition: lwtriangle.c:69
LWTRIANGLE * lwtriangle_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwtriangle.c:40
void printLWTRIANGLE(LWTRIANGLE *triangle)
Definition: lwtriangle.c:82
LWTRIANGLE * lwtriangle_construct_empty(int srid, char hasz, char hasm)
Definition: lwtriangle.c:58
void lwtriangle_release(LWTRIANGLE *lwtriangle)
Definition: lwtriangle.c:119
uint8_t flags
Definition: liblwgeom.h:422
POINTARRAY * points
Definition: liblwgeom.h:425
int32_t srid
Definition: liblwgeom.h:424
int32_t srid
Definition: liblwgeom.h:435
uint8_t type
Definition: liblwgeom.h:432
GBOX * bbox
Definition: liblwgeom.h:434
POINTARRAY * points
Definition: liblwgeom.h:436
uint8_t flags
Definition: liblwgeom.h:433
double y
Definition: liblwgeom.h:331
double x
Definition: liblwgeom.h:331
uint32_t npoints
Definition: liblwgeom.h:374
uint8_t flags
Definition: liblwgeom.h:372
unsigned int uint32_t
Definition: uthash.h:78