-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
375 lines (355 loc) · 13.2 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"errors"
"fmt"
"gsbox/cmn"
"gsbox/gsplat"
"os"
"time"
)
func main() {
args := cmn.ParseArgs("-v", "-version", "--version", "-h", "-help", "--help")
gsplat.Args = args
if args.HasCmd("-v", "-version", "--version") && args.ArgCount == 2 {
version()
} else if args.HasCmd("-h", "-help", "--help") && args.ArgCount == 2 {
usage()
} else if args.HasCmd("p2s", "ply2splat") {
ply2splat(args)
} else if args.HasCmd("p2x", "ply2spx") {
ply2spx(args)
} else if args.HasCmd("p2z", "ply2spz") {
ply2spz(args)
} else if args.HasCmd("p2p", "ply2ply") {
ply2ply(args)
} else if args.HasCmd("s2p", "splat2ply") {
splat2ply(args)
} else if args.HasCmd("s2x", "splat2spx") {
splat2spx(args)
} else if args.HasCmd("s2z", "splat2spz") {
splat2spz(args)
} else if args.HasCmd("s2s", "splat2splat") {
splat2splat(args)
} else if args.HasCmd("x2p", "spx2ply") {
spx2ply(args)
} else if args.HasCmd("x2s", "spx2splat") {
spx2splat(args)
} else if args.HasCmd("x2z", "spx2spz") {
spx2spz(args)
} else if args.HasCmd("x2x", "spx2spx") {
spx2spx(args)
} else if args.HasCmd("z2p", "spz2ply") {
spz2ply(args)
} else if args.HasCmd("z2s", "spz2splat") {
spz2splat(args)
} else if args.HasCmd("z2x", "spz2spx") {
spz2spx(args)
} else if args.HasCmd("z2z", "spz2spz") {
spz2spz(args)
} else if args.HasCmd("info") {
plyInfo(args)
} else {
usage()
}
fmt.Print(cmn.NewVersionMessage)
}
func version() {
fmt.Println("")
fmt.Println("gsbox", cmn.VER)
fmt.Println("homepage", "https://github.com/gotoeasy/gsbox")
}
func usage() {
fmt.Println("")
fmt.Println("gsbox", cmn.VER)
fmt.Println("homepage", "https://github.com/gotoeasy/gsbox")
fmt.Println("")
fmt.Println("")
fmt.Println("Usage:")
fmt.Println(" gsbox [options]")
fmt.Println("")
fmt.Println("Options:")
fmt.Println(" p2s, ply2splat convert ply to splat")
fmt.Println(" p2x, ply2spx convert ply to spx")
fmt.Println(" p2z, ply2spz convert ply to spz")
fmt.Println(" p2p, ply2ply convert ply to ply")
fmt.Println(" s2p, splat2ply convert splat to ply")
fmt.Println(" s2x, splat2spx convert splat to spx")
fmt.Println(" s2z, splat2spz convert splat to spz")
fmt.Println(" s2s, splat2splat convert splat to splat")
fmt.Println(" x2p, spx2ply convert spx to ply")
fmt.Println(" x2s, spx2splat convert spx to splat")
fmt.Println(" x2z, spx2spz convert spx to spz")
fmt.Println(" x2x, spx2spx convert spx to spx")
fmt.Println(" z2p, spz2ply convert spz to ply")
fmt.Println(" z2s, spz2splat convert spz to splat")
fmt.Println(" z2x, spz2spx convert spz to spx")
fmt.Println(" z2z, spz2spz convert spz to spz")
fmt.Println(" info <file> display the model file information")
fmt.Println(" -i, --input <file> specify the input file")
fmt.Println(" -o, --output <file> specify the output file")
fmt.Println(" -c, --comment <text> specify the comment for ply/spx output")
fmt.Println(" -bs, --block-size <num> specify the block size for spx output (default 20480)")
fmt.Println(" -sh, --shDegree <num> specify the SH degree for ply/spx/spz output")
fmt.Println(" -f1, --flag1 <num> specify the header flag1 for spx output")
fmt.Println(" -f2, --flag2 <num> specify the header flag2 for spx output")
fmt.Println(" -f3, --flag3 <num> specify the header flag3 for spx output")
fmt.Println(" -v, --version display version information")
fmt.Println(" -h, --help display help information")
fmt.Println("")
fmt.Println("Examples:")
fmt.Println(" gsbox ply2splat -i /path/to/input.ply -o /path/to/output.splat")
fmt.Println(" gsbox s2x -i /path/to/input.splat -o /path/to/output.spx -c \"your comment\" -bs 10240")
fmt.Println(" gsbox x2z -i /path/to/input.spx -o /path/to/output.spz -sh 0")
fmt.Println(" gsbox z2p -i /path/to/input.spz -o /path/to/output.ply -c \"your comment\"")
fmt.Println(" gsbox info -i /path/to/file.spx")
fmt.Println("")
}
func plyInfo(args *cmn.OsArgs) {
// info
input := args.GetArgIgnorecase("-i", "--input")
if args.ArgCount == 3 {
input = args.LastParam
if cmn.EqualsIngoreCase(input, "info") {
input = args.GetArgByIndex(1)
}
}
if input == "" {
cmn.ExitOnError(errors.New("please specify the input ply file"))
}
if !cmn.IsExistFile(input) {
cmn.ExitOnError(errors.New("file not found: " + input))
}
isPly := cmn.Endwiths(input, ".ply", true)
isSpx := cmn.Endwiths(input, ".spx", true)
isSplat := cmn.Endwiths(input, ".splat", true)
isSpz := cmn.Endwiths(input, ".spz", true)
if isPly {
header, err := gsplat.ReadPlyHeaderString(input, 1024)
cmn.ExitOnError(err)
fmt.Print(header)
} else if isSpx {
header := gsplat.ParseSpxHeader(input)
fmt.Println(header.ToStringSpx())
} else if isSpz {
header, _ := gsplat.ReadSpz(input, true)
fmt.Println(header.ToString())
} else if isSplat {
fileInfo, err := os.Stat(input)
cmn.ExitOnError(err)
fileSize := fileInfo.Size()
if (fileSize)%32 > 0 {
cmn.ExitOnError(errors.New("invalid splat format"))
} else {
fmt.Println("SplatCount :", fileSize/32)
}
} else {
cmn.ExitOnError(errors.New("the input file must be (ply | splat | spx) format"))
}
}
func ply2splat(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
_, datas := gsplat.ReadPly(input, "ply-3dgs")
gsplat.Sort(datas)
gsplat.WriteSplat(output, datas)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func ply2spx(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
header, datas := gsplat.ReadPly(input, "ply-3dgs")
gsplat.Sort(datas)
comment := args.GetArgIgnorecase("-c", "--comment")
shDegree := getArgShDegree(header.MaxShDegree(), args)
flag1 := getArgFlag(0, args, "-f1", "--flag1")
flag2 := getArgFlag(0, args, "-f2", "--flag2")
flag3 := getArgFlag(0, args, "-f3", "--flag3")
gsplat.WriteSpx(output, datas, comment, shDegree, flag1, flag2, flag3)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func ply2spz(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
header, datas := gsplat.ReadPly(input, "ply-3dgs")
gsplat.Sort(datas)
shDegree := getArgShDegree(header.MaxShDegree(), args)
gsplat.WriteSpz(output, datas, shDegree)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func ply2ply(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
header, datas := gsplat.ReadPly(input, "ply-3dgs")
gsplat.Sort(datas)
comment := args.GetArgIgnorecase("-c", "--comment")
shDegree := getArgShDegree(header.MaxShDegree(), args)
gsplat.WritePly(output, datas, comment, shDegree)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func splat2ply(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
datas := gsplat.ReadSplat(input)
gsplat.Sort(datas)
comment := args.GetArgIgnorecase("-c", "--comment")
shDegree := getArgShDegree(0, args)
gsplat.WritePly(output, datas, comment, shDegree)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func splat2spx(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
datas := gsplat.ReadSplat(input)
gsplat.Sort(datas)
comment := args.GetArgIgnorecase("-c", "--comment")
shDegree := getArgShDegree(0, args)
flag1 := getArgFlag(0, args, "-f1", "--flag1")
flag2 := getArgFlag(0, args, "-f2", "--flag2")
flag3 := getArgFlag(0, args, "-f3", "--flag3")
gsplat.WriteSpx(output, datas, comment, shDegree, flag1, flag2, flag3)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func splat2spz(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
datas := gsplat.ReadSplat(input)
gsplat.Sort(datas)
gsplat.WriteSpz(output, datas, 0)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func splat2splat(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
datas := gsplat.ReadSplat(input)
gsplat.Sort(datas)
gsplat.WriteSplat(output, datas)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func spx2ply(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
header, datas := gsplat.ReadSpx(input)
gsplat.Sort(datas)
comment := args.GetArgIgnorecase("-c", "--comment")
shDegree := getArgShDegree(int(header.ShDegree), args)
gsplat.WritePly(output, datas, comment, shDegree)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func spx2splat(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
_, datas := gsplat.ReadSpx(input)
gsplat.Sort(datas)
gsplat.WriteSplat(output, datas)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func spx2spz(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
header, datas := gsplat.ReadSpx(input)
gsplat.Sort(datas)
shDegree := getArgShDegree(int(header.ShDegree), args)
gsplat.WriteSpz(output, datas, shDegree)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func spx2spx(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
header, datas := gsplat.ReadSpx(input)
gsplat.Sort(datas)
comment := args.GetArgIgnorecase("-c", "--comment")
shDegree := getArgShDegree(int(header.ShDegree), args)
flag1 := getArgFlag(0, args, "-f1", "--flag1")
flag2 := getArgFlag(0, args, "-f2", "--flag2")
flag3 := getArgFlag(0, args, "-f3", "--flag3")
gsplat.WriteSpx(output, datas, comment, shDegree, flag1, flag2, flag3)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func spz2ply(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
header, datas := gsplat.ReadSpz(input, false)
gsplat.Sort(datas)
comment := args.GetArgIgnorecase("-c", "--comment")
shDegree := getArgShDegree(int(header.ShDegree), args)
gsplat.WritePly(output, datas, comment, shDegree)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func spz2splat(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
_, datas := gsplat.ReadSpz(input, false)
gsplat.Sort(datas)
gsplat.WriteSplat(output, datas)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func spz2spx(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
header, datas := gsplat.ReadSpz(input, false)
gsplat.Sort(datas)
comment := args.GetArgIgnorecase("-c", "--comment")
shDegree := getArgShDegree(int(header.ShDegree), args)
flag1 := getArgFlag(0, args, "-f1", "--flag1")
flag2 := getArgFlag(0, args, "-f2", "--flag2")
flag3 := getArgFlag(0, args, "-f3", "--flag3")
gsplat.WriteSpx(output, datas, comment, shDegree, flag1, flag2, flag3)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func spz2spz(args *cmn.OsArgs) {
startTime := time.Now()
input := checkInputFileExists(args)
output := createOutputDir(args)
header, datas := gsplat.ReadSpz(input, false)
gsplat.Sort(datas)
shDegree := getArgShDegree(int(header.ShDegree), args)
gsplat.WriteSpz(output, datas, shDegree)
fmt.Println("Processing time conversion:", cmn.GetTimeInfo(time.Since(startTime).Milliseconds()))
}
func checkInputFileExists(args *cmn.OsArgs) string {
input := args.GetArgIgnorecase("-i", "--input")
cmn.ExitOnConditionError(input == "", errors.New(`please specify the input file`))
cmn.ExitOnConditionError(!cmn.IsExistFile(input), errors.New("file not found: "+input))
return input
}
func createOutputDir(args *cmn.OsArgs) string {
output := args.GetArgIgnorecase("-o", "--output")
cmn.ExitOnConditionError(output == "", errors.New(`please specify the output file`))
cmn.ExitOnError(cmn.MkdirAll(cmn.Dir(output)))
return output
}
func getArgShDegree(dataShDegree int, args *cmn.OsArgs) int {
shDegree := dataShDegree
if args.HasArg("-sh", "--shDegree") {
sh := cmn.StringToInt(args.GetArgIgnorecase("-sh", "--shDegree"), -1)
if sh >= 0 && sh <= 3 {
shDegree = sh
}
}
return shDegree
}
func getArgFlag(defaultFlag uint8, args *cmn.OsArgs, arg1 string, arg2 string) uint8 {
flag := defaultFlag
if args.HasArg(arg1, arg2) {
val := cmn.StringToInt(args.GetArgIgnorecase(arg1, arg2), -1)
if val >= 0 && val < 256 {
flag = uint8(val)
}
}
return flag
}