赞
踩
wiki PolygonIntersection 多边形和线的交叉点
vtk绘制多边形流程
vtkPoints,vtkCellArray,vtkPolyData组合使用
先添加点,相邻两点建立单元,给数据对象添加点和单元
只是利用vtkPolyData不够的 凹多边形绘制会有问题,利用vtkTrianglFiler 转三角形后 就可以了
首先通过
// 先添加多边形的各点
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(0.0, 0.0, 0.0);
points->InsertNextPoint(1.0, 0.0, 0.0);
points->InsertNextPoint(1.0, 1.0, 0.0);
points->InsertNextPoint(0.0, 1.0, 0.0);
// 通过点集建立多边形
vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
polygon->GetPoints()->DeepCopy(points);
polygon->GetPointIds()->SetNumberOfIds(4); // 4 corners of the square
polygon->GetPointIds()->SetId(0, 0);
polygon->GetPointIds()->SetId(1, 1);
polygon->GetPointIds()->SetId(2, 2);
polygon->GetPointIds()->SetId(3, 3);
void SetId(const vtkIdType i, const vtkIdType vtkid) {this->Ids[i] = vtkid;};
适用于已知总数。void InsertId(const vtkIdType i, const vtkIdType vtkid);
设置 i 的 id 为 vtkid,做溢出检查,并在必要时自动分配空间。
适用于未知总数。
// 声明
int IntersectWithLine(double p1[3], double p2[3], double tol, double& t,double x[3], double pcoords[3], int& subId);
// 测试数据
double p1[3] = { 0.1, 0, -1.0 };
double p2[3] = { 0.1, 0, 1.0 };
double tolerance = 0.001; // 公差
// 输出
double t; // 交点的线段参量(比例)坐标,p1是0,p2是1
double x[3]; // 交点坐标
double pcoords[3];
int subId;
// 使用
vtkIdType iD = polygon->IntersectWithLine(p1, p2, tolerance, t, x, pcoords, subId);
检测多边形polgon
与线段(p1,p2)
是否相交,若相交,则返回1,不相交(包括内部),则返回0。
t :面与直线相交于直线的哪部分。
相对于 线段 的参量(比例)坐标(p1为0,p2为1),可能超出[0,1]。
x :直线与平面相交于哪点。
p1、p2所在直线 与 多边形所在平面 的相交点的坐标,若平行则无限大:
-9.25596e+061 -9.25596e+061 -9.25596e+061。
pcoords :线段与面相交与哪点。
p1、p2所构成的 线段 与 多边形所在平面 的相交点的坐标,无则 0 0 0
官方api解释:
Intersect with a ray. Return parametric coordinates (both line and cell) and global intersection coordinates,
given ray definition and tolerance. The method returns non-zero value if intersection occurs.
与上同
static int Intersection(double p1[3], double p2[3],
double x1[3], double x2[3],
double& u, double& v);
vtkIdType isture = line->Intersection(p1, p2, p3, p4, u, v);
检测两线是否相交,并返回相交位置在各线的比例位置。
如果两线相交,则返回2,若不相交,则返回3。
若不相交,则将x1,x2投影到p1,p2所在平面,计算u、v。
#include "vtkSmartPointer.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkCylinderSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include <vtkLine.h>
#include <vtkPoints.h>
#include <vtkPolygon.h>
int main(int, char *[])
{
// Create a square in the XY plane
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(0.0, 0.0, 0.0);
points->InsertNextPoint(1.0, 0.0, 0.0);
points->InsertNextPoint(1.0, 1.0, 0.0);
points->InsertNextPoint(0.0, 1.0, 0.0);
// Create the polygon
vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
polygon->GetPoints()->DeepCopy(points);
polygon->GetPointIds()->SetNumberOfIds(4); // 4 corners of the square
polygon->GetPointIds()->SetId(0, 0);
polygon->GetPointIds()->SetId(1, 1);
polygon->GetPointIds()->SetId(2, 2);
polygon->GetPointIds()->SetId(3, 3);
// Inputs
double p1[3] = { 1, 0, -1 };
double p2[3] = { 1, 0, 1 };
double tolerance = 0.001; // 公差
// Outputs
double t; // Parametric coordinate of intersection (0 (corresponding to p1) to 1 (corresponding to p2))
double x[3]; // The coordinate of the intersection
double pcoords[3];
int subId;
vtkIdType iD = polygon->IntersectWithLine(p1, p2, tolerance, t, x, pcoords, subId);
std::cout << "t: " << t << std::endl;
std::cout << "pcoords: " << pcoords[0] << " " << pcoords[1] << " " << pcoords[2] << std::endl;
std::cout << "相交? " << iD << std::endl;
std::cout << "subid: " << subId << std::endl;
std::cout << "x : " << x[0] << " " << x[1] << " " << x[2] << std::endl;
vtkSmartPointer<vtkLine> line = vtkSmartPointer<vtkLine>::New();
double p3[3] = { 1, -1, 0 };
double p4[3] = { 1, 1, 0 };
double u, v;
vtkIdType iD2 = line->Intersection(p1, p2, p3, p4, u, v);
std::cout <<"u :" << u << endl;
std::cout << "v :" << v << endl;
std::cout << "相交? :" << iD2 << endl;
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。