How to find pair edge while building half-edge datastructure - data-structures

I wrote my half-edge datastructure based on this webpage.
The mesh is loaded from a .obj, contained info for each vertex and what 3 vertices compose a face.
The only problem is: how do I know what's the pair edge for a specific edge?
Right now I record not only end vertex but also start vertex info in edge struct,
compare O(N^2) to find out pair edge.
I think there is a better way but don't know how.

Related

Algorithm to generate a network that fills a 10x10 grid with a source, horizontal lines, right angles, t-junctions and nodes?

Just for fun I'm trying to create a non-flash version of http://www.jurjans.lv/stuff/net/FreeNet.htm. It's all pretty straightforward stuff, but I'm mentally stuck on how to generate the initial network.
I could do it square by square with lots of if/else logic checking neighboring squares, but frankly it seems very laborious and I wonder if there's a much much MUCH smarter way. Generate a mathematical graph or something similar and then translate that into the grid?
I'm not asking for someone to code it all for me - just point me in the right direction!
The completed circuit appears to be a spanning tree.
There is an easy way to generate random minimum spanning trees by:
assigning random weights from some distribution to the edges of an undirected graph, and then constructing the minimum spanning tree of the graph.
So to summarise:
Build a graph with a vertex at the centre of each square
Add edges between each vertex and its neighbours above/down/left/right
Assign random weights (e.g. uniform real from 0 to 1) to each edge
Construct minimum spanning tree e.g. with Prim or Kruskal
Convert graph into tiles
If there are certain shapes you want to forbid (e.g. a fully connected vertex) you may want an extra iteration to increase the weight for edges used in any tiles that are illegal, and then regenerate the spanning tree until you end up with a legal graph.

How to find connected triangles on a 3d mesh

I have a file exported from a 3d application like Blender or Maya. I am able to read the files. I end up with indices describing each triangle and a vertex list.
The file can have more than one connected mesh, two spheres for example. I want to select individual meshes by finding connected triangles. What is the best way to do this? What geometric algorithms can I use? Any examples? Can it be multi threaded?
What you need is a graph solution.
Take all the data, the vertices become nodes in the graph, the edges linking vertices, link the nodes in the graph. Run a DSF/BFS on it marking all the nodes that you visit. All the nodes marked belong to the same object.
Run it again starting from an unmarked nodes to find additional objects.
You can construct a graph in parallel if you think it's easier, but you should be able to do it with the geometrical data as well.
A graph solution indeed, but with triangles as nodes and adjacent edges as connections between nodes. This can then be solved as a max clique problem.

Finding the nearest neighboring edge in a Quadtree

I have stored a set of Points in a Quadtree. Once the quadtree has been created with the points, I then add all the edges to the quadtree such that each edge gets stored in ALL the leaf nodes that it crosses, begins or ends at.
Now, I have a point, say A, and I need to find the closest edge to it. In my current Algorithm, I recurse to the leaf node that contains this Point A and find the distances between A and all line segments contained by this leaf node.
Now this may look like the right solution but it isn't as I have to compare edges that are there in adjacent nodes as well to be able to give an accurate answer.
Now my questions are
a)How do I go about extracting the closest edges?
b)Should I just compare all edges contained in the parent(to the point of interest) node?
(But I know for a fact that putting a hard limit on the number of levels one must go up to find the nearest edge is incorrect based on intuition)
Every node on the quad-tree represents a cube in space (where some sides may be at infinitum) and you can calculate the minimum distance between that cube and the target point A. Note that the distance is 0 for cubes containing A.
Starting from the root node you have to calculate the distance for every of its child cubes (nodes) to A and insert it into a min-heap.
Iteratively, you get the nearest cube at the top of heap and repeat the process. When you reach some leaf node, you just search for the nearest edge to A inside it using brute force.
Once the distance of the cube at top of the heap is greater than the distance of the nearest edge found so far, you can stop the search.
Update: BTW, this is actually the general approach for searching for anything using a quad-tree or a kd-tree or probably most spatial structures.
You can try a voronoi diagram and look for edges inside the voronoi cell only.

Creating a graph and finding strongly connected components in a single pass (not just Tarjan!)

I have a particular problem where each vertex of a directed graph has exactly four outward-pointing paths (which can point back to the same vertex).
At the beginning, I have only the starting vertex and I use DFS to discover/enumerate all the vertices and edges.
I can then use something like Tarjan's algo to break the graph into strongly connected components.
My question is, is there a more efficient way to doing this than discovering the graph and then applying an algorithm. For example, is there a way of combining the two parts to make them more efficient?
To avoid having to "discover" the graph at the outset, the key property that Tarjan's algorithm would need is that, at any point in its execution, it should only depend on the subgraph it has explored so far, and it should only ever extend this explored region by enumerating the neighbours of some already-visited vertex. (If, for example, it required knowing the total number of nodes or edges in the graph at the outset, then you would be sunk.) From looking at the Wikipedia page it seems that the algorithm does indeed have this property, so no, you don't need to perform a separate discovery phase at the start -- you can discover each vertex "lazily" at the lines for each (v, w) in E do (enumerate all neighbours of v just as you currently do in your discovery DFS) and for each v in V do (just pick v to be any vertex you have already discovered as a w in the previous step, but which you haven't yet visited yet with a call to strongconnect(v)).
That said, since your initial DFS discovery phase only takes linear time anyway, I'd be surprised if eliminating it sped things up much. If your graph is so large that it doesn't fit in cache, it could halve the total time, though.

graph - What are the differences between Embedded and Topological in Graph?

In Algorithm Design Manual, page 178 describes some properties of Graph, and one of them is embedded and Topological:
Embedded vs. Topological
A graph is embedded if the vertices and
edges are assigned geometric positions. Thus, any drawing of a graph
is an embedding, which may or may not have algorithmic significance.
Occasionally, the structure of a graph is completely defined by the
geometry of its embedding. For example, if we are given a collection
of points in the plane, and seek the minimum cost tour visiting all of
them (i.e., the traveling salesman problem), the underlying topology
is the complete graph connecting each pair of vertices. The weights
are typically defined by the Euclidean distance between each pair of
points.
Grids of points are another example of topology from geometry.
Many problems on an n × m grid involve walking between neighboring
points, so the edges are implicitly defined from the geometry.
I quite don't understand it:
First of all, what exactly does embedded mean here? As long as the vertices have their own geometric positions, then can I call the graph embedded?
What does any drawing of a graph is an embedding mean? Does it mean what I said in point 1?
What does Topological mean? I don't think it is explained in this description.
The examples in this description really confused me a lot. Could someone please use simplest words to let me understand these two terms for graph?
Is it really important to get these two term understood?
Thanks
I remind you that a graph is just a set of vertices and a set of edges defined on them, so the vertices don't have a geometric position of their own. A drawing of a graph is called an embedding, a drawn graph is called embedded.
It means that any way of drawing a graph is called an embedding of that graph.
A topological graph is a graph whose vertices and edges are points and arcs, respectively.
In addition to msj's answer.
Graph = G(V, E), where V is set of vertex and and E is set pair of vertices from V. This is definition of graph as per Skiena. Note how there is no mention of how this graph visually appear (i.e no mention of its topology).
Example (note that it doesn't define where a, b are located in say X,Y coordinate system)
V = { a, b, c, d, e, f } and E = { (a,b), (b,c), (a,e) }
To 'draw' a graph you assign it geometric positions e.g. in X,Y coordinate systems.
|
| b (2,3)
| a(1,2)
|
|
|____________________________
Fig 1
Fig 1 is simply an embedding where we are drawing vertex pairs specified in E
Difference between embedded and topological graph is how does the "topology" comes to be. In any "embedding" you manually assign geometric locations as explained above, but in topological graph you define a "rule" based on which topology of a graph defines itself. e.g. you specify a G(V,E) and define a rule, say "visit each node exactly once" defines the topology which is called "complete graph".
Skiena uses geographical friendship graph as an example for embedded graph because each vertex is associated with a geographical point in this world where friends live.
Excerpt from the book -
Do my friends live near me? – Social networks are not divorced from geography. Many of your friends are your friends only because they happen to live near you (e.g., neighbors) or used to live near you (e.g., college roommates).
Thus, a full understanding of social networks requires an embedded graph, where each vertex is associated with the point in this world where they live. This geographic information may not be explicitly encoded, but the fact that the graph is inherently embedded in the plane shapes our interpretation of any analysis.

Resources