Revision: 6072
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6072&view=rev
Author:   jouni
Date:     2008-09-07 10:57:15 +0000 (Sun, 07 Sep 2008)

Log Message:
-----------
Fix conversion of quadratic to cubic Bezier curves in PDF 
and PS backends. Patch by Jae-Joon Lee.

Modified Paths:
--------------
    trunk/matplotlib/CHANGELOG
    trunk/matplotlib/examples/tests/backend_driver.py
    trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py
    trunk/matplotlib/lib/matplotlib/backends/backend_ps.py
    trunk/matplotlib/lib/matplotlib/cbook.py

Added Paths:
-----------
    trunk/matplotlib/examples/api/quad_bezier.py

Modified: trunk/matplotlib/CHANGELOG
===================================================================
--- trunk/matplotlib/CHANGELOG  2008-09-06 21:37:53 UTC (rev 6071)
+++ trunk/matplotlib/CHANGELOG  2008-09-07 10:57:15 UTC (rev 6072)
@@ -1,3 +1,6 @@
+2008-09-07 Fix conversion of quadratic to cubic Bezier curves in PDF 
+           and PS backends. Patch by Jae-Joon Lee. - JKS
+
 2008-09-06 Added 5-point star marker to plot command - EF
 
 2008-09-05 Fix hatching in PS backend - MGD

Added: trunk/matplotlib/examples/api/quad_bezier.py
===================================================================
--- trunk/matplotlib/examples/api/quad_bezier.py                                
(rev 0)
+++ trunk/matplotlib/examples/api/quad_bezier.py        2008-09-07 10:57:15 UTC 
(rev 6072)
@@ -0,0 +1,17 @@
+import numpy as np
+import matplotlib.path as mpath
+import matplotlib.patches as mpatches
+import matplotlib.pyplot as plt
+
+fig = plt.figure()
+ax = fig.add_subplot(111)
+pp1 = mpatches.PathPatch(
+    mpath.Path([(0, 0), (1, 0), (1, 1), (0, 0)], [1, 3, 3, 5]),
+    fc="none", transform=ax.transData)
+
+ax.add_patch(pp1)
+ax.plot([0.75], [0.25], "ro")
+ax.set_title('The red point should be on the path')
+
+plt.draw()
+

Modified: trunk/matplotlib/examples/tests/backend_driver.py
===================================================================
--- trunk/matplotlib/examples/tests/backend_driver.py   2008-09-06 21:37:53 UTC 
(rev 6071)
+++ trunk/matplotlib/examples/tests/backend_driver.py   2008-09-07 10:57:15 UTC 
(rev 6072)
@@ -127,7 +127,8 @@
     'colorbar_only.py',
     'color_cycle.py',
     'donut_demo.py',
-    'path_patch_demo.py'
+    'path_patch_demo.py',
+    'quad_bezier.py'
 ]
 
 units_dir = os.path.join('..', 'units')

Modified: trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py     2008-09-06 
21:37:53 UTC (rev 6071)
+++ trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py     2008-09-07 
10:57:15 UTC (rev 6072)
@@ -26,7 +26,7 @@
      FigureManagerBase, FigureCanvasBase
 from matplotlib.backends.backend_mixed import MixedModeRenderer
 from matplotlib.cbook import Bunch, is_string_like, reverse_dict, \
-    get_realpath_and_stat, is_writable_file_like, maxdict
+    get_realpath_and_stat, is_writable_file_like, maxdict, quad2cubic
 from matplotlib.figure import Figure
 from matplotlib.font_manager import findfont, is_opentype_cff_font
 from matplotlib.afm import AFM
@@ -1096,6 +1096,7 @@
         tpath = transform.transform_path(path)
 
         cmds = []
+        last_points = None
         for points, code in tpath.iter_segments():
             if code == Path.MOVETO:
                 cmds.extend(points)
@@ -1104,15 +1105,15 @@
                 cmds.extend(points)
                 cmds.append(Op.lineto)
             elif code == Path.CURVE3:
-                cmds.extend([points[0], points[1],
-                             points[0], points[1],
-                             points[2], points[3],
-                             Op.curveto])
+                points = quad2cubic(*(list(last_points[-2:]) + list(points)))
+                cmds.extend(points)
+                cmds.append(Op.curveto)
             elif code == Path.CURVE4:
                 cmds.extend(points)
                 cmds.append(Op.curveto)
             elif code == Path.CLOSEPOLY:
                 cmds.append(Op.closepath)
+            last_points = points
         return cmds
     pathOperations = staticmethod(pathOperations)
 

Modified: trunk/matplotlib/lib/matplotlib/backends/backend_ps.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/backends/backend_ps.py      2008-09-06 
21:37:53 UTC (rev 6071)
+++ trunk/matplotlib/lib/matplotlib/backends/backend_ps.py      2008-09-07 
10:57:15 UTC (rev 6072)
@@ -20,7 +20,7 @@
      FigureManagerBase, FigureCanvasBase
 
 from matplotlib.cbook import is_string_like, get_realpath_and_stat, \
-    is_writable_file_like, maxdict
+    is_writable_file_like, maxdict, quad2cubic
 from matplotlib.figure import Figure
 
 from matplotlib.font_manager import findfont, is_opentype_cff_font
@@ -448,22 +448,23 @@
         path = transform.transform_path(path)
 
         ps = []
+        last_points = None
         for points, code in path.iter_segments():
             if code == Path.MOVETO:
                 ps.append("%g %g m" % tuple(points))
             elif code == Path.LINETO:
                 ps.append("%g %g l" % tuple(points))
             elif code == Path.CURVE3:
+                points = quad2cubic(*(list(last_points[-2:]) + list(points)))
                 ps.append("%g %g %g %g %g %g c" %
-                          (points[0], points[1],
-                           points[0], points[1],
-                           points[2], points[3]))
+                          tuple(points[2:]))
             elif code == Path.CURVE4:
                 ps.append("%g %g %g %g %g %g c" % tuple(points))
             elif code == Path.CLOSEPOLY:
                 ps.append("cl")
+            last_points = points
+            
         ps = "\n".join(ps)
-
         return ps
 
     def _get_clip_path(self, clippath, clippath_transform):

Modified: trunk/matplotlib/lib/matplotlib/cbook.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/cbook.py    2008-09-06 21:37:53 UTC (rev 
6071)
+++ trunk/matplotlib/lib/matplotlib/cbook.py    2008-09-07 10:57:15 UTC (rev 
6072)
@@ -1390,6 +1390,20 @@
     """
     return np.all(X[0] == X[-1])
 
+def quad2cubic(q0x, q0y, q1x, q1y, q2x, q2y):
+    """
+    Converts a quadratic Bezier curve to a cubic approximation.
+
+    The inputs are the x and y coordinates of the three control points
+    of a quadratic curve, and the output is a tuple of x and y
+    coordinates of the four control points of the cubic curve.
+    """
+    # c0x, c0y = q0x, q0y
+    c1x, c1y = q0x + 2./3. * (q1x - q0x), q0y + 2./3. * (q1y - q0y)
+    c2x, c2y = c1x + 1./3. * (q2x - q0x), c1y + 1./3. * (q2y - q0y)
+    # c3x, c3y = q2x, q2y
+    return q0x, q0y, c1x, c1y, c2x, c2y, q2x, q2y
+
 # a dict to cross-map linestyle arguments
 _linestyles = [('-', 'solid'),
     ('--', 'dashed'),


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-checkins mailing list
Matplotlib-checkins@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to