Sine function along inclined z axis
13 views (last 30 days)
Show older comments
Johnny Dessoulavy
on 5 Oct 2021
Commented: Johnny Dessoulavy
on 6 Oct 2021
Im wanting to create a 'trench' like topography running down an inclined z plane. currently all my sine functions are along the y/x axis and are just creating bumps in the z plane.
The aim is to create a 's' shape with adjustable depth and thickness down the inclined z plane.
Is there a function to rotate a sine wave to do this? or am i approaching the problerm incorrectly? My code i have so far is below.
row = 94;
col = 195;
h = 10 ;
grid_row = linspace(0,h,row);
grid_col = linspace(0,h,col);
%% Setting the rows and columns into a meshed matrix to generate x and y coordinates
[x y] = meshgrid(grid_col,grid_row);
%% Setting up the initial flat plane which travels
z_vector = ones(row,col).* sort(linspace(h,0,length(x)));
z_matrix_flat = zeros(height(y),length(x))+ z_vector;
%% Z function which defines the trench
Z = z_matrix_flat + 0.5*sin(3*x);
Z_rotated = rot90(Z);
%Z = repelem(Z_rotated,2,4);
Z_scaled = imresize(Z,[94,195]);
%% Testing for loop to replace the orginal z plane in a predescribed area with the z values of the trench
%initialising the trench matrix to be the right size
trench = zeros(row, col);
for i= floor((0.1*height(x))):floor(0.9*height(x))
for j = floor(0.02*length(y)):floor(0.95*length(y))
trench(i,j) = trench(i,j) + Z_scaled(i,j);
end
end
ind = find(trench == 0); %finds where the trench matrix is 0 and changes the value to be the corresponding one on the plane.
trench(ind) = z_matrix_flat(ind) ;
surf(x,y,trench)
xlabel 'X', ylabel 'Y'
2 Comments
Matt J
on 5 Oct 2021
Your code doesn't run:
Index in position 2 is invalid. Array indices must be positive integers or logical
values.
Error in test (line 25)
trench(i,j) = trench(i,j) + Z_scaled(i,j);
Accepted Answer
DGM
on 6 Oct 2021
Actually, I think I misinterpreted what you meant by "trough". Let's start over.
% surface parameters
s = [500 500]; % [columns rows]
xyrange = [0 10]; % range of x,y axes
zrange = [0 10]; % range of z axis (and inclined plane)
% sine wave parameters
wamp = 1;
wfreq = 1;
woffset = mean(xyrange);
wdepth = 0.25;
wwidth = 1.5;
x = linspace(xyrange(1),xyrange(2),s(2));
y = linspace(xyrange(1),xyrange(2),s(1)).';
% i'm going to cheat and use image processing tools to create wave
% use a dummy image display to generate a binary image of the profile
% normally this doesn't show up, but it does in web-view
yw = wamp*sin(wfreq*x) + woffset;
h = image(x,y,ones(s(1),s(2)));
L = images.roi.Polyline(gca);
L.Position = [x; yw].';
% get mask and dilate to desired width
% this really relies on s being square, otherwise strel needs to be elliptical
% that can be done, but not with strel(). lemme know if that's needed.
ypxperunit = s(1)/range(xyrange);
mask = imdilate(createMask(L),strel('disk',round(wwidth/2*ypxperunit)));
% scale and incline mask to final form
zramp = linspace(zrange(1),zrange(2),s(2)); % oriented along x-axis
mask = zramp - wdepth*double(mask);
h = surf(x,y,mask);
shading flat
light('position',[-5 -10 10])
view(-68,6)
colormap(jet)
That said, there are definitely shortcomings to this approach. I'll let you see if any of them are an obstacle for your usage.
4 Comments
More Answers (2)
DGM
on 5 Oct 2021
Edited: DGM
on 6 Oct 2021
Consider the 1-D example:
rotangle = 45;
x = linspace(0,6*pi,500);
y = 2*sin(x);
trl = tand(rotangle)*x;
% simple method using a linear offset
xs = x;
ys = y + trl;
% actual rotation
x2 = linspace(0,6*pi/cosd(rotangle),500); % needs to be longer due to angle
r = sqrt(x2.^2 + y.^2);
t = atan2(y,x2)+deg2rad(rotangle);
xr = r.*cos(t);
yr = r.*sin(t);
h(1) = plot(x,trl,':'); hold on
h(2) = plot(xs,ys);
h(3) = plot(xr,yr);
axis equal
legend(h,{'trend line','skewed','rotated'},'location','northwest')
1 Comment
Matt J
on 6 Oct 2021
Edited: Matt J
on 6 Oct 2021
So, the problem is just adjusting the direction of the waves? If so, then perhaps as follows?
row = 94;
col = 195;
h=10;
theta=50;
%% Setting the rows and columns into a meshed matrix to generate x and y coordinates
grid_row = linspace(0,h,row);
grid_col = linspace(0,h,col);
[X,Y] = meshgrid(grid_col,grid_row);
T=cosd(theta)*X+sind(theta)*Y;
slope=h/max(T(:));
fun=@(t) slope*t+0.5*sin(3*t); %desired profile
Z=fun(T);
surf(X,Y,Z);
xlabel X, ylabel Y
10 Comments
See Also
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!