Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Rotate 3D-Figures

Asked by Max

Max (view profile)

on 11 Jul 2011

[EDIT: 20110711 16:20 CDT - reformat - WDR]

Hi,

I have a little problem while programming with Matlab. Our task is to create a GUI which makes it possible to rotate, to move and scaling 3D-Figures.

Moving, picturing the figures and the structure of the GUI is no problem, but I´m not able to program the part of rotating and scaling.

Maybe someone can help me...

Thanks a lot....

Next I will give you the functions I had programed:

GUI:

    function varargout = bedieninterface_do(varargin)
  % BEDIENINTERFACE_DO M-file for bedieninterface_do.fig
  %      BEDIENINTERFACE_DO, by itself, creates a new BEDIENINTERFACE_DO or raises the existing
  %      singleton*.
  %
  %      H = BEDIENINTERFACE_DO returns the handle to a new BEDIENINTERFACE_DO or the handle to
  %      the existing singleton*.
  %
  %      BEDIENINTERFACE_DO('CALLBACK',hObject,eventData,handles,...) calls the local
  %      function named CALLBACK in BEDIENINTERFACE_DO.M with the given input arguments.
  %
  %      BEDIENINTERFACE_DO('Property','Value',...) creates a new BEDIENINTERFACE_DO or raises the
  %      existing singleton*.  Starting from the left, property value pairs are
  %      applied to the GUI before bedieninterface_do_OpeningFcn gets called.  An
  %      unrecognized property name or invalid value makes property application
  %      stop.  All inputs are passed to bedieninterface_do_OpeningFcn via varargin.
  %
  %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
  %      instance to run (singleton)".
  %
  % See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help bedieninterface_do
% Last Modified by GUIDE v2.5 11-Jul-2011 14:11:45
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @bedieninterface_do_OpeningFcn, ...
                   'gui_OutputFcn',  @bedieninterface_do_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before bedieninterface_do is made visible.
function bedieninterface_do_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to bedieninterface_do (see VARARGIN)
global h
h = handles;
% Choose default command line output for bedieninterface_do
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes bedieninterface_do wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = bedieninterface_do_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes during object creation, after setting all properties.
function panel1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to panel1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
set (hObject, 'SelectionChangeFcn', @sel_body);
function sel_body(source, eventdata)
global b
rb_Koerper = get(eventdata.NewValue,'Tag')
switch rb_Koerper
case 'rb_q'
    b = Quader(5,5,8);
case 'rb_p'
    b = Pyramide(5,5,8);
case 'rb_d'
    b = Dreieck(5,5,8);
case 'rb_t'
    b = Tetraeder(5,5,8);
case 'rb_o'
    b = Octaeder(5,5,8);
case 'rb_i'
    b = Initiale(5,5,8);
otherwise
        disp('Fehler bei Körperauswahl');
end
Zeichnen(b)
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
addlistener(hObject,'Action', @Schieberegler)
function Schieberegler(hObject, eventdata)
global rb_transfer b R Q
Wert = get(hObject, 'Value');
Wert = Wert - 0.5;
Wert = (Wert)*10;
switch rb_transfer
    case 'rb_tx'
            R = Translation (b, Wert,0,0);
            %Zeichnen (bb, Xo);
    case 'rb_ty'
              R = Translation (b, 0,Wert,0);
              %Zeichnen (bb, Yo);
      case 'rb_tz'
              R = Translation (b, 0,0,Wert);
              %Zeichnen (bb, Zo);
  end
  Zeichnen(R)
  %addlistener(hObject,'Action', @Schieberegler)
% --- Executes during object creation, after setting all properties.
function panel2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to panel2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
set (hObject, 'SelectionChangeFcn', @sel_transformation);
function sel_transformation(source, eventdata)
global rb_transfer
rb_transfer = get(eventdata.NewValue,'Tag')
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1
if (get(h.checkbox1,'Value') == get(h.checkbox1,'Max'))
   global R Q
    Q = R;
    Q = Translation (Q, Wert,Wert,Wert);
%     switch Q
%     case 'rb_tx'
%             Q = Translation (Q, Wert,0,0);
%             %Zeichnen (bb, Xo);
%     case 'rb_ty'
%            
%             Q = Translation (Q, 0,Wert,0);
%             %Zeichnen (bb, Yo);
%     case 'rb_tz'
%             Q = Translation (Q, 0,0,Wert);
%             %Zeichnen (bb, Zo);
% end
% Zeichnen(Q)
else
      Q = b;
      Zeichnen(R)
     % Checkbox is not checked-take approriate action
  end
% --- Executes during object creation, after setting all properties.
function rb_q_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rb_q (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function rb_t_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rb_t (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function rb_tx_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rb_tx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

Function Dreieck:

function body = Dreieck(x,y,z)
     %Punkten 1  2  3  4  5    6
    body.p = [0, x, x, 0, x/2, x/2;...
              0, 0, y, y, y,   0;...
              0, 0, 0, 0, z,   z];
       %Linien  1  2  3  4  5  6  7  8  9
      body.l = [1, 2, 3, 4, 4, 1, 2, 3  6;...
                2, 3, 4, 1, 5, 6, 6, 5, 5]; 

same for the other figures

Function Rotation:

    %Drehung
  function Gedreht = Rotation(grad, Ax, body)
Gedreht = body;
D = zeros(3);
switch Ax
    case 'x'
        D(1,1) = 1            ;
        D(2,2) = cosd(grad)   ;
        D(2,3) = -sind(grad)  ;
        D(3,2) = sind(grad)   ;
        D(3,3) = cosd(grad)   ;
    case 'y'
    case 'z'
end
Gedreht.p = D*body.p 
Function Skalierung:
    %Skalierung
  function skaliertes = Skalierung(body,f)
  skaliertes=body;
  skaliertes.p = f*body.p;

Function Translation:

    %Verschiebung
function Verschiebtes = Translation(body, dX, dY, dZ)
Verschiebtes = body;
for i=1: size(body.p,2)
   Verschiebtes.p (:,i)= body.p (:,i) + [dX; dY;dZ];
  end

Function Zeichnen:

    function Zeichnen(body)
  figure(1)
  clf
  for Linie = 1: size(body.l,2)
      plot3([body.p(1,body.l(1,Linie)), body.p(1,body.l(2,Linie))],...
            [body.p(2,body.l(1,Linie)), body.p(2,body.l(2,Linie))],...
            [body.p(3,body.l(1,Linie)), body.p(3,body.l(2,Linie))]);
        hold on;
  end;
%plot ([10 0], [0 0])
%plot ([-10 0], [0 0])
plot3 ([-10 10], [0 0],[0 0], 'r');
plot3 ([0 0], [-10 10], [0 0], 'g');
plot3 ([0 0], [0 0], [-10 10], 'k'); 

0 Comments

Max

Max (view profile)

Products

No products are associated with this question.

2 Answers

Answer by Jan Simon

Jan Simon (view profile)

on 11 Jul 2011

There are some FEX submissions, which use the mouse for 3D naviagtion: FEX: mouse3d, FEX: view3d, FEX: rotate3D. I think the pure mouse navigation is more natural than using a slider.

0 Comments

Jan Simon

Jan Simon (view profile)

Answer by Walter Roberson

Walter Roberson (view profile)

on 11 Jul 2011

Your function "Rotation" cannot be called in the code you show.

Your function "Translation" is associated with a slider by way of adding a listener to the slider object. The more common way of doing that is to instead set the function as the Callback property of the slider -- for example,

set(hObject, 'CallBack', @Schieberegler)

1 Comment

Jan Simon

Jan Simon (view profile)

on 11 Jul 2011

@Walter: The callback of sliders is triggered, when the mouse is released - at least under Windows. This is very unconvenient for scrolling or moving. The listener is triggered when the slider is moved, such that a direct feedback of the motion can be seen.

Walter Roberson

Walter Roberson (view profile)

Contact us