-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwriter.go
326 lines (274 loc) · 7.68 KB
/
writer.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package ply
import (
"encoding/binary"
"fmt"
"io"
"math"
"github.com/EliCDavis/polyform/formats/txt"
"github.com/EliCDavis/polyform/modeling"
)
// ============================================================================
type PropertyWriter interface {
MeshQualifies(mesh modeling.Mesh) bool
Properties() []Property
build(mesh modeling.Mesh, endian Format) builtPropertyWriter
}
type builtPropertyWriter interface {
Write(out io.Writer, i int) error
}
// ============================================================================
type MeshWriter struct {
Format Format
Properties []PropertyWriter
// Whether or not to save extra ply data that wasn't defined by the
// property writers
WriteUnspecifiedProperties bool
}
func (mw MeshWriter) Write(mesh modeling.Mesh, texture string, writer io.Writer) error {
writers := make([]PropertyWriter, 0)
properties := make([]Property, 0)
builtWriters := make([]builtPropertyWriter, 0)
claimedV1 := make(map[string]bool)
claimedV2 := make(map[string]bool)
claimedV3 := make(map[string]bool)
claimedV4 := make(map[string]bool)
for _, prop := range mw.Properties {
if !prop.MeshQualifies(mesh) {
continue
}
writers = append(writers, prop)
switch v := prop.(type) {
case Vector4PropertyWriter:
claimedV4[v.ModelAttribute] = true
case Vector3PropertyWriter:
claimedV3[v.ModelAttribute] = true
case Vector2PropertyWriter:
claimedV2[v.ModelAttribute] = true
case Vector1PropertyWriter:
claimedV1[v.ModelAttribute] = true
case *Vector4PropertyWriter:
claimedV4[v.ModelAttribute] = true
case *Vector3PropertyWriter:
claimedV3[v.ModelAttribute] = true
case *Vector2PropertyWriter:
claimedV2[v.ModelAttribute] = true
case *Vector1PropertyWriter:
claimedV1[v.ModelAttribute] = true
default:
panic("what is this type")
}
}
if mw.WriteUnspecifiedProperties {
for _, p := range mesh.Float4Attributes() {
if claimedV4[p] {
continue
}
writers = append(writers, Vector4PropertyWriter{
ModelAttribute: p,
Type: Float,
PlyPropertyX: fmt.Sprintf("%s_0", p),
PlyPropertyY: fmt.Sprintf("%s_1", p),
PlyPropertyZ: fmt.Sprintf("%s_2", p),
PlyPropertyW: fmt.Sprintf("%s_3", p),
})
}
for _, p := range mesh.Float3Attributes() {
if claimedV3[p] {
continue
}
writers = append(writers, Vector3PropertyWriter{
ModelAttribute: p,
Type: Float,
PlyPropertyX: fmt.Sprintf("%s_0", p),
PlyPropertyY: fmt.Sprintf("%s_1", p),
PlyPropertyZ: fmt.Sprintf("%s_2", p),
})
}
for _, p := range mesh.Float2Attributes() {
if claimedV2[p] || p == modeling.TexCoordAttribute {
continue
}
writers = append(writers, Vector2PropertyWriter{
ModelAttribute: p,
Type: Float,
PlyPropertyX: fmt.Sprintf("%s_0", p),
PlyPropertyY: fmt.Sprintf("%s_1", p),
})
}
for _, p := range mesh.Float1Attributes() {
if claimedV1[p] {
continue
}
writers = append(writers, Vector1PropertyWriter{
ModelAttribute: p,
Type: Float,
PlyProperty: p,
})
}
}
for _, prop := range writers {
builtWriters = append(builtWriters, prop.build(mesh, mw.Format))
properties = append(properties, prop.Properties()...)
}
attributeLength := mesh.AttributeLength()
header := Header{
Format: mw.Format,
Elements: []Element{
{
Name: VertexElementName,
Count: int64(attributeLength),
Properties: properties,
},
},
Comments: []string{},
}
if texture != "" {
header.Comments = append(header.Comments, fmt.Sprintf("TextureFile %s", texture))
}
header.Comments = append(header.Comments, "Created with github.com/EliCDavis/polyform")
if mesh.Topology() == modeling.TriangleTopology {
faceProps := []Property{
ListProperty{
PropertyName: "vertex_indices",
CountType: UChar,
ListType: Int,
},
}
if mesh.HasFloat2Attribute(modeling.TexCoordAttribute) {
faceProps = append(faceProps, ListProperty{
PropertyName: "texcoord",
CountType: UChar,
ListType: Float,
})
}
header.Elements = append(header.Elements, Element{
Name: "face",
Count: int64(mesh.PrimitiveCount()),
Properties: faceProps,
})
}
err := header.Write(writer)
if err != nil {
return err
}
spaceByte := []byte{' '}
newLineByte := []byte{'\n'}
for i := 0; i < attributeLength; i++ {
for propI, prop := range builtWriters {
err = prop.Write(writer, i)
if err != nil {
return err
}
if mw.Format == ASCII {
if propI < len(builtWriters)-1 {
writer.Write(spaceByte)
} else {
writer.Write(newLineByte)
}
}
}
}
if mesh.Topology() != modeling.TriangleTopology {
return nil
}
switch mw.Format {
case ASCII:
return writeAsciiTriTopo(writer, mesh)
case BinaryLittleEndian, BinaryBigEndian:
return writeBinaryTriTopo(writer, mesh, mw.Format)
}
return nil
}
func writeBinaryTriTopo(out io.Writer, model modeling.Mesh, format Format) error {
if model.Topology() != modeling.TriangleTopology {
return nil
}
var endian binary.ByteOrder = binary.LittleEndian
if format == BinaryBigEndian {
endian = binary.BigEndian
}
indices := model.Indices()
if model.HasFloat2Attribute(modeling.TexCoordAttribute) {
texData := model.Float2Attribute(modeling.TexCoordAttribute)
buf := make([]byte, 1+(3*4)+1+(2*4*3))
buf[0] = 3
buf[13] = 6
for i := 0; i < indices.Len(); i += 3 {
endian.PutUint32(buf[1:], uint32(indices.At(i)))
endian.PutUint32(buf[5:], uint32(indices.At(i+1)))
endian.PutUint32(buf[9:], uint32(indices.At(i+2)))
p1 := texData.At(i).ToFloat32()
endian.PutUint32(buf[14:], math.Float32bits(p1.X()))
endian.PutUint32(buf[18:], math.Float32bits(p1.Y()))
p2 := texData.At(i + 1).ToFloat32()
endian.PutUint32(buf[22:], math.Float32bits(p2.X()))
endian.PutUint32(buf[26:], math.Float32bits(p2.Y()))
p3 := texData.At(i + 2).ToFloat32()
endian.PutUint32(buf[30:], math.Float32bits(p3.X()))
endian.PutUint32(buf[34:], math.Float32bits(p3.Y()))
_, err := out.Write(buf)
if err != nil {
return err
}
}
return nil
}
buf := make([]byte, 1+(3*4))
buf[0] = 3
for i := 0; i < indices.Len(); i += 3 {
endian.PutUint32(buf[1:], uint32(indices.At(i)))
endian.PutUint32(buf[5:], uint32(indices.At(i+1)))
endian.PutUint32(buf[9:], uint32(indices.At(i+2)))
_, err := out.Write(buf)
if err != nil {
return err
}
}
return nil
}
func writeAsciiTriTopo(out io.Writer, model modeling.Mesh) error {
writer := txt.NewWriter(out)
if model.Topology() != modeling.TriangleTopology {
return nil
}
if model.HasFloat2Attribute(modeling.TexCoordAttribute) {
for i := 0; i < model.PrimitiveCount(); i++ {
writer.StartEntry()
writer.String("3 ")
tri := model.Tri(i)
writer.Int(tri.P1())
writer.Space()
writer.Int(tri.P2())
writer.Space()
writer.Int(tri.P3())
writer.String(" 6 ")
writer.Float64(tri.P1Vec2Attr(modeling.TexCoordAttribute).X())
writer.Space()
writer.Float64(tri.P1Vec2Attr(modeling.TexCoordAttribute).Y())
writer.Space()
writer.Float64(tri.P2Vec2Attr(modeling.TexCoordAttribute).X())
writer.Space()
writer.Float64(tri.P2Vec2Attr(modeling.TexCoordAttribute).Y())
writer.Space()
writer.Float64(tri.P3Vec2Attr(modeling.TexCoordAttribute).X())
writer.Space()
writer.Float64(tri.P3Vec2Attr(modeling.TexCoordAttribute).Y())
writer.NewLine()
writer.FinishEntry()
}
return writer.Error()
}
for i := 0; i < model.PrimitiveCount(); i++ {
writer.StartEntry()
writer.String("3 ")
tri := model.Tri(i)
writer.Int(tri.P1())
writer.Space()
writer.Int(tri.P2())
writer.Space()
writer.Int(tri.P3())
writer.NewLine()
writer.FinishEntry()
}
return writer.Error()
}