Describing Multiple Cubes using a Single Struct

2 views (last 30 days)
If I use patch to draw two cubes like this:
format compact h(1) = axes('Position',[0.2 0.2 0.6 0.6]);
%----first cube------ vert = [1 1 1; 1 2 1; 2 2 1; 2 1 1 ; ... 1 1 2;1 2 2; 2 2 2;2 1 2]; fac = [1 2 3 4; ... 2 6 7 3; ... 4 3 7 8; ... 1 5 8 4; ... 1 2 6 5; ... 5 6 7 8]; patch('Faces',fac,'Vertices',vert,'FaceColor','r'); % patch function material shiny; alpha('color'); alphamap('rampdown'); view(30,30);
%------second cube----- hold on; vert2 = [1 1 1; 1 2 1; 2 2 1; 2 1 1 ; ... 1 1 2;1 2 2; 2 2 2;2 1 2]-0.5; fac2 = [1 2 3 4; ... 2 6 7 3; ... 4 3 7 8; ... 1 5 8 4; ... 1 2 6 5; ... 5 6 7 8]; patch('Faces',fac2,'Vertices',vert2,'FaceColor','b'); % patch function
...how can I then combine the data into a single struct? I need the data for both cubes to be in the same struct (with fields fv.faces and fv.vertices), so I can then save it to an STL file using stlwrite.
Alternatively (to achieve the same objective) if I have face and vertices data for two overlapping objects, how can I combine it into one set of face and vertices data?
Thanks.

Answers (1)

Aniruddha Katre
Aniruddha Katre on 4 Sep 2014
You can create struct arrays to store the same type of data for multiple cubes.
For example, you can store the faces and vertices data for the first cube as:
fv(1).faces = fac;
fv(1).vertices = vert;
Then you can store the same information for the second cube as:
fv(2).faces = fac2;
fv(2).vertices = vert2;
For more information about Struct Arrays, see:
and
  1 Comment
Chris Williams
Chris Williams on 5 Sep 2014
Thanks, but I don't think stlwrite will accept a struct in that form.
This works:
nv1=length(fv1.vertices);
fv_combined.vertices=[fv1.vertices;fv2.vertices]
fv_combined_faces=[fv1.faces; fv2.faces+nv1]
However , the problem now is that the merged file is getting very large. How can I address this please?|

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!