2005-11-30 01:51:06 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder Polygon Class Implementation, $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
|
|
|
//
|
|
|
|
|
|
|
|
#include "zm.h"
|
|
|
|
#include "zm_poly.h"
|
|
|
|
|
2015-05-18 08:18:54 +08:00
|
|
|
#ifndef SOLARIS
|
2005-11-30 01:51:06 +08:00
|
|
|
#include <math.h>
|
2015-05-18 08:18:54 +08:00
|
|
|
#else
|
|
|
|
#include <cmath>
|
|
|
|
#endif
|
2005-11-30 01:51:06 +08:00
|
|
|
|
|
|
|
void Polygon::calcArea()
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
double float_area = 0.0L;
|
|
|
|
for ( int i = 0, j = n_coords-1; i < n_coords; j = i++ )
|
|
|
|
{
|
|
|
|
double trap_area = ((coords[i].X()-coords[j].X())*((coords[i].Y()+coords[j].Y())))/2.0L;
|
|
|
|
float_area += trap_area;
|
|
|
|
//printf( "%.2f (%.2f)\n", float_area, trap_area );
|
|
|
|
}
|
|
|
|
area = (int)round(fabs(float_area));
|
2005-11-30 01:51:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Polygon::calcCentre()
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( !area && n_coords )
|
|
|
|
calcArea();
|
|
|
|
double float_x = 0.0L, float_y = 0.0L;
|
|
|
|
for ( int i = 0, j = n_coords-1; i < n_coords; j = i++ )
|
|
|
|
{
|
|
|
|
float_x += ((coords[i].Y()-coords[j].Y())*((coords[i].X()*2)+(coords[i].X()*coords[j].X())+(coords[j].X()*2)));
|
|
|
|
float_y += ((coords[j].X()-coords[i].X())*((coords[i].Y()*2)+(coords[i].Y()*coords[j].Y())+(coords[j].Y()*2)));
|
|
|
|
}
|
|
|
|
float_x /= (6*area);
|
|
|
|
float_y /= (6*area);
|
|
|
|
//printf( "%.2f,%.2f\n", float_x, float_y );
|
|
|
|
centre = Coord( (int)round(float_x), (int)round(float_y) );
|
2005-11-30 01:51:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Polygon::Polygon( int p_n_coords, const Coord *p_coords ) : n_coords( p_n_coords )
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
coords = new Coord[n_coords];
|
2005-11-30 01:51:06 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
int min_x = -1;
|
|
|
|
int max_x = -1;
|
|
|
|
int min_y = -1;
|
|
|
|
int max_y = -1;
|
|
|
|
for( int i = 0; i < n_coords; i++ )
|
|
|
|
{
|
|
|
|
coords[i] = p_coords[i];
|
|
|
|
if ( min_x == -1 || coords[i].X() < min_x )
|
|
|
|
min_x = coords[i].X();
|
|
|
|
if ( max_x == -1 || coords[i].X() > max_x )
|
|
|
|
max_x = coords[i].X();
|
|
|
|
if ( min_y == -1 || coords[i].Y() < min_y )
|
|
|
|
min_y = coords[i].Y();
|
|
|
|
if ( max_y == -1 || coords[i].Y() > max_y )
|
|
|
|
max_y = coords[i].Y();
|
|
|
|
}
|
|
|
|
extent = Box( min_x, min_y, max_x, max_y );
|
|
|
|
calcArea();
|
|
|
|
calcCentre();
|
2005-11-30 01:51:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Polygon::Polygon( const Polygon &p_polygon ) : n_coords( p_polygon.n_coords ), extent( p_polygon.extent ), area( p_polygon.area ), centre( p_polygon.centre )
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
coords = new Coord[n_coords];
|
|
|
|
for( int i = 0; i < n_coords; i++ )
|
|
|
|
{
|
|
|
|
coords[i] = p_polygon.coords[i];
|
|
|
|
}
|
2005-11-30 01:51:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Polygon &Polygon::operator=( const Polygon &p_polygon )
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( n_coords < p_polygon.n_coords )
|
|
|
|
{
|
|
|
|
delete[] coords;
|
|
|
|
coords = new Coord[p_polygon.n_coords];
|
|
|
|
}
|
|
|
|
n_coords = p_polygon.n_coords;
|
|
|
|
for( int i = 0; i < n_coords; i++ )
|
|
|
|
{
|
|
|
|
coords[i] = p_polygon.coords[i];
|
|
|
|
}
|
|
|
|
extent = p_polygon.extent;
|
|
|
|
area = p_polygon.area;
|
|
|
|
centre = p_polygon.centre;
|
|
|
|
return( *this );
|
2005-11-30 01:51:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Polygon::isInside( const Coord &coord ) const
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
bool inside = false;
|
|
|
|
for ( int i = 0, j = n_coords-1; i < n_coords; j = i++ )
|
|
|
|
{
|
|
|
|
if ( (((coords[i].Y() <= coord.Y()) && (coord.Y() < coords[j].Y()) )
|
|
|
|
|| ((coords[j].Y() <= coord.Y()) && (coord.Y() < coords[i].Y())))
|
|
|
|
&& (coord.X() < (coords[j].X() - coords[i].X()) * (coord.Y() - coords[i].Y()) / (coords[j].Y() - coords[i].Y()) + coords[i].X()))
|
|
|
|
{
|
|
|
|
inside = !inside;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return( inside );
|
2005-11-30 01:51:06 +08:00
|
|
|
}
|