2017-12-05 25 views
0

krlzlxからのアドバイスの後、新しい質問として投稿しました。3Dラインセグメントと平面交差 - Contd

ここから:

3D Line Segment and Plane Intersection

私はこのアルゴリズムに問題がある、私はそうのようにそれを実装している:

class Point3d { 
    public: 
    double x; 
    double y; 
    double z; 

    /** 
    * constructor 
    * 
    * 0 all elements 
    */ 
    Point3d() { 
     x = 0.0; 
     y = 0.0; 
     z = 0.0; 
    } 

template <class T> 
class AnyCollision { 
    public: 
    std::pair<bool, T> operator()(Point3d &ray, Point3d &rayOrigin, Point3d &normal, Point3d &coord) const { 

    // get d value 
    float d = (normal.x * coord.x) + (normal.y * coord.y) + (normal.z * coord.z); 

    if (((normal.x * ray.x) + (normal.y * ray.y) + (normal.z * ray.z)) == 0) { 
     return std::make_pair(false, T()); 
    } 

    // Compute the X value for the directed line ray intersecting the plane 
    float a = (d - ((normal.x * rayOrigin.x) + (normal.y * rayOrigin.y) + (normal.z * rayOrigin.z))/((normal.x * ray.x) + (normal.y * ray.y) + (normal.z * ray.z))); 

    // output contact point 
    float rayMagnitude = (sqrt(pow(ray.x, 2) + pow(ray.y, 2) + pow(ray.z, 2))); 
    Point3d rayNormalised((ray.x/rayMagnitude), (ray.y/rayMagnitude), (ray.z/rayMagnitude)); 
    Point3d contact((rayOrigin.x + (rayNormalised.x * a)), (rayOrigin.y + (rayNormalised.y * a)), (rayOrigin.z + (rayNormalised.z * a))); //Make sure the ray vector is normalized 
    return std::make_pair(true, contact); 

}; 

Point3dとは、次のように定義されます私はこの構造を使用することを余儀なくされています。なぜなら、より大きなシステムでは、これのように定義され、変更することはできません。

私のコードは正常にコンパイルされますが、テストではポイントの値が正しくありません。 x、y、zの比率は正しいが、大きさは間違っている。例えば

は場合:私は、ポイントがあることを期待

rayOrigin.x = 0; 
rayOrigin.y = 0; 
rayOrigin.z = 0; 

ray.x = 3; 
ray.y = -5; 
ray.z = 12; 

normal.x = -3; 
normal.y = 12; 
normal.z = 0; 

coord.x = 7; 
coord.y = -5; 
coord.z = 10; 

(0.63, 1.26, 1.89) 

しかし、それは次のとおりです。

(3.52, -5.87, 14.09) 

大きすぎる5.09の大きさ。

そして私も試験:

rayOrigin.x = 0; 
rayOrigin.y = 0; 
rayOrigin.z = 0; 

ray.x = 2; 
ray.y = 3; 
ray.z = 3; 

normal.x = 4; 
normal.y = 1; 
normal.z = 0; 

p0.x = 2; 
p0.y = 1; 
p0.z = 5; 

私はポイントがあることを期待:

(1.64, 2.45, 2.45) 

しかし、それは次のとおりです。

(3.83761, 5.75642, 5.75642) 

大きすぎる2.34の大きさは?

+0

あなたは慎重にチェックした場合、ポイントは*完全*間違っています。スケールファクタを持つための物理的な理由はなく、実際にはそうではありません。 – meowgoesthedog

+0

私はこれらをチェックのために使用しました: http://onlinemschool.com/math/assistance/cartesian_coordinate/plane/ http://www.ambrsoft.com/TrigoCalc/Plan3D/PlaneLineIntersection_.htm – Steve

答えて

0

擬似コード(ベクトルの正規化を必要としない):

Diff = PlaneBaseCoordinate - RayOrigin 
d = Normal.dot.Diff 
e = Normal.dot.RayVector 

if (e) 
    IntersectionPoint = RayOrigin + RayVector * d/e 
otherwise 
    ray belongs to the plane or is parallel 

クイックチェック:

Ray (0,0,0) (2,2,2) //to catch possible scale issues 
Plane (0,1,0) (0,3,0) //plane y=1 
Diff = (0,1,0) 
d = 3 
e = 6 
IntersectionPoint = (0,0,0) + (2,2,2) * 3/6 = (1, 1, 1) 
関連する問題