translation of a 3d node
10 ビュー (過去 30 日間)
古いコメントを表示
Hello, I have a triangular mesh wich I "build" from a nodal array. I also have all the information about normals.
What I need is to isolate and translate one triangular face of this mesh in the normal direction in an amplitude = 2 units.
The triangular face is allready selected but I dont know how to translate.
0 件のコメント
回答 (2 件)
BhaTTa
2025 年 8 月 14 日
If you’ve already picked out the three vertex indices of your triangle—say they are i1, i2, i3—and you have its outward unit normal n, then moving that face 2 units along the normal is as simple as:
function newVertices = translateTriangleAlongNormal(vertices, normal, amplitude)
% translateTriangleAlongNormal
% Translates a triangular face by a given amplitude along its normal vector.
%
% Args:
% vertices (double array): A 3x3 array where each row represents a vertex
% of the triangle (e.g., [x1, y1, z1; x2, y2, z2; x3, y3, z3]).
% normal (double array): A 1x3 array representing the face normal vector.
% amplitude (double): The distance to translate the face along the normal.
%
% Returns:
% double array: A 3x3 array with the new, translated vertex coordinates.
% Normalize the normal vector to ensure the translation is exactly `amplitude` units.
% The norm() function calculates the Euclidean length of the vector.
norm_length = norm(normal);
% Check for a zero-length normal to prevent division by zero.
if norm_length < 1e-6
disp("Warning: Normal vector has a magnitude of zero. Returning original vertices.");
newVertices = vertices;
return;
end
unitNormal = normal / norm_length;
% Calculate the translation vector by scaling the unit normal by the amplitude.
translationVector = unitNormal * amplitude;
% Apply the translation vector to each vertex of the triangle.
% The repmat() function is used to create a 3x3 matrix where each row is the
% translation vector, allowing for easy addition to the vertices matrix.
newVertices = vertices + repmat(translationVector, 3, 1);
end
% --- Main Example ---
% 1. Define the original triangular face vertices.
% Let's create a simple triangle on the XY-plane.
vertices = [0.0, 0.0, 0.0; % Vertex A
4.0, 0.0, 0.0; % Vertex B
2.0, 3.0, 0.0]; % Vertex C
% 2. Define the normal vector for this face.
% For a triangle on the XY-plane, the normal is pointing in the positive Z direction.
faceNormal = [0.0, 0.0, 1.0];
% 3. Define the translation amplitude.
amplitude = 2.0;
% 4. Perform the translation using our function.
translatedVertices = translateTriangleAlongNormal(vertices, faceNormal, amplitude);
% 5. Print the results to see the translation.
fprintf('Original Vertices:\n');
disp(vertices);
fprintf('\nFace Normal:\n');
disp(faceNormal);
fprintf('\nTranslation Amplitude: %f\n', amplitude);
fprintf('\nTranslated Vertices:\n');
disp(translatedVertices);
% Verification: The new Z-coordinates should be 2.
fprintf('\nVerification:\n');
fprintf('Original Z-coordinates: [%f, %f, %f]\n', vertices(:, 3));
fprintf('Translated Z-coordinates: [%f, %f, %f]\n', translatedVertices(:, 3));
0 件のコメント
DGM
2025 年 8 月 14 日
編集済み: DGM
2025 年 8 月 14 日
I would expect that we're dealing with F,V lists instead of a giant monolithic wad of ordered coordinate data. I also assume we're going to want to operate on more than one face, so we should design for it.
unzip stepholecube.stl.zip % for the forum
% some FV data from somewhere
T = stlread('stepholecube.stl');
[F V] = t2fv(T);
% some parameters
faceidx = [1:4 22]; % the face indices to offset
faceos = 0.2; % the offset distance
% generate new offset faces
[newF newV] = faceoffset(F(faceidx,:),V,faceos);
% display everything
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','k');
view(3); view(-30,47); camlight;
axis equal; grid on; hold on
patch('faces',newF,'vertices',newV,'facecolor','r','edgecolor','k');
function [newF newV] = faceoffset(F,V,os)
% [newF newV] = FACEOFFSET(F,V,OFFSET)
% Offset the selected faces in the direction of their face normals.
% Degenerate faces will be returned with no offset applied.
%
% F is a NFACESx3 list of integer indices into the rows of V
% V is a NVERTSx3 list of vertex positions
% OFFSET is a scalar specifying the distance to offset the faces
% get normals internally
% if the normals are supplied externally, we can't be sure they're right.
% if they came from an STL file, then it's a fair chance they're wrong.
% we should also want degenerate faces to be handled gracefully (or at least consistently).
T = triangulation(F,V);
N = faceNormal(T)*os;
% reduce FV data to ordered coordinate list, apply offset
expandedfacets = V.';
expandedfacets = reshape(expandedfacets(:,F.'),3,3,[]); % dims: [coords verts faces]
expandedfacets = expandedfacets + permute(N,[2 3 1]); % apply offset
newV = reshape(expandedfacets,3,[]).'; % reshape to form uncosolidated V list
newF = reshape(1:size(newV,1),3,[]).'; % form new F list
% uniqueness of vertices is not maintained
% get rid of duplicate vertices and remap F as needed
[newV,~,ic] = unique(newV,'rows','stable');
newF = ic(newF);
end
Note that adjacent faces will only remain adjacent in two conditions: either the faces are coplanar, or the calculated offset is zero. This technique will not allow you to dilate a solid or add thickness to an open surface.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
