2005-11-30 01:51:06 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder Polygon Class Interfaces, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2005-11-30 01:51:06 +08:00
|
|
|
//
|
|
|
|
// 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
|
2016-12-26 23:23:16 +08:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-11-30 01:51:06 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
#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 {
|
2005-12-21 08:13:23 +08:00
|
|
|
protected:
|
2017-10-20 05:12:36 +08:00
|
|
|
struct Edge {
|
2016-04-04 22:11:48 +08:00
|
|
|
int min_y;
|
|
|
|
int max_y;
|
|
|
|
double min_x;
|
|
|
|
double _1_m;
|
2005-12-21 08:13:23 +08:00
|
|
|
|
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);
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( e1->min_y == e2->min_y )
|
2020-03-05 03:14:38 +08:00
|
|
|
return int(e1->min_x - e2->min_x);
|
2016-04-04 22:11:48 +08:00
|
|
|
else
|
2020-03-05 03:14:38 +08:00
|
|
|
return int(e1->min_y - e2->min_y);
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
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);
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
};
|
2005-12-21 08:13:23 +08:00
|
|
|
|
2017-10-20 05:12:36 +08:00
|
|
|
struct Slice {
|
2016-04-04 22:11:48 +08:00
|
|
|
int min_x;
|
|
|
|
int max_x;
|
|
|
|
int n_edges;
|
|
|
|
int *edges;
|
2005-12-21 08:13:23 +08:00
|
|
|
|
2017-10-20 05:12:36 +08:00
|
|
|
Slice() {
|
|
|
|
min_x = 0;
|
|
|
|
max_x = 0;
|
2016-04-04 22:11:48 +08:00
|
|
|
n_edges = 0;
|
|
|
|
edges = 0;
|
|
|
|
}
|
2017-10-20 05:12:36 +08:00
|
|
|
~Slice() {
|
2016-04-04 22:11:48 +08:00
|
|
|
delete edges;
|
|
|
|
}
|
|
|
|
};
|
2005-12-21 08:13:23 +08:00
|
|
|
|
2005-11-30 01:51:06 +08:00
|
|
|
protected:
|
2016-04-04 22:11:48 +08:00
|
|
|
int n_coords;
|
|
|
|
Coord *coords;
|
|
|
|
Box extent;
|
|
|
|
int area;
|
|
|
|
Coord centre;
|
|
|
|
Edge *edges;
|
|
|
|
Slice *slices;
|
2005-11-30 01:51:06 +08:00
|
|
|
|
|
|
|
protected:
|
2016-04-04 22:11:48 +08:00
|
|
|
void initialiseEdges();
|
|
|
|
void calcArea();
|
|
|
|
void calcCentre();
|
2005-11-30 01:51:06 +08:00
|
|
|
|
|
|
|
public:
|
2020-03-05 03:14:38 +08:00
|
|
|
inline Polygon() : n_coords(0), coords(0), area(0), edges(0), slices(0) {
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
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() {
|
2016-04-04 22:11:48 +08:00
|
|
|
delete[] coords;
|
|
|
|
}
|
2005-11-30 01:51:06 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
Polygon &operator=( const Polygon &p_polygon );
|
2005-11-30 01:51:06 +08:00
|
|
|
|
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];
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2005-11-30 01:51:06 +08:00
|
|
|
|
2020-03-05 03:14:38 +08:00
|
|
|
inline const Box &Extent() const { return extent; }
|
|
|
|
inline int LoX() const { return extent.LoX(); }
|
|
|
|
inline int HiX() const { return extent.HiX(); }
|
|
|
|
inline int LoY() const { return extent.LoY(); }
|
|
|
|
inline int HiY() const { return extent.HiY(); }
|
|
|
|
inline int Width() const { return extent.Width(); }
|
|
|
|
inline int Height() const { return extent.Height(); }
|
2005-11-30 01:51:06 +08:00
|
|
|
|
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;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
bool isInside( const Coord &coord ) const;
|
2005-11-30 01:51:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ZM_POLY_H
|