alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
546 stars 56 forks source link

face: cache perimeter for better performance #96

Closed schelmo closed 2 years ago

schelmo commented 2 years ago

this is an optimization for pointAtLength (#95) caching the perimeter increases the performance very much. i tried a big polygon with many edges and trying to get 3000 points from it with the cache, it went down from 1600ms to 12ms ;)

the cached perimeter is cleared if one arc length is recomputed

alexbol99 commented 2 years ago

Sorry, @schelmo , I don't like an idea to cache property value inside object. I know I already did it for orientation property, and I don't like this too. I will better change perimeter calculation, because this.edges and edge.length are slow methods. Actually we don't need any iteration at all, because

perimeter = face.last.arc_length + face.last.length

I think performance after this fix will be acceptable.

By the way, what is the purpose of sampling point along face boundary? (Just curiosity)

Best, Alex

schelmo commented 2 years ago

Hi @alexbol99, your method works too, thanks for that!

i use the pointAtLength for generating 3d geometries from polygons