convex-hull.h revision 76addc201c409e81eaaa73fe27cc0f79c4db097c
/** @file
* @brief Convex hull data structures
*//*
* Copyright 2006 Nathan Hurst <njh@mail.csse.monash.edu.au>
* Copyright 2006 Michael G. Sloan <mgsloan@gmail.com>
*
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
*/
#ifndef LIB2GEOM_SEEN_CONVEX_HULL_H
#define LIB2GEOM_SEEN_CONVEX_HULL_H
#include <vector>
#include <algorithm>
#include <boost/operators.hpp>
#include <boost/optional.hpp>
/** @brief Iterator for the lower convex hull.
* This iterator allows us to avoid duplicating any points in the hull
* boundary and still express most algorithms in a concise way. */
, Point
, Point const *
, Point const &
>
{
typedef ConvexHullLowerIterator Self;
, _size(0)
, _x(0)
{}
, _x(x)
{}
*this += 1;
return *this;
}
*this -= 1;
return *this;
}
_x += d;
return *this;
}
_x -= d;
return *this;
}
}
} else {
return *_data;
}
}
}
}
};
} // end anonymous namespace
/**
* @brief Convex hull based on the Andrew's monotone chain algorithm.
* @ingroup Shapes
*/
typedef ConvexHullLowerIterator LowerIterator;
/// @name Construct a convex hull.
/// @{
/// Create an empty convex hull.
ConvexHull() {}
/// Construct a singular convex hull.
: _boundary(1, a)
, _lower(1)
{}
/// Construct a convex hull of two points.
/// Construct a convex hull of three points.
/// Construct a convex hull of four points.
/// Create a convex hull of a vector of points.
/// Create a convex hull of a range of points.
: _lower(0)
{
_construct();
}
/// @}
/// @name Inspect basic properties.
/// @{
/// Check for emptiness.
/// Get the number of points in the hull.
/// Check whether the hull contains only one point.
/// Check whether the hull is a line.
/// Check whether the hull has zero area.
/// Calculate the area of the convex hull.
double area() const;
//Point centroid() const;
//double areaAndCentroid(Point &c);
//FatLine maxDiameter() const;
//FatLine minDiameter() const;
/// @}
/// @name Inspect bounds and extreme points.
/// @{
/// Compute the bounding rectangle of the convex hull.
/// Get the leftmost (minimum X) coordinate of the hull.
/// Get the rightmost (maximum X) coordinate of the hull.
/// Get the topmost (minimum Y) coordinate of the hull.
/// Get the bottommost (maximum Y) coordinate of the hull.
/// Get the leftmost (minimum X) point of the hull.
/// If the leftmost edge is vertical, the top point of the edge is returned.
/// Get the rightmost (maximum X) point of the hull.
/// If the rightmost edge is vertical, the bottom point edge is returned.
/// Get the topmost (minimum Y) point of the hull.
/// If the topmost edge is horizontal, the right point of the edge is returned.
/// Get the bottommost (maximum Y) point of the hull.
/// If the bottommost edge is horizontal, the left point of the edge is returned.
Point bottomPoint() const;
///@}
/// @name Iterate over points.
/// @{
/** @brief Get the begin iterator to the points that form the hull.
* Points are are returned beginning the the leftmost one, going along
* the upper (minimum Y) side, and then along the bottom.
* Thus the points are always ordered clockwise. No point is
* repeated. */
/// Get the end iterator to the points that form the hull.
/// Get the first, leftmost point in the hull.
/// Get the penultimate point of the lower hull.
return _boundary[i];
}
/** @brief Get an iterator range to the upper part of the hull.
* This returns a range that includes the leftmost point,
* all points of the upper hull, and the rightmost point. */
return r;
}
/** @brief Get an iterator range to the lower part of the hull.
* This returns a range that includes the leftmost point,
* all points of the lower hull, and the rightmost point. */
LowerIterator(_boundary, 0));
return r;
}
return r;
}
return r;
}
/// @}
/// @name Check for containment and intersection.
/// @{
/** @brief Check whether the given point is inside the hull.
* This takes logarithmic time. */
/** @brief Check whether the given axis-aligned rectangle is inside the hull.
* A rectangle is inside the hull if all of its corners are inside. */
/// Check whether the given convex hull is completely contained in this one.
//bool interiorContains(Point const &p) const;
//bool interiorContains(Rect const &r) const;
//bool interiorContains(ConvexHull const &other) const;
//bool intersects(Rect const &r) const;
//bool intersects(ConvexHull const &other) const;
//ConvexHull &operator|=(ConvexHull const &other);
//ConvexHull &operator&=(ConvexHull const &other);
//ConvexHull &operator*=(Affine const &m);
//ConvexHull &expand(Point const &p);
//void unifyWith(ConvexHull const &other);
//void intersectWith(ConvexHull const &other);
/// @}
void _construct();
/// Take a vector of points and produce a pruned sorted vector.
Point p = *i;
ymin = p;
}
xmin = p;
}
ymax = p;
}
xmax = p;
}
}
if (!ymin) return;
}
}
/// Sequence of points forming the convex hull polygon.
/// Index one past the rightmost point, where the lower part of the boundary starts.
};
/** @brief Output operator for convex hulls.
* Prints out all the coordinates. */
out_file << "ConvexHull(";
}
out_file << ")";
return out_file;
}
} // end namespace Geom
#endif // LIB2GEOM_SEEN_CONVEX_HULL_H
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :