Camera and view function and properties
조회 수: 7 (최근 30일)
이전 댓글 표시
I've been trying to understand the view and camera functions and created the following code for illustration:
peaks(30);
ax=get(gca);
% Display values
disp('Initial values')
dispprops(ax)
% Change target
ax.CameraTarget = [0 0 10];
disp('Target changed by property')
dispprops(ax)
camtarget([0 0 20])
disp('Target changed by function')
dispprops(ax)
% Change position
ax.CameraPosition = [-30 -40 70];
disp('Position changed by property')
dispprops(ax)
campos([-30 -40 80])
disp('Position changed by function')
dispprops(ax)
% Change view
ax.View = [45 90];
disp('View changed by property')
dispprops(ax)
view([60 60])
disp('View changed by function')
dispprops(ax)
function dispprops(ax)
viewfromax = ax.View %#ok
[azfromfun,elfromfun] = view;
disp(['viewfromfun = ' num2str(azfromfun) ',' num2str(elfromfun)])
posfromax = ax.CameraPosition %#ok
disp(['posfromfun = ' num2str(campos)])
targetfromax = ax.CameraTarget %#ok
disp(['targetfromfun = ' num2str(camtarget)])
end
There are two things that seem odd to me:
1) Changes via the ax properties are stored, but don't do anything in the plot (even if the respective modes are set to manual). On the other hand, changes via functions are not stored in the properties, but do modify the plot. So what are the properties actually good for?
2) Changes to position and target do not change the view values, but view on the other hand does change target and position. Shouldn't the dependency be mutual? I. e. aren't azimuth and elevation determined by camera position and target (since target and position are independent from each other, c. f. https://mathworks.com/help/matlab/creating_plots/defining-scenes-with-camera-graphics.html)?
댓글 수: 0
채택된 답변
Voss
2022년 4월 1일
편집: Voss
2022년 4월 1일
When you do
ax = get(gca);
you are storing the axes properties in a struct called ax. (In this case these are the initial properties of the axes, since you do this before anything else.) Setting values of fields in ax doesn't affect the axes itself:
ax.View
ax.View = [0 0]
% actual View is still [0 90] because 'View' was set
% in ax, which is a copy of the axes properties
get(gca,'View')
Store the axes itself instead:
ax = gca;
and the properties will be accurate after they are set, regardless of how they are set.
peaks(30);
% ax=get(gca);
ax = gca;
% Display values
disp('Initial values')
dispprops(ax)
% Change target
ax.CameraTarget = [0 0 10];
disp('Target changed by property')
dispprops(ax)
camtarget([0 0 20])
disp('Target changed by function')
dispprops(ax)
% Change position
ax.CameraPosition = [-30 -40 70];
disp('Position changed by property')
dispprops(ax)
campos([-30 -40 80])
disp('Position changed by function')
dispprops(ax)
% Change view
ax.View = [45 90];
disp('View changed by property')
dispprops(ax)
view([60 60])
disp('View changed by function')
dispprops(ax)
function dispprops(ax)
viewfromax = ax.View %#ok
[azfromfun,elfromfun] = view;
disp(['viewfromfun = ' num2str(azfromfun) ',' num2str(elfromfun)])
posfromax = ax.CameraPosition %#ok
disp(['posfromfun = ' num2str(campos)])
targetfromax = ax.CameraTarget %#ok
disp(['targetfromfun = ' num2str(camtarget)])
end
댓글 수: 3
Voss
2022년 4월 2일
It looks like it's because the properties don't update instantly, but only when the next drawnow (or other command that processes the graphics event queue such as figure or pause) occurs.
Here's an example where CameraPosition is set and then View is queried afterward. In Case 1 no drawnow is used and View remains at its old value, and in Case 2 drawnow is used in between setting CameraPosition and getting View, and View shows the updated value:
% First, a reference figure with default axes 'View' and
% 'CameraPosition' properties shown:
figure();
peaks(30);
ax = gca();
get(ax,'View')
get(ax,'CameraPosition')
% Case 1:
% when setting 'CameraPosition', 'View' property doesn't change
% (but actual apparent 'View' does):
figure();
peaks(30);
ax = gca();
get(ax,'View')
get(ax,'CameraPosition')
set(ax,'CameraPosition',[0 -40 80])
% still the old View (but clearly the surface is seen
% from a different angle than in the first figure)
get(ax,'View')
% Case 2:
% putting a drawnow() after setting 'CameraPosition'
% causes 'View' property to show the updated value:
figure();
peaks(30);
ax = gca();
get(ax,'View')
get(ax,'CameraPosition')
set(ax,'CameraPosition',[0 -40 80])
drawnow()
get(ax,'View') % now View is accurate
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!