Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Apr 19, 2024
1 parent ae4878d commit a73d217
Showing 1 changed file with 98 additions and 5 deletions.
103 changes: 98 additions & 5 deletions apps/find_plane.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
local apps = require 'apps.apps'
local matrix = require 'matrix'
local advice = require 'advice'
local ask = require('dialog.input').display_sync
local choice = require('dialog.choice').display_sync
local choice = require('dialog.choice').display_sync
local matrix_ed = require('rpn.matrixeditor').display_sync
local expr = require 'expressiontree'
local sym = require 'ti.sym'

Expand Down Expand Up @@ -31,6 +35,7 @@ local function ask_vector(title, dim)
end
end

-- Compute plane equation from 3 points
local function run_plane_3pt(stack)
local pts = {}
for i = 1, 3 do
Expand All @@ -52,15 +57,25 @@ local function run_plane_3pt(stack)
local res_p = "x = " .. table_to_vec(a) .. " + s*" .. ab .. " + t*" .. ac
local res_n = "(x - " .. table_to_vec(a) .. ") " .. sym.CDOT .. " " .. n

local store = choice { title = 'Results', items = {
{ title = 'E: ' .. res_c, align = -1, result = 'c', seq = {'c'} },
local action = choice { title = 'Results', items = {
{ title = 'E: ' .. res_c, align = -1, result = 'c' },
{ title = 'E: ' .. res_p, align = -1, result = 'p' },
{ title = 'E: ' .. res_n, align = -1, result = 'n' },
{ title = 'Exit', result = false},
{ title = 'Store...', result = 'store' },
{ title = 'Exit', result = 'done' },
}}

if store == 'c' then
if action == 'store' then
local sym = ask { title = 'Variable', text = 'E1' }
if sym and #sym > 0 then
stack:push_infix(res_c)
stack:push_infix(sym)
stack:push_operator('=:')
end
elseif action == 'c' then
stack:push_infix(res_c)
elseif action == 'done' then
return
end
end

Expand All @@ -71,5 +86,83 @@ local function run_angle_2vec(stack)
stack:push_infix("arccos(dotp(" .. a .. "," .. b .. ")/(norm(" .. a .. "*norm(" .. b .. "))")
end

local function run_dist_2pt(stack)
local a = table_to_vec(ask_vector("Point A"))
local b = table_to_vec(ask_vector("Point B"))

stack:push_infix("norm(" .. b .. "-" .. a .. ")")
end

local function run_2lines(ctrl, stack)
local mat = matrix.new(4, 5)
mat:set(0,0," ")
mat:set(1,2,"p1")
mat:set(1,3,"d1")
mat:set(1,4,"p2")
mat:set(1,5,"d2")
mat:set(2,1,"x")
mat:set(3,1,"y")
mat:set(4,1,"z")

local function get_line(mat, m, n)
local o = {
mat:get(m + 0, n),
mat:get(m + 1, n),
mat:get(m + 2, n),
}
local d = {
mat:get(m + 0, n + 1),
mat:get(m + 1, n + 1),
mat:get(m + 2, n + 1),
}
return o, d
end

local function vec_str(l)
return string.format("[[%s,%s,%s]]", table.unpack(l))
end

local function setup_dlg(dlg)
local grid = dlg.grid
grid:set_header(1,1)
grid:set_selection(2,2)
end

local res = matrix_ed(ctrl, mat, setup_dlg)
if res then
local p1, d1 = get_line(2, 2)
local p2, d2 = get_line(2, 4)
local vp1, vd1 = vec_str(p1), vec_str(d1)
local vp2, vd2 = vec_str(p2), vec_str(d2)

local eq1 = vp1 .. "+a*".. vd1
local eq2 = vp2 .. "+b*".. vd2

local isection = "solve(" .. eq1 .. "=" .. eq2 .. ",a,b)"
local parallel = "solve(" .. vd1 .. "=t*" .. vd2 .. ",t)"
local n = math.evalStr("crossp(" .. vd1 .. "," .. vd2 .. ")")
local n1 = math.evalStr("crossp(" .. vd1 .. "," .. n .. ")")
local n2 = math.evalStr("crossp(" .. vd2 .. "," .. n .. ")")
local nearest1 = string.format("%s+(dotp(%s-%s,%s)/dotp(%s,%s))*%s",
vp1, vp2, vp1, n2, vd1, n2, vd1)
local nearest2 = string.format("%s+(dotp(%s-%s,%s)/dotp(%s,%s))*%s",
vp2, vp1, vp2, n1, vd2, n1, vd2)
local distance = "norm(" .. nearest1 .. "-" .. nearest2 ..")"

local r_isection = math.evalStr(isection)
local r_parallel = math.evalStr(parallel)
print(r_isection)
print(r_parallel)

if r_isection ~= 'false' then
return
else
return
end
end
end

apps.add('plane - 3pt', 'Plane - 3 Points', run_plane_3pt)
apps.add('angle - 2vec', 'Angle - 2 Vectors', run_angle_2vec)
apps.add('angle - 2vec', 'Angle - 2 Vectors', run_angle_2vec)
apps.add('dist - 2pt', 'Dist. - 2 Points', run_dist_2pt)
apps.add('2line', '2line', run_2lines)

0 comments on commit a73d217

Please sign in to comment.