Skip to content

Commit

Permalink
Removing some old unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
Adi Suissa-Peleg committed Jun 16, 2016
1 parent 6ce1612 commit a2703c2
Showing 1 changed file with 1 addition and 91 deletions.
92 changes: 1 addition & 91 deletions rh_renderer/single_tile_affine_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import math

class SingleTileAffineRenderer:


def __init__(self, img_path, width, height, compute_mask=False, compute_distances=True):
self.img_path = img_path
Expand Down Expand Up @@ -36,41 +37,6 @@ def get_start_point(self):
def get_bbox(self):
return self.bbox

# def contains_point(self, p):
# """Return True if the point is inside the image boundaries (bounding polygon)."""

def get_min_distance(self, points):
"""Returns a list of minimal distances between each of the given points and any of the image boundaries (bounding polygon).
Assumes that the given points are inside the bounding polygon."""
#assert(p.shape == (2,))
# Get the normals of each line, and compute the distance between the point and the normal
# Based on method 2 (but for 2D) from: http://www.qc.edu.hk/math/Advanced%20Level/Point_to_line.htm
denominators = [np.linalg.norm(self.corners[i] - self.corners[(i + 1) % len(self.corners)]) for i in range(len(self.corners))]
self_normals = get_normals(self.corners)
if points.shape == (2,): # A single point
dist = np.min([np.linalg.norm(np.dot(n, points - c)) / denom
for c, n, denom in zip(self.corners, self_normals, denominators)])
return dist
else: # multiple points
dists = [np.min([np.linalg.norm(np.dot(n, p - c)) / denom
for c, n, denom in zip(self.corners, self_normals, denominators)])
for p in points]
return dists

def is_overlapping(self, other_tile):
"""Uses Separating Axes Theorem (http://www.dyn4j.org/2010/01/sat/) in order to decide
whether the the current transformed tile and the other transformed tile are overlapping"""
# Fetch the normals of each tile
self_normals = get_normals(self.corners)
other_normals = get_normals(other_tile.corners)
# Check all edges of self against the normals of the other tile
if not check_normals_side(self.corners, self_normals, other_tile.corners):
return True
# Check all edges of the other tile against the normals of self
if not check_normals_side(other_tile.corners, other_normals, self.corners):
return True
return False

def render(self):
"""Returns the rendered image (after transformation), and the start point of the image in global coordinates"""
if self.already_rendered:
Expand Down Expand Up @@ -145,20 +111,6 @@ def crop_with_distances(self, from_x, from_y, to_x, to_y):
self.render()
cropped_img = self.img[overlapping_area[2] - self.bbox[2]:overlapping_area[3] - self.bbox[2] + 1,
overlapping_area[0] - self.bbox[0]:overlapping_area[1] - self.bbox[0] + 1]
# if self.compute_mask:
# cropped_mask = self.mask[overlapping_area[2] - self.bbox[2]:overlapping_area[3] - self.bbox[2] + 1,
# overlapping_area[0] - self.bbox[0]:overlapping_area[1] - self.bbox[0] + 1]
# # find for each row where is the first and last one
# rows_ones = find_per_row_first_last_one(cropped_mask)
# cols_ones = find_per_row_first_last_one(cropped_mask.T)
# # fill the weights matrix according to the distance of each pixel from it's row and col values
# grid = np.mgrid[0:cropped_mask.shape[0], 0:cropped_mask.shape[1]]
# cropped_distances = np.minimum(
# np.minimum(np.abs(grid[0] - cols_ones[:, 0]), np.abs(grid[0] - cols_ones[:, 1])),
# np.minimum(np.abs(grid[1].T - rows_ones[:, 0].T).T, np.abs(grid[1].T - rows_ones[:, 1].T).T)
# )
# # Filter only the weights that are on the mask (using elementwise multiplication)
# cropped_distances = cropped_distances * cropped_mask

if self.compute_distances:
cropped_distances = self.weights[overlapping_area[2] - self.bbox[2]:overlapping_area[3] - self.bbox[2] + 1,
Expand All @@ -181,46 +133,4 @@ def update_img_transformed_corners_and_bbox(self):
self.shape = (self.bbox[1] - self.bbox[0] + 1, self.bbox[3] - self.bbox[2] + 1)


def get_normals(corners):
"""Given a polygon corners list, returns a list of non-normalized normals for each edge"""
edges = [(corners[i] - corners[(i + 1) % len(corners)]) for i in range(len(corners))]
normals = [(-e[1], e[0]) for e in edges]
return normals

def check_normals_side(corners1, normals1, corners2):
"""Checks if all corners2 appear on one side of polygon1"""
assert(len(corners1) == len(normals1))
for c, n in zip(corners1, normals1):
signs2 = [np.sign(np.dot(n, p - c)) for p in corners2]
signs2 = [s for s in signs2 if abs(s - 0.) > 0.0001] # remove all +-0.
if np.any(signs2 != signs2[0]):
return False
return True

def find_per_row_first_last_one(arr):
"""Given a 2D array (of only 1's in a quadrangle shape, and 0's), for each row find the first and the last occurrance of 1.
Returns a 2D array with the same number of rows as arr, and 2 columns with the column-indices
of the first and last one. If a row has only 0's, -1 will be returned on both indices"""
res = np.full((arr.shape[0], 2), -1, dtype=np.int16)
# take the first and last column of arr, and find all 1's
arr_T = arr.T
first_col_non_zero = np.nonzero(arr_T[0])
last_col_non_zero = np.nonzero(arr_T[-1])
for r in first_col_non_zero[0]:
res[r, 0] = 0
for r in last_col_non_zero[0]:
res[r, 1] = arr.shape[1] - 1
# Now find the positions where the value changes in the middle of the matrix using np.diff
nonzero = np.nonzero(np.diff(arr))
# nonzero contents for each row, r:
# if nonzero doesn't have a row with r, the row has the same value (either 0 or 1)
# if nonzero has a single row with r, the row changes the value once (either from 0 to 1 or from 1 to 0)
# if nonzero has row r twice, the row changes both from 0 to 1 and then from 1 to 0
for r, c in zip(*nonzero):
if res[r, 0] > -1:
# already updated the left value, or there is a single change from 1 to 0
res[r, 1] = c
else:
res[r, 0] = c + 1
return res

0 comments on commit a2703c2

Please sign in to comment.