필터 지우기
필터 지우기

Sine function along inclined z axis

조회 수: 7 (최근 30일)
Johnny Dessoulavy
Johnny Dessoulavy 2021년 10월 5일
댓글: Johnny Dessoulavy 2021년 10월 6일
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
Matt J
Matt J 2021년 10월 5일
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);
Johnny Dessoulavy
Johnny Dessoulavy 2021년 10월 5일
oops, I have updated the code.

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2021년 10월 6일
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
Johnny Dessoulavy
Johnny Dessoulavy 2021년 10월 6일
There is nothing stopping me from using that tool box so I'll download that and give it a go!
Johnny Dessoulavy
Johnny Dessoulavy 2021년 10월 6일
Got it to work with the resize! Thank you for all your help.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

DGM
DGM 2021년 10월 5일
편집: DGM 2021년 10월 6일
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
Johnny Dessoulavy
Johnny Dessoulavy 2021년 10월 6일
편집: Johnny Dessoulavy 2021년 10월 6일
This is what i am after, but in a 3D sense. Would you know how to shift this represenation ( the rotated plot) to be in 3D?
I tried but it only returned a ramp.

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2021년 10월 6일
편집: Matt J 2021년 10월 6일
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
Matt J
Matt J 2021년 10월 6일
편집: Matt J 2021년 10월 6일
but is there a way to not have it rotate around the origin?
With makehgtform, you can do any combination of rotations and translations that you wish. You can also compute the transform matrix of a rotation about an arbitrary 3D line using,
Johnny Dessoulavy
Johnny Dessoulavy 2021년 10월 6일
Yep I got it to work! thank you so much. It was great seeing these two different methods and learning two new things :D

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by