00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 import shapely.geometry as sg
00032
00033 import math
00034 import numpy as np
00035
00036
00037 def inverse_2x2(a):
00038 return 1/np.linalg.det(a) * np.matrix([[a[1,1],-a[0,1]],[-a[1,0],a[0,0]]])
00039
00040
00041
00042 def project_point_on_line(q, p1, p2):
00043 v1 = p2-p1
00044 v1 = v1 / np.linalg.norm(v1)
00045 v2 = q - p1
00046 p = p1 + np.dot(v2.A1, v1.A1) * v1
00047 return p
00048
00049
00050
00051 def convex_hull(pts):
00052 mp = sg.MultiPoint(pts)
00053 ch = mp.convex_hull
00054 if ch.__class__ == sg.polygon.Polygon:
00055 hull_pts = np.array(ch.exterior.coords)
00056 else:
00057 hull_pts = np.array(ch.coords)
00058 return hull_pts
00059
00060
00061
00062
00063 def distance_from_curve(pt, pts_list):
00064 spt = sg.Point(pt.A1)
00065 s_pts_list = sg.LineString(pts_list)
00066 return s_pts_list.distance(spt)
00067
00068
00069
00070
00071 def distance_along_curve(pt, pts_list):
00072 spt = sg.Point(pt.A1)
00073 s_pts_list = sg.LineString(pts_list)
00074 return s_pts_list.project(spt)
00075
00076
00077
00078
00079 def project_point_on_curve(pt, pts_list):
00080 spt = sg.Point(pt.A1)
00081 s_pts_list = sg.LineString(pts_list)
00082 d = s_pts_list.project(spt)
00083 spt_new = s_pts_list.interpolate(d)
00084 return np.matrix(spt_new.coords[0]).T
00085
00086
00087
00088
00089
00090
00091 def get_point_along_curve(pts_list, dist, normalized=False):
00092 s_pts_list = sg.LineString(pts_list)
00093 spt_new = s_pts_list.interpolate(dist, normalized)
00094 return np.matrix(spt_new.coords[0]).T
00095
00096 def test_convex_hull():
00097 pp.axis('equal')
00098 pts = np.random.uniform(size=(40,2))
00099 h_pts = convex_hull(pts)
00100 print 'h_pts.shape:', h_pts.shape
00101
00102 all_x = pts[:,0]
00103 all_y = pts[:,1]
00104 x_ch, y_ch = h_pts[:,0], h_pts[:,1]
00105
00106 pp.plot(all_x, all_y, '.b', ms=5)
00107 pp.plot(x_ch, y_ch, '.-g', ms=5)
00108
00109 pp.show()
00110
00111 def test_distance_from_curve():
00112 pt = np.matrix([0.8, 0.5, 0.3]).T
00113 pts_l = [[0, 0.], [1,0.], [1.,1.]]
00114 print 'distance_from_curve:', distance_from_curve(pt, pts_l)
00115 print 'distance_along_curve:', distance_along_curve(pt, pts_l)
00116
00117 pt_proj = project_point_on_curve(pt, pts_l)
00118
00119 pp.axis('equal')
00120 pp.plot([pt[0,0]], [pt[1,0]], 'go', ms=7)
00121 pp.plot([pt_proj[0,0]], [pt_proj[1,0]], 'yo', ms=7)
00122 pp.plot(*zip(*pts_l))
00123 pp.show()
00124
00125
00126
00127
00128 def fit_line_high_slope(x, y):
00129 A = np.column_stack((y, np.ones((y.shape[0],1))))
00130 x,resids,rank,s = np.linalg.linalg.lstsq(A, x)
00131 a = x[0,0]
00132 b = x[1,0]
00133 return a,b,resids/y.shape[0]
00134
00135
00136
00137
00138 def fit_line_low_slope(x, y):
00139 A = np.column_stack((x, np.ones((x.shape[0],1))))
00140 y,resids,rank,s = np.linalg.linalg.lstsq(A, y)
00141 a = y[0,0]
00142 b = y[1,0]
00143 return a,b,resids/x.shape[0]
00144
00145
00146 if __name__ == '__main__':
00147 import matplotlib.pyplot as pp
00148
00149 test_project_point_on_line = True
00150 test_convex_hull_flag = False
00151 test_distance_from_curve_flag = False
00152
00153 if test_project_point_on_line:
00154 p1 = np.matrix([2,3.]).T
00155 p2 = np.matrix([-1.5,6.3]).T
00156 q = np.matrix([1.5,7.3]).T
00157
00158 p = project_point_on_line(q, p1, p2)
00159
00160 x = [p1[0,0], p2[0,0]]
00161 y = [p1[1,0], p2[1,0]]
00162 pp.plot(x, y, 'b', label='line')
00163
00164 x = [q[0,0], p[0,0]]
00165 y = [q[1,0], p[1,0]]
00166 pp.plot(x, y, '-r.', ms=10, label='projection')
00167
00168 pp.axis('equal')
00169 pp.legend()
00170 pp.show()
00171
00172 if test_convex_hull_flag:
00173 test_convex_hull()
00174
00175 if test_distance_from_curve_flag:
00176 test_distance_from_curve()
00177
00178
00179