您可能会使用以下数据结构:
struct Point
{
double x, y;
};
struct Triangle
{
unsigned int ids[3];
bool isValid;
};
// Store points for each frame
std::vector<std::vector<Point>> points;
// Store triangles
std::vector<Triangle> triangles;
您还可以像这样为每个帧保留所有三角形:
// Store triangles for each frame
std::vector<std::vector<Triangle>> triangles;
这是一个“工作示例”。3 帧期间 4 个点,2 个三角形。它只输出顶点都是正的三角形。从一帧到另一帧,点云沿 Y 轴平移 -1。这显然是一个假测试,但它希望能帮助你开始。
#include <vector>
#include <iostream>
struct Point
{
double x, y;
};
struct Triangle
{
unsigned int ids[3];
bool isValid;
};
void TrackFirstFrame(std::vector<Point> &firstFrame)
{
Point p1, p2, p3, p4;
p1.x = 1; p1.y = 0;
p2.x = 2; p2.y = 1;
p3.x = 1; p3.y = 2;
p4.x = 0; p4.y = 1;
firstFrame[0] = p1;
firstFrame[1] = p2;
firstFrame[2] = p3;
firstFrame[3] = p4;
}
void Delaunay(const std::vector<Point> &points, std::vector<Triangle> &triangles)
{
Triangle t1;
t1.ids[0] = 0;
t1.ids[1] = 1;
t1.ids[2] = 3;
triangles.push_back(t1);
Triangle t2;
t2.ids[0] = 1;
t2.ids[1] = 2;
t2.ids[2] = 3;
triangles.push_back(t2);
}
// Assumption: all previous frame points give a new tracked point for current frame
void TrackFrame(const std::vector<Point> &previousFramePoints, unsigned int currentFrame, std::vector<Point> &trackedPoints)
{
for (unsigned int i = 0; i < previousFramePoints.size(); ++i)
{
Point previousPoint = previousFramePoints[i];
Point trackedPoint;
trackedPoint.x = previousPoint.x;
trackedPoint.y = previousPoint.y - 1;
trackedPoints[i] = trackedPoint;
}
}
// Assumption: all vertices are positive. If not, triangle is invalid
void UpdateTriangles(const std::vector<Point> &points, std::vector<Triangle> &triangles)
{
std::vector<Triangle>::iterator trianglesIT = triangles.begin();
for (; trianglesIT != triangles.end(); ++trianglesIT)
{
(*trianglesIT).isValid = true; // By default
for (unsigned int i = 0; i < 3; ++i)
{
Point vertex = points[(*trianglesIT).ids[i]];
if (vertex.x < 0 || vertex.y < 0)
{
(*trianglesIT).isValid = false;
break;
}
}
}
}
void PrintPoints(const std::vector<Point> &points)
{
std::cout<<"Points"<<std::endl;
std::vector<Point>::const_iterator pointsIT = points.begin();
for (; pointsIT != points.end(); ++pointsIT)
{
std::cout<<"("<<pointsIT->x<<", "<<pointsIT->y<<")"<<std::endl;
}
}
void PrintTriangles(const std::vector<Triangle> &triangles)
{
std::cout<<"Triangles"<<std::endl;
std::vector<Triangle>::const_iterator trianglesIT = triangles.begin();
for (; trianglesIT != triangles.end(); ++trianglesIT)
{
if (trianglesIT->isValid)
{
std::cout<<"["<<trianglesIT->ids[0]<<", "<<trianglesIT->ids[1]<<", "<<trianglesIT->ids[2]<<"])"<<std::endl;
}
}
}
int main()
{
unsigned int nbFrames = 3;
unsigned int nbPoints = 4;
// Init 2D points
std::vector<std::vector<Point>> points;
points.resize(nbFrames);
std::vector< std::vector<Point> >::iterator framesIT = points.begin();
for (; framesIT != points.end(); ++framesIT)
{
framesIT->resize(nbPoints);
}
TrackFirstFrame(points[0]);
std::cout<<"Frame 0"<<std::endl;
PrintPoints(points[0]);
// Init triangles with Delaunay. 4 points => 2 triangles;
std::vector<Triangle> triangles;
Delaunay(points[0], triangles);
PrintTriangles(triangles);
for (unsigned int i = 1; i < nbFrames; ++i)
{
std::cout<<"Track frame #"<<i<<std::endl;
TrackFrame(points[i-1], i, points[i]);
PrintPoints(points[i]);
UpdateTriangles(points[i], triangles);
PrintTriangles(triangles);
}
char c;
std::cin >> c;
}