Vectorized rotation of 3D gridded volumes
Ältere Kommentare anzeigen
Problem
I understand that in general, if I define a 3D rotation matrix such as:
R = [
1,0,0;
0,cos(theta),-sin(theta);
0,sin(theta),cos(theta)
]
Then I can rotate a pointcloud pts where:
pts = [x(:), y(:), z(:)]
where x, y, z are datasets of arbitrary size and
by:
pts = pts*R
then:
[xp, yp, zp] = deal(pts(:,1), pts(:,2), pts(:,3))
I can then reshape the output vectors using
xp = reshape(xp, size(x))
yp = reshape(yp, size(y))
zp = reshape(zp, size(z))
Is there a more generalized way for matlab to apply a 3D rotation as described with R (3 x 3 matrix) such that I can for instance apply the rotation to an X, Y, Z volume (3 x m x n) in a single operation? For instance, say I define my X, Y, Z as a meshgrid so that each X, Y, Z is a m x n x p volume, can I do somethine like this
V = cat(3, X, Y, Z)
V = V*R
I would think an operation like this would be much faster and cleaner to implement than unloading into vectors and reshaping afterwards.
Attempt at Solution
[Lx,Ly, dL] = deal(10,20,1);
x = linspace(-Lx/2, Lx/2, Lx/dL);
y = linspace(-Ly/2, Ly/2, Ly/dL);
[X,Y] = meshgrid(x,y);
Z = zeros(size(X));
% Convert to pointcloud (should be optional?)
%%{
X = X(:);
Y = Y(:);
Z = Z(:);
%}
V = cat(3, X, Y, Z);
TH = [pi/4, 0, 0]; % define rotation about x, then y, then z (radians)
% Basic Rotations (https://en.wikipedia.org/wiki/Rotation_matrix)
Rx = [1,0,0; 0,cos(TH(1)),-sin(TH(1)); 0,sin(TH(1)),cos(TH(1))];
Ry = [cos(TH(2)),0,sin(TH(2)); 0,1,0; -sin(TH(2)),0,cos(TH(2))];
Rz = [cos(TH(3)),-sin(TH(3)),0; sin(TH(3)),cos(TH(3)),0; 0,0,1];
R = Rx*Ry*Rz; % generalize rotation matrix for 3D data
V = V*R; % generalized rotation operation
surf(V(:,:,1), V(:,:,2), V(:,:,3),'facecolor','none','edgecolor','black')
Antworten (1)
Maybe this is what you're looking for?
[XYZ{1:3}] = meshgrid(x,y,0);
V = cat(4, XYZ{:}); sz=size(V);
V = reshape(V,[],3)*R; % generalized rotation operation
V = reshape(V,sz);
surf(V(:,:,:,1), V(:,:,:,2), V(:,:,:,3),'facecolor','none','edgecolor','black')
5 Kommentare
Alexander Laut
am 24 Apr. 2019
Maybe what I'm looking for isn't that important, but Ideally I'd like the shape step of either R or V to be done in some what such that the tranform could be applied without needing to reshape the data. It's only two lines of code and probably doesn't even add that much overhead, but I feel like there may be a way to generalize V and R such that you can just do
V = V*R without the reshape functions.
This might be more of a math question as I'm looking for readability & efficiency. I'm starting to read up on quaternions notation as an alternative to perform these types of rotations as they are also supposed to be better suited to rounding errors which I've encountered using a standard rotation matrix (unless I use sind, cosd functions to compute at right angles)
There is no overhead to the reshaping. But if it's just syntactic sugar that you want, you can put the above in a classdef file with an mtimes() method customized to your tastes, e.g.,
classdef PointCloud
properties
xyz
end
methods
function V=PointCloud(X,Y,Z)
V.xyz = cat(4, X,Y,Z);
end
function V=mtimes(V,R)
xyz = reshape(V.xyz,[],3)*R;
V = reshape(xyz,size(obj.xyz));
end
function surf(V)
surf(V.xyz(:,:,:,1), V.xyz(:,:,:,2), V.xyz(:,:,:,3),...
'facecolor','none','edgecolor','black');
end
end
end
Now it is as simple as,
[XYZ{1:3}] = meshgrid(x,y,0);
V=PointCloud(XYZ{:});
surf(V*R);
Alexander Laut
am 30 Apr. 2019
I think I prefer your initial solution. I'm curious how "clean" is something like that considered to an experienced MATLAB user. I've only seen output unloaded into cells like that a handful of times. Now I'm wondering if I should be using that more often.
They wouldn't have made it,
if they didn't want you to use it...
Guillaume
am 1 Mai 2019
I'm curious how "clean" is something like that considered to an experienced MATLAB user
Matt's answer is exactly how I would have coded it (except with ndgrid instead of meshgrid) and I certainly use expansion of cell arrays (and structures) in comma separated lists very often. It's efficient and there's nothing unclean about it.
Kategorien
Mehr zu Filtering, Conversion, and Geometric Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!