View a 2-d projection of a 3D object
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I tried usind the matlab function "view" but it doesn't give a projected face
0 Kommentare
Antworten (2)
Shaik
am 11 Mai 2023
Hi Lovett,
The view function in MATLAB sets the camera viewpoint for the current figure, but it does not render the 3D object in perspective. To render a 3D object in perspective so that it appears to have depth in a 2D image, you can use the camproj and camva functions to adjust the camera projection and field of view (FOV). Here's an example:
% Generate some random data
x = randn(100,1);
y = randn(100,1);
z = randn(100,1);
% Plot the data as a 3D scatter plot
figure;
scatter3(x,y,z);
% Adjust the view to a projected 2D view from above
camproj('perspective'); % set perspective projection
camup([0 1 0]); % set camera up direction
campos([0 0 max(z)]); % set camera position
camva(30); % set camera view angle
2 Kommentare
DGM
am 8 Jul. 2025
Bearbeitet: DGM
am 8 Jul. 2025
If you wanted to get a straight projection along z (as you would with a telecentric lens), you wouldn't use those parameters.
% Adjust the view to a 2D view from above
camproj('orthographic'); % use the default
camup([0 1 0]); % set camera up direction
campos([0 0 Sizez/2]); % set camera position
camva(0); % set camera view angle
For the same results, you don't even need to mess with that. You can just do
view(2) % same thing
DGM
am 8 Jul. 2025
Bearbeitet: DGM
am 8 Jul. 2025
I know what the title says, but I'm not convinced that an ephemeral view orientation was the intended goal. I'd figure you'd want the actual projection of the solid geometry, and pursuing graphics setup feels like the road to working with screenshots as data.
I'm not saying I have a great answer either. This feels like a horribly wasteful abuse of polyshape(), but I guess it works. I'm sure there are better ways of doing this.
% Variables you can change
Sizex = 1; % average length of RVE
Sizey = 1;
Sizez = 1;
Def = 10; % we probably don't need a quarter million triangles
% Variables you shouldn´t change
SFactx = (Sizex/2)/pi; % size factor of RVE
SFacty = (Sizey/2)/pi;
SFactz = (Sizez/2)/pi;
A = SFactx*pi; % lowest and max coordinates of meshgrid
B = SFacty*pi;
C = SFactz*pi;
Dx = A/Def; % definition factor
Dy = B/Def;
Dz = C/Def;
% Generation of gyroids
[X,Y,Z] = meshgrid(-A:Dx:A, -B:Dy:B, -C:Dz:C); % creates mesh grid
% Gyroid equation
OBJ =(cos(X/SFactx).* sin(Y/SFacty) + cos(Y/SFacty).* sin(Z/SFactz)...
+ cos(Z/SFactz).* sin(X/SFactx)+(0));
T = 0.5;
OBJ =(OBJ-T);
% Isosurface and isocap
[F1,V1] = isosurface(X,Y,Z,OBJ,0);
[F2,V2] = isocaps(X,Y,Z,OBJ,0,'below');
% Combines isosurface and isocaps into one
F3 = [F1;F2+length(V1(:,1))];
V3 = [V1;V2];
% Visualization
P = patch('Vertices',V3,'Faces',F3,'FaceColor', ...
'cyan','EdgeColor','none');
view(3)
camlight
axis equal
% project all the faces onto the xy plane
% polyshape() _can_ reorient backwards triangles,
% but vertical triangles will become degenerate during projection.
% we don't really need them, but they will cause a flood of warning messages.
% if our model is a closed surface, we don't need the backwards triangles either.
fn = faceNormal(triangulation(F3,V3));
fnz = 1E-9; % pick some threshold
%F = F3(abs(fn(:,3)) > fnz,:); % get rid of vertical faces only
F = F3(fn(:,3) > fnz,:); % get rid of vertical and backwards faces
% find union of all triangles using polyshape()
ps = repmat(polyshape(),[size(F,1) 1]);
for k = 1:size(F,1)
ps(k) = polyshape(V3(F(k,:),1:2));
end
ps = union(ps);
plot(ps)
axis equal
At that point, the 2D geometry can be retriangulated or whatever is required.
Again, this isn't a beautiful way of doing it, but at least it's a way.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Camera Views finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!