Skip to content

Commit

Permalink
Merge 6c3b8b6 into 267d7f3
Browse files Browse the repository at this point in the history
  • Loading branch information
corranwebster committed Aug 31, 2014
2 parents 267d7f3 + 6c3b8b6 commit 0236ab2
Show file tree
Hide file tree
Showing 17 changed files with 2,278 additions and 1,520 deletions.
536 changes: 536 additions & 0 deletions kiva/abstract_graphics_context.py

Large diffs are not rendered by default.

184 changes: 100 additions & 84 deletions kiva/affine.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
Expand All @@ -10,7 +10,7 @@
#
# Author: Enthought, Inc.
# Description: <Enthought kiva package component>
#------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
""" Functions for affine matrices.
:Copyright: Space Telescope Science Institute
Expand All @@ -22,7 +22,7 @@
it by the affine transform matrix::
a b 0
[x, y, 1] * c d 0 = [x',y',1]
[x, y, 1] * c d 0 = [x', y', 1]
tx ty 1
This is the opposite order of multiplication from what many are
Expand All @@ -35,60 +35,73 @@
Notes:
I'm not using a class because of possible speed implications.
Currently the affine transform is a 3x3 array. Other tools use
a 6-tuple of (a,b,c,d,tx,ty) to represent the transform because
a 6-tuple of (a, b, c, d, tx, ty) to represent the transform because
the other 3 array entries are constant. Other code should call
methods from this module instead of manipulating the array,
in case the implementation is changed at some future date.
"""

from numpy import array, alltrue, arctan2, cos, dot, eye, float64, ones, \
ravel, sin, zeros
from numpy import (array, array_equal, arctan2, cos, dot, eye, float64, ones,
ravel, sin, zeros)

#-----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# Affine transform construction
#-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------

def affine_identity():
""" Returns a new identity affine_transform object.
"""
return eye(3,3)
return eye(3, 3)


def affine_from_values(a, b, c, d, tx, ty):
""" Return the affine matrix corresponding to the values
The result is the array::
[ a b 0 ]
[ c d 0 ]
[ tx ty 1 ]
def affine_from_values(a,b,c,d,tx,ty):
"""
"""
transform = array(((a, b, 0), (c, d, 0), (tx, ty, 1)), float64)
return transform

def affine_from_scale(sx,sy):

def affine_from_scale(sx, sy):
""" Returns an affine transform providing the given scaling.
"""
r = affine_identity()
return scale(r,sx,sy)
return scale(r, sx, sy)


def affine_from_rotation(angle):
""" Returns an affine transform rotated by angle in radians.
"""
r = affine_identity()
return rotate(r,angle)
return rotate(r, angle)


def affine_from_translation(x,y):
def affine_from_translation(x, y):
""" Returns an affine transform with the given translation.
"""
r = affine_identity()
return translate(r,x,y)
return translate(r, x, y)


#-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Affine transform manipulation
#-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------

def scale(transform,sx,sy):
def scale(transform, sx, sy):
""" Returns a scaled version of the transform by the given values.
Scaling is done using the following formula::
Scaling is done using the following formula::
sx 0 0 a b 0 sx*a sx*b 0
0 sy 0 * c d 0 = sy*c sy*d 0
0 0 1 tx ty 1 0 0 1
"""
# this isn't the operation described above, but produces the
# same results.
Expand All @@ -97,34 +110,36 @@ def scale(transform,sx,sy):
scaled[1] *= sy
return scaled

def rotate(transform,angle):

def rotate(transform, angle):
""" Rotates transform by angle in radians.
Rotation is done using the following formula::
Rotation is done using the following formula::
cos(x) sin(x) 0 a b 0
-sin(x) cos(x) 0 * c d 0 =
0 0 1 tx ty 1
::
::
cos(x)*a+sin(x)*b cos(x)*b+sin(x)*d 0
-sin(x)*a+cos(x)*c -sin(x)*b+cos(x)*d 0
tx ty 1
where x = angle.
where x = angle.
"""
a = cos(angle)
b = sin(angle)
c = -b
d = a
tx = 0.
ty = 0.
rot = affine_from_values(a,b,c,d,tx,ty)
return dot(rot,transform)
rot = affine_from_values(a, b, c, d, tx, ty)
return dot(rot, transform)

def translate(transform,x,y):
""" Returns transform translated by (x,y).

def translate(transform, x, y):
""" Returns transform translated by (x, y).
Translation::
Expand All @@ -133,68 +148,70 @@ def translate(transform,x,y):
x y 1 tx ty 1 x*a+y*c+y x*b+y*d+ty 1
"""
r = affine_identity()
r[2,0] = x
r[2,1] = y
return dot(r,transform)
r[2, 0] = x
r[2, 1] = y
return dot(r, transform)


def concat(transform,other):
def concat(transform, other):
""" Returns the concatenation of transform with other. This
is simply transform pre-multiplied by other.
"""
return dot(other,transform)
return dot(other, transform)


def invert(m):
""" Returns the inverse of the transform, m.
"""
inv = zeros(m.shape, float64)
det = m[0,0] * m[1,1] - m[0,1] * m[1,0]
inv = zeros(m.shape, float64)
det = m[0, 0] * m[1, 1] - m[0, 1]*m[1, 0]

inv[0,0] = m[1,1]
inv[0,1] = -m[0,1]
inv[0,2] = 0
inv[0, 0] = m[1, 1]
inv[0, 1] = -m[0, 1]
inv[0, 2] = 0

inv[1,0] = -m[1,0]
inv[1,1] = m[0,0]
inv[1,2] = 0
inv[1, 0] = -m[1, 0]
inv[1, 1] = m[0, 0]
inv[1, 2] = 0

inv[2,0] = m[1,0]*m[2,1] - m[1,1]*m[2,0]
inv[2,1] = -m[0,0]*m[2,1] + m[0,1]*m[2,0]
inv[2,2] = m[0,0]*m[1,1] - m[0,1]*m[1,0]
inv[2, 0] = m[1, 0]*m[2, 1] - m[1, 1]*m[2, 0]
inv[2, 1] = -m[0, 0]*m[2, 1] + m[0, 1]*m[2, 0]
inv[2, 2] = m[0, 0]*m[1, 1] - m[0, 1]*m[1, 0]
inv /= det
return inv

#-----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# Affine transform information
#-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------

IDENTITY = affine_identity()


def is_identity(m):
""" Tests whether an affine transform is the identity transform.
"""
m1 = ravel(IDENTITY)
m2 = ravel(m)
return alltrue(m1 == m2)
# numarray return None for .flat...
#return alltrue(IDENTITY.flat == m.flat)
return array_equal(m, IDENTITY)


def affine_params(m):
""" Returns the a, b, c, d, tx, ty values of an
affine transform.
"""
a = m[0,0]
b = m[0,1]
c = m[1,0]
d = m[1,1]
tx = m[2,0]
ty = m[2,1]
return a,b,c,d,tx,ty
a = m[0, 0]
b = m[0, 1]
c = m[1, 0]
d = m[1, 1]
tx = m[2, 0]
ty = m[2, 1]
return a, b, c, d, tx, ty


def trs_factor(m):
""" Factors a matrix as if it is the product of translate/rotate/scale
matrices applied (i.e., concatenated) in that order. It returns:
tx,ty,sx,sy,angle
tx, ty, sx, sy, angle
where tx and ty are the translations, sx and sy are the scaling
values and angle is the rotational angle in radians.
Expand All @@ -206,59 +223,58 @@ def trs_factor(m):
Needs Test!
"""

#-------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Extract Values from Matrix
#
# Translation values are correct as extracted. Rotation and
# scaling need a little massaging.
#-------------------------------------------------------------------------
a,b,c,d,tx,ty = affine_params(m)
# -------------------------------------------------------------------------
a, b, c, d, tx, ty = affine_params(m)

#-------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Rotation -- tan(angle) = b/d
#-------------------------------------------------------------------------
angle = arctan2(b,d)
# -------------------------------------------------------------------------
angle = arctan2(b, d)

#-------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Scaling
#
# sx = a/cos(angle) or sx = -c/sin(angle)
# sy = d/cos(angle) or sy = b/sin(angle)
#-------------------------------------------------------------------------
# -------------------------------------------------------------------------
cos_ang = cos(angle)
sin_ang = sin(angle)
if cos_ang != 0.0:
sx,sy = a/cos_ang, d/cos_ang
sx, sy = a/cos_ang, d/cos_ang
else:
sx,sy = -c/sin_ang, b/sin_ang
sx, sy = -c/sin_ang, b/sin_ang

return tx, ty, sx, sy, angle

return tx,ty,sx,sy,angle

#-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Transforming points and arrays of points
#-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------

def transform_point(ctm,pt):
def transform_point(ctm, pt):
""" Returns pt transformed by the affine transform, ctm.
"""
p1 = ones(3,float64)
p1 = ones(3, float64)
p1[:2] = pt
res = dot(p1,ctm)[:2]
res = dot(p1, ctm)[:2]
return res

def transform_points(ctm,pts):

def transform_points(ctm, pts):
""" Transforms an array of points using the affine transform, ctm.
"""
if is_identity(ctm):
res = pts
else:
x = pts[...,0]
y = pts[...,1]
a,b,c,d,tx,ty = affine_params(ctm)
# x_p = a*x+c*y+tx
# y_p = b*x+d*y+ty
x = pts[..., 0]
y = pts[..., 1]
a, b, c, d, tx, ty = affine_params(ctm)
res = zeros(pts.shape, float64)
res[...,0] = a*x+c*y+tx
res[...,1] = b*x+d*y+ty
res[..., 0] = a*x+c*y+tx
res[..., 1] = b*x+d*y+ty
return res

17 changes: 9 additions & 8 deletions kiva/arc_conversion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from numpy import sqrt, dot, sin, dot, array, pi
from numpy import sqrt, dot, sin, array, pi

from math import atan2, acos


def two_point_arc_to_kiva_arc(p1, p2, theta):
"""
Converts an arc in two point and subtended angle format (startpoint,
Expand All @@ -12,22 +13,23 @@ def two_point_arc_to_kiva_arc(p1, p2, theta):
chord = p2-p1
chordlen = sqrt(dot(chord, chord))
radius = abs(chordlen/(2*sin(theta/2)))
altitude = sqrt(pow(radius, 2)-pow(chordlen/2, 2))
if theta>pi or theta<0:
altitude = sqrt(pow(radius, 2) - pow(chordlen/2, 2))
if theta > pi or theta < 0:
altitude = -altitude
chordmidpoint = (p1+p2)/2
rotate90 = array(((0.0,-1.0),
rotate90 = array(((0.0, -1.0),
(1.0, 0.0)))
centerpoint = dot(rotate90, (chord/chordlen))*altitude + chordmidpoint

start_angle = atan2(*(p1-centerpoint)[::-1])
end_angle = start_angle+theta
if theta<0:
start_angle = atan2(*(p1 - centerpoint)[::-1])
end_angle = start_angle + theta
if theta < 0:
start_angle, end_angle, = end_angle, start_angle
cw = False
radius = abs(radius)
return (centerpoint[0], centerpoint[1], radius, start_angle, end_angle, cw)


def arc_to_tangent_points(start, p1, p2, radius):
""" Given a starting point, two endpoints of a line segment, and a radius,
calculate the tangent points for arc_to().
Expand Down Expand Up @@ -60,4 +62,3 @@ def normalize_vector(x, y):
t2 = (p1[0]+v2[0]*dist_to_tangent, p1[1]+v2[1]*dist_to_tangent)

return (t1, t2)

0 comments on commit 0236ab2

Please sign in to comment.