66 lines
2.8 KiB
Python
66 lines
2.8 KiB
Python
"""Independent-frame trajectory metrics; no alignment or ground-truth feedback."""
|
|
import bisect
|
|
import math
|
|
|
|
|
|
def conjugate(q):
|
|
return [-q[0],-q[1],-q[2],q[3]]
|
|
|
|
|
|
def multiply(a,b):
|
|
x,y,z,w=a;X,Y,Z,W=b
|
|
return [w*X+x*W+y*Z-z*Y,w*Y-x*Z+y*W+z*X,w*Z+x*Y-y*X+z*W,w*W-x*X-y*Y-z*Z]
|
|
|
|
|
|
def rotate(q,p):
|
|
return multiply(multiply(q,[*p,0]),conjugate(q))[:3]
|
|
|
|
|
|
def angle(q):
|
|
return 2*math.acos(min(1,abs(q[3])/math.sqrt(sum(x*x for x in q))))
|
|
|
|
|
|
def interpolate(rows,t,gap=0.05):
|
|
i=bisect.bisect_left(rows,[t])
|
|
if i<len(rows) and abs(rows[i][0]-t)<1e-8:return rows[i][1:]
|
|
if i==0 or i==len(rows) or rows[i][0]-rows[i-1][0]>gap:return None
|
|
a,b=rows[i-1],rows[i];u=(t-a[0])/(b[0]-a[0])
|
|
p=[(1-u)*a[j]+u*b[j] for j in range(1,4)]
|
|
sign=1 if sum(a[j]*b[j] for j in range(4,8))>=0 else -1
|
|
q=[(1-u)*a[j]+u*sign*b[j] for j in range(4,8)];norm=math.sqrt(sum(x*x for x in q))
|
|
return p+[x/norm for x in q]
|
|
|
|
|
|
def metrics(estimate,truth):
|
|
# A single pose per stamp; ordering changes never create false zero-duration RPE.
|
|
estimate=sorted({row[0]:row for row in estimate}.values())
|
|
truth=sorted({row[0]:row for row in truth}.values())
|
|
translation=[];rotation=[];relative_t=[];relative_r=[]
|
|
for row in estimate:
|
|
t=row[0];e=row[1:];g=interpolate(truth,t)
|
|
if g is None:continue
|
|
translation.append(sum((e[j]-g[j])**2 for j in range(3)))
|
|
rotation.append(angle(multiply(conjugate(g[3:]),e[3:]))**2)
|
|
en=interpolate(estimate,t+1);gn=interpolate(truth,t+1)
|
|
if en is None or gn is None:continue
|
|
ep=rotate(conjugate(e[3:]),[en[j]-e[j] for j in range(3)])
|
|
gp=rotate(conjugate(g[3:]),[gn[j]-g[j] for j in range(3)])
|
|
relative_t.append(sum((ep[j]-gp[j])**2 for j in range(3)))
|
|
eq=multiply(conjugate(e[3:]),en[3:]);gq=multiply(conjugate(g[3:]),gn[3:])
|
|
relative_r.append(angle(multiply(conjugate(gq),eq))**2)
|
|
def rmse(values):return math.sqrt(sum(values)/len(values)) if values else None
|
|
return {'matched':len(translation),'total':len(estimate),'rpe_pairs':len(relative_t),
|
|
'ate_translation_m':rmse(translation),'ate_rotation_deg':None if not rotation else math.degrees(rmse(rotation)),
|
|
'rpe_1s_translation_m':rmse(relative_t),'rpe_1s_rotation_deg':None if not relative_r else math.degrees(rmse(relative_r)),
|
|
'maximum_position_error_m':math.sqrt(max(translation)) if translation else None}
|
|
|
|
|
|
def cylinder_clearance(row,x,y,radius):
|
|
q=row[4:];yaw=math.atan2(2*(q[3]*q[2]+q[0]*q[1]),1-2*(q[1]*q[1]+q[2]*q[2]))
|
|
dx,dy=x-row[1],y-row[2]
|
|
bx=math.cos(yaw)*dx+math.sin(yaw)*dy;by=-math.sin(yaw)*dx+math.cos(yaw)*dy
|
|
# Union of the actual SDF chassis and gimbal collision boxes.
|
|
chassis=math.hypot(max(0,abs(bx)-0.289),max(0,abs(by)-0.2605))
|
|
gimbal=math.hypot(max(0,abs(bx-0.12)-0.20),max(0,abs(by)-0.15))
|
|
return min(chassis,gimbal)-radius
|