
Comments and Discussions




Hi!
For (partly) infinite edges I provided two properties do make drawing easier: FixedPoint and DirectionVector. FixedPoint is either the (one) Vertex that exists or the middle between the two data points. DirectionVector is orthogonal to the connecting line between the data points (this is a property of all voronoi edges).
The secret to getting the direction of an infinite edge is in the voronoi algorithm itself  it provides edges with 'left' and 'right' data points. This gives a direct way to calculating the direction from the vertex. It goes in the direction where LeftData is left and RightData is right. Simple
Cheers,
Ben





Do you mean that the Voronoi edge ray must start at VVertexA and must have the direction of the line made by the points LeftData and RightData (i.e. from LeftData to RightData comes first). If yes, then your algorithm is wrong. Just test it on a rectangle and you will see the result. If I misunderstood you, please explain how to get the rays in more details.
VladovsoftSoftware products for fitness and health club management, storehouses, shops and barcode generation.





They start in VVertexA and go orthogonal to the line from LeftData to RightData, so that LeftData is on the left and RightData is on the right.





Ok, thanks. But what about the vertices. I made a simple polygon generator to test the algorithm and I saw that the location of the vertex for 3 points (i.e. a triangle) is sometimes out of the triangle. But it should be the center of the inscribed circle, shouldn't it ?
VladovsoftSoftware products for fitness and health club management, storehouses, shops and barcode generation.





No, Voronoi Vertex of a triangle is the center of it's outer circle, which is only inside the triangle for max(angle)<90 degree. (see wiki[^])
B.





Ok, thanks for the info. I was planning to use Voronoi diagrams for finding the Maximum Inscribed Circle in a simple polygon, but I guess I should read some more information about its properties to determine if it will work for me.
VladovsoftSoftware products for fitness and health club management, storehouses, shops and barcode generation.





According to this, if I take RightDataLeftData, I get the vector from left to right. Then swapping X and Y and negating one gets an orthogonal vector. Taking the nonunknown point and adding the orthogonal vector should produce the correct ray, with LeftData on the left and RightData on the right. However in some cases it does not, and the ray is cast in the wrong direction, as if LeftData and RightData are in the wrong order.
Do you have a correction for me, or updated version of this code?





Hi,
sorry, I dont have any free time at the moment... can you verify whether the left/right data points are actually wrong?
B.





I solved this the same way many other implementations do  don't try to draw the unbounded edges, just contain the points you want to draw within a much larger triangle or square.
With this, I don't have any issues.





Hello BenDi,
I tried to fix the ray problem many people seem to have. I verified the left/right data points and they really seem to be wrong. If I draw a green/blue line from RightData to LeftData (where the left part of the line is green and the right part is blue) you can see in the picture below, that the right/left side is sometimes wrong.
https://www.dropbox.com/s/1o4zl2izmkuv96d/voronoi_ray_problem.png[^]
Do you have any idea where I could fix that problem? How do you decide which one is the left and which one the right point?
It would be kind if you could help me with this problem, or at least give me a hint. Drawing the rays correctly is really necessary for me and I can't skip them.
Peh





Hi! As you can see from the image, the left data point (as seen from the center of the VoronoiEdge) is the one with the lower X coordinate.
Can you make the ray problem more precise? Then maybe I can help more.
Cheers,
B.





Hi,
thanks for your interest in my problem. I understand what you say and you are right for the image I posted. I made a more precise one now.
P are points
M are mids between two points
blue lines point to right data
green lines point to left data
V is the voronoi point (VVertexA)
Rays are drawn by hand so far. Starting at V.
https://www.dropbox.com/s/fqxkg9im5e5bnmk/voronoi_ray_problem2.png[^]
My Problem is how to get the correct direction of the ray vector.





Ah, I remember.
The right solution was posted a long time by ckaut:
On an unbounded edge, only one point, let's call is 'center', is set. Comput a vector
diff = (left_data+right_data)/2  center
and paint a line from center to center + N * diff (where N is large enough to go out of your viewing area.
For this it does not matter if left and right are correctly assigned.
BTW: I haven't looked at the code in a while, but it seems like I added
VoronoiEdge.FixedPoint (=center) and VoronoiEdge.DirectionVector (=diff, normalized)
for exactly this purpose Sorry for not documenting it better.





Yes this looks like a very easy solution and this was what I already tried.
But it's only the solution for the half of this problem.
If the center (V) is within the triangle of the three Points (P) then this way is right (correct for this problem[^]).
But if the center (V) is outside the triangle then it fails too (still fails in this problem[^]). Ray13 should be the other direction here.





Uh, that's true. Appearantly I did not see this case during debugging.
This will probably need a bit more smarts in the Algorithm itself to correctly assign left and right data points, but I might not have time for that in the near future.
Feel free to give it a shot: in FortuneVoronoi.cs, ProcessDataEvent, left/right data is assigned, but after that there are some special cases for how to insert the new tree nodes. My guess is that left/right data points should be assigned based on the same if/else distinguation. Give it a try!





This was a nasty trap ;(
I saw this example (Visualization of the 2D Voronoi Diagram and the Delaunay Triangulation[^]) of an implementation of your algorithm. I downloaded both (yours and the example) and picked the wrong one. What I didn't see is that this example is using an old and buggy version of your code.
With the code from this article here I finally got the correct rays by drawing a line using the DirectionVector and the FixedPoint. I better only post a class below how I did it. If anyone is interested in my full example code you can email me, because I don't want anyone to run into the same problems again by republishing another version of this code.
Thanks a lot for your help.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BenTools.Mathematics;
namespace FortuneVoronoiVisualisation
{
class Visualisation
{
public static Bitmap GetVoronoiMap(int width, int height, HashSet<Vector> datapoints)
{
Bitmap bmp = new Bitmap(width, height);
VoronoiGraph graph = Fortune.ComputeVoronoiGraph(datapoints);
Graphics g = Graphics.FromImage(bmp);
for (int w = 0; w <= width; w = w + 20)
g.DrawLine(Pens.AntiqueWhite, w, 0, w, height);
for (int h = 0; h <= height; h = h + 20)
g.DrawLine(Pens.AntiqueWhite, 0, h, width, h);
foreach (Vector v in datapoints)
{
g.FillEllipse(Brushes.Red, (int)v[0]  2, (int)v[1]  2, 4, 4);
g.DrawEllipse(Pens.Red, (int)v[0]  2, (int)v[1]  2, 4, 4);
}
foreach (Vector v in graph.Vertizes)
g.DrawEllipse(Pens.Indigo, (int)v[0]  2, (int)v[1]  2, 4, 4);
foreach (VoronoiEdge edge in graph.Edges)
{
try
{
if (!edge.IsPartlyInfinite)
{
g.DrawLine(Pens.Gray, (int)edge.VVertexA[0], (int)edge.VVertexA[1],
(int)edge.VVertexB[0], (int)edge.VVertexB[1]);
}
else
{
if (!edge.IsInfinite)
{
Vector VVertexB = edge.FixedPoint + edge.DirectionVector*edge.VVertexA.SquaredLength;
g.DrawLine(Pens.Blue, (int) edge.VVertexA[0], (int) edge.VVertexA[1],
(int) VVertexB[0], (int) VVertexB[1]);
}
else
{
Vector VVertexA = edge.FixedPoint 
edge.DirectionVector*Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2));
Vector VVertexB = edge.FixedPoint +
edge.DirectionVector*Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2));
g.DrawLine(Pens.Blue, (int)VVertexA[0], (int)VVertexA[1],
(int)VVertexB[0], (int)VVertexB[1]);
}
}
}
catch (Exception e)
{
}
}
return bmp;
}
public static Bitmap GetDelaunayTriangulation(int width, int height, HashSet<Vector> datapoints)
{
Bitmap bmp = new Bitmap(width, height);
VoronoiGraph graph = Fortune.ComputeVoronoiGraph(datapoints);
Graphics g = Graphics.FromImage(bmp);
foreach (Vector v in datapoints)
{
g.FillEllipse(Brushes.Red, (int)v[0]  2, (int)v[1]  2, 4, 4);
g.DrawEllipse(Pens.Red, (int)v[0]  2, (int)v[1]  2, 4, 4);
foreach (VoronoiEdge edge in graph.Edges)
if (edge.LeftData == v)
g.DrawLine(Pens.Black, (int)edge.LeftData[0], (int)edge.LeftData[1], (int)edge.RightData[0], (int)edge.RightData[1]);
}
return bmp;
}
}
}





Just a note to say I get the same thing unfortunately.





In most cases you have 2 infinite edges per area. Just find the crossing point of them and choose direction from this point to the VVertixA (or the midpoint between Left and Right points) of the edge.





Hi,
Can somebody provides sample code,how to call this function. I am trying following code & getting error as invalid cast
Vector V1 = new Vector(2,2);
Fourtune.ComputeVoronoiGraph(V1);
Quick help would be appreciated.
Thanks
Sunil Terkar





You need to put your values into a List.. like
List v=new List();
v.add(myVector);
//more points added
feed v into ComputeVoronoiGraph
Cheers
Wayne





Hi great code!
I am trying to change your algorithm to fit the needs of sharpmap.geometries.point.
Sharpamap is a GIS .net .
http://sharpmap.codeplex.com/[^]
As you can imagine I am having some difficulties.
any ideas on the steps i should take ?





What problems do you have? Contact me by direct message if you want
B.





i've a problem of virsion with the code diagrm voronoi
i've the virsion 2005 please help me!





MMMM, very good code! Thank's. I want make class for visualization 2D Voronoi diagram and building Delaunay triangulation.





Thanks, for the code. Now I'm stuck, I'm trying to make polygons as the input instead of data points for Voronoi diagram. Please help!





Hi,
as far as I know, the Fortune algorithm is fixed to data points. Dealing with polygon has some nasty possible sidecases (what if they overlap?).
I would suggest this solution:
 Get all points of all polygons
 Compute the voronoigraph on all points
 'merge' regions where two neighboring points belong to the same polygon (i.e. remove voronoi edge > this may also lead to the removal of some voronoi vertices)
if the polygons are sufficiently far from one another, this should give good results. The solution is not perfect because distance to the polygon is interpreted as distance to one of its edges (which is incorrect, distance should also be measured to the edges of the polygons). You can increase precision by artificially creating polygon vertices _on_ the polygon edges, though this will increase processing time, of course.
Just give a try ,
Ben.





Afternoon
First, great piece of code  have always been interest in Thessien Polygons.
Implementing your code to determine regions of influence use lat/longs as well as using GDI to show the image plot via HTML. Had to add some extra hashing, as well as Convex Hulling to determine boundries to build out necessary polygons  worked perfect.
I have only found one issue, one of lines/vector seem to be computing values that cause the line to plot in the wrong direction. This only occurs if the point it the lowest.
Any ideas ... If you there is way you can get me your email address I will gladly to send you the code or at least send you the out graphic.
Thanks in advance
Ken
Ken





Yes I am going to answer my own question ...
I found out the issue was my code relating to image area boundry in relationship to the formula to compute the end point of an Infinity point. Forgot to adjust the slope of line equation to account for variable image size.
Hence you do not need to answer this posting, but yet again, can you post a complete API document/sample code for the DLL. Notice you have some stat functions (e.g. DAStdv). I was able to figure out a few, but would like to see how the other function.
Thanks
Ken
Ken





Trying to figure out how your implementation of this algorithm works, I instead figured out that this code is a mess!
Implementation is quite poor, code is not commented at all!
That Vector class? Very slow and not usable at all. Makes the code more unreadable with all that v[0], v[1] to say v.X and v.Y.
I prefer the use of Vector2, Vector3, Vector4 STRUCTS with specialized methods for linear algebra things applied in 2D and 3D.
VectorX class should be IMMUTABLE, so, it makes no sense to use a class: with Vectors, Points, Size and whatever you should use always structs when you can.
Use of Vector2, Vector3, Vector4 structs is safer and a lot faster!! See XNA for example.
You can even pass these vectors by reference in private methods to increase performance if you don't want to copy all these.
Another important thing: arrays of structs are always lot faster than arrays of objects!
Another very bad thing is to use floats or doubles as key for hash tables!
double.GetHashcode = unreliable code, also if you use rounding!
You should use some kind of AVL or Red Black tree with a fuzzy comparator (a comparator with tolerance).
For example, you can use SortedDictionary<> that is internally implemented with a Red Black tree (if you use .NET 2.0 or later).
This is an implementation for .NET 2.0 of the fuzzy comparer.
Of course, you can write it too for .NET 1.0 and for your datastructures like VoronoiEdge, or Vector2.
public sealed class FuzzyComparer : IComparer<double>, IComparer
{
public static readonly FuzzyComparer Default;
private readonly double tolerance;
public double Tolerance { get { return this.tolerance; } }
public int Compare(double a, double b)
{
double diff = a  b;
if (diff > this.tolerance) return 1;
if (diff < this.tolerance) return 1;
return 0;
}
int IComparer.Compare(object a, object b)
{
return Compare(a is double ? (double)a : 0, b is double ? (double)b : 0);
}
public FuzzyComparer()
: this(0.00000001)
{
}
public FuzzyComparer(double tolerance)
{
this.tolerance = tolerance;
}
static FuzzyComparer()
{
Default = new FuzzyComparer();
}
}
Of course, hashtables are O(1) while trees are O(log(n)), but really, this doesn't change the complexity of the overall algorithm, of course.
Instead, you can optimize a lot of other portions of that code using safer and faster data structures.
Sorry if I was rude, I don't want to offend anyone of course.
I want to thank you sharing your work to the code project community too.
Thanks.
I don't fear computers.
I fear the lack of them.
(Isaac Asimov)
modified on Monday, June 9, 2008 2:35 PM





One more question:
How could I easily extract centers of each face, made by VoronoiGraph.
Because the graph only returns vertices and edges, but no information about faces...
thanks in advance





Hej, can u help me with this code; FortuneVoronoi...
How do I get values of calculated vertices and edges, once the graph is calculated? I have never worked with hashSets before and I can't figure it out by myself, but I would appreciate if anyone could tell me.
The VoronoiGraph class only returns hashSet, so how do I get values?
Thanks in advance.





Never mind, I figured it out.
VoroniGraph.Vertices is a list of Vectors, which are readable...
thanks anyway for the code





Please show a sample with this lib,Thanks





First of all many thanks for providing this implementation of Fortune's algorithm in c#. The code seems to be working fine on randomly created point sets. However I have tested the code on several point sets of realworld (geographical) data and many times incorrect Voronoi diagrams are the result. I don't know where the error lies but I can send you screenshots+data if you want, to show you the problem.





Mmmh, we had one real problem and several double rounding errors in the past, but I've used the current version for real applications as well (PIONEER robot laser data) and it worked very well.
Anyway, if you're sure it's not a rounding problem (relevant data <1e6), I'll be happy to look at the examples you have and try to fix something.
Ben.





Okay I found the problem and it was my own fault
There were duplicate voronoi sites in the input data set which caused the wrong output.
It seems that the check for identical datapoints in the ParabolicCut routine does not work?





I would like to congratulation to your work.
This was the unique implementation in a civilized language (as you say).
But I don´t understand why you represent the 2D Vector as a double?
Could you make available some examples?
Bye
Cristiano





Thank you very much, Cristiano. I dont really understand the question, though. I represent a 2dvector with two doubles. Single lacks the precision and Decimal is much too slow. There is some downside when it comes to rounding doubles, but I'm confident I got it working. Does that answer your question?
Regards,
Ben.





Hello!
I have a big problem to use the lib.
I type two code like this
Vector[] v = new Vector[] { new Vector(1, 1), new Vector(3, 1)};
VoronoiGraph r = Fortune.ComputeVoronoiGraph(v);
and then I dont know how to use the result.
Can anyone help me
Give me a little sample(ex:how to draw in pictureBox)
Thanks for help.
Have a nice day!!
Li.





Ben,
Under what EULA this software is distrubuted?
Best Regards,
Andrea







Hi all,
I dont know how to easily update the article so just download the new version from here.
modified on Friday, March 7, 2008 9:46 AM





Hi BenDi,
Thanks alot for the fast answer. Now everything is there. So then i will give it a big try.
Andre





New code, but the same old errors.
vectors (1, 1),(2, 3),(5, 1). Result is only 2 edges!





I've had a look at it and I think I got it (at least I could construct no example that didn't work). Please try it and let me know if you find any.
modified on Friday, March 7, 2008 9:46 AM





Congratulations!
I did not find any errors. So far.





First of all: nice work!  (just looked into your code yesterday) !
This means as well I do not yet get all implications (for instance, I would think the data events should be processed in the order highest Ycoordinate to lowest one) but your code seems to work.
This data set did not produce what it should be:
Point(0.4, 0.4)
Point(0.1, 0.4)
Point(0.3, 0.2)
Point(0.05, 0.2)
Point(0.4, 0)
Point(0.1, 0)
Point(0.3, 0.2)
Point(0.05, 0.2)
Point(0.4, 0.4)
Point(0.1, 0.4)
(I got my inspiration here: http://www.diku.dk/hjemmesider/studerende/duff/Fortune/[^])
I have it rendered but seem not to be able to post the results  please try it, you'll see a lot of edges get their second point on (+infinity, +infinity).
Thanks and good luck
Puzzled Dirk





Hi, checked your example, works.
It's perfectly normal that there are edges with VVertexA or VVertexB equal to Fortune.VVInfinity: this means the voronoi edge is unbounded (e.g. if you only have two data points, the graph will have one infinite line between them).
To draw it, the VoronoiEdge offers the FixedPoint and DirectionVector properties:
Vector A = VE.VVertexA;
Vector B = VE.VVertexB;
if (A == Fortune.VVInfinite)
A = VE.FixedPoint  10 * VE.DirectionVector;
if (B == Fortune.VVInfinite)
B = VE.FixedPoint + 10 * VE.DirectionVector;
Simple .
Cheers, Ben.







General News Suggestion Question Bug Answer Joke Rant Admin Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

First Posted  10 Aug 2005 
Views  212,343 
Bookmarked  49 times 

