Main menu

Post navigation

Automated Edge Flip Operator

Last week I ran in to a long lasting Houdini frustration, the lack of proper Edge support.
We get the control over various primitive types, points and verts, but not Edges.

This becomes frustrating when dealing with complex primitive sorting and optimization techniques. For that purpose I decided to create my own Edge object, a rather simple wrapper that let’s me create Edges from points, verts and primitives inside Houdini. An edge can be constructed using two points, a primitive or 2 verts. Functionality includes comparing angles, getting the length, comparing other existing edges on point presence, not order etc.
Just for convenience

One of the first things I needed it for was an automatic edge flip operator. The SOP samples primitive data and flips edges based on the optimal angle of neighbouring prims. Similar to the Delauney edge orientation principle. The flipping of an edge is only possible when the union of the two incident faces forms a convex quadrilateral. Flip Constraints are based on the curvature of a reference mesh, optimizing the triangle edge orientation on harsh edges or cutoff points.

To determine if the edge needs to be flipped, both edge lengths are computed, together with the maximum shared angle. If the angle (thus the edge length) is higher for the incident face, the edge can be flipped. Otherwise the angle is compared based on the maximum difference between the point normals and prim to point normals. If the difference comes close to 0, the edge follows curvature. If it is higher then the specified threshold, the edge should be flipped. (conforming the proposed mesh orientation and curvature).

One of the implementation issues: how do you keep track of the mesh when you flip an edge? Flipping an edge makes the next edge (sharing one of the previous edge primitives) most likely invalid, or a possible candidate. You could solve this by recursively update the mesh as you flip an edge, keeping track of the deforming state and marking areas that have been computed. This would be rather slow and tedious to incorporate. I ended up marking the edge that should be flipped, any new edge being sampled is being checked against any shared edge points. If one of the shared edge points match a flipped edge, it isn’t considered. Running the operator a couple of times would asure most edges will align. Most likely ater two computational rounds. I also added a “dirty” mode, not taking into account previously flipped edges. (works surprisingly well)

The actual operator doesn’t flip the edges. Houdini’s Edge Flip Sop does that for me. I simply count the total number of edges to be flipped and save the Edge Points as a Detail String Attribute.
The count is used to specify the number of iterations the loop should make and inside I simply sample each saved edge. It is a limitation I had to work with in Houdini.

A handy tip on speed and flexibility might be to pre-sample your data-set in to clusters. Clusters are formed based on any Vector3 attribute and are weighted over X recursions. Finding the most optimal distribution as it is being computed. Creating clusters (based on Position for example) speeds up the computing of the edges to be flipped, because the scope is limited to only a small island of primitives. In comparison to the entire mesh. Similar to using a Kd-Tree, but simpler. Read up on the algorithm here and here

Now for some code, the edge class should be easy to recreate based on what was told and can be seen in the following snippet:

# if the edge is part of already existing edges, it shared a primitive# i know this is dirty but i'd rather have this then do 2 lookups :(try:
Console("shared edge: %s" % edge)
existing_edge = edge_list[edge_list.index(edge)]
existing_edge.addPrim(inPrim.number())
shared_edges.append(existing_edge)exceptValueError:
Console("new edge: %s" % edge)
edge.addPrim(prim.number())
edge_list.append(edge)

def CompareAngle(inPrimOne, inPrimTwo, inEdge, inUnsharedPoints):"""
Compares the angle of the point normal angle against primitive normal
Returns true if the edge needs to be inverted...
The comparison is based on the maximum difference between the prim point normals,
and the prim point normals compared with the prim normal. If the difference is rather
large on both sides, consider the edge to be flipped

An extra constrain attribute can be specified. Works best with curvature.
If the curvature of the unshared points is higher then the combined edge points,
It is allowed to flip, together with the angle constrain
"""

# get the point normals
prim_one_normals =[]
prim_two_normals =[]for point in prim_one_points:
prim_one_normals.append(hou.Vector3(point.attribValue(angle_attrib)))for point in prim_two_points:
prim_two_normals.append(hou.Vector3(point.attribValue(angle_attrib)))

# for both primitives find the point that isn't shared and figure out if it should be flipped
unshared_points = FindUnsharedPoints(prim_one, prim_two, inEdge)iflen(unshared_points)!=2:("%s has more more or less then 2 unshared points" % inEdge)return -1