#define CGAL_USE_BASIC_VIEWER #include //#include //#include //#include #include #include #include #include #include #include #include #include "mex.hpp" #include "mexAdapter.hpp" #include "MatlabDataArray.hpp" //#include typedef CGAL::Exact_predicates_exact_constructions_kernel K; typedef K::Point_2 Point2; typedef CGAL::Polygon_2 Polygon2; class MexFunction : public matlab::mex::Function { public: void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) { checkArguments(outputs, inputs); matlab::data::TypedArray MMCs = std::move(inputs[0]); matlab::data::CellArray polycell = std::move(inputs[1]); size_t nmmc = MMCs.getDimensions()[0]; int npgn = polycell.getDimensions()[0]; //std::cout << nmmc <<","<< npgn << std::endl; matlab::data::ArrayFactory factory; matlab::data::CellArray interP = factory.createCellArray({ nmmc, 1 }); std::shared_ptr matlabPtr = getEngine(); for (int i = 0; i < nmmc; ++i) { Polygon2 pgon; for (int j = 0; j < 4; ++j) { double x = MMCs[i][2*j]; // MMCs: [nmmc, 4*2] double y = MMCs[i][2*j+1]; pgon.push_back(Point2(x, y)); } if (!pgon.is_simple()) matlabPtr->feval(u"error", 0, std::vector({ factory.createScalar("MMC polygon is not simple!") })); if (pgon.is_clockwise_oriented()) pgon.reverse_orientation(); std::vector interIdx; for (int j = 0; j < npgn; ++j) { matlab::data::TypedArray polyj = polycell[j]; Polygon2 bpoly; //std::cout << polyj.getDimensions()[0] << std::endl; for (int k = 0; k < polyj.getDimensions()[0]; ++k) { double x = polyj[k][0]; // polyj: [k, 2] double y = polyj[k][1]; bpoly.push_back(Point2(x, y)); } if (!bpoly.is_simple()) matlabPtr->feval(u"error", 0, std::vector({ factory.createScalar("Voronoi polygon is not simple!") })); if (bpoly.is_clockwise_oriented()) bpoly.reverse_orientation(); if (CGAL::do_intersect(pgon, bpoly)) interIdx.push_back(j+1); // matlab started with 1 } if (interIdx.size() < 1) { matlabPtr->feval(u"error", 0, std::vector({ factory.createScalar("None intersection!") })); } matlab::data::TypedArray interMat = factory.createArray({ interIdx.size(), 1 }); for (int j = 0; j < interIdx.size(); ++j) interMat[j] = interIdx[j]; interP[i] = interMat; } outputs[0] = interP; } void checkArguments(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) { std::shared_ptr matlabPtr = getEngine(); matlab::data::ArrayFactory factory; if (inputs.size() != 2) { matlabPtr->feval(u"error", 0, std::vector({ factory.createScalar("Two inputs required") })); } if (inputs[0].getDimensions()[1] != 8) { matlabPtr->feval(u"error", 0, std::vector({ factory.createScalar("MMCs input 1 must be NX8 dimension") })); } if (inputs[0].getType() != matlab::data::ArrayType::DOUBLE || inputs[0].getType() == matlab::data::ArrayType::COMPLEX_DOUBLE) { matlabPtr->feval(u"error", 0, std::vector({ factory.createScalar("MMCs Input 1 must be a noncomplex scalar double") })); } if (inputs[1].getType() != matlab::data::ArrayType::CELL) { matlabPtr->feval(u"error", 0, std::vector({ factory.createScalar("PolyCell Input 2 must be type CELL") })); } if (inputs[1].getDimensions()[1] != 1) { matlabPtr->feval(u"error", 0, std::vector({ factory.createScalar("Index Input 2 must be NX1 dimension") })); } //if (inputs[1][0].getDimensions()[1] != 2) { // matlabPtr->feval(u"error", 0, // std::vector({ factory.createScalar("Index Input 2 content must be NX2 dimension") })); // } if (outputs.size() > 1) { matlabPtr->feval(u"error", 0, std::vector({ factory.createScalar("Only one output is returned") })); } } };