Efficient way of finding distance between two 3D points

后端 未结 11 1060
借酒劲吻你
借酒劲吻你 2021-02-01 06:42

I am writing a code in C++ and want to compute distance between two points. Question 1:

I have two points P(x1, y1, z1) and Q(x2, y2, z2) , where x,

11条回答
  •  醉梦人生
    2021-02-01 07:09

    You may try to use SSE extensions. For example, you can init two vectors A(x1,y1,z1) and B(x2,y2,z2):

    _m128 A = _mm_set_ps(x1, y1, z1, 0.0f)
    _m128 B = _mm_set_ps(x2, y2, z2, 0.0f)
    

    Then compute diff using _mm_sub_ps:

    __m128 Diff = _mm_sub_ps(A, B)
    

    Next compute sqr of diff:

    __m128 Sqr = __mm_mul_ps(Diff, Diff)
    

    And finally:

    __m128 Sum = add_horizontal(Sqr)
    __m128 Res = _mm_sqrt_ss(Sum)
    

    Res[0] will be filled with your answer.

    P.S. add_horizontal is a place for optimization

提交回复
热议问题