/// <summary> /// Calculates value and derivative of the position of the arm with respect to whichDeriv /// </summary> DualNumber ArmFKError(Vector3 clampedPos, int whichDeriv) { DualNumber thetaVar = whichDeriv == 0 ? DualNumber.Variable(theta) : DualNumber.Constant(theta); DualNumber phiVar = whichDeriv == 1 ? DualNumber.Variable(phi) : DualNumber.Constant(phi); baseJoint = new DualVector3(Math.Sin(thetaVar), Math.Cos(thetaVar), new DualNumber()); endJoint = baseJoint + new DualVector3(Math.Sin(thetaVar + phiVar), Math.Cos(thetaVar + phiVar), new DualNumber()); return(((DualVector3)clampedPos - endJoint).SqrMagnitude()); }
/// <summary> /// Calculates value and derivative of the position of the arm with respect to whichDeriv /// </summary> static DualNumber[] ArmFK(float theta, float phi, int whichDeriv) { DualNumber thetaVar = whichDeriv == 0 ? DualNumber.Variable(theta) : DualNumber.Constant(theta); DualNumber phiVar = whichDeriv == 1 ? DualNumber.Variable(phi) : DualNumber.Constant(phi); DualNumber[] outputValues = { Math.Sin(thetaVar), //X Position of First Joint Math.Cos(thetaVar), //Y Position of First Joint Math.Sin(thetaVar) + Math.Sin(thetaVar + phiVar), //X Position of Second Joint Math.Cos(thetaVar) + Math.Cos(thetaVar + phiVar), //Y Position of Second Joint }; return(outputValues); }
/// <summary> /// Calculates value and derivative of the position of the sphere with respect to whichDeriv /// </summary> DualNumber SphereFittingError(int whichDeriv, bool drawDebug) { DualVector3 thisPosition = new DualVector3( whichDeriv == 0 ? DualNumber.Variable(transform.position.x) : DualNumber.Constant(transform.position.x), whichDeriv == 1 ? DualNumber.Variable(transform.position.y) : DualNumber.Constant(transform.position.y), whichDeriv == 2 ? DualNumber.Variable(transform.position.z) : DualNumber.Constant(transform.position.z)); DualVector3 thisYBasis = new DualVector3( whichDeriv == 3 ? DualNumber.Variable(transform.up.x) : DualNumber.Constant(transform.up.x), whichDeriv == 4 ? DualNumber.Variable(transform.up.y) : DualNumber.Constant(transform.up.y), whichDeriv == 5 ? DualNumber.Variable(transform.up.z) : DualNumber.Constant(transform.up.z)); DualVector3 thisZBasis = new DualVector3( whichDeriv == 6 ? DualNumber.Variable(transform.forward.x) : DualNumber.Constant(transform.forward.x), whichDeriv == 7 ? DualNumber.Variable(transform.forward.y) : DualNumber.Constant(transform.forward.y), whichDeriv == 8 ? DualNumber.Variable(transform.forward.z) : DualNumber.Constant(transform.forward.z)); DualVector3 thisScale = new DualVector3( whichDeriv == 9 ? DualNumber.Variable(transform.localScale.x) : DualNumber.Constant(transform.localScale.x), whichDeriv == 10 ? DualNumber.Variable(transform.localScale.y) : DualNumber.Constant(transform.localScale.y), whichDeriv == 11 ? DualNumber.Variable(transform.localScale.z) : DualNumber.Constant(transform.localScale.z)); thisYBasis = thisYBasis.Normalize(); thisZBasis = thisZBasis.Normalize(); DualVector3 thisXBasis = thisYBasis.Cross(thisZBasis); thisYBasis = thisZBasis.Cross(thisXBasis); //THIS IS A FULLY DIFFERENTIABLE RIGID TRANSFORM - NO SINGULARITIES DualMatrix4x3 thisTransform = new DualMatrix4x3(thisXBasis * thisScale.x, thisYBasis * thisScale.y, thisZBasis * thisScale.z, thisPosition); //Sum the Squared Errors DualNumber error = new DualNumber(); for (int i = 0; i < icoVerts.Length; i++) { DualVector3 thisPoint = thisTransform * icoVerts[i]; DualVector3 basePoint = baseTransformMatrix * icoVerts[i]; if (drawDebug) { Debug.DrawLine(thisPoint, basePoint); } error += (thisPoint - basePoint).SqrMagnitude(); } //Divide by Number of Squared Errors error /= icoVerts.Length; return(error); }
/// <summary> /// Calculates value and derivative of the position of the sphere with respect to whichDeriv /// </summary> DualNumber SphereFittingError(int whichDeriv, bool drawDebug) { DualVector3 spherePosition = new DualVector3( whichDeriv == 0 ? DualNumber.Variable(transform.position.x) : DualNumber.Constant(transform.position.x), whichDeriv == 1 ? DualNumber.Variable(transform.position.y) : DualNumber.Constant(transform.position.y), whichDeriv == 2 ? DualNumber.Variable(transform.position.z) : DualNumber.Constant(transform.position.z)); DualNumber radius = whichDeriv == 3 ? DualNumber.Variable(transform.localScale.x) : DualNumber.Constant(transform.localScale.x); //Sum the Squared Errors DualNumber error = new DualNumber(); for (int i = 0; i < icoVerts.Length; i++) { error += DistFromSurface(baseSphere.position, baseSphere.localScale.x * 0.5f, spherePosition + (radius * icoVerts[i]), drawDebug).Squared(); } //Divide by Number of Squared Errors error /= icoVerts.Length; return(error); }