Clear Filters
Clear Filters

Change in the gradient of points in 3D space with respect to its neighbour.

6 views (last 30 days)
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)

Yatharth
Yatharth on 3 May 2024
Edited: Yatharth on 3 May 2024
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
Unable to resolve the name 'TR.ConnectivityList'.
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.
Here is the link to the "patch" function : https://www.mathworks.com/help/matlab/ref/patch.html
  5 Comments
Saurav
Saurav on 6 May 2024
Edited: Saurav on 10 May 2024
Hello Yatharth
Currently above code is giving local gradient Question1. How we can get global gradient change using those local gradient change.
(Local gradient means , gradient of each vertices)
Qustion2: How many neighbor of a vertices is considering in above code??. for example it considered 4 neighbor, 8 neighbor or 12 neighbor points of each vertices.
Issue: I am not geting same consistency in case of sphere.stl. I have attached sphere stl file also.
Saurav
Saurav on 21 May 2024
Hello @Yatharth
Above all problem i resolved. I have one question regarding sphere.
For sphere it is not giving zero or lesser value as compared to Icosahedron.
Kindly hep me out for this.
I have attached sphere stl in abpove comment.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!