1 #ifndef teca_geometry_h
2 #define teca_geometry_h
4 #include "teca_binary_stream.h"
16 template<
typename n_t>
17 bool left(n_t e0x, n_t e0y, n_t e1x, n_t e1y, n_t px, n_t py)
23 ((e1x - e0x)*(py - e0y) - (px - e0x)*(e1y - e0y)) >= n_t();
39 template<
typename n_t>
41 n_t *vx, n_t *vy,
unsigned long nppts)
45 unsigned long npptsm1 = nppts - 1;
46 for (
unsigned long i = 0; i < npptsm1; ++i)
54 (
left(vx[i], vy[i], vx[i+1], vy[i+1], px, py))) ++wn;
60 if ((vy[i+1] <= py) &&
61 (!
left(vx[i], vy[i], vx[i+1], vy[i+1], px, py))) --wn;
70 template <
typename coord_t>
73 polygon() : vx(
nullptr), vy(
nullptr), n_verts(0) {}
76 template <
typename in_coord_t>
77 void copy(
const in_coord_t *in_vx,
78 const in_coord_t *in_vy,
unsigned long in_n_verts);
84 bool inside(coord_t px, coord_t py);
97 std::shared_ptr<coord_t> vx;
98 std::shared_ptr<coord_t> vy;
99 unsigned long n_verts;
104 template <
typename coord_t>
105 template <
typename in_coord_t>
107 const in_coord_t *in_vy,
unsigned long in_n_verts)
109 unsigned long nbytes = in_n_verts*
sizeof(coord_t);
111 vx = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
112 memcpy(vx.get(), in_vx, nbytes);
114 vy = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
115 memcpy(vy.get(), in_vy, nbytes);
117 n_verts = in_n_verts;
121 template <
typename coord_t>
124 coord_t *pvx = vx.get();
125 for (
unsigned long i = 0; i < n_verts; ++i)
126 pvx[i] = pvx[i] < coord_t(0) ? pvx[i] + coord_t(360) : pvx[i];
130 template <
typename coord_t>
137 template <
typename coord_t>
140 const coord_t *pvx = vx.get();
141 const coord_t *pvy = vy.get();
143 bounds[0] = std::numeric_limits<coord_t>::max();
144 for (
unsigned long i = 0; i < n_verts; ++i)
145 bounds[0] = pvx[i] < bounds[0] ? pvx[i] : bounds[0];
147 bounds[1] = std::numeric_limits<coord_t>::lowest();
148 for (
unsigned long i = 0; i < n_verts; ++i)
149 bounds[1] = pvx[i] > bounds[1] ? pvx[i] : bounds[1];
151 bounds[2] = std::numeric_limits<coord_t>::max();
152 for (
unsigned long i = 0; i < n_verts; ++i)
153 bounds[2] = pvy[i] < bounds[2] ? pvy[i] : bounds[2];
155 bounds[3] = std::numeric_limits<coord_t>::lowest();
156 for (
unsigned long i = 0; i < n_verts; ++i)
157 bounds[3] = pvy[i] > bounds[3] ? pvy[i] : bounds[3];
161 template <
typename coord_t>
165 bs.pack(vx.get(), n_verts);
166 bs.pack(vy.get(), n_verts);
170 template <
typename coord_t>
175 unsigned long nbytes = n_verts*
sizeof(coord_t);
176 vx = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
177 vy = std::shared_ptr<coord_t>((coord_t*)malloc(nbytes), free);
179 bs.unpack(vx.get(), n_verts);
180 bs.unpack(vy.get(), n_verts);