I need to replace TriREp with triangulation and get length of a 3D particle

2 views (last 30 days)
clc;clear all;close all;
tic
% -------------------------------------------------------------
% Date: 4/17/2021
% Modified by SJ after finding the original Matlab code takes too long.
% If obj model is small (e.g., 10 vertices), it's OK to use which_version_to_use = 1;
% If obj model is large (e.g., million vertices), use which_version_to_use = 2;
% -------------------------------------------------------------
% Assumption: There is no comment line in the input obj file. If there is any, delete.
% e.g., below are example comments. If any, delete before running this code
% # Blender v2.79 (sub 0) OBJ File: ''
% # www.blender.org
% mtllib Pyramid1.mtl
% o Pyramid1
% usemtl None
% s off
% # 12 faces, 0 coords texture
% -------------------------------------------------------------
% If you want to use version 2, specify the numbers below.
num_vertex = 1635821; % starting with v (e.g., v -1.418804 4.008611 -3.247752)
num_face = 4907281-num_vertex; % starting with f with 3 numbers (e.g., f 75347 75332 75361)
num_face_long = 0; % starting with f with 6 numbers (e.g., f 2//2 5//2 3//2)
num_face_long_long = 0; % starting with f with 9 numbers (e.g., f 1/2/4 3/2/5 7/2/7)
% Choose which version to use:
which_version_to_use = 2;
if which_version_to_use == 1
[V,F] = read_vertices_and_faces_from_obj_file_v1('cube.obj');
elseif which_version_to_use == 2
[V,F] = read_vertices_and_faces_from_obj_file_v2('A066.obj',num_vertex, num_face, num_face_long, num_face_long_long);
else
error('Unexpected version selection')
end
% -------------------------------------------------------------
trisurf(F,V(:,1),V(:,2),V(:,3)); axis equal;
colormap gray
light('Position',[-1.0,-1.0,100.0],'Style','infinite');
lighting phong;
TR = TriRep(F, V(:,1),V(:,2),V(:,3));
[R,C]=ExactMinBoundSphere3D(TR.X);
[~]=VisualizeBoundSphere(TR,R,C);
R1=ApproxMinBoundSphereND(TR.X);
size_p=2*R
% v=44760040;
% D_sv=2*(3*v/(4*pi))^(1/3);
% D_cir=2*R;
% spher=D_sv/D_cir;
% [center,radius]=insphere(V,F);
% sp=radius/R;
toc
(Use triangulation in the same code and get same results)

Answers (2)

Raag
Raag on 20 Feb 2025
Hi Priya,
I am assuming your goal is to update your code by replacing the ‘TriRep with the triangulation class and then compute the particle length as the diameter of the minimal bounding sphere (i.e., 2×R).
The required modifications are minimal:
First replace ‘TriRep’ with ‘triangulation’:
Change
TR = TriRep(F, V(:,1), V(:,2), V(:,3));
To
TR = triangulation(F, V(:,1), V(:,2), V(:,3));
Next update property access, the ‘TR.X’ property is TR.Points in the triangulation class. So, wherever you had TR.X, replace it with TR.Points.
Here is the modified code for the steps mentioned above:
clc; clear all; close all;
tic
% -------------------------------------------------------------
% Date: 4/17/2021
% Modified by SJ after finding the original Matlab code takes too long.
% If obj model is small (e.g., 10 vertices), it's OK to use which_version_to_use = 1;
% If obj model is large (e.g., million vertices), use which_version_to_use = 2;
% -------------------------------------------------------------
% Assumption: There is no comment line in the input obj file. If there is any, delete them.
% -------------------------------------------------------------
% If you want to use version 2, specify the numbers below.
num_vertex = 1635821; % starting with v (e.g., v -1.418804 4.008611 -3.247752)
num_face = 4907281 - num_vertex; % starting with f with 3 numbers (e.g., f 75347 75332 75361)
num_face_long = 0; % starting with f with 6 numbers (e.g., f 2//2 5//2 3//2)
num_face_long_long = 0; % starting with f with 9 numbers (e.g., f 1/2/4 3/2/5 7/2/7)
% Choose which version to use:
which_version_to_use = 2;
if which_version_to_use == 1
[V, F] = read_vertices_and_faces_from_obj_file_v1('cube.obj');
elseif which_version_to_use == 2
[V, F] = read_vertices_and_faces_from_obj_file_v2('A066.obj', num_vertex, num_face, num_face_long, num_face_long_long);
else
error('Unexpected version selection')
end
% -------------------------------------------------------------
trisurf(F, V(:,1), V(:,2), V(:,3)); axis equal;
colormap gray
light('Position',[-1.0,-1.0,100.0],'Style','infinite');
lighting phong;
% Replace TriRep with the new triangulation class
TR = triangulation(F, V(:,1), V(:,2), V(:,3));
% Use TR.Points (instead of TR.X) in your function calls
[R, C] = ExactMinBoundSphere3D(TR.Points);
[~] = VisualizeBoundSphere(TR, R, C);
R1 = ApproxMinBoundSphereND(TR.Points);
% The length of the 3D particle is defined as 2*R (i.e., the diameter of the minimal bounding sphere)
size_p = 2 * R;
disp(['Length of 3D particle: ', num2str(size_p)]);
% Optional additional calculations:
% v = 44760040;
% D_sv = 2 * (3 * v / (4 * pi))^(1/3);
% D_cir = 2 * R;
% spher = D_sv / D_cir;
% [center, radius] = insphere(V, F);
% sp = radius / R;
toc
As you can see the switch from ‘TriRep’ to ‘triangulation’ is as simple as changing the constructor and the property names.
Functions like ‘ExactMinBoundSphere3D’, ‘VisualizeBoundSphere’, and ‘ApproxMinBoundSphereND’ now operate on ‘TR.Points’ (which holds the vertex coordinates) rather than ‘TR.X’.
For more details on the triangulation class, see the MATLAB documentation:
https://www.mathworks.com/help/matlab/ref/triangulation.html

Harshavardhan
Harshavardhan on 21 Feb 2025
From what I understand, you are trying to find the length of a 3D particle and you have the code for it.
However, the code uses the function “TriRep” which is not recommended and hence you want to replace it with the function “triangulation” instead.
The lines:
TR = TriRep(F, V(:,1),V(:,2),V(:,3));
[R,C]=ExactMinBoundSphere3D(TR.X);
[~]=VisualizeBoundSphere(TR,R,C);
R1=ApproxMinBoundSphereND(TR.X);
need to be replaced with
TR = triangulation(F, V);
[R, C] = ExactMinBoundSphere3D(TR.Points);
[~] = VisualizeBoundSphere(TR.Points, R, C);
R1 = ApproxMinBoundSphereND(TR.Points);
Hope this helps.

Categories

Find more on Triangulation Representation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!