在3D空间中给定多条直线(N条),使用最小二乘法找到距离所有直线距离最小的3D点。
How can I express this in Least Square Matrix Form? Thus, Having:
[A] - Variables matrix ( known )
[x] - Unknown vector
[b] - dependent variables vector
Ax=b => Least-Square
有了距离公式的基础,A、x和'b'应该填什么?
在3D空间中给定多条直线(N条),使用最小二乘法找到距离所有直线距离最小的3D点。
How can I express this in Least Square Matrix Form? Thus, Having:
[A] - Variables matrix ( known )
[x] - Unknown vector
[b] - dependent variables vector
Ax=b => Least-Square
d^2
的公式,您会发现它的形式为:d^2 = Cxx Px Px + Cxy Px Py + ... + Ex Px + Ey Py + Ez Pz + F
其中Cxx,Ex,F等是与A和B向量有关的常数。现在对每一行求和,将其称为S,这是Px、Py和Pz的二次函数。
S = Sxx Px Px + Sxy Px Py + ... + Rx Px + Ry Py + Rz Pz + T
dS/dx = 2 Sxx Px + Sxy Py + Sxz Pz + Rx
dS/dy = Syx Px + 2 Syy Py + Syz Pz + Ry
dS/dz = Szx Px + Szy Py + 2 Szz Pz + Rz
var sxx = 0;
var sxy = 0;
var syy = 0;
var rx = 0;
var ry = 0;
var t = 0;
// each line is defined by a x + b y + c = 0
lines.forEach(function(line, index, array) {
var div = line.a * line.a + line.b * line.b;
sxx += line.a * line.a / div;
sxy += line.a * line.b / div;
syy += line.b * line.b / div;
rx += line.a * line.c / div;
ry += line.b * line.c / div;
t += line.c * line.c / div;
});
// Derivative of S wrt x and y is
// 2 sxx x + 2 sxy y + 2 rx = 0
// 2 sxy x + 2 syy y + 2 ry = 0
// Solve this pair of linear equations
var det = sxx * syy - sxy * sxy;
if(Math.abs(det) < 1e-6) {
sol = { x: -10, y: -10 };
return;
}
// (x) = 1 ( syy -sxy ) ( -rx )
// ( ) = --- ( ) ( )
// (y) det ( -sxy sxx ) ( -ry )
var x = ( - syy * rx + sxy * ry ) / det;
var y = ( sxy * rx - sxx * ry ) / det;
console.log("x "+x+" y "+y);
sol = { x: x, y: y };
return;