通过关键字define 定义函数,通过关键字 载入文件,而后可以直接调用。当然所定义的函数和调用的代码可以写在一起。
外部函数文件,需要保存为eulerXYZ.tpl
{' This templex macro calculates the Euler params based on three sequential rotations.}
{' Rotation sequence Rx -> Ry -> Rz.}
{define eulerXYZ(rx, ry, rz)}
{ t1 = dtor(rx)}
{ t2 = dtor(ry)}
{ t3 = dtor(rz)}
{' Cosine matrix:}
{ a11=cos(t2)*cos(t3)}
{ a12=-cos(t2)*sin(t3)}
{ a13=sin(t2)}
{ a21=sin(t1)*sin(t2)*cos(t3)+sin(t3)*cos(t1)}
{ a22=cos(t1)*cos(t3)-sin(t1)*sin(t2)*sin(t3)}
{ a23=-sin(t1)*cos(t2)}
{ a31=sin(t1)*sin(t3)-cos(t1)*sin(t2)*cos(t3)}
{ a32=sin(t1)*cos(t3)+cos(t1)*sin(t2)*sin(t3)}
{ a33=cos(t1)*cos(t2)}
{ trace = a11+a22+a33 }
{' Euler parameters:}
{ e0 = 0.5*sqrt(trace+1.0) }
{ e1 = 0.5*sqrt(1.0 + 2.0*a11 - trace) }
{ e2 = 0.5*sqrt(1.0 + 2.0*a22 - trace) }
{ e3 = 0.5*sqrt(1.0 + 2.0*a33 - trace) }
{ if e0 != 0.0 }
{ e0 = sqrt(e0*e0) }
{ e1 = (a32-a23)/(4.0*e0) }
{ e2 = (a13-a31)/(4.0*e0) }
{ e3 = (a21-a12)/(4.0*e0) }
{ elseif e1 != 0.0 }
{ e1 = sqrt(e1*e1) }
{ e2 = (a13+a31)/(4.0*e1) }
{ e3 = (a21+a12)/(4.0*e1) }
{ elseif e2 != 0.0 }
{ e2 = sqrt(e2*e2) }
{ e3 = (a32+a23)/(4.0*e2) }
{ else }
{ e3 = 1.0 }
{ endif }
{enddefine}
调用外部所定义的函数
"eulerXYZ.tpl"}
SYSTEM 1 JOINT DOF
90, 0, 0)}
1 FREE {e0} {e1} {e2} {e3} 0.5 0.5 0.5 0.0 0.0 0.0 50.0 0.0 0.0
END JOINT DOF
END SYS
SYSTEM 2 JOINT DOF
0, 90, 0)}
1 FREE {e0} {e1} {e2} {e3} 0.5 0.5 0.5 0.0 0.0 0.0 50.0 0.0 0.0
END JOINT DOF
END SYS
最终显示
SYSTEM 1 JOINT DOF
1 FREE 0.707107 0.707107 0 0 0.5 0.5 0.5 0.0 0.0 0.0 50.0 0.0 0.0
END JOINT DOF
END SYS
SYSTEM 2 JOINT DOF
1 FREE 0.707107 0 0.707107 0 0.5 0.5 0.5 0.0 0.0 0.0 50.0 0.0 0.0
END JOINT DOF
END SYS
函数与调用代码写在一起
{define greeting(first, last)}
Dear {first} {last},
{enddefine}
{greeting("John", "Doe")}
我们来写一个类似于python的range函数
{define range(start, end, step)}
{for (i = start; i < end; i += step)}
{i}
{endloop}
{enddefine}
{range(0, 20, 2)}