I'm using R-Tree boost. I added a hundred thousand points in r-tree boost. Now I want to cluster and group my points like this link. It seems like that I should calculate k-mean value from points. How is it possible to calculate k-mean value from r-tree points geometry.
There are various clustering algorithms having different properties and inputs. What needs to be considered before choosing an algorithm is what do you want to achieve. k-means referred by you in the question aims to partition set of points into k clusters. So the input is the desired number of clusters. On the other hand, the algorithm described in the blog you linked, a variant of greedy clustering algorithm aims to partition set of points into circular clusters of some size. The input is the radius of the desired cluster.
There are various algorithms performing k-means clustering used for different data and applications like separating 2 n-dimensional subsets with hyperplane or clustering with Voronoi diagram (Lloyd's algorithm) often called k-means algorithm. There are also density-based clustering algorithms mentioned by #Anony-Mousse in the comments under your question.
In the article, you mentioned it's a hierarchical version of greedy clustering. They have to calculate the clusters for multiple zoom levels and to avoid analyzing all of the points each time they use the centroids of clusters from the previously analyzed level as a source of points for clustering for the next level. However, in this answer, I'll show how to implement this algorithm for one level only. So the input will be a set of points and a size of cluster as a radius. If you need hierarchical version you should calculate centroids of the output clusters and use them as the input of the algorithm for the next level.
Using Boost.Geometry R-tree the algorithm for one level (so not hierarchical) could be implemented like this (in C++11):
#include <boost/geometry.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <boost/range/adaptor/indexed.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <iostream>
#include <vector>
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
typedef bg::model::point<double, 2, bg::cs::cartesian> point_t;
typedef bg::model::box<point_t> box_t;
typedef std::vector<point_t> cluster_t;
// used in the rtree constructor with Boost.Range adaptors
// to generate std::pair<point_t, std::size_t> from point_t on the fly
template <typename First, typename Second>
struct pair_generator
{
typedef std::pair<First, Second> result_type;
template<typename T>
inline result_type operator()(T const& v) const
{
return result_type(v.value(), v.index());
}
};
// used to hold point-related information during clustering
struct point_data
{
point_data() : used(false) {}
bool used;
};
// find clusters of points using cluster radius r
void find_clusters(std::vector<point_t> const& points,
double r,
std::vector<cluster_t> & clusters)
{
typedef std::pair<point_t, std::size_t> value_t;
typedef pair_generator<point_t, std::size_t> value_generator;
if (r < 0.0)
return; // or return error
// create rtree holding std::pair<point_t, std::size_t>
// from container of points of type point_t
bgi::rtree<value_t, bgi::rstar<4> >
rtree(points | boost::adaptors::indexed()
| boost::adaptors::transformed(value_generator()));
// create container holding point states
std::vector<point_data> points_data(rtree.size());
// for all pairs contained in the rtree
for(auto const& v : rtree)
{
// ignore points that were used before
if (points_data[v.second].used)
continue;
// current point
point_t const& p = v.first;
double x = bg::get<0>(p);
double y = bg::get<1>(p);
// find all points in circle of radius r around current point
std::vector<value_t> res;
rtree.query(
// return points that are in a box enclosing the circle
bgi::intersects(box_t{{x-r, y-r},{x+r, y+r}})
// and were not used before
// and are indeed in the circle
&& bgi::satisfies([&](value_t const& v){
return points_data[v.second].used == false
&& bg::distance(p, v.first) <= r;
}),
std::back_inserter(res));
// create new cluster
clusters.push_back(cluster_t());
// add points to this cluster and mark them as used
for(auto const& v : res) {
clusters.back().push_back(v.first);
points_data[v.second].used = true;
}
}
}
int main()
{
std::vector<point_t> points;
for (double x = 0.0 ; x < 10.0 ; x += 1.0)
for (double y = 0.0 ; y < 10.0 ; y += 1.0)
points.push_back(point_t{x, y});
std::vector<cluster_t> clusters;
find_clusters(points, 3.0, clusters);
for(size_t i = 0 ; i < clusters.size() ; ++i) {
std::cout << "Cluster " << i << std::endl;
for (auto const& p : clusters[i]) {
std::cout << bg::wkt(p) << std::endl;
}
}
}
See also their implementation: https://github.com/mapbox/supercluster/blob/master/index.js#L216
Furthermore, take into account the remarks of #Anony-Mousse about the accuracy of distance calculation on the globe. The solution above is for cartesian coordinate system. If you want to use different coordinate system then you have to define point type differently, e.g. use bg::cs::spherical_equatorial<bg::degree> or bg::cs::geographic<bg::degree> instead of bg::cs::cartesian. You also have to generate the query bounding box differently. But bg::distance() will automatically return correct distance after changing the point type.
Related
I'm writing some algorithm in C++ for parallel graph coloring using Boost Graph and adjacency_list.
I'm working with very big graph (the smallest has 32K vertices).
What I'm trying to do is to take the whole set of vertices, split them in parts and assign each part to a different thread and work in parallel, but I'm struggling with some passages.
The basic idea what this:
int step = g.m_vertices.size()/4;
int min = 0;
for(int i = 0; i < 4; i++){
// call the function
}
And the function I call inside is something like that
for (vp = vertices(g); vp.first != vp.second; ++vp.first) {
cout << *vp.first << endl;
}
So I have two questions:
g.m_vertices.size()/4; is the right solutions? If initially I have 10 vertices, then I remove some vertex in the middle (e.g. 4), only 6 vertices left (so this is the new size) but the index of the vertices go from 0 to 5 or from 0 to 9?
How can pass only a subset of vertices to vp instead of passing vertices(g)?
g.m_vertices.size()/4; is the right solutions?
That depends ONLY on your requirements.
If initially I have 10 vertices, then I remove some vertex in the middle (e.g. 4), only 6 vertices left (so this is the new size) but the index of the vertices go from 0 to 5 or from 0 to 9?
That depends on your graph model. You don't specify the type of your graph (I know, you do say which template, but not the template parameters). Assuming vecS for the Vertex container selector, then yes, after 4 removals, the vertex descriptors (and index) will be [0,6).
How can pass only a subset of vertices to vp instead of passing vertices(g)
Many ways.
you can std::for_each with a parallel execution policy
you can use openmp to create a parallel section from the plain loop
you can use filtered_graph adapter to create 4 "views" of the underlying graph and operate on those
you can use PBGL which is actually created for dealing with huge graphs. This has the added benefit that it works with threading/interprocess/inter-host communication, can coordinate algorithms across segments etc.
you can use sub_graphs; this is mainly (only) interesting if the way your graphs get built have a natural segmentation
None of the solutions are trivial. But, here's naive demo using filtered_graph:
Live On Compiler Explorer
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/random.hpp>
#include <iostream>
#include <random>
using G = boost::adjacency_list<>;
using V = G::vertex_descriptor;
G make_graph() {
G g;
std::mt19937 prng(std::random_device{}());
generate_random_graph(g, 32 * 1024 - (prng() % 37), 64 * 1024, prng);
return g;
}
template <int NSegments, int Segment> struct SegmentVertices {
std::hash<V> _h;
bool operator()(V vd) const { return (_h(vd) % NSegments) == Segment; }
};
template <int N>
using Quart = boost::filtered_graph<G, boost::keep_all, SegmentVertices<4, N>>;
template <typename Graph>
void the_function(Graph const& g, std::string_view name)
{
std::cout << name << " " << size(boost::make_iterator_range(vertices(g)))
<< " vertices\n";
}
int main()
{
G g = make_graph();
the_function(g, "full graph");
Quart<0> f0(g, {}, {});
Quart<1> f1(g, {}, {});
Quart<2> f2(g, {}, {});
Quart<3> f3(g, {}, {});
the_function(f0, "f0");
the_function(f1, "f1");
the_function(f2, "f2");
the_function(f3, "f3");
}
Printing e.g.
full graph 32766 vertices
f0 8192 vertices
f1 8192 vertices
f2 8191 vertices
f3 8191 vertices
I would like to extract the medial axis of a MultiPolygon using CGAL.
Looking through the CGAL documentation I only see functions to make a straight skeleton.
How can I use CGAL to get the medial axis?
Overview
If we have a polygon (a planar domain with straight edges, possibly convex) then the edges which form the medial axis of a polygon are a subset of the multipolygon's Voronoi diagram.
Our strategy, then, is to find the polygon's Voronoi diagram and then remove edges from the diagram until we are left with the medial axis. If we have a MultiPolygon we just repeat the process for each of its constituent polygons.
I'll demonstrate the process visually below and then present code to accomplish it. The data for the test polygon I'll use is this WKT:
MULTIPOLYGON(((223.83073496659313 459.9703924976702,256.1247216035629 304.08821449578033,1.1135857461033538 187.63424160514955,468.8195991091309 189.21374898389445,310.69042316258424 318.7630937196408,432.07126948775 451.93407043677666,453.2293986636971 612.1086753947116,32.29398663697134 616.325100949687,223.83073496659313 459.9703924976702)))
The polygon looks like this:
The full Voronoi diagram of the polygon looks like this:
At this point we can consider three types of vertices in the Voronoi diagram: those that are properly within the polygon, those which lie on its border, and those which are properly outside the polygon. It's visually obvious that the medial axis does not contain any edge of the Voronoi diagram which is defined in part by a point which is properly outside the polygon. Therefore, we remove these exterior edges. Leaving us with this:
Not all of the Voronoi edges in the above figure are part of the medial axis. It turns out that those edges which are in part defined by the concave vertices (aka "reflex vertices") of the polygon are not part of the medial axis. The concave vertices of the polygon are those that "point inwards". Computationally, we can find them by using cross-products. (Incidentally, the area of a polygon can be calculated by summing the cross-products of each of its vertices.)
Removing the edges discussed above gives us the final medial axis:
The Code
The following C++ code calculates the medial axis of a multipolygon.
// Compile with: clang++ -DBOOST_ALL_NO_LIB -DCGAL_USE_GMPXX=1 -O3 -g -Wall -Wextra -pedantic -march=native -frounding-math main.cpp -lgmpxx -lmpfr -lgmp
#include <CGAL/Boolean_set_operations_2.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/IO/WKT.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Segment_Delaunay_graph_2.h>
#include <CGAL/Segment_Delaunay_graph_adaptation_policies_2.h>
#include <CGAL/Segment_Delaunay_graph_adaptation_traits_2.h>
#include <CGAL/Segment_Delaunay_graph_traits_2.h>
#include <CGAL/squared_distance_2.h>
#include <CGAL/Voronoi_diagram_2.h>
#include <algorithm>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stdexcept>
#include <unordered_set>
typedef CGAL::Exact_predicates_exact_constructions_kernel K;
typedef CGAL::Segment_Delaunay_graph_traits_2<K> Gt;
typedef CGAL::Segment_Delaunay_graph_2<Gt> SDG2;
typedef CGAL::Segment_Delaunay_graph_adaptation_traits_2<SDG2> AT;
typedef CGAL::Segment_Delaunay_graph_degeneracy_removal_policy_2<SDG2> AP;
typedef CGAL::Voronoi_diagram_2<SDG2, AT, AP> VoronoiDiagram;
typedef AT::Site_2 Site_2;
typedef AT::Point_2 Point_2;
typedef VoronoiDiagram::Locate_result Locate_result;
typedef VoronoiDiagram::Vertex_handle Vertex_handle;
typedef VoronoiDiagram::Face_handle Face_handle;
typedef VoronoiDiagram::Halfedge_handle Halfedge_handle;
typedef VoronoiDiagram::Ccb_halfedge_circulator Ccb_halfedge_circulator;
typedef VoronoiDiagram::Bounded_halfedges_iterator BHE_Iter;
typedef VoronoiDiagram::Halfedge Halfedge;
typedef VoronoiDiagram::Vertex Vertex;
typedef CGAL::Polygon_with_holes_2<K> Polygon;
typedef std::deque<Polygon> MultiPolygon;
/// Creates a hash of a Point_2, used for making O(1) point lookups
// struct Point2Hash {
// size_t operator()(const Point_2 &pt) const {
// std::hash<double> hasher;
// auto seed = hasher(pt.x());
// // boost::hash_combine from https://stackoverflow.com/q/35985960/752843
// seed ^= hasher(pt.y()) + 0x9e3779b9 + (seed<<6) + (seed>>2);
// return seed;
// }
// };
typedef std::set<Point_2> Point2_Set;
typedef std::map<Vertex_handle, int> VH_Int_Map;
/// Holds a more accessible description of the Voronoi diagram
struct MedialData {
/// Map of vertices comprising the Voronoi diagram
VH_Int_Map vertex_handles;
/// List of edges in the diagram (pairs of the vertices above)
std::vector<std::pair<int, int>> edges;
/// Medial axis up governor. 1:1 correspondance with edges above.
std::vector<VoronoiDiagram::Delaunay_graph::Vertex_handle> ups;
/// Medial axis down governor. 1:1 correspondance with edges above.
std::vector<VoronoiDiagram::Delaunay_graph::Vertex_handle> downs;
};
/// Read well-known text from #p filename to obtain shape boundary
MultiPolygon get_wkt_from_file(const std::string& filename){
std::ifstream fin(filename);
MultiPolygon mp;
CGAL::read_multi_polygon_WKT(fin, mp);
if(mp.empty()){
throw std::runtime_error("WKT file '" + filename + "' was empty!");
}
for(const auto &poly: mp){
if(poly.outer_boundary().size()==0){
throw std::runtime_error("WKT file '" + filename + "' contained a polygon without an outer boundary!");
}
}
return mp;
}
/// Converts a MultiPolygon into its corresponding Voronoi diagram
VoronoiDiagram convert_mp_to_voronoi_diagram(const MultiPolygon &mp){
VoronoiDiagram vd;
const auto add_segments_to_vd = [&](const auto &poly){
for(std::size_t i=0;i<poly.size();i++){
std::cerr<<i<<" "<<std::fixed<<std::setprecision(10)<<poly[i]<<std::endl;
// Modulus to close the loop
vd.insert(
Site_2::construct_site_2(poly[i], poly[(i+1)%poly.size()])
);
}
};
for(const auto &poly: mp){ // For each polygon in MultiPolygon
std::cout<<poly<<std::endl; // Print polygon to screen for debugging
add_segments_to_vd(poly.outer_boundary()); // Add the outer boundary
for(const auto &hole : poly.holes()){ // And any holes
add_segments_to_vd(hole);
}
}
if(!vd.is_valid()){
throw std::runtime_error("Voronoi Diagram was not valid!");
}
return vd;
}
/// Find #p item in collection #p c or add it if not present.
/// Returns the index of `item`'s location
int find_or_add(VH_Int_Map &c, const Vertex_handle &item){
// Map means we can do this in log(N) time
if(c.count(item) == 0){
c.emplace(item, c.size());
return c.size() - 1;
}
return c.at(item);
}
/// Convert a map of <T, int> pairs to a vector of `T` ordered by increasing int
std::vector<Vertex_handle> map_to_ordered_vector(const VH_Int_Map &m){
std::vector<std::pair<Vertex_handle, int>> to_sort(m.begin(), m.end());
to_sort.reserve(m.size());
std::sort(to_sort.begin(), to_sort.end(), [](const auto &a, const auto &b){
return a.second < b.second;
});
std::vector<Vertex_handle> ret;
ret.reserve(to_sort.size());
std::transform(begin(to_sort), end(to_sort), std::back_inserter(ret),
[](auto const& pair){ return pair.first; }
);
return ret;
}
/// Find vertex handles which are in the interior of the MultiPolygon
std::set<Vertex_handle> identify_vertex_handles_inside_mp(
const VoronoiDiagram &vd,
const MultiPolygon &mp
){
// Used to accelerate interior lookups by avoiding Point-in-Polygon checks for
// vertices we've already considered
std::set<Vertex_handle> considered;
// The set of interior vertices we are building
std::set<Vertex_handle> interior;
for (
auto edge_iter = vd.bounded_halfedges_begin();
edge_iter != vd.bounded_halfedges_end();
edge_iter++
) {
// Determine if an orientation implies an interior vertex
const auto inside = [](const auto &orientation){
return orientation == CGAL::ON_ORIENTED_BOUNDARY || orientation == CGAL::POSITIVE;
};
// Determine if a vertex is in the interior of the multipolygon and, if so,
// add it to `interior`
const auto vertex_in_mp_interior = [&](const Vertex_handle& vh){
// Skip vertices which have already been considered, since a vertex may
// be connected to multiple halfedges
if(considered.count(vh)!=0){
return;
}
// Ensure we don't look at a vertex twice
considered.insert(vh);
// Determine if the vertex is inside of any polygon of the MultiPolygon
const auto inside_of_a_poly = std::any_of(
mp.begin(), mp.end(), [&](const auto &poly) {
return inside(CGAL::oriented_side(vh->point(), poly));
}
);
// If the vertex was inside the MultiPolygon make a note of it
if(inside_of_a_poly){
interior.insert(vh);
}
};
// Check both vertices of the current halfedge of the Voronoi diagram
vertex_in_mp_interior(edge_iter->source());
vertex_in_mp_interior(edge_iter->target());
}
return interior;
}
/// The medial axis is formed by building a Voronoi diagram and then removing
/// the edges of the diagram which connect to the concave points of the
/// MultiPolygon. Here, we identify those concave points
Point2_Set identify_concave_points_of_mp(const MultiPolygon &mp){
Point2_Set concave_points;
// Determine cross-product, given three points. The sign of the cross-product
// determines whether the point is concave or convex.
const auto z_cross_product = [](const Point_2 &pt1, const Point_2 &pt2, const Point_2 &pt3){
const auto dx1 = pt2.x() - pt1.x();
const auto dy1 = pt2.y() - pt1.y();
const auto dx2 = pt3.x() - pt2.x();
const auto dy2 = pt3.y() - pt2.y();
return dx1 * dy2 - dy1 * dx2;
};
// Loop through all the points in a polygon, get their cross products, and
// add any concave points to the set we're building.
// `sense` should be `1` for outer boundaries and `-1` for holes, since holes
// will have points facing outward.
const auto consider_polygon = [&](const auto &poly, const double sense){
for(size_t i=0;i<poly.size()+1;i++){
const auto zcp = z_cross_product(
poly[(i + 0) % poly.size()],
poly[(i + 1) % poly.size()],
poly[(i + 2) % poly.size()]
);
if(sense*zcp < 0){
concave_points.insert(poly[(i + 1) % poly.size()]);
}
}
};
// Loop over the polygons of the MultiPolygon, as well as their holes
for(const auto &poly: mp){
// Outer boundary has positive sense
consider_polygon(poly.outer_boundary(), 1);
for(const auto &hole: poly.holes()){
// Inner boundaries (holes) have negative (opposite) sense
consider_polygon(hole, -1);
}
}
return concave_points;
}
/// Print the points which collectively comprise the medial axis
void print_medial_axis_points(const MedialData &md, const std::string &filename){
std::ofstream fout(filename);
fout<<"x,y"<<std::endl;
for (const auto &vh : map_to_ordered_vector(md.vertex_handles)) {
fout << vh->point().x() << "," << vh->point().y() << std::endl;
}
}
/// Prints the edges of the medial diagram
void print_medial_axis_edges(const MedialData &md, const std::string &filename){
std::ofstream fout(filename);
fout<<"SourceIdx,TargetIdx,UpGovernorIsPoint,DownGovernorIsPoint"<<std::endl;
for (std::size_t i = 0; i < md.edges.size(); i++) {
fout << md.edges[i].first << ","
<< md.edges[i].second << ","
<< md.ups[i]->is_point() << "," // Is up-governor a point?
<< md.downs[i]->is_point() // Is down-governor a point?
<< std::endl;
}
}
MedialData filter_voronoi_diagram_to_medial_axis(
const VoronoiDiagram &vd,
const MultiPolygon &mp
){
MedialData ret;
const auto interior = identify_vertex_handles_inside_mp(vd, mp);
const auto concave_points = identify_concave_points_of_mp(mp);
// Returns true if a point is a concave point of the MultiPolygon
const auto pconcave = [&](const Point_2 &pt){
return concave_points.count(pt) != 0;
};
// The Voronoi diagram is comprised of a number of vertices connected by lines
// Here, we go through each edge of the Voronoi diagram and determine which
// vertices it's incident on. We add these vertices to `ret.vertex_handles`
// so that they will have unique ids.
// The `up` and `down` refer to the medial axis governors - that which
// constrains each edge of the Voronoi diagram
for (
auto edge_iter = vd.bounded_halfedges_begin();
edge_iter != vd.bounded_halfedges_end();
edge_iter++
) {
const Halfedge& halfedge = *edge_iter;
const Vertex_handle& v1p = halfedge.source();
const Vertex_handle& v2p = halfedge.target();
// Filter Voronoi diagram to only the part in the interior of the
// MultiPolygon
if(interior.count(v1p)==0 || interior.count(v2p)==0){
continue;
}
// Drop those edges of the diagram which are not part of the medial axis
if(pconcave(v1p->point()) || pconcave(v2p->point())){
continue;
}
// Get unique ids for edge vertex handle that's part of the medial axis
const auto id1 = find_or_add(ret.vertex_handles, v1p);
const auto id2 = find_or_add(ret.vertex_handles, v2p);
ret.edges.emplace_back(id1, id2);
// Keep track of the medial axis governors
ret.ups.push_back(halfedge.up());
ret.downs.push_back(halfedge.down());
}
return ret;
}
int main(int argc, char** argv) {
if(argc!=2){
std::cerr<<"Syntax: "<<argv[0]<<" <Shape Boundary WKT>"<<std::endl;
return -1;
}
CGAL::set_pretty_mode(std::cout);
const auto mp = get_wkt_from_file(argv[1]);
const auto voronoi = convert_mp_to_voronoi_diagram(mp);
const auto ma_data = filter_voronoi_diagram_to_medial_axis(voronoi, mp);
print_medial_axis_points(ma_data, "voronoi_points.csv");
print_medial_axis_edges(ma_data, "voronoi_edges.csv");
return 0;
}
You can plot the resulting medial axis with this Python script:
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
from shapely import wkt
fig, ax = plt.subplots()
# Output file from C++ medial axis code
pts = np.loadtxt("build/voronoi_points.csv", skiprows=1, delimiter=',')
fig4 = wkt.loads(open("fig4_data.wkt").read())
for geom in fig4.geoms:
xs, ys = geom.exterior.xy
ax.plot(xs, ys, '-ok', lw=4)
# Output file from C++ medial axis code
ma = np.loadtxt("build/voronoi_edges.csv", dtype=int, skiprows=1, delimiter=',')
for mal in ma:
print(mal)
ax.plot((pts[mal[0]][0], pts[mal[1]][0]), (pts[mal[0]][1], pts[mal[1]][1]), '-o')
plt.show()
I need to be able to calculate the Reock compactness value for an arbitrary polygon (guaranteed to be simple). This value is defined as the ratio of the polygon's area to the area of its minimum bounding circle. I have the following data structure:
#include <vector>
#include <array>
using namespace std;
typedef array<int, 2> coordinate;
class Polygon {
vector<coordinate> border; // contains all coordinates
double get_area();
// a bunch of other data here...
};
class Polygon_Group {
vector<Polygon> borders;
double get_area();
};
This structure cannot be changed. I need a way to find the Reock score for an object of type Polygon_Group, and I need it to be as fast as possible. I have played around with Miniball for this, but it is really quite slow for me with large amounts of points. (Here's a small example of my miniball code)
#include "Miniball.hpp"
typedef std::vector<std::vector<double> >::const_iterator PointIterator;
typedef std::vector<double>::const_iterator CoordIterator;
typedef Miniball::Miniball <Miniball::CoordAccessor<PointIterator, CoordIterator> > MB;
double get_reock(Polygon_Group pg) {
// the list of points as vector<int, 2>
vector<coordinate> lp;
// the list of points as vector<double>, because
// through experimentation, this was all that worked
vector<vector<double> > p;
for (Polygon poly : pg.borders) {
lp.insert(lp.end(), poly.border.begin(), poly.border.end());
}
p.reserve(lp.size());
for (int i = 0; i < lp.size(); i++) {
p.emplace_back(lp[i].begin(), lp[i].end());
}
lp.clear();
MB mb (2, p.begin(), p.end());
return (pg.get_area() / (mb.squared_radius() * PI));
}
As stated above, this function was too slow for my needs (taking 0.4 seconds in some cases).
Really, what I need to know is how to quickly collapse the all the points in Polygon_Group into some sort of list, and then have some way to find the minimum bounding circle of those points. I can handle the area myself.
I have created a vector of points. Each point can have any of the specified 16 colors. I am able to create a Voronoi diagram using the Boost library but am unable to color the cells efficiently. The Boost documentation says that there is a function as below :
void color(color_type color) const
to set color of the cells but I am unable to find any implementation example of it. The way Voronoi diagram is created, I um unable to do it. Please point in the right direction. This is what I have till now.
struct Point {
double a;
double b;
Point(double x, double y) : a(x), b(y) {}
};
// This point is mapped to Point concept of Boost.Code omitted
std::vector<Point_Collection> *points_info_vector;
points_info_vector = new std::vector<Point_Collection>();
for (int i = 0; i < 5000; i++) {
Point_Collection xy_color = { Point(rand() % 1000, rand() % 1000), rand() % 16 };
points_info_vector->push_back(xy_color);
}
std::vector<Point> *points;
points = new std::vector<Point>();
for (int i = 0; i < points_info_vector->size(); i++) {
points->push_back(points_info_vector->at(i).xy);
}
voronoi_diagram<double> vd;
construct_voronoi(points->begin(), points->end(), &vd);
Also, is there a more efficient way of using the construct_voronoi() with our Point_Collection structure without making a temporary structure just for points?
Found a partial answer : We can iterate over the cells and find out the index of the input geometry that is given by the builder library. Quoting the documentation :
Source index corresponds to the unique id, issued to each input geometry (e.g. incremental counter, used by the Voronoi builder).
So, I can do :
for (voronoi_diagram<double>::const_cell_iterator it = vd.cells().begin(); it!= vd.cells().end(); it++ )
{
std::cout << it->source_index() << "\n" ;
it->color(points_info_vector->at(it->source_index()).color);
}
So, from this index I can use the void color(color_type color) const to set the color of the cell from my earlier vector index.
Still, I would like to know an efficient way to create Voronoi diagram with the custom structure iterator as the function parameter, without having to copy the points to other vector.
I'm using CGAL with Qt to draw Voronoi diagram. I used CGAL::Voronoi_diagram_2<DT,AT,AP>since I need the faces. This is the example code:
for(Face_iterator f = VD.faces_begin(); f != VD.faces_end(); f++)
{
Ccb_halfedge_circulator ec_start = (f)->ccb();
Ccb_halfedge_circulator ec = ec_start;
do {
if (!ec->has_source())
{
}
else
QpolyF << QPointF(((Halfedge_handle)ec)->source()->point().x(), ((Halfedge_handle)ec)->source()->point().y());
} while ( ++ec != ec_start );
VectPolygon.push_back(QpolyF);
QpolyF.clear();}
I need to clip the rays that has source or target in infinity. If I use the Cropped_voronoi_from_delaunay to generate voronoi it only gives the segments not the faces. these are the typedefs:
typedef K::Line_2 Line_2;
typedef CGAL::Delaunay_triangulation_2<K> Delaunay_triangulation_2;
typedef Delaunay_triangulation_2::Face_iterator dt_Face_iterator;
typedef Delaunay_triangulation_2::Edge_circulator dt_Edge_circulator;
// typedefs for defining the adaptor
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Delaunay_triangulation_2<K> DT;
typedef CGAL::Delaunay_triangulation_adaptation_traits_2<DT> AT;
typedef CGAL::Delaunay_triangulation_caching_degeneracy_removal_policy_2<DT> AP;
typedef CGAL::Voronoi_diagram_2<DT,AT,AP> VD;
// typedef for the result type of the point location
typedef AT::Site_2 Site_2;
typedef AT::Point_2 Point_2;
typedef VD::Locate_result Locate_result;
typedef VD::Vertex_handle Vertex_handle;
typedef VD::Face_handle Face_handle;
typedef VD::Face_iterator Face_iterator;
typedef VD::Halfedge_handle Halfedge_handle;
typedef VD::Ccb_halfedge_circulator Ccb_halfedge_circulator;
There is some experimental code here: http://code.google.com/p/cgal-voronoi-cropping that crop a voronoi diagram to a rectangle, the result being a HDS. See main.cpp in the test directory
I know it's possible to do it with CGAL, but I found a workaround for now. in Qt, QPolygon class has the function to find intersected polygons. Qpolygon::intersected(yourPolygon).
this is the results:
The following will generate a random point cloud, find its Voronoi diagram, crop that diagram to the cloud's bounding box, and generate well-known text polygons.
I'm not sure how to integrate this with Qt, but, presumably, once you have the polygons this part will be easy(ish).
//Finds the cropped Voronoi diagram of a set of points and saves it as WKT
//Compile with: g++ main.cpp -Wall -lCGAL -lgmp
//Author: Richard Barnes (rbarnes.org)
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Regular_triangulation_filtered_traits_2.h>
#include <CGAL/Regular_triangulation_adaptation_traits_2.h>
#include <CGAL/Regular_triangulation_adaptation_policies_2.h>
#include <CGAL/Regular_triangulation_2.h>
#include <CGAL/Voronoi_diagram_2.h>
#include <CGAL/Boolean_set_operations_2.h>
#include <CGAL/bounding_box.h>
#include <CGAL/Polygon_2.h>
#include <iostream>
#include <cstdint>
//Used to convert otherwise infinite rays into looooong line segments
const int RAY_LENGTH = 1000;
typedef CGAL::Exact_predicates_exact_constructions_kernel K;
typedef CGAL::Regular_triangulation_filtered_traits_2<K> Traits;
typedef CGAL::Regular_triangulation_2<Traits> RT2;
typedef CGAL::Regular_triangulation_adaptation_traits_2<RT2> AT;
typedef CGAL::Regular_triangulation_degeneracy_removal_policy_2<RT2> DRP;
typedef CGAL::Voronoi_diagram_2<RT2, AT, DRP> VD;
int main(int argc, char **argv){
std::vector<RT2::Weighted_point> wpoints;
std::cout.precision(4);
std::cout.setf(std::ios::fixed);
//Generated random points
for(int i=0;i<100;i++)
//Weight of 0 gives a Voronoi diagram. Non-zero weight gives a power diagram
wpoints.push_back(RT2::Weighted_point(K::Point_2(rand()%100,rand()%100), 0));
//Find the bounding box of the points. This will be used to crop the Voronoi
//diagram later.
const K::Iso_rectangle_2 bbox = CGAL::bounding_box(wpoints.begin(), wpoints.end());
//Create a Regular Triangulation from the points
RT2 rt(wpoints.begin(), wpoints.end());
rt.is_valid();
//Wrap the triangulation with a Voronoi diagram adaptor. This is necessary to
//get the Voronoi faces.
VD vd(rt);
//CGAL often returns objects that are either segments or rays. This converts
//these objects into segments. If the object would have resolved into a ray,
//that ray is intersected with the bounding box defined above and returned as
//a segment.
const auto ConvertToSeg = [&](const CGAL::Object seg_obj, bool outgoing) -> K::Segment_2 {
//One of these will succeed and one will have a NULL pointer
const K::Segment_2 *dseg = CGAL::object_cast<K::Segment_2>(&seg_obj);
const K::Ray_2 *dray = CGAL::object_cast<K::Ray_2>(&seg_obj);
if (dseg) { //Okay, we have a segment
return *dseg;
} else { //Must be a ray
const auto &source = dray->source();
const auto dsx = source.x();
const auto dsy = source.y();
const auto &dir = dray->direction();
const auto tpoint = K::Point_2(dsx+RAY_LENGTH*dir.dx(),dsy+RAY_LENGTH*dir.dy());
if(outgoing)
return K::Segment_2(
dray->source(),
tpoint
);
else
return K::Segment_2(
tpoint,
dray->source()
);
}
};
//First line of WKT CSV output
std::cout<<"\"id\",\"geom\"\n";
int fnum = 0;
//Loop over the faces of the Voronoi diagram in some arbitrary order
for(VD::Face_iterator fit = vd.faces_begin(); fit!=vd.faces_end();++fit,fnum++){
CGAL::Polygon_2<K> pgon;
//Edge circulators traverse endlessly around a face. Make a note of the
//starting point so we know when to quit.
VD::Face::Ccb_halfedge_circulator ec_start = fit->ccb();
//Current location of the edge circulator
VD::Face::Ccb_halfedge_circulator ec = ec_start;
do {
//A half edge circulator representing a ray doesn't carry direction
//information. To get it, we take the dual of the dual of the half-edge.
//The dual of a half-edge circulator is the edge of a Delaunay triangle.
//The dual of the edge of Delaunay triangle is either a segment or a ray.
// const CGAL::Object seg_dual = rt.dual(ec->dual());
const CGAL::Object seg_dual = vd.dual().dual(ec->dual());
//Convert the segment/ray into a segment
const auto this_seg = ConvertToSeg(seg_dual, ec->has_target());
pgon.push_back(this_seg.source());
//If the segment has no target, it's a ray. This means that the next
//segment will also be a ray. We need to connect those two rays with a
//segment. The following accomplishes this.
if(!ec->has_target()){
const CGAL::Object nseg_dual = vd.dual().dual(ec->next()->dual());
const auto next_seg = ConvertToSeg(nseg_dual, ec->next()->has_target());
pgon.push_back(next_seg.target());
}
} while ( ++ec != ec_start ); //Loop until we get back to the beginning
//In order to crop the Voronoi diagram, we need to convert the bounding box
//into a polygon. You'd think there'd be an easy way to do this. But there
//isn't (or I haven't found it).
CGAL::Polygon_2<K> bpoly;
bpoly.push_back(K::Point_2(bbox.xmin(),bbox.ymin()));
bpoly.push_back(K::Point_2(bbox.xmax(),bbox.ymin()));
bpoly.push_back(K::Point_2(bbox.xmax(),bbox.ymax()));
bpoly.push_back(K::Point_2(bbox.xmin(),bbox.ymax()));
//Perform the intersection. Since CGAL is very general, it believes the
//result might be multiple polygons with holes.
std::list<CGAL::Polygon_with_holes_2<K>> isect;
CGAL::intersection(pgon, bpoly, std::back_inserter(isect));
//But we know better. The intersection of a convex polygon and a box is
//always a single polygon without holes. Let's assert this.
assert(isect.size()==1);
//And recover the polygon of interest
auto &poly_w_holes = isect.front();
auto &poly_outer = poly_w_holes.outer_boundary();
//Print the polygon as a WKT polygon
std::cout<<fnum<<", "
"\"POLYGON ((";
for(auto v=poly_outer.vertices_begin();v!=poly_outer.vertices_end();v++)
std::cout<<v->x()<<" "<<v->y()<<", ";
std::cout<<poly_outer.vertices_begin()->x()<<" "<<poly_outer.vertices_begin()->y()<<"))\"\n";
}
return 0;
}