My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package toxi.geom.util;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import toxi.geom.Vec3D;

/**
* A simple, but flexible and memory efficient exporter for binary STL files.
* Custom color support is implemented via the STLcolorModel interface and the
* exporter comes with the 2 most common format variations defined by the
* DEFAULT and MATERIALISE constants.
*
* The minimal design of this exporter means it does not build an extra list of
* faces in RAM and so is able to easily export models with millions of faces.
*
* http://en.wikipedia.org/wiki/STL_(file_format)
*
* @author kschmidt
*
*/
public class STLWriter {

public static final STLColorModel DEFAULT = new DefaultSTLColorModel();

public static final STLColorModel MATERIALISE = new MaterialiseSTLColorModel(
0xffffffff);

protected DataOutputStream ds;

protected Vec3D scale = new Vec3D(1, 1, 1);

protected boolean useInvertedNormals = false;

protected byte[] buf = new byte[4];

protected STLColorModel colorModel;

public STLWriter() {
this(DEFAULT);
}

public STLWriter(STLColorModel cm) {
colorModel = cm;
}

public void beginSave(String fn, int numFaces) {
try {
ds = new DataOutputStream(new FileOutputStream(fn));
writeHeader(numFaces);
} catch (Exception e) {
e.printStackTrace();
}
}

public void endSave() {
try {
ds.flush();
ds.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void face(Vec3D a, Vec3D b, Vec3D c) {
face(a, b, c, 0);
}

public void face(Vec3D a, Vec3D b, Vec3D c, int rgb) {
try {
// normal
Vec3D normal = b.sub(a).cross(c.sub(a)).normalize();
if (useInvertedNormals) {
normal.invert();
}
writeVector(normal);
// vertices
writeVector(a);
writeVector(b);
writeVector(c);
// vertex attrib (color)
if (rgb != 0) {
writeShort(colorModel.formatRGB(rgb));
} else {
writeShort(0);
}
} catch (IOException e) {
e.printStackTrace();
}
}

private final void prepareBuffer(int a) {
buf[3] = (byte) (a >>> 24);
buf[2] = (byte) (a >> 16 & 0xff);
buf[1] = (byte) (a >> 8 & 0xff);
buf[0] = (byte) (a & 0xff);
}

public void setScale(float s) {
scale.set(s, s, s);
}

public void setScale(Vec3D s) {
scale.set(s);
}

public void useInvertedNormals(boolean state) {
useInvertedNormals = state;
}

protected void writeFloat(float a) throws IOException {
prepareBuffer(Float.floatToRawIntBits(a));
ds.write(buf, 0, 4);
}

protected void writeHeader(int num) throws IOException {
byte[] header = new byte[80];
colorModel.formatHeader(header);
ds.write(header, 0, 80);
writeInt(num);
}

protected void writeInt(int a) throws IOException {
prepareBuffer(a);
ds.write(buf, 0, 4);
}

protected void writeShort(int a) throws IOException {
ds.writeByte(a & 0xff);
ds.writeByte(a >> 8 & 0xff);
}

protected void writeVector(Vec3D v) {
try {
writeFloat(v.x * scale.x);
writeFloat(v.y * scale.y);
writeFloat(v.z * scale.z);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Change log

r188 by toxmeister on Feb 27, 2009   Diff
renaming src.geom folder into src.core WRT
the replacement of geomutils with
toxiclibscore packages, which now also
include the formerly separate prefutils
classes. also removing toxi.geom.volume
package from this source folder
Go to: 
Project members, sign in to write a code review

Older revisions

r179 by toxmeister on Feb 19, 2009   Diff
fixed little endian bug in STLWriter
and minor changes to IsoSurface
r158 by toxmeister on Dec 18, 2008   Diff
switching back to the american
spelling (2nd time) and renaming Color
into TColor in the interim until the
naming conflict caused by Processing's
pre-processor autoimport is
...
r72 by toxmeister on Jun 13, 2008   Diff
renamed main src folder into src.geom
to fix some weird build path reference
errors
All revisions of this file

File info

Size: 3262 bytes, 143 lines
Powered by Google Project Hosting