Geometry 计算GLSL中的点到线距离

Geometry 计算GLSL中的点到线距离,geometry,glsl,webgl,Geometry,Glsl,Webgl,我正试图在GSLS中计算点到线的距离——精确地说是在turbo.js中 这是一个更一般问题的一部分,我试图找到与一组GeoJSON点相对应的[GeoJSON多行上最近的点]——在1000条线段上设置的500个点的计算数量最终是500k点到距离计算 这在浏览器中(甚至在workers中)处理起来太多了,所以并行性帮助很大 诀窍是AFAIK I只能使用vec4作为输入,这意味着我只能对点对进行计算 到目前为止,我已经开始计算所有线对的距离和方位,但无法计算点到线的距离 所以问题是-给3分a,b和c,

我正试图在GSLS中计算点到线的距离——精确地说是在turbo.js中

这是一个更一般问题的一部分,我试图找到与一组GeoJSON点相对应的[GeoJSON多行上最近的点]——在1000条线段上设置的500个点的计算数量最终是500k点到距离计算

这在浏览器中(甚至在workers中)处理起来太多了,所以并行性帮助很大

诀窍是AFAIK I只能使用vec4作为输入,这意味着我只能对点对进行计算

到目前为止,我已经开始计算所有线对的距离和方位,但无法计算点到线的距离

所以问题是-给3分a,b和c,并且知道

  • 它们在lon和lat中的位置
  • 它们成对的方位和距离
是否可以使用使用vec2、vec3或vec4作为输入参数的变换来计算从a到b和c定义的直线的距离

作为一个子问题,我知道如果三角形(a,b,c)的高度不与直线(a,b)相交,那么如何计算距离,因为它是最小的(距离(a,b),距离(a,c))


但是,我如何计算它是否相交呢?

我不能完全确定我是否理解你的问题

对于500个输入点,对于1000条线段,对于每个点,哪一条线段是最近的

如果这就是你要问的,那么把所有的点放在浮点纹理中(纹理的另一个词是2D数组)。绘制一个-1到+1的四边形,这是纹理分辨率中通过的结果数(500个结果,因此为50x10或25x20等…)的大小。使用
gl\u FragCoord
计算一个索引,以获得输入、A和所有其他行的循环。通过将最近对的索引编码为颜色,通过readPixels读取结果

  precision highp float;

  uniform sampler2D aValues;
  uniform vec2 aDimensions;  // the size of the aValues texture in pixels (texels)
  uniform sampler2D bValues;
  uniform vec2 bDimensions;  // the size of the bValues texture in pixels (texels)
  uniform sampler2D cValues;
  uniform vec2 cDimensions;  // the size of the cValues texture in pixels (texels)
  uniform vec2 outputDimensions; // the size of the thing we're drawing to (canvas)

  // this code, given a sampler2D, the size of the texture, and an index
  // computes a UV coordinate to pull one RGBA value out of a texture
  // as though the texture was a 1D array.
  vec3 getPoint(in sampler2D tex, in vec2 dimensions, in float index) {
    vec2 uv = (vec2(
       floor(mod(index, dimensions.x)),
       floor(index / dimensions.x)) + 0.5) / dimensions;
    return texture2D(tex, uv).xyz;
  }

  // from https://stackoverflow.com/a/6853926/128511
  float distanceFromPointToLine(in vec3 a, in vec3 b, in vec3 c) {
    vec3 ba = a - b;
    vec3 bc = c - b;
    float d = dot(ba, bc);
    float len = length(bc);
    float param = 0.0;
    if (len != 0.0) {
      param = clamp(d / (len * len), 0.0, 1.0);
    }
    vec3 r = b + bc * param;
    return distance(a, r);
  }

  void main() {
    // gl_FragCoord is the coordinate of the pixel that is being set by the fragment shader.
    // It is the center of the pixel so the bottom left corner pixel will be (0.5, 0.5).
    // the pixel to the left of that is (1.5, 0.5), The pixel above that is (0.5, 1.5), etc...
    // so we can compute back into a linear index 
    float ndx = floor(gl_FragCoord.y) * outputDimensions.x + floor(gl_FragCoord.x); 
    
    // find the closest points
    float minDist = 10000000.0; 
    float minIndex = -1.0;
    vec3 a = getPoint(aValues, aDimensions, ndx);
    for (int i = 0; i < ${bPoints.length / 4}; ++i) {
      vec3 b = getPoint(bValues, bDimensions, float(i));
      vec3 c = getPoint(cValues, cDimensions, float(i));
      float dist = distanceFromPointToLine(a, b, c);
      if (dist < minDist) {
        minDist = dist;
        minIndex = float(i);
      }
    }
    
    // convert to 8bit color. The canvas defaults to RGBA 8bits per channel
    // so take our integer index (minIndex) and convert to float values that
    // will end up as the same 32bit index when read via readPixels as
    // 32bit values.
    gl_FragColor = vec4(
      mod(minIndex, 256.0),
      mod(floor(minIndex / 256.0), 256.0),
      mod(floor(minIndex / (256.0 * 256.0)), 256.0) ,
      floor(minIndex / (256.0 * 256.0 * 256.0))) / 255.0;
  }

绿光释光
js

这是否回答了您的问题@LJᛃ - 不,它不是,因为问题是当一个函数的最大输入参数数为4(由
read()
x拉取的vec4)时如何进行。好吧,这不是
webgl
glsl
问题。由于图书馆也没有标签,我想你最好的办法是在图书馆的github上提交一个问题。我怀疑你对我的问题理解正确,我希望我能对你的答案说同样的话,这远远超过我目前对GLSL的理解水平。我会在接下来的几天里学习并接受它-非常感谢!你在理解哪一部分时有困难?添加了一些评论。这也是一个例子,不是你,是我。我得到了一般的概念和想法,但我还不熟悉GLSL,所以一些概念目前还不清楚。不幸的是,我最不清楚的部分是读/写数据。我会尽快翻阅教程。我已经很感激这个答案和你过去给我的其他答案是的,我知道你对GLSL不太熟悉。我加了些东西以防万一。我知道这不像几段解释那么简单,但也许会有帮助。