Last active
March 8, 2023 16:56
-
-
Save nervoussystem/2dfdcd4d0c5b6b89b4b80bb4bd0d29d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void meshOutlines(const vector<vector<array<float, 2> > > & crvs,vector<array<float, 2> > & outPts, vector<unsigned int> & indices) { | |
GEO::CDT2d cdt; | |
if (crvs.size() == 0) return; | |
//get bounding box | |
float minX = crvs[0][0][0], maxX = crvs[0][0][0], minY = crvs[0][0][1], maxY = crvs[0][0][1]; | |
vector<double> pts; | |
for (int c = 0; c < crvs.size();++c) { | |
auto & crv = crvs[c]; | |
float area = 0; | |
for (int i = 0; i < crv.size(); ++i) { | |
pts.push_back(crv[i][0]); | |
pts.push_back(crv[i][1]); | |
minX = min(crv[i][0], minX); | |
maxX = max(crv[i][0], maxX); | |
minY = min(crv[i][1], minY); | |
maxY = max(crv[i][1], maxY); | |
} | |
} | |
cdt.create_enclosing_rectangle(minX-10, minY-10, maxX+10, maxY+10); | |
vector<GEO::index_t> newIndices(pts.size() / 2); | |
cdt.insert(pts.size()/2, pts.data(),newIndices.data()); | |
int currIndex = 0; | |
for (auto & crv : crvs) { | |
for (int i = 0; i < crv.size(); ++i) { | |
cdt.insert_constraint(newIndices[i + currIndex], newIndices[(i + 1) % crv.size() + currIndex]); | |
} | |
currIndex += crv.size(); | |
} | |
cdt.remove_external_triangles(); | |
cdt.remove_holes(); | |
auto numTriangles = cdt.nT(); | |
for (int i = 0; i < numTriangles; ++i) { | |
int index1 = cdt.Tv(i, 0)-4; | |
int index2 = cdt.Tv(i, 1)-4; | |
int index3 = cdt.Tv(i, 2)-4; | |
indices.push_back(index1); | |
indices.push_back(index2); | |
indices.push_back(index3); | |
} | |
for (int i = 4; i < cdt.nv(); ++i) { | |
auto p = cdt.point(i); | |
outPts.push_back({p.x, p.y}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment