A theory of degrees of freedom for mechanisms