1 #ifndef teca_geometry_h
2 #define teca_geometry_h
4 #include "teca_config.h"
5 #include "teca_binary_stream.h"
7 #if defined(TECA_HAS_CUDA)
9 #include <cuda_runtime.h>
10 #include <hamr_cuda_malloc_async_allocator.h>
26 template<
typename n_t>
29 bool left(n_t e0x, n_t e0y, n_t e1x, n_t e1y, n_t px, n_t py)
35 ((e1x - e0x)*(py - e0y) - (px - e0x)*(e1y - e0y)) >= n_t();
51 template<
typename n_t>
55 const n_t *vx,
const n_t *vy,
unsigned long nppts)
59 unsigned long npptsm1 = nppts - 1;
60 for (
unsigned long i = 0; i < npptsm1; ++i)
68 (
left(vx[i], vy[i], vx[i+1], vy[i+1], px, py))) ++wn;
74 if ((vy[i+1] <= py) &&
75 (!
left(vx[i], vy[i], vx[i+1], vy[i+1], px, py))) --wn;
84 template <
typename coord_t>
87 using element_type = coord_t;
89 polygon() : vx(
nullptr), vy(
nullptr), n_verts(0) {}
92 template <
typename in_coord_t>
93 void copy(
const in_coord_t *in_vx,
94 const in_coord_t *in_vy,
unsigned long in_n_verts);
97 void normalize_coordinates();
100 bool inside(coord_t px, coord_t py)
const;
105 void get_bounds(coord_t *bounds)
const;
113 std::shared_ptr<coord_t> vx;
114 std::shared_ptr<coord_t> vy;
115 unsigned long n_verts;
120 template <
typename coord_t>
121 template <
typename in_coord_t>
123 const in_coord_t *in_vy,
unsigned long in_n_verts)
125 unsigned long nbytes = in_n_verts*
sizeof(coord_t);
127 vx = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
128 memcpy(vx.get(), in_vx, nbytes);
130 vy = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
131 memcpy(vy.get(), in_vy, nbytes);
133 n_verts = in_n_verts;
137 template <
typename coord_t>
140 coord_t *pvx = vx.get();
141 for (
unsigned long i = 0; i < n_verts; ++i)
142 pvx[i] = pvx[i] < coord_t(0) ? pvx[i] + coord_t(360) : pvx[i];
146 template <
typename coord_t>
153 template <
typename coord_t>
156 const coord_t *pvx = vx.get();
157 const coord_t *pvy = vy.get();
159 bounds[0] = std::numeric_limits<coord_t>::max();
160 for (
unsigned long i = 0; i < n_verts; ++i)
161 bounds[0] = pvx[i] < bounds[0] ? pvx[i] : bounds[0];
163 bounds[1] = std::numeric_limits<coord_t>::lowest();
164 for (
unsigned long i = 0; i < n_verts; ++i)
165 bounds[1] = pvx[i] > bounds[1] ? pvx[i] : bounds[1];
167 bounds[2] = std::numeric_limits<coord_t>::max();
168 for (
unsigned long i = 0; i < n_verts; ++i)
169 bounds[2] = pvy[i] < bounds[2] ? pvy[i] : bounds[2];
171 bounds[3] = std::numeric_limits<coord_t>::lowest();
172 for (
unsigned long i = 0; i < n_verts; ++i)
173 bounds[3] = pvy[i] > bounds[3] ? pvy[i] : bounds[3];
177 template <
typename coord_t>
181 bs.pack(vx.get(), n_verts);
182 bs.pack(vy.get(), n_verts);
186 template <
typename coord_t>
191 unsigned long nbytes = n_verts*
sizeof(coord_t);
192 vx = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
193 vy = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
195 bs.unpack(vx.get(), n_verts);
196 bs.unpack(vy.get(), n_verts);
Serialize objects into a binary stream.
Definition: teca_binary_stream.h:17
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
Codes dealing with computational geometry.
Definition: teca_geometry.h:23
TECA_EXPORT bool left(n_t e0x, n_t e0y, n_t e1x, n_t e1y, n_t px, n_t py)
tests if a point is Left|On|Right of an infinite line.
Definition: teca_geometry.h:29
TECA_EXPORT bool point_in_poly(n_t px, n_t py, const n_t *vx, const n_t *vy, unsigned long nppts)
Definition: teca_geometry.h:54
TECA_EXPORT bool copy(teca_variant_array *varr, PyObject *obj)
Copy values from the object into variant array.
Definition: teca_py_array.h:290
Definition: teca_geometry.h:86
bool inside(coord_t px, coord_t py) const
returns true if a point is inside the polygon
Definition: teca_geometry.h:147
void copy(const in_coord_t *in_vx, const in_coord_t *in_vy, unsigned long in_n_verts)
deep copy the vertices of the polygon.
Definition: teca_geometry.h:122
int from_stream(teca_binary_stream &bs)
deseriealize from the given stream
Definition: teca_geometry.h:187
void normalize_coordinates()
transforms coordinates to be in [0 360 -90 90]
Definition: teca_geometry.h:138
void get_bounds(coord_t *bounds) const
Definition: teca_geometry.h:154
void to_stream(teca_binary_stream &bs) const
serialize to the given stream
Definition: teca_geometry.h:178