octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

implementation of patch objects


From: Kai Habel
Subject: implementation of patch objects
Date: Wed, 08 Aug 2007 20:13:51 +0000
User-agent: IceDove 1.5.0.12 (X11/20070607)

Hello all,

I have implemented the patch object. Of course this is a first version and not every possible property/argument is implemented. To evaluate the patch/cdata property correctly, I have added clim and climmode for the axes object. Currently something like patch(x,y,"yellow") does not work but patch(x,y, [1 1 0]) does, see my other meesage about this problem as of today [1].

Attached are patches to graphics.{cc|h} and first versions of patch.m and __patch__.m

2007-08-08  Kai Habel  <address@hidden>

        * graphics.cc, graphics.h: added patch object and clim/climmode
         property for axes object. New files __patch__.m and patch.m

[1] 
http://www.nabble.com/patch-for-color_properties-assignment-tf4041176.html#a12050135

Kai
--- graphics.cc.orig    2007-08-08 17:16:29.000000000 +0000
+++ graphics.cc 2007-08-08 17:19:18.000000000 +0000
@@ -245,7 +245,7 @@
        {
          pfx = name.substr (0, 5);
 
-         if (pfx.compare ("image"))
+         if (pfx.compare ("image") || pfx.compare ("patch"))
            offset = 5;
          else if (len > 6)
            {
@@ -318,7 +318,7 @@
        {
          pfx = name.substr (0, 5);
 
-         if (pfx.compare ("image"))
+         if (pfx.compare ("image") || pfx.compare ("patch"))
            offset = 5;
          else if (len > 6)
            {
@@ -945,9 +945,11 @@
     xlim (),
     ylim (),
     zlim (),
+    clim (),
     xlimmode ("auto"),
     ylimmode ("auto"),
     zlimmode ("auto"),
+    climmode ("auto"),
     xlabel (octave_NaN),
     ylabel (octave_NaN),
     zlabel (octave_NaN),
@@ -985,6 +987,9 @@
   xlim = tlim;
   ylim = tlim;
   zlim = tlim;
+  Matrix cl (1, 2, 0);
+  cl(1) = 1;
+  clim = cl;
 
   Matrix tview (1, 2, 0.0);
   tview(1) = 90;
@@ -1054,12 +1059,19 @@
       zlim = val;
       zlimmode = "manual";
     }
+  else if (name.compare ("clim"))
+    {
+      clim = val;
+      climmode = "manual";
+    }
   else if (name.compare ("xlimmode"))
     xlimmode = val;
   else if (name.compare ("ylimmode"))
     ylimmode = val;
   else if (name.compare ("zlimmode"))
     zlimmode = val;
+  else if (name.compare ("climmode"))
+    climmode = val;
   else if (name.compare ("xlabel"))
     {
       graphics_handle h = ::reparent (val, "set", "xlabel",
@@ -1198,10 +1210,15 @@
   xlim = tlim;
   ylim = tlim;
   zlim = tlim;
-
+  
+  Matrix cl (1, 2, 0);
+  cl(1) = 1;
+  clim = cl;
+  
   xlimmode = "auto";
   ylimmode = "auto";
   zlimmode = "auto";
+  climmode = "auto";
   xlabel = octave_NaN;
   ylabel = octave_NaN;
   zlabel = octave_NaN;
@@ -1287,9 +1304,11 @@
   m.assign ("xlim", xlim);
   m.assign ("ylim", ylim);
   m.assign ("zlim", zlim);
+  m.assign ("clim", clim);
   m.assign ("xlimmode", xlimmode);
   m.assign ("ylimmode", ylimmode);
   m.assign ("zlimmode", zlimmode);
+  m.assign ("climmode", climmode);
   m.assign ("xlabel", xlabel);
   m.assign ("ylabel", ylabel);
   m.assign ("zlabel", zlabel);
@@ -1365,12 +1384,16 @@
     retval = ylim;
   else if (name.compare ("zlim"))
     retval = zlim;
+  else if (name.compare ("clim"))
+    retval = clim;
   else if (name.compare ("xlimmode"))
     retval = xlimmode;
   else if (name.compare ("ylimmode"))
     retval = ylimmode;
   else if (name.compare ("zlimmode"))
     retval = zlimmode;
+  else if (name.compare ("climmode"))
+    retval = climmode;
   else if (name.compare ("xlabel"))
     {
       if (xisnan (xlabel))
@@ -1506,10 +1529,16 @@
   m["xlim"] = tlim;
   m["ylim"] = tlim;
   m["zlim"] = tlim;
+  
+  Matrix cl(1, 2, 0);
+  cl(1) = 1;
+  
+  m["clim"] = cl;
 
   m["xlimmode"] = "auto";
   m["ylimmode"] = "auto";
   m["zlimmode"] = "auto";
+  m["climmode"] = "auto";
   m["xlabel"] = octave_NaN;
   m["ylabel"] = octave_NaN;
   m["zlabel"] = octave_NaN;
@@ -1952,6 +1981,173 @@
 
 // ---------------------------------------------------------------------
 
+patch::patch_properties::patch_properties (const graphics_handle& mh,
+                                          const graphics_handle& p)
+  : base_properties (go_name, mh, p),
+    cdata (Matrix ()),
+    xdata (Matrix ()),
+    ydata (Matrix ()),
+    zdata (Matrix ()),
+    facecolor (radio_values("{flat}|none|interp")),
+    facealpha (1.0),
+    edgecolor (color_values(0, 0, 0), radio_values("flat|none|interp")),
+    linestyle ("-"),
+    linewidth (0.5),
+    marker ("none"),
+    markeredgecolor ("auto"),
+    markerfacecolor ("none"),
+    markersize (1)
+{ }
+
+void
+patch::patch_properties::set (const property_name& name,
+                             const octave_value& val)
+{
+  bool modified = true;
+
+  if (name.compare ("parent"))
+    set_parent (val);
+  else if (name.compare ("children"))
+    children = maybe_set_children (children, val);
+  else if (name.compare ("__modified__"))
+    {
+      __modified__ = val.bool_value ();
+      modified = false;
+    }
+  else if (name.compare ("cdata"))
+    cdata = val;
+  else if (name.compare ("xdata"))
+    xdata = val;
+  else if (name.compare ("ydata"))
+    ydata = val;
+  else if (name.compare ("zdata"))
+    zdata = val;
+  else if (name.compare ("facecolor"))
+    facecolor = val;
+  else if (name.compare ("facealpha"))
+    facealpha = val;
+  else if (name.compare ("edgecolor"))
+    edgecolor = val;
+  else if (name.compare ("linestyle"))
+    linestyle = val;
+  else if (name.compare ("linewidth"))
+    linewidth = val;
+  else if (name.compare ("marker"))
+    marker = val;
+  else if (name.compare ("markeredgecolor"))
+    markeredgecolor = val;
+  else if (name.compare ("markerfacecolor"))
+    markerfacecolor = val;
+  else if (name.compare ("markersize"))
+    markersize = val;
+
+  else
+    {
+      modified = false;
+      warning ("set: invalid property `%s'", name.c_str ());
+    }
+
+  if (modified)
+    mark_modified ();
+}
+
+octave_value
+patch::patch_properties::get (void) const
+{
+  Octave_map m;
+
+  m.assign ("type", type);
+  m.assign ("parent", parent);
+  m.assign ("children", children);
+  m.assign ("__modified__", __modified__);
+  m.assign ("cdata", cdata);
+  m.assign ("xdata", xdata);
+  m.assign ("ydata", ydata);
+  m.assign ("zdata", zdata);
+  m.assign ("facecolor", facecolor);
+  m.assign ("facealpha", facealpha);
+  m.assign ("edgecolor", edgecolor);
+  m.assign ("linestyle", linestyle);
+  m.assign ("linewidth", linewidth);
+  m.assign ("marker", marker);
+  m.assign ("markeredgecolor", markeredgecolor);
+  m.assign ("markerface", markerfacecolor);
+  m.assign ("markersize", markersize);
+
+  return m;
+}
+
+octave_value
+patch::patch_properties::get (const property_name& name) const
+{
+  octave_value retval;
+
+  if (name.compare ("type"))
+    retval = type;
+  else if (name.compare ("parent"))
+    retval = parent;
+  else if (name.compare ("children"))
+    retval = children;
+  else if (name.compare ("__modified__"))
+    retval = __modified__;
+  else if (name.compare ("cdata"))
+    retval = cdata;
+  else if (name.compare ("xdata"))
+    retval = xdata;
+  else if (name.compare ("ydata"))
+    retval = ydata;
+  else if (name.compare ("zdata"))
+    retval = zdata;
+  else if (name.compare ("facecolor"))
+    retval = facecolor;
+  else if (name.compare ("facealpha"))
+    retval = facecolor;
+  else if (name.compare ("egdecolor"))
+    retval = edgecolor;
+  else if (name.compare ("linestyle"))
+    retval = linestyle;
+  else if (name.compare ("linewidth"))
+    retval = linewidth;
+  else if (name.compare ("marker"))
+    retval = marker;
+  else if (name.compare ("markeredgecolor"))
+    retval = markeredgecolor;
+  else if (name.compare ("markerfacecolor"))
+    retval = markerfacecolor;
+  else if (name.compare ("markersize"))
+    retval = markersize;
+  else
+    warning ("get: invalid property `%s'", name.c_str ());
+
+  return retval;
+}
+
+property_list::pval_map_type patch::patch_properties::factory_defaults (void)
+{
+  property_list::pval_map_type m;
+
+  m["cdata"] = Matrix ();
+  m["xdata"] = Matrix ();
+  m["ydata"] = Matrix ();
+  m["zdata"] = Matrix ();
+  m["facecolor"] = color_property();
+  m["facealpha"] = 1.0;
+  m["edgecolor"] = color_property("black");
+  m["linestyle"] = "-";
+  m["linewidth"] = 0.5;
+  m["marker"] = "none";
+  m["markeredgecolor"] = "auto";
+  m["markerfacecolor"] = "none";
+  m["markersize"] = 1;
+
+
+  return m;
+}
+
+std::string patch::patch_properties::go_name ("patch");
+
+// ---------------------------------------------------------------------
+
 surface::surface_properties::surface_properties (const graphics_handle& mh,
                                                 const graphics_handle& p)
   : base_properties (go_name, mh, p),
@@ -2096,9 +2292,10 @@
     go = new text (h, p);
   else if (go_name == "image")
     go = new image (h, p);
+  else if (go_name == "patch")
+    go = new patch (h, p);
   else if (go_name == "surface")
     go = new surface (h, p);
-
   if (go)
     handle_map[h] = graphics_object (go);
   else
@@ -2151,6 +2348,7 @@
   plist_map["line"] = line::line_properties::factory_defaults ();
   plist_map["text"] = text::text_properties::factory_defaults ();
   plist_map["image"] = image::image_properties::factory_defaults ();
+  plist_map["patch"] = patch::patch_properties::factory_defaults ();
   plist_map["surface"] = surface::surface_properties::factory_defaults ();
 
   return plist_map;
@@ -2440,6 +2638,15 @@
   GO_BODY (surface);
 }
 
+DEFUN (__go_patch__, args, ,
+  "-*- texinfo -*-\n\
address@hidden {Built-in Function} {} __go_patch__ (@var{parent})\n\
+Create a patch graphics object.\n\
address@hidden deftypefn")
+{
+  GO_BODY (patch);
+}
+
 DEFUN (__go_delete__, args, ,
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} __go_delete__ (@var{h})\n\
--- graphics.h.orig     2007-08-08 17:16:48.000000000 +0000
+++ graphics.h  2007-08-08 17:22:21.000000000 +0000
@@ -1179,9 +1179,11 @@
     OCTAVE_GRAPHICS_PROPERTY (octave_value, xlim);
     OCTAVE_GRAPHICS_PROPERTY (octave_value, ylim);
     OCTAVE_GRAPHICS_PROPERTY (octave_value, zlim);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, clim);
     OCTAVE_GRAPHICS_PROPERTY (octave_value, xlimmode);
     OCTAVE_GRAPHICS_PROPERTY (octave_value, ylimmode);
     OCTAVE_GRAPHICS_PROPERTY (octave_value, zlimmode);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, climmode);
     OCTAVE_GRAPHICS_MUTABLE_PROPERTY (graphics_handle, xlabel);
     OCTAVE_GRAPHICS_MUTABLE_PROPERTY (graphics_handle, ylabel);
     OCTAVE_GRAPHICS_MUTABLE_PROPERTY (graphics_handle, zlabel);
@@ -1584,6 +1586,99 @@
 
 // ---------------------------------------------------------------------
 
+class patch : public base_graphics_object
+{
+public:
+  class patch_properties : public base_properties
+  {
+  public:
+    patch_properties (const graphics_handle& mh, const graphics_handle& p);
+
+    ~patch_properties (void) { }
+
+    void set (const property_name& name, const octave_value& val);
+
+    octave_value get (void) const;
+
+    octave_value get (const property_name& name) const;
+
+    std::string graphics_object_name (void) const { return go_name; }
+
+    static property_list::pval_map_type factory_defaults (void);
+
+  private:
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, cdata);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, xdata);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, ydata);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, zdata);
+    OCTAVE_GRAPHICS_PROPERTY (color_property, facecolor);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, facealpha);
+    OCTAVE_GRAPHICS_PROPERTY (color_property, edgecolor);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, linestyle);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, linewidth);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, marker);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, markeredgecolor);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, markerfacecolor);
+    OCTAVE_GRAPHICS_PROPERTY (octave_value, markersize);
+
+    static std::string go_name;
+  };
+
+  patch_properties properties;
+
+public:
+  patch (const graphics_handle& mh, const graphics_handle& p)
+    : base_graphics_object (), properties (mh, p)
+  {
+    properties.override_defaults (*this);
+  }
+
+  ~patch (void) { properties.delete_children (); }
+
+  std::string type (void) const { return properties.graphics_object_name (); }
+
+  void mark_modified (void) { properties.mark_modified (); }
+
+  void override_defaults (base_graphics_object& obj)
+  {
+    // Allow parent (figure) to override first (properties knows how
+    // to find the parent object).
+    properties.override_defaults (obj);
+  }
+
+  void set_from_list (property_list& plist)
+  {
+    properties.set_from_list (*this, plist);
+  }
+
+  void set (const property_name& name, const octave_value& val)
+  {
+    properties.set (name, val);
+  }
+
+  octave_value get (void) const
+  {
+    return properties.get ();
+  }
+
+  octave_value get (const property_name& name) const
+  {
+    return properties.get (name);
+  }
+
+  graphics_handle get_parent (void) const { return properties.get_parent (); }
+
+  void remove_child (const graphics_handle& h) { properties.remove_child (h); }
+
+  void adopt (const graphics_handle& h) { properties.adopt (h); }
+
+  void reparent (const graphics_handle& h) { properties.reparent (h); }
+
+  bool valid_object (void) const { return true; }
+};
+
+// ---------------------------------------------------------------------
+
 class surface : public base_graphics_object
 {
 public:
## Copyright (C) 2005 John W. Eaton
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, write to the Free
## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
## 02110-1301, USA.

## -*- texinfo -*-
## @deftypefn {Function File} {} patch ()
## @deftypefnx {Function File} {} patch (@var{x}, @var{y}, @var{c})
## @deftypefnx {Function File} {} patch (@var{x}, @var{y}, @var{c}, @var{opts})
## Create patch object from @var{x} and @var{y} with color @var{c} and insert 
in current
## axes object.  Return handle to patch object.
## For an uniform colored patch, @var{c} can be given as [r, g, b]-vector, 
scalar value refering
## to the current colormap, or string value (e.g. "r" or "red").
## @end deftypefn

## Author: jwe

function h = patch (varargin)

  ## make a default patch object, and make it the current axes for
  ## the current figure.
  tmp = __patch__ (gca (), varargin{:});

  if (nargout > 0)
    h = tmp;
  endif

endfunction
## Copyright (C) 2007 John W. Eaton, Shai Ayal, Kai Habel
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, write to the Free
## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
## 02110-1301, USA.

## -*- texinfo -*-
## @deftypefn {Function File} {} __patch__ (@var{p}, @var{x}, @var{y}, @var{c})
## Create patch object from @var{x} and @var{y} with color @var{c} and parent 
@var{p}.
## Return handle to patch object.
## @end deftypefn

## Author: Kai Habel

function h = __patch__ (p, varargin)

  if (nargin < 1)
    print_usage ();
  endif

  nvargs = numel (varargin);

  if (nvargs > 1 && isnumeric (varargin{1}) && isnumeric (varargin{2}))
    num_data_args = 2;
  else
    num_data_args = 0;
  endif

  if (rem (nvargs - num_data_args - 1, 2) == 0) && (nvargs > 2)
  else
    print_usage ("patch");
  endif

  x = varargin{1};
  y = varargin{2};
  c = varargin{3};

  h = __go_patch__ (p);
  ax = get(h,"parent");
  if (num_data_args > 1)
    set (h, "xdata", x, "ydata", y);
  endif

  if (isstr(c))
    # have color string
    set(h, "FaceColor", c);
  elseif (length(c) == 1)
    if isnan(c)
      set(h, "FaceColor", [1 1 1]);
      set(h, "CData", c);
    elseif isnumeric(c)
      # have color index
      set(h, "FaceColor", "flat");
      set(h, "CData", c);

      clim = get(ax, "CLim");
      if (c < clim(1))
        set(ax, "CLim", [c, clim(2)])
      endif
      if (c > clim(2))
        set(ax, "CLim", [clim(1), c])
      end

    else
      # unknown color value
      error("color value not valid");
    end
  elseif (length(c) == 3)
    # have rgb/rgba value
    set(h, "FaceColor", c);
  else
    # color vector
    if (length(c) != length(x) || length(c) != length(y))
      usage("size of x, y, and c must be equal")
    else
      set(h, "FaceColor", "interp");
      set(h, "CData", c);
      if (abs(max(c) - min(c)) < eps)
        set(ax, "CLim", [c(1) - 1, c(1) + 1])
      else
        set(ax, "CLim", [min(c) max(c)]);
      end
    end
  end 

  if (nvargs > num_data_args + 1)
    set (h, varargin{num_data_args+2:end});
  endif

endfunction

reply via email to

[Prev in Thread] Current Thread [Next in Thread]