简体   繁体   English

Javascript中的3D三边形

[英]3D Trilateration in Javascript

I'm trying to do 3D trilateration in Javascript using https://github.com/gheja/trilateration.js and it seems to be working. 我正在尝试使用https://github.com/gheja/trilateration.js在Javascript中进行3D三边测量,它似乎正在工作。 However, certain simple cases should be yielding solutions but they are not. 但是,某些简单的案例应该产生解决方案,但事实并非如此。 For example: 例如:

var p1 = {x:69, y:0,  r:69, z:0}
var p2 = {x:0,  y:50, r:50, z:0};
var p3 = {x:0,  y:80, r:80, z:0};
trilaterate(p1, p2, p3, false)

This seems like a ridiculously simple example that should yield a solution at x:0,y:0, but instead the function tells me that there is no solution. 这似乎是一个非常简单的例子,它应该在x:0,y:0处产生一个解决方案,但是该函数告诉我没有解决方案。 Am I misunderstanding something about trilateration or is there an error in the function? 我是否误解了关于三边测量的事情或者函数中是否存在错误?

Any help would be appreciated. 任何帮助,将不胜感激。

it looks like it's an issue with the repo you found. 它看起来像你发现的回购的问题。

specifically if you look at this line https://github.com/gheja/trilateration.js/blob/master/trilateration.js#L111 and log out the values it's using to calculate z = Math.sqrt(sqr(p1.r) - sqr(x) - sqr(y)); 特别是如果你看这行https://github.com/gheja/trilateration.js/blob/master/trilateration.js#L111并注销它用来计算z = Math.sqrt(sqr(p1.r) - sqr(x) - sqr(y)); :

sqr(p1.r): 4761
-sqr(x) - sqr(y): -4761.000000000017
sqr(p1.r) - sqr(x) - sqr(y): -0.000000000017
z: Math.sqrt(-0.000000000017)
therefore: z: NaN

it's just a feature of floats ( Is floating point math broken? ). 它只是浮点数的一个特征( 浮动点数学是否被打破? )。 If you change the order of your arguments ( trilaterate(p1, p3, p2, false) ) you get 2 values that are very close to the right answer. 如果更改参数的顺序( trilaterate(p1, p3, p2, false) ),则会得到2个非常接近正确答案的值。

Really your test should be a special case; 真的,你的考试应该是一个特例; the intersection of your first 2 spheres is a single point. 前两个球体的交点是单点。 You might consider forking the repo and testing for just barely touching spheres if this is an expected use case. 如果这是一个预期的用例,您可能会考虑分配repo并测试几乎不接触球体。

Yes, it was actually a bug in the library, thanks @logidelic for finding it and @dtudury for tracking it down. 是的,它实际上是库中的一个错误,感谢@logidelic找到它并且@dtudury跟踪它。

I have fixed it now by zeroing the value if it is near to zero: 我现在通过将值归零来修复它 ,如果它接近于零:

b = sqr(p1.r) - sqr(x) - sqr(y);

if (Math.abs(b) < 0.0000000001)
{
    b = 0;
}

z = Math.sqrt(b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM