Skip to content

Commit 459a9bc

Browse files
authored
[src] Fix advanced carving logic with refinement (#58)
* backup work on advanced carving * simplify the refine vs carving process * fix commented code
1 parent ce2e46a commit 459a9bc

3 files changed

Lines changed: 64 additions & 49 deletions

File tree

src/InfinyToolkit/CarvingTools/BaseCarvingPerformer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class SOFA_INFINYTOOLKIT_API BaseCarvingPerformer
5555

5656
virtual ~BaseCarvingPerformer();
5757

58-
void clearContacts();
58+
virtual void clearContacts();
5959

6060
const std::string& getType() { return m_performerType; }
6161

src/InfinyToolkit/CarvingTools/RefineCarvingPerformer.cpp

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ RefineCarvingPerformer::RefineCarvingPerformer(TetrahedronSetTopologyContainer::
3030
, m_tetraAlgo(nullptr)
3131
{
3232
m_performerType = "RefineCarvingPerformer";
33+
m_doRefine = true;
3334
}
3435

3536

@@ -51,6 +52,14 @@ bool RefineCarvingPerformer::initPerformer()
5152
return resInit;
5253
}
5354

55+
void RefineCarvingPerformer::clearContacts()
56+
{
57+
BaseCarvingPerformer::clearContacts();
58+
m_tetra2Filter.clear();
59+
m_carvingPositions.clear();
60+
m_triIdsToFilter.clear();
61+
}
62+
5463

5564
void RefineCarvingPerformer::filterContacts()
5665
{
@@ -60,9 +69,11 @@ void RefineCarvingPerformer::filterContacts()
6069
const SReal& refineDistance = m_carvingMgr->d_refineDistance.getValue();
6170
const SReal& carvingDistance = m_carvingMgr->d_carvingDistance.getValue();
6271
m_tetra2Filter.clear();
63-
//m_tetra2Filter2.clear();
6472
m_triIdsToFilter.clear();
73+
m_carvingPositions.clear();
74+
m_tetra2Carv.clear();
6575

76+
// If triangle collision involved (only surface) take tetra behind the triangle, if close enough, for refinement
6677
for (contactInfo * cInfo : m_triangleContacts)
6778
{
6879
if (cInfo->dist > refineDistance)
@@ -76,15 +87,17 @@ void RefineCarvingPerformer::filterContacts()
7687
continue;
7788
}
7889

90+
// Add the tetra behind the triangle to the list of tetra to refine
7991
m_tetra2Filter.insert(tetraAT[0]);
92+
m_carvingPositions.push_back(cInfo->pointA);
8093

81-
//if (cInfo->dist <= carvingDistance)
82-
// m_tetra2Filter2.push_back(tetraAT[0]);
83-
84-
85-
carvingPosition = cInfo->pointA;
94+
// Add the tetra behind the triangle to the list of tetra to carve if close enough
95+
if (cInfo->dist <= carvingDistance) {
96+
m_tetra2Carv.insert(tetraAT[0]);
97+
}
8698
}
8799

100+
// If point collision involved (can be surface or inside) take all tetra around the point, if close enough, for refinement
88101
for (contactInfo * cInfo : m_pointContacts)
89102
{
90103
if (cInfo->dist > refineDistance)
@@ -95,46 +108,34 @@ void RefineCarvingPerformer::filterContacts()
95108
for (auto tetraId : tetraAV)
96109
m_tetra2Filter.insert(tetraId);
97110

98-
if (cInfo->dist <= carvingDistance) {
99-
const core::topology::BaseMeshTopology::TrianglesAroundVertex& triAV = m_topologyCon->getTrianglesAroundVertex(cInfo->elemId);
100-
101-
for (auto triId : triAV)
102-
m_triIdsToFilter.insert(triId);
111+
if (cInfo->dist <= carvingDistance)
112+
{
113+
for (auto tetraId : tetraAV)
114+
m_tetra2Carv.insert(tetraId);
103115
}
104116

105-
carvingPosition = cInfo->pointA;
117+
m_carvingPositions.push_back(cInfo->pointA);
106118
}
107119
}
108120

109121

110122
bool RefineCarvingPerformer::runPerformer()
111123
{
112-
if (!m_tetra2Filter.empty())
124+
// nothin to do if no tetra to refine
125+
if (m_tetra2Filter.empty())
126+
return false;
127+
128+
// will try to refine or carve but not the two at the same time for now, to avoid some conflict.
129+
if (m_doRefine)
113130
{
114131
sofa::Size nbrTetra = m_topologyCon->getNbTetrahedra();
115-
bool res = m_tetraAlgo->refineTetrahedra(m_tetra2Filter, m_carvingMgr->d_refineCriteria.getValue());
116-
117-
if (res)
118-
{
119-
#if 0
120-
return res;
121-
#else
122-
sofa::Size nbrTetraNew = m_topologyCon->getNbTetrahedra();
123-
for (sofa::Index tetraId = nbrTetra; tetraId < nbrTetraNew; ++tetraId)
124-
m_tetra2Filter.insert(tetraId);
125-
#endif
126-
}
127-
#if 0
128-
else
129-
{
130-
surfaceCarving2();
131-
}
132-
#else
133-
simpleCarving();
134-
#endif
135-
136-
137-
return res;
132+
m_tetraAlgo->refineTetrahedra(m_tetra2Filter, m_carvingMgr->d_refineCriteria.getValue());
133+
m_doRefine = false;
134+
}
135+
else
136+
{
137+
m_tetraAlgo->removeTetrahedra(m_tetra2Carv);
138+
m_doRefine = true;
138139
}
139140

140141
return false;
@@ -158,10 +159,16 @@ void RefineCarvingPerformer::simpleCarving()
158159
bary += vertices[tetra[j]];
159160
}
160161
bary *= 0.25;
161-
SReal dist = (carvingPosition - bary).norm();
162162

163-
if (dist < carvingDistance)
164-
tetraToremove.insert(tetraId);
163+
for (const auto& carvingPos : m_carvingPositions)
164+
{
165+
SReal dist = (carvingPos - bary).norm();
166+
if (dist < carvingDistance)
167+
{
168+
tetraToremove.insert(tetraId);
169+
break;
170+
}
171+
}
165172
}
166173

167174
if (!tetraToremove.empty())
@@ -192,15 +199,19 @@ void RefineCarvingPerformer::surfaceCarving()
192199
for (auto pointId : pointsToCheck)
193200
{
194201
Vec3& vertex = vertices[pointId];
195-
Vec3 dir = vertex - carvingPosition;
196-
const SReal dist = dir.norm();
197202

198-
if (dist > carvingDistance)
199-
continue;
203+
for (const auto& carvingPos : m_carvingPositions)
204+
{
205+
Vec3 dir = vertex - carvingPos;
206+
const SReal dist = dir.norm();
207+
208+
if (dist > carvingDistance)
209+
continue;
200210

201-
SReal factor = (carvingDistance - dist)* invCarv; // ]0, carvingDistance]
202-
msg_info("RefineCarvingPerformer") << "pointId: " << pointId << " | factor: " << factor << " | carvingDistance: " << carvingDistance << " | dist: " << dist;
203-
vertex = vertex + dir * factor;
211+
SReal factor = (carvingDistance - dist) * invCarv; // ]0, carvingDistance]
212+
msg_info("RefineCarvingPerformer") << "pointId: " << pointId << " | factor: " << factor << " | carvingDistance: " << carvingDistance << " | dist: " << dist;
213+
vertex = vertex + dir * factor;
214+
}
204215
}
205216
}
206217

@@ -271,7 +282,7 @@ void RefineCarvingPerformer::surfaceCarving2()
271282
{
272283
Vec3 vertex = vertices[pId];
273284
Vec3 dir = oppoPCoord - vertex; // normalise??
274-
const SReal dist = (vertex - carvingPosition).norm();
285+
const SReal dist = 0.0;// TODO check that (vertex - carvingPosition).norm();
275286

276287
if (dist > carvingDistance)
277288
continue;

src/InfinyToolkit/CarvingTools/RefineCarvingPerformer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class SOFA_INFINYTOOLKIT_API RefineCarvingPerformer : public BaseCarvingPerforme
3737

3838
bool initPerformer() override;
3939

40+
void clearContacts() override;
41+
4042
void filterContacts();
4143

4244
bool runPerformer() override;
@@ -58,8 +60,10 @@ class SOFA_INFINYTOOLKIT_API RefineCarvingPerformer : public BaseCarvingPerforme
5860
sofa::type::vector<unsigned int> m_tetra2Filter2;
5961
std::set<unsigned int> m_triIdsToFilter;
6062
std::set<unsigned int> m_triIds;
63+
std::set<unsigned int> m_tetra2Carv;
6164

62-
Vec3 carvingPosition;
65+
std::vector<Vec3> m_carvingPositions;
66+
bool m_doRefine = true;
6367
};
6468

6569
} // namespace sofa::infinytoolkit

0 commit comments

Comments
 (0)