Meshgrid with variable spacing

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 Kommentare

Rik
Rik am 10 Jun. 2020
What do you mean? What is your intended output? What result are you looking for? Posting slow code with nested loops is fine, then we have an idea what result we should work towards.
Lennart Hinz
Lennart Hinz am 10 Jun. 2020
Hi Rik,
thanks for your answer. I will try to explain the above example in more detail.
I have tried to create a three-dimensional point grid, which has a coarser step size in the border areas (i.e. X,Y,Z < -3 and X,Y,Z > 3) than in the middle (-3 < X,Y,Z < 3).
Theoretically, only superfluous points need to be removed from the generated mesh grid. But I am a bit confused about how to proceed there.
An approach using loops usually results in a less adaptive, more complex and less performant approach. But I am not sure whether it is possible at all without a segmented grid generation.
Best wishes
Lennart
Rik
Rik am 10 Jun. 2020
Bearbeitet: Rik am 10 Jun. 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 am 10 Jun. 2020
Bearbeitet: Lennart Hinz am 10 Jun. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ameer Hamza
Ameer Hamza am 10 Jun. 2020
Bearbeitet: Ameer Hamza am 10 Jun. 2020

0 Stimmen

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 Kommentare

Lennart Hinz
Lennart Hinz am 10 Jun. 2020
Hello Ameer,
This is an interesting alternative to my problem and is particularly well suited for achieving a gradual change in density. But in this case the grid is always rotationally symmetric and never uniform. I need a grid with segment by segment constant density, which does not necessarily have to be rotationally symmetric.
Nevertheless I thank you very much for your approach!
Ameer Hamza
Ameer Hamza am 10 Jun. 2020
Can you elaborate "segment by segment constant density"? Can you show an example image?
Lennart Hinz
Lennart Hinz am 10 Jun. 2020
Bearbeitet: Lennart Hinz am 10 Jun. 2020
Sure,
please look at the code in the thread with Rik, which I posted there last.
This is of course also a simple (and symmetric) example. But I hope it becomes clear what I am trying to achieve.
Offtopic: Did you render your pictures and then insert them manually or is there a possibility to visualize Matlab-Figures directly?
Ameer Hamza
Ameer Hamza am 10 Jun. 2020
Bearbeitet: Ameer Hamza am 10 Jun. 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 am 10 Jun. 2020
Bearbeitet: Lennart Hinz am 10 Jun. 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.

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2018a

Gefragt:

am 10 Jun. 2020

Bearbeitet:

am 10 Jun. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by