Change in the gradient of points in 3D space with respect to its neighbour.
Show older comments
Hello matlab Community,
Is there any function in Matlab to find the gradient change of a point in 3d space with respect to its neighbour point"?
I have sutface coordinates of approximately 15000 points (.STL file contains this). Now i want to find the gradient change of each point with resppecte to its neighbour.
I am attaching .STL file which contain coordinates of all the surface point.
It can be read in matlab using
TR=stlread('aggrgate_1.stl');
trimesh(TR);
Answers (1)
Hi Saurav,
There is no exisiting function in MATLAB as of now (R2024a) that can find the gradient change of a point in 3D space with respect to its neighbour.
However, you can manually do it by calculating Normals for each vertex (points) and iteratively calculate the gradient change.
MATLAB's "patch" function can be used to compute normals.
Here is how you can approch the problem:
Note: I was not able to open your STL file
% Using the stl file from the below example
% openExample('matlab/ReadTriangulationFromSTLTextFileExample')
TR = stlread('tristltext.stl');
F = TR.ConnectivityList; % Faces or Connectivity List
V = TR.Points; % Vertex or Points
p = patch('Faces', F, 'Vertices', V);
% storing the normals in a matrix
normals = p.VertexNormals;
% Initialize matrix to hold gradient changes
gradientChange = zeros(size(V, 1), 1);
% Loop through each vertex
for i = 1:size(V, 1)
% Find faces that include this vertex (row indices of a vertix in F)
[row, ~] = find(F == i);
% Find unique vertices connected to those faces, excluding the current vertex
neighbors = unique(F(row,:));
neighbors(neighbors == i) = [];
% Calculate the difference between the current vertex and its neighbors
% Here, we simply calculate the Euclidean distance as a proxy for gradient change
diffs = sqrt(sum((V(i,:) - V(neighbors,:)).^2, 2));
% Store the mean difference (or choose another metric)
gradientChange(i) = mean(diffs);
end
disp(gradientChange);
Gradient change for each point/ vertex is stored in the gradientChange matrix.
5 Comments
Yatharth
on 6 May 2024
- I am sorry about the confusion regarding the normal variable, you are correct it is not being used in the code further.
- When using disp(gradientChange) the output should be an array not an image.
- For regular geometry, like a tetrahedron or an octahedron, each vertex is positioned symmetrically relative to its neighbors. This symmetry means that the Euclidean distance from any given vertex to its immediate neighbors is the same across the entire geometry. This is expected behavior and validates that your code is functioning correctly for these types of geometries.
- To visualize the gradient change spatially over your 3D geometry, you can use MATLAB's visualization functions to map the gradientChange values onto the geometry. Here's an example using the "patch" function:
figure;
patch('Faces', F, 'Vertices', V, 'FaceVertexCData', gradientChange, 'FaceColor', 'interp', 'EdgeColor', 'none');
colorbar; % Adds a color bar to indicate the scale of the gradient change
camlight; lighting phong; % Improves the lighting and appearance
axis equal; % Sets equal scaling for x, y, and z axes to preserve the 3D aspect ratio
Saurav
on 21 May 2024
Categories
Find more on Vector Volume Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!