Meshgrid with variable spacing

8 vues (au cours des 30 derniers jours)
Lennart Hinz
Lennart Hinz le 10 Juin 2020
Modifié(e) : Lennart Hinz le 10 Juin 2020
Dear community,
I need a three-dimensional point grid with variable spacing. In the example below, I would like to have a coarser spacing at the edges and a finer spacing in the middle.
X_vec=unique([-7:1:-3 -3:.1:3 3:1:7]);
Y_vec=unique([-7:1:-3 -3:.1:3 3:1:7]);
Z_vec=unique([-7:1:-3 -3:.1:3 3:1:7]);
[X,Y,Z]=meshgrid(X_vec, Y_vec, Z_vec);
pcshow([X(:) Y(:) Z(:)]);
As you can see, my approach does not really work, because the grid is also too fine in the edges. Does anyone have an idea how to modify this approach without calling the meshgrid function segment by segment?
Best regards
Lennart
  4 commentaires
Rik
Rik le 10 Juin 2020
Modifié(e) : Rik le 10 Juin 2020
I'm not sure I understand the difference between what you want and what you currently have. Can you show the code with nested loops? If you have code that can't be adapted and is slow, we can compare the output of a faster, more flexible function to make sure the results are the same.
To give an example: if you wouldn't know about the sum function, you could describe it like this:
s=0;
for n=1:size(X,1)
for m=1:size(X,2)
s=s+X(n,m);
end
end
Which we could then test against faster code:
s2=sum(X,[1 2]); % or: s2=sum(X(:));
isequal(s2,s)
Lennart Hinz
Lennart Hinz le 10 Juin 2020
Modifié(e) : Lennart Hinz le 10 Juin 2020
Hi Rik,
below is an example of how I would like it to be. I apologize for my ambiguous presentation of the problem.
%coarse grid
[X_c,Y_c,Z_c]=meshgrid(-7:1:7,-7:1:7,-7:1:7);
pc_c=[X_c(:),Y_c(:),Z_c(:)];
mask=abs(pc_c(:,1))<3&abs(pc_c(:,2))<3&abs(pc_c(:,3))<3;
pc_c(mask,:)=[];
figure(1); %coarse grid preview
pcshow(pc_c,vecnorm(pc_c,2,2),'MarkerSize',30);
colorbar;
%fine grid
[X_f,Y_f,Z_f]=meshgrid(-3:.1:3,-3:.1:3,-3:.1:3);
pc_f=[X_f(:),Y_f(:),Z_f(:)];
%merge and visalize
figure(2);
pcshow([pc_c;pc_f],[vecnorm(pc_c,2,2);vecnorm(pc_f,2,2)],'MarkerSize',15);
Best compare the second figure with the initial example. The problem is that this approach is very hardcoded and requires a certain knowledge of how the grid is built. Ideally, I would simply pass the corresponding grid vectors to the function (i.e. X_vec, Y_vec, Z_vec) and can also realize different grid levels.

Connectez-vous pour commenter.

Réponses (1)

Ameer Hamza
Ameer Hamza le 10 Juin 2020
Modifié(e) : Ameer Hamza le 10 Juin 2020
Try this or something similar to create the grid points
x = (-7:0.3:7);
x = sign(x).*x.^2;
X_vec=x;
Y_vec=x;
Z_vec=x;
[X,Y,Z]=meshgrid(X_vec, Y_vec, Z_vec);
pcshow([X(:) Y(:) Z(:)]);
Or something like this
R_ved = 0:0.05:1;
A_vec = 0:0.2:2*pi;
E_vec = 0:0.2:2*pi;
[R, A, E] = meshgrid(R_ved, A_vec, E_vec);
[X, Y, Z] = sph2cart(A, E, R);
pcshow([X(:) Y(:) Z(:)]);
If rectangular grid is requirement, then modify the above example
rect_limit = 1;
R_ved = 0:0.05:sqrt(3*rect_limit.^2);
A_vec = 0:0.2:2*pi;
E_vec = 0:0.2:2*pi;
[R, A, E] = meshgrid(R_ved, A_vec, E_vec);
[X, Y, Z] = sph2cart(A, E, R);
M = [X(:) Y(:) Z(:)];
idx = any(abs(M) > rect_limit, 2);
M(idx, :) = [];
pcshow(M);
  5 commentaires
Ameer Hamza
Ameer Hamza le 10 Juin 2020
Modifié(e) : Ameer Hamza le 10 Juin 2020
Ok! I understand the shape of the grid now. Do you only want to have 2 levels or multiple levels? What format of X_vec, Y_vec, and Z_vec you intend to use in the final code. For example, you can specify the levels like this
X_vec = [-7 -3 3 7];
% similar for Y_vec and Z_vec
or is there some other format?
Also, the visuals attached in the answers are images inserted manually after rendering. fig file cannot be directly visualized here.
Lennart Hinz
Lennart Hinz le 10 Juin 2020
Modifié(e) : Lennart Hinz le 10 Juin 2020
Thanks a lot for your answer!
It would also be desirable to calculate a grid adaptively for different levels. So that it also works with one or more levels. And also to deviate from the ideal symmetry.
The most logical and simplest parameterization of the grid for me would be to use three vectors that span the space. Each vector would then have its individual gradation. For each vector the subarrays would be arranged horizontally next to each other and passed as one vector. Like in the initial example above.
In think that is consistent with what you are proposing.

Connectez-vous pour commenter.

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by