// 调用函数
int CTriNetInterp::CreateTriNet(float **pfPole, int **pnPoleIndex, int nIndexNum, int *pnPoleNum, triangleMSet &triNet)
{
vertexSet vertexNew;
triangleMSet triNetNew;
triangleIterator tItDump;
vertexIterator vItDump;
triNet.clear();
if (pfPole == NULL || pnPoleIndex == NULL || pnPoleNum == NULL)
return -1;
for (i=0; i<nIndexNum; i++)
{
vertexNew.clear();
triNetNew.clear();
// insert data into vertex new
for (j=0; j<pnPoleNum[i]; j++)
vertexNew.insert(CVertex(pfPole[pnPoleIndex[i][j]][0], pfPole[pnPoleIndex[i][j]][1], pfPole[pnPoleIndex[i][j]][2]));
// dump vertex new,打印的对的
TRACE("/tNew Vertex get %d elements!/n", vertexNew.size());
TRACE("++/n/tDump vertex set begin/n");
for (vItDump=vertexNew.begin(); vItDump!=vertexNew.end(); vItDump++)
{
TRACE("/t/tvertex new :/t(%f, %f, %f)/n",
vItDump->GetX(), vItDump->GetY(), vItDump->GetZ());
}
TRACE("/tDump end/n--/n");
// CALL THIS FUNCTION HERE!!
GrowthTriangulation(vertexNew, triNetNew);
if (triNetNew.empty())
return -1;
// walk through triNetNew, dump each value elements, some dumps content is not correct
for (tItDump = triNetNew.begin(); tItDump != triNetNew.end(); tItDump++)
{
TRACE("/t/tTriangle Insert:/t(%f, %f, %f) /t-/t (%f, %f, %f) /t-/t (%f, %f, %f)/n",
tItDump->GetVertex(0)->GetX(), tItDump->GetVertex(0)->GetY, tItDump->GetVertex(0)->GetZ(),
tItDump->GetVertex(1)->GetX(), tItDump->GetVertex(1)->GetY, tItDump->GetVertex(1)->GetZ(),
tItDump->GetVertex(2)->GetX(), tItDump->GetVertex(2)->GetY, tItDump->GetVertex(2)->GetZ());
// insert each elements into triNet
triNet.insert(CTriangle(tItDump->GetVertex(0), tItDump->GetVertex(1), tItDump->GetVertex(2)));
}
}
return 0;
}
// 被调函数
void CTriNetInterp::GrowthTriangulation(vertexSet &vertex, triangleMSet & TriNet)
{
TRACE("CTriNetInterp::GrowthTriangulation/n");
edgeSet workESet;
vertexIterator vIt, vItDump;
CVertex vertexFirst[3];
CreateFirstTri(vertex, vertexFirst);
workESet.insert(CEdge(&vertexFirst[0], &vertexFirst[1])); // base edge I (begin -> third)
workESet.insert(CEdge(&vertexFirst[1], &vertexFirst[2])); // base edge II (third -> closest)
// insert one element here
TriNet.insert(CTriangle(&vertexFirst[0], &vertexFirst[1], &vertexFirst[2]));
edgeIterator eIt = workESet.begin(), eItDump;
triangleIterator tItDump;
// Go growth
TRACE("++/n/tBegin growth/n");
while (eIt != workESet.end())
{
double disMin = 1E20F;
vertexIterator vItClosest = vertex.begin();
//
// find the closest vertex to the base edge, and this vertex is not in
// any existed triangle's circumcircle, we stop searching when no more
// match vertex
//
......
TRACE("/t/tFind a closest vertex(%f, %f, %f) to current edge/n", vItClosest->GetX(), vItClosest->GetY(), vItClosest->GetZ());
workESet.insert(CEdge(eIt->m_pV0, &(*vItClosest)));
workESet.insert(CEdge(&(*vItClosest), eIt->m_pV1));
// insert new triangle here,找到了满足条件的顶点,就生成一个新的三角形
TriNet.insert(CTriangle(eIt->m_pV0, &(*vItClosest), eIt->m_pV1));
// dump triangle, all dump content is correct
TRACE("/t/t++/n/t/t/tDump trinet begin/n");
for (tItDump = TriNet.begin(); tItDump!=TriNet.end(); tItDump++)
{
TRACE("/t/t/t/tTriangle :/t(%f, %f, %f) /t-/t (%f, %f, %f) /t-/t (%f, %f, %f)/n",
tItDump->GetVertex(0)->GetX(), tItDump->GetVertex(0)->GetY(), tItDump->GetVertex(0)->GetZ(),
tItDump->GetVertex(1)->GetX(), tItDump->GetVertex(1)->GetY(), tItDump->GetVertex(1)->GetZ(),
tItDump->GetVertex(2)->GetX(), tItDump->GetVertex(2)->GetY(), tItDump->GetVertex(2)->GetZ());
}
TRACE("/t/t/tDump end/n/t/t--/n");
eIt++;
}
TRACE("/tEnd growth/n--/n");
}
// 下面是一些类的定义
//
// CTriangle
class CTriangle
{
public:
void SetCircumCircle();
CTriangle(const CTriangle& tri)
:m_Center(tri.m_Center)
,m_R(tri.m_R)
{
for (int i=0; i<3; i++)
m_pVertex[i] = tri.m_pVertex[i];
}
CTriangle(const CVertex *pV0, const CVertex *pV1, const CVertex *pV2)
{
m_pVertex[0] = pV0;
m_pVertex[1] = pV1;
m_pVertex[2] = pV2;
SetCircumCircle();
}
CTriangle(const CVertex *pV)
{
for (int i=0; i<3; i++)
m_pVertex[i] = pV++;
SetCircumCircle();
}
const CVertex* GetVertex(int i) const
{
ASSERT(i >=0 && i<3);
return m_pVertex[i];
}
BOOL operator< (const CTriangle& tri) const
{
return m_Center < tri.m_Center;
}
BOOL operator== (const CTriangle& tri) const
{
return !(m_Center == tri.m_Center);
}
//
// Return true if vIt is in the triangle's circumcircle
// Return false if vIt is out or on the circle
//
BOOL bVertexInCircumcircle(CVertex v) const
{
CVertex vetexCenter(m_Center);
CEdge deltaVector(&vetexCenter, &v);
return deltaVector.GetLength() < sqrt(pow(m_R,2));
}
protected:
CPointF m_Center; // center of circumcircle
double m_R; // redius of circumcircle
const CVertex *m_pVertex[3]; // counterclockwise
};
//
// CPoint
class CPointF
{
public:
CPointF() {X=0.0; Y=0.0; Z=0.0;}
CPointF(const CPointF& p) {X=p.X; Y=p.Y; Z=p.Z;}
CPointF(double dx, double dy, double dz){X=dx;Y=dy;Z=dz;}
CPointF operator+(const CPointF& p) const
{
return CPointF(X+p.X, Y+p.Y, Z+p.Z);
}
CPointF operator-(const CPointF& p) const
{
return CPointF(X-p.X, Y-p.Y, Z-p.Z);
}
//
// the order of x,y,z is very important, as follow define, the bigger
// one is in the top-right-front direction of smaller one
//
BOOL operator<(const CPointF& p) const
{
if (X == p.X && Y == p.Y)
return Z < p.Z;
else if (X == p.X)
return Y < p.Y;
else
return X < p.X;
}
BOOL operator==(const CPointF& p) const
{
return X==p.X && Y==p.Y && Z==p.Z;
}
double X;
double Y;
double Z;
};
//
// CVertex
class CVertex
{
public:
CVertex():m_point(0.0, 0.0, 0.0),m_bBeAssigned(FALSE) {}
CVertex(const CVertex& v):m_point(v.m_point),m_bBeAssigned(FALSE) {}
CVertex(const CPointF& p):m_point(p),m_bBeAssigned(FALSE) {}
CVertex(double dx, double dy, double dz):m_point(dx,dy,dz),m_bBeAssigned(FALSE) {}
CVertex(int nx, int ny, int nz):m_point((double)nx, (double)ny, (double)nz),m_bBeAssigned(FALSE){}
BOOL operator<(const CVertex &v) const
{
return m_point < v.m_point;
}
// Effect STL code 21
BOOL operator==(const CVertex &v) const
{
return !(m_point == v.m_point);
}
BOOL IsAssigned() const {return m_bBeAssigned;}
void MarkAssigned() {m_bBeAssigned = TRUE;}
double GetX() const {return m_point.X;}
double GetY() const {return m_point.Y;}
double GetZ() const {return m_point.Z;}
void SetX(double dx) {m_point.X = dx;}
void SetY(double dy) {m_point.Y = dy;}
void SetZ(double dz) {m_point.Z = dz;}
const CPointF& GetPoint() const {return m_point;}
protected:
CPointF m_point;
BOOL m_bBeAssigned;
};
//
// CEdge, m_pV0 -> m_pV1
class CEdge
{
public:
CEdge(const CEdge& e): m_pV0(e.m_pV0), m_pV1(e.m_pV1) {}
CEdge(const CVertex *pV0, const CVertex *pV1): m_pV0(pV0), m_pV1(pV1) {}
//
// large one is on the top-right-front direction of small one
//
BOOL operator<(const CEdge& e) const
{
if (m_pV0 == e.m_pV0)
return *m_pV1 < *e.m_pV1;
return *m_pV0 < *e.m_pV0;
}
// Effect STL code 21
BOOL operator==(const CEdge& e) const
{
return !(m_pV0->GetPoint() == e.m_pV0->GetPoint() && m_pV1->GetPoint() == e.m_pV1->GetPoint());
}
......
const CVertex *m_pV0;
const CVertex *m_pV1;
};
//
// CTriNetInterp
class CTriNetInterp
{
public:
int CreateTriNet(float **pfPole, int **pnPoleIndex, int nIndexNum, int *pnPoleNum, triangleMSet &triNet);
void GrowthTriangulation(vertexSet &vertex, triangleMSet &TriNet);
CTriNetInterp(){}
virtual ~CTriNetInterp(){}
};
typedef set<CVertex> vertexSet;
typedef set<CVertex>::iterator vertexIterator;
typedef set<CVertex>::const_iterator cvertexIterator;
typedef set<CEdge> edgeSet;
typedef set<CEdge>::iterator edgeIterator;
typedef set<CEdge>::const_iterator cedgeIterator;
typedef multiset<CTriangle> triangleMSet;
typedef multiset<CTriangle>::iterator triangleIterator;
typedef multiset<CTriangle>::const_iterator ctriangleIterator;
// 下面是打印的内容
// 在被调用函数里面的最后一次dump内容, 是对的
00001823 13:49:04.572 [800] ++
00001824 13:49:04.572 [800] Dump trinet begin
00001825 13:49:04.572 [800] Triangle : (-39.000000, -0.500000, 8.500000) - (-39.200001, -1.000000, 15.000000) - (-39.000000, 0.000000, 3.000000)
00001826 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 8.000000) - (-40.000000, -5.000000, 13.800000) - (-39.500000, -4.300000, 2.500000)
00001827 13:49:04.572 [800] Triangle : (-37.000000, -10.500000, 15.000000) - (-37.500000, 2.000000, 17.000000) - (-40.000000, -8.000000, 7.300000)
00001828 13:49:04.572 [800] Triangle : (-41.000000, -9.200000, 11.600000) - (-39.500000, -4.300000, 2.500000) - (-40.000000, -8.000000, 7.300000)
00001829 13:49:04.572 [800] Triangle : (-41.000000, -9.200000, 11.600000) - (-39.000000, 0.000000, 3.000000) - (-39.500000, -4.300000, 8.000000)
00001830 13:49:04.572 [800] Triangle : (-41.000000, -9.200000, 11.600000) - (-39.000000, -0.500000, 8.500000) - (-39.000000, 0.000000, 3.000000)
00001831 13:49:04.572 [800] Triangle : (-39.000000, 0.000000, 3.000000) - (-37.500000, 5.000000, 4.500000) - (-39.500000, -4.300000, 8.000000)
00001832 13:49:04.572 [800] Triangle : (-41.000000, -9.200000, 11.600000) - (-39.500000, -4.300000, 8.000000) - (-39.500000, -4.300000, 2.500000)
00001833 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 2.500000) - (-36.000000, -8.000000, 17.000000) - (-37.000000, -10.500000, 15.000000)
00001834 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 2.500000) - (-35.000000, -5.000000, 18.000000) - (-36.000000, -8.000000, 17.000000)
00001835 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 2.500000) - (-34.000000, -3.000000, 19.000000) - (-35.000000, -5.000000, 18.000000)
00001836 13:49:04.572 [800] Triangle : (-37.500000, 5.000000, 4.500000) - (-32.000000, 8.400000, 13.000000) - (-39.500000, -4.300000, 8.000000)
00001837 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 2.500000) - (-33.000000, -1.000000, 20.000000) - (-34.000000, -3.000000, 19.000000)
00001838 13:49:04.572 [800] Triangle : (-37.500000, 3.500000, 10.500000) - (-35.000000, 6.500000, 12.300000) - (-32.000000, 11.100000, 7.500000)
00001839 13:49:04.572 [800] Triangle : (-39.000000, 0.000000, 3.000000) - (-37.500000, 3.500000, 10.500000) - (-32.000000, 11.100000, 7.500000)
00001840 13:49:04.572 [800] Triangle : (-37.500000, 5.000000, 4.500000) - (-26.000000, 7.300000, 18.500000) - (-28.500000, 9.700000, 13.300000)
00001841 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 2.500000) - (-23.000000, 7.600000, 19.000000) - (-20.000000, 2.500000, 22.000000)
00001842 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 2.500000) - (-31.000000, 0.600000, 21.000000) - (-33.000000, -1.000000, 20.000000)
00001843 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 2.500000) - (-37.000000, -10.500000, 15.000000) - (-40.000000, -8.000000, 7.300000)
00001844 13:49:04.572 [800] Triangle : (-37.000000, -10.500000, 15.000000) - (-35.000000, 4.000000, 17.799999) - (-37.500000, 2.000000, 17.000000)
00001845 13:49:04.572 [800] Triangle : (-37.500000, 5.000000, 4.500000) - (-28.500000, 9.700000, 13.300000) - (-32.000000, 8.400000, 13.000000)
00001846 13:49:04.572 [800] Triangle : (-39.000000, 0.000000, 3.000000) - (-35.000000, 8.000000, 6.000000) - (-37.500000, 5.000000, 4.500000)
00001847 13:49:04.572 [800] Triangle : (-37.500000, 5.000000, 4.500000) - (-29.000000, 6.500000, 18.299999) - (-26.000000, 7.300000, 18.500000)
00001848 13:49:04.572 [800] Triangle : (-37.000000, -10.500000, 15.000000) - (-32.000000, 5.600000, 18.000000) - (-35.000000, 4.000000, 17.799999)
00001849 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 2.500000) - (-26.000000, 1.900000, 22.000000) - (-29.000000, 1.400000, 22.000000)
00001850 13:49:04.572 [800] Triangle : (-37.000000, -10.500000, 15.000000) - (-20.000000, 7.800000, 19.000000) - (-32.000000, 5.600000, 18.000000)
00001851 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 2.500000) - (-29.000000, 1.400000, 22.000000) - (-31.000000, 0.600000, 21.000000)
00001852 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 2.500000) - (-23.000000, 2.300000, 22.000000) - (-26.000000, 1.900000, 22.000000)
00001853 13:49:04.572 [800] Triangle : (-39.500000, -4.300000, 2.500000) - (-20.000000, 2.500000, 22.000000) - (-23.000000, 2.300000, 22.000000)
00001854 13:49:04.572 [800] Triangle : (-39.000000, 0.000000, 3.000000) - (-32.000000, 11.100000, 7.500000) - (-35.000000, 8.000000, 6.000000)
00001855 13:49:04.572 [800] Dump end
00001856 13:49:04.572 [800] --
00001857 13:49:04.572 [800] Find no more closet vertex
00001858 13:49:04.572 [800] End growth
00001859 13:49:04.572 [800] --
// 在调用函数里面dump的内容,完全是错的
00001860 13:49:04.572 [800] Triangle Insert: (-39.000000, 0.000000, -0.000000) - (0.000000, 15.000000, -39.000000) - (0.000000, -41536555050348815000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000, 0.000000)
00001861 13:49:04.587 [800] Triangle Insert: (-39.500000, 0.000000, 0.000000) - (0.000000, 13.800000, 0.000000) - (0.000000, -41536549821854824000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000, 0.000000)