mexopencv  3.4.1
MEX interface for OpenCV library
Subdiv2D_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/imgproc.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 // Persistent objects
14 namespace {
16 int last_id = 0;
19 
22  ("NextAroundOrg", cv::Subdiv2D::NEXT_AROUND_ORG)
23  ("NextAroundDst", cv::Subdiv2D::NEXT_AROUND_DST)
24  ("PrevAroundOrg", cv::Subdiv2D::PREV_AROUND_ORG)
25  ("PrevAroundDst", cv::Subdiv2D::PREV_AROUND_DST)
26  ("NextAroundLeft", cv::Subdiv2D::NEXT_AROUND_LEFT)
27  ("NextAroundRight", cv::Subdiv2D::NEXT_AROUND_RIGHT)
28  ("PrevAroundLeft", cv::Subdiv2D::PREV_AROUND_LEFT)
29  ("PrevAroundRight", cv::Subdiv2D::PREV_AROUND_RIGHT);
30 
33  (cv::Subdiv2D::PTLOC_ERROR, "Error")
34  (cv::Subdiv2D::PTLOC_OUTSIDE_RECT, "OutsideRect")
35  (cv::Subdiv2D::PTLOC_INSIDE, "Inside")
36  (cv::Subdiv2D::PTLOC_VERTEX, "Vertex")
37  (cv::Subdiv2D::PTLOC_ON_EDGE, "OnEdge");
38 }
39 
47 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
48 {
49  // Check the number of arguments
50  nargchk(nrhs>=2 && nlhs<=3);
51 
52  // Arguments vector
53  vector<MxArray> rhs(prhs, prhs+nrhs);
54  int id = rhs[0].toInt();
55  string method(rhs[1].toString());
56 
57  // Constructor is called. Create a new object from argument
58  if (method == "new") {
59  nargchk((nrhs==2 || nrhs==3) && nlhs<=1);
60  obj_[++last_id] = (nrhs == 3) ?
61  makePtr<Subdiv2D>(rhs[2].toRect()) : makePtr<Subdiv2D>();
62  plhs[0] = MxArray(last_id);
63  mexLock();
64  return;
65  }
66 
67  // Big operation switch
68  Ptr<Subdiv2D> obj = obj_[id];
69  if (obj.empty())
70  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
71  if (method == "delete") {
72  nargchk(nrhs==2 && nlhs==0);
73  obj_.erase(id);
74  mexUnlock();
75  }
76  else if (method == "initDelaunay") {
77  nargchk(nrhs==3 && nlhs==0);
78  obj->initDelaunay(rhs[2].toRect());
79  }
80  else if (method == "insert") {
81  nargchk(nrhs==3);
82  if (rhs[2].isNumeric() && rhs[2].numel() == 2) {
83  nargchk(nlhs<=1);
84  int curr_point = obj->insert(rhs[2].toPoint2f());
85  plhs[0] = MxArray(curr_point);
86  }
87  else {
88  nargchk(nlhs==0);
89  obj->insert(rhs[2].toVector<Point2f>());
90  }
91  }
92  else if (method == "locate") {
93  nargchk(nrhs==3 && nlhs<=3);
94  Point2f pt(rhs[2].toPoint2f());
95  int edge = 0, vertex = 0;
96  int location = obj->locate(pt, edge, vertex);
97  plhs[0] = MxArray(PointLocationInvMap[location]);
98  if (nlhs > 1)
99  plhs[1] = MxArray(edge);
100  if (nlhs > 2)
101  plhs[2] = MxArray(vertex);
102  }
103  else if (method == "findNearest") {
104  nargchk(nrhs==3 && nlhs<=2);
105  Point2f pt(rhs[2].toPoint2f()), nearestPt;
106  int vertex = obj->findNearest(pt, (nlhs>1) ? &nearestPt : NULL);
107  plhs[0] = MxArray(vertex);
108  if (nlhs > 1)
109  plhs[1] = MxArray(nearestPt);
110  }
111  else if (method == "getEdgeList") {
112  nargchk(nrhs==2 && nlhs<=1);
113  vector<Vec4f> edgeList;
114  obj->getEdgeList(edgeList);
115  plhs[0] = MxArray(edgeList);
116  }
117  else if (method == "getLeadingEdgeList") {
118  nargchk(nrhs==2 && nlhs<=1);
119  vector<int> leadingEdgeList;
120  obj->getLeadingEdgeList(leadingEdgeList);
121  plhs[0] = MxArray(leadingEdgeList);
122  }
123  else if (method == "getTriangleList") {
124  nargchk(nrhs==2 && nlhs<=1);
125  vector<Vec6f> triangleList;
126  obj->getTriangleList(triangleList);
127  plhs[0] = MxArray(triangleList);
128  }
129  else if (method == "getVoronoiFacetList") {
130  nargchk(nrhs==3 && nlhs<=2);
131  vector<int> idx(rhs[2].toVector<int>());
132  vector<vector<Point2f> > facetList;
133  vector<Point2f> facetCenters;
134  obj->getVoronoiFacetList(idx, facetList, facetCenters);
135  plhs[0] = MxArray(facetList);
136  if (nlhs > 1)
137  plhs[1] = MxArray(facetCenters);
138  }
139  else if (method == "getVertex") {
140  nargchk(nrhs==3 && nlhs<=2);
141  int vertex = rhs[2].toInt();
142  int firstEdge = 0;
143  Point2f pt = obj->getVertex(vertex, (nlhs>1) ? &firstEdge : NULL);
144  plhs[0] = MxArray(pt);
145  if (nlhs > 1)
146  plhs[1] = MxArray(firstEdge);
147  }
148  else if (method == "getEdge") {
149  nargchk(nrhs==4 && nlhs<=1);
150  int edge = rhs[2].toInt();
151  int nextEdgeType = EdgeTypeMap[rhs[3].toString()];
152  int e = obj->getEdge(edge, nextEdgeType);
153  plhs[0] = MxArray(e);
154  }
155  else if (method == "nextEdge") {
156  nargchk(nrhs==3 && nlhs<=1);
157  int edge = rhs[2].toInt();
158  int e = obj->nextEdge(edge);
159  plhs[0] = MxArray(e);
160  }
161  else if (method == "rotateEdge") {
162  nargchk(nrhs==4 && nlhs<=1);
163  int edge = rhs[2].toInt();
164  int rotate = rhs[3].toInt();
165  int e = obj->rotateEdge(edge, rotate);
166  plhs[0] = MxArray(e);
167  }
168  else if (method == "symEdge") {
169  nargchk(nrhs==3 && nlhs<=1);
170  int edge = rhs[2].toInt();
171  int e = obj->symEdge(edge);
172  plhs[0] = MxArray(e);
173  }
174  else if (method == "edgeOrg") {
175  nargchk(nrhs==3 && nlhs<=2);
176  int edge = rhs[2].toInt();
177  Point2f orgpt;
178  int e = obj->edgeOrg(edge, (nlhs>1) ? &orgpt : NULL);
179  plhs[0] = MxArray(e);
180  if (nlhs > 1)
181  plhs[1] = MxArray(orgpt);
182  }
183  else if (method == "edgeDst") {
184  nargchk(nrhs==3 && nlhs<=2);
185  int edge = rhs[2].toInt();
186  Point2f dstpt;
187  int e = obj->edgeDst(edge, (nlhs>1) ? &dstpt : NULL);
188  plhs[0] = MxArray(e);
189  if (nlhs > 1)
190  plhs[1] = MxArray(dstpt);
191  }
192  else
193  mexErrMsgIdAndTxt("mexopencv:error",
194  "Unrecognized operation %s", method.c_str());
195 }
void getLeadingEdgeList(std::vector< int > &leadingEdgeList) const
const ConstMap< int, string > PointLocationInvMap
inverse point location types for option processing
Definition: Subdiv2D_.cpp:32
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
int insert(Point2f pt)
STL namespace.
int rotateEdge(int edge, int rotate) const
int nextEdge(int edge) const
int symEdge(int edge) const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
int edgeDst(int edge, Point2f *dstpt=0) const
int last_id
Last object id to allocate.
Definition: Subdiv2D_.cpp:16
const ConstMap< string, int > EdgeTypeMap
edge types for option processing
Definition: Subdiv2D_.cpp:21
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: Subdiv2D_.cpp:47
int edgeOrg(int edge, Point2f *orgpt=0) const
STL class.
bool empty() const
Global constant definitions.
void getTriangleList(std::vector< Vec6f > &triangleList) const
int getEdge(int edge, int nextEdgeType) const
void getEdgeList(std::vector< Vec4f > &edgeList) const
T c_str(T... args)
T rotate(T... args)
int locate(Point2f pt, int &edge, int &vertex)
map< int, Ptr< Subdiv2D > > obj_
Object container.
Definition: Subdiv2D_.cpp:18
int findNearest(Point2f pt, Point2f *nearestPt=0)
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
void initDelaunay(Rect rect)
void getVoronoiFacetList(const std::vector< int > &idx, std::vector< std::vector< Point2f > > &facetList, std::vector< Point2f > &facetCenters)
Point2f getVertex(int vertex, int *firstEdge=0) const