1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
"""
Defines markers classes, used by a variety of renderers.
"""
# Major library imports
from numpy import array, pi
# Enthought library imports
from traits.api import HasTraits, Bool, Instance, Trait
from traitsui.api import EnumEditor
from kiva.constants import STROKE, FILL_STROKE, \
SQUARE_MARKER, DIAMOND_MARKER, CIRCLE_MARKER, \
CROSS_MARKER, TRIANGLE_MARKER, \
INVERTED_TRIANGLE_MARKER, PLUS_MARKER, DOT_MARKER, \
PIXEL_MARKER, NO_MARKER
# Local imports
from compiled_path import CompiledPath
class AbstractMarker(HasTraits):
""" Abstract class for markers.
"""
# How this marker is to be stroked (from kiva.constants).
# Since this needs to be a class variable, it can't be a trait.
draw_mode = STROKE
#draw_mode = Enum(FILL, EOF_FILL, STROKE, FILL_STROKE, EOF_FILL_STROKE)
# The kiva marker type (from kiva.constants).
kiva_marker = NO_MARKER
# Close the path object after drawing this marker?
close_path = Bool(True)
# Render the marker antialiased? Some
# markers render faster and look better if they are not anti-aliased..
antialias = Bool(True)
def add_to_path(self, path, size):
""" Adds this marker's representation to *path*, scaled appropriately
for *size*.
Parameters
----------
path : GraphicsContext
The target for drawing the marker.
size : number
Size of the marker, in pixels
"""
if self.close_path:
self._add_to_path(path, size)
path.close_path()
else:
self._add_to_path(path, size)
def get_compiled_path(self, size):
""" Returns a compiled path object that represents this marker, scaled
appropriately for *size*.
"""
raise NotImplementedError
def _add_to_path(self, path, size):
# subclasses must implement this method
raise NotImplementedError
class SquareMarker(AbstractMarker):
""" A marker that is a square.
"""
# How this marker is to be stroked. (Overrides AbstractMarker.)
draw_mode = FILL_STROKE
# The Kiva marker type. (Overrides AbstractMarker.)
kiva_marker = SQUARE_MARKER
# Do not render anti-aliased. (Overrides AbstractMarker.)
antialias = False
def _add_to_path(self, path, size):
path.rect(-size, -size, size * 2, size * 2)
class DiamondMarker(AbstractMarker):
""" A marker that is a diamond.
"""
# How this marker is to be stroked. (Overrides AbstractMarker.)
draw_mode = FILL_STROKE
# The Kiva marker type. (Overrides AbstractMarker.)
kiva_marker = DIAMOND_MARKER
# Do not render anti-aliased. (Overrides AbstractMarker.)
antialias = False
def _add_to_path(self, path, size):
path.lines(array(((0, -size),
(-size, 0),
(0, size),
(size, 0))))
class CircleMarker(AbstractMarker):
""" A marker that is a circle.
"""
# How this marker is to be stroked. (Overrides AbstractMarker.)
draw_mode = FILL_STROKE
# The Kiva marker type. (Overrides AbstractMarker.)
kiva_marker = CIRCLE_MARKER
# Array of points in a circle
circle_points = array([[ 1. , 0. ],
[ 0.966, 0.259],
[ 0.866, 0.5 ],
[ 0.707, 0.707],
[ 0.5 , 0.866],
[ 0.259, 0.966],
[ 0. , 1. ],
[-0.259, 0.966],
[-0.5 , 0.866],
[-0.707, 0.707],
[-0.866, 0.5 ],
[-0.966, 0.259],
[-1. , 0. ],
[-0.966, -0.259],
[-0.866, -0.5 ],
[-0.707, -0.707],
[-0.5 , -0.866],
[-0.259, -0.966],
[ 0. , -1. ],
[ 0.259, -0.966],
[ 0.5 , -0.866],
[ 0.707, -0.707],
[ 0.866, -0.5 ],
[ 0.966, -0.259],
[ 1. , 0. ]])
def _add_to_path(self, path, size):
if size <= 5:
pts = self.circle_points[::3] * size
elif size <= 10:
pts = self.circle_points[::2] * size
else:
pts = self.circle_points * size
path.lines(pts)
class TriangleMarker(AbstractMarker):
""" A marker that is a triangle with one apex pointing up.
"""
# How this marker is to be stroked. (Overrides AbstractMarker.)
draw_mode = FILL_STROKE
# The Kiva marker type. (Overrides AbstractMarker.)
kiva_marker = TRIANGLE_MARKER
# Do not render anti-aliased. (Overrides AbstractMarker.)
antialias = False
def _add_to_path(self, path, size):
path.lines(array(((-size, -size),
(size, -size),
(0, 0.732 * size))))
class Inverted_TriangleMarker(AbstractMarker):
""" A marker that is a triangle with one apex pointing down.
"""
# How this marker is to be stroked. (Overrides AbstractMarker.)
draw_mode = FILL_STROKE
# The Kiva marker type. (Overrides AbstractMarker.)
kiva_marker = INVERTED_TRIANGLE_MARKER
# Do not render anti-aliased. (Overrides AbstractMarker.)
antialias = False
def _add_to_path(self, path, size):
path.lines(array(((-size, size),
(size, size),
(0, -0.732 * size))))
class PlusMarker(AbstractMarker):
""" A marker that is a plus-sign.
"""
# How this marker is to be stroked. (Overrides AbstractMarker.)
draw_mode = STROKE
# The Kiva marker type. (Overrides AbstractMarker.)
kiva_marker = PLUS_MARKER
# Do not render anti-aliased. (Overrides AbstractMarker.)
antialias = False
def _add_to_path(self, path, size):
path.move_to(0, -size)
path.line_to(0, size)
path.move_to(-size, 0)
path.line_to(size, 0)
class CrossMarker(AbstractMarker):
""" A marker that is an X.
"""
# How this marker is to be stroked. (Overrides AbstractMarker.)
draw_mode = STROKE
# The Kiva marker type. (Overrides AbstractMarker.)
kiva_marker = CROSS_MARKER
# Do not render anti-aliased. (Overrides AbstractMarker.)
antialias = False
def _add_to_path(self, path, size):
path.move_to(-size, -size)
path.line_to(size, size)
path.move_to(size, -size)
path.line_to(-size, size)
class DotMarker(AbstractMarker):
""" A marker that is a dot.
"""
# How this marker is to be stroked. (Overrides AbstractMarker.)
draw_mode = FILL_STROKE
# The Kiva marker type. (Overrides AbstractMarker.)
kiva_marker = DOT_MARKER
def _add_to_path(self, path, size):
path.arc(0, 0, size, 0, 2 * pi)
class PixelMarker(AbstractMarker):
""" A marker that is a pixel.
"""
# How this marker is to be stroked. (Overrides AbstractMarker.)
draw_mode = STROKE
# The Kiva marker type. (Overrides AbstractMarker.)
kiva_marker = PIXEL_MARKER
# Do not render anti-aliased. (Overrides AbstractMarker.)
antialias = False
def _add_to_path(self, path, size):
# It's impossible to emulate a true Pixel Marker in a vector
# system, so we just draw a sub-pixel square 1.0 unit across.
path.rect(-0.5, -0.5, 1.0, 1.0)
class CustomMarker(AbstractMarker):
""" A marker that is a custom shape.
"""
# How this marker is to be stroked. (Overrides AbstractMarker.)
draw_mode = STROKE
# The Kiva marker type. (Overrides AbstractMarker.)
kiva_marker = NO_MARKER
# The custom path that represents this marker.
path = Instance(CompiledPath)
# Automatically scale **path** based on the input size parameter?
# If False, then the path does not respond to the 'size' parameter!
scale_path = Bool(True)
def _add_to_path(self, path, size):
if self.scale_path:
path.save_ctm()
path.scale_ctm(size)
path.add_path(path)
if self.scale_path:
path.restore_ctm()
def get_compiled_path(self, size):
""" Returns a path instance.
If **scale_path** is True, then the returned path is a new compiled
path that is scaled based on *size*. If **scaled_path** is False,
then this method just returns the current **path**.
"""
if self.scale_path:
newpath = CompiledPath()
newpath.scale_ctm(size)
newpath.add_path(self.path)
return newpath
else:
return self.path
# String names for marker types.
marker_names = ("square", "circle", "triangle", "inverted_triangle", "plus",
"cross", "diamond", "dot", "pixel")
# Mapping of marker string names to classes.
MarkerNameDict = {"square": SquareMarker,
"circle": CircleMarker,
"triangle": TriangleMarker,
"inverted_triangle": Inverted_TriangleMarker,
"plus": PlusMarker,
"cross": CrossMarker,
"diamond": DiamondMarker,
"dot": DotMarker,
"pixel": PixelMarker,
"custom": CustomMarker}
# A mapped trait that allows string naming of marker classes.
MarkerTrait = Trait("square", MarkerNameDict,
editor=EnumEditor(values=marker_names))
marker_trait = MarkerTrait
|