Skip to content

Commit

Permalink
Integration of new code of mesh arrangement
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleFaedda committed May 21, 2024
1 parent 67d77b4 commit c03e6a1
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 55 deletions.
108 changes: 108 additions & 0 deletions arrangements/code/custom_stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// Created by Gianmarco Cherchi on 07/03/24.
//

#ifndef MESH_ARRANGEMENT_CUSTOM_STACK_H
#define MESH_ARRANGEMENT_CUSTOM_STACK_H

#include <vector>
#include <cassert>
#include <aux_structure.h>

class CustomStack
{
public:
CustomStack(int preallocate_size)
{
stack.resize(preallocate_size);
cursor = -1;
}

auxvector<uint>& pop()
{
cursor -= 1;
return stack.at(cursor +1);
}

void push(auxvector<uint> new_vec)
{
if(cursor == stack.size() -1)
stack.push_back(new_vec);
else
stack[cursor +1] = new_vec;

cursor++;
}

bool empty()
{
return cursor == -1;
}

const std::vector<auxvector<uint>> &getStack(int &size)
{
size = stack.size();
return stack;
}

auxvector<uint> &getSingleVector(int index)
{
assert(index <= cursor && "Index out of range");
return stack[index];
}

void clearSingleVector(int index)
{
assert(index <= cursor && "Index out of range");
stack.at(index).clear();
}

int findTriplet(uint v0, uint v1, uint v2)
{
for(int i = cursor; i >= 0; --i)
{
assert(stack[i].size() >= 3 && "Empty element in the queue");

if ((stack[i][0] == v0 && stack[i][1] == v1 && stack[i][2] == v2) ||
(stack[i][0] == v0 && stack[i][1] == v2 && stack[i][2] == v1) ||
(stack[i][0] == v1 && stack[i][1] == v0 && stack[i][2] == v2) ||
(stack[i][0] == v1 && stack[i][1] == v2 && stack[i][2] == v0) ||
(stack[i][0] == v2 && stack[i][1] == v0 && stack[i][2] == v1) ||
(stack[i][0] == v2 && stack[i][1] == v1 && stack[i][2] == v0) ){
return i;
}
}

assert(false && "Triplet not found!");
return -1; // Triplet not found
}

const auxvector<uint>& getTriangleFromStack(uint v0, uint v1, uint v2)
{
for(int i = cursor; i >= 0; --i)
{
assert(stack[i].size() >= 3 && "Empty element in the queue");

if ((stack[i][0] == v0 && stack[i][1] == v1 && stack[i][2] == v2) ||
(stack[i][0] == v0 && stack[i][1] == v2 && stack[i][2] == v1) ||
(stack[i][0] == v2 && stack[i][1] == v1 && stack[i][2] == v0) ||
(stack[i][0] == v1 && stack[i][1] == v0 && stack[i][2] == v2) ||
(stack[i][0] == v1 && stack[i][1] == v2 && stack[i][2] == v0) ||
(stack[i][0] == v2 && stack[i][1] == v0 && stack[i][2] == v1)){
return stack[i];
}
}

assert(false && "Triplet not found!");
return auxvector<uint>(); // Triplet not found
}



private:
std::vector<auxvector<uint>> stack;
int cursor;
};


#endif //MESH_ARRANGEMENT_CUSTOM_STACK_H
15 changes: 9 additions & 6 deletions arrangements/code/fast_trimesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,12 @@ inline uint FastTrimesh::vertOrigID(uint new_v_id) const

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

inline uint FastTrimesh::vertNewID(uint orig_v_id) const
inline uint FastTrimesh::vertNewID(uint orig_v_id) const
{
auto it = rev_vtx_map.find(orig_v_id);
assert(it != rev_vtx_map.end() && "vtx id not found in reverse map");
if(it == rev_vtx_map.end())
return -1;

return it->second;
}
Expand Down Expand Up @@ -710,8 +712,9 @@ inline void FastTrimesh::splitEdge(const uint &e_id, uint v_id)
uint ev0_id = edges[e_id].v.first;
uint ev1_id = edges[e_id].v.second;

for(uint t_id : e2t[e_id])
for(int i=0; i < e2t[e_id].size(); i++)
{
uint t_id = e2t[e_id][i];
uint v_opp = triVertOppositeTo(t_id, ev0_id, ev1_id);
if(triVertsAreCCW(t_id, ev0_id, ev1_id)) std::swap(ev0_id, ev1_id);

Expand Down Expand Up @@ -872,10 +875,10 @@ inline void FastTrimesh::edgeSwitch(uint e0_id, const uint e1_id)
std::swap(edges[e0_id], edges[e1_id]);
std::swap(e2t[e0_id], e2t[e1_id]);

std::array<uint, 4> verts_to_update{edges[e0_id].v.first,
edges[e0_id].v.second,
edges[e1_id].v.first,
edges[e1_id].v.second};
std::array<uint, 4> verts_to_update{edges[e0_id].v.first,
edges[e0_id].v.second,
edges[e1_id].v.first,
edges[e1_id].v.second};
auto len = (int)remove_duplicates(verts_to_update);

for(int i = 0; i < len; i++)
Expand Down
28 changes: 23 additions & 5 deletions arrangements/code/intersection_classification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ inline void find_intersections(const std::vector<cinolib::vec3d> & verts, const
intersections.reserve((int)sqrt(tris.size()));
tbb::spin_mutex mutex;
tbb::parallel_for((uint)0, (uint)o.leaves.size(), [&](uint i)
{
{
auto & leaf = o.leaves.at(i);
if(leaf->item_indices.empty()) return;
for(uint j=0; j<leaf->item_indices.size()-1; ++j)
Expand Down Expand Up @@ -419,7 +419,10 @@ inline void checkSingleCoplanarEdgeIntersections(TriangleSoup &ts, point_arena&


// e_v0 position
cinolib::PointInSimplex v0_inters = cinolib::point_in_triangle_3d(ts.vertPtr(e_v0), ts.triVertPtr(o_t_id, 0), ts.triVertPtr(o_t_id, 1), ts.triVertPtr(o_t_id, 2));
cinolib::PointInSimplex v0_inters = cinolib::point_in_triangle_3d(ts.vertPtr(e_v0),
ts.triVertPtr(o_t_id, 0),
ts.triVertPtr(o_t_id, 1),
ts.triVertPtr(o_t_id, 2));
if(v0_inters == cinolib::ON_VERT0 || v0_inters == cinolib::ON_VERT1 || v0_inters == cinolib::ON_VERT2)
{
v0_in_vtx = true; // v0 in a vertex
Expand Down Expand Up @@ -556,7 +559,12 @@ inline void checkSingleCoplanarEdgeIntersections(TriangleSoup &ts, point_arena&
else if(tv2_in_edge)
{
addSymbolicSegment(ts,ts.triVertID(o_t_id, 2), static_cast<uint>(seg0_cross), o_t_id, e_t_id, g);
il.insert(ts.triVertID(o_t_id, 2));
uint v_id = ts.triVertID(o_t_id, 2);
int edge_id = ts.edgeID(e_v0, e_v1);
assert(edge_id != -1 && "edge not found!");

il.insert(v_id);
g.addVertexInEdge(edge_id, v_id);
return;
}
}
Expand All @@ -582,7 +590,12 @@ inline void checkSingleCoplanarEdgeIntersections(TriangleSoup &ts, point_arena&
else if(tv0_in_edge)
{
addSymbolicSegment(ts, ts.triVertID(o_t_id, 0), static_cast<uint>(seg1_cross), o_t_id, e_t_id, g);
il.insert(ts.triVertID(o_t_id, 0));
uint v_id = ts.triVertID(o_t_id, 0);
int edge_id = ts.edgeID(e_v0, e_v1);
assert(edge_id != 0 && "edge not foubd!");

il.insert(v_id);
g.addVertexInEdge(edge_id, v_id);
return;
}
}
Expand All @@ -608,7 +621,12 @@ inline void checkSingleCoplanarEdgeIntersections(TriangleSoup &ts, point_arena&
else if(tv1_in_edge)
{
addSymbolicSegment(ts, ts.triVertID(o_t_id, 1), static_cast<uint>(seg2_cross), o_t_id, e_t_id, g);
il.insert(ts.triVertID(o_t_id, 1));
uint v_id = ts.triVertID(o_t_id, 1);
int edge_id = ts.edgeID(e_v0, e_v1);
assert(edge_id != 0 && "edge not found!");

il.insert(v_id);
g.addVertexInEdge(edge_id, v_id);
return;
}
}
Expand Down
25 changes: 3 additions & 22 deletions arrangements/code/io_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@
* *
* ***************************************************************************************/


#include "io_functions.h"

#include <cinolib/octree.h>

extern int vert_offset;

inline void load(const std::string &filename, std::vector<double> &coords, std::vector<uint> &tris)
{
std::vector<cinolib::vec3d> tmp_verts;
Expand Down Expand Up @@ -72,28 +75,6 @@ inline void load(const std::string &filename, std::vector<double> &coords, std::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

inline void loadMultipleFiles(const std::vector<std::string> &files, std::vector<double> &coords, std::vector<uint> &tris, std::vector<uint> &labels)
{
for(uint f_id = 0; f_id < files.size(); f_id++)
{
std::vector<double> tmp_coords;
std::vector<uint> tmp_tris;

load(files[f_id], tmp_coords, tmp_tris);

uint off = static_cast<uint>(coords.size() / 3); // prev num verts

coords.insert(coords.end(), tmp_coords.begin(), tmp_coords.end());

for(auto &i : tmp_tris) tris.push_back(i + off);

for(uint i = 0; i < tmp_tris.size() / 3; i++)
labels.push_back(f_id);
}
}

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

inline void loadMultipleFiles(const std::vector<std::string> &files, std::vector<double> &coords, std::vector<uint> &tris, std::vector<uint> &labels, int &vert_offset)
{
for(uint f_id = 0; f_id < files.size(); f_id++)
{
Expand Down
6 changes: 4 additions & 2 deletions arrangements/code/solve_intersections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
inline void meshArrangementPipeline(const std::vector<double> &in_coords, const std::vector<uint> &in_tris, const std::vector< std::bitset<NBIT> > &in_labels, point_arena &arena,
std::vector<genericPoint*> &vertices, std::vector<uint> &out_tris, std::vector< std::bitset<NBIT> > &out_labels)
{
bool parallel_value = true;

initFPU();

AuxiliaryStructure g;
Expand All @@ -49,11 +51,11 @@ inline void meshArrangementPipeline(const std::vector<double> &in_coords, const
std::vector<uint> tmp_tris;
std::vector< std::bitset<NBIT> > tmp_labels;

mergeDuplicatedVertices(in_coords, in_tris, arena, vertices, tmp_tris, true);
mergeDuplicatedVertices(in_coords, in_tris, arena, vertices, tmp_tris, parallel_value);

removeDegenerateAndDuplicatedTriangles(vertices, in_labels, tmp_tris, tmp_labels);

TriangleSoup ts(arena, vertices, tmp_tris, tmp_labels, multiplier, true);
TriangleSoup ts(arena, vertices, tmp_tris, tmp_labels, multiplier, parallel_value);

detectIntersections(ts, g.intersectionList());

Expand Down
Loading

0 comments on commit c03e6a1

Please sign in to comment.