You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
4.5 KiB
140 lines
4.5 KiB
3 years ago
|
#define CGAL_USE_BASIC_VIEWER
|
||
|
|
||
|
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
|
||
|
//#include <CGAL/Gmpq.h>
|
||
|
//#include <CGAL/Lazy_exact_nt.h>
|
||
|
//#include <CGAL/Simple_cartesian.h>
|
||
|
|
||
|
#include <CGAL/Boolean_set_operations_2.h>
|
||
|
#include <CGAL/bounding_box.h>
|
||
|
#include <CGAL/Polygon_2.h>
|
||
|
|
||
|
#include <iostream>
|
||
|
#include <cstdint>
|
||
|
#include <fstream>
|
||
|
#include <iostream>
|
||
|
|
||
|
#include "mex.hpp"
|
||
|
#include "mexAdapter.hpp"
|
||
|
#include "MatlabDataArray.hpp"
|
||
|
//#include <omp.h>
|
||
|
|
||
|
typedef CGAL::Exact_predicates_exact_constructions_kernel K;
|
||
|
|
||
|
typedef K::Point_2 Point2;
|
||
|
typedef CGAL::Polygon_2<K> Polygon2;
|
||
|
|
||
|
class MexFunction : public matlab::mex::Function {
|
||
|
public:
|
||
|
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
|
||
|
|
||
|
checkArguments(outputs, inputs);
|
||
|
|
||
|
matlab::data::TypedArray<double> 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<matlab::engine::MATLABEngine> 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<matlab::data::Array>({ factory.createScalar("MMC polygon is not simple!") }));
|
||
|
if (pgon.is_clockwise_oriented())
|
||
|
pgon.reverse_orientation();
|
||
|
|
||
|
std::vector<int> interIdx;
|
||
|
|
||
|
for (int j = 0; j < npgn; ++j)
|
||
|
{
|
||
|
matlab::data::TypedArray<double> 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<matlab::data::Array>({ 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<matlab::data::Array>({ factory.createScalar("None intersection!") }));
|
||
|
}
|
||
|
|
||
|
matlab::data::TypedArray<int> interMat = factory.createArray<int>({ 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<matlab::engine::MATLABEngine> matlabPtr = getEngine();
|
||
|
matlab::data::ArrayFactory factory;
|
||
|
|
||
|
if (inputs.size() != 2) {
|
||
|
matlabPtr->feval(u"error",
|
||
|
0, std::vector<matlab::data::Array>({ factory.createScalar("Two inputs required") }));
|
||
|
}
|
||
|
|
||
|
if (inputs[0].getDimensions()[1] != 8) {
|
||
|
matlabPtr->feval(u"error",
|
||
|
0, std::vector<matlab::data::Array>({ 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<matlab::data::Array>({ 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<matlab::data::Array>({ factory.createScalar("PolyCell Input 2 must be type CELL") }));
|
||
|
}
|
||
|
|
||
|
if (inputs[1].getDimensions()[1] != 1) {
|
||
|
matlabPtr->feval(u"error",
|
||
|
0, std::vector<matlab::data::Array>({ factory.createScalar("Index Input 2 must be NX1 dimension") }));
|
||
|
}
|
||
|
|
||
|
//if (inputs[1][0].getDimensions()[1] != 2) {
|
||
|
// matlabPtr->feval(u"error", 0,
|
||
|
// std::vector<matlab::data::Array>({ factory.createScalar("Index Input 2 content must be NX2 dimension") }));
|
||
|
// }
|
||
|
|
||
|
if (outputs.size() > 1) {
|
||
|
matlabPtr->feval(u"error", 0,
|
||
|
std::vector<matlab::data::Array>({ factory.createScalar("Only one output is returned") }));
|
||
|
}
|
||
|
}
|
||
|
};
|