Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab 10: 3D & Projections Advanced
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Tutorial contents 3D primitives Plotting a cube View Transformations basic Projections Orthographic Perspective Isometric View Transformation Advanced
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering 3D Primitives surf(X,Y,Z) surf(...,'PropertyName',PropertyValue) Uses Z for the color data and surface height. X and Y are vectors or matrices defining the x and y components of a surface For a full property list use linklink
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering 3D Primitives patch(X,Y,Z,C) patch('PropertyName',propertyvalue,...) Adds a filled 3-D patch object to the current axes. A patch object is one or more polygons defined by the coordinates of its vertices. The elements of X, Y and Z specify the vertices of a polygon. If X, Y and Z are m-by-n matrices, MATLAB draws n polygons with m vertices. C determines the color of the patch. For a full property list visit linklink
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering 3D Primitives sphere The sphere function generates the x-, y-, and z-coordinates of a unit sphere for use with surf sphere generates a sphere consisting of 20-by-20 faces
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Plotting a cube Create a new.m file and call it “yourname_3D_projection” Create 5 functions (code is given in following slides) Main9() DrawingCB() drawCube ( size ) KeyboardCB(hObject, event) ReshapeCB(ax) In main9 open a new figure with the following properties Black background Position and size - as you wish Name – Yourname 3D projection Number title – off Set keyboard button press callback to be KeyboardCB Call drawingCB();
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Plotting a cube Your code for main9 should look something like this function main9() %main9 demonstarits 3D primitives and Projections Tutorial %initialize clear all; close all; clc; Figureh=figure('NumberTitle','off','Name','3D Projection Example',... 'Position',[ ],'color','k'); DrawingCB(); end
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Plotting a cube In DrawingCB Clear the current figure (use clf) Creat axes with limits [-1 1] in all directions Set background to black Initialize cube size variable to be [1 1 1] Draw the cube Call Reshape function
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Plotting a cube Your code for DrawingCB should look something like this function []=DrawingCB() %initialize variables clf; cubesize=[1,1,1]; %draw Axesh=axes('XLim',[-1 1],'YLim',[-1,1],'ZLim',[-1,1],'Color','k'); cubehandle=drawCube ( cubesize); ReshapeCB(Axesh); end
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Plotting a cube In draw cube function – Input is cube size Initialize vertices around 0 Draw using patch Set different color to each face Set edge color to be white
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Plotting a cube Your code should look something like this function cube=drawCube ( size ) %drawcube draws a cube around (0,0,0) in input size and return the cubes %handle %INPUT: size - 3 element vector [sizex, sizey,sizez) %OUTPUT: cube - a handle of the patches drawn %Initialize cube parameters x=([ ; ; ; ]-0.5)*size(1); y=([ ; ; ; ]-0.5)*size(2); z=([ ; ; ; ]-0.5)*size(3); %Define patches color cdata(:,:,1) = [ ]; cdata(:,:,2) = [ ]; cdata(:,:,3) = [ ]; cube=patch(x,y,z,cdata); set(cube,'edgecolor','w'); end
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Plotting a cube Copy the following code to KeyboardCB – What does it do? function []=KeyboardCB(hObject, event) global ProjType Phi Teta switch event.Key case 'p' if ProjType==1 ProjType=2; else ProjType=1; end case 'leftarrow' Phi=Phi+5; case 'rightarrow' Phi=Phi-5; case 'uparrow' Teta=Teta+5; case 'downarrow' Teta=Teta-5; end DrawingCB(); end
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Plotting a cube Copy the following code to reshape function []=ReshapeCB(ax) global ProjType daspect([1,1,1]); if ProjType==1 else end
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Plotting a cube In main9 define global variables and set their initial value global ProjType Phi Teta ProjType=1; Phi=0; Teta=0;
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Plotting a cube Your output should look like this Question Where is the cube ?
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering View Transformation Basic In order to see the cube in 3D space we need to set the viewpoint Currently we are viewing the cube in perpendicular direction to one of its faces view([az,el]) view([x,y,z]) view(2|3) The position of the viewer (the viewpoint) determines the orientation of the axes. You specify the viewpoint in terms of azimuth and elevation, or by a point in three-dimensional space. view(2) sets the default two-dimensional view, az = 0, el = 90. view(3) sets the default three-dimensional view, az = –37.5, el = 30 – isometric.
View Transformations Basic Lets set the initial view to be isometric Add view(3); in the end of DrawingCB Your Output should look like this Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering
Projections In order to see the effects of changing projection type first make the following alterations to your code: Set face color to none after plotting the patches- set(cube,'facecolor','none'); Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering
Projections The output depends on several parameters Projection type (Orthographic \ Perspective) Projection Parameters (camera position, view angle etc.) The object location in space In order to see the effects of changing projection type we must first set the projection type and then set all of these parameters Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering
Projections Set The projection Type set(ax,'Projection','Orthographic'); OR set(ax,'Projection','Perspective'); Question: We wish to toggle between these two modes by pressing ‘p’ – where should you put this code? Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering
Projections Run the code and press p – do you see the change in projection? In order to see the change better we should look perpendicular to a face Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering
Projections Comment view(3); - %view(3) Run the code again Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering
View Transformations Advanced Now we wish to change the view angle manually by pressing the arrows (up, down, left and right). We can move the camera/eye as we wish Lets move it along a spherical shell Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering
View Transformations Advanced Changing the view manually 24
View Transformations Advanced Lets place the eye on a sphere of radius r: Spherical coordinates: From wikipedia However – our coordinate system is rotated so we replace z with y, x with z, and y with x. 25 z y x
View Transformations Advanced To initialize the view in the –z direction Now all we have left is to find the vector up 26 z y x t
View Transformations Advanced Axes camera properties 'CameraPosition‘ – A 3 element vector containing the x,y,z coordinates of the camera. 'CameraTarget‘ – A 3 element vector containing the x,y,z coordineates of the target (what is the camera looking at) 'CameraUpVector‘ – Camera orientation definition in respects to the vector from the camera to the target. 'CameraViewAngle‘ – The scene range of visibility Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering
View Transformations Advanced Set camera parameters in DrawingCB r=5; CamX=r*sind(Teta)*sind(Phi); CamY=r*cosd(Teta); CamZ=r*sind(Teta)*cosd(Phi); UpX=-cosd(Teta)*sind(Phi);UpY=sind(Teta);UpZ=- cosd(Teta)*cosd(Phi); set(Axesh,'CameraPosition',[CamX,CamY,CamZ],'CameraTarget',[0 0 0],... 'CameraUpVector',[UpX,UpY,UpZ],'CameraViewAngle',40); Dont forget to define the global parameters global Phi Teta Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering
View Transformations Advanced Change some of the parameters and see what happens Change r Change ViewAngle Change CameraTarget Set face color back on and play around Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering
View Transformations Advanced ViewAngle property Defines the range of visibility of the scene (not the distortion) Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering
Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Sources and References Mathworks Documentation Center Further read is highly recommended projections.html projections.html camera-graphics.html camera-graphics.html camera-toolbar.html camera-toolbar.html