|
| 1 | +""" |
| 2 | +Contains a classes for generating hatch patterns. |
| 3 | +""" |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +from matplotlib.path import Path |
| 7 | + |
| 8 | +class HatchPatternBase: |
| 9 | + """ |
| 10 | + The base class for a hatch pattern. |
| 11 | + """ |
| 12 | + pass |
| 13 | + |
| 14 | +class HorizontalHatch(HatchPatternBase): |
| 15 | + def __init__(self, hatch, density): |
| 16 | + self.num_lines = (hatch.count('-') + hatch.count('+')) * density |
| 17 | + self.num_vertices = self.num_lines * 2 |
| 18 | + |
| 19 | + def set_vertices_and_codes(self, vertices, codes): |
| 20 | + steps = np.linspace(0.0, 1.0, self.num_lines, False) |
| 21 | + vertices[0::2, 0] = 0.0 |
| 22 | + vertices[0::2, 1] = steps |
| 23 | + vertices[1::2, 0] = 1.0 |
| 24 | + vertices[1::2, 1] = steps |
| 25 | + codes[0::2] = Path.MOVETO |
| 26 | + codes[1::2] = Path.LINETO |
| 27 | + |
| 28 | +class VerticalHatch(HatchPatternBase): |
| 29 | + def __init__(self, hatch, density): |
| 30 | + self.num_lines = (hatch.count('|') + hatch.count('+')) * density |
| 31 | + self.num_vertices = self.num_lines * 2 |
| 32 | + |
| 33 | + def set_vertices_and_codes(self, vertices, codes): |
| 34 | + steps = np.linspace(0.0, 1.0, self.num_lines, False) |
| 35 | + vertices[0::2, 0] = steps |
| 36 | + vertices[0::2, 1] = 0.0 |
| 37 | + vertices[1::2, 0] = steps |
| 38 | + vertices[1::2, 1] = 1.0 |
| 39 | + codes[0::2] = Path.MOVETO |
| 40 | + codes[1::2] = Path.LINETO |
| 41 | + |
| 42 | +class NorthEastHatch(HatchPatternBase): |
| 43 | + def __init__(self, hatch, density): |
| 44 | + self.num_lines = (hatch.count('/') + hatch.count('x') + hatch.count('X')) * density |
| 45 | + self.num_vertices = self.num_lines * 4 |
| 46 | + |
| 47 | + def set_vertices_and_codes(self, vertices, codes): |
| 48 | + steps = np.linspace(0.0, 1.0, self.num_lines, False) |
| 49 | + rev_steps = 1.0 - steps |
| 50 | + vertices[0::4, 0] = 0.0 |
| 51 | + vertices[0::4, 1] = steps |
| 52 | + vertices[1::4, 0] = rev_steps |
| 53 | + vertices[1::4, 1] = 1.0 |
| 54 | + vertices[2::4, 0] = rev_steps |
| 55 | + vertices[2::4, 1] = 0.0 |
| 56 | + vertices[3::4, 0] = 1.0 |
| 57 | + vertices[3::4, 1] = steps |
| 58 | + codes[0::2] = Path.MOVETO |
| 59 | + codes[1::2] = Path.LINETO |
| 60 | + |
| 61 | +class SouthEastHatch(HatchPatternBase): |
| 62 | + def __init__(self, hatch, density): |
| 63 | + self.num_lines = (hatch.count('\\') + hatch.count('x') + hatch.count('X')) * density |
| 64 | + self.num_vertices = self.num_lines * 4 |
| 65 | + |
| 66 | + def set_vertices_and_codes(self, vertices, codes): |
| 67 | + steps = np.linspace(0.0, 1.0, self.num_lines, False) |
| 68 | + vertices[0::4, 0] = 1.0 |
| 69 | + vertices[0::4, 1] = steps |
| 70 | + vertices[1::4, 0] = steps |
| 71 | + vertices[1::4, 1] = 1.0 |
| 72 | + vertices[2::4, 0] = steps |
| 73 | + vertices[2::4, 1] = 0.0 |
| 74 | + vertices[3::4, 0] = 0.0 |
| 75 | + vertices[3::4, 1] = steps |
| 76 | + codes[0::2] = Path.MOVETO |
| 77 | + codes[1::2] = Path.LINETO |
| 78 | + |
| 79 | +class Shapes(HatchPatternBase): |
| 80 | + filled = False |
| 81 | + def __init__(self, hatch, density): |
| 82 | + if self.num_rows == 0: |
| 83 | + self.num_shapes = 0 |
| 84 | + self.num_vertices = 0 |
| 85 | + else: |
| 86 | + self.num_shapes = ((self.num_rows / 2 + 1) * (self.num_rows + 1) + |
| 87 | + (self.num_rows / 2) * (self.num_rows)) |
| 88 | + self.num_vertices = (self.num_shapes * |
| 89 | + len(self.shape_vertices) * |
| 90 | + (self.filled and 1 or 2)) |
| 91 | + |
| 92 | + def set_vertices_and_codes(self, vertices, codes): |
| 93 | + offset = 1.0 / self.num_rows |
| 94 | + shape_vertices = self.shape_vertices * offset * self.size |
| 95 | + if not self.filled: |
| 96 | + inner_vertices = shape_vertices[::-1] * 0.9 |
| 97 | + shape_codes = self.shape_codes |
| 98 | + shape_size = len(shape_vertices) |
| 99 | + |
| 100 | + cursor = 0 |
| 101 | + for row in xrange(self.num_rows + 1): |
| 102 | + if row % 2 == 0: |
| 103 | + cols = np.linspace(0.0, 1.0, self.num_rows + 1, True) |
| 104 | + else: |
| 105 | + cols = np.linspace(offset / 2.0, 1.0 - offset / 2.0, self.num_rows, True) |
| 106 | + row_pos = row * offset |
| 107 | + for col_pos in cols: |
| 108 | + vertices[cursor:cursor+shape_size] = shape_vertices + (col_pos, row_pos) |
| 109 | + codes[cursor:cursor+shape_size] = shape_codes |
| 110 | + cursor += shape_size |
| 111 | + if not self.filled: |
| 112 | + vertices[cursor:cursor+shape_size] = inner_vertices + (col_pos, row_pos) |
| 113 | + codes[cursor:cursor+shape_size] = shape_codes |
| 114 | + cursor += shape_size |
| 115 | + |
| 116 | +class Circles(Shapes): |
| 117 | + def __init__(self, hatch, density): |
| 118 | + path = Path.unit_circle() |
| 119 | + self.shape_vertices = path.vertices |
| 120 | + self.shape_codes = path.codes |
| 121 | + Shapes.__init__(self, hatch, density) |
| 122 | + |
| 123 | +class SmallCircles(Circles): |
| 124 | + size = 0.2 |
| 125 | + |
| 126 | + def __init__(self, hatch, density): |
| 127 | + self.num_rows = (hatch.count('o')) * density |
| 128 | + Circles.__init__(self, hatch, density) |
| 129 | + |
| 130 | +class LargeCircles(Circles): |
| 131 | + size = 0.35 |
| 132 | + |
| 133 | + def __init__(self, hatch, density): |
| 134 | + self.num_rows = (hatch.count('O')) * density |
| 135 | + Circles.__init__(self, hatch, density) |
| 136 | + |
| 137 | +class SmallFilledCircles(SmallCircles): |
| 138 | + size = 0.1 |
| 139 | + filled = True |
| 140 | + |
| 141 | + def __init__(self, hatch, density): |
| 142 | + self.num_rows = (hatch.count('.')) * density |
| 143 | + Circles.__init__(self, hatch, density) |
| 144 | + |
| 145 | +class Stars(Shapes): |
| 146 | + size = 1.0 / 3.0 |
| 147 | + filled = True |
| 148 | + |
| 149 | + def __init__(self, hatch, density): |
| 150 | + self.num_rows = (hatch.count('*')) * density |
| 151 | + path = Path.unit_regular_star(5) |
| 152 | + self.shape_vertices = path.vertices |
| 153 | + self.shape_codes = np.ones(len(self.shape_vertices)) * Path.LINETO |
| 154 | + self.shape_codes[0] = Path.MOVETO |
| 155 | + Shapes.__init__(self, hatch, density) |
| 156 | + |
| 157 | +_hatch_types = [ |
| 158 | + HorizontalHatch, |
| 159 | + VerticalHatch, |
| 160 | + NorthEastHatch, |
| 161 | + SouthEastHatch, |
| 162 | + SmallCircles, |
| 163 | + LargeCircles, |
| 164 | + SmallFilledCircles, |
| 165 | + Stars |
| 166 | + ] |
| 167 | + |
| 168 | +def get_path(hatchpattern, density=6): |
| 169 | + """ |
| 170 | + Given a hatch specifier, *hatchpattern*, generates Path to render |
| 171 | + the hatch in a unit square. *density* is the number of lines per |
| 172 | + unit square. |
| 173 | + """ |
| 174 | + size = 1.0 |
| 175 | + density = int(density) |
| 176 | + |
| 177 | + patterns = [hatch_type(hatchpattern, density) for hatch_type in _hatch_types] |
| 178 | + num_vertices = sum([pattern.num_vertices for pattern in patterns]) |
| 179 | + |
| 180 | + if num_vertices == 0: |
| 181 | + return Path(np.empty((0, 2))) |
| 182 | + |
| 183 | + vertices = np.empty((num_vertices, 2)) |
| 184 | + codes = np.empty((num_vertices,), np.uint8) |
| 185 | + |
| 186 | + cursor = 0 |
| 187 | + for pattern in patterns: |
| 188 | + if pattern.num_vertices != 0: |
| 189 | + vertices_chunk = vertices[cursor:cursor + pattern.num_vertices] |
| 190 | + codes_chunk = codes[cursor:cursor + pattern.num_vertices] |
| 191 | + pattern.set_vertices_and_codes(vertices_chunk, codes_chunk) |
| 192 | + cursor += pattern.num_vertices |
| 193 | + |
| 194 | + return Path(vertices, codes) |
0 commit comments