Documentation Center

  • Trial Software
  • Product Updates

Transforming a Hierarchy of Objects

This example creates a hierarchy of hgtransform objects, which are then transformed in sequence to create a cube from six squares. The example illustrates how you can parent hgtransform objects to other hgtransform objects to create a hierarchy and how transforming members of a hierarchy affects subordinate members.

The following picture illustrates the hierarchy.

  1. Set the figure Renderer property to zbuffer so MATLAB uses double buffering to prevent flashing during the loop. Set up the figure and the view:

    set(gcf,'Renderer','zbuffer');
    % Set axis limits and view
    set(gca,'XLim',[0 4], 'YLim',[0 4], 'ZLim', [0 3]);
    view(3); axis equal; grid on
  2. Define a hierarchy of hgtransform objects:

    t(1) = hgtransform;
    t(2) = hgtransform('parent',t(1));
    t(3) = hgtransform('parent',t(2));
    t(4) = hgtransform('parent',t(3));
    t(5) = hgtransform('parent',t(4));
    t(6) = hgtransform('parent',t(5));
  3. Create the patch and text objects and parent each pair to the respective hgtransform object.

    The data defining each patch object and the locations of all text objects are the same and are assigned by a single call to set. The objects are then translated to the desired positions on screen.

    % Patch data
    X = [0 0 1 1];
    Y = [0 1 1 0];
    Z = [0 0 0 0];
    % Text data
    Xtext = .5;
    Ytext = .5;
    Ztext = .15;
    % Parent corresponding pairs of objects (patch and text)
    % into the object hierarchy
    p(1) = patch('FaceColor','red','Parent',t(1));
    txt(1) = text('String','Bottom','Parent',t(1));
    p(2) = patch('FaceColor','green','Parent',t(2));
    txt(2) = text('String','Right','Parent',t(2));
    p(3) = patch('FaceColor','blue','Parent',t(3));
    txt(3) = text('String','Back','Color','white','Parent',t(3));
    p(4) = patch('FaceColor','yellow','Parent',t(4));
    txt(4) = text('String','Top','Parent',t(4));
    p(5) = patch('FaceColor','cyan','Parent',t(5));
    txt(5) = text('String','Left','Parent',t(5));
    p(6) = patch('FaceColor','magenta','Parent',t(6));
    txt(6) = text('String','Front','Parent',t(6));
    % Set the patch x, y, and z data
    set(p,'XData',X,'YData',Y,'ZData',Z)
    % Set the position and alignment of the text
    set(txt,'Position',[Xtext Ytext Ztext],...
        'HorizontalAlignment','center',...
        'VerticalAlignment','middle')
  4. Translate the squares (patch objects) to the desired locations. As hgtransform object 2 is translated, all its children (including hgtransform objects 3 through 6) are also translated. Therefore, each translation requires moving the square by only one unit in either the x or y direction. hgtransform object 1 is left at its original position.

    % Set up initial translation transform matrices
    % Translate 1 unit in x
    Tx = makehgtform('translate',[1 0 0]);
    % Translate 1 unit in y
    Ty = makehgtform('translate',[0 1 0]);
    % Set the Matrix property of each hgtransform object (2-6)
    set(t(2),'Matrix',Tx);
    drawnow
    set(t(3),'Matrix',Ty);
    drawnow
    set(t(4),'Matrix',Tx);
    drawnow
    set(t(5),'Matrix',Ty);
    drawnow
    set(t(6),'Matrix',Tx);
  5. Specify the rotation angle and perform transforms:

    % Specify rotation angle (pi/2 radians = 90 degrees)
    fold = pi/2;
    % Rotate -y, translate x
    Ry = makehgtform('yrotate',-fold);
    RyTx = Tx*Ry;
    % Rotate x, translate y
    Rx = makehgtform('xrotate',fold);
    RxTy = Ty*Rx;
  6. Set the transforms and draw after each group transform with a slight pause:

    set(t(6),'Matrix',RyTx);
    pause(.5)
    set(t(5),'Matrix',RxTy);
    pause(.5)
    set(t(4),'Matrix',RyTx);
    pause(.5)
    set(t(3),'Matrix',RxTy);
    pause(.5)
    set(t(2),'Matrix',RyTx);
    pause(.5)
    
Was this topic helpful?