Comments (5)
I can confirm that it works faster than the pure python version for me as well.
from rdp.
@kkonevets I can't figure out how to convert your code to work on 3D data.. Is it possible to do something similar with 3D data?
from rdp.
Dear all,
is there a way to limit number of simplified points together with max distance (epsilon)?
Or to be more precise, to pick only subset of N the most relevant points for given dmax.
Thanks in advance!
Cheers,
Ivica
from rdp.
import numpy as np
def line_dists2D(points, start, end):
if np.all(start == end):
return np.linalg.norm(points - start, axis=1)
vec = end - start
cross = np.cross(vec, start - points)
return np.divide(abs(cross), np.linalg.norm(vec))
# https://stackoverflow.com/questions/56463412/distance-from-a-point-to-a-line-segment-in-3d-python
def lineseg_dist3D(p: np.ndarray, a: np.ndarray, b: np.ndarray):
# normalized tangent vector
d = np.divide(b - a, np.linalg.norm(b - a))
# signed parallel distance components
s = np.dot(a - p, d)
t = np.dot(p - b, d)
# clamped parallel distance
h = np.maximum.reduce([s, t, np.zeros(len(p))])
# perpendicular distance component
c = np.cross(p - a, d)
res = np.hypot(h, np.linalg.norm(c, axis=1))
res[res < np.finfo(np.float64).eps] = 0
return res
def rdp(M, epsilon=0):
M = np.array(M)
start, end = M[0], M[-1]
if M.shape[1] == 2:
dists = line_dists2D(M, start, end)
elif M.shape[1] == 3:
dists = lineseg_dist3D(M, start, end)
else:
raise ValueError("point dimension must be 2d or 3d")
index = np.argmax(dists)
dmax = dists[index]
if dmax == np.nan:
raise ValueError
if dmax > epsilon:
result1 = rdp(M[: index + 1], epsilon)
result2 = rdp(M[index:], epsilon)
result = np.vstack((result1[:-1], result2))
else:
result = np.array([start, end])
return result
points = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]).reshape(-1, 3)
points = np.array([1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 3]).reshape(-1, 2)
print(rdp(points))
@soichih
I tried to make a sample on 3d Points, i think this is working
edit: fixed close to 0 and linalg norm over rows
from rdp.
from rdp.
Related Issues (12)
- Makes no difference in reducing the number of points HOT 1
- simplify_coords function causes unexpected exit
- Wrong calculation of pldist HOT 3
- Link to blog post in docs gets redirected to blog front page HOT 2
- epsilon is int type HOT 1
- A c++ (speed up) version of Ramer-Douglas-Peucker with python binding of identical interface HOT 1
- Can you put a new release? HOT 1
- Recursion bug HOT 5
- Bizarre np.linalg.norm error HOT 6
- keeping track of the index element output from rdp HOT 3
- limiting number of points HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rdp.