Synthesizing Geometries For 21st Century Electromagnetics: Lecture Outline

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

9/13/2018

ECE 5322 
21st Century Electromagnetics
Instructor: Dr. Raymond C. Rumpf
Office: A‐337
Phone: (915) 747‐6958
E‐Mail: rcrumpf@utep.edu

Lecture #19

Synthesizing Geometries for


21st Century Electromagnetics
Interfacing MATLAB with CAD
Lecture 19 1

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
– Converting flat images to STL
– Point clouds
– Importing custom polygons into SolidWorks
– STL to CAD file conversion
– Exporting STL from Blender with proper units

Lecture 19 Slide 2

1
9/13/2018

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.
Lecture 19 Slide 4

2
9/13/2018

Surface Mesh
Despite this sphere really being a solid object, it is represented in an 
STL file by just its surface.

Solid Object

STL Representation

Lecture 19 5

STL File Format


solid name
This set of text is repeated for every triangle on the 
facet normal nx ny nz surface of the object.
outer loop

vertex vx,1 vy,1 vz,1
    v1
vertex vx,2 vy,2 vz,2  v  v    v3  v1 
nˆ  2 1
vertex vx,3 vy,3 vz,3  
 v2  v1    v3  v1 
endloop
endfacet 
v3

endsolid name v2
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.

Lecture 19 6

3
9/13/2018

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 An STL file is essentially just a 
endfacet list of all the triangles on the 
facet normal 0.000000e+000 -1.000000e+000 0.000000e+000
outer loop surface of an object.
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 Each triangle is defined with a 
endloop
endfacet surface normal and the 
endsolid pyramid
position of the three vertices.
Lecture 19 Slide 7

A Single Triangle
facet normal -8.281842e-001 2.923717e-001 -4.781524e-001
outer loop
Vertex 3
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
1. Facet normal must follow right‐hand 
rule and point outward from object.
a) Some programs set this to [0;0;0] 
or convey shading information. Vertex 1
b) Don’t depend on it!
2. Adjacent triangles must have two 
common vertices. Vertex 2
3. STL files appear to be setup to handle 
arbitrary polygons.  Don’t do this.

Lecture 19 Slide 8

4
9/13/2018

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.
• Don’t depend on the right-hand rule being
followed.
• Basically, don’t depend on anything!

Lecture 19 9

STL File
Problems and
Repairing

5
9/13/2018

Inverted Normals
All surface normals should point outwards.

Good Bad

http://admproductdesign.com/workshop/3d‐printing/definition‐of‐stl‐errors.html

Lecture 19 11

Intersecting Triangles
No faces should cut through each other.  Intersections should be 
removed.

http://admproductdesign.com/workshop/3d‐printing/definition‐of‐stl‐errors.html

Lecture 19 12

6
9/13/2018

Noise Shells
A shell is a collection of triangles that form a single independent 
object.  Some STL files may contain small shells that are just noise.  
These should be eliminated.

Main shell

Noise Shell

Lecture 19 13

Nonmanifold Meshes
A manifold (i.e. watertight) mesh has no holes and is described by a 
single continuous surface.

http://http://www.autodesk.com/

Lecture 19 14

7
9/13/2018

Mesh Repair Software


• Commercial Software
– Magics
– NetFabb
– SpaceClaim
– Autodesk
• Open Source Alternatives
– MeshLab
– NetFabb Basic
– Blender
– Microsoft Model Repair
Lecture 19 Slide 15

Importing and
Exporting STL
Files in MATLAB

8
9/13/2018

MATLAB Functions for STL Files


The Mathworks website has very good functions for reading and 
writing STL files in both ASCII and binary formats.

STL File Reader
http://www.mathworks.com/matlabcentral/fileexchange/29906‐binary‐stl‐file‐reader

STL File Writer
http://www.mathworks.com/matlabcentral/fileexchange/36770‐stlwrite‐write‐binary‐or‐
ascii‐stl‐file

Lecture 19 17

How to Store the Data


We have N facets and ≤ 3N vertices to store in arrays.

F(N,3) Array of triangle facets


V(?,3) Array of triangle vertices

Many times, the number of vertices is 3N.  Observing that many of 
the triangle facets share vertices, there will be redundant vertices.

STL files can be compressed to eliminate redundant vertices, but 
many times they are not. 

Lecture 19 18

9
9/13/2018

Lazy Array of Vertices (1 of 2)


5
 vx ,1 v y ,1 vz ,1 
v v y ,2 vz ,2 
V 
x ,2
8
  
 
vx , M vy ,M vz , M 
2
V is an array  4
6
containing the 
position of all the 
vertices in Cartesian  9
coordinates.

M is the total 
number of  7
vertices. 1

3 11

12
10
Lecture 19 19

Lazy Array of Vertices (2 of 2)


There is redundancy here.  Twelve vertices are stored, but the device 
is really only composed of four.
While an inefficient representation,  
this is probably not worth your time fixing. 2,5,8
SolidWorks exports a lazy array of vertices.

 vx ,1 v y ,1 vz ,1 
v v y ,2 vz ,2 
V 
x ,2 4,9,11
  
  1,6,12
vx , M vy,M vz , M 
3,7,10

Lecture 19 20

10
9/13/2018

Compact Array of Vertices


 vx ,1 v y ,1 vz ,1  2
v v y ,2 vz ,2 
V 
x ,2
2
 vx ,3 v y ,3 vz ,3 
  2
vx ,4 v y ,4 vz ,4 
2
4
Array of Vertices, V 1

1 3
1
3
3 4

1
Lecture 19 3 21

Array of Faces
5
 n1,1 n1,2 n1,3 
n n2,2 n2,3  all integers
F 
2,1
8
  
  2
 nN ,1 nN ,2 nN ,3 
2
4
F is an array  6
indicating the array 
indices of the 
vertices defining  9
the facet.
4

1 7
1
3
N is the total  11
3
number of 
faces. 12
Lecture 19 10 22

11
9/13/2018

Example of Lazy Arrays

2,5,8

4,9,11

1,6,12

3,7,10

Lecture 19 23

Example of Compact Arrays

3
This can make a very large difference for large and complex objects.

Lecture 19 24

12
9/13/2018

Eliminating Redundant Vertices


Redundant vertices are easily eliminated using MATLAB’s built in 
unique() command.
Identifies unique rows in V
and eliminates them from V.
% ELIMINATE REDUNDENT VERTICES
[V,indm,indn] = unique(V,'rows');
Corrects indices in F that 
F = indn(F);
referenced eliminated vertices.

indm – indices of rows in V that correspond to unique vertices.


indn – Map of row indices in original V to new row indices in the reduced V.

Lecture 19 25

STL Files Generated by


SolidWorks
For some reason, Solidworks does not use the z‐axis as the vertical 
axis.  
For convenience, STL files can be easily reoriented.
% REORIENT SOLIDWORKS AXES TO MATLAB AXES
Y = V(:,2);
V(:,2) = V(:,3);
V(:,3) = Y;

Orientation in SolidWorks Imported Orientation Adjusted Orientation

Lecture 19 26

13
9/13/2018

Visualizing
Surface Meshes
in MATLAB

How to Draw the Object


Given the faces and vertices, the object can be drawn in MATLAB 
using the patch() command.
% DRAW STRUCTURE USING PATCHES
P = patch('faces',F,'vertices',V);
set(P,'FaceColor','b','FaceAlpha',0.5);
set(P,'EdgeColor','k','LineWidth',2);

Lecture 19 28

14
9/13/2018

Generating
Faces and
Vertices Using
MATLAB

MATLAB Surfaces
Surfaces composed of square facets are stored in X, Y, and Z arrays.

The surface shown is constructed 
of arrays that are all 5×5.

Lecture 19 30

15
9/13/2018

Direct Construction of the


Surface Mesh
MATLAB has a number of built‐in commands for generating surfaces.  
Some of these are cylinder(), sphere() and ellipsoid().
% CREATE A UNIT SPHERE
[X,Y,Z] = sphere(41);

Surfaces can be converted to triangular patches (facets and vertices) 
using the surf2patch() function.
% CONVERT TO PATCH
[F,V] = surf2patch(X,Y,Z,’triangles’);

The faces and vertices can be directly visualized using the patch()
function.
% VISUALIZE FACES AND VERTICES
h = patch('faces',F,'vertices',V);
set(h,'FaceColor',[0.5 0.5 0.8],'EdgeColor','k');
Lecture 19 31

Grid  Surface Mesh


3D objects on a grid can be converted to a surface mesh using the 
command isosurface().
% 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);

Object on 3D Grid Surface Mesh
Lecture 19 32

16
9/13/2018

May Need isocaps()


When 3D objects extend to the edge of the grid, you may need to 
use isocaps() in addition to isosurface().

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);

Lecture 19 33

Combining Faces and Vertices


from Two Objects
There are no functions in MATLAB to perform Boolean operations on multiple 
meshes.  We can, however, combine the faces and vertices from two objects.  Be 
careful this does not result in overlaps or gaps between objects.

Correctly Incorrectly
F1 and V1 F2 and V2 Combined Combined

% COMBINE FACES AND VERTICES % WRONG WAY TO


F3 = [ F1 ; F2+length(V1(:,1)) ] % COMBINE FACES AND VERTICES
F3 = [ F1 ; F2 ]
V3 = [ V1 ; V2 ] V3 = [ V1 ; V2 ]
Lecture 19 34

17
9/13/2018

Converting
Surface Meshes
to Objects on a
3D Grid

Example – Pyramid

ER(nx,ny,nz)
SolidWorks Model

Import STL into MATLAB Convert to Volume Object
Lecture 19 36

18
9/13/2018

Example – Dinosaur
ER(nx,ny,nz)

Import STL into MATLAB Convert to Volume Object
Lecture 19 37

MATLAB Functions for


“Voxelization” of STL Files
The Mathworks website has excellent functions for 
converting surface meshes to points on a 3D array.

Function for Voxelization
http://www.mathworks.com/matlabcentral/fileexchange/27390‐
mesh‐voxelisation

Lecture 19 38

19
9/13/2018

Converting
Images and 2D
Objects to STL

Load and Resize the Image

% LOAD IMAGE
B = imread(‘letter.jpg');

% RESIZE IMAGE
B = imresize(B,0.2);
[Nx,Ny,Nc] = size(B);

This will give us a coarser mesh in order 
to be faster and more memory efficient.

Lecture 19 40

20
9/13/2018

Flatten the Image

Images loaded from file usually contain RGB 
information making them 3D arrays.  These arrays 
must be converted to flat 2D arrays before 
meshing.

% FLATTEN COLOR IMAGE


B = double(B);
B = B(:,:,1) + B(:,:,2) + B(:,:,3);
B = 1 - B/765;

Lecture 19 41

Stack the Image

We will mesh the image using isocaps(), but 
that function requires a 3D array.  So, we will stack 
this image to be two layers thick.

% STACK IMAGE
B(:,:,2) = B;

Lecture 19 42

21
9/13/2018

Mesh the Image Using


isocaps()

We only wish to mesh a single surface so we give 
isocaps() the additional input argument 
‘zmin’ to do this.

% CREATE 2D MESH
[F,V] = isocaps(ya,xa,[0 1],B,0.5,'zmin');
Lecture 19 43

Save the Mesh as an STL File


We can save this mesh as an STL file.

Lecture 19 44

22
9/13/2018

Extrude Using Blender (1 of 2)

1. Open Blender.exe.
2. File  Import  Stl (.stl)
3. Open the STL file you just created.

Lecture 19 45

Extrude Using Blender (2 of 2)

1. Select the object with right 
mouse click.
2. Press TAB to enter Edit mode.
3. Press ‘e’ to extrude mesh.
4. Press TAB again to exit Edit mode.
5. You can now edit your object
or export as a new 3D STL file.

Lecture 19 46

23
9/13/2018

Point Clouds

What is a Point Cloud?


Klein Bottle (see MATLAB demos) Point Cloud Description of a Klein Bottle

Point clouds represent the outside surface of object as a set of vertices defined by X, Y, and Z 
coordinates.  They are typically generated by 3D scanners, but can also be used to export 3D 
objects from MATLAB into SolidWorks or other CAD programs.

Lecture 19 48

24
9/13/2018

Other Examples of Point Clouds

Lecture 19 49

Point Cloud Data


The position of all the points in the point 
cloud can be stored in an array.
 x1 y1 z1 
x y2 z2 
 2
PC   x3 y3 z3 
 
  
 xN yN z N 

PC =
0.1200 0.0000 0.7071
0.1159 -0.0311 0.7071
0.0311 -0.1159 0.7071
0.0000 -0.1200 0.7071
-0.0311 -0.1159 0.7071
-0.0600 -0.1039 0.7071

Lecture 19 50

25
9/13/2018

Saving Point Cloud Data to File


(1 of 2)
Point cloud data files are comma separated 
text files with the extension “.xyz”

These can be easily generated using the 
built‐in MATLAB command csvwrite().

% SAVE POINT CLOUD AS A COMMA SEPERATED FILE


PC = [ X Z Y ];
dlmwrite('pointcloud.xyz',PC,'delimiter',',','newline','pc');

PC = [ X Y Z ]; PC = [ X Z Y ];

Lecture 19 51

Saving Point Cloud Data to File


(2 of 2)
SolidWorks wants to see an X Y Z at 
the start of the file.

You can add this manually using 
Notepad or write a more sophisticated 
MATLAB text file creator.

Lecture 19 52

26
9/13/2018

Activate ScanTo3D Add-In in


SolidWorks
First, you will need to activate the 
ScanTo3D in SolidWorks.

Click ToolsAdd‐Ins.

Check the Scanto3D check box.

Click OK.

Lecture 19 53

Open the Point Cloud File

Lecture 19 54

27
9/13/2018

Run the Mesh Prep Wizard

ToolsScanTo3DMesh Prep Wizard…
1. Run the Mesh Prep Wizard.
2. Select the point cloud.
3. Click the right array button.
4. Orientation method, select None 
because we did this in MATLAB.
5. Noise removal, zero assuming 
geometry came from MATLAB.
6. Work through all options.
7. Click the green check mark to finish.

Lecture 19 55

Point Cloud Density

Lecture 19 56

28
9/13/2018

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.

Lecture 19 57

Importing
Custom
Polygons into
SolidWorks

29
9/13/2018

The Problem
Suppose we calculate the vertices of a polygon from an optimization 
in MATLAB.  

How do we important the 
vertices so that the polygon 
can be imported exactly into 
Solidworks so that it can be 
extruded, cut, modified, etc.?

There is no feature in 
SolidWorks to do this!!

Lecture 19 59

Example Application
Placing a GMR filter onto  Grating period is spatially 
a curved surface. varied to compensate.
x  sin  x R  
inc  x    tan 1  
R 1  d R  cos  x R  
K  x   K 0  k0 ninc sin inc  x  
x
  x    K  x  dx
0

 1 cos   x    cos  f 


r  x  
r cos   x    cos  f 

R. C. Rumpf, M. Gates, C. L. Kozikowski, W. A. Davis, 
"Guided‐Mode Resonance Filter Compensated to Operate 
on a Curved Surface," PIER C, Vol. 40, pp. 93‐103, 2013.

Lecture 19 60

30
9/13/2018

Be Careful About XYZ Curves


There is a feature in SolidWorks “Curve Through XYZ Points” that 
appears to import discrete points, but it fits the points to a spline.  
This effectively rounds any corners and may produce intersections 
that cannot be rendered.

This should be a square! The four points are fit to a spline!
Lecture 19 61

Step 1 – Define the Polygon


Create an N×3 array in memory which contains all of the points 
around the perimeter of the polygon. N is the number of points.

P =
0.6448 0 0
1.0941 0.3758 0
1.3427 1.0452 0
0.3642 0.5573 0
0.4753 1.8765 0
-0.0651 0.7853 0
-0.5258 1.1984 0
-0.4824 0.5240 0
-0.8912 0.4822 0
-0.9966 0.1664 0
-0.9087 -0.1517 0
-1.0666 -0.5774 0
-1.1762 -1.2777 0
-0.5403 -1.2314 0
-0.1403 -1.6931 0
0.3818 -1.5074 0
0.7795 -1.1927 0
0.8293 -0.6455 0
1.0972 -0.3766 0
0.6448 -0.0000 0

Lecture 19 62

31
9/13/2018

Step 2 – Save as an IGES from


MATLAB
There is a function called igesout() available for free download 
from the Mathworks website.  This will save your polygon to an IGES 
file.

% SAVE IGES FILE


igesout(P,'poly');

This creates a file called “poly.igs” in your working directory.

Lecture 19 63

Step 3 – Open the IGES in


SolidWorks (1 of 2)
Select the IGES file type in the [Open] file dialog.  Then click on the 
[Options…] button. 

Lecture 19 64

32
9/13/2018

Step 3 – Open the IGES in


SolidWorks (2 of 2)
1. Make sure that 
“Surface/solid 
entities” is 
unchecked 
2. Ensure that “Free 
point/curve entities” 
is checked.  
3. Click on the “Import 
as sketch(es) radio 
button.

Open the file.

Lecture 19 65

Step 4 – Convert to a Standard


2D Sketch
The polygon imports as a 3D sketch by default.
Edit the 3D sketch and copy.
Open a new 2D sketch and paste.
Now you can do whatever you wish with the sketch!  Extrude, 
revolve, cut extrude, etc.

Lecture 19 66

33
9/13/2018

STL to CAD
Conversion

Import STL Into MeshLab


1. Open meshlab.exe.
2. File  Import Mesh
3. Open the 3D STL file you just created.

Lecture 19 68

34
9/13/2018

Convert to SolidWorks Part


1. Open meshlab.exe.
2. File  Export Mesh As
3. Select DXF file type.
4. Save mesh.
5. Launch SolidWorks.
6. File  Open
7. Select DXF file.
8. Select ‘Import to a new part as:’ 
then ‘3D curves or model’ 
then ‘Next >.’
9. Select ‘All Layers’

Lecture 19 69

Exporting STL
from Blender with
Proper Units

35
9/13/2018

Properly Sizing STL Files in


Blender

Lecture 19 71

36

You might also like