-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstruct.jl
148 lines (141 loc) · 4.93 KB
/
construct.jl
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
# construct BVH and export to binary file
begin
import Pkg
Pkg.activate(@__DIR__)
end
using ArgParse
include("src/AccelerateRT.jl")
using .AccelerateRT
function parseCommandline()
s = ArgParseSettings()
@add_arg_table! s begin
"--algMiddle"
help = "construct simple BVH with middle criteria"
action = :store_true
"--algMedian"
help = "construct simple BVH with media criteria"
action = :store_true
"--algSAH"
help = "construct BVH with SAH"
action = :store_true
"--algSAHM"
help = "construct BVH with modifed SAH (new algorithm)"
action = :store_true
"--save"
help = "save constructed structure"
action = :store_true
"--skip"
help = "skip existing structure"
action = :store_true
"--show"
help = "print structure"
action = :store_true
"model"
help = "obj model path"
required = true
end
return parse_args(s)
end
function main()
args = parseCommandline()
modelPath = args["model"]
model = loadObjFile(modelPath)
describeModel(model)
bvhPath = joinpath("structures", replace(strip(modelPath), r"[^a-zA-Z0-9]" => "_"))
bvh = nothing
if args["algMiddle"]
ext = ".middle.jld2"
path = bvhPath * ext
ordered = nothing
if args["skip"] && isfile(path)
println("Loading existing $path")
data = loadFileBinary(path)
bvh = data["BVH"]
ordered = data["Ordered"]
else
println("Constructing BVHSimple with middle criteria")
primitives = createPrimitives(model)
ordered = Vector3{UInt32}[]
bvh = BVH.constructBVHSimple!(primitives, ordered, 1, length(primitives), :middle)
end
if args["save"]
println("Saving to $path")
saveFileBinary(path, Dict("BVH" => bvh, "Ordered" => ordered, "Vertices" => model.vertices))
end
elseif args["algMedian"]
ext = ".median.jld2"
path = bvhPath * ext
ordered = nothing
if args["skip"] && isfile(path)
println("Loading existing $path")
data = loadFileBinary(path)
bvh = data["BVH"]
ordered = data["Ordered"]
else
println("Constructing BVHSimple with median criteria")
primitives = createPrimitives(model)
ordered = Vector3{UInt32}[]
bvh = BVH.constructBVHSimple!(primitives, ordered, 1, length(primitives), :median)
end
if args["save"]
println("Saving to $path")
saveFileBinary(path, Dict("BVH" => bvh, "Ordered" => ordered, "Vertices" => model.vertices))
end
elseif args["algSAH"]
ext = ".sah.jld2"
path = bvhPath * ext
ordered = nothing
if args["skip"] && isfile(path)
println("Loading existing $path")
data = loadFileBinary(path)
bvh = data["BVH"]
ordered = data["Ordered"]
else
println("Constructing BVH with SAH")
primitives = createPrimitives(model)
ordered = Vector3{UInt32}[]
bvh = BVH.constructBVHSAH!(primitives, ordered, 1, length(primitives))
end
if args["save"]
println("Saving to $path")
saveFileBinary(path, Dict("BVH" => bvh, "Ordered" => ordered, "Vertices" => model.vertices))
end
elseif args["algSAHM"]
ext = ".sahm.jld2"
path = bvhPath * ext
ordered = nothing
if args["skip"] && isfile(path)
println("Loading existing $path")
data = loadFileBinary(path)
bvh = data["BVH"]
ordered = data["Ordered"]
else
println("Constructing BVH with modified SAH (new algorithm)")
primitives = createPrimitives(model)
ordered = Vector3{UInt32}[]
bvh = BVH.constructBVHModified!(primitives, ordered, 1, length(primitives))
end
if args["save"]
println("Saving to $path")
saveFileBinary(path, Dict("BVH" => bvh, "Ordered" => ordered, "Vertices" => model.vertices))
end
end
if bvh !== nothing && args["show"]
println("==============")
println("BVH Structure:")
BVH.displayBVH(bvh)
println("==============")
end
BVH.describeBVH(bvh)
end
function createPrimitives(model::ModelData)
primitives = Vector{BVH.BVHPrimitive{Float32, UInt32}}(undef, length(model.facesV))
for idx in range(1, length(model.facesV))
face = model.facesV[idx]
bounds = BVH.AABB(model.vertices[face.x], model.vertices[face.y], model.vertices[face.z])
centroid = BVH.computeCentroid(bounds)
primitives[idx] = BVH.BVHPrimitive{Float32, UInt32}(face, bounds, centroid)
end
return primitives
end
main()