MATLAB Answers

Is there a way to control distortion in Matlab's 3D viewer?

14 views (last 30 days)
Theo
Theo on 9 Oct 2015
Answered: Bjorn Gustavsson on 9 Oct 2015
The background of this problem relates to my attempt to combine output from a ray tracer with Matlab's 3d plotters. When doing ray tracing, there is no need to apply a perspective transformation to the rendered image. You see this in the image below. Basically, the intersections of the rays with the viewport will automatically adjust for the perspective scaling.
Suppose I've gone and created a ray-traced image (so I am given my camera, my focal length, viewport dimensions, etc.). How do I create exactly the same view in Matlab's 3d plotting environment?
Here is an example:
clear
close all
evec = [0 200 300]; % Camera position
recw = 200; % cm width of box
recl = 200; % cm length of box
h = 150; % cm height of box
% Create the front face rectangle
front = zeros(3,5);
front(:,1) = [-recw/2; 0; -recl/2];
front(:,2) = [recw/2; 0; -recl/2];
front(:,3) = [recw/2; h; -recl/2];
front(:,4) = [-recw/2; h; -recl/2];
front(:,5) = front(:,1);
% Back face rectangle
back = zeros(3,5);
back(:,1) = [-recw/2; 0; recl/2];
back(:,2) = [recw/2; 0; recl/2];
back(:,3) = [recw/2; h; recl/2];
back(:,4) = [-recw/2; h; recl/2];
back(:,5) = back(:,1);
% Plot the world view
figure(1);
patch(front(1,:), front(2,:), front(3,:), 'r'); hold all
patch(back(1,:), back(2,:), back(3,:), 'b');
plot3(evec(1), evec(2), evec(3), 'bo');
xlabel('x'); ylabel('y'); zlabel('z');
title('world view'); view([-30 40]);
% Plot the camera view
figure(2);
patch(front(1,:), front(2,:), front(3,:), 'r'); hold all
patch(back(1,:), back(2,:), back(3,:), 'b');
xlabel('x'); ylabel('y'); zlabel('z');
title('Camera view');
campos(evec);
camup([0 1 0]); % Up vector is y+
camproj('perspective');
camtarget([evec(1), evec(2), 0]);
title('camera view');
Now you see the world view
and the camera view
I know how to adjust the camera position, the camera view angle, and orientation to match the output from my ray tracer. However, I do not know how to adjust Matlab's built-in perspective command
camproj('perspective')
for different distortions.
Note: within the documentation, there is the viewmtx command, which allows you to output a transformation matrix corresponding to a perspective distortion of a certain angle. This is not quite what I want. I want to do things in 3D and through Matlab's OpenGL viewer. In essence, I want a command like
camproj('perspective', distortionamount)
so I can match up the amount of distortion in Matlab's viewer with the distortion from the ray tracer. If you use the viewmtx command to create the 2D projections, you will not be able to use patch or surf and keep colours and faces intact. You would essentially have to do some complicated guff to keep the depths sorted, whereas within Matlab's 3d plotter, this is already handled.

  0 Comments

Sign in to comment.

Answers (2)

Mike Garrity
Mike Garrity on 9 Oct 2015
I think that the CameraViewAngle property is what you're looking for. You mentioned it as one of the things you know how to adjust, but I don't see it in your example.
What do the view parameters of your raytracer look like? Instead of view angle, they might have focal length. You can convert between the two using tan/arctan, as shown here .

  1 Comment

Theo
Theo on 9 Oct 2015
Maybe I am not thinking correctly, but I don't think that camera view angle is the same. You can see in the camera graphics terminology part of the documentation that the camva command controls the dimensions of the viewport.
In the ray-tracer, I do specify both a horizontal field of view (the angle subtended by the left-and-right viewport edges) and the vertical field of view. Coupled with a focal length, this determines the size of the viewport.
Perspective distortion seems to be something different, but maybe the quantities are all related somehow (that is to say, given a pinhole location and focal length for the ray tracer, this corresponds to a different camera location and CameraViewAngle when the Matlab perspective command is applied). I set Matlab's CameraViewAngle to equal the vertical field of view. However, the perspective still seems off.

Sign in to comment.


Bjorn Gustavsson
Bjorn Gustavsson on 9 Oct 2015
Matlab uses only a pinhole camera model for their 3-D rendering in the perspective mode, and pinhole cameras has strictly speaking no distortion at all. The plot you show for your ray-tracing indicates that it too uses the pinhole camera model, so there should be no distortion there either.
HTH

  0 Comments

Sign in to comment.

Sign in to answer this question.