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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
########################################################################
##
## Copyright (C) 2016-2024 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## 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 3 of the License, 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, see
## <https://www.gnu.org/licenses/>.
##
########################################################################
## -*- texinfo -*-
## @deftypefn {} {} camlookat ()
## @deftypefnx {} {} camlookat (@var{h})
## @deftypefnx {} {} camlookat (@var{handle_list})
## @deftypefnx {} {} camlookat (@var{hax})
## Move the camera and adjust its properties to look at objects.
##
## When the input is a handle @var{h}, the camera is set to point toward the
## center of the bounding box of @var{h}. The camera's position is adjusted so
## the bounding box approximately fills the field of view.
##
## This command fixes the camera's viewing direction
## (@code{camtarget() - campos()}), camera up vector
## (@pxref{XREFcamup,,@code{camup}}) and viewing angle
## (@pxref{XREFcamva,,@code{camva}}). The camera target
## (@pxref{XREFcamtarget,,@code{camtarget}}) and camera position
## (@pxref{XREFcampos,,@code{campos}}) are changed.
##
## If the argument is a list @var{handle_list}, then a single bounding box for
## all the objects is computed and the camera is then adjusted as above.
##
## If the argument is an axis object @var{hax}, then the children of the axis
## are used as @var{handle_list}. When called with no inputs, it uses the
## current axis (@pxref{XREFgca,,@code{gca}}).
##
## @seealso{camorbit, camzoom, camroll}
## @end deftypefn
function camlookat (h)
if (nargin == 0)
hax = gca ();
h = get (hax, "children");
elseif (nargin == 1)
if (isaxes (h))
hax = h;
h = get (hax, "children");
elseif (all (ishghandle (h)))
hax = ancestor (h, "axes");
if (numel (hax) > 1)
hax = unique ([hax{:}]);
endif
if (numel (hax) > 1)
error ("camlookat: HANDLE_LIST must be children of the same axes");
endif
endif
endif
if (isempty (h))
return;
endif
x0 = x1 = y0 = y1 = z0 = z1 = [];
for i = 1:numel (h)
hi = h(i);
if (! ishghandle (hi))
error ("camlookat: Inputs must be handles");
endif
x0_ = min (get (hi, "xdata")(:));
x1_ = max (get (hi, "xdata")(:));
y0_ = min (get (hi, "ydata")(:));
y1_ = max (get (hi, "ydata")(:));
z0_ = min (get (hi, "zdata")(:));
z1_ = max (get (hi, "zdata")(:));
if (i == 1)
x0 = x0_; x1 = x1_;
y0 = y0_; y1 = y1_;
z0 = z0_; z1 = z1_;
else
x0 = min (x0, x0_); x1 = max (x1, x1_);
y0 = min (y0, y0_); y1 = max (y1, y1_);
z0 = min (z0, z0_); z1 = max (z1, z1_);
endif
endfor
dar = daspect (hax);
## current view direction
curdir = (camtarget (hax) - campos (hax)) ./ dar;
curdir /= norm (curdir);
## target to middle of bounding box
mid = [x0+x1, y0+y1, z0+z1]/2 ./ dar;
## vertices of the bounding box
bb = [x0 y0 z0;
x0 y0 z1;
x0 y1 z0;
x0 y1 z1;
x1 y0 z0;
x1 y0 z1;
x1 y1 z0;
x1 y1 z1] ./ dar;
## Find corner of bounding box with maximum opening angle.
## Make sure temporary pov is well outside boundary of bounding box.
bb_diag = norm ([x0-x1, y0-y1, z0-z1] ./ dar);
cp_test = mid - 2*bb_diag*curdir;
bb_cp = bb - cp_test;
bb_cp ./= norm (bb_cp, 2, "rows");
aperture = norm (curdir .* bb_cp, 2, "rows");
max_corner = find (aperture == max (aperture), 1, "first");
## projection of corner on line of sight
sz = curdir * (bb(max_corner,:) - mid)';
bb_proj = mid + sz * curdir;
## Calculate distance for which that corner appears at camva/2
dist = norm (bb(max_corner,:) - bb_proj) / tand (camva () / 2);
## Is bb_proj in front of or behind mid?
if (curdir * (mid - bb_proj)' > 0)
cp = bb_proj - dist * curdir;
else
cp = 2*mid - bb_proj - dist * curdir;
endif
## set camera properties
camva (hax, "manual"); # avoid auto-adjusting
camtarget (hax, mid .* dar);
campos (hax, cp .* dar);
endfunction
%!demo
%! clf;
%! [x, y, z] = peaks ();
%! surf (x, y, z/5);
%! hold on
%! [x, y, z] = sphere ();
%! s1 = surf (x/2, y/2+1.5, z/2+2);
%! s2 = surf (x/5+0.2, y/5-2, z/5+1);
%! axis equal
%! axis tight
%! title ("camlookat() demo #1");
%! pause (1);
%! camlookat (s1);
%! pause (1);
%! camlookat (s2);
%! pause (1);
%! camlookat ([s1 s2]);
%!test
%! ## not an error (does nothing)
%! camlookat ([]);
%!test
%! hf = figure ("visible", "off");
%! unwind_protect
%! sphere ();
%! camlookat ();
%! assert (camva ("mode"), "manual");
%! unwind_protect_cleanup
%! close (hf);
%! end_unwind_protect
## direction is preserved
%!test
%! hf = figure ("visible", "off");
%! unwind_protect
%! [x y z] = sphere ();
%! h1 = surf (x + 1, y, z);
%! hold on
%! h2 = surf (x - 1, y + 2, z + 1);
%! dir = camtarget () - campos ();
%! dir /= norm (dir);
%! camlookat (h1);
%! dir2 = camtarget () - campos ();
%! dir2 /= norm (dir2);
%! assert (dir, dir2, -4*eps);
%! camlookat (h2);
%! dir2 = camtarget () - campos ();
%! dir2 /= norm (dir2);
%! assert (dir, dir2, -4*eps);
%! camlookat ([h1 h2]);
%! dir2 = camtarget () - campos ();
%! dir2 /= norm (dir2);
%! assert (dir, dir2, -4*eps);
%! unwind_protect_cleanup
%! close (hf);
%! end_unwind_protect
## look at axes not same as default auto view
%!test
%! hf = figure ("visible", "off");
%! unwind_protect
%! sphere ();
%! zlim ([0 10]);
%! xlim ([0 5]);
%! A = camtarget ();
%! assert (A, [2.5 0 5]);
%! camlookat ();
%! B = camtarget ();
%! assert (B, [0 0 0]);
%! unwind_protect_cleanup
%! close (hf);
%! end_unwind_protect
## list, empty and hax input give same results
%!test
%! hf = figure ("visible", "off");
%! unwind_protect
%! [x y z] = sphere ();
%! h1 = surf (x + 1, y, z);
%! hold on
%! h2 = surf (x - 1, y + 2, z + 1);
%! camlookat ();
%! T1 = camtarget ();
%! P1 = campos ();
%! camtarget ("auto");
%! campos ("auto");
%! camlookat ([h1 h2]);
%! T2 = camtarget ();
%! P2 = campos ();
%! assert (T1, T2, -10*eps);
%! assert (P1, P2, -10*eps);
%! camtarget ("auto");
%! campos ("auto");
%! camlookat (gca ());
%! T3 = camtarget ();
%! P3 = campos ();
%! assert (T1, T3, -10*eps);
%! assert (P1, P3, -10*eps);
%! unwind_protect_cleanup
%! close (hf);
%! end_unwind_protect
## act on given axes
%!test
%! hf = figure ("visible", "off");
%! unwind_protect
%! [x, y, z] = sphere ();
%! hax1 = subplot (1, 2, 1);
%! hs11 = surf (hax1, x, y, z);
%! hold on
%! hs12 = surf (hax1, x, y+2, z+3);
%! hax2 = subplot (1, 2, 2);
%! hs21 = surf (hax2, x, y, z);
%! hold on
%! hs22 = surf (hax2, x, y+2, z+3);
%! ct2 = camtarget (hax2);
%! camlookat (hs11);
%! assert (camtarget (hax1), [0 0 0]);
%! assert (camtarget (hax2), ct2);
%! camlookat (hs22);
%! assert (camtarget (hax1), [0 0 0]);
%! assert (camtarget (hax2), [0 2 3]);
%! unwind_protect_cleanup
%! close (hf);
%! end_unwind_protect
## compare to Matlab R2016a output
%!test <61532>
%! hf = figure ("visible", "off");
%! unwind_protect
%! [x, y, z] = peaks ();
%! s3 = surf (x, y, z/5);
%! hold on
%! [x, y, z] = sphere ();
%! s2 = surf (x/2, y/2+1.5, z/2+2);
%! s1 = mesh (x/2-4, 3*y, z/2 - 1);
%! axis equal
%! axis tight
%! camlookat (s1);
%! assert (camtarget (), [-4 0 -1], -eps);
%! assert (campos (), [-22.806319527015962 -24.508872777366225 16.835942167146133], -1e-7);
%! camlookat (s2);
%! assert (camtarget (), [0 1.5 2], -eps);
%! assert (campos (), [-5.8209352826617424 -6.0859905540313779 7.5205839138865720], -1e-7);
%! camlookat (s3);
%! assert (camtarget (), [0 0 0.15285290208388014], 1e-10);
%! assert (campos (), [-30.372839208265287 -39.582654701437512 28.958500003444449], -1e-7);
%! camlookat ();
%! assert (camtarget (), [-0.75 0 0.5], -eps);
%! assert (campos (), [-35.795562033972338 -45.672265648153193 33.737264567111389], -1e-7);
%! unwind_protect_cleanup
%! close (hf);
%! end_unwind_protect
## Test input validation
%!error <called with too many inputs> camlookat (1, 2)
%!error <must be handle> camlookat ("a")
%!error <children of the same axes>
%! hf = figure ("visible", "off");
%! unwind_protect
%! [x, y, z] = sphere ();
%! hax1 = subplot (1, 2, 1);
%! hs1 = surf (hax1, x, y, z);
%! hax2 = subplot (1, 2, 2);
%! hs2 = surf (hax2, x, y, z);
%! camlookat ([hs1 hs2]);
%! unwind_protect_cleanup
%! close (hf);
%! end_unwind_protect
|