0

I want to rotate and align a 3d mesh model represented in face-vertex form. Transformation of a 3d matrix would have worked if my model was a in point cloud form. I couldn't find a function to convert a face-vertex information to [x y z] point cloud information.

view(-[0 20]);

the above command help to see the image in desired angle but it is not changing the face-vertex values. My 3d object is in .stl format and i used ftread.m function to load it to matlab .

Function to read .stl file

function [fout, vout, cout] = ftread(filename)
% Reads CAD STL ASCII files, which most CAD programs can export.
% Used to create Matlab patches of CAD 3D data.
% Returns a vertex list and face list, for Matlab patch command.
% 
% filename = 'hook.stl';  % Example file.
%
fid=fopen(filename, 'r'); %Open the file, assumes STL ASCII format.
if fid == -1 
    error('File could not be opened, check name or path.')
end
%
% Render files take the form:
%   
%solid BLOCK
%  color 1.000 1.000 1.000
%  facet
%      normal 0.000000e+00 0.000000e+00 -1.000000e+00
%      normal 0.000000e+00 0.000000e+00 -1.000000e+00
%      normal 0.000000e+00 0.000000e+00 -1.000000e+00
%    outer loop
%      vertex 5.000000e-01 -5.000000e-01 -5.000000e-01
%      vertex -5.000000e-01 -5.000000e-01 -5.000000e-01
%      vertex -5.000000e-01 5.000000e-01 -5.000000e-01
%    endloop
% endfacet
%
% The first line is object name, then comes multiple facet and vertex lines.
% A color specifier is next, followed by those faces of that color, until
% next color line.
%
CAD_object_name = sscanf(fgetl(fid), '%*s %s');  %CAD object name, if needed.
%                                                %Some STLs have it, some don't.   
vnum=0;       %Vertex number counter.
report_num=0; %Report the status as we go.
VColor = 0;
%
while feof(fid) == 0                    % test for end of file, if not then do stuff
    tline = fgetl(fid);                 % reads a line of data from file.
    fword = sscanf(tline, '%s ');       % make the line a character string
% Check for color
    if strncmpi(fword, 'c',1) == 1;    % Checking if a "C"olor line, as "C" is 1st char.
       VColor = sscanf(tline, '%*s %f %f %f'); % & if a C, get the RGB color data of the face.
    end                                % Keep this color, until the next color is used.
    if strncmpi(fword, 'v',1) == 1;    % Checking if a "V"ertex line, as "V" is 1st char.
       vnum = vnum + 1;                % If a V we count the # of V's
       report_num = report_num + 1;    % Report a counter, so long files show status
       if report_num > 249;
           disp(sprintf('Reading vertix num: %d.',vnum));
           report_num = 0;
       end
       v(:,vnum) = sscanf(tline, '%*s %f %f %f'); % & if a V, get the XYZ data of it.
       c(:,vnum) = VColor;              % A color for each vertex, which will color the faces.
    end                                 % we "*s" skip the name "color" and get the data.                                          
end
%   Build face list; The vertices are in order, so just number them.
%
fnum = vnum/3;      %Number of faces, vnum is number of vertices.  STL is triangles.
flist = 1:vnum;     %Face list of vertices, all in order.
F = reshape(flist, 3,fnum); %Make a "3 by fnum" matrix of face list data.
%
%   Return the faces and vertexs.
%
fout = F';  %Orients the array for direct use in patch.
vout = v';  % "
cout = c';
%
fclose(fid);
5
  • 1
    Can you show us precisely how you end up with the face-vertex structure? Once you have this data structure you should be able to simply transform the vertices using a rotation matrix. The faces are normally just indices into the vertices array, so they do not need transforming. So, this should be easy to do, once it is clear in what data structure your 3d mesh is stored after you've imported it. Commented Oct 17, 2015 at 17:51
  • MATLAB might not be the best tool for this job. Have you considered using a CAD program, e.g. SolidWorks? Commented Oct 17, 2015 at 18:59
  • @WhiteViking Thanks for the help By the way I used the above given function(i edited my question) to read my 3d object file and it gives vertices and faces. I dont know to do transformation now but i will try transformation by considering vertices data as point cloud info Commented Oct 18, 2015 at 19:26
  • @JeffIrwin I need MATLAB to do the rest of my project once I get this done like Laplacian surface deformation(LSD) . I didnt try SolidWorks. Anyway thanks for the help Commented Oct 18, 2015 at 19:34
  • Do something like voutrot = vout * r, where r is a 3x3 rotation matrix that you would define based on the rotation angle, as @WhiteViking suggested. Commented Oct 18, 2015 at 20:38

1 Answer 1

2

Let's try to read a simple STL file, apply a transformation to its vertices (for example a rotation around the Z axis), and render it.

Here is an example (text format) STL file for a cube with faces perpendicular to the coordinate axes:

solid
  facet normal 0.000000 1.000000 0.000000
    outer loop
      vertex 1.000000 1.000000 1.000000
      vertex 1.000000 1.000000 -1.000000
      vertex -1.000000 1.000000 -1.000000
    endloop
  endfacet
  facet normal 0.000000 1.000000 0.000000
    outer loop
      vertex 1.000000 1.000000 1.000000
      vertex -1.000000 1.000000 -1.000000
      vertex -1.000000 1.000000 1.000000
    endloop
  endfacet
  facet normal 0.000000 0.000000 1.000000
    outer loop
      vertex 1.000000 1.000000 1.000000
      vertex -1.000000 1.000000 1.000000
      vertex -1.000000 -1.000000 1.000000
    endloop
  endfacet
  facet normal 0.000000 0.000000 1.000000
    outer loop
      vertex 1.000000 1.000000 1.000000
      vertex -1.000000 -1.000000 1.000000
      vertex 1.000000 -1.000000 1.000000
    endloop
  endfacet
  facet normal 1.000000 0.000000 0.000000
    outer loop
      vertex 1.000000 1.000000 1.000000
      vertex 1.000000 -1.000000 1.000000
      vertex 1.000000 -1.000000 -1.000000
    endloop
  endfacet
  facet normal 1.000000 0.000000 0.000000
    outer loop
      vertex 1.000000 1.000000 1.000000
      vertex 1.000000 -1.000000 -1.000000
      vertex 1.000000 1.000000 -1.000000
    endloop
  endfacet
  facet normal 0.000000 0.000000 -1.000000
    outer loop
      vertex -1.000000 -1.000000 -1.000000
      vertex 1.000000 1.000000 -1.000000
      vertex 1.000000 -1.000000 -1.000000
    endloop
  endfacet
  facet normal 0.000000 -1.000000 0.000000
    outer loop
      vertex -1.000000 -1.000000 -1.000000
      vertex 1.000000 -1.000000 -1.000000
      vertex 1.000000 -1.000000 1.000000
    endloop
  endfacet
  facet normal 0.000000 -1.000000 0.000000
    outer loop
      vertex -1.000000 -1.000000 -1.000000
      vertex 1.000000 -1.000000 1.000000
      vertex -1.000000 -1.000000 1.000000
    endloop
  endfacet
  facet normal -1.000000 0.000000 0.000000
    outer loop
      vertex -1.000000 -1.000000 -1.000000
      vertex -1.000000 -1.000000 1.000000
      vertex -1.000000 1.000000 1.000000
    endloop
  endfacet
  facet normal -1.000000 0.000000 0.000000
    outer loop
      vertex -1.000000 -1.000000 -1.000000
      vertex -1.000000 1.000000 1.000000
      vertex -1.000000 1.000000 -1.000000
    endloop
  endfacet
  facet normal 0.000000 0.000000 -1.000000
    outer loop
      vertex -1.000000 -1.000000 -1.000000
      vertex -1.000000 1.000000 -1.000000
      vertex 1.000000 1.000000 -1.000000
    endloop
  endfacet
endsolid

Save it to cube.stl. Then import it using your ftread code:

[F, V, C] = ftread('cube.stl');

I'll be ignoring the color information in C here.

This is the 3D rotation matrix that rotates 60 degrees around the Z-axis:

Rz = [ cos(pi/3), sin(pi/3), 0 ;
      -sin(pi/3), cos(pi/3), 0 ;
               0,         0, 1 ];

Transform all vertices:

V = V * Rz';

Draw the cube as a set of faces using MATLAB's patch command:

patch('Faces', F, 'Vertices', V, 'FaceVertexCData', (1:12)', 'FaceColor', 'flat');
view(3)
axis equal

It looks like this:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Yup!! Thats what I want. Thanks alot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.