Commit 91886e96 authored by Jayant Khatkar's avatar Jayant Khatkar
Browse files

bug fixes in voxmap generation and structure of cost function code

parent 3ce8c051
...@@ -45,7 +45,7 @@ function problem(cons::Vector{contour}, max_layers::Int, min_dist::Number) ...@@ -45,7 +45,7 @@ function problem(cons::Vector{contour}, max_layers::Int, min_dist::Number)
G = LightGraphs.SimpleDiGraph(0) G = LightGraphs.SimpleDiGraph(0)
# separate contours into layers # separate contours into layers
layer_heights = sort(collect(Set([c.pos[1,3] for c in cons]))) layer_heights = sort(collect(Set([c.pos[end,3] for c in cons])))
layers = [[] for i in 1:length(layer_heights)] layers = [[] for i in 1:length(layer_heights)]
clayeri = [] clayeri = []
contour_trees = [] contour_trees = []
...@@ -96,18 +96,22 @@ end ...@@ -96,18 +96,22 @@ end
function seg_helper_orientation(p,q,r) function seg_helper_orientation(p,q,r)
val = (q[2]-p[2]) * (r[1]-q[1]) - (q[1]-p[1]) * (r[2]-q[2]) val = (q[2]-p[2]) * (r[1]-q[1]) - (q[1]-p[1]) * (r[2]-q[2])
if val > 0 if val > 0
# clockwise return 1 # clockwise
return 1
elseif val < 0 elseif val < 0
# anticlockwise return 2 # anticlockwise
return 2
else else
# colinear return 0 # colinear
return 0
end end
end end
function onseg(p,q,r)
# check if q lies on segment pr assuming 3 points are colinear
return ((q[1] <= max(p[1], r[1])) && (q[1] >= min(p[1], r[1])) &&
(q[2] <= max(p[2], r[2])) && (q[2] >= min(p[2], r[2])))
end
function seg_intersect(p1,q1,p2,q2) function seg_intersect(p1,q1,p2,q2)
o1 = seg_helper_orientation(p1, q1, p2) o1 = seg_helper_orientation(p1, q1, p2)
...@@ -115,10 +119,12 @@ function seg_intersect(p1,q1,p2,q2) ...@@ -115,10 +119,12 @@ function seg_intersect(p1,q1,p2,q2)
o3 = seg_helper_orientation(p2, q2, p1) o3 = seg_helper_orientation(p2, q2, p1)
o4 = seg_helper_orientation(p2, q2, q1) o4 = seg_helper_orientation(p2, q2, q1)
if (o1 o2) && (o3 o4) if (o1 o2) && (o3 o4) ||
o1==0 && onseg(p1, p2, q1) ||
o2==0 && onseg(p1, q2, q1) ||
o3==0 && onseg(p2, p1, q2) ||
o4==0 && onseg(p2, q1, q2)
return true return true
elseif o1==0 || o2==0 || o3==0 || o4==0
print("Colinearity! - TODO")
end end
return false return false
...@@ -134,7 +140,7 @@ function voxmap(vox, vox_d, prob::problem) ...@@ -134,7 +140,7 @@ function voxmap(vox, vox_d, prob::problem)
# for one vox, get all contours which pass through it # for one vox, get all contours which pass through it
# only need to search contours in its layer # only need to search contours in its layer
l = Int((vox[3] + prob.layer_height/2)/prob.layer_height) l = Int(round((vox[3] + prob.layer_height/2)/prob.layer_height))
voxx1 = vox[1] + vox_d/2 voxx1 = vox[1] + vox_d/2
voxx2 = vox[1] - vox_d/2 voxx2 = vox[1] - vox_d/2
voxy1 = vox[2] + vox_d/2 voxy1 = vox[2] + vox_d/2
...@@ -154,6 +160,11 @@ function voxmap(vox, vox_d, prob::problem) ...@@ -154,6 +160,11 @@ function voxmap(vox, vox_d, prob::problem)
# check if contour passes thorough this vox # check if contour passes thorough this vox
for i in 2:size(c.pos)[1] for i in 2:size(c.pos)[1]
# make sure it is a line segment, not a point
if c.pos[i-1,1:2] == c.pos[i,1:2]
continue
end
# is this line segment completely outside vox? # is this line segment completely outside vox?
if c.pos[i, 1] > voxx1 && c.pos[i-1, 1] > voxx1 || if c.pos[i, 1] > voxx1 && c.pos[i-1, 1] > voxx1 ||
c.pos[i,1] < voxx2 && c.pos[i-1, 1] < voxx2 || c.pos[i,1] < voxx2 && c.pos[i-1, 1] < voxx2 ||
...@@ -162,7 +173,7 @@ function voxmap(vox, vox_d, prob::problem) ...@@ -162,7 +173,7 @@ function voxmap(vox, vox_d, prob::problem)
# segment outside vox entirely # segment outside vox entirely
if seg_now if seg_now
print("Something's gone wrong: segment entirely outside voxel, but last segment inside") println("Something's gone wrong: segment entirely outside voxel, but last segment inside")
end end
continue continue
end end
...@@ -178,7 +189,7 @@ function voxmap(vox, vox_d, prob::problem) ...@@ -178,7 +189,7 @@ function voxmap(vox, vox_d, prob::problem)
seg_now = true seg_now = true
if i!=2 if i!=2
print("Whole segment inside but something wrong") println("Whole segment inside but something wrong")
end end
continue continue
end end
...@@ -189,9 +200,6 @@ function voxmap(vox, vox_d, prob::problem) ...@@ -189,9 +200,6 @@ function voxmap(vox, vox_d, prob::problem)
cross_side3 = seg_intersect(c.pos[i-1,:], c.pos[i,:], [voxx2, voxy1], [voxx2, voxy2]) cross_side3 = seg_intersect(c.pos[i-1,:], c.pos[i,:], [voxx2, voxy1], [voxx2, voxy2])
cross_side4 = seg_intersect(c.pos[i-1,:], c.pos[i,:], [voxx2, voxy2], [voxx1, voxy2]) cross_side4 = seg_intersect(c.pos[i-1,:], c.pos[i,:], [voxx2, voxy2], [voxx1, voxy2])
println(cid)
println([cross_side1, cross_side2, cross_side3, cross_side4])
# does this line segment intersect with vox only once # does this line segment intersect with vox only once
if p1inside p2inside if p1inside p2inside
...@@ -201,7 +209,7 @@ function voxmap(vox, vox_d, prob::problem) ...@@ -201,7 +209,7 @@ function voxmap(vox, vox_d, prob::problem)
xi = [voxx1, voxx2][[cross_side1, cross_side3]][1] xi = [voxx1, voxx2][[cross_side1, cross_side3]][1]
p_i = interpolate(c.pos[i-1,:], c.pos[i,:], xi, 1) p_i = interpolate(c.pos[i-1,:], c.pos[i,:], xi, 1)
t_i = interpolate(c.time[i-1], c.time[i], c.pos[i-1,1], xi, c.pos[i,1]) t_i = interpolate(c.time[i-1], c.time[i], c.pos[i-1,1], xi, c.pos[i,1])
elseif cross_side3 || cross_side4 elseif cross_side2 || cross_side4
# intersection with y # intersection with y
yi = [voxy1, voxy2][[cross_side2, cross_side4]][1] yi = [voxy1, voxy2][[cross_side2, cross_side4]][1]
p_i = interpolate(c.pos[i-1,:], c.pos[i,:], yi, 2) p_i = interpolate(c.pos[i-1,:], c.pos[i,:], yi, 2)
...@@ -211,7 +219,9 @@ function voxmap(vox, vox_d, prob::problem) ...@@ -211,7 +219,9 @@ function voxmap(vox, vox_d, prob::problem)
if p1inside if p1inside
# end existing segment # end existing segment
if !seg_now if !seg_now
print("Something's gone wrong") # contour end on the first segment
t_start = 0
seglen_sofar = 0
end end
seglen_sofar += dist(c.pos[i-1, :], p_i) seglen_sofar += dist(c.pos[i-1, :], p_i)
push!(segcontours, cid) push!(segcontours, cid)
...@@ -232,36 +242,57 @@ function voxmap(vox, vox_d, prob::problem) ...@@ -232,36 +242,57 @@ function voxmap(vox, vox_d, prob::problem)
p_is = [] p_is = []
t_is = [] t_is = []
if cross_side1 if cross_side1
push!(p_is, interpolate(c.pos[i-1,:], c.pos[i,:], voxx1, 1)) p = interpolate(c.pos[i-1,:], c.pos[i,:], voxx1, 1)
push!(t_is, interpolate(c.time[i-1], c.time[i], c.pos[i-1,1], voxx1, c.pos[i,1])) if !isnan(p[1])
push!(p_is, p)
push!(t_is, interpolate(c.time[i-1], c.time[i], c.pos[i-1,1], voxx1, c.pos[i,1]))
end
end end
if cross_side2 if cross_side2
push!(p_is, interpolate(c.pos[i-1,:], c.pos[i,:], voxy1, 2)) p = interpolate(c.pos[i-1,:], c.pos[i,:], voxy1, 2)
push!(t_is, interpolate(c.time[i-1], c.time[i], c.pos[i-1,2], voxy1, c.pos[i,2])) if !isnan(p[1])
push!(p_is,p)
push!(t_is, interpolate(c.time[i-1], c.time[i], c.pos[i-1,2], voxy1, c.pos[i,2]))
end
end end
if cross_side3 if cross_side3
push!(p_is, interpolate(c.pos[i-1,:], c.pos[i,:], voxx2, 1)) p = interpolate(c.pos[i-1,:], c.pos[i,:], voxx2, 1)
push!(t_is, interpolate(c.time[i-1], c.time[i], c.pos[i-1,1], voxx2, c.pos[i,1])) if !isnan(p[1])
push!(p_is,p)
push!(t_is, interpolate(c.time[i-1], c.time[i], c.pos[i-1,1], voxx2, c.pos[i,1]))
end
end end
if cross_side4 if cross_side4
push!(p_is, interpolate(c.pos[i-1,:], c.pos[i,:], voxy2, 2)) p = interpolate(c.pos[i-1,:], c.pos[i,:], voxy2, 2)
push!(t_is, interpolate(c.time[i-1], c.time[i], c.pos[i-1,2], voxy2, c.pos[i,2])) if !isnan(p[1])
push!(p_is, p)
push!(t_is, interpolate(c.time[i-1], c.time[i], c.pos[i-1,2], voxy2, c.pos[i,2]))
end
end end
if seg_now if seg_now
print("Something's wrong") print("Something's wrong")
end end
push!(segoffset, mean(t_is)) if length(p_is) >= 2
push!(segcontours, cid) push!(segoffset, mean(t_is))
n_intersections = sum([cross_side1, cross_side2, cross_side3, cross_side4]) push!(segcontours, cid)
if n_intersections ==2 if length(p_is) == 2
push!(seglen, dist(p_is[1], p_is[2])) push!(seglen, dist(p_is[1], p_is[2]))
else
push!(seglen, dist(p_is[1], p_is[3]))
end
else else
push!(seglen, dist(p_is[1], p_is[3])) p1inside = c.pos[i-1, 1] <= voxx1 && c.pos[i-1, 1] >= voxx2 &&
c.pos[i-1, 2] >= voxy2 && c.pos[i-1, 2] <= voxy1
p = p1inside ? c.pos[i-1,:] : c.pos[i,:]
t = p1inside ? c.time[i-1] : c.time[i]
push!(segcontours, cid)
push!(segoffset, (t + t_is[1])/2)
push!(seglen, dist(p_is[1], p))
end end
end end
end end
# if contour ends inside the voxel # if contour ends inside the voxel
...@@ -327,6 +358,7 @@ function valid_swap(rollout::Vector{Int}, i::Int, j::Int, prob::problem) ...@@ -327,6 +358,7 @@ function valid_swap(rollout::Vector{Int}, i::Int, j::Int, prob::problem)
# would swapping indices i and j in rollout result in another valid rollout? # would swapping indices i and j in rollout result in another valid rollout?
# NOTE THIS FUNCTION DOESNT WORK # NOTE THIS FUNCTION DOESNT WORK
# IT ONLY CHECKS DEPENDENCIES TO A DEPTH OF 1 # IT ONLY CHECKS DEPENDENCIES TO A DEPTH OF 1
# TODO, leave for now, use check_validity to double check at the end
if i>j if i>j
i,j = j,i i,j = j,i
...@@ -372,12 +404,6 @@ function check_validity(rollout::Vector{Int}, prob::problem) ...@@ -372,12 +404,6 @@ function check_validity(rollout::Vector{Int}, prob::problem)
end end
#voxels = DataFrames.DataFrame(CSV.File("tensile-1-1.csv"))
contours = contour.(JSON.parse(open("tensilecontours.json")))
dt = problem(contours, 5, 5)
@benchmark random_rollout(dt.G)
function test_voxmap() function test_voxmap()
# create vox # create vox
vox = [0,0,0.5] vox = [0,0,0.5]
...@@ -402,4 +428,52 @@ function test_voxmap() ...@@ -402,4 +428,52 @@ function test_voxmap()
prob = problem(contours, 1, 1) prob = problem(contours, 1, 1)
vm = voxmap(vox, vox_d, prob) vm = voxmap(vox, vox_d, prob)
return vm return vm
end end
\ No newline at end of file
function rollout2time(rollout::Vector{Int}, prob)
# start time of each contour, assuming no travel time
return cumsum([prob.contours[c].time[end] for c in rollout])
end
function calc_cost(rollout::Vector{Int}, prob::problem, voxms::Vector{voxmap})
# go from rollout to timestart for each contour
timestart = rollout2time(rollout, prob)
# calculate time at each voxel
# calculate temp difference from voxel below it
# calculate stresses at each voxel
# calculate cost func at each voxel
# sum over the voxels
end
function construct_cost(prob::problem)
a = 1
b = :($a + 5) # using $ 'interpolates' literal expression into the 'quoted' expression
c = :(5*5 + a)
d = :($b + $c) #interpolate other expressions to form larger expressions
ex1 = :(i*2) # construct this according to voxels and contours
ex2 = quote
function cost_f(i::Int)
return $ex1
end
end
return eval(ex2) # return the contructed function
end
voxels = DataFrames.DataFrame(CSV.File("tensile-1-1.csv"))
contours = contour.(JSON.parse(open("tensilecontours.json")))
dt = problem(contours, 5, 5)
@benchmark random_rollout(dt)
rl = random_rollout(dt)
voxms = [voxmap([v.x, v.y, v.z], 1, dt) for v in eachrow(voxels)]
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment