PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/matlab_tools/Converted/kkshad.m

http://github.com/aludnam/MATLAB
Objective C | 271 lines | 268 code | 3 blank | 0 comment | 63 complexity | b38377a32fc1f836e075f416d3c6cf6f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. %kkshad 'Compute the object surface shading giving a 3D cue '
  2. % This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros kshad.pane file
  3. %
  4. % Parameters:
  5. % InputFile: i1 'Z-buffer', required: 'Distance between view plane and object surface'
  6. % OutputFile: o 'Shading', required: 'Shading information using Phong s model'
  7. % Integer: background 'background', default: 255: 'Image background color (gray level)'
  8. % Integer: Imin ' minimum ', default: 100: 'Minimum intensity of light that reaches the object surface '
  9. % Integer: Imax ' maximum ', default: 244: 'Maximum intensity of light that reaches the object surface '
  10. % InputFile: i2 'Normal Vectors', optional: 'Surface normal vectors'
  11. % Double: Ka 'ambient ', default: 20: 'Coeficient of reflection for the ambient light'
  12. % Double: Kd 'difuse ', default: 80: 'Coeficient of reflection for the difuse light'
  13. % Double: Ks 'specular', default: 20: 'Coficient of reflection for the specular light'
  14. % Integer: n 'n factor', default: 7: 'Falling factor for the specular light'
  15. % Double: alpha 'alpha', default: 0: 'View plane rotation angle around Z axis'
  16. % Double: beta 'beta ', default: 0: 'View plane rotation angle around X axis'
  17. % InputFile: i3 '"Texture"', optional: '"Texture" information to be incorporated on shading'
  18. % Double: Kt '"texture"', default: 0: 'Coficient of texture influence'
  19. %
  20. % Example: o = kkshad({i1, i2, i3}, {'i1','';'o','';'background',255;'Imin',100;'Imax',244;'i2','';'Ka',20;'Kd',80;'Ks',20;'n',7;'alpha',0;'beta',0;'i3','';'Kt',0})
  21. %
  22. % Khoros helpfile follows below:
  23. %
  24. % PROGRAM
  25. % kshad - Compute shading
  26. %
  27. % DESCRIPTION
  28. % Create a shaded image file based on z-buffer and/or normal vectors.
  29. %
  30. % The operator "kshad"
  31. % creates a shaded image file of type KUBYTE from the z-buffer of a volume and
  32. % optionally, also from the normal vectors of the volume.
  33. %
  34. % There are two
  35. % shading possibilities: depth shading and gradient shading. The depth shading
  36. % uses only the z-buffer information from the input file. The gradient shading
  37. % uses the z-buffer and the angles between the normals and the light source
  38. % direction (given by angles alpha and beta) to create the shaded image.
  39. %
  40. % The normal vectors can be computed in three different ways: in the image
  41. % space, in the object space and in the voxel space. Each one will possibly
  42. % give a different shaded image. These vectors must have their directions
  43. % inverted (pointing towards the interior of the object).
  44. %
  45. % The input parameters Imax, Imin, Ka, Kd, Ks, and n, are chosen to
  46. % adjust the gradient shading quality according to the Phong equation (see
  47. % equation below).
  48. % However, the depth shading only needs the Imax and Imin parameters. The
  49. % image background color (gray level) also can be chosen by the user. If the
  50. % texture information is to be added, then the texture file as well as the
  51. % texture coeficient Kt must be given.
  52. %
  53. % Angles alpha and beta estabilish rotations of the light source around z and x
  54. % axes respectively (see figure in man page of kzbuff), and they are
  55. % recommended to be
  56. % the same used to generate the z-buffer image (so that observer and light
  57. % source have the same position), avoiding problems with shadowing, not
  58. % implemented. Different values of alpha and beta can be used without
  59. % restrictions when there is only one object and its visible surface is
  60. % convex. The rotations are made firstly around x and then around z.
  61. %
  62. % Depth Shading Intensity (Id):
  63. % Id(u,v) = [(Imax-Imin)/(dmin-dmax)] * [d(u,v)-dmax] + Imin
  64. % where
  65. % d corresponds to the distance in the z-buffer image
  66. %
  67. % Gradient Shading Intensity (Ig):
  68. % Ig(u,v) = Imax*Ka + Id(u,v)*{kd*cos[theta(u,v)] + Ks*[cos(2*theta(u,v))^n] +
  69. % Kt*T(u,v)}
  70. % where
  71. % theta is the angle between the surface nomal and the ray of light;
  72. % Ka is the coeficient of reflection for the ambient light;
  73. % Kd is the coeficient of diffuse reflection;
  74. % Ks is the coeficient of specular reflection;
  75. % Kt is the coeficient of texture;
  76. % T is the optional texture information;
  77. %
  78. %
  79. %
  80. % EXAMPLES
  81. % kshad -i1 zbuffer.viff -i2 normals.viff -o image.viff -Ka 30 -Kd 80 -Ks 20
  82. % -n 7 -Imax 220 -Imin 100 -background 255 -alpha 0 -beta 0
  83. %
  84. % The program will create from the zbuffer.viff and normals.viff files an image
  85. % represented by the
  86. % image.viff file using the gradient shading. The gradient
  87. % shading will use 30% (Ka) of reflection coeficient for the ambient light,
  88. % 80% (Kd) of difuse reflection coeficient and 20% (Ks) of specular reflection
  89. % coeficient for the light source. The maximum (Imax) and minimum (Imin)
  90. % intensity of the light source reflected on the object surface will be
  91. % respectively 220 and 100. The falling factor (n) for the specular reflection
  92. % light component will be 7 and the image background color (gray level) will
  93. % be 255. The zbuffer.viff image was generated by a view plane rotated zero
  94. % degree around x axis and zero degree around z axis, therefore the position
  95. % of the light source is given by setting angles alpha and beta to zero.
  96. %
  97. % "SEE ALSO"
  98. % kzbuff, kisnorm, kvsnorm, ktextu, kvoxext
  99. %
  100. % RESTRICTIONS
  101. % The input objects must have only the value segment.
  102. %
  103. % The z-buffer and texture
  104. % input objects can not have dimention e > 1. The input object normal must have
  105. % dimention e=3.
  106. %
  107. % The input objects must match in their dimentions w and h.
  108. %
  109. % The input objects can not be of data types KBIT and KCOMPLEX.
  110. %
  111. % In case of t > 1 in the input objects, the operator will be applied to the time
  112. % t=0 only.
  113. %
  114. % None of the input and output objects are referenced, therefore some attributes
  115. % may change, as the VALUE_POSITION, for example.
  116. %
  117. % REFERENCES
  118. %
  119. % COPYRIGHT
  120. % Copyright (C) 1993, 1994, 1995 UNICAMP, R A Lotufo, All rights reserved.
  121. %
  122. function varargout = kkshad(varargin)
  123. if nargin ==0
  124. Inputs={};arglist={'',''};
  125. elseif nargin ==1
  126. Inputs=varargin{1};arglist={'',''};
  127. elseif nargin ==2
  128. Inputs=varargin{1}; arglist=varargin{2};
  129. else error('Usage: [out1,..] = kkshad(Inputs,arglist).');
  130. end
  131. if size(arglist,2)~=2
  132. error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
  133. end
  134. narglist={'i1', '__input';'o', '__output';'background', 255;'Imin', 100;'Imax', 244;'i2', '__input';'Ka', 20;'Kd', 80;'Ks', 20;'n', 7;'alpha', 0;'beta', 0;'i3', '__input';'Kt', 0};
  135. maxval={0,0,255,255,255,1,100,100,100,200,0,0,1,100};
  136. minval={0,0,0,0,0,1,0,0,0,0,0,0,1,0};
  137. istoggle=[0,0,1,1,1,1,1,1,1,1,1,1,1,1];
  138. was_set=istoggle * 0;
  139. paramtype={'InputFile','OutputFile','Integer','Integer','Integer','InputFile','Double','Double','Double','Integer','Double','Double','InputFile','Double'};
  140. % identify the input arrays and assign them to the arguments as stated by the user
  141. if ~iscell(Inputs)
  142. Inputs = {Inputs};
  143. end
  144. NumReqOutputs=1; nextinput=1; nextoutput=1;
  145. for ii=1:size(arglist,1)
  146. wasmatched=0;
  147. for jj=1:size(narglist,1)
  148. if strcmp(arglist{ii,1},narglist{jj,1}) % a given argument was matched to the possible arguments
  149. wasmatched = 1;
  150. was_set(jj) = 1;
  151. if strcmp(narglist{jj,2}, '__input')
  152. if (nextinput > length(Inputs))
  153. error(['Input ' narglist{jj,1} ' has no corresponding input!']);
  154. end
  155. narglist{jj,2} = 'OK_in';
  156. nextinput = nextinput + 1;
  157. elseif strcmp(narglist{jj,2}, '__output')
  158. if (nextoutput > nargout)
  159. error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']);
  160. end
  161. if (isempty(arglist{ii,2}))
  162. narglist{jj,2} = 'OK_out';
  163. else
  164. narglist{jj,2} = arglist{ii,2};
  165. end
  166. nextoutput = nextoutput + 1;
  167. if (minval{jj} == 0)
  168. NumReqOutputs = NumReqOutputs - 1;
  169. end
  170. elseif isstr(arglist{ii,2})
  171. narglist{jj,2} = arglist{ii,2};
  172. else
  173. if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
  174. error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
  175. end
  176. if (minval{jj} ~= 0 | maxval{jj} ~= 0)
  177. if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
  178. error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
  179. elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
  180. error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
  181. elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
  182. error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
  183. elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
  184. error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
  185. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
  186. error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
  187. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
  188. error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
  189. end
  190. end
  191. end
  192. if ~strcmp(narglist{jj,2},'OK_out') & ~strcmp(narglist{jj,2},'OK_in')
  193. narglist{jj,2} = arglist{ii,2};
  194. end
  195. end
  196. end
  197. if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
  198. error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
  199. end
  200. end
  201. % match the remaining inputs/outputs to the unused arguments and test for missing required inputs
  202. for jj=1:size(narglist,1)
  203. if strcmp(paramtype{jj}, 'Toggle')
  204. if (narglist{jj,2} ==0)
  205. narglist{jj,1} = '';
  206. end;
  207. narglist{jj,2} = '';
  208. end;
  209. if ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
  210. narglist{jj,1} = '';
  211. narglist{jj,2} = '';
  212. end;
  213. if strcmp(narglist{jj,2}, '__input')
  214. if (minval{jj} == 0) % meaning this input is required
  215. if (nextinput > size(Inputs))
  216. error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']);
  217. else
  218. narglist{jj,2} = 'OK_in';
  219. nextinput = nextinput + 1;
  220. end
  221. else % this is an optional input
  222. if (nextinput <= length(Inputs))
  223. narglist{jj,2} = 'OK_in';
  224. nextinput = nextinput + 1;
  225. else
  226. narglist{jj,1} = '';
  227. narglist{jj,2} = '';
  228. end;
  229. end;
  230. else
  231. if strcmp(narglist{jj,2}, '__output')
  232. if (minval{jj} == 0) % this is a required output
  233. if (nextoutput > nargout & nargout > 1)
  234. error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']);
  235. else
  236. narglist{jj,2} = 'OK_out';
  237. nextoutput = nextoutput + 1;
  238. NumReqOutputs = NumReqOutputs-1;
  239. end
  240. else % this is an optional output
  241. if (nargout - nextoutput >= NumReqOutputs)
  242. narglist{jj,2} = 'OK_out';
  243. nextoutput = nextoutput + 1;
  244. else
  245. narglist{jj,1} = '';
  246. narglist{jj,2} = '';
  247. end;
  248. end
  249. end
  250. end
  251. end
  252. if nargout
  253. varargout = cell(1,nargout);
  254. else
  255. varargout = cell(1,1);
  256. end
  257. global KhorosRoot
  258. if exist('KhorosRoot') && ~isempty(KhorosRoot)
  259. w=['"' KhorosRoot];
  260. else
  261. if ispc
  262. w='"C:\Program Files\dip\khorosBin\';
  263. else
  264. [s,w] = system('which cantata');
  265. w=['"' w(1:end-8)];
  266. end
  267. end
  268. [varargout{:}]=callKhoros([w 'kshad" '],Inputs,narglist);