00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <opencv/cv.h>
00022 #include <QPointF>
00023 #include <QRect>
00024 #include <QList>
00025
00026 #ifndef QVDELAUNAYTRIANGULATION_H
00027 #define QVDELAUNAYTRIANGULATION_H
00028
00029 #ifndef DOXYGEN_IGNORE_THIS
00030
00031
00032 class QVDelaunayTriangulation
00033 {
00034 private:
00035 QRect range;
00036 CvMemStorage* storage;
00037 CvSubdiv2D* subdiv;
00038 QList<QPointF> points;
00039
00040 public:
00041 QVDelaunayTriangulation(const QRect &range): range(range)
00042 {
00043 storage = cvCreateMemStorage(0);
00044 subdiv = cvCreateSubdiv2D(CV_SEQ_KIND_SUBDIV2D, sizeof(*subdiv), sizeof(CvSubdiv2DPoint), sizeof(CvQuadEdge2D), storage);
00045 cvInitSubdivDelaunay2D(subdiv, (CvRect){ range.left(), range.top(), range.right(), range.bottom() });
00046 }
00047
00048 void addPoint(const QPointF &point) { points << point; cvSubdivDelaunay2DInsert(subdiv, (CvPoint2D32f){ point.x(), point.y() }); }
00049 QList<QPointF> getPoints() const { return points; }
00050
00051 QHash<QPointF, QPointF> getLinks() const
00052 {
00053 const int minX = range.left(), minY = range.top(),
00054 maxX = range.right(), maxY = range.bottom();
00055
00056 CvSeqReader reader;
00057 QHash <QPointF, QPointF> correspondences;
00058 cvStartReadSeq((CvSeq *)subdiv->edges, &reader);
00059
00060 for(int i=0; i<subdiv->edges->total; i++)
00061 {
00062 CvQuadEdge2D * edge = (CvQuadEdge2D *) reader.ptr;
00063 if(CV_IS_SET_ELEM(edge))
00064 {
00065 CvSubdiv2DPoint * org = cvSubdiv2DEdgeOrg( (CvSubdiv2DEdge) edge );
00066 CvSubdiv2DPoint * dst = cvSubdiv2DEdgeDst( (CvSubdiv2DEdge) edge );
00067 if(org && dst && (CV_IS_SET_ELEM(org) && ((org)->flags & CV_SUBDIV2D_VIRTUAL_POINT_FLAG)==0))
00068 if ( (org->pt.x > minX) && (org->pt.x < maxX) && (dst->pt.y > minY) && (dst->pt.y < maxY) )
00069 correspondences.insertMulti(QPointF(org->pt.x, org->pt.y), QPointF(dst->pt.x, dst->pt.y));
00070 }
00071 CV_NEXT_SEQ_ELEM(subdiv->edges->elem_size, reader);
00072 }
00073
00074 return correspondences;
00075 }
00076 };
00077 #endif // DOXIGEN_IGNORE_THIS
00078 #endif // QVDELAUNAYTRIANGULATION_H