6

Suppose I have a data set of 3D coordinates that form a pretty continuous path in space. I would like to measure the total arc length (or path length) of the entire system. At this point the coordinates are not connected. Here's what I have so far

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Erika
  • 63
  • 4

1 Answers1

11

The very best method

Bob Hanlon, in a comment below, solved the issue with what I further call an "ingenious method":

Total[RegionMeasure@*Line /@ (list[[#]] & /@ FindCurvePath[list])]

working with list of points forming a nicely behaving (e.g., no loops) curve in 2D or 3D.


Straightforward approach

data = Table[{x, x^2, x^3}, {x, 0, 1, 0.01}];

ListPointPlot3D @ data

enter image description here

The "exact" result in this case is

NIntegrate[Norm @ D[{x, x^2, x^3}, x], {x, 0, 1}]

1.86302

A rough estimate might be the sum of distances between consecutive pairs of points:

Total @ (EuclideanDistance @@@ Partition[data, 2, 1])

1.86301

or in a compact form

RegionMeasure @ Line[data] (* thanks: Szabolcs *)

1.86301

or

ArcLength @ Line @ data (* thanks: JasonB *)

1.86301


Overcoming some caveats

The above assumes that the points are ordered in the "correct" way. Simple Sort won't work when the curve "runs back", and the points are not in the correct order. As an illustration let's take

f[x_] := -1 + 4 x - 4 x^2 + x^3

data2 = Table[{f[x], x^2, x^3}, {x, 0, 3, 0.01}];

ListPointPlot3D @ data2

enter image description here

NIntegrate[Norm @ D[{f[x], x^2, x^3}, x], {x, 0, 3}]

30.0472

and

RegionMeasure @ Line[data2]

30.0472

or

ArcLength @ Line @ data2

30.0472

coincide nicely. Here's what happens when the points are in random order:

data3 = RandomSample @ data2;

RegionMeasure @ Line[data3]

2747.76

and Sort fails miserably:

RegionMeasure @ Line[Sort@data3]

1548.66

I define a set

data4 = data3;

in which I find the "smallest" point:

start = Min /@ Transpose@data4

{-1., 0., 0.}

which for future use I'll make a copy of:

s0 = {start};

and now I seek point after point the point nearest to the preceding:

data5 = s0~Join~Reap[Do[
      data4 = Complement[data4, {start}]; 
      Sow[start = Nearest[data4, start][[1]]];, {i, 300}
      ]
     ][[2, 1]];
(* I guess this could be done in a different way, possibly with NestList, or with some other ingenious method *)

which gives

data5 == data2

True

and of course

RegionMeasure @ Line[data5]

30.0472


Another approach can employ FindShortestTour (thanks: JulienKluge and Rahul):

start = Min /@ Transpose@data3
end = Max /@ Transpose@data3

t1 = Max @ Position[data3, start]
t2 = Max @ Position[data3, end]

First @ FindShortestTour[data3, t1, t2]

30.0472


Wrapping things up in a single function

curveLength[data_] := Block[{data4 = data, start, s0, data5},
  start = Min /@ Transpose@data4;
  s0 = {start};
  data5 = s0~Join~Reap[Do[
       data4 = Complement[data4, {start}]; 
       Sow[start = Nearest[data4, start][[1]]];, {i, 
        Length @ data4 - 1}
       ]
      ][[2, 1]];
  RegionMeasure @ Line[data5]
  ]

curveLength[data]

1.86301


ISSUE

None of the above approaches (except for the FindCurvePath one) will work with curves like this:

enter image description here

because the starting point was found as a point with its coordinates smallest in the data set, which is not the case now. One can of course provide the starting point manually, and then the length of the curve can be still obtained.

corey979
  • 23,947
  • 7
  • 58
  • 101
  • 6
    Lazy man's way: RegionMeasure@Line[data]. – Szabolcs Oct 12 '16 at 21:08
  • 1
    For the last Part: you could have also used FindShortestTour – Julien Kluge Oct 12 '16 at 22:09
  • 1
    @JulienKluge I thought about that, but RegionMeasure@Line@data3[[FindShortestTour[data3][[2]]]] gives 58.6909; one needs first to find the starting point (which is on position 196 in this case) and swap it with the first element in the list, which appears not to be so trivial. If you can come up with an idea I'd love to hear it. – corey979 Oct 12 '16 at 22:21
  • Ohh okay. Thanks for the clarification. – Julien Kluge Oct 12 '16 at 22:22
  • If you know the indices of the starting and ending points you can use FindShortestTour[data, i, j]. If not, FindShortestTour[data] returns a cycle, from which deleting the longest edge should give you the desired path. –  Oct 12 '16 at 22:40
  • @JulienKluge There's more to it: FindShortestTour gives the length of a cycle, i.e. from start to end + from end to start; it doesn't matter which point is first. It gives 58.6909, the distance between start and end is 28.6182; their difference is 30.0727, which is pretty close to the result of 30.0472 (I'm a bitt concerned by the discrepancy though). – corey979 Oct 12 '16 at 22:44
  • @JulienKluge Btw: you can swap any two points at positions a and b in a list with list[[{a, b}]] = list[[{b, a}]]. – corey979 Oct 12 '16 at 22:57
  • 2
    Total[RegionMeasure@*Line /@ (data3[[#]] & /@ FindCurvePath[data3])] – Bob Hanlon Oct 13 '16 at 00:13
  • "working with any list of points forming a curve" - it works in a lot of cases, but certainly not all of them: pts = RandomSample[First[Cases[Normal[ParametricPlot[{Sin[3 t], Sin[8 t]}, {t, 0, 2 Pi}]], Line[l_] :> l, Infinity]]]; – J. M.'s missing motivation Oct 13 '16 at 02:31
  • @BobHanlon That's what I called an "ingenious method" in my post. – corey979 Oct 13 '16 at 08:02