written by: itzik ben shabat technion - israel institute of technology faculty of mechanical...

16
Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab 6 : Transformations Lab 6 : Transformations using using MATLAB MATLAB

Upload: roland-walters

Post on 22-Dec-2015

218 views

Category:

Documents


4 download

TRANSCRIPT

Written by: Itzik Ben Shabat

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Lab 6 : Transformations using Lab 6 : Transformations using MATLAB MATLAB

Written by: Itzik Ben Shabat

Implementation of Dry Exercise

Using MATLAB

Transform a Square with parameters width =1 Height=1 Center at (0,0)

Into a rectangle with the following parameters Width=0.5 Height=2 Center at (2,2) Rotated around Z axis by 45 deg

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

Straightforward Implementation

We wish to transform the blue square into the red rectangle

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

Implementation of Dry Exercise

Solution outline Calculate square vertices and initialize parameters Plot square Convert vertices to homogeneous coordinates Matrix multiplication (notice ordering) plot rectangle

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

Straightforward Implementation

Open a new .m file and Create a function called main6 which opens a new figure and axis in it and calls the drawingCB

function [ ] = main6( )

%main6 Demonstrates the use of Basic Transformations using matrix

%multiplications

clear all; close all; clc;

%code

%Open figure and create axis

Figureh=figure('NumberTitle','off','Name','Transformation Example',...

'Position',[200 200 500 500]); %bg is set to red so we know that we can only see the axes

Axesh=axes('XLim',[-3 3],'YLim',[-3,3]);

DrawingCB( );

end

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

Straightforward Implementation

Create a function called drawingCB which performs as the solution outline demonstrated

Calculate square vertices and initialize parameters%Initializing Variables

square=[-0.5 -0.5;-0.5 0.5;0.5 0.5;0.5 -0.5]; %represented by its vertices

Sx=0.5;

Sy=2;

Tx=2;

Ty=2;

teta=45;

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

Straightforward Implementation

Plot square

line([square(:,1);square(1,1)],[square(:,2);square(1,2)],'Color','b','LineWidth',3);

Convert to homogeneous coordinates

Hsquare=[square, ones(size(square,1),1)];

Generate MatrixS=[Sx 0 0;0 Sy 0 ;0 0 1];

T=[1 0 Tx;0 1 Ty; 0 0 1];

R=[cos(teta) -sin(teta) 0;sin(teta) cos(teta) 0; 0 0 1];

Question: What is the right matrix order of multiplication?

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

Straightforward Implementation

Answer: scale first, then rotate, then translate.%Calculate rectangle vertices

Hrect=T*R*S*Hsquare';

Hrect=Hrect';

line([Hrect(:,1);Hrect(1,1)],[Hrect(:,2);Hrect(1,2)],'Color','r','LineWidth',3);

grid on;

Result:

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

Straightforward Implementation

Lets explore what are the results if we used a different order:

Question: Can you guess which order is represented in green and which in yellow ?

Hint : what is special about the yellow?

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

Straightforward Implementation

Answer:

green – R*S*T

Yellow - S*R*T

(the scaling after rotation

creates the distortion)

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

MATALB Implementation – The right way

In previous section we explored the implementation of transformations using matrix multiplication

In MATLAB there are built in routines that make our lives easier ( no need to define the entire matrix and multiply it or transform to homogeneous coordinates)

Makehgtform Create transform matrices for translation, scaling, and rotation of graphics

objects

M = makehgtform('translate',[tx ty tz]) - TranslationM = makehgtform('scale',s) - Uniform scaleM = makehgtform('scale',[sx,sy,sz])  - nonuniform scaleM = makehgtform('xrotate',t)  - rotate around x axis – t in radiansM = makehgtform('yrotate',t)  - rotate around y axis – t in radiansM = makehgtform('zrotate',t) - rotate around z axis – t in radians

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

MATALAB Transformation implementation

h = hgtransform('PropertyName',propertyvalue,...)

We will mainly use

h = hgtransform(‘Parent’,ax,‘matrix’,M);

Executes the transformation defined by the matrix M on all children (graphic objects) of ax. Ax can be an axes, hggroup or hgtransform objects

For more information on hgtransform properties visit this link

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

MATALAB Transformation implementation

Implement in previous section example Guidance:

Plot 2 squares and save their handles Calculate matrix Set hgtform object to parent one of the

squares Refresh the figure using drawnow

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

MATALAB Transformation implementation

Your Final Drawing function should look something like this

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

Final notes

We recommend that you use this code to experiment and further investigate the nature of transformations

Do not use the straightforward implementation for standard transformation in future codes (it is only an example to help you understand the principles)

It is important to understand by now the object handles principles (figure,axis, graphic objects, groups etc.)

It is important to understand child-parent relations between objects (visit link) – more on this in hierarchy tutorial

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering

Written by: Itzik Ben Shabat

Sources and Refrences

Mathworks Documentation Center - http://www.mathworks.com/help/documentation-center.html

Technion - Israel Institute of TechnologyFaculty of Mechanical Engineering

Laboratory for CAD & Lifecycle Engineering