1 #ifndef teca_geometry_h
2 #define teca_geometry_h
4 #include "teca_config.h"
5 #include "teca_binary_stream.h"
17 template<
typename n_t>
19 bool left(n_t e0x, n_t e0y, n_t e1x, n_t e1y, n_t px, n_t py)
25 ((e1x - e0x)*(py - e0y) - (px - e0x)*(e1y - e0y)) >= n_t();
41 template<
typename n_t>
44 n_t *vx, n_t *vy,
unsigned long nppts)
48 unsigned long npptsm1 = nppts - 1;
49 for (
unsigned long i = 0; i < npptsm1; ++i)
57 (
left(vx[i], vy[i], vx[i+1], vy[i+1], px, py))) ++wn;
63 if ((vy[i+1] <= py) &&
64 (!
left(vx[i], vy[i], vx[i+1], vy[i+1], px, py))) --wn;
73 template <
typename coord_t>
76 polygon() : vx(
nullptr), vy(
nullptr), n_verts(0) {}
79 template <
typename in_coord_t>
80 void copy(
const in_coord_t *in_vx,
81 const in_coord_t *in_vy,
unsigned long in_n_verts);
84 void normalize_coordinates();
87 bool inside(coord_t px, coord_t py);
92 void get_bounds(coord_t *bounds);
100 std::shared_ptr<coord_t> vx;
101 std::shared_ptr<coord_t> vy;
102 unsigned long n_verts;
107 template <
typename coord_t>
108 template <
typename in_coord_t>
110 const in_coord_t *in_vy,
unsigned long in_n_verts)
112 unsigned long nbytes = in_n_verts*
sizeof(coord_t);
114 vx = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
115 memcpy(vx.get(), in_vx, nbytes);
117 vy = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
118 memcpy(vy.get(), in_vy, nbytes);
120 n_verts = in_n_verts;
124 template <
typename coord_t>
127 coord_t *pvx = vx.get();
128 for (
unsigned long i = 0; i < n_verts; ++i)
129 pvx[i] = pvx[i] < coord_t(0) ? pvx[i] + coord_t(360) : pvx[i];
133 template <
typename coord_t>
140 template <
typename coord_t>
143 const coord_t *pvx = vx.get();
144 const coord_t *pvy = vy.get();
146 bounds[0] = std::numeric_limits<coord_t>::max();
147 for (
unsigned long i = 0; i < n_verts; ++i)
148 bounds[0] = pvx[i] < bounds[0] ? pvx[i] : bounds[0];
150 bounds[1] = std::numeric_limits<coord_t>::lowest();
151 for (
unsigned long i = 0; i < n_verts; ++i)
152 bounds[1] = pvx[i] > bounds[1] ? pvx[i] : bounds[1];
154 bounds[2] = std::numeric_limits<coord_t>::max();
155 for (
unsigned long i = 0; i < n_verts; ++i)
156 bounds[2] = pvy[i] < bounds[2] ? pvy[i] : bounds[2];
158 bounds[3] = std::numeric_limits<coord_t>::lowest();
159 for (
unsigned long i = 0; i < n_verts; ++i)
160 bounds[3] = pvy[i] > bounds[3] ? pvy[i] : bounds[3];
164 template <
typename coord_t>
168 bs.pack(vx.get(), n_verts);
169 bs.pack(vy.get(), n_verts);
173 template <
typename coord_t>
178 unsigned long nbytes = n_verts*
sizeof(coord_t);
179 vx = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
180 vy = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
182 bs.unpack(vx.get(), n_verts);
183 bs.unpack(vy.get(), n_verts);