Hausdorff Distance between Points of two meshes - algorithm

I have to implement the Hausdorff distance for 2 meshes. The meshes are different segmentation results of an human organ and I have to compare them, one mesh is a gold seg. and the second one the result of a segmentation algorithm.
I shall use the Hausdorff distance, but have some problems understanding what exactly I have to do.
I understood that I have to calculate the nearest point for each point in meshA in meshB and vice versa. These are my relative distances. For 2 corresponding points in the sets I take the maximal relative distance => hausdorff. (thats how far I am)
Now my problem:
One mesh has ~100,000 points the other one ~2,000. Hence, it will be n:1 relationships. Which points do I take for calculating Hausdorff, how do I tackle that?
Would appreciate any hint. Thx!

If is a finite or countable union, then
If and are metric spaces, then the Hausdorff dimension of their product satisfies
upd: Brute force algorithm :
1. h = 0
2. for every point ai of A,
2.1 shortest = Inf ;
2.2 for every point bj of B
dij = d (ai , bj )
if dij < shortest then
shortest = dij
2.3 if shortest > h then
h = shortest

Hausdorff distance between two objects is the longest distance from a point of one of the objects to the closest point on the other object.
For triangular meshes there are two possible scenarios, where such longest distance can be found:
One point is in a vertex of one mesh, and the closest point is on triangle of the other mesh.
Both points are situated inside edges of the corresponding meshes. This scenario is frequently ignored for performance reasons.
But even brute force approach with considering only vertex-triangle distances is prohibitively slow for the meshes like in the question. And a typical solution is to construct a bounding volume hierarchy BVH (e.g. AABB-tree) for each mesh, and then to use BVH for acceleration of closest point location.
One of the open-source implementations of this algorithm with the usage of AABB-trees can be found in MeshLib, see the function findMaxDistanceSq, which actually returns the square of Hausdorff distance (yet another small optimization).

Related

Algorithm to find best fitting point on a plane

I am working on a path finding system for my game that uses A* and i need to position the nodes in such a way that they would be within minimal distance from other points.
I wonder if there is an algorithm that would allow me to find best fitting point on a plane or a line (between neighboring points) as close as possible to the specified position, while maintaining minimal distance between the neighbors.
Basically i need an algorithm that given input (in pseudocode) min distance = 2, original position = 1, 1 and a set of existing points would do this:
In the example the shape is a triangle and the point can be calculated using Pythagoras theorem, but i need it to work for any shape.
Your problem seems uneasy. If you draw the "forbidden areas", they form a complex geometry made of the union of disks.
The there are two cases:
if the new point belongs to the allowed area, you are done;
otherwise you need to find the nearest allowed point.
It is easy to see if a point is allowed, by computing all distances. But finding the nearest allowed point seems more challenging. (By the way, this point could be very far.)
If the target point lies inside a circle, the nearest candidate location might be the orthogonal projection on a circle, or the intersection between two circles. Compute all these points and check if they are allowed. Then keep the nearest candidate.
In red, the allowed candidates. In black the forbidden candidates.
For N points, this is an O(N³) process. This can probably be reduced by a factor N by means of computational geometry techniques, but at the price of high complexity.

Can one polygon be transformed into another using only parallel translation and proportional scaling?

At the entrance, two polygons are given (the coordinates of the vertices of these polygons are listed in the order of their traversal; however, the traversal order for different polygon angles can be chosen different). Can one polygon be transformed into another using only parallel translation and proportional scaling?
I have following idea
So, find some common peak for two polygons and make the transfer of one polygon so that these vertices lie on one point then Scaling so that the neighboring point matches the corresponding point of another polygon, but I think it's wrong , at least I can't write it in code
Is there some special formula or theorem for this problem?
I would solve it like this.
Find the necessary parallel transport.
Find the necessary scaling.
See if they are the same polygon now.
So to start take the vertex that it farthest to the left, and if there is a tie, the one that is farthest down. Find that for both polygons. Use parallel transport to put that vertex at the origin for both.
Now take the vertex that is farthest to the right, and if there is a tie, the one that is farthest up. Find that for both polygons. If it is not at the same slope, then they are different. If it is, then scale one so that the points match.
Now see if all of the points match. If not, they are different. Otherwise the answer is yes.
Compute the axis-aligned bounding boxes of the two polygons.
If the aspect ratios do not match, the answer is negative. Otherwise the ratio of corresponding sides is your scaling factor. The translation is obtained by linking the top left corners and the transformation equations are
X = s.(x - xtl) + Xtl
Y = s.(y - ytl) + Ytl
where s is the scaling factor and (xtl, ytl), (Xtl, Ytl) are the corners.
Now choose a vertex of the first polygon, predict the coordinates in the other and find the matching vertex. If you can't, the answer is negative. Otherwise, you can compare the remaining vertices*.
*I assume that the polygons do not have overlapping vertices. If they can have arbitrary self-overlaps, I guess that you have to try matching all vertices, with all cyclic permutations.

Plotting Distance Constrained Points on a Plane

I'm crossposting this from the mathematics stack exchange at the suggestion of one user who thought somebody here with experience in embedding algorithms might be able to help, though it should be noted that I'm not trying to do a strict graph embedding (which would not allow for vertices to intersect).
Does anybody know of some algorithmic way to tell if it is possible to plot a set of distance constrained points on a cartesian plane. Or, better still, a method to determine the minimum number of dimensions required to accurately depict the points.
As an example: If you have three points and a constraint that says they are all one unit away from each other, you can plot this easily on a cartesian plane as an equilateral triangle.
However, if you have the constraints A->B = 1, A->C = 1, and B->C = 3 then you will not be able to plot these points while maintaining their distances.
However in my case I have a graph with many more than three vertices. The graph is definitely non-planar: one such case involves 1407 vertices all of which are connected by a weighted bidirectional edge that defines the "distance" between the two vertices.
The question is, is there some way to tell if I can depict this graph with accurate distances on a cartesian plane. I know I can't depict it without edges crossing, but I don't care about doing that. I just want the points on the plane an appropriate distance from each other.
Additional information about the graph in case it helps:
1) Each node represents a set of points. 2) The edge weights are derived by optimally overlaying the point sets from each pair of nodes and then taking the RMSD of the resulting point sets. 3) The sets of points represented by any two nodes can be paired with each other. That is, we can think of each node as a set of 8 points numbered 1-8. This numbering is static. When I overlay node A and node B, the points are numbered identically to when I overlay A and C and B and C.
My thoughts: Because RMSD is a metric on R^3 (At least I believe so. This paper claims to prove it http://onlinelibrary.wiley.com/doi/10.1107/S0108767397010325/abstract), it should be possible for me to do this in R^3 at the very least.
As my real goal here is to turn this set of points into a nice figure, a three dimensional depiction would actually suffice, as I could depict the 3D figure in 2D. I also recognize that numerical instability in the particular optimal overlay algorithm I'm using will cause issues, but I'm interested in the answer for an ideal case.

How do you implement a program to find the shortest path in a 2d plane?

If on a 2d plane there are a no. of obstacles of all possible 2d shapes(circles, quadrilaterals, triangles, irregular shapes...) then how do you implement a mechanism to find the shortest path around the obstacles? I'm considering visual c++, as it provides many graphical classes to draw such figures.
I have come quite far
1) Firstly i'll be using A* search(A-star) to find the path with least cost
2) The path with the least displacement from the straight path will be considered for best path. (not really sure though)
3) The shortest path to get around a figure, for eg from the start, is a line from that point to :
a) the farthest vertex in case of a polygon/quadrilateral
b) a point on the circumference such that the line drawn would be tangential to the circle, in case of a circle or arc
c) (not sure about irregular figures)
Now coming back to the 2) point- least displacement between 2 or more paths can be determined by comparing perpendiculars from those lines to the farthest points of an object on their respective sides. (hope i've made myself understood) .
So then- how do we draw perpendiculars to the straight path?
x1,x2,y1,y2,k and l are known. We just have to find a,b.
Slope of the straight path * slope of it's perpendicular = -1
=> (y2-y1)/(x2-x1) * (b-l)/(1-k) = -1
hence, b = [(x1-x2)/(y2-y1) * (a-k)] + l
I've imagined that by using pythagoras theorem we can find the other equation in terms of the co-ordinates. The lengths of each line can be found by this way:
dx = x1-x2
dy = y1-y2
dist = sqrt(dxdx + dydy)
And then by solving these 2 eqns we can find the correct values of a,b.
I can't think of anything further- any ideas or suggestions?
Is it possible that you use polygonal (i.e. straight line segment) approximations for all shapes?
This would simplify the implementation of the algorithm a lot.
Assuming that this is indeed possible: If you want to use A* then you'll need a graph representation
of the possible paths that you can take. The nodes of this graph are the combination of:
all the vertices of all shapes[1], and
the start and end points
all intersection points between {the straight line segment between the start and end point} and all shapes.
The edges in this graph, then, are between each pair of nodes only if
there exists a straight line between their corresponding two vertices
that doesn't intersect any of the shapes[2].
The length of each edge in the graph is then simply the (euclidean) distance between the two vertices it represents, and the shortest path is always a subset of these edges (I think), which you can find by applying A* to this graph.
[1] - To reduce the number of vertices, you can make all concave shapes convex (unless this causes the start
or end point to lie inside such a shape, then it should be kept concave).
[2] - You can use a variety of data structures to speed up these queries, such as kD or quad trees, or maybe use a sweep line algorithm (such as http://en.wikipedia.org/wiki/Bentley%E2%80%93Ottmann_algorithm) in combination with a doubly-connected edge list.
Well i am not pretty sure if this might help but anyways every irregular object can be split into a combination of regular objects like a circle just that the radius of the curve keeps changing.So you can consider it to be a combination of arcs corresponding to different radii.
For the second point if have the points (l,k). Consider two points located on the line(x1,y1),(x2,y2) which are equidistant from(l,k). So the perpendicular will be the combination of all points which are equidistant from (x1,y1)and (x2,y2).

Algorithm to find point of minimum total distance from locations

I'm building an application based around finding a "convenient meeting point" given a set of locations.
Currently I'm defining "convenient" as "minimising the total travel distance". This is a different problem from finding the centroid as illustrated by the following example (using cartesian coordinates rather than latitude and longitude for convenience):
A is at (0,0)
B is at (0,0)
C is at (0,12)
The location of minimum total travel for these points is at (0,0) with total travel distance of 12; the centroid is at (0,4) with total travel distance of 16 (4 + 4 + 8).
If the location were confined to being at one of the points, the problem appears to become simpler, but this isn't a constraint I intend to have (unlike, for example, this otherwise similar question).
What I can't seem to do is come up with any sort of algorithm to solve this - suggestions welcomed please!
Here is a solution that finds the geographical midpoint and then iteratively explores nearby positions to adjust that towards the minimum total distance point.
http://www.geomidpoint.com/calculation.html
This question is also quite similar to
Minimum Sum of All Travel Times
Here is a wikipedia article on the general problem you're trying to solve:
http://en.wikipedia.org/wiki/Geometric_median
In a way what you appear to be looking for is the center of mass of a triangle with equal weights at the vertices. That would point to barycentric coordinates.
When going beyond a triangle there are solutions for generalized barycentric coordinates and you could give priorities to persons by modifying the weight of the vertices. What that still would not account for is distances on a real map (can't just travel straight in any direction) but it may be a start?
One option is to define an objective (and gradient) function and use a generic optimization library, such as scipy.optimize. fmin_cg would be a good algorithm to try for your problem. Your objective would be the sum of distances as defined in the "Definition" section of the Geometric median Wikipedia page referenced by hatchet. The argument to your objective function is y.

Resources