zoneminder/src/zm_poly.h

121 lines
3.2 KiB
C
Raw Normal View History

//
// ZoneMinder Polygon Class Interfaces, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#ifndef ZM_POLY_H
#define ZM_POLY_H
#include "zm.h"
#include "zm_coord.h"
#include "zm_box.h"
#include <math.h>
//
// Class used for storing a box, which is defined as a region
// defined by two coordinates
//
2017-10-20 05:12:36 +08:00
class Polygon {
protected:
2017-10-20 05:12:36 +08:00
struct Edge {
int min_y;
int max_y;
double min_x;
double _1_m;
2017-10-20 05:12:36 +08:00
static int CompareYX( const void *p1, const void *p2 ) {
2017-11-19 05:00:10 +08:00
const Edge *e1 = reinterpret_cast<const Edge *>(p1), *e2 = reinterpret_cast<const Edge *>(p2);
if ( e1->min_y == e2->min_y )
2020-03-05 03:14:38 +08:00
return int(e1->min_x - e2->min_x);
else
2020-03-05 03:14:38 +08:00
return int(e1->min_y - e2->min_y);
}
2017-10-20 05:12:36 +08:00
static int CompareX( const void *p1, const void *p2 ) {
2017-11-19 05:00:10 +08:00
const Edge *e1 = reinterpret_cast<const Edge *>(p1), *e2 = reinterpret_cast<const Edge *>(p2);
2020-03-05 03:14:38 +08:00
return int(e1->min_x - e2->min_x);
}
};
2017-10-20 05:12:36 +08:00
struct Slice {
int min_x;
int max_x;
int n_edges;
int *edges;
2017-10-20 05:12:36 +08:00
Slice() {
min_x = 0;
max_x = 0;
n_edges = 0;
edges = nullptr;
}
2017-10-20 05:12:36 +08:00
~Slice() {
delete edges;
}
};
protected:
int n_coords;
Coord *coords;
Box extent;
int area;
Coord centre;
Edge *edges;
Slice *slices;
protected:
void initialiseEdges();
void calcArea();
void calcCentre();
public:
inline Polygon() : n_coords(0), coords(nullptr), area(0), edges(nullptr), slices(nullptr) {
}
2020-03-05 03:14:38 +08:00
Polygon(int p_n_coords, const Coord *p_coords);
Polygon(const Polygon &p_polygon);
2017-10-20 05:12:36 +08:00
~Polygon() {
delete[] coords;
}
Polygon &operator=( const Polygon &p_polygon );
2020-03-05 03:14:38 +08:00
inline int getNumCoords() const { return n_coords; }
2017-10-20 05:12:36 +08:00
inline const Coord &getCoord( int index ) const {
2020-03-05 03:14:38 +08:00
return coords[index];
}
2020-03-05 03:14:38 +08:00
inline const Box &Extent() const { return extent; }
inline int LoX() const { return extent.LoX(); }
2020-12-16 05:32:41 +08:00
inline int LoX(int p_lo_x) { return extent.LoX(p_lo_x); }
2020-03-05 03:14:38 +08:00
inline int HiX() const { return extent.HiX(); }
2020-12-16 05:32:41 +08:00
inline int HiX(int p_hi_x) { return extent.HiX(p_hi_x); }
2020-03-05 03:14:38 +08:00
inline int LoY() const { return extent.LoY(); }
2020-12-16 05:32:41 +08:00
inline int LoY(int p_lo_y) { return extent.LoY(p_lo_y); }
2020-03-05 03:14:38 +08:00
inline int HiY() const { return extent.HiY(); }
2020-12-16 05:32:41 +08:00
inline int HiY(int p_hi_y) { return extent.HiY(p_hi_y); }
2020-03-05 03:14:38 +08:00
inline int Width() const { return extent.Width(); }
inline int Height() const { return extent.Height(); }
2020-03-05 03:14:38 +08:00
inline int Area() const { return area; }
2017-10-20 05:12:36 +08:00
inline const Coord &Centre() const {
2020-03-05 03:14:38 +08:00
return centre;
}
bool isInside( const Coord &coord ) const;
};
#endif // ZM_POLY_H