Rotate 3D Shape so Specific Face is Normal to Given Vector

I have a 3D shape made up of faces and vertices. I've been struggling to create code that will respond dynamically to rotate the shape so that the red face is 1. Normal to a given input vector; 2. The red face points in the direction of the vector.
For example, if the above photo is the starting state and I am given an input vector of [-1, 0, 0], I expect an output like this where the red face is: 1. Orthogonal to the vector; 2. The red face is closer to -x than the blue body.
My issue is that I can't figure out how to rotate the red square so that it is normal to the vector while also rotating the blue body to properly maintain the original shape. Enclosed is a copy of the shape, if you'd like to use that as a starting point. Any input is greatly appreciated!

2 commentaires

Have you tried rotate?
Rotate works for obvious vectors like [1,0,0] or [0,1,0], but not when given something like [0.707, -0.5, 1]. Or at least I haven't been able to figure out how to incorporate "rotate" + the front face being normal to the vector.

Connectez-vous pour commenter.

 Réponse acceptée

Matt J
Matt J le 6 Déc 2023
Modifié(e) : Matt J le 6 Déc 2023
You can use this to compute the alpha and direction arguments needed for rotate.
function [alpha,direction]=vecrot(vstart,vend)
%Find rotation parameters that rotate one vector into coincidence with another about their
%common perpendicular axis.
%
% [alpha,direction]=vecrot(vstart,vend)
%
%IN:
%
% vstart: Initial normal vector
% vend: Final normal vector
%
%OUT:
%
% alpha: the rotation angle in degrees
% direction: the rotation axis
vstart=vstart(:)/norm(vstart);
vend=vend(:)/norm(vend);
direction=cross(vstart,vend);
b=vend.'*vstart;
alpha = atan2d(sqrt(1-b^2),b);
end

4 commentaires

This works great for the figure! Thank you! But I realize now that I wasn't specific about the type of output I expected; I care less about the figure and more about the output of faces and vertices since I need to do further math on this rotated shape.
Matt J
Matt J le 7 Déc 2023
Modifié(e) : Matt J le 7 Déc 2023
This works great for the figure! Thank you!
You're welcome, but please Accept-click the answer to indicate the question was resolved.
But I realize now that I wasn't specific about the type of output I expected; I care less about the figure and more about the output of faces and vertices since I need to do further math on this rotated shape.
Given the direction and rotation angle, you can perform the rotation on a given set of points using AxelRot, in this FEX download,
Thank you! I have questions about the AxelRot:
I know I'd be using the form [XYZnew, R, t] = AxelRot(XYZold, deg, u, x0)
where XYZold = shape.vertices
u = cross(norm_original, norm_desired)
x0 = [0,0,0] %default
but I don't understand the deg value -- is it the same as the Alpha from the vecrot from above?
Matt J
Matt J le 7 Déc 2023
Modifié(e) : Matt J le 7 Déc 2023
You would do,
[alpha,direction]=vecrot(norm_original, norm_desired);
shape.vertices = AxelRot(shape.vertices.', alpha, direction, [0,0,0]).' ;

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactions, Camera Views, and Lighting dans Centre d'aide et File Exchange

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by