Failed to load a mesh from gptoolbox. I am missing something, what is it?

% 1. Load a Mesh
[V, F] = readOFF('D:\Conferences and Workshops\SG Summer\MFC - S\codes\gptoolbox-master\gptoolbox-master\mesh.off');
% 2. Display the Mesh
figure;
trisurf(F, V(:,1), V(:,2), V(:,3), 'FaceColor', 'cyan', 'EdgeColor', 'none');
axis equal;
lighting gouraud;
camlight;
title('Original Mesh');
xlabel('X');
ylabel('Y');
zlabel('Z');
% Define the number of iterations and timestep for smoothing
num_iterations = 100;
time_step = 0.01;
% 3. Mean Curvature Flow (MCF) - Explicit Method
V_explicit = V; % Copy the vertices for the explicit method
for iter = 1:num_iterations
% Compute the Laplace-Beltrami operator and mean curvature normal
L = cotmatrix(V_explicit, F);
HN = -L * V_explicit;
% Update vertex positions
V_explicit = V_explicit + time_step * HN;
end
% Display the smoothed mesh - Explicit Method
figure;
trisurf(F, V_explicit(:,1), V_explicit(:,2), V_explicit(:,3), 'FaceColor', 'cyan', 'EdgeColor', 'none');
axis equal;
lighting gouraud;
camlight;
title('MCF - Explicit Method');
xlabel('X');
ylabel('Y');
zlabel('Z');
% 3. Mean Curvature Flow (MCF) - Semi-Implicit Method
V_implicit = V; % Copy the vertices for the implicit method
for iter = 1:num_iterations
% Compute the Laplace-Beltrami operator
L = cotmatrix(V_implicit, F);
% Solve (I - time_step * L) * V_new = V_old
V_implicit = (speye(size(V_implicit, 1)) - time_step * L) \ V_implicit;
end
% Display the smoothed mesh - Semi-Implicit Method
figure;
trisurf(F, V_implicit(:,1), V_implicit(:,2), V_implicit(:,3), 'FaceColor', 'cyan', 'EdgeColor', 'none');
axis equal;
lighting gouraud;
camlight;
title('MCF - Semi-Implicit Method');
xlabel('X');
ylabel('Y');
zlabel('Z');
I get this error:
shittyday (%sorry this is the name of the script)
Error using fscanf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in readOFF (line 27)
OFFheader = upper(fscanf( fp, '%s\n', 1 ));
Error in shittyday (line 2)
[V, F] = readOFF('D:\Conferences and Workshops\SG Summer\MFC - S\codes\gptoolbox-master\gptoolbox-master\mesh.off');

12 Comments

Yes, I forgot to put that in the main body of the question edited it now @Walter Roberson
Hello Eshan,
I tried running the code on an irregular polyhedron (.off file) and it is working correctly. Could you please provide more details about the error you are encountering?
Can you share the 'mesh.off' file ?
I will put it there too:
function [V,F,UV,C,N] = readOFF( filename )
% READOFF reads an OFF file with vertex/face information
%
% [V,F,UV,C,N] = readOFF( filename )
%
% Input:
% filename path to .off file
% Outputs:
% V #V by 3 list of vertices
% F #F by 3 list of triangle indices
% UV #V by 2 list of texture coordinates
% C #V by 3 list of colors
% N #V by 3 list of normals
%
% See also: load_mesh, readOBJfast, readOBJ
% (C) 2007 Denis Kovacs, NYU
%-------------------------------------------------------------------------
V = [];
F = [];
UV = [];
C = [];
N = [];
fp = fopen( filename, 'r' );
OFFheader = upper(fscanf( fp, '%s\n', 1 ));
if OFFheader(end-2:end) ~= 'OFF'
warning('no OFF file!')
fclose(fp);
return;
end
OFFdim = 3;
OFF_N = 0; OFF_C=0; OFF_ST=0;
if find(OFFheader=='N') OFFdim = OFFdim+3; OFF_N=1; end
if find(OFFheader=='C') OFFdim = OFFdim+3; OFF_C=1; end
if find(OFFheader=='S') OFFdim = OFFdim+2; OFF_ST=1; end
% eat any comments before
line = eat_comments(fp,'#');
d = sscanf( line, '%d', 3);
nV = d(1); nF = d(2); nE = d(3);
%disp(sprintf(' - Reading %d vertices', nV));
switch OFFdim
case 3; OFFV = textscan( fp, '%f %f %f', nV);
case 5; OFFV = textscan( fp, '%f %f %f %f %f', nV);
case 6; OFFV = textscan( fp, '%f %f %f %f %f %f', nV);
case 7; OFFV = textscan( fp, '%f %f %f %f %f %f %f', nV);
case 8; OFFV = textscan( fp, '%f %f %f %f %f %f %f %f', nV);
case 9; OFFV = textscan( fp, '%f %f %f %f %f %f %f %f %f', nV);
case 10; OFFV = textscan( fp, '%f %f %f %f %f %f %f %f %f %f', nV);
case 11; OFFV = textscan( fp, '%f %f %f %f %f %f %f %f %f %f %f', nV);
otherwise; fclose(fp); error('Unsupported number of vertex entries');
end
try
OFFV = cell2mat(OFFV);
end
OFFdim = 1;
V = OFFV(:,OFFdim:(OFFdim+2)); OFFdim = OFFdim + 3;
if (OFF_N) N = OFFV(:,OFFdim:(OFFdim+2)); OFFdim = OFFdim + 3; end
if (OFF_C) C = OFFV(:,OFFdim:(OFFdim+2)); OFFdim = OFFdim + 3; end
if (OFF_ST) UV = OFFV(:,OFFdim:(OFFdim+1)); OFFdim = OFFdim + 2; end
if (nF ~= 0)
%disp(sprintf(' - Reading %d faces', nF));
temp = textscan( fp, '%d %d %d %d %d %d %d %d %d %d %d', nF );
sz = temp{1}(1);
if all(sz == cell2mat(temp(1)))
F = double (cell2mat( temp(2:sz+1 ))) +1;
else
warning('Trivially triangulating high degree facets');
F = zeros(sum(temp{1}-2),3);
fi = 1;
for f = 1:size(temp{1},1)
for j = 3:temp{1}(f);
F(fi,:) = [temp{2}(f) temp{j}(f) temp{j+1}(f)]+1;
fi = fi+1;
end
end
end
else
F = [];
end
fclose(fp);
%disp(' - done.');
end
am I loading the data correctly?
Try adding this command in the readoff.m file to check if 'fopen' is opening the file correctly or not
fp = fopen( filename, 'r' );
if fp == -1
error('Failed to open file: %s', filename);
end
OFFheader = upper(fscanf( fp, '%s\n', 1 ));
@Vinay You are correct, it doesn't open. It says:
Error using readOFF
Failed to open file
@ehsan The file 'mesh.off' might not be present in the current folder or the path name may be wrong.
Also share the 'mesh.off' file you are trying to open
Aha! - I think the path is correct .. Here is the load_mesh.m
function [V,F] = load_mesh(filename,varargin)
% read in vertices and faces from a .off or .obj file
%
% [V,F] = load_mesh(filename)
% [V,F] = load_mesh(filename,'ParameterName',ParameterValue, ...)
%
% Input:
% filename file holding mesh
% Optional:
% 'Quiet' followed by whether to be quiet {false}
% Output:
% V (vertex list)
% F (face list) fields
%
% Copyright 2011, Alec Jacobson (jacobson@inf.ethz.ch)
%
% See also: readOBJ, readOBJfast, readOFF
%
% parse any optional input
v = 1;
quiet = false;
while(v <= numel(varargin))
switch varargin{v}
case 'Quiet'
v = v+1;
assert(v<=nargin);
quiet = varargin{v};
quiet = quiet * 1;
otherwise
error(['Unsupported parameter: ' varargin{v}]);
end
v = v + 1;
end
% if mex libigl reader exists use that (up to 25x faster for .obj)
if exist('read_triangle_mesh','file')==3
try
[V,F] = read_triangle_mesh(GetFullPath(filename));
if ~isempty(V) && ~isempty(F)
return;
end
% else keep trying below
catch e
warning(e.message);
% else keep trying below
end
end
[~,~,ext] = fileparts(filename);
ext = lower(ext);
switch ext
case '.3ds'
[V,F] = read3DS(filename);
case '.obj'
try
[V,F] = readOBJfast(filename);
catch exception
if ~quiet
fprintf('Fast reader failed, retrying with more robust, slower reader\n');
end
[V,F] = readOBJ(filename);
end
case '.off'
[V,F] = readOFF(filename);
case '.ply'
[V,F] = readPLY(filename);
case '.stl'
[V,F] = readSTL(filename);
case '.wrl'
[V,F] = readWRL(filename);
case '.xml'
[V,F] = read_mesh_from_xml(filename);
otherwise
error('Unknown mesh format: %s',ext);
end
end
@ehsan I have attached a 'face.zip' file containig the face.off file and the 'demo.m' which is loading the mesh.
Try to run this script and if you face any issue please tell me.
Also can you share this 'D:\Conferences and Workshops\SG Summer\MFC - S\codes\gptoolbox-master\gptoolbox-master\mesh.off' file you are trying to load in your script.
@VinayYour demo worked and gave this output
@Vinay oh, I think there is not any filename.off in the gptoolbox package I downloaded. The package is on this git: https://github.com/geometry-processing/gptoolbox/tree/master

Sign in to comment.

 Accepted Answer

Hii eshan,
The issue is due to the filename is incorrect or not present.You can use the correct file while loading the data in the "load_mesh" function.
You can use different "off" files from the given link
I hope this helps!

More Answers (0)

Asked:

on 13 Aug 2024

Edited:

on 13 Aug 2024

Community Treasure Hunt

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

Start Hunting!