function [arr] = flipmesh(p, t, axis) % [arr] = flipmesh(p, t, axis) % flips the mesh around the horizontal/vertical axis % p, t triangle coordinates and positions % axis is either 'vertical' or 'horizontal' % p: x = -4:+4 (p(:,1)) % y = -2:+2 (p(:,2)) % the following parameters will have to be adjusted to the actual mesh % geometry. Find 3 triangles that share a unique point that is at the % mirror x axis or y axis. ty_ax = [109 53 108; 30, 29, 6 ]; tx_ax = [233 234 123; 293 291 291; 292 291 291]; % y axis: common points of 3 corner triangles p_y = [ common_point(t, ty_ax(1,:)) common_point(t, ty_ax(2,:)) ]; % x axis: dorsal: as y axis, ventral: common in triangles 293 and 291 but not in 292 pi_x = setdiff(common_point(t, tx_ax(2,:)), common_point(t, tx_ax(3,:))); p_x = [ common_point(t, tx_ax(1,:)) pi_x ]; y_ax = mean(p(p_y, 2)); x_ax = mean(p(p_x, 1)); cent = zeros(length(t), 2); for i = 1:length(t) cent(i, :) = mean(p(t(i,:),1:2)); end arr = zeros(length(t),1); for i = 1:length(t), if length(find(arr == i)) > 0, continue; end switch axis case 'vertical' dist = cent(i, 2) - y_ax; mcent = y_ax - dist; comp_cent = abs(cent - repmat([cent(i,1) mcent], [length(t), 1])); case 'horizontal' dist = cent(i, 1) - x_ax; mcent = x_ax - dist; comp_cent = abs(cent - repmat([mcent, cent(i,2)], [length(t), 1])); end [cent_min, cent_pos] = min(sum(comp_cent, 2)); arr(i) = cent_pos; arr(cent_pos) = i; end % i = 22; % test with # 22 (should be 56 around y axis (vertical)) % dist = cent(i, 2) - y_ax; % mcent = y_ax - dist; % comp_cent = abs(cent - repmat([cent(i,1) mcent], [length(t), 1])); % [cent_min, cent_pos] = min(sum(comp_cent, 2)) function [pt] = common_point(t, triangle_no) % finds common point in all triangles specified in vector triangle_no pt = t(triangle_no(1),:); for i = 2:length(triangle_no), pt = intersect(pt, t(triangle_no(i),:)); end