mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-05-14 00:52:04 +00:00
BBS Port: Mesh Subdivision (#12150)
Ported from BBS You can now right-click a part and choose Subdivide Part to apply Loop subdivision with multiple iterations. This is useful for models with low original mesh resolution. > [!NOTE] > 1. Only meshes without non-manifold edges are supported. > 2. Color attributes will be lost after subdivision. We recommend subdividing first, then painting and applying colors. Not perfect and it can break some features but a nice to have and we can improve it. https://github.com/user-attachments/assets/33f10e49-f6dc-44d3-8c21-9e12e1fe23dc Best case scenario a sphere: Each picture is a Mesh subdivision step over the other <img width="541" height="495" alt="260202_164257_orca-slicer" src="https://github.com/user-attachments/assets/e62b3f4d-ee6b-4451-95a4-40a154d3a405" /> <img width="541" height="495" alt="260202_164302_%pn" src="https://github.com/user-attachments/assets/f7399457-be8d-45e7-b93f-f42064dadd64" /> <img width="541" height="495" alt="260202_164306_%pn" src="https://github.com/user-attachments/assets/55370035-219f-4b7f-94f4-9b31733820d6" /> <img width="541" height="495" alt="260202_164310_%pn" src="https://github.com/user-attachments/assets/3be8c904-cc6f-4efe-b4f8-f390b50d310c" />
This commit is contained in:
@@ -18,7 +18,7 @@ template <
|
||||
typename DerivedF,
|
||||
typename SType,
|
||||
typename DerivedNF>
|
||||
IGL_INLINE void igl::loop(
|
||||
IGL_INLINE bool igl::loop(
|
||||
const int n_verts,
|
||||
const Eigen::PlainObjectBase<DerivedF> & F,
|
||||
Eigen::SparseMatrix<SType>& S,
|
||||
@@ -26,15 +26,15 @@ IGL_INLINE void igl::loop(
|
||||
{
|
||||
typedef Eigen::SparseMatrix<SType> SparseMat;
|
||||
typedef Eigen::Triplet<SType> Triplet_t;
|
||||
|
||||
|
||||
//Ref. https://graphics.stanford.edu/~mdfisher/subdivision.html
|
||||
//Heavily borrowing from igl::upsample
|
||||
|
||||
|
||||
DerivedF FF, FFi;
|
||||
triangle_triangle_adjacency(F, FF, FFi);
|
||||
std::vector<std::vector<typename DerivedF::Scalar>> adjacencyList;
|
||||
adjacency_list(F, adjacencyList, true);
|
||||
|
||||
|
||||
//Compute the number and positions of the vertices to insert (on edges)
|
||||
Eigen::MatrixXi NI = Eigen::MatrixXi::Constant(FF.rows(), FF.cols(), -1);
|
||||
Eigen::MatrixXi NIdoubles = Eigen::MatrixXi::Zero(FF.rows(), FF.cols());
|
||||
@@ -48,12 +48,18 @@ IGL_INLINE void igl::loop(
|
||||
{
|
||||
NI(i,j) = counter;
|
||||
NIdoubles(i,j) = 0;
|
||||
if (FF(i,j) != -1)
|
||||
if (FF(i,j) != -1)
|
||||
{
|
||||
//If it is not a boundary
|
||||
NI(FF(i,j), FFi(i,j)) = counter;
|
||||
NIdoubles(i,j) = 1;
|
||||
} else
|
||||
int adj_triangle = FF(i, j);
|
||||
int adj_edge = FFi(i, j);
|
||||
if (adj_triangle >= 0 && adj_triangle < NI.rows() && adj_edge >= 0 && adj_edge < NI.cols()) {
|
||||
NI(adj_triangle, adj_edge) = counter;
|
||||
NIdoubles(i, j) = 1;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else
|
||||
{
|
||||
//Mark boundary vertices for later
|
||||
vertIsOnBdry(F(i,j)) = 1;
|
||||
@@ -63,24 +69,24 @@ IGL_INLINE void igl::loop(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const int& n_odd = n_verts;
|
||||
const int& n_even = counter;
|
||||
const int n_newverts = n_odd + n_even;
|
||||
|
||||
|
||||
//Construct vertex positions
|
||||
std::vector<Triplet_t> tripletList;
|
||||
for(int i=0; i<n_odd; ++i)
|
||||
for(int i=0; i<n_odd; ++i)
|
||||
{
|
||||
//Old vertices
|
||||
const std::vector<int>& localAdjList = adjacencyList[i];
|
||||
if(vertIsOnBdry(i)==1)
|
||||
if(vertIsOnBdry(i)==1)
|
||||
{
|
||||
//Boundary vertex
|
||||
tripletList.emplace_back(i, localAdjList.front(), 1./8.);
|
||||
tripletList.emplace_back(i, localAdjList.back(), 1./8.);
|
||||
tripletList.emplace_back(i, i, 3./4.);
|
||||
} else
|
||||
} else
|
||||
{
|
||||
const int n = localAdjList.size();
|
||||
const SType dn = n;
|
||||
@@ -99,19 +105,19 @@ IGL_INLINE void igl::loop(
|
||||
tripletList.emplace_back(i, i, 1.-dn*beta);
|
||||
}
|
||||
}
|
||||
for(int i=0; i<FF.rows(); ++i)
|
||||
for(int i=0; i<FF.rows(); ++i)
|
||||
{
|
||||
//New vertices
|
||||
for(int j=0; j<3; ++j)
|
||||
for(int j=0; j<3; ++j)
|
||||
{
|
||||
if(NIdoubles(i,j)==0)
|
||||
if(NIdoubles(i,j)==0)
|
||||
{
|
||||
if(FF(i,j)==-1)
|
||||
if(FF(i,j)==-1)
|
||||
{
|
||||
//Boundary vertex
|
||||
tripletList.emplace_back(NI(i,j) + n_odd, F(i,j), 1./2.);
|
||||
tripletList.emplace_back(NI(i,j) + n_odd, F(i, (j+1)%3), 1./2.);
|
||||
} else
|
||||
} else
|
||||
{
|
||||
tripletList.emplace_back(NI(i,j) + n_odd, F(i,j), 3./8.);
|
||||
tripletList.emplace_back(NI(i,j) + n_odd, F(i, (j+1)%3), 3./8.);
|
||||
@@ -123,33 +129,34 @@ IGL_INLINE void igl::loop(
|
||||
}
|
||||
S.resize(n_newverts, n_verts);
|
||||
S.setFromTriplets(tripletList.begin(), tripletList.end());
|
||||
|
||||
|
||||
// Build the new topology (Every face is replaced by four)
|
||||
NF.resize(F.rows()*4, 3);
|
||||
for(int i=0; i<F.rows();++i)
|
||||
{
|
||||
Eigen::VectorXi VI(6);
|
||||
VI << F(i,0), F(i,1), F(i,2), NI(i,0) + n_odd, NI(i,1) + n_odd, NI(i,2) + n_odd;
|
||||
|
||||
|
||||
Eigen::VectorXi f0(3), f1(3), f2(3), f3(3);
|
||||
f0 << VI(0), VI(3), VI(5);
|
||||
f1 << VI(1), VI(4), VI(3);
|
||||
f2 << VI(3), VI(4), VI(5);
|
||||
f3 << VI(4), VI(2), VI(5);
|
||||
|
||||
|
||||
NF.row((i*4)+0) = f0;
|
||||
NF.row((i*4)+1) = f1;
|
||||
NF.row((i*4)+2) = f2;
|
||||
NF.row((i*4)+3) = f3;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <
|
||||
typename DerivedV,
|
||||
typename DerivedV,
|
||||
typename DerivedF,
|
||||
typename DerivedNV,
|
||||
typename DerivedNF>
|
||||
IGL_INLINE void igl::loop(
|
||||
IGL_INLINE bool igl::loop(
|
||||
const Eigen::PlainObjectBase<DerivedV>& V,
|
||||
const Eigen::PlainObjectBase<DerivedF>& F,
|
||||
Eigen::PlainObjectBase<DerivedNV>& NV,
|
||||
@@ -158,16 +165,19 @@ IGL_INLINE void igl::loop(
|
||||
{
|
||||
NV = V;
|
||||
NF = F;
|
||||
for(int i=0; i<number_of_subdivs; ++i)
|
||||
for(int i=0; i<number_of_subdivs; ++i)
|
||||
{
|
||||
DerivedNF tempF = NF;
|
||||
Eigen::SparseMatrix<typename DerivedV::Scalar> S;
|
||||
loop(NV.rows(), tempF, S, NF);
|
||||
if (!loop(NV.rows(), tempF, S, NF)) {
|
||||
return false;
|
||||
}
|
||||
// This .eval is super important
|
||||
NV = (S*NV).eval();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef IGL_STATIC_LIBRARY
|
||||
template void igl::loop<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, int);
|
||||
#endif
|
||||
#endif
|
||||
@@ -29,7 +29,7 @@ namespace igl
|
||||
typename DerivedF,
|
||||
typename SType,
|
||||
typename DerivedNF>
|
||||
IGL_INLINE void loop(
|
||||
IGL_INLINE bool loop(
|
||||
const int n_verts,
|
||||
const Eigen::PlainObjectBase<DerivedF> & F,
|
||||
Eigen::SparseMatrix<SType>& S,
|
||||
@@ -44,11 +44,11 @@ namespace igl
|
||||
// NV a matrix containing the new vertices
|
||||
// NF a matrix containing the new faces
|
||||
template <
|
||||
typename DerivedV,
|
||||
typename DerivedV,
|
||||
typename DerivedF,
|
||||
typename DerivedNV,
|
||||
typename DerivedNF>
|
||||
IGL_INLINE void loop(
|
||||
IGL_INLINE bool loop(
|
||||
const Eigen::PlainObjectBase<DerivedV>& V,
|
||||
const Eigen::PlainObjectBase<DerivedF>& F,
|
||||
Eigen::PlainObjectBase<DerivedNV>& NV,
|
||||
|
||||
Reference in New Issue
Block a user