Increase the elements of a patch

1 view (last 30 days)
Matteo Verardo
Matteo Verardo on 22 Sep 2022
This function plots four facets. As soon as a facets is clicked on, it gives the number of the selected facet and one more random triangle is added. Now if I click on the 5th facet I can't get the index equal to 5.
How can I update "meshTri"?
Any help would be greatly appreciated.
Runnable code:
function select_mesh_facet
% Vedere "C:\funzioni varie\esempi da File Exchange\Callback function for selecting triangular faces of patch objects".
meshTri = [1 1 0 0 1 0 0 0 0; 0 0 0 1 0 0 1 1 0; 2 1 0 1 1 0 1 0 0; 1 0 0 2 0 0 2 1 0]; % Superficie piatta con 4 facce.
X = [meshTri(:,1), meshTri(:,4), meshTri(:,7)]';
Y = [meshTri(:,2), meshTri(:,5), meshTri(:,8)]';
Z = [meshTri(:,3), meshTri(:,6), meshTri(:,9)]';
GrHn = patch('Xdata', X, 'Ydata', Y, 'Zdata', Z, 'FaceColor', [0.5 0.5 0.5], 'EdgeColor', [0.5 0.5 0.5], 'LineWidth', 1, 'FaceAlpha' ,0.2, 'EdgeAlpha', 1);
GrHn.ButtonDownFcn = {@facet_selection, meshTri};
axis equal
function meshTri = facet_selection(src, event, meshTri)
dLim = 1e-6; % Precision for finding planes of faces.
I = event.IntersectionPoint;
vt12 = meshTri(:, 4:6)-meshTri(:, 1:3);
vt13 = meshTri(:, 7:9)-meshTri(:, 1:3);
vt1I= bsxfun(@minus, I, meshTri(:, 1:3));
% check whether the plane of any face contains the point
det = sum(cross(vt1I,vt12).*vt13, 2);
% find points within the plane
pIdx = find(abs(det) < dLim);
% determine barycentric coordinates
bCoord = zeros(numel(pIdx),2); % Sono le coordinate "u" e "v", vedi "C:\mohinga\note\nota 1\nota 1.pdf" --> Barycentric Technique.
for ii = 1:numel(pIdx)
bCoord(ii,:) = ([vt12(pIdx(ii),:)' vt13(pIdx(ii),:)']\vt1I(pIdx(ii),:)')';
end
% find the right face(s)
fIdx = pIdx(all(bCoord >= 0 & bCoord <= 1 & sum(bCoord,2) <= 1, 2));
% Per una questione di precisione, se si clicca sul bordo di un triangolo può succedere che "fIdx" sia empty.
if isempty(fIdx)
fIdx = 0;
end
fIdx
elemNum = size(src.XData,2);
src.XData(:,elemNum+1) = rand(1,3)';
src.YData(:,elemNum+1) = rand(1,3)';
src.ZData(:,elemNum+1) = [0 0 1]';

Answers (0)

Tags

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!