3/17/2016
ECE5322
21st CenturyElectromagnetics
Instructor:
Office:
Phone:
EMail:
Dr.Raymond C.Rumpf
A337
(915)7476958
rcrumpf@utep.edu
Lecture #19
Synthesizing Geometries for
21st Century Electromagnetics
Interfacing MATLAB with CAD
Lecture 19
Lecture Outline
STL Files
File format description
Problems and repairing
MATLAB Topics
Importing and exporting STL files in MATLAB
Visualizing surface meshes in MATLAB
Generating faces and vertices using MATLAB
Surface mesh 3D grid
CAD Topics
Lecture19
Converting flat images to STL
Point clouds
Importing custom polygons into SolidWorks
STL to CAD file conversion
Slide2
3/17/2016
STL File Format
Description
What is an STL File?
STL Standard Tessellation Language
This file format is widely used in rapid
prototyping (i.e. 3D printing, additive manufacturing). It
contains only a single triangular mesh of an objects surface.
Color, texture, materials, and other attributes are not
represented in the standard STL file format. Hacked
formats exists to accommodate this type of additional
information.
They can be text files or binary. Binary is more common
because they are more compact. We will look at text files
because that is more easily interfaced with MATLAB.
Lecture19
Slide4
3/17/2016
Surface Mesh
Despitethisspherereallybeingasolidobject,itisrepresentedinan
STLfilebyjustitssurface.
SolidObject
STLRepresentation
Lecture19
STL File Format
solid name
facet normal nx ny nz
outer loop
vertex vx,1 vy,1 vz,1
vertex vx,2 vy,2 vz,2
vertex vx,3 vy,3 vz,3
endloop
endfacet
endsolid name
Thissetoftextisrepeatedforeverytriangleonthe
surfaceoftheobject.
v v v3 v1
n 2 1
v2 v1 v3 v1
v2
v1
v3
Bold face indicates a keyword; these must appear in lower case. Note that there is a space in facet normal and in
outer loop, while there is no space in any of the keywords beginning with end. Indentation must be with spaces;
tabs are not allowed. The notation, {}+, means that the contents of the brace brackets can be repeated one or
more times. Symbols in italics are variables which are to be replaced with user-specified values. The numerical data
in the facet normal and vertex lines are single precision floats, for example, 1.23456E+789. A facet normal
coordinate may have a leading minus sign; a vertex coordinate may not.
Lecture19
3/17/2016
Example STL File
solid pyramid
facet normal -8.281842e-001 2.923717e-001 -4.781524e-001
outer loop
vertex 4.323172e-018 1.632799e-017 6.495190e-001
vertex 3.750000e-001 7.081604e-001 4.330127e-001
vertex 3.750000e-001 0.000000e+000 0.000000e+000
endloop
endfacet
facet normal 0.000000e+000 2.923717e-001 9.563048e-001
outer loop
vertex 7.500000e-001 0.000000e+000 6.495190e-001
vertex 3.750000e-001 7.081604e-001 4.330127e-001
vertex 0.000000e+000 0.000000e+000 6.495190e-001
endloop
endfacet
facet normal 8.281842e-001 2.923717e-001 -4.781524e-001
outer loop
vertex 3.750000e-001 -1.632799e-017 0.000000e+000
vertex 3.750000e-001 7.081604e-001 4.330127e-001
vertex 7.500000e-001 0.000000e+000 6.495190e-001
endloop
endfacet
facet normal 0.000000e+000 -1.000000e+000 0.000000e+000
outer loop
vertex 3.750000e-001 0.000000e+000 0.000000e+000
vertex 7.500000e-001 0.000000e+000 6.495190e-001
vertex 0.000000e+000 0.000000e+000 6.495190e-001
endloop
endfacet
endsolid pyramid
AnSTLfileisessentiallyjusta
listofallthetrianglesonthe
surfaceofanobject.
Eachtriangleisdefinedwitha
surfacenormalandthe
positionofthethreevertices.
Lecture19
Slide7
A Single Triangle
facet normal -8.281842e-001 2.923717e-001 -4.781524e-001
outer loop
vertex 4.323172e-018 1.632799e-017 6.495190e-001
vertex 3.750000e-001 7.081604e-001 4.330127e-001
vertex 3.750000e-001 0.000000e+000 0.000000e+000
endloop
endfacet
1. Facetnormalmustfollowrighthand
ruleandpointoutwardfromobject.
a) Someprogramssetthisto[0;0;0]
orconveyshadinginformation.
b) Dontdependonit!
2. Adjacenttrianglesmusthavetwo
commonvertices.
3. STLfilesappeartobesetuptohandle
arbitrarypolygons.Dontdothis.
Lecture19
Vertex3
FacetNormal
Vertex1
Vertex2
Slide8
3/17/2016
Warnings About Facet Normals
Since the facet normal can be calculated from the
vertices using the right-hand rule, sometimes the
facet normal in the STL file contains other
information like shading, color, etc.
Dont depend on the right-hand rule being
followed.
Basically, dont depend on anything!
Lecture19
STL File
Problems and
Repairing
3/17/2016
Inverted Normals
Allsurfacenormals shouldpointoutwards.
Good
Bad
http://admproductdesign.com/workshop/3dprinting/definitionofstlerrors.html
Lecture19
11
Intersecting Triangles
Nofacesshouldcutthrougheachother.Intersectionsshouldbe
removed.
http://admproductdesign.com/workshop/3dprinting/definitionofstlerrors.html
Lecture19
12
3/17/2016
Noise Shells
Ashellisacollectionoftrianglesthatformasingleindependent
object.SomeSTLfilesmaycontainsmallshellsthatarejustnoise.
Theseshouldbeeliminated.
Mainshell
NoiseShell
Lecture19
13
Nonmanifold Meshes
Amanifold(i.e.watertight)meshhasnoholesandisdescribedbya
singlecontinuoussurface.
http://http://www.autodesk.com/
Lecture19
14
3/17/2016
Mesh Repair Software
Commercial Software
Magics
NetFabb
SpaceClaim
Autodesk
Open Source Alternatives
MeshLab
NetFabb Basic
Blender
Microsoft Model Repair
Lecture19
Slide15
Importing and
Exporting STL
Files in MATLAB
3/17/2016
MATLAB Functions for STL Files
TheMathworks websitehasverygoodfunctionsforreadingand
writingSTLfilesinbothASCIIandbinaryformats.
STLFileReader
http://www.mathworks.com/matlabcentral/fileexchange/29906binarystlfilereader
STLFileWriter
http://www.mathworks.com/matlabcentral/fileexchange/36770stlwritewritebinaryor
asciistlfile
Lecture19
17
How to Store the Data
WehaveN facetsand 3N verticestostoreinarrays.
F(N,3) Array of triangle facets
V(?,3) Array of triangle vertices
Manytimes,thenumberofverticesis3N.Observingthatmanyof
thetrianglefacetssharevertices,therewillberedundantvertices.
STLfilescanbecompressedtoeliminateredundantvertices,but
manytimestheyarenot.
Lecture19
18
3/17/2016
Lazy Array of Vertices (1 of 2)
vx ,1
v
x ,2
V
vx , M
v y ,1
v y ,2
vy ,M
vz ,1
vz ,2
vz , M
V isanarray
containingthe
positionofallthe
verticesinCartesian
coordinates.
5
8
2
M isthetotal
numberof
1
vertices.
7
11
3
12
10
Lecture19
19
Lazy Array of Vertices (2 of 2)
Thereisredundancyhere.Twelveverticesarestored,butthedevice
isreallyonlycomposedoffour.
Whileaninefficientrepresentation,
thisisprobablynotworthyourtimefixing.
2,5,8
SolidWorks exportsalazyarrayofvertices.
vx ,1
v
x ,2
V
vx , M
Lecture19
v y ,1
v y ,2
vy,M
vz ,1
vz ,2
vz , M
4,9,11
1,6,12
3,7,10
20
10
3/17/2016
Compact Array of Vertices
vx ,1
v
x ,2
V
vx ,3
vx ,4
vz ,1
vz ,2
vz ,3
vz ,4
v y ,1
v y ,2
v y ,3
v y ,4
2
2
2
2
ArrayofVertices,V
4
4
3
3
4
3
1
Lecture19
21
Array of Faces
n1,1
n
2,1
F
nN ,1
n1,2
n2,2
nN ,2
n1,3
n2,3
nN ,3
F isanarray
indicatingthearray
indicesofthe
verticesdefining
thefacet.
5
allintegers
8
2
9
4
N isthetotal
numberof
faces.
Lecture19
3
11
3
12
10
22
11
3/17/2016
Example of Lazy Arrays
2,5,8
4,9,11
1,6,12
3,7,10
Lecture19
23
Example of Compact Arrays
4
1
3
Thiscanmakeaverylargedifferenceforlargeandcomplexobjects.
Lecture19
24
12
3/17/2016
STL Files Generated by
SolidWorks
Forsomereason,Solidworks doesnotusethezaxisasthevertical
axis.
Forconvenience,STLfilescanbeeasilyreoriented.
% REORIENT SOLIDWORKS AXES TO MATLAB AXES
Y
= V(:,2);
V(:,2) = V(:,3);
V(:,3) = Y;
OrientationinSolidWorks
ImportedOrientation
AdjustedOrientation
Lecture19
25
Visualizing
Surface Meshes
in MATLAB
13
3/17/2016
How to Draw the Object
Giventhefacesandvertices,theobjectcanbedrawninMATLAB
usingthepatch() command.
% DRAW STRUCTURE USING PATCHES
P = patch('faces',F,'vertices',V);
set(P,'FaceColor','b','FaceAlpha',0.5);
set(P,'EdgeColor','k','LineWidth',2);
Lecture19
27
Generating
Faces and
Vertices Using
MATLAB
14
3/17/2016
MATLAB Surfaces
SurfacescomposedofsquarefacetsarestoredinX,Y,andZ arrays.
Thesurfaceshownisconstructed
ofarraysthatareall55.
Lecture19
29
Direct Construction of the
Surface Mesh
MATLABhasanumberofbuiltincommandsforgeneratingsurfaces.
Someofthesearecylinder(),sphere() andellipsoid().
% CREATE A UNIT SPHERE
[X,Y,Z] = sphere(41);
Surfacescanbeconvertedtotriangularpatches(facetsandvertices)
usingthesurf2patch() function.
% CONVERT TO PATCH
[F,V] = surf2patch(X,Y,Z,triangles);
Thefacesandverticescanbedirectlyvisualizedusingthepatch()
function.
% VISUALIZE FACES AND VERTICES
h = patch('faces',F,'vertices',V);
set(h,'FaceColor',[0.5 0.5 0.8],'EdgeColor','k');
Lecture19
30
15
3/17/2016
Grid Surface Mesh
3Dobjectsonagridcanbeconvertedtoasurfacemeshusingthe
commandisosurface().
% CREATE ELLIPTICAL OBJECT
OBJ = (X/rx).^2 + (Y/ry).^2 + (Z/rz).^2;
OBJ = (OBJ < 1);
% CREATE SURFACE MESH
[F,V] = isosurface(X,Y,Z,OBJ,0.5);
Objecton3DGrid
SurfaceMesh
Lecture19
31
May Need isocaps()
When3Dobjectsextendtotheedgeofthegrid,youmayneedto
useisocaps() inadditiontoisosurface().
isosurface()
isocaps()
isosurface()
+ isocaps()
% CREATE SURFACE MESH
[F,V]
= isosurface(X,Y,Z,OBJ,0.5);
[F2,V2] = isocaps(X,Y,Z,OBJ,0.5);
Lecture19
32
16
3/17/2016
Combining Faces and Vertices
from Two Objects
TherearenofunctionsinMATLABtoperformBooleanoperationsonmultiple
meshes.Wecan,however,combinethefacesandverticesfromtwoobjects.Be
carefulthisdoesnotresultinoverlapsorgapsbetweenobjects.
F1 andV1
F2 andV2
Correctly
Combined
% COMBINE FACES AND VERTICES
F3 = [ F1 ; F2+length(V1(:,1)) ]
V3 = [ V1 ; V2 ]
Incorrectly
Combined
% WRONG WAY TO
% COMBINE FACES AND VERTICES
F3 = [ F1 ; F2 ]
V3 = [ V1 ; V2 ]
Lecture19
33
Converting
Surface Meshes
to Objects on a
3D Grid
17
3/17/2016
Example Pyramid
ER(nx,ny,nz)
SolidWorks Model
Lecture19
ImportSTLintoMATLAB
ConverttoVolumeObject
35
Example Dinosaur
ER(nx,ny,nz)
ImportSTLintoMATLAB
Lecture19
ConverttoVolumeObject
36
18
3/17/2016
MATLAB Functions for
Voxelization of STL Files
TheMathworks websitehasexcellentfunctionsfor
convertingsurfacemeshestopointsona3Darray.
FunctionforVoxelization
http://www.mathworks.com/matlabcentral/fileexchange/27390
meshvoxelisation
Lecture19
37
Converting
Images and 2D
Objects to STL
19
3/17/2016
Load and Resize the Image
% LOAD IMAGE
B = imread(letter.jpg');
% RESIZE IMAGE
B = imresize(B,0.2);
[Nx,Ny,Nc] = size(B);
Thiswillgiveusacoarsermeshinorder
tobefasterandmorememoryefficient.
Lecture19
39
Flatten the Image
ImagesloadedfromfileusuallycontainRGB
informationmakingthem3Darrays.Thesearrays
mustbeconvertedtoflat2Darraysbefore
meshing.
%
B
B
B
Lecture19
FLATTEN COLOR IMAGE
= double(B);
= B(:,:,1) + B(:,:,2) + B(:,:,3);
= 1 - B/765;
40
20
3/17/2016
Stack the Image
Wewillmeshtheimageusingisocaps(),but
thatfunctionrequiresa3Darray.So,wewillstack
thisimagetobetwolayersthick.
% STACK IMAGE
B(:,:,2) = B;
Lecture19
41
Mesh the Image Using
isocaps()
Weonlywishtomeshasinglesurfacesowegive
isocaps(),theadditionalinputargument
zmintodothis.
% CREATE 2D MESH
[F,V] = isocaps(ya,xa,[0 1],B,0.5,'zmin');
Lecture19
42
21
3/17/2016
Save the Mesh as an STL File
WecansavethismeshasanSTLfile.
Lecture19
43
Extrude Using Blender (1 of 2)
1. OpenBlender.exe.
2. File Import Stl (.stl)
3. OpentheSTLfileyoujustcreated.
Lecture19
44
22
3/17/2016
Extrude Using Blender (2 of 2)
1. Selecttheobjectwithright
mouseclick.
2. PressTABtoenterEditmode.
3. Pressetoextrudemesh.
4. PressTABagaintoexitEditmode.
5. Youcannowedityourobject
orexportasanew3DSTLfile.
Lecture19
45
Point Clouds
23
3/17/2016
What is a Point Cloud?
KleinBottle(seeMATLABdemos)
PointCloudDescriptionofaKleinBottle
PointcloudsrepresenttheoutsidesurfaceofobjectasasetofverticesdefinedbyX,Y,andZ
coordinates.Theyaretypicallygeneratedby3Dscanners,butcanalsobeusedtoexport3D
objectsfromMATLABintoSolidWorks orotherCADprograms.
Lecture19
47
Other Examples of Point Clouds
Lecture19
48
24
3/17/2016
Point Cloud Data
Thepositionofallthepointsinthepoint
cloudcanbestoredinanarray.
x1
x
2
PC x3
xN
PC =
0.1200
0.1159
0.0311
0.0000
-0.0311
-0.0600
y1
y2
y3
yN
0.0000
-0.0311
-0.1159
-0.1200
-0.1159
-0.1039
z1
z2
z3
z N
0.7071
0.7071
0.7071
0.7071
0.7071
0.7071
Lecture19
49
Saving Point Cloud Data to File
(1 of 2)
Pointclouddatafilesarecommaseparated
textfileswiththeextension.xyz
Thesecanbeeasilygeneratedusingthe
builtinMATLABcommandcsvwrite().
% SAVE POINT CLOUD AS A COMMA SEPERATED FILE
PC = [ X Z Y ];
dlmwrite('pointcloud.xyz',PC,'delimiter',',','newline','pc');
PC = [ X Y Z ];
Lecture19
PC = [ X Z Y ];
50
25
3/17/2016
Saving Point Cloud Data to File
(2 of 2)
SolidWorkswantstoseeanXYZat
thestartofthefile.
Youcanaddthismanuallyusing
Notepadorwriteamoresophisticated
MATLABtextfilecreator.
Lecture19
51
Activate ScanTo3D Add-In in
SolidWorks
First,youwillneedtoactivatethe
ScanTo3DinSolidWorks.
ClickToolsAddIns.
ChecktheScanto3Dcheckbox.
ClickOK.
Lecture19
52
26
3/17/2016
Open the Point Cloud File
Lecture19
53
Run the Mesh Prep Wizard
ToolsScanTo3DMeshPrepWizard
Lecture19
1.
2.
3.
4.
RuntheMeshPrepWizard.
Selectthepointcloud.
Clicktherightarraybutton.
Orientationmethod,selectNone
becausewedidthisinMATLAB.
5. Noiseremoval,zeroassuming
geometrycamefromMATLAB.
6. Workthroughalloptions.
7. Clickthegreencheckmarktofinish.
54
27
3/17/2016
Point Cloud Density
Lecture19
55
Final Notes on Point Clouds
Other CAD packages have better point cloud
import capabilities than SolidWorks. Rhino 3D is
said to be excellent.
Generating a solid model from the data can be
done in SolidWorks. The meshed model is
essentially used as a template for creating the
solid model. This procedure is beyond the scope
of this lecture.
Lecture19
56
28
3/17/2016
Importing
Custom
Polygons into
SolidWorks
The Problem
Supposewecalculatetheverticesofapolygonfromanoptimization
inMATLAB.
Howdoweimportantthe
verticessothatthepolygon
canbeimportedexactlyinto
Solidworks sothatitcanbe
extruded,cut,modified,etc.?
Thereisnofeaturein
SolidWorks todothis!!
Lecture19
58
29
3/17/2016
Example Application
PlacingaGMRfilteronto
acurvedsurface.
Gratingperiodisspatially
variedtocompensate.
sin x R
x
tan 1
R
1 d R cos x R
K x K 0 k0 ninc sin inc x
inc x
x K x dx
0
r x
r
cos x cos f
cos x cos f
R.C.Rumpf,M.Gates,C.L.Kozikowski,W.A.Davis,
"GuidedModeResonanceFilterCompensatedtoOperate
onaCurvedSurface,"PIERC,Vol.40,pp.93103,2013.
Lecture19
59
Be Careful About XYZ Curves
ThereisafeatureinSolidWorks CurveThroughXYZPointsthat
appearstoimportdiscretepoints,butitfitsthepointstoaspline.
Thiseffectivelyroundsanycornersandmayproduceintersections
thatcannotberendered.
Thisshouldbeasquare!
Lecture19
Thefourpointsarefittoaspline!
60
30
3/17/2016
Step 1 Define the Polygon
CreateanN3arrayinmemorycontainingtheallthepointsinthe
polygon.N isthenumberofpoints.
P =
0.6448
1.0941
1.3427
0.3642
0.4753
-0.0651
-0.5258
-0.4824
-0.8912
-0.9966
-0.9087
-1.0666
-1.1762
-0.5403
-0.1403
0.3818
0.7795
0.8293
1.0972
0.6448
0
0.3758
1.0452
0.5573
1.8765
0.7853
1.1984
0.5240
0.4822
0.1664
-0.1517
-0.5774
-1.2777
-1.2314
-1.6931
-1.5074
-1.1927
-0.6455
-0.3766
-0.0000
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Lecture19
61
Step 2 Save as an IGES from
MATLAB
Thereisafunctioncalledigesout() availableforfreedownload
fromtheMathworks website.ThiswillsaveyourpolygontoanIGES
file.
% SAVE IGES FILE
igesout(P,'poly');
Thiscreatesafilecalledpoly.igsinyourworkingdirectory.
Lecture19
62
31
3/17/2016
Step 3 Open the IGES in
SolidWorks (1 of 2)
SelecttheIGESfiletypeinthe[Open]filedialog.Thenclickonthe
[Options]button.
Lecture19
63
Step 3 Open the IGES in
SolidWorks (2 of 2)
1. Makesurethat
Surface/solid
entitiesis
unchecked
2. EnsurethatFree
point/curveentities
ischecked.
3. ClickontheImport
assketch(es)radio
button.
Openthefile.
Lecture19
64
32
3/17/2016
Step 4 Convert to a Standard
2D Sketch
Thepolygonimportsasa3Dsketchbydefault.
Editthe3Dsketchandcopy.
Openanew2Dsketchandpaste.
Nowyoucandowhateveryouwishwiththesketch!Extrude,
revolve,cutextrude,etc.
Lecture19
65
Extrude Using Blender (1 of 2)
1. OpenBlender.exe.
2. File Import Stl (.stl)
3. OpentheSTLfileyoujustcreated.
Lecture19
66
33
3/17/2016
Extrude Using Blender (2 of 2)
1. Selecttheobjectwithright
mouseclick.
2. PressTABtoenterEditmode.
3. Pressetoextrudemesh.
4. PressTABagaintoexitEditmode.
5. Youcannowedityourobject
orexportasanew3DSTLfile.
Lecture19
67
STL to CAD
Conversion
34
3/17/2016
Import STL Into MeshLab
1. Openmeshlab.exe.
2. File ImportMesh
3. Openthe3DSTLfileyoujustcreated.
Lecture19
69
Convert to SolidWorks Part
1.
2.
3.
4.
5.
6.
7.
8.
Openmeshlab.exe.
File ExportMeshAs
SelectDXFfiletype.
Savemesh.
LaunchSolidWorks.
File Open
SelectDXFfile.
SelectImporttoanewpartas:
then3Dcurvesormodel
thenNext>.
9. SelectAllLayers
Lecture19
70
35