Any rules of coruse depends on what strategy/algoritm is being used.
Early on I mentioned that I was considering a collision strategy. Which means something like dropping a pen (horizontally) and let it find a resting position in the “valley”. That would illustrate pretty well the end goal, a straight line at the bottom-most path of the cavity (however, even small peaks or irregularities could push the pen, or cylinder, to the side, and therefore it can be better to include points to a certain depth around such a pen. Alternatively just follow the surface faces which has been the main idea in this thread so far)
But if calculating this "bottom-most path from the Z values of vertices or face center (or whatever intersecting that probing plane), and if traversing the red faces to both sides from the topmost face in the picture, then I would cook up a set of rules to exclude irrelevant mesh faces by,
-
excluding any line segment (mesh intersection polyline segment) located lower than, say, 20º or max 25º below the direction (slope inclination) of the last visited faces (starting from one face, adding up to say 10 of the last visited faces and calculate the a current slope orientation based on those face centers).
-
Any “last face” (as a variable in pseudo code) must not advance until it finds a face above the mentioned constraint angle.
-
But when it advances, it connects a “bridge segment” to that next polyline segment.
-
Continue loop until no further line segments are found.
Such a loop would probably shorten the bottom-most path at both ends of the path, due to the steeper slopes at the ends. Which is fine, because they tend to be part if “sideways” slopes anyway. By exloduing them, the major part of the path will remain in the middle/bottom-most grove, less affected by the side-ways slopes at the ends (which drags down the Z values, although not due to being in the middle of the valley, only by being part of a slope irrelevant to the mid-valley path we are after.
These are the same rules and same line of thought I have repeated in the thread. But I know from my own experience that the significance of these rules (how they affect the final line) can easily be overlooked until actually displaying a FitLine on screen based on the result of any experimental code. Then it is become obvious to the naked eye how, and why, the FitLine tends to drift off to the sides if not carefully avoiding the end slopes.
Bridging gaps between segments (at holes in the mesh) is equally important if traversing the mesh like this, or alternatively, skip sections with holes altogether (see angle rule above) and divide the Z totals with the total length of the fewer accepted top-segments (otherwise the gaps/holes would tend to give a certain path a too low Z total, or whatshallmecallit).
There are people writing PhD thesises about problems like this (like ConvexHull, ConcaveHull, bridging gaps, patching holes, aso).
Any problem can be solved, and I have my own slogan about the fact, which goes like this: “There’s always a solution”. Which I think is true. It’s only a matter about how much time and effort needs to be spent. But as for me I’m a newbie in geometry, so it takes some time before I have become familiar with the best algorithms for the most common problems.
// Rolf