-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
356 lines (308 loc) · 10.3 KB
/
main.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"compress/gzip"
"encoding/json"
"flag"
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/jonnenauha/obj-simplify/objectfile"
"github.com/pkg/profile"
)
var (
StartParams = startParams{
Gzip: -1,
Epsilon: 1e-6,
}
ApplicationName = "obj-simplify"
ApplicationURL = "https://github.com/jonnenauha/" + ApplicationName
Version string
VersionHash string
VersionDate string
Processors = []*processor{
&processor{Processor: Duplicates{}},
&processor{Processor: Merge{}},
}
)
type startParams struct {
Input string
Output string
Workers int
Gzip int
Epsilon float64
Strict bool
Stdout bool
Quiet bool
NoProgress bool
CpuProfile bool
}
func (sp startParams) IsGzipEnabled() bool {
return sp.Gzip >= gzip.BestSpeed && sp.Gzip <= gzip.BestCompression
}
func init() {
version := false
StartParams.Workers = runtime.NumCPU() * 4
if StartParams.Workers < 4 {
StartParams.Workers = 4
}
flag.StringVar(&StartParams.Input,
"in", StartParams.Input, "Input file.")
flag.StringVar(&StartParams.Output,
"out", StartParams.Output, "Output file or directory.")
flag.IntVar(&StartParams.Workers,
"workers", StartParams.Workers, "Number of worker goroutines.")
flag.IntVar(&StartParams.Gzip,
"gzip", StartParams.Gzip, "Gzip compression level on the output for both -stdout and -out. <=0 disables compression, use 1 (best speed) to 9 (best compression) to enable.")
flag.Float64Var(&StartParams.Epsilon,
"epsilon", StartParams.Epsilon, "Epsilon for float comparisons.")
flag.BoolVar(&StartParams.Strict,
"strict", StartParams.Strict, "Errors out on spec violations, otherwise continues if the error is recoverable.")
flag.BoolVar(&StartParams.Stdout,
"stdout", StartParams.Stdout, "Write output to stdout. If enabled -out is ignored and logging directed to stderr. Use -quiet if you can't separate stdout from stderr (e.g. non-trivial in Windows).")
flag.BoolVar(&StartParams.Quiet,
"quiet", StartParams.Quiet, "Silence stdout printing.")
flag.BoolVar(&StartParams.NoProgress,
"no-progress", StartParams.NoProgress, "No shell progress bars.")
flag.BoolVar(&StartParams.CpuProfile,
"cpu-profile", StartParams.CpuProfile, "Record ./cpu.pprof profile.")
flag.BoolVar(&version,
"version", false, "Print version and exit, ignores -quiet.")
// -no-xxx to disable post processors
for _, processor := range Processors {
flag.BoolVar(&processor.Disabled, processor.NameCmd(), processor.Disabled, processor.Desc())
}
flag.Parse()
initLogging(!StartParams.Stdout)
// -version: ignores -stdout as we are about to exit
if version {
fmt.Printf("%s %s\n", ApplicationName, getVersion(true))
os.Exit(0)
}
if StartParams.Workers < 1 {
logFatal("-workers must be a positive number, given: %d", StartParams.Workers)
}
// -gzip
if StartParams.Gzip < -1 || StartParams.Gzip > gzip.BestCompression {
logFatal("-gzip must be -1 to 9, given: %d", StartParams.Gzip)
}
// -in
StartParams.Input = cleanPath(StartParams.Input)
if len(StartParams.Input) == 0 {
logFatal("-in missing")
} else if !fileExists(StartParams.Input) {
logFatal("-in file %q does not exist", StartParams.Input)
}
// -out
if !StartParams.Stdout {
if len(StartParams.Output) > 0 {
StartParams.Output = cleanPath(StartParams.Output)
} else {
if iExt := strings.LastIndex(StartParams.Input, "."); iExt != -1 {
StartParams.Output = StartParams.Input[0:iExt] + ".simplified" + StartParams.Input[iExt:]
} else {
StartParams.Output = StartParams.Input + ".simplified"
}
}
// don't allow user to overwrite source file, this app can be destructive and should
// not overwrite the source files. If user really wants to do this, he can rename the output file.
if StartParams.Input == StartParams.Output {
logFatal("Overwriting input file is not allowed, both input and output point to %s\n", StartParams.Input)
}
}
}
func getVersion(date bool) (version string) {
if Version == "" {
return "dev"
}
version = fmt.Sprintf("v%s (%s)", Version, VersionHash)
if date {
version += " " + VersionDate
}
return version
}
type processor struct {
Processor
Disabled bool
}
func (p *processor) NameCmd() string {
return "no-" + strings.ToLower(p.Name())
}
type Processor interface {
Name() string
Desc() string
Execute(obj *objectfile.OBJ) error
}
func main() {
// cpu profiling for development: github.com/pkg/profile
if StartParams.CpuProfile {
defer profile.Start(profile.ProfilePath(".")).Stop()
}
if b, err := json.MarshalIndent(StartParams, "", " "); err == nil {
logInfo("\n%s %s %s", ApplicationName, getVersion(false), b)
} else {
logFatalError(err)
}
type timing struct {
Step string
Duration time.Duration
}
var (
start = time.Now()
pre = time.Now()
timings = []timing{}
timeStep = func(step string) {
timings = append(timings, timing{Step: step, Duration: time.Now().Sub(pre)})
pre = time.Now()
}
)
// parse
obj, linesParsed, err := ParseFile(StartParams.Input)
if err != nil {
logFatalError(err)
}
timeStep("Parse")
// store stats before post-processing
preStats := obj.Stats()
// @todo this is ugly, maybe the face objects could be marked somehow.
// we want to show real stats, not faked object count stats at the end
preStats.Objects = ObjectsParsed
preStats.Groups = GroupsParsed
// post processing
for pi, processor := range Processors {
logInfo(" ")
if processor.Disabled {
logInfo("processor #%d: %s - Disabled", pi+1, processor.Name())
continue
}
logInfo("processor #%d: %s", pi+1, processor.Name())
logFatalError(processor.Execute(obj))
timeStep(processor.Name())
}
postStats := obj.Stats()
// write file out
var (
w = &Writer{obj: obj}
linesWritten int
errWrite error
)
if StartParams.Stdout {
linesWritten, errWrite = w.WriteTo(os.Stdout)
} else {
linesWritten, errWrite = w.WriteFile(StartParams.Output)
}
logFatalError(errWrite)
timeStep("Write")
// print stats etc
logInfo(" ")
durationTotal := time.Since(start)
for _, timing := range timings {
logResultsPostfix(timing.Step, formatDuration(timing.Duration), computeDurationPerc(timing.Duration, durationTotal)+"%%")
}
logResults("Total", formatDuration(durationTotal))
logGeometryStats(preStats.Geometry, postStats.Geometry)
logVertexDataStats(preStats, postStats)
logObjectStats(preStats, postStats)
logFileStats(linesParsed, linesWritten)
if StartParams.IsGzipEnabled() {
logInfo(" ")
logInfo("Gzip compression enabled with level %d.", StartParams.Gzip)
logInfo("Remeber to set 'Content-Encoding: gzip' header if you are hosting this file over HTTP.")
}
logInfo(" ")
}
func logGeometryStats(stats, postprocessed objectfile.GeometryStats) {
if !stats.IsEmpty() {
logInfo(" ")
}
if stats.Vertices > 0 {
logResultsIntPostfix("Vertices", postprocessed.Vertices, computeStatsDiff(stats.Vertices, postprocessed.Vertices))
}
if stats.Normals > 0 {
logResultsIntPostfix("Normals", postprocessed.Normals, computeStatsDiff(stats.Normals, postprocessed.Normals))
}
if stats.UVs > 0 {
logResultsIntPostfix("UVs", postprocessed.UVs, computeStatsDiff(stats.UVs, postprocessed.UVs))
}
if stats.Params > 0 {
logResultsIntPostfix("Params", postprocessed.Params, computeStatsDiff(stats.Params, postprocessed.Params))
}
}
func logObjectStats(stats, postprocessed objectfile.ObjStats) {
logInfo(" ")
// There is a special case where input has zero objects and we have created one or more.
if stats.Groups > 0 || postprocessed.Groups > 0 {
logResultsIntPostfix("Groups", postprocessed.Groups, computeStatsDiff(stats.Groups, postprocessed.Groups))
}
if stats.Objects > 0 || postprocessed.Objects > 0 {
logResultsIntPostfix("Objects", postprocessed.Objects, computeStatsDiff(stats.Objects, postprocessed.Objects))
}
}
func logVertexDataStats(stats, postprocessed objectfile.ObjStats) {
if stats.Faces > 0 || stats.Lines > 0 || stats.Points > 0 {
logInfo(" ")
}
if stats.Faces > 0 {
logResultsIntPostfix("Faces", postprocessed.Faces, computeStatsDiff(stats.Faces, postprocessed.Faces))
}
if stats.Lines > 0 {
logResultsIntPostfix("Lines", postprocessed.Lines, computeStatsDiff(stats.Lines, postprocessed.Lines))
}
if stats.Points > 0 {
logResultsIntPostfix("Points", postprocessed.Points, computeStatsDiff(stats.Points, postprocessed.Points))
}
}
func logFileStats(linesParsed, linesWritten int) {
logInfo(" ")
logResults("Lines input", formatInt(linesParsed))
if linesWritten < linesParsed {
logResultsPostfix("Lines output", formatInt(linesWritten), fmt.Sprintf("%-10s %s", formatInt(linesWritten-linesParsed), "-"+intToString(int(100-computePerc(float64(linesWritten), float64(linesParsed))))+"%%"))
} else {
logResultsPostfix("Lines output", formatInt(linesWritten), fmt.Sprintf("+%-10s %s", formatInt(linesWritten-linesParsed), "+"+intToString(int(computePerc(float64(linesWritten), float64(linesParsed))-100))+"%%"))
}
logInfo(" ")
sizeIn, sizeOut := fileSize(StartParams.Input), fileSize(StartParams.Output)
logResults("File input", formatBytes(sizeIn))
if !StartParams.Stdout {
if sizeOut < sizeIn {
logResultsPostfix("File output", formatBytes(sizeOut), fmt.Sprintf("%-10s %s", formatBytes(sizeOut-sizeIn), "-"+intToString(int(100-computePerc(float64(sizeOut), float64(sizeIn))))+"%%"))
} else {
logResultsPostfix("File output", formatBytes(sizeOut), fmt.Sprintf("+%-10s %s", formatBytes(sizeOut-sizeIn), "+"+intToString(int(computePerc(float64(sizeOut), float64(sizeIn))-100))+"%%"))
}
}
}
func computeStatsDiff(a, b int) string {
if a == b {
return ""
}
diff := b - a
perc := computePerc(float64(b), float64(a))
if perc >= 99.999999 {
// positive 0 decimals
return fmt.Sprintf("+%-7d", diff)
} else if perc <= 99.0 {
// negative 0 decimals
return fmt.Sprintf("%-7d -%d", diff, 100-int(perc)) + "%%"
}
// negative 2 decimals
return fmt.Sprintf("%-7d -%.2f", diff, 100-perc) + "%%"
}
func computePerc(step, total float64) float64 {
if step == 0 {
return 0.0
} else if total == 0 {
return 100.0
}
return (step / total) * 100.0
}
func computeFloatPerc(step, total float64) string {
perc := computePerc(step, total)
if perc < 1.0 {
return fmt.Sprintf("%.2f", perc)
}
return intToString(int(perc))
}
func computeDurationPerc(step, total time.Duration) string {
return computeFloatPerc(step.Seconds(), total.Seconds())
}