-- various functions for track construction and more. (incomplete and not very explicit)

function yo_trackutil.tdump(o)
-- returns a table as a string. usefull for debugging
-- "o" = a table

function yo_trackutil.tcontains(tbl,str)
-- returns true if table "tbl" contains value "str"

function yo_trackutil.tcontainskey(tbl,key)
-- returns true if table "tbl" contains the key "key"

function yo_trackutil.calcScale(dist, angle)
-- from laneutil. computes scale factor of tangents
-- "dist" = distance between the 2 points
-- "angle" = angle of the circular arc that joins the 2 points

function yo_trackutil.torad(degrees)
-- convert degrees to radians

function yo_trackutil.todegrees(radians)
-- convert radians to degrees

function yo_trackutil.totangent(angle_degrees)
-- returns tangent vector vec3 from angle (degrees)

function yo_trackutil.toangle(tg)
-- returns angle in degrees from tangent vector as a value between -180 and 180

function yo_trackutil.toanglerad(tg)
-- returns angle in radians from tangent vector as a value between -PI to PI

-- point = { p=vec3, t=vec3 } -- p=point position, t=direction vector from this point

function yo_trackutil.point.new(point)
-- new point. optional point parameter copies value of that point into a new one

function yo_trackutil.point.newv(px,py,pz, tx,ty,tz)
-- new point with value parameters
-- if only tx is present and no ty and tz, it is assumed that tx is an angle in degrees

function yo_trackutil.point.mirror(point,point_origin)
-- mirrors <point> relative to <point_origin> keeping direction (modifies point; tangent is kept the same)

function yo_trackutil.point.flipx(point,point_origin)
-- flips <point> along x axis relative to <point_origin>. (modifies point)

function yo_trackutil.point.flipy(point,point_origin)
-- flips <point> along y axis relative to <point_origin>. (modifies point)

function yo_trackutil.point.flipxy(point,point_origin)
-- flips <point> along x and y axis relative to <point_origin>. (modifies point)

function yo_trackutil.point.distance(p0,p1)
-- distance between 2 points

function yo_trackutil.point.intersect(p0,p1)
-- returns a pair of coords {x,y} that is the intersection point of the lines defined by points p0 and p1 and their directions
-- returns {0,0} if there is no intersection

function yo_trackutil.calcCurved(point_p0, tangent_t1, cordlength)
-- computes end point of an arc starting from p0, with direction t0
-- and goes forward until tangent=t1 and cord length=cordlength (straight distance from p0 to p1)

function yo_trackutil.calcSide(point_p0, sidedelta,cordlength)
-- computes end point of an arc starting from p0, with direction t0
-- and goes forward until cord length=cordlength and side distance from direction line t0=sidedelta

function yo_trackutil.radius_from_speed_limit(speed,coeff)
-- computes radius needed for a curve to have minimum speed <speed> and track parameters are <coeff>

function yo_trackutil.cordlength_from_speed_limit_and_side(speed,coeff,side)
-- used for calcSide when cordlength is computed from wanted speed and sideway displacement

function yo_trackutil.cordlength_from_speed_limit_and_angle(speed,coeff,deltaangle)
-- used for calcCurved when cordlength is computed from wanted speed and exit angle

function yo_trackutil.calcSideS(point_p0, sidedelta, speed, coeff)
-- calcSide with speed limit instead of cordlength

function yo_trackutil.calcCurvedS(point_p0, tangent_t1, speed, coeff)
-- calcCurved with speed limit instead of cordlength

function yo_trackutil.addSegment(point_p0,point_p1,snap_p0,snap_p1, tracks,snaps)
-- adds a segment of track from p0 to p1 to tracks list
-- tracks list is in the format required by result.edgeLists.edges
-- snap_p0 and snap_p1=boolean. if true add respective point to snaps
-- NOTES:
-- tangents must point one after another at a maximum angle delta of 90 degrees (90 degrees makes nice tracks, more will make something but usually impossible tracks)
-- p0 t0=---> and p1 t1=---> OK
-- p0 t0=<--- and p1 t1=---> WRONG or can be OK if it is needed. diverging. the game will try to make a BIIIIG curve between the points not the shortest one and it will fail almost in all cases
-- p0 t0=---> and p1 t1=<--- WRONG. converging. the game will make a track starting in the "good" direction but will try to connect to point p1 from the other side resulting a curve like a question mark "?"
