完全未经测试。
如果你从十字路口沿着线路的方向走
Principle Component
这样可以得到它们的方向。然后使用该方向和任意点创建一个平面,将平面相交计算中的所有点投影到平面上,并找到这些投影点的平均点。
使用这个平均点和主分量来定义你的线。
有点像。。。
class Plane
{
public:
Vector3 Point;
Vector3 Normal;
Line Intersect (const Plane &other);
Vector3 Project (const Vector3 &point);
}
class Line
{
public:
Vector3 Point;
Vector3 Direction;
Line (Vector3 point, Vector3 dir);
};
Vector3 PrincipleComponent (const std::vector<Line> &lines)
{
//You could use the covariance matrix to get this but I will try the interative method on wikipedia.
Vector3 p(1,2,3); //a random vector?
static const int c = 10;
for (int i = 0; i < c; ++i)
{
Vector3 t;
for (auto i = lines.begin(); i != lines.end (); ++i)
{
t = t + ((*i).Direction.Dot (p)) * (*i).Direction;
}
t.Normalize();
p = t;
}
return p;
}
int main ()
{
std::vector<Line> LinesFromPlaneIntersections;
Vector3 direction = PrincipleComponent (LinesFromPlaneIntersections);
Plane projplane;
projplane.Normal = direction;
projplane.Point = LinesFromPlaneIntersections[0].Point;
Vector3 meanpoint;
for (auto i = LinesFromPlaneIntersections.begin(); i != LinesFromPlaneIntersections.end (); ++i)
{
meanpoint += projplane.Project ((*i).Point);
}
meanpoint /= LinesFromPlaneIntersections.size ();
Line result (meanpoint,direction);
}