|
| 1 | +import numpy as np |
| 2 | +from sklearn.neighbors import NearestNeighbors |
| 3 | +from sklearn.preprocessing import normalize |
| 4 | + |
| 5 | + |
| 6 | +def select_nearest_surfaces_points(geo_model, surface_points, searchcrit): |
| 7 | + """ |
| 8 | + Find the neighbour points of the same surface |
| 9 | + by given radius (radius-search) or fix number (knn). |
| 10 | + |
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + geo_model : geo_model |
| 14 | + GemPy-model. |
| 15 | + surface_points: Pandas-dataframe |
| 16 | + Contains the dataframe of the (point-)data from the GemPy-model. |
| 17 | + searchcrit : int or float |
| 18 | + if is int: uses knn-search. |
| 19 | + if is float: uses radius-search. |
| 20 | + """ |
| 21 | + |
| 22 | + # extract surface names |
| 23 | + surfaces = np.unique(surface_points['surface']) |
| 24 | + neighbours = [] |
| 25 | + # for each surface |
| 26 | + if isinstance(searchcrit, int): # in case knn-search |
| 27 | + searchcrit = searchcrit + 1 # because the point itself is also found |
| 28 | + for s in range(surfaces.size): |
| 29 | + # extract point-ids |
| 30 | + i_surfaces = surface_points['surface'] == surfaces[s] |
| 31 | + # extract point coordinates |
| 32 | + p_surfaces = surface_points[i_surfaces][['X', 'Y', 'Z']] |
| 33 | + # create search-tree |
| 34 | + Tree = NearestNeighbors(n_neighbors=searchcrit) |
| 35 | + # add data to tree |
| 36 | + Tree.fit(p_surfaces) |
| 37 | + # find neighbours |
| 38 | + neighbours_surfaces = Tree.kneighbors(p_surfaces, n_neighbors=searchcrit, |
| 39 | + return_distance=False) |
| 40 | + # add neighbours with initial index to total list |
| 41 | + for n in neighbours_surfaces: |
| 42 | + neighbours.append(p_surfaces.index[n]) |
| 43 | + else: # in case radius-search |
| 44 | + for s in range(surfaces.size): |
| 45 | + # extract point-ids |
| 46 | + i_surfaces = surface_points['surface'] == surfaces[s] |
| 47 | + # extract point coordinates |
| 48 | + p_surfaces = surface_points[i_surfaces][['X', 'Y', 'Z']] |
| 49 | + # create search-tree |
| 50 | + Tree = NearestNeighbors(radius=searchcrit) |
| 51 | + # add data to tree |
| 52 | + Tree.fit(p_surfaces) |
| 53 | + # find neighbours (attention: relativ index!) |
| 54 | + neighbours_surfaces = Tree.radius_neighbors(p_surfaces, |
| 55 | + radius=searchcrit, |
| 56 | + return_distance=False) |
| 57 | + # add neighbours with initial index to total list |
| 58 | + for n in neighbours_surfaces: |
| 59 | + neighbours.append(p_surfaces.index[n]) |
| 60 | + return neighbours |
| 61 | + |
| 62 | + |
| 63 | +def set_orientation_from_neighbours(geo_model, neighbours): |
| 64 | + """ |
| 65 | + Calculates the orientation of one point with its neighbour points |
| 66 | + of the same surface. |
| 67 | + Parameters |
| 68 | + ---------- |
| 69 | + geo_model : geo_model |
| 70 | + GemPy-model. |
| 71 | + neighbours : Int64Index |
| 72 | + point-neighbours-id, first id is the point itself. |
| 73 | + """ |
| 74 | + |
| 75 | + |
| 76 | + # compute normal vector for the point |
| 77 | + if neighbours.size > 2: |
| 78 | + # extract point coordinates |
| 79 | + coo = geo_model._surface_points.df.loc[neighbours][['X', 'Y', 'Z']] |
| 80 | + # calculates covariance matrix |
| 81 | + cov = np.cov(coo.T) |
| 82 | + # calculate normalized normal vector |
| 83 | + normvec = normalize(np.cross(cov[0].T, cov[1].T).reshape(1, -1))[0] |
| 84 | + # check orientation of normal vector (has to be oriented to sky) |
| 85 | + if normvec[2] < 0: |
| 86 | + normvec = normvec * (-1) |
| 87 | + # append to the GemPy-model |
| 88 | + geo_model.add_orientations(geo_model._surface_points.df['X'][neighbours[0]], |
| 89 | + geo_model._surface_points.df['Y'][neighbours[0]], |
| 90 | + geo_model._surface_points.df['Z'][neighbours[0]], |
| 91 | + geo_model._surface_points.df['surface'][neighbours[0]], |
| 92 | + normvec.tolist()) |
| 93 | + # if computation is impossible set normal vector to default orientation |
| 94 | + else: |
| 95 | + print("orientation calculation of point" + str(neighbours[0]) + "is impossible") |
| 96 | + print("-> default vector is set [0,0,1]") |
| 97 | + geo_model.add_orientations(geo_model._surface_points.df['X'][neighbours[0]], |
| 98 | + geo_model._surface_points.df['Y'][neighbours[0]], |
| 99 | + geo_model._surface_points.df['Z'][neighbours[0]], |
| 100 | + geo_model._surface_points.df['surface'][neighbours[0]], |
| 101 | + orientation=[0, 0, 1]) |
| 102 | + return geo_model._orientations |
| 103 | + |
| 104 | + |
| 105 | +def set_orientation_from_neighbours_all(geo_model, neighbours): |
| 106 | + """ |
| 107 | + Calculates the orientations for all points with given neighbours. |
| 108 | + Parameters |
| 109 | + ---------- |
| 110 | + geo_model : geo_model |
| 111 | + GemPy-model. |
| 112 | + neighbours : list of Int64Index |
| 113 | + point-neighbours-IDs, the first id is the id of the point |
| 114 | + for which the orientation is calculated. |
| 115 | + """ |
| 116 | + |
| 117 | + # compute normal vector for the points |
| 118 | + for n in neighbours: |
| 119 | + set_orientation_from_neighbours(geo_model, n) |
| 120 | + |
| 121 | + return geo_model._orientations |
0 commit comments