-
Notifications
You must be signed in to change notification settings - Fork 3
/
Screen.m
157 lines (145 loc) · 5.34 KB
/
Screen.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
classdef Screen < Surface
% SCREEN implements a rectangular screen surface
% Detailed explanation goes here
%
% Member functions:
%
% p = Screen( r, w, h, wbins, hbins ) - object constructor
% INPUT:
% r - 1x3 position vector
% w - width
% h - height
% wbins - number of bins in the horizontal direction
% hbins - number of bins in the vertical direction
% OUTPUT:
% p - screen object
%
% p.display() - displays the screen p information
%
% p.draw() - draws the screen p in the current axes
%
% p.rotate( rot_axis, rot_angle ) - rotate the screen p
% INPUT:
% rot_axis - 1x3 vector defining the rotation axis
% rot_angle - rotation angle (radians)
%
% Copyright: Yury Petrov, 2016
%
properties
h = 1; % height
w = 1; % width
hbins = 128; % number of bins along y-axis
wbins = 128; % number of bins along x-axis
image = []; % image on the screen
end
methods
function self = Screen( ar, aw, ah, awbins, ahbins )
if nargin == 0
return;
end
self.R = Inf;
self.k = 0;
self.r = ar;
self.h = ah;
self.w = aw;
self.hbins = round( ahbins );
self.wbins = round( awbins );
end
function display( self )
% describe self
fprintf( 'Position:\t [%.3f %.3f %.3f]\n', self.r );
fprintf( 'Orientation:\t [%.3f %.3f %.3f]\n', self.n );
if ~isinf( self.R )
fprintf( 'Curv. radius:\t %.3f\n', self.R );
end
if self.R ~= 0
fprintf( 'Conic coefficient:\t %.3f\n', self.k );
end
fprintf( 'Width:\t %.3f\n', self.w );
fprintf( 'Height:\t %.3f\n', self.h );
fprintf( 'Width bins:\t %i\n', self.wbins );
fprintf( 'Height bins:\t %i\n', self.hbins );
fprintf( 'Rotation axis:\t [%.3f %.3f %.3f]\n', self.rotax );
fprintf( 'Rotation angle:\t %.3f\n', self.rotang );
end
function n = normal( self )
% draw self
y = linspace( -self.w/2, self.w/2, 3 );
z = linspace( -self.h/2, self.h/2, 3 );
[ y, z ] = meshgrid( y, z );
if isinf( self.R )
x = zeros( size( y ) );
else
r2yz = ( y.^2 + z.^2 ) / self.R^2;
a = 1 + self.k;
if a == 0 % paraboloid, special case
x = r2yz * self.R / 2;
else
x = self.R * r2yz ./ ( 1 + sqrt( 1 - a * r2yz ) );
end
end
S = [ x(:) y(:) z(:) ];
% rotate and shift
if self.rotang ~= 0
S = rodrigues_rot( S, self.rotax, self.rotang );
end
x(:) = S( :, 1 ) + self.r( 1 );
y(:) = S( :, 2 ) + self.r( 2 );
z(:) = S( :, 3 ) + self.r( 3 );
e1 = [ x( 2 ) - x( 1 ), y( 2 ) - y( 1 ), z( 2 ) - z( 1 ) ];
e2 = [ x( 6 ) - x( 1 ), y( 6 ) - y( 1 ), z( 6 ) - z( 1 ) ];
n = cross( e1, e2 );
n = n ./ norm( n );
end
function hndl = draw( self, color, plot_type )
if nargin < 2
color = [ .2 .2 .2 1 ];
end
% draw self
y = linspace( -self.w/2, self.w/2, self.wbins );
z = linspace( -self.h/2, self.h/2, self.hbins );
[ y, z ] = meshgrid( y, z );
if isinf( self.R )
x = zeros( size( y ) );
else
r2yz = ( y.^2 + z.^2 ) / self.R^2;
a = 1 + self.k;
if a == 0 % paraboloid, special case
x = r2yz * self.R / 2;
else
x = self.R * r2yz ./ ( 1 + sqrt( 1 - a * r2yz ) );
end
end
S = [ x(:) y(:) z(:) ];
% rotate and shift
if self.rotang ~= 0
S = rodrigues_rot( S, self.rotax, self.rotang );
end
x(:) = S( :, 1 ) + self.r( 1 );
y(:) = S( :, 2 ) + self.r( 2 );
z(:) = S( :, 3 ) + self.r( 3 );
% draw
if isempty( self.image )
% c = 0.2 * ones( size( z, 1 ), size( z, 2 ) );
c = repmat( reshape( color( 1:3 ), [ 1 1 3 ] ), size( x, 1 ), size( x, 2 ), 1 );
else
c = self.image;
end
%c = repmat( reshape( color( 1:3 ), [ 1 1 3 ] ), size( x, 1 ), size( x, 2 ), 1 );
switch plot_type
case '3D'
hndl = surf( x, y, z, c, 'EdgeColor', 'none', 'FaceLighting','phong', 'FaceColor', 'interp', ...
'AmbientStrength', 0., 'SpecularStrength', 0 ); % dull
case 'XY'
[ ~, hndl ] = contour( x, y, z, [ 0 0 ] );
hndl.LineColor = 'k';
hndl.LineWidth = 2.0;
case 'XZ'
[ ~, hndl ] = contour( x, z, y, [ 0 0 ] );
hndl.LineColor = 'k';
hndl.LineWidth = 2.0;
end
colormap summer;
end
end
end