I'm using CGAL to construct a half-edge based structure of 2D Voronoi diagram. I chose the CGAL::Voronoi_diagram_2<DT,AT,AP> class to do that, and starting with the example provided by CGAL.
What confuses me now is: for the initial sites (which are loaded from file) added by the insert method as follow:
Site_2 t;
while ( ifs >> t ) { vd.insert(t); }
Things seem to be correct.
But when I try to add some extra sites by the insert method again, and use vd.is_valid() to check the validity, the assertion always return false. I enter the the is_valid() function and find that the following line:
valid = valid && ap_.is_valid(dual_);
is false since ap_.is_valid(dual_) return false. Since I'm new to CGAL, I only know that ap_ is some Adaption_policy class and here it is Delaunay_triangulation_caching_degeneracy_removal_policy_2.
I tried to comment this assertion, but the program crashed for some reason, which means apparently this assertion is necessary.
Does anyone know how to make this assertion correct? Thanks in advance.
Edit:
Following is the main part of my code (which uses OpenGL under GLUT, and I left out some unrelated part such as rendering):
#include <iostream>
#include <fstream>
#include <cassert>
#include <windows.h>
#include <gl\glut.h>
#include <cmath>
// includes for defining the Voronoi diagram adaptor
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/Voronoi_diagram_2.h>
#include <CGAL/Delaunay_triangulation_adaptation_traits_2.h>
#include <CGAL/Delaunay_triangulation_adaptation_policies_2.h>
#include <CGAL/intersections.h>
// 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::Halfedge_handle Halfedge_handle;
typedef VD::Ccb_halfedge_circulator Ccb_halfedge_circulator;
VD vd;
int Width = 640;
int Height = 480;
void myMouseClick(int button, int state, int x, int y)
{
if (state != GLUT_DOWN) return;
if (button == GLUT_LEFT_BUTTON)
{
double viewPort[4];
glGetDoublev(GL_VIEWPORT, viewPort);
Point_2 p0(x, viewPort[3] - y);
vd.insert(Site_2(p0));
assert(vd.is_valid()); //here the assertion fails
}
glutPostRedisplay();
}
int myInit()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_POINT_SMOOTH);
// load voronoi data
std::ifstream ifs("data/data2.dt.cin");
assert( ifs );
Site_2 t;
while ( ifs >> t ) { vd.insert(t); }
ifs.close();
assert( vd.is_valid() ); //here the assertion passes
return 0;
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(Width, Height);
glutInitWindowPosition(100, 100);
glutCreateWindow("2-D Voronoi Diagram");
glutMouseFunc(myMouseClick);
if(myInit() < 0) return -1;
glutMainLoop();
return 0;
}
Edit:
It seems that the CGAL::Delaunay_triangulation_caching_degeneracy_removal_policy_2<DT> policy's problem. I replace it with CGAL::Identity_policy_2<DT, AT>, the default one, and the assertion passes.
According to the documents, the former policy seems to find degeneracy edge in the voronoi diagram, but I don't see any one visually (I don't have reputation to upload the result, but the input data is the same as what I mentioned in the comment.
Related
My problem is that when i run programm it runs normally for around 10-20 seconds, and then it glitch out. You can see further what's happening on the video.
https://youtu.be/YOlhjQFTzZc
This error is hunting me for over a month, First i thought this was some mistake in making shorter Render function. But not.
You can see it here.
void Render(char * image_place, int object_x, int object_y)
{
SDL_Surface * object_image = IMG_Load(image_place);
SDL_Rect object_position;
object_position.x=object_x;
object_position.y=object_y;
SDL_BlitSurface(object_image, NULL, ekran, &object_position);
}
But when i started "researching" on this topic more, i discovered that it happen even without using this function!
Here is code from the video:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#include <windows.h>
#include <time.h>
using namespace std;
//SDL
SDL_Window * okno;
SDL_Surface * ekran;
SDL_Rect pozycja_obramowki;
SDL_Event zdarzenie;
SDL_Rect tlo_pos;
//zmienne
int x_obraz=0;
int y_obraz=0;
int main(int argc, char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
okno = SDL_CreateWindow("LevelEditor",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, 1280, 720, NULL);
ekran = SDL_GetWindowSurface(okno);
while(true)
{
{//render
SDL_Surface * tlo = IMG_Load("biel.png");
tlo_pos.x=0;
tlo_pos.y=0;
SDL_BlitSurface(tlo,NULL, ekran, &tlo_pos);
SDL_Surface * obramowka = IMG_Load("obramowka.png");
pozycja_obramowki.x=x_obraz;
pozycja_obramowki.y=y_obraz;
SDL_BlitSurface(obramowka,NULL, ekran, &pozycja_obramowki);
}
{//zdarzenia
if(SDL_PollEvent(&zdarzenie))
{
if(zdarzenie.type==SDL_QUIT)
{
return 0;
}
}
}
{//sterowanie
if(GetAsyncKeyState(VK_RIGHT)) {x_obraz=x_obraz+5;}
if(GetAsyncKeyState(VK_LEFT)) {x_obraz=x_obraz-5;}
if(GetAsyncKeyState(VK_UP)) {y_obraz=y_obraz-5;}
if(GetAsyncKeyState(VK_DOWN)) {y_obraz=y_obraz+5;}
}
{//fps end & odswiezanie ekranu
SDL_UpdateWindowSurface(okno);
}
}
}
If i wrote something wrong or explained anything wrong, feel free to comment on this post. Any help will be useful, thanks ; )
You shouldn't call IMG_Load repeatedly. (I suspect that you're running out of memory pretty quickly.)
Load all images at startup and store pointers to the resulting surfaces.
// Moved out of the loop
SDL_Surface * tlo = IMG_Load("biel.png");
SDL_Surface * obramowka = IMG_Load("obramowka.png");
while(true)
{
// As before, but without declaring the variables mentioned above.
}
Im trying to make a terrain from a grid of vertices and i have a bug and just cant find it.Im stuck with it for 3 hours.Im using c++ and opengl.Im plan to use a blendmap for texturing and a height map later.Anyway here's the code:
Heres how it should look like: http://postimg.org/image/9431kcvy7/
Heres how it looks:
http://postimg.org/image/xxsoesqkp/
As you can see the tringles are separated by a 1 unit rectagle and it look like all the bottom points form a triangle with the point that has coordinates (0,0,0)
I know this problem might seem easy to solve but ive lost already 3 hours trying.Please help:)
Map.h
#ifndef MAP_H
#define MAP_H
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <windows.h>
using namespace std;
struct coordinate{
float x,y,z;
};
struct face{
int v[3];
int n[3];
};
struct uv{
float x;
float y;
};
class Map
{
private:
int mapX,mapY;
vector<coordinate> vertex;
vector<uv>textureCoordinates;
vector<coordinate>normals;
vector< vector<face> > faces;
string fileNameString;
sf::Image image[5];
sf::Color faceColor,blendPixel,p0,p1,p2;
sf::Image texture;
sf::Uint8 pixels[256*256*4];
unsigned int imageID[3],textureID;
public:
void load(const char *fileName);
void draw();
};
#endif // MAP_H
And Map.cpp
#include "Map.h"
#define blendMap 3
#define heightMap 4
void Map::load(const char *fileName)
{
int i,j;
fileNameString=fileName;
vector<face> F;
coordinate v;
face f;
image[0].loadFromFile(fileNameString+"/0.png");
image[1].loadFromFile(fileNameString+"/1.png");
image[2].loadFromFile(fileNameString+"/2.png");
image[blendMap].loadFromFile(fileNameString+"/blendMap.png");
image[heightMap].loadFromFile(fileNameString+"/heightMap.png");
mapX=image[blendMap].getSize().x;
mapY=image[blendMap].getSize().y;
for(i=-mapY/2;i<mapY/2;i++)
for(j=-mapX/2;j<mapX/2;j++)
{
v.x=j*0.5;
v.z=i*0.5;
vertex.push_back(v);
}
for(i=0;i<mapY-1;i++)
{
for(j=0;j<2*(mapX-1);j++)
F.push_back(f);
faces.push_back(F);
}
for(i=0;i<mapY-1;i++)
for(j=0;j<(mapX-1)*2;j+=2)
{
faces[i][j].v[0]=i*mapX+j;
faces[i][j].v[1]=i*mapX+j+1;
faces[i][j].v[2]=(i+1)*mapX+j;
faces[i][j+1].v[0]=i*mapX+j+1;
faces[i][j+1].v[1]=(i+1)*mapX+j+1;
faces[i][j+1].v[2]=(i+1)*mapX+j;
}
for(i=0;i<mapX*mapY;i++)
{
color=image[heightMap].getPixel(i/mapX,i%mapX);
vertex[i].y=0;//(float)color.r/25.5-10;
}
}
void Map::draw()
{
unsigned int i,j;
for(i=0;i<mapY-1;i++)
for(j=0;j<(mapX-1)*2;j+=2)
{
glBindTexture(GL_TEXTURE_2D,imageID[0]);
glBegin(GL_TRIANGLES);
glTexCoord2f (0,0);
glVertex3f(vertex[faces[i][j].v[0]].x , vertex[faces[i][j].v[0]].y , vertex[faces[i][j].v[0]].z);
glTexCoord2f (1,0);
glVertex3f(vertex[faces[i][j].v[1]].x , vertex[faces[i][j].v[1]].y , vertex[faces[i][j].v[1]].z);
glTexCoord2f (0,1);
glVertex3f(vertex[faces[i][j].v[2]].x , vertex[faces[i][j].v[2]].y , vertex[faces[i][j].v[2]].z);
glTexCoord2f (0,0);
glVertex3f(vertex[faces[i][j+1].v[0]].x , vertex[faces[i][j+1].v[0]].y , vertex[faces[i][j+1].v[0]].z);
glTexCoord2f (1,0);
glVertex3f(vertex[faces[i][j+1].v[1]].x , vertex[faces[i][j+1].v[1]].y , vertex[faces[i][j+1].v[1]].z);
glTexCoord2f (0,1);
glVertex3f(vertex[faces[i][j+1].v[2]].x , vertex[faces[i][j+1].v[2]].y , vertex[faces[i][j+1].v[2]].z);
glEnd();
}
}
A few things:
for(i=-mapY/2;i<mapY/2;i++)
This is dangerous and probably not the intention of the loop, anyway. You want to loop mapY times. However, if mapY is odd, you will loop only mapY - 1 times. E.g. if mapY = 3, then -mapY / 2 = -1; mapY / 2 = 1. So you will loop with the values -1 and 0. That's a first problem, which results in too few vertices in your buffer (this is probably the main problem). Instead do the shifting on the coordinate level:
for(i = 0; i < mapY; i++)
for(j = 0; j < mapX; j++)
{
v.x = j * 0.5 - mapY / 2.0;
v.z = i * 0.5 - mapX / 2.0;
vertex.push_back(v);
}
Is there a reason why you use a vector<vector<...>> for the faces? It will give you all kinds of problems regarding indexing as you already noticed. Just use a vector<Face> and put all your faces in there. Usually, you create this structure once and never touch it again. So the 2D indexing is probably not necessary. If you want to stay with the 2D indexing, this loop has wrong bounds:
for(j=0;j<(mapX-1)*2;j+=2)
This upper bound is an inclusive bound. Therefore, use
for(j = 0; j <= (mapX - 1) * 2; j += 2)
I have an graph which consists of edges and vertices. When the clicks an edge in the graph, the edge is supposed to change colour. I have included some code samples to demonstrate my problem.
To draw the initial graph;
#include "StdAfx.h"
#include <vtkSmartPointer.h>
#include <vtkCallbackCommand.h>
#include <vtkAnnotationLink.h>
#include <vtkRenderedGraphRepresentation.h>
#include <vtkRenderer.h>
#include <vtkDoubleArray.h>
#include <vtkSelectionNode.h>
#include <vtkIdTypeArray.h>
#include <vtkSelection.h>
#include <vtkRenderWindow.h>
#include <vtkUnsignedCharArray.h>
#include <vtkObjectFactory.h>
#include <vtkGraphLayoutStrategy.h>
#include <vtkGraphLayoutView.h>
#include <vtkGraphWriter.h>
#include <vtkMutableUndirectedGraph.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkIntArray.h>
#include <vtkLookupTable.h>
#include <vtkDataSetAttributes.h>
#include <vtkViewTheme.h>
void SelectionCallbackFunction(vtkObject* caller, long unsigned int eventId, void* clientData, void* callData);
vtkSmartPointer<vtkMutableUndirectedGraph> g;
int main(int, char *[])
{
g =
vtkSmartPointer<vtkMutableUndirectedGraph>::New();
vtkIdType v1 = g->AddVertex();
vtkIdType v2 = g->AddVertex();
g->AddEdge(v1, v2);
g->AddEdge(v1, v2);
vtkSmartPointer<vtkCallbackCommand> selectionCallback =
vtkSmartPointer<vtkCallbackCommand>::New();
selectionCallback->SetCallback (SelectionCallbackFunction);
// Create the color array
vtkSmartPointer<vtkIntArray> edgeColors =
vtkSmartPointer<vtkIntArray>::New();
edgeColors->SetNumberOfComponents(1);
edgeColors->SetName("Color");
vtkSmartPointer<vtkLookupTable> lookupTable =
vtkSmartPointer<vtkLookupTable>::New();
lookupTable->SetNumberOfTableValues(1);
lookupTable->SetTableValue(0, 1.0, 0.0, 0.0); // red
lookupTable->Build();
edgeColors->InsertNextValue(0);
// Add the color array to the graph
g->GetEdgeData()->AddArray(edgeColors);
vtkSmartPointer<vtkGraphLayoutView> view =
vtkSmartPointer<vtkGraphLayoutView>::New();
view->SetEdgeColorArrayName("Color");
view->ColorEdgesOn();
vtkSmartPointer<vtkViewTheme> theme =
vtkSmartPointer<vtkViewTheme>::New();
theme->SetCellLookupTable(lookupTable);
view->ApplyViewTheme(theme);
view->AddRepresentationFromInput(g);
view->SetLayoutStrategy("Simple 2D");
view->GetRepresentation()->GetAnnotationLink()->AddObserver("AnnotationChangedEvent", selectionCallback);
view->ResetCamera();
view->Render();
view->GetInteractor()->Start();
return EXIT_SUCCESS;
}
For the mouse click function I have used the below code;
vtkAnnotationLink* annotationLink =
static_cast<vtkAnnotationLink*>(caller);
vtkSelection* selection = annotationLink->GetCurrentSelection();
vtkSelectionNode* edges;
if(selection->GetNode(0)->GetFieldType() == vtkSelectionNode::EDGE)
{
edges = selection->GetNode(0);
}
if(selection->GetNode(1)->GetFieldType() == vtkSelectionNode::EDGE)
{
edges = selection->GetNode(1);
}
vtkIdTypeArray* edgeList = vtkIdTypeArray::SafeDownCast(edges->GetSelectionList());
for(vtkIdType i = 0; i < edgeList->GetNumberOfTuples(); i++)
{
//Change colour of the edge
}
My problem is that I cannot change the colour of the edge dynamically. I would be very grateful for any help regarding the matter.
The below code worked for me. First when I create the graph I set the colour of each and every edge,
edgeColors = vtkSmartPointer<vtkIntArray>::New();
edgeColors->SetNumberOfComponents(1);
edgeColors->SetName("Color");
vtkSmartPointer<vtkLookupTable> lookupTable =
vtkSmartPointer<vtkLookupTable>::New();
lookupTable->SetNumberOfTableValues(2);
lookupTable->SetTableValue(0, 0.5, 1.0, 0.5); // green
lookupTable->SetTableValue(1, 0.0, 1.0, 0.0); // white
lookupTable->Build();
//For each edge id insert colour
for(int i = 0;i<=graph->GetNumberOfEdges();i++)
edgeColors->InsertValue(i,0);
// Add the color array to the graph
graph->GetEdgeData()->AddArray(edgeColors);
Then in my mouse click function I get the vtkIdType of the clicked edge and set the colour of it.
vtkIdType edge = edgeList->GetValue(0);
edgeColors->InsertValue(edge.Id,1);//set colour of edge
graphLayoutView->GetInteractor()->Render();
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;
}
So I want to join all interrelated poligons in a multi_polygon. How to do such thing?
We have such image (of one green multi_polygon) which we want to optimize (we can see yellow doted lines - result of simplification that apparently was performed on each poligon of multi_polygon not on multi_polygon in general):
And here is compilable code to generate such image:
#include <iostream>
#include <fstream>
#include <boost/assign.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <boost/geometry/extensions/io/svg/svg_mapper.hpp>
template <typename Geometry1, typename Geometry2>
void create_svg(std::string const& filename, Geometry1 const& a, Geometry2 const& b)
{
typedef typename boost::geometry::point_type<Geometry1>::type point_type;
std::ofstream svg(filename.c_str());
boost::geometry::svg_mapper<point_type> mapper(svg, 400, 400);
mapper.add(a);
mapper.add(b);
mapper.map(a, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2");
mapper.map(b, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
}
boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > make_point(int x, int y)
{
boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > return_item;
boost::geometry::model::d2::point_xy<double> p1(x, y);
boost::geometry::model::d2::point_xy<double> p2(x-1, y);
boost::geometry::model::d2::point_xy<double> p3(x-1, y-1);
boost::geometry::model::d2::point_xy<double> p4(x, y-1);
boost::geometry::append( return_item, p1);
boost::geometry::append( return_item, p2);
boost::geometry::append( return_item, p3);
boost::geometry::append( return_item, p4);
return return_item;
}
int main()
{
// create a container for joined points structure
boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > output, simpl;
// join points one by one (because one day we would have many=))
output.push_back(make_point(1,1));
boost::geometry::correct(output);
output.push_back(make_point(2,1));
boost::geometry::correct(output);
output.push_back(make_point(3,1));
boost::geometry::correct(output);
output.push_back(make_point(4,1));
boost::geometry::correct(output);
output.push_back(make_point(5,1));
boost::geometry::correct(output);
output.push_back(make_point(2,2));
boost::geometry::correct(output);
output.push_back(make_point(3,2));
boost::geometry::correct(output);
output.push_back(make_point(5,2));
boost::geometry::correct(output);
output.push_back(make_point(5,5));
boost::geometry::correct(output);
// simplify joined structure
boost::geometry::simplify(output, simpl, 0.5);
// create an svg image
create_svg("make_envelope.svg", output, simpl );
}
requires at least boost 1.47.0 and 3 files from boost/geometry/extensions/io/svg/
what I need is simple: how to group interrelated poligons? In this case we shall get 2 poligons in our multy_poligon like shown here - red and green:
Update:
So I found this info on dissolve and created sample code that uses rings for cels creation:
#include <iostream>
#include <fstream>
#include <boost/assign.hpp>
//Boost
#include <boost/algorithm/string.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
#include <boost/foreach.hpp>
//Boost Geometry extensions (from trunk)
#include <boost/geometry/extensions/io/svg/svg_mapper.hpp>
template <typename Geometry1, typename Geometry2>
void create_svg(std::string const& filename, Geometry1 const& a, Geometry2 const& b)
{
typedef typename boost::geometry::point_type<Geometry1>::type point_type;
std::ofstream svg(filename.c_str());
boost::geometry::svg_mapper<point_type> mapper(svg, 400, 400);
mapper.add(a);
mapper.add(b);
mapper.map(a, "fill-rule:nonzero;fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2;");
mapper.map(b, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
}
void make_point(int x, int y, boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> > & ring)
{
using namespace boost::assign;
ring +=
boost::geometry::model::d2::point_xy<double>(x-1, y-1),
boost::geometry::model::d2::point_xy<double>(x, y-1),
boost::geometry::model::d2::point_xy<double>(x, y),
boost::geometry::model::d2::point_xy<double>(x-1, y),
boost::geometry::model::d2::point_xy<double>(x-1, y-1);
}
int main()
{
using namespace boost::assign;
boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> > ring0, ring1,ring;
boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > outputw;
make_point(1, 1, ring) ;
make_point(2, 1, ring) ;
make_point(3, 1, ring) ;
make_point(4, 1, ring) ;
make_point(5, 1, ring) ;
make_point(2, 2, ring) ;
make_point(3, 2, ring) ;
make_point(5, 2, ring) ;
boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> > output;
boost::geometry::simplify(ring, output, 1);
// create an svg image
create_svg("make_envelope.svg", ring, output );
}
It returns such image of ring:
If we could use dissolve to turn it into poligon that would really solve some of my problems. But looks like currently we can not due to this compiler errors problem described here
What about using Qt. If you use a QPolygonF you can call unite which does exactly what you need. After unison you can extract the points and put them back into your boost container.
If qt is not an option take a look at the algorithms proposed here http://www.wykobi.com
You can use this function from boost library:
void union_(Geometry1 const & geometry1, Geometry2 const & geometry2, Collection & output_collection)
It takes two geometries and unites them if they have intersections or puts them into one collection (vector, deque, etc.) if not.