 # Best way to "Measure" a Surface of a model between a set of points

So lets say I have a sphere and on that I want to get the distance in BJS units of the surface of the sphere between any number of points.

What would be the best way to get the measurement value of the curved meshes surface between these points as if I had cut away a section of it and laid it flat then measured it?

Hi Pryme8,

I’m a little rusty on this stuff, but I think shortest path between two points on the surface of a sphere will be a part of the circumference, right? So, if you have the world position of both points `p1`, `p2` and the center of the sphere `c`, the angle between the vectors `(p1 - c)` and `(p2 - c)` should give you a the required “portion” of the circumference. So, if that angle is PI / 6 radians, then the distance along the surface of the sphere between those two points is 1/12th of the circumference.

I think.

Unless I’ve completely misunderstood what you’re trying to do, which not super uncommon for me. Lets say its not a circle though, but an organic mesh. A circle was a super simple mesh and you I think your solution would work for that.

I think I might have a solution I just wanted to see what others might be able to think of.

Hmm, interesting. I haven’t thought about this for long, but if there are no constraints on what the mesh can look like, I think you might have to path-find, perhaps by borrowing some techniques from navigation meshes. Every other trick I’ve thought of so far falls down at one or more pathological cases. It’s definitely not an area of expertise for me, though, so I’m curious if others know of a better approach.

What I am going to try, is have a list of vertices that will be used as the “waypoints” and then iterate through the list, but instead of using the waypoints will prolly use the averages of two of the waypoints as a measuring point and then go around the mesh, or the distance on the mesh that I need to measure.

I have a pretty specific case and the precision can have some variance and error cause its really wont matter to the user. I just need a quick method that can be fired off fairly fast.

This is just a wild unthought through idea so it might have no value at all. Let O be the the center of the bounding sphere of the mesh. Let P and Q be two points on the mesh. Let R be the point on the sphere where the extended line OP meets the sphere and S the same for OQ. Let RS be the shortest path from R to S on the surface of the sphere. For each point T on the path çonstruct OT. For each T calculate the point M where OT meets the mesh. Hopefully all points M would form the shortest path on the mesh from P to Q. Might give it a go next week.

EDIT Sorry prym8 this is nonsense. Imagine three tall buildings in a row in the order, A, B, C a free runner (no jumping) at the top of A wants the shortest route on the surface to the top of C. The method above would take the runner down A, up one side of B, down the other side of B and up the side of C. Necessary in 2D but in 3D go down A, run on the ground around B and up C.

My idea is to convert first the spherical coordinates into cartesian coordinates. the distance is the quit easy to calculate in the cartesian system.

Finding the shortest path between two points on the surface of a sphere is not a problem however they are given. There is a formula. The problem is the mesh prym8 has is not a sphere.

1 Like

Its crazy how easy it is to measure things in real life… and now trying to get accurate measurements of a dynamic 3d mesh is kinda interesting.

Hi @Pryme8 I have been having a play with shortest paths.

Just path Babylon.js Playground
path and distance in console https://www.babylonjs-playground.com/#7YCXAI#1

Not close to an accurate measurement yet! However have made a start at finding the shortest path between two points on the surface of a mesh using Dijkstra’s algorithm

At the moment the PG converts the facets into a graph of vertices and edges and the shortest path is only along facet edges from vertex to vertex. Green sphere marks starting point using a position index, red sphere marks end point and blue spheres mark the vertices visited.

Next is to see if there is a way to refine the results so if it is shorter to go across a facet rather than around its edges this will be used.

1 Like

Great work buddy, I think I might be able to adapt this.

Much respect for taking the time to really think about it.

Just realised distance is wrong as am working with the square of the length of each edge. Will sort out tomorrow, minor change.

Distance corrected as per line 25 https://www.babylonjs-playground.com/#7YCXAI#2

1 Like