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

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);

Réponses (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
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 commentaires

Saurav
Saurav le 6 Mai 2024
Modifié(e) : Saurav le 6 Mai 2024
Thanks Yatharth
I have some queries. Listed Down.
  1. When I am running the code on my stl file , normal variable remains empty. Anyway this normal variable is not used in finding of gradient change.
  2. disp(gradient change) it is giving 2d image but .stl is 3d surface geometry.
  3. For regular geometry(tetrahedron, octahedron, for each vertices it is giving same gradient change
  4. I have attached the gradient change of tetrahedron.stl. It is giving rectangular shape.
  1. I am sorry about the confusion regarding the normal variable, you are correct it is not being used in the code further.
  2. When using disp(gradientChange) the output should be an array not an image.
  3. 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.
  4. 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
Saurav le 6 Mai 2024
Modifié(e) : Saurav le 9 Mai 2024
Thanks Yatharth for your reply.
In above point3. what is the immediate neighbors means(How we will know the number of immediate neighbors)
Saurav
Saurav le 6 Mai 2024
Modifié(e) : Saurav le 10 Mai 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.
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.

Connectez-vous pour commenter.

Question posée :

le 23 Avr 2024

Commenté :

le 21 Mai 2024

Community Treasure Hunt

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

Start Hunting!

Translated by