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