VTK—— “点” “线” “面”相关示例

示例包含:

  • 求两点之间的距离
  • 求点到线之间的距离
  • 把一个点投射到平面上
  • 透视坐标转化 perspective transform 和 transform

1. 求两点之间的距离

#include 
#include 

int main(int, char *[])
{
    double p1[3] = {0.0, 0.0, 0.0};
    double p2[3] = {1.0, 1.0, 1.0};
    
    double squaredDistance = vtkMath::Distance2BetweenPoints(p1, p2); //两点之间的距离的平方
    double distance = sqrt(squaredDistance);
    
    qDebug() << "squaredDistance : " << squaredDistance;        //3
    qDebug() << "distance : " << distance;                      //1.73205
    
    return EXIT_SUCCESS;
}

 

2.求点到线之间的距离

#include 
#include 
#include 

int main(int, char *[])
{
    double lineP0[3] = {0.0, 0.0, 0.0};
    double lineP1[3] = {2.0, 0.0, 0.0};
    
    double p0[3] = {1.0, 0, 0};
    double p1[3] = {1.0, 2.0, 0};
    
    double dist0 = vtkLine::DistanceToLine(p0, lineP0, lineP1); //点到线距离的平方
    double dist1 = vtkLine::DistanceToLine(p1, lineP0, lineP1);
    
    qDebug() << "dist0 : " << dist0;        //0
    qDebug() << "dist1 : " << dist1;        //4  
    
    double t;
    double closest[3];
    double dist2 = vtkLine::DistanceToLine(p0, lineP0, lineP1, t, closest); //最近点,即点到线的垂直投影点
    
    qDebug() << "dist2 : " << dist2 
             << "closest point: " << closest[0] << " , " << closest[1] << " , " << closest[2];
             //0    1, 0, 0
    
    double dist3 = vtkLine::DistanceToLine(p1, lineP0, lineP1, t, closest);
    
    qDebug() << "dist3 : " << dist3 
             << "closest point: " << closest[0] << " , " << closest[1] << " , " << closest[2];
             //4    1, 0, 0
    
    
    return EXIT_SUCCESS;
}

 

3.把一个点投射到平面上

#include 
#include 
#include 

int main(int, char *[])
{
    vtkSmartPointer plane = vtkSmartPointer::New();
    double p[3] = {1, 2, 3};
    double projected[3];
    
    //重载方式1
    plane->SetOrigin(0.0, 0.0, 0.0);
    plane->SetNormal(0.0, 0.0, 1.0);
    plane->ProjectPoint(p, projected);
    qDebug() << "projected : " << projected[0] << " " << projected[1] << " " << projected[2];
    //projected :  1   2   0
    
    //重载方式2
    double origin[3] = {0.0, 0.0, 0.0};
    double normal[3] = {0.0, 0.0, 1.0};

    plane->ProjectPoint(p, origin, normal, projected);    
    qDebug() << "projected : " << projected[0] << " " << projected[1] << " " << projected[2];
    //projected :  1   2   0
    
    return EXIT_SUCCESS;
}

 

4. 透视坐标转化 perspective transform 和 transform

perspective transform透视变换在与矩阵相乘之前,会自动将“齐次1”附加到该点上。
然后将结果的前3个坐标除以结果的第4个坐标。
#include 
#include 
#include 
#include 
#include 

int main(int, char *[])
{
    vtkSmartPointer m = vtkSmartPointer::New();
    m->SetElement(0, 0, 1);
    m->SetElement(0, 1, 2);
    m->SetElement(0, 2, 3);
    m->SetElement(0, 3, 4);
    m->SetElement(1, 0, 2);
    m->SetElement(1, 1, 2);
    m->SetElement(1, 2, 3);
    m->SetElement(1, 3, 4);
    m->SetElement(2, 0, 3);
    m->SetElement(2, 1, 2);
    m->SetElement(2, 2, 3);
    m->SetElement(2, 3, 4);
    m->SetElement(3, 0, 4);
    m->SetElement(3, 1, 2);
    m->SetElement(3, 2, 3);
    m->SetElement(3, 3, 4);
    
    //normal
    vtkSmartPointer transform = vtkSmartPointer::New();
    transform->SetMatrix(m);
    
    float normalProjection[3];
    transform->TransformFloatPoint(normalProjection);
    
    qDebug() << "standard projection : " << normalProjection[0] << " , "
             << normalProjection[1] << " , " << normalProjection[2];
    //standard projection :  -1.07374e+08  ,  -1.07374e+08  ,  -1.07374e+08
    
    //perspective
    vtkSmartPointer perspectiveTrans = vtkSmartPointer::New();
    perspectiveTrans->SetMatrix(m);
    
    float perspectiveProjection[3];
    perspectiveTrans->TransformFloatPoint(perspectiveProjection);
    
    qDebug() << "perspective projection : " << perspectiveProjection[0] << " , "
             << perspectiveProjection[1] << " , " << perspectiveProjection[2];
    //perspective projection :  -1.07374e+08  ,  -1.07374e+08  ,  -1.07374e+08
    

    return EXIT_SUCCESS;
}

 

你可能感兴趣的:(VTK)