-
Notifications
You must be signed in to change notification settings - Fork 21
/
Plane.m
125 lines (116 loc) · 4.3 KB
/
Plane.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
classdef Plane < Surface
% PLANE implements a planar refracting or reflecting surface, e.g. one
% face of a prism or a mirror
%
% Member functions:
%
% p = Plane( r, D, glass ) - circular planar surface constructor
% INPUT:
% r - 1x3 position vector
% D - diameter, 1x1 vector (outer) or 2x1 vector (inner, outer)
% glass - 1 x 3 cell array of two strings, e.g., { 'air' 'acrylic' }
% OUTPUT:
% p - plane object
%
% p = Plane( r, w, h, glass ) - rectangular planar surface constructor
% INPUT:
% r - 1x3 position vector
% w - width
% h - height
% glass - 1 x 3 cell array of two strings, e.g., { 'air' 'acrylic' }
% OUTPUT:
% p - plane object
%
% p.display() - displays the plane p information
%
% p.draw() - draws the plane p in the current axes
%
% p.rotate( rot_axis, rot_angle ) - rotate the plane p
% INPUT:
% rot_axis - 1x3 vector defining the rotation axis
% rot_angle - rotation angle (radians)
%
% Copyright: Yury Petrov, 2016
%
properties
w = [] % width of the surface
h = [] % height of the surface
D = [] % lens diameter (inner, outer)
end
methods
function self = Plane( varargin ) % some function overloading here
if nargin == 0
return;
elseif nargin == 3
self.r = varargin{1};
aD = varargin{2};
if size( aD, 1 ) < size( aD, 2 )
aD = aD';
end
if size( aD, 1 ) == 1
aD = [ 0; aD ];
end
self.D = aD;
self.glass = varargin{3};
elseif nargin == 4
self.r = varargin{1};
self.w = varargin{2};
self.h = varargin{3};
self.glass = varargin{4};
end
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 ~isempty( self.w ) && ~isempty( self.h )
fprintf( 'Width:\t %.3f\n', self.w );
fprintf( 'Height:\t %.3f\n', self.h );
elseif ~isempty( self.D )
fprintf( 'Diameter:\t %.3f\n', self.D(2) );
if self.D(1) ~= 0
fprintf( 'Inner diameter:\t %.3f\n', self.D(1) );
end
end
fprintf( 'Rotation axis:\t [%.3f %.3f %.3f]\n', self.rotax );
fprintf( 'Rotation angle:\t %.3f\n', self.rotang );
fprintf( 'Material:\t %s | %s\n', self.glass{ 1 }, self.glass{ 2 } );
end
function h = draw( self, color )
% draw self
if nargin < 2
color = [ 1 1 1 .5 ];
end
if ~isempty( self.w == 0 ) && ~isempty( self.h )
y = [-self.w/2 self.w/2 ];
z = [-self.h/2 self.h/2 ];
[ y, z ] = meshgrid( y, z );
elseif ~isempty( self.D )
nrad = 50;
if length( self.D ) == 1
rad = linspace( 0, self.D / 2, nrad );
else
rad = linspace( self.D(1)/2, self.D(2) / 2, nrad );
end
nang = 50;
ang = linspace( 0, 2 * pi, nang );
[ ang, rad ] = meshgrid( ang, rad );
[ y, z ] = pol2cart( ang, rad );
end
x = zeros( size( y ) );
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
c = repmat( reshape( color( 1:3 ), [ 1 1 3 ] ), size( x, 1 ), size( x, 2 ), 1 );
h = surf( x, y, z, c, ...
'EdgeColor', 'none', 'FaceLighting','phong', 'FaceColor', 'interp', 'FaceAlpha', color(4), ...
'AmbientStrength', 0., 'SpecularStrength', 1 ); % grey color, shiny
end
end
end