-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsimplify_paths_rdp.py
More file actions
72 lines (59 loc) · 2.71 KB
/
simplify_paths_rdp.py
File metadata and controls
72 lines (59 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import rdp as rdp
import numpy as np
import logging
import progressbar
from compas.geometry import Point
from compas.plugins import PluginNotInstalledError
logger = logging.getLogger('logger')
__all__ = ['simplify_paths_rdp',
'simplify_paths_rdp_igl']
def simplify_paths_rdp(slicer, threshold):
"""Simplifies a path using the Ramer–Douglas–Peucker algorithm, implemented in the rdp python library.
https://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
Parameters
----------
slicer: :class:`compas_slicer.slicers.BaseSlicer`
An instance of one of the compas_slicer.slicers classes.
threshold: float
Controls the degree of polyline simplification.
Low threshold removes few points, high threshold removes many points.
"""
logger.info("Paths simplification rdp")
remaining_pts_num = 0
with progressbar.ProgressBar(max_value=len(slicer.layers)) as bar:
for i, layer in enumerate(slicer.layers):
if not layer.is_raft: # no simplification necessary for raft layer
for path in layer.paths:
pts_rdp = rdp.rdp(np.array(path.points), epsilon=threshold)
path.points = [Point(pt[0], pt[1], pt[2]) for pt in pts_rdp]
remaining_pts_num += len(path.points)
bar.update(i)
logger.info('%d Points remaining after rdp simplification' % remaining_pts_num)
def simplify_paths_rdp_igl(slicer, threshold):
"""
https://libigl.github.io/libigl-python-bindings/igl_docs/#ramer_douglas_peucker
Parameters
----------
slicer: :class:`compas_slicer.slicers.BaseSlicer`
An instance of one of the compas_slicer.slicers classes.
threshold: float
Controls the degree of polyline simplification.
Low threshold removes few points, high threshold removes many points.
"""
try:
import igl
logger.info("Paths simplification rdp - igl")
remaining_pts_num = 0
for i, layer in enumerate(slicer.layers):
if not layer.is_raft: # no simplification necessary for raft layer
for path in layer.paths:
pts = np.array([[pt[0], pt[1], pt[2]] for pt in path.points])
S, J, Q = igl.ramer_douglas_peucker(pts, threshold)
path.points = [Point(pt[0], pt[1], pt[2]) for pt in S]
remaining_pts_num += len(path.points)
logger.info('%d Points remaining after rdp simplification' % remaining_pts_num)
except PluginNotInstalledError:
logger.info("Libigl is not installed. Falling back to python rdp function")
simplify_paths_rdp(slicer, threshold)
if __name__ == "__main__":
pass