Voronoi Regions - matlab

I'm facing issues when trying to create voronoi regions in MATLAB and handle infinite vertices. I am relatively inexperienced in programming which I feel is hampering my ability to solve this problem.
I want to trace specific locations near the central nodes of the voronoi sub-polygons using the function inpolygon. I can not utilise the function if one of the vertices is Infinite (INF). Therefore, I wanted to create a grid around the voronoi split (As seen in image), and only consider the region within this grid, thereby neglecting the infinite vertices.
What I have done so far:
I already have a certain 100 pairs of Latitudes and Longitudes of nodes, around which I create a voronoi split using voronoi(x_coordinates, y_coordinates) function.
When I have to determine the various coordinates of the sub-polygons within this split, I use the
[V,C] = voronoin([x_coordinates, y_coordinates]);
Till now, this gives me all the of voronoi regions in V and all indexes of vertices for all sub-polygons (voronoi regions) in C.
Then I have implemented a function which uses inpolygon and takes x and y coordinates of the point that has to be traced within one of the voronoi regions. And here is where I am stuck. I am trying to implement a grid all around, but can not find the new intersecting vertices of the grid and the voronoi sub-region to find the new vertices of the voronoi sub-polygons.
Also, if there is any other way to achieve the same task, any help would be highly appreciated.
Many Thanks.

Related

Separating points/clusters with a line

Context: I want to create an interactive heatmap with areas separated by a ZIP code. I've found no way of displaying it directly (i.e. using Google Maps or OSM), so I want to create curves or lines that are separating those areas, and visualize it in maps.
I have a set of points, represented by their coordinates and their according class (ZIP code). I want to get a curve separating them. The problem is that these points are not linearly separable.
I tried to use softmax regression, but that doesn't work well with non-linearly separable classes. The only methods I know which are able to separate non-linearly are nearest neighbors and neural networks. But such classifiers only classify, they don't tell me the borders between classes.
Is there a way to get the borders somehow?
If you have a dense cloud of known points within each Zip code with coordinates [latitude. longitude, zip code], using machine learning to find the boundary enclosing those points sounds like overkill.
You could probably get a good approximation of the boundary by using computational geometry, e.g finding the 2D convex hull of each Zip code's set of points using the Matlab convhull function
K = convhull(X,Y)
The result K would be a vector of points enclosing the input X, Y vector of points, that could be used to draw a polygon.
The only complication would be what coordinate system to work in, you might need to do a bit of work going between (lat, lon) and map (x,y) coordinates. If you do not have the Matlab Mapping Toolbox, you could look at the third party library M_Map M_Map home page, which offers some of the same functionality.
Edit: If the cloud of points for Zip codes has a bounding region that is non convex, you may need a more general computational geometry technique to find a better approximation to the bounding region. Performing a Voronoi tesselation of the region, as suggested in the comments, is one such possibility.

Extract contour from obj 3d object in Matlab

I have an .obj file representing a 3D object.
I need to extract from this 3D object the contour that is obtained by intersection with a plane. So for example, I have an object representing a cylinder oriented with vertical axis, then I want to extract a circle contour when the intersecting plane is horizontal or a rectangular contour when the intersection plane is vertical. Any suggestion about how to do it?
Since I didn't know how to visualise this obj file, I have converted to a patch with the following code (some function taken from loadawobj from Matlab file exchange).
modelname='file.obj';
S=loadawobj(modelname);
mtl=loadawmtl(['obj/' S.mtllib]);
p3=patch('Vertices',S.v','Faces',S.f3');
for ii=1:length(S.umat3)
mtlnum=S.umat3(ii);
fvcd3(ii,:)=mtl(1).Kd';
end
p3.FaceVertexCData=fvcd3;
p3.FaceColor='flat';
But I don't necessarily need to extract the contour from the resulting patch if this is too complex to accomplish. If there is an easier procedure starting from the obj file, it's also fine and acceptable. Thank you!
That's the way I solved the problem, after collecting information all around the web. I couldn't find anything ready on line so I had to implement an algorithm on my own. The basic idea is very simple but there are many steps required. I start from two info: one array containing the coordinates of the cloud point and another array containing a bunch of tuples about how the 3 vertex are connected to form a triangle.
First of all you need to find a representation of the plane you want to use for your cutting. That means you just use one point and the normal to the plane to represent it. That plane is required in order to identify the cutting point on the structure.
Second step is to identify the triangles on the plane. In few words you just need to scroll over all the triangles of the structure and find those having one corner above the cutting plane and another corner below the cutting plane. Also don't forget to account for the condition where one corner is on the plane or two are on the plane. All the other triangles are not needed, since they are totally above or below the cutting plane.
Now you have a subset of all your triangles. You need to extract points of the contour. So for each triangle you have 3 vertex: in general case you can imagine that one vertex is above the plane and the other two are below. Then you have two lines cutting the plane. You can extract two point by simply intersecting these lines with the cutting plane.
By repeating this operation you get a series of points on 2D space. But they have no order and if you plot them as a continuous plot, you get lines jumping up and down since the points you have extracted are randomly located in the array. So, it's required to order them in a proper way. The method I used is the very simple: start from one point and connect to the closest one. There are some bad situations where that doesn't work but you can probably avoid it by adding some more rules on the algorithm.

Smooth circular data - Matlab

I am currently doing some image segmentation on a bone qCT picture, see for instance images below.
I am trying to find the different borders in the picture for instance the outer border separating the bone to the noisy background. In this analysis I am getting a list of points (vec(1,:) containing x values and vex(2,:) containing the y values) in random order.
To get them into order I am using using a block of code which effectively takes the first point vec(1,1),vec(1,2) and then finds the closest point among the rest of the points in the vector. And then repeats.
Now my problem is that I want to smooth the data but how do I do that as the points lie in a circular formation? (I do have the Curve Fitting Toolbox)
Not exactly a smoothing procedure, but a way to simplify your data would be to compute the boundary of the convex hull of the data.
K = convhull(O(1,:), O(2,:));
plot(O(1,K), O(2,K));
You could also consider using alpha shapes if you want more control.

voronoi diagram matlab

I was wondering how I show/plot a voronoi diagram in the below FCM method? Also is there a method where you can watch the programme from the figure as it places and computes each point in matlab? Almost like a running trailer.
[centers, U, objFun] = fcm(data, 6);
plot(data(:,1), data(:,2),'o');
maxU = max(U);
index1 = find(U(1, :) == maxU);
index2 = find(U(2, :) == maxU);
line(data(index1,1),data(index1, 2),'linestyle','none',...
'marker','*','color','g');
line(data(index2,1),data(index2, 2),'linestyle','none',...
'marker', '*','color','r');
This should be the same for k-means and FCM, btw.
To get the Voronoi diagram, you need to compute the Delaunay triangulation, then place a side of the Voronoi diagram orthogonal on the mean of each Delaunay edge.
There are efficient algorithms for Delaunay in at least 2D and 3D. This is quite closely related to computing the convex hull. Plus, as you don't have many cluster centers, the scalability is not that hard.
However, you have one big problem: your data is 6 dimensional. This means that the sides of your Voronoi cells are in fact 5-dimensional, and they will not trivially map to a reasonable 2d projection.
Computing the Voronoi diagram in the 2D projection that you are using however will be inaccurate. You could try to compute the Voronoi cells in 6D, and map all the corners of the voronoi cells into 2D, then connect neighboring corners. But that may yield a big mess of lines, and is not particularly helpful IMHO.
Sorry, as far as I know, Voronoi cell visualization is mostly useful for understanding k-means in 2D and if you have a good 3D visualization engine in 3D.
Don't get me wrong: Voronoi cells is exactly what k-means cluster look like. They're not spheres or blobs or stars. They are Voronoi cells: the cell exactly is the area that would be assigned to a particular mean.
Have a look at this image from Wikipedia:
The black lines are the borders (which in a 2D data set are simple 1d lines) that separate the clusters. In the top center there is a blue object just right of the line. It is blue, because it is on the right of the line - it is in the Voronoi cell of the blue mean.
This is a key drawback of k-means: it does not have the notion of size as in spatial extend for a cluster. They only have a center, and the data is split on the orthogonal hyperplane inbetween of two neighboring centers. For this particular data set, k-means *does not have a chance to split the data correctly! It hasn't converged to a "bad" local minimum, but the correct solution cannot be found by k-means, because the clusters have different size (and there is not enough gap inbetween for k-means to be lucky). To properly cluster this data set, you actually need an EM-like notion of cluster size or a density based method. If k-means were able to detect that the green clusters is about twice as big as the blue ones, it would probably work much better (but then it almost were EM already anyway)

How to represent a polygon with hole(s)?

It's usually popular to work with polygons with their vertices sorted CW or CCW in vectors(2*1 or 1*2 matrices). However, how to state polygons with holes in vectors?
I'm going to apply various process on these polygons, so I want a way of representing with which I could work easily or efficiently.(i.e how to state that kind of polygons in my program in order to ease my algorithms?)
polygons are 2D and I'm programming in MATLAB.
EDIT 1 : I'm going to calculate visibility graph of these polygons(with or without holes).
As others have mentioned, a polygon with holes can be represented as an exterior boundary, plus zero or more interior boundaries, all of which are mutually nonoverlapping*. If you use nonzero winding number to determine inside/outside, be sure to specify your interior boundaries in the opposite direction as the exterior boundaries (counterclockwise for exterior and clockwise for interior, or vice-versa) so that the contour integrals are zero inside the holes.
FYI, tis kind of definition/representation has been formalized in the OpenGIS Simple Features Specification (PDF).
As far as representation:
I'd probably have a cell array of K Nx2 matrices, where the first element in the cell array is the exterior boundary, and the remaining elements (if any) in the cell array are the interior boundaries. I would use a cell array because there may not be the same number of points on each boundary.
*nonoverlapping = except at individual points, e.g. a diamond inside a square:
You can break a polygon with a hole in it into two shapes without a hole. When you're doing contour integration in a complex plane, you can create a "cut" from one edge of the polygon that brings you to the edge of the hole; integrate around one side of the hole and back; then traverse around the other side for the second polygon. You end up with two path integrals along each cut that cancel each other out.
"visibility graph" - is this for a radiation view factor calculation with shading? Or a ray-tracing graphics algorithm?
A polygon, plus a list of polygonal holes. Just be sure the various polygons don't intersect.
What do you plan to do with this thing?
It sounds like each hole is just a polygon inside the polygon itself. Perhaps you could store a vector like you describe for the outer polygon, then a vector of more polygon vectors for the holes.
Presumably you'll want to have a tree structure if you want this to be as generic as possible (i.e. polygons with polygonal holes that have polygons inside them with holes inside that, ...). Matlab isn't really great at representing tree structures efficiently, but here's one idea...
Have a struct-array of polygons.
Each polygon is a struct with two fields, 'corners', and 'children'.
The 'corners' field contains a matrix of (x,y) coordinates of the corners, accessed as "data{polyIdx}.corners(:,cornerIdx)".
The 'children' field is a struct-array of polygons.
Here's an example of some code to make a triangle with bogus children that are holes (they aren't really valid though because they will likely overlap:
polygon = struct;
npoints = 3;
polygon.corners = rand(2,npoints);
polygon.children = struct;
nchildren = 5;
for c=1:nchildren
polygon.children(c).corners = rand(2,npoints);
polygon.children(c).children = struct;
end
You could continue to recursively define children that alternate between creating holes and filling them.
What exactly do you mean under "a visibility graph" ?
Two "full" poligons, two states possible, either +1 or -1.
If you're representing a hole, you've got one with state +1 and one with state -1, which represents a hole, resulting in state 0.
If you've got overlapping polygons, you'll end up with resultant state >1. Then you can calculate the borders of a new polygon.
If you've got two polygons with holes that intersect, then first calculate the state of a new polygon which consists of outer borders of the two old ones, then deal with holes.
Anyways, ... I think you get the general principle.
Have no idea how to do it in matlab, I used it only marginally so far, and even that for very simple things.