


paints facets of a polytope
Syntax: paintFacet(patchHandleVector, facetNormalMatrix, colorMatrix,)
A typical call could be:
paintFacet(get(gca, 'Children), cdef.A, [1 0 0])
to paint all facets of the crystal definition in red for a polytope
that is just viewed in a new figure.
The rows of colorMatrix define the colors of facets in the order
specified in facetNormalMatrix
See also: viewPolytope

0001 function paintFacet(patchHandleVector, facetNormalMatrix, colorMatrix, varargin) 0002 % paints facets of a polytope 0003 % 0004 % Syntax: paintFacet(patchHandleVector, facetNormalMatrix, colorMatrix,) 0005 % 0006 % A typical call could be: 0007 % paintFacet(get(gca, 'Children), cdef.A, [1 0 0]) 0008 % to paint all facets of the crystal definition in red for a polytope 0009 % that is just viewed in a new figure. 0010 % The rows of colorMatrix define the colors of facets in the order 0011 % specified in facetNormalMatrix 0012 % 0013 % See also: viewPolytope 0014 0015 % The elk-library: convex geometry applied to crystallization modeling. 0016 % Copyright (C) 2012 Alexander Reinhold 0017 % 0018 % This program is free software: you can redistribute it and/or modify it 0019 % under the terms of the GNU General Public License as published by the 0020 % Free Software Foundation, either version 3 of the License, or (at your 0021 % option) any later version. 0022 % 0023 % This program is distributed in the hope that it will be useful, but 0024 % WITHOUT ANY WARRANTY; without even the implied warranty of 0025 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0026 % General Public License for more details. 0027 % 0028 % You should have received a copy of the GNU General Public License along 0029 % with this program. If not, see <http://www.gnu.org/licenses/>. 0030 0031 0032 %% make color matrix if only a single color is specified 0033 if numel(colorMatrix) == 1 0034 colorMatrix(size(facetNormalMatrix, 1), :) = colorMatrix; 0035 elseif (size(colorMatrix, 1) == 3 && size(colorMatrix, 2) == 1) || ... 0036 (size(colorMatrix, 1) == 1 && size(colorMatrix, 2) == 3) 0037 tmp = colorMatrix; 0038 colorMatrix(1:size(facetNormalMatrix, 1), 1) = tmp(1); 0039 colorMatrix(1:size(facetNormalMatrix, 1), 2) = tmp(2); 0040 colorMatrix(1:size(facetNormalMatrix, 1), 3) = tmp(3); 0041 elseif size(colorMatrix, 1) == size(facetNormalMatrix, 1) && ... 0042 size(colorMatrix, 2) == 3 0043 %everything OK 0044 else 0045 error('elk:polytope:wrongInput', ... 0046 'the color matrix is wrong, try "help paintFacet"'); 0047 end 0048 0049 % ensure only patches are in the handles 0050 patchHandleVector(~strcmp(get(patchHandleVector, 'type'), 'patch')) = []; 0051 0052 patchX = []; 0053 patchY = []; 0054 patchZ = []; 0055 for iPatch = 1:length(patchHandleVector) 0056 patchX = [patchX; get(patchHandleVector(iPatch), 'XData')]; 0057 patchY = [patchY; get(patchHandleVector(iPatch), 'YData')]; 0058 patchZ = [patchZ; get(patchHandleVector(iPatch), 'ZData')]; 0059 end 0060 0061 origin = [(max(patchX) + min(patchX))/2; ... 0062 (max(patchY) + min(patchY))/2; ... 0063 (max(patchZ) + min(patchZ))/2]; 0064 0065 %% cycle through facets and move the patches coordinates so that 0066 % origin is in the middle of the polytope 0067 for iPatch = 1:length(patchHandleVector) 0068 patchX = get(patchHandleVector(iPatch), 'XData'); 0069 patchX = patchX - origin(1) * ones(size(patchX)); 0070 patchY = get(patchHandleVector(iPatch), 'YData'); 0071 patchY = patchY - origin(2) * ones(size(patchY)); 0072 patchZ = get(patchHandleVector(iPatch), 'ZData'); 0073 patchZ = patchZ - origin(3) * ones(size(patchZ)); 0074 0075 % distances of each point from each facet 0076 idx = facetNormalMatrix*[patchX';patchY';patchZ']; 0077 0078 % This checks if all distances for one facet have the same positive 0079 % value, meaning they are on that facet. 0080 % The value should be positive as the origin is in the middle of 0081 % the polytope. Therefore, if the values are the same and positive, 0082 % idx will be zero everywhere for that facet, if all values are same 0083 % and negative, they will have twice the original value 0084 idx = idx - abs(idx(:,1)) * ones([1, size(idx,2)]); 0085 0086 % find the rows with all values equal to zero 0087 idx = abs(idx) < 1e-6; 0088 idx = prod(double(idx), 2); 0089 0090 % take first valid facet 0091 idx = find(idx, 1, 'first'); 0092 0093 % color patch 0094 if isempty(idx),continue;end 0095 set(patchHandleVector(iPatch), 'FaceColor', colorMatrix(idx, :), varargin{:}); 0096 end