我想我找到了一个可行但不优雅的解决方案——在这个版本中,我生成了 2D 内核,并将其嵌套在 3D 数组中。然后我使用旋转操作符将某个位置的适当值映射到旋转变换。这很丑陋,但似乎有效。
%generate a test 2d kernel as before...
A = 625; B = 25; c = 51; cen = 151; grid2d = zeros(101,101);
for m = 1:101
for n = 1:101
r(m,n) = sqrt((m - c).^2 + (n - c).^2 ); %distance of point on grid to centre
%Populating the grid as a kernal
if r(m,n) <= 5
grid2d(m,n) = 100;
elseif r(m,n) >= 25
grid2d(m,n) = 0;
else
grid2d(m,n) = A./r(m,n) - B;
end
end
end
%make a bigger array for this to nest in
flat3d = zeros(301,301,301);
range = 101:201; %centre range of new grid
flat3d(range,range,cen) = grid2d; % inserts grid2d at centre of new flat3d grid!
%now the incline angles
theta = input('Enter incline in XZ (vertical) plane in degrees : ' );
phi = input('Enter incline in XY (horizontal) plane in degrees : ' );
%Now a loop which inclines each point relative to theta around (x,y,z) = (cen,cen,cen);
g = range(1);
inc3d = zeros(301,301,301); %new inclined grid initialised
for m = 1:101
for n = 1:101
%the g - cen value is added to the index to map arrays sync up correctly and
%keep loop reasonable / avoid overlap outside index !
xpart(m,n) = cen + cosd(theta).*(g + m -cen) - sind(theta).*(g + n - cen);
xn(m,n) = cen + cosd(phi).*(xpart(m,n) - cen) - sind(phi).*(g + n - cen); %this maps a value of x to new position theta and phi
yn(m,n) = cen + sind(phi).*(xn(m,n) - cen) + cosd(phi).*(g + n - cen); %maps a value of y to new position relative to phi
zn(m,n) = cen + sind(theta).*(g + m - cen) + cosd(theta).*(g + n - cen); %maps z to new position relative to theta (doesnt change relative to phi)
xn = round(xn);
yn = round(yn); %round to avoid integer problems with indexing
zn = round(zn);
inc3d(xn(m,n),yn(m,n),zn(m,n)) = flat3d(g + m,g + n,cen);
end
end
这似乎有效;例如,如果我生成 theta = 30 和 phi = 15 的 plot3 图片,我会得到这样的结果;
从这张 2D 平面图像中,通过直线方程,我认为可以简单地总结并生成需要任何区域的内核。这很混乱,但似乎有效。如果有人知道更清洁的方法,请务必添加它:)