annotate scripts/plot/appearance/specular.m @ 20830:b65888ec820e draft default tip gccjit

dmalcom gcc jit import
author Stefan Mahr <dac922@gmx.de>
date Fri, 27 Feb 2015 16:59:36 +0100
parents 83792dd9bcc1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19898
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 18400
diff changeset
1 ## Copyright (C) 2009-2015 Kai Habel
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
2 ##
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
3 ## This file is part of Octave.
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
4 ##
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
8 ## your option) any later version.
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
9 ##
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
13 ## General Public License for more details.
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
14 ##
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
18
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
19 ## -*- texinfo -*-
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
20 ## @deftypefn {Function File} {} specular (@var{sx}, @var{sy}, @var{sz}, @var{lv}, @var{vv})
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
21 ## @deftypefnx {Function File} {} specular (@var{sx}, @var{sy}, @var{sz}, @var{lv}, @var{vv}, @var{se})
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
22 ## Calculate the specular reflection strength of a surface defined by the
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
23 ## normal vector elements @var{sx}, @var{sy}, @var{sz} using Phong's
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
24 ## approximation.
17122
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14138
diff changeset
25 ##
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
26 ## The light source location and viewer location vectors are specified using
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
27 ## parameters @var{lv} and @var{vv} respectively. The location vectors can
17122
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14138
diff changeset
28 ## given as 2-element vectors [azimuth, elevation] in degrees or as 3-element
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14138
diff changeset
29 ## vectors [x, y, z].
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14138
diff changeset
30 ##
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
31 ## An optional sixth argument specifies the specular exponent (spread) @var{se}.
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
32 ## If not given, @var{se} defaults to 10.
17122
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14138
diff changeset
33 ## @seealso{diffuse, surfl}
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
34 ## @end deftypefn
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
35
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
36 ## Author: Kai Habel <kai.habel@gmx.de>
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
37
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
38 function retval = specular (sx, sy, sz, lv, vv, se)
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
39
8460
e4c9ecb64411 diffuse.m, specular.m, surfl.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8450
diff changeset
40 if (nargin < 5 || nargin > 6)
e4c9ecb64411 diffuse.m, specular.m, surfl.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8450
diff changeset
41 print_usage ();
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
42 endif
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
43
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
44 ## Check normal vectors
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
45 if (! size_equal (sx, sy, sz))
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
46 error ("specular: SX, SY, and SZ must be the same size");
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
47 endif
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
48
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
49 ## Check light vector (lv) argument
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
50 if (! isvector (lv) || length (lv) < 2 || length (lv) > 3)
8460
e4c9ecb64411 diffuse.m, specular.m, surfl.m: style fixes
John W. Eaton <jwe@octave.org>
parents: 8450
diff changeset
51 error ("specular: light vector LV must be a 2- or 3-element vector");
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
52 elseif (length (lv) == 2)
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
53 [lv(1), lv(2), lv(3)] = sph2cart (lv(1) * pi/180, lv(2) * pi/180, 1.0);
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
54 endif
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
55
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
56 ## Check view vector (vv) argument
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
57 if (! isvector (vv) || length (vv) < 2 || length (lv) > 3)
10635
d1978e7364ad Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents: 9040
diff changeset
58 error ("specular: view vector VV must be a 2- or 3-element vector");
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
59 elseif (length (vv) == 2)
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
60 [vv(1), vv(2), vv(3)] = sph2cart (vv(1) * pi / 180, vv(2) * pi / 180, 1.0);
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
61 endif
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
62
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
63 ## Check specular exponent (se) argument
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
64 if (nargin < 6)
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
65 se = 10;
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
66 elseif (! (isnumeric (se) && numel (se) == 1 && se > 0))
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
67 error ("specular: exponent SE must be a positive scalar");
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
68 endif
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
69
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
70 ## Normalize view and light vectors
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
71 if (sum (abs (lv)) > 0)
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
72 lv /= norm (lv);
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
73 endif
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
74 if (sum (abs (vv)) > 0)
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
75 vv /= norm (vv);
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
76 endif
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
77
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
78 ## Calculate normal vector lengths and dot-products
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
79 ns = sqrt (sx.^2 + sy.^2 + sz.^2);
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
80 l_dot_n = (sx * lv(1) + sy * lv(2) + sz * lv(3)) ./ ns;
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
81 v_dot_n = (sx * vv(1) + sy * vv(2) + sz * vv(3)) ./ ns;
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
82
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
83 ## Calculate specular reflection using Phong's approximation
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
84 retval = 2 * l_dot_n .* v_dot_n - dot (lv, vv);
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
85
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
86 ## Set reflectance to zero if light is on the other side
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
87 retval(l_dot_n < 0) = 0;
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
88
18400
925bdd86a479 specular.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
89 ## Allow postive values only
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
90 retval(retval < 0) = 0;
20441
83792dd9bcc1 Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents: 19898
diff changeset
91 retval .^= se;
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
92
8450
6c4e2fc14beb Add new 3D plotting function surfl. Add light functions diffuse and specular
Kai Habel
parents:
diff changeset
93 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17122
diff changeset
94