Skip to content

Commit

Permalink
Merge pull request #169 from imadhammani/main
Browse files Browse the repository at this point in the history
XCore intersectMesh: update (OK EPFL case)
  • Loading branch information
vincentcasseau authored Sep 5, 2024
2 parents 9c655dd + e9ddd1e commit cdfa0d6
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 42 deletions.
12 changes: 12 additions & 0 deletions Cassiopee/XCore/XCore/PyTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,14 @@ def prepareMeshesForIntersection(master, slave):
tm = C.newPyTree(["M_adapted", zmo])
ts = C.newPyTree(["S_adapted", zso])

'''
try: import Intersector.PyTree as XOR
except: raise ImportError("XCore.PyTree: requires Intersector.PyTree module.")
print("Closing meshes...", flush=True)
ts = XOR.closeCells(ts)
tm = XOR.closeCells(tm)
'''

return tm, ts

Expand Down Expand Up @@ -387,3 +389,13 @@ def intersectMesh(master, slave):
ts = XOR.closeCells(ts)

return tm, ts

def extractCell(a, cid):
z = I.getZones(a)[0]

m = C.getFields(I.__GridCoordinates__, z, api=3)[0]

zmo = I.createZoneNode("cell"+str(cid), xcore.extractCell(m, cid))

return zmo

25 changes: 9 additions & 16 deletions Cassiopee/XCore/XCore/intersectMesh/dcel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ void Dcel::init_hedges_and_faces(Smesh &M, E_Int color)
Vertex *P = xit->key;

Hedge *h = new Hedge(P);
h->eid = i;

list[p].push_back(h);

Expand All @@ -367,6 +368,7 @@ void Dcel::init_hedges_and_faces(Smesh &M, E_Int color)
Vertex *V = xit->key;

Hedge *t = new Hedge(V);
t->eid = i;

list[q].push_back(t);

Expand All @@ -392,7 +394,7 @@ void Dcel::init_hedges_and_faces(Smesh &M, E_Int color)
const E_Float *N = &pnormals[3*pid];
assert(Sign(K_MATH::norm(N, 3)-1) == 0);

sort_leaving_hedges(hedges, N);
sort_leaving_hedges(hedges, N, M);

for (size_t i = 0; i < hedges.size(); i++) {
Hedge *h = hedges[i];
Expand All @@ -419,23 +421,10 @@ void Dcel::init_hedges_and_faces(Smesh &M, E_Int color)

Face *f = new Face;
f->oid[color] = i;
//f->oid[color] = M.l2gf.at(i);


assert(M.E2F[first_edge][0] == (E_Int)i || M.E2F[first_edge][1] == E_Int(i));
Hedge *REP = (M.E2F[first_edge][0] == (E_Int)i) ? h : t;

/*
if (REP->left != NULL) {
Vertex *v = REP->orig;
point_write("point", v->x, v->y, v->z);
hedge_write("hedge", REP);
hedge_write("twin", REP->twin);
hedge_write("next", REP->next);
hedge_write("prev", REP->prev);
}
*/

assert(REP->left == NULL);

f->rep = REP;
Expand Down Expand Up @@ -1141,7 +1130,9 @@ void Dcel::find_intersections_3D(const Smesh &M, const Smesh &S)
}
}

void Dcel::sort_leaving_hedges(std::vector<Hedge *> &leaving, const E_Float N[3]) const
void Dcel::sort_leaving_hedges(std::vector<Hedge *> &leaving,
const E_Float N[3],
const Smesh &M) const
{
// Choose a vector that is not parallel to N

Expand Down Expand Up @@ -1221,7 +1212,9 @@ void Dcel::sort_leaving_hedges(std::vector<Hedge *> &leaving, const E_Float N[3]
else {
Hedge *h = leaving[i];
Hedge *w = leaving[j];

assert(h->color != w->color);

Vertex *P = h->orig;
Vertex *Q = h->twin->orig;
if (cmp_vtx(P, Q) < 0) return true;
Expand Down Expand Up @@ -1370,7 +1363,7 @@ void Dcel::resolve_hedges(const Smesh &M, const Smesh &S)
E_Float NORM = K_MATH::norm(N, 3);
assert(Sign(NORM -1) == 0);

sort_leaving_hedges(leaving, N);
sort_leaving_hedges(leaving, N, M);

for (size_t i = 0; i < leaving.size(); i++) {
Hedge *h = leaving[i];
Expand Down
3 changes: 2 additions & 1 deletion Cassiopee/XCore/XCore/intersectMesh/dcel.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,6 @@ struct Dcel {

void trace_hedge(Hedge *sh, const Smesh &M, const Smesh &S, E_Int hid);

void sort_leaving_hedges(std::vector<Hedge *> &leaving, const E_Float N[3]) const;
void sort_leaving_hedges(std::vector<Hedge *> &leaving, const E_Float N[3],
const Smesh &M) const;
};
94 changes: 94 additions & 0 deletions Cassiopee/XCore/XCore/intersectMesh/extract.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "mesh.h"
#include "karray.h"

PyObject *K_XCORE::extractCell(PyObject *self, PyObject *args)
{
PyObject *MESH;
E_Int cid;

if (!PYPARSETUPLE_(args, O_ I_, &MESH, &cid)) {
RAISE("Bad input.");
return NULL;
}

Karray marray;

E_Int ret;

ret = Karray_parse_ngon(MESH, marray);

if (ret != 0) return NULL;

IMesh M(*marray.cn, marray.X, marray.Y, marray.Z, marray.npts);

IMesh M_out;

auto &C = M_out.C;
auto &F = M_out.F;
auto &X = M_out.X;
auto &Y = M_out.Y;
auto &Z = M_out.Z;

E_Int NC = 1;

E_Int NF = M.C[cid].size();

std::vector<E_Int> cell(NF);

for (E_Int i = 0; i < NF; i++) cell[i] = i;

C.push_back(cell);

for (E_Int fid : M.C[cid]) {
F.push_back(M.F[fid]);
}

std::map<E_Int, E_Int> new_pids;

E_Int NP = 0;

for (auto &pn : F) {
for (E_Int &p : pn) {
auto it = new_pids.find(p);

if (it == new_pids.end()) {
new_pids[p] = NP;
p = NP;
NP++;
} else {
p = it->second;
}
}
}

X.resize(NP);
Y.resize(NP);
Z.resize(NP);

for (const auto &pdat : new_pids) {
E_Int opid = pdat.first;
E_Int npid = pdat.second;
X[npid] = M.X[opid];
Y[npid] = M.Y[opid];
Z[npid] = M.Z[opid];
}

M_out.nc = NC;
M_out.nf = NF;
M_out.np = NP;

/*
for (auto &cell : M_out.C) {
for (auto f : cell) printf("%d ", f);
puts("");
}
for (auto &face : M_out.F) {
for (auto f : cell) printf("%d ", f);
puts("");
}
*/


return M_out.export_karray();
}
2 changes: 2 additions & 0 deletions Cassiopee/XCore/XCore/intersectMesh/hedge.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ struct Hedge {
E_Int color;
Cycle *cycle;

E_Int eid;

// Projection of orig
E_Float proj_ox = -10000;
E_Float proj_oy = -10000;
Expand Down
12 changes: 7 additions & 5 deletions Cassiopee/XCore/XCore/intersectMesh/intersectMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ PyObject *K_XCORE::intersectMesh(PyObject *self, PyObject *args)
IMesh S(*sarray.cn, sarray.X, sarray.Y, sarray.Z, sarray.npts);

M.make_skin();
S.make_skin();

M.orient_skin(OUT);
S.orient_skin(IN);

assert(M.patch.empty());
for (E_Int fid : M.skin) M.patch.insert(fid);

Expand All @@ -334,15 +339,12 @@ PyObject *K_XCORE::intersectMesh(PyObject *self, PyObject *args)

for (E_Int i = 0; i < spatch_size; i++) S.patch.insert(spatch[i]-1);

M.orient_skin(OUT);
S.orient_skin(IN);

// Extract surface meshes
Smesh Mf(M);
Smesh Sf(S);

//Mf.write_ngon("Mf");
//Sf.write_ngon("Sf");
Mf.write_ngon("Mf");
Sf.write_ngon("Sf");

puts("Making point edges...");
Mf.make_point_edges();
Expand Down
42 changes: 42 additions & 0 deletions Cassiopee/XCore/XCore/intersectMesh/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,37 @@ E_Int IMesh::face_contains_point(E_Int face, E_Float x, E_Float y, E_Float z) co
return hit;
}

void IMesh::extract_edge_points(E_Int a, E_Int b, std::list<E_Int> &points)
{
E_Int ref = 0;

points.clear();
points.push_back(a);
points.push_back(b);

do {
ref = 0;

auto pos = points.begin();

assert(*std::prev(points.end()) == b);

for (auto it = points.begin(); it != std::prev(points.end()); it++) {
E_Int a = *it;
E_Int b = *std::next(it);

UEdge e(a, b);

auto search = ecenter.find(e);

if (search != ecenter.end()) {
points.insert(std::next(it), search->second);
ref = 1;
}
}
} while (ref);
}

IMesh IMesh::extract_conformized()
{
// Keep all the points
Expand All @@ -880,6 +911,16 @@ IMesh IMesh::extract_conformized()
E_Int p = pn[j];
E_Int q = pn[(j+1)%pn.size()];

std::list<E_Int> epoints;

extract_edge_points(p, q, epoints);

epoints.pop_back();

for (auto it = epoints.begin(); it != epoints.end(); it++)
new_face.push_back(*it);

/*
UEdge e(p, q);
auto it = ecenter.find(e);
Expand All @@ -890,6 +931,7 @@ IMesh IMesh::extract_conformized()
} else {
new_face.push_back(p);
}
*/
}

new_nf++;
Expand Down
3 changes: 3 additions & 0 deletions Cassiopee/XCore/XCore/intersectMesh/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <array>
#include <map>
#include <set>
#include <list>

#include "point.h"
#include "xcore.h"
Expand Down Expand Up @@ -112,6 +113,8 @@ struct IMesh {

void make_edges();

void extract_edge_points(E_Int a, E_Int b, std::list<E_Int> &pts);

inline bool face_is_quad(E_Int face) const { return F[face].size() == 4; }

inline bool face_is_tri(E_Int face) const { return F[face].size() == 3; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ PyObject *K_XCORE::prepareMeshesForIntersection(PyObject *self, PyObject *args)

puts("Preparing meshes for intersection...");

// Init and orient master/slave meshes
// Init master/slave meshes
IMesh M(*marray.cn, marray.X, marray.Y, marray.Z, marray.npts);
IMesh S(*sarray.cn, sarray.X, sarray.Y, sarray.Z, sarray.npts);

Expand All @@ -83,11 +83,6 @@ PyObject *K_XCORE::prepareMeshesForIntersection(PyObject *self, PyObject *args)
//for (E_Int i = 0; i < S.nf; i++) {
const auto &pn = S.F[fid];
size_t stride = pn.size();
if (stride > 4) {
std::vector<Point> points;
for (auto p : pn) points.push_back(Point(S.X[p], S.Y[p], S.Z[p]));
point_write("face", points);
}
assert(stride == 3 || stride == 4);

E_Int keep = 1;
Expand Down
Loading

0 comments on commit cdfa0d6

Please sign in to comment.