changeset 25687:b6f3e8ce6815

camlight.m: Allow an axes handle as first input (bug #54372). * camlight.m: Add additional calling form to documentation. Check "type" of graphics handle first argument to distinguish between a "light" object and an "axes" opbject. If variable hax has not been initialized, use gca. Change calls to get() and light() to use axes handle hax. Add BIST test for new behavior. Modify existing BIST test to pass with new behavior.
author Guillaume Flandin <guillaume.offline@gmail.com>
date Thu, 26 Jul 2018 14:44:36 -0700
parents af0b81f2ffe6
children b2917b7858ba
files scripts/plot/draw/camlight.m
diffstat 1 files changed, 37 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/draw/camlight.m	Thu Jul 26 14:10:25 2018 -0700
+++ b/scripts/plot/draw/camlight.m	Thu Jul 26 14:44:36 2018 -0700
@@ -24,6 +24,7 @@
 ## @deftypefnx {} {} camlight (@var{az}, @var{el})
 ## @deftypefnx {} {} camlight (@dots{}, @var{style})
 ## @deftypefnx {} {} camlight (@var{hl}, @dots{})
+## @deftypefnx {} {} camlight (@var{hax}, @dots{})
 ## @deftypefnx {} {@var{h} =} camlight (@dots{})
 ## Add a light object to a figure using a simple interface.
 ##
@@ -45,6 +46,9 @@
 ## If the first argument @var{hl} is a handle to a light object, then act on
 ## this light object rather than creating a new object.
 ##
+## If the first argument @var{hax} is an axes handle, then create a new light
+## object in this axes, rather than the current axes returned by @code{gca}.
+##
 ## The optional return value @var{h} is a graphics handle to the light object.
 ## This can be used to move or further change properties of the light object.
 ##
@@ -100,14 +104,21 @@
   ## We don't worry about that.
   if (numel (varargin) > 0 && numel (varargin{1}) == 1
       && ishghandle (varargin{1}))
-    hl = varargin{1};
-    if (! isgraphics (hl, "light"))
-      error ("camlight: HL must be a handle to a light object");
+    typ = get (varargin{1}, "type");
+    if (strcmp (typ, "light"))
+      hl  = varargin{1};
+      hax = get (hl, "parent");
+    elseif (strcmp (typ, "axes"))
+      hax = varargin{1};
+      hl  = [];
+    else
+      error ("camlight: HL must be a handle to an axes or light object");
     endif
     varargin(1) = [];
     nargin = nargin - 1;
   else
-    hl = [];
+    hl  = [];
+    hax = [];
   endif
 
   style = "local";
@@ -168,9 +179,12 @@
     endswitch
   endif
 
-  cam_up = get (gca (), "cameraupvector");
-  cam_pos = get (gca (), "cameraposition");
-  cam_target = get (gca (), "cameratarget");
+  if (isempty (hax))
+    hax = gca ();
+  endif
+  cam_up = get (hax, "cameraupvector");
+  cam_pos = get (hax, "cameraposition");
+  cam_target = get (hax, "cameratarget");
 
   view_ax = cam_target - cam_pos;
   view_ax /= norm (view_ax);
@@ -186,7 +200,7 @@
   pos = [pos{:}];
 
   if (isempty (hl))
-    hl = light ("Position", pos, "style", style);
+    hl = light (hax, "Position", pos, "style", style);
   else
     set (hl, "Position", pos, "style", style);
   endif
@@ -296,9 +310,23 @@
 %!   close (hf);
 %! end_unwind_protect
 
+## Test an axes handle as first argument
+%!test
+%! hf = figure ("visible", "off");
+%! unwind_protect
+%!   hax1 = subplot (1, 2, 1);
+%!   hax2 = subplot (1, 2, 2);
+%!   hl1  = camlight ();
+%!   hl2  = camlight (hax1);
+%!   assert (get (hl1, "Parent"), hax2);
+%!   assert (get (hl2, "Parent"), hax1);
+%! unwind_protect_cleanup
+%!   close (hf);
+%! end_unwind_protect
+
 ## Test input validation
 %!error camlight (1,2,3,4,5)
-%!error <HL must be a handle to a light object> camlight (0, "left")
+%!error <HL must be a handle to an axes or light object> camlight (0, "left")
 %!error <Invalid call> camlight ({1}, {2})
 %!error <Invalid call> camlight (rand (), 1, 2, 3)
 %!error <invalid light position 'foobar'> camlight ("foobar")