rendering.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import math
  2. import numpy as np
  3. def downsample(img, factor):
  4. """
  5. Downsample an image along both dimensions by some factor
  6. """
  7. assert img.shape[0] % factor == 0
  8. assert img.shape[1] % factor == 0
  9. img = img.reshape([img.shape[0]//factor, factor, img.shape[1]//factor, factor, 3])
  10. img = img.mean(axis=3)
  11. img = img.mean(axis=1)
  12. return img
  13. def fill_coords(img, fn, color):
  14. """
  15. Fill pixels of an image with coordinates matching a filter function
  16. """
  17. for y in range(img.shape[0]):
  18. for x in range(img.shape[1]):
  19. yf = (y + 0.5) / img.shape[0]
  20. xf = (x + 0.5) / img.shape[1]
  21. if fn(xf, yf):
  22. img[y, x] = color
  23. return img
  24. def rotate_fn(fin, cx, cy, theta):
  25. def fout(x, y):
  26. x = x - cx
  27. y = y - cy
  28. x2 = cx + x * math.cos(-theta) - y * math.sin(-theta)
  29. y2 = cy + y * math.cos(-theta) + x * math.sin(-theta)
  30. return fin(x2, y2)
  31. return fout
  32. def point_in_line(x0, y0, x1, y1, r):
  33. p0 = np.array([x0, y0])
  34. p1 = np.array([x1, y1])
  35. dir = p1 - p0
  36. dist = np.linalg.norm(dir)
  37. dir = dir / dist
  38. xmin = min(x0, x1) - r
  39. xmax = max(x0, x1) + r
  40. ymin = min(y0, y1) - r
  41. ymax = max(y0, y1) + r
  42. def fn(x, y):
  43. # Fast, early escape test
  44. if x < xmin or x > xmax or y < ymin or y > ymax:
  45. return False
  46. q = np.array([x, y])
  47. pq = q - p0
  48. # Closest point on line
  49. a = np.dot(pq, dir)
  50. a = np.clip(a, 0, dist)
  51. p = p0 + a * dir
  52. dist_to_line = np.linalg.norm(q - p)
  53. return dist_to_line <= r
  54. return fn
  55. def point_in_circle(cx, cy, r):
  56. def fn(x, y):
  57. return (x-cx)*(x-cx) + (y-cy)*(y-cy) <= r * r
  58. return fn
  59. def point_in_rect(xmin, xmax, ymin, ymax):
  60. def fn(x, y):
  61. return x >= xmin and x <= xmax and y >= ymin and y <= ymax
  62. return fn
  63. def point_in_triangle(a, b, c):
  64. a = np.array(a)
  65. b = np.array(b)
  66. c = np.array(c)
  67. def fn(x, y):
  68. v0 = c - a
  69. v1 = b - a
  70. v2 = np.array((x, y)) - a
  71. # Compute dot products
  72. dot00 = np.dot(v0, v0)
  73. dot01 = np.dot(v0, v1)
  74. dot02 = np.dot(v0, v2)
  75. dot11 = np.dot(v1, v1)
  76. dot12 = np.dot(v1, v2)
  77. # Compute barycentric coordinates
  78. inv_denom = 1 / (dot00 * dot11 - dot01 * dot01)
  79. u = (dot11 * dot02 - dot01 * dot12) * inv_denom
  80. v = (dot00 * dot12 - dot01 * dot02) * inv_denom
  81. # Check if point is in triangle
  82. return (u >= 0) and (v >= 0) and (u + v) < 1
  83. return fn
  84. def highlight_img(img, color=(255, 255, 255), alpha=0.30):
  85. """
  86. Add highlighting to an image
  87. """
  88. blend_img = img + alpha * (np.array(color, dtype=np.uint8) - img)
  89. blend_img = blend_img.clip(0, 255).astype(np.uint8)
  90. img[:, :, :] = blend_img