JuliaGeometry / GeometryTypes.jl

Geometry types for Julia
Other
67 stars 41 forks source link

Fix `area` for higher dimensions #182

Closed CarpeNecopinum closed 4 years ago

CarpeNecopinum commented 4 years ago

While the signature of area(contour) accepts points of any dimension as polygon to compute the area of, the implementation doesn't generalize to dimensions larger than 2.

In particular, computing the area of a 3d polygon returns a 3d point as "area" (due to the cross-product returning a vector rather than a scalar for 3d):

> area(rand(GeometryTypes.Point3f0, 3))
3-element Point{3,Float32} with indices SOneTo(3):
 -0.15032549
  0.1441128 
 -0.0745391

This PR thus constrains the existing implementation to 2d points and implements a variant of the algorithm that works for (approximately planar) 3d polygons as well (for non-planar polygons the area depends on the triangulation, so there is no general answer).

I also added 2 test cases.

CarpeNecopinum commented 4 years ago

Wondering how I managed to seemingly decrease test coverage by submitting a fully covered method and a test for a previously non-covered method. ¯\(ツ)/¯ Hope that isn't a deal-breaker.

SimonDanisch commented 4 years ago

Looks good to me :) @sjkelly, any comments?!

CarpeNecopinum commented 4 years ago

Just noticed my 3d variant can't do non-convex polygons, so I WIPed this PR until I get around to fixing that and adding a test for it. Okay, done.