Least-Squares Fitting Algorithms of The NIST Algorithm Testing System
Least-Squares Fitting Algorithms of The NIST Algorithm Testing System
Volume 103
Craig M. Shakarji
National Institute of Standards and
Technology,
Gaithersburg, MD 20899-0001
1.
Number 6
NovemberDecember 1998
numerical analysis, and optimization methods it employs. Most of the fitting routines rely on the Levenberg-Marquardt optimization routine.
Key words: coordinate measuring machine; curve fitting; least-squares fitting;
Levenberg-Marquardt; orthogonal distance regression; surface fitting.
Accepted: August 7, 1998
Available online: http://www.nist.gov/jres
Introduction
Mathematical software, particularly curve and surface fitting routines, is a critical component of coordinate metrology systems. An important but difficult task
is to assess the performance of such routines [1,2]. The
National Institute of Standards and Technology has developed a software package, the NIST Algorithm Testing System (ATS), that can assist in assessing the performance of geometry-fitting routines [3]. This system has
been aiding in the development of a U. S. standard
(American Society of Mechanical Engineers (ASME)
B89.4.10) for software performance evaluation of coordinate measuring systems [4]. The ATS is also the basis
of NISTs Algorithm Testing and Evaluation Program
for Coordinate Measuring Systems (ATEP-CMS) [5,6].
The ATS incorporates three core modules: a data
generator [7] for defining and generating test data sets,
a collection of reference algorithms which provides a
performance baseline for fitting algorithms, and a comparator for analyzing the results of fitting algorithms
versus the reference algorithms. This paper concentrates
on the development of highly accurate reference algorithms.
The paper is organized as follows. We first introduce
notation and certain key functions and derivatives that
will be used throughout the paper. Section 2 describes
fitting algorithms for linear geometriesplanes and
lines. Lagrange multipliers [8] are used in solving the
constrained minimization problems, which are developed into standard eigenvector problems. Section 3 deals
with nonlinear geometry fitting. We use an unconstrained optimization method that requires derivatives of
appropriate distance functions. These required functions
and derivatives are provided for the reader. Appendix A
gives an outline of the unconstrained optimization
633
2.1
Defining parameters:
x a point on the plane.
a the direction cosines of the normal to the plane.
Distance equation:
di = d (xi ) = d (xi , x , a ) = a ? (xi 2 x )
|?|
xi = (xi , yi , zi)
x = (x , y , z)
Objective function:
J (x , a ) = [a ? (xi 2 x )]2
Description:
The centroid of the data must lie on the least-squares
plane. This can be seen because =J = 0 at the least
squares solution, yielding
a ? (xi 2 x ) = 0. Multia
b
plying by 1/N gives
(xi 2 x ) +
(yi 2 y )
N
N
c
(zi 2 z ) = 0. Distributing the summation gives
+
N
a (x 2 x ) + b (y 2 y ) + c (z 2 z ) = 0, which is to say
d (x , x , A ) = 0, i.e., x lies on the least-squares plane.
Since by assumption the data points have been translated to the origin, and since the centroid of the data
must be a point on the least squares plane, we can set
x = 0.
The direction of the fitted plane, a , can be found by
solving the constrained minimization problem, namely,
minimizing J subject to the constraint that | a | = 1. Define a function, G = | a |2 2 1, so that the problem is to
minimize J subject to the constraint that G = 0. The
method of Lagrange multipliers [8] tells us that the
minimum occurs at a point where =J = l =G , for some
real number l . (Here, a , b , and c are treated as independent variables, since the constraint is accounted for in
G . Therefore, = = (/a , /b , /c ).) But =G = 2a ,
and =J = 2(M TM )a , yielding the eigen-problem,
(M TM )a = la , referred to as the normal equations .
This 3 3 3 eigenvector problem can be easily solved
using well-established routines (e.g., Jacobi iterations
[9]). However, we note that the eigenvectors of M TM
are also the singular vectors (from the singular value
decomposition) of M [9]. This allows us to gain numerical stability by applying the singular value decomposition (SVD) to M without ever computing M TM , which
is the method implemented in the ATS.
Finally, we must determine how to select the correct
eigenvector (i.e., singular vector) of the three produced
by the SVD. The normal equations can be written as
follows:
SO O O D
A = (A , B , C )
a = (a , b , c )
The N 3 3
x 1 y1
x2 y2
...
...
xN yN
3
=
2.
O
O
1
xi , yi , zi .
N
(Note: These and all other sums in this paper are
taken from i = 1, 2,..., N .)
The centroid of the data,
Plane Fitting
Linear Geometries
O
O
O
634
xi (a ? xi ) = la
yi (a ? xi ) = lb
zi (a ? xi ) = lc
2.2
Line Fitting
Defining parameters:
x a point on the line.
a the direction cosines of the line.
u = c (yi 2 y ) 2 b (zi 2 z )
fi = u 2 + v 2 + w 2, where v = a (zi 2 z ) 2 c (xi 2 x )
w = b (xi 2 x ) 2 a (yi 2 y )
Distance equation:
d (xi ) = d (xi , x , a ) = | a 3 (xi 2 x )| (which also equals
| xi 2 x |2 2 [a ? (xi 2 x )]2) where 3 denotes the
vector cross product.
Objective function:
J (x , a ) = | a 3 (xi 2 x )|2
Description:
Since the data has been translated to the origin, and
since the centroid of the data must be a point on the least
squares plane (similar to the case above with the least
squares plane), we set x = 0.
The direction a can be found by following the same
strategy as in the case of a plane. For line fitting, the
normal equations are M TMa = la just as in the case of
plane fitting. Once again, the correct eigenvector must
be chosen to minimize the sum-of-squares of the residuals. As shown with planes, we obtain l = (a ? x )2, so
J = 2 l + | xi |2, meaning that J is minimized when l
is maximized. Thus the correct eigenvector choice is the
one corresponding to the largest eigenvalue. As in the
case of plane fitting, numerical stability is gained by
finding the eigenvectors of M through the SVD, rather
than by solving the normal equations. Since the singular
values are the square roots of the eigenvalues [10], we
choose the eigenvector corresponding to the largest singular value.
3.
Nonlinear Geometries
3.1
34 3
a a a
A B C
12a 2 2ab
2ac
b b b
1
2
=b =
=
2ab
12b 2bc
A B C
|A|
=c
c c c
2ac
2bc
12c 2
A B C
=a
In the ATS, nonlinear geometries are fit in an unconstrained manner using the Levenberg-Marquardt algorithm. The algorithm requires an initial guess as well
as the first derivatives of the distance function. In practice it converges quickly and accurately even with a wide
range of initial guesses. Details of this algorithm are
given in appendix A. Additionally, the code allows us to
normalize the fitting parameters after every iteration of
the algorithm. For each geometry we list the distance
and objective functions, the appropriate derivatives, and
the parameter normalization we use.
gi
=2a
x
gi
=2b
y
gi
=2c
z
gi
= (xi 2 x ) 2 agi
A
gi
= (yi 2 y ) 2 bgi
B
gi
= (zi 2 z ) 2 cgi
C
fi
= [agi 2 (xi 2 x )]/fi
x
fi
= [bgi 2 (yi 2 y )]/fi
y
fi
= [cgi 2 (zi 2 z )]/fi
z
fi
= gi [agi 2 (xi 2 x )]/fi
A
fi
= gi [bgi 2 (yi 2 y )]/fi
B
fi
= gi [cgi 2 (zi 2 z )]/fi
C
3.3
Defining parameters:
x the center of the sphere.
r the radius of the sphere.
Distance equation:
d (xi ) = | xi 2 x | 2 r
Objective function:
J (x , r ) = (| xi 2 x | 2 r )2
Normalization:
(None)
S1 2 a , 1 2 b , 1 2 c ,
2
Derivatives:
di
= 2 (xi 2 x )/| xi 2 x |
x
di
= 2 (yi 2 y )/| xi 2 x |
y
di
= 2 (zi 2 z )/| xi 2 x |
z
di
=21
r
g 1 2 a 2, g 1 2 b 2, g 1 2 c 2 .
For cylinders, cones, and tori, the line associated with
f is the geometrys axis. For cones the plane associated
with g is the plane through the point, x , perpendicular to
the axis. For tori, the plane associated with g is the plane
perpendicular to the axis that divides the torus in half.
3.2
Sphere Fitting
3.4
Choice of Algorithm
Distance equation:
d (xi , yi ) = (xi 2 x )2 + (yi 2 y )2 2 r
Objective function:
J (x , y , r ) =
636
OS(x 2 x ) + (y 2 y ) 2 rD
i
Derivatives:
di
= 2 (xi 2 x )/(di + r )
x
di
= 2 (yi 2 y )/(di + r )
y
di
=21
r
3.5
Some coordinate measuring system software packages stop at step (5) and report the orientation, center,
and radius as the least-squares circle in 3D. This approach is valid when the projection onto the plane is
done simply to compensate for measurement errors on
points which would otherwise be coplanar. But this
method does not in general produce the circle yielding
the least sum-of-squares possible (even though it is usually a good approximation.) In order to achieve the true
3D least-squares fit, the circle computed at step (5) is
used as an initial guess in the Levenberg-Marquardt
algorithm, which optimizes over all the parameters
simultaneously [step (6)].
Step (2) is carried out using the appropriate rotation
matrix to rotate the direction, a , to the z -direction,
namely,
Defining parameters:
x the center of the circle.
A the direction numbers of the normal to circles
plane.
r the radius of the circle.
Distance equation:
d (xi ) = gi2 + (fi 2 r )2
Objective function:
J (x , A , r ) =
OSg + (f 2 r ) D
1
i
12
Normalization:
A A /| A | (Here and elsewhere, denotes assignment of value. In this case, the
value of A is replaced by the value
A /| A |.)
Derivatives:
di
= [gi (gi )x + fi (fi )x ]/di
x
di
= [gi (gi )y + fi (fi )y ]/di
y
di
= [gi (gi )z + fi (fi )z ]/di
z
di
= [gi (gi )A + fi (fi )A ]/di
A
di
= [gi (gi )B + fi (fi )B ]/di
B
di
= [gi (gi )C + fi (fi )C ]/di
C
di
= 2 (fi 2 r )/di
r
a2
1+c
2 ab
1+c
2 ab
1+c
12
b2
1+c
2a
2b
12
3.6
Description:
We use a multi-step process to accomplish 3D circle
fitting:
1. Compute the least-squares plane of the data.
a2
1+c
2 ab
1+c
2a
2 ab
1+c
12
b2
1+c
2b
b
c
Cylinder Fitting
Defining parameters:
x a point on the cylinder axis.
A the direction numbers of the cylinder axis.
r the radius of the cylinder.
637
Derivatives:
di
= (fi )x cosc + (gi )x sinc
x
di
= (fi )y cosc + (gi )y sinc
y
di
= (fi )z cosc + (gi )z sinc
z
di
= (fi )A cosc + (gi )A sinc
A
di
= (fi )x cosc + (gi )x sinc
B
di
= (fi )x cosc + (gi )x sinc
C
di
=21
s
di
= 2 fi sinc + gi cosc
c
Objective function:
J (x , A , r ) = (fi 2 r )2
Normalization:
A A /| A |
x (point on axis closest to origin)
Derivatives:
di
= (fi )x
x
di
= (fi )y
y
di
= (fi )z
z
di
= (fi )A
A
di
= (fi )B
B
di
= (fi )C
C
di
=21
r
3.7
3.8
Torus Fitting
Defining parameters:
x the torus center.
A the direction numbers of the torus axis.
r the major radius.
R the minor radius.
Distance equation:
d (xi ) = gi2 + (fi 2 r )2 2 R
Cone Fitting
Defining parameters:
x a point on the cone axis (not the apex).
A the direction numbers of the cone axis (pointing
toward the apex).
s the orthogonal distance from the point, x , to the
cone.
c the cones apex semi-angle.
Objective function:
J (x , A , r , R ) =
OFg + (f 2 r ) 2 RG
2
i
Normalization:
A A /| A |
Distance equation:
d (xi ) = fi cosc + gi sinc 2 s
Derivatives:
di
= [gi (gi )x + (fi 2 r )(fi )x ]/(di + R )
x
di
= [gi (gi )y + (fi 2 r )(fi )y ]/(di + R )
y
di
= [gi (gi )z + (fi 2 r )(fi )z ]/(di + R )
z
di
= [gi (gi )A + (fi 2 r )(fi )A ]/(di + R )
A
di
= [gi (gi )B + (fi 2 r )(fi )B ]/(di + R )
B
di
= [gi (gi )C + (fi 2 r )(fi )C ]/(di + R )
C
di
= 2 (fi 2 r )(di + R )
r
di
=21
R
Objective function:
J (x , A , s , c ) = (fi cosc + gi sinc 2 s )2
Normalization:
A A /| A |
x (point on axis closest to origin)
c c (mod 2p)
if c > p then [c c (mod p); A 2 A ]
p
if c > then c p 2 c
2
if s < 0 then [s 2 s ; A 2 A ]
638
Discussion
J ( p ) J ( p ) =
O(d ( p ) + =d ( p ) ? p )
i
639
O
O
O
O
J
= 2 fi (fi )z
z
J
= 2 fi (fi )A
A
J
= 2 fi (fi )B
B
J
= 2 fi (fi )C
C
6.
Appendix B.
Correctness
Spheres
Od (x 2 x )/| x 2 x |
Od (y 2 y )/| x 2 x |
Od (z 2 z )/| x 2 x |
Od
J
=22
x
J
=22
y
J
=22
z
J
=22
r
2D Circles
Od (x 2 x )/(d + r )
Od (y 2 y )/(d + r )
Od
J
=22
x
J
=22
y
J
=22
r
Planes
J (x , A ) =
Og
3D Circles
2
i
O
O
O
O
O
O
O
J
= 2 [gi (gi )x + (fi 2 r )(fi )x ]
x
J
= 2 [gi (gi )y + (fi 2 r )(fi )y ]
y
J
= 2 [gi (gi )z + (fi 2 r )(fi )z ]
z
J
= 2 [gi (gi )A + (fi 2 r )(fi )A ]
A
J
= 2 [gi (gi )B + (fi 2 r )(fi )B ]
B
J
= 2 [gi (gi )C + (fi 2 r )(fi )C ]
C
J
= 2 2 (fi 2 r )
r
O
O
O
O
O
O
J
= 2 gi (gi )x
x
J
= 2 gi (gi )y
y
J
= 2 gi (gi )z
z
J
= 2 gi (gi )A
A
J
= 2 gi (gi )B
B
J
= 2 gi (gi )C
C
Lines
J (x , A ) =
J
=2
x
J
=2
y
Of
Cylinders
O(f 2 r )(f )
O(f 2 r )(f )
O(f 2 r )(f )
2
i
J
=2
x
J
=2
y
J
=2
z
Of (f )
Of (f )
i
i x
i y
640
i x
i y
i z
O
O
O
O
J
= 2 (fi 2 r )(fi )A
A
J
= 2 (fi 2 r )(fi )B
B
J
= 2 (fi 2 r )(fi )C
C
J
= 2 2 (fi 2 r )
r
7.
References
[1] T. H. Hopp, Computational MetrologyVol. 6, No. 4, Manufacturing Review, American Society of Mechanical Engineers,
New York (1993) pp. 295304.
[2] R. Walker, CMM Form Tolerance Algorithm Testing, GIDEP
Alert X1-A-88-01, Government-Industry Data Exchange Program, Department of Defense, Washington, D.C. (1988).
[3] D. A. Rosenfeld, Users Guide for the Algorithm Testing System Version 2.0, NISTIR 5674, National Institute of Standards
and Technology, Gaithersburg, MD (1995).
[4] ASME, Methods for Performance Evaluation of Coordinate
Measuring System Software, B89.4.10-199x, DRAFT, American Society of Mechanical Engineers, New York (1998).
[5] C. Diaz and T. H. Hopp, NIST Special Test Service: The Algorithm Testing and Evaluation Program for Coordinate Measuring Systems, NISTSP 250-41, National Institute of Standards
and Technology, Gaithersburg, MD (to be published).
[6] C. Diaz and T. H. Hopp, Testing Coordinate Measuring Systems
Software, Proceedings of ASQC Measurement Quality Conference, October 2627, National Institutes of Standards and Technology, Gaithersburg, MD (1993).
[7] M. E. A. Algeo and T. H. Hopp, Form Error Models of the NIST
Algorithm Testing System, NISTIR 4740, National Institute of
Standards and Technology, Gaithersburg, MD, January 1992.
[8] T. M. Apostol, Calculus, Volume II, Second Edition, John Wiley & Sons, Inc., New York (1969) pp. 314318.
[9] G. H. Golub and C. F. van Loan, Matrix Computations, The
Johns Hopkins University Press, Baltimore, MD (1983).
[10] G. W. Stewart, Matrix Computations, Academic Press, Inc.,
Orlando, FL (1973) p. 320.
[11] J. C. Nash, Compact Numerical Methods for Computers: Linear
Algebra and Function Minimisation, Adam Hilger, Ltd., Bristol,
England (1979).
[12] J. J. More, The Levenberg-Marquardt Algorithm: Implementation and Theory, in Numerical Analysis: Proc. Biennial Conf. on
Numerical Analysis, Dold and Eckmann, eds., Dundee, Scotland
(1977).
Cones
O
O
O
O
O
O
O
O
J
= 2 di [(fi )x cosc + (gi )x sinc ]
x
J
= 2 di [(fi )y cosc + (gi )y sinc ]
y
J
= 2 di [(fi )z cosc + (gi )z sinc ]
z
J
= 2 di [(fi )A cosc + (gi )A sinc ]
A
J
= 2 di [(fi )B cosc + (gi )B sinc ]
B
J
= 2 di [(fi )C cosc + (gi )C sinc ]
C
J
= 2 di [2 fi sinc + gi cosc ]
c
J
= 2 2 di
s
Tori
O
O
O
O
O
O
O
O
J
di
=2
[g (g ) + (fi 2 r )(fi )x ]
x
di + R i i x
di
J
=2
[g (g ) + (fi 2 r )(fi )y ]
y
di + R i i y
J
di
=2
[g (g ) + (fi 2 r )(fi )z ]
z
di + R i i z
di
J
=2
[g (g ) + (fi 2 r )(fi )A ]
A
di + R i i A
J
di
=2
[g (g ) + (fi 2 r )(fi )B ]
B
di + R i i B
J
di
=2
[g (g ) + (fi 2 r )(fi )C ]
C
di + R i i C
J
di
=2
[g (g ) 2 (fi 2 r )]
di + R i i r
r
J
= 2 2 di
R
641