remove memset from meshtree.h

This commit is contained in:
alemuntoni 2021-10-19 12:14:31 +02:00
parent 70ac3d8248
commit 5e17997b37
1 changed files with 190 additions and 148 deletions

View File

@ -125,189 +125,231 @@ namespace vcg {
return count; return count;
} }
void Process(vcg::AlignPair::Param &ap, MeshTree::Param &mtp) { void Process(vcg::AlignPair::Param& ap, MeshTree::Param& mtp)
{
std::array<char, 1024> buf;
std::sprintf(
buf.data(),
"Starting Processing of %i glued meshes out of %zu meshes\n",
gluedNum(),
nodeMap.size());
cb(0, buf.data());
char buf[1024]; /******* Occupancy Grid Computation *************/
std::sprintf(buf, "Starting Processing of %i glued meshes out of %zu meshes\n", gluedNum(), nodeMap.size()); buf.fill('\0');
cb(0, buf); std::sprintf(buf.data(), "Computing Overlaps %i glued meshes...\n", gluedNum());
cb(0, buf.data());
/******* Occupancy Grid Computation *************/ OG.Init(
std::memset(buf, '\0', 1024); static_cast<int>(nodeMap.size()),
std::sprintf(buf, "Computing Overlaps %i glued meshes...\n", gluedNum()); vcg::Box3<ScalarType>::Construct(gluedBBox()),
cb(0, buf); mtp.OGSize);
OG.Init(static_cast<int>(nodeMap.size()), vcg::Box3<ScalarType>::Construct(gluedBBox()), mtp.OGSize); for (auto& ni : nodeMap) {
MeshTree::MeshNode* mn = ni.second;
if (mn->glued) {
OG.AddMesh(mn->m->cm, vcg::Matrix44<ScalarType>::Construct(mn->tr()), mn->Id());
}
}
for (auto& ni : nodeMap) { OG.Compute();
MeshTree::MeshNode *mn = ni.second; OG.Dump(stdout);
if (mn->glued) { // Note: the s and t of the OG translate into fix and mov, respectively.
OG.AddMesh(mn->m->cm, vcg::Matrix44<ScalarType>::Construct(mn->tr()), mn->Id());
}
}
OG.Compute(); /*************** The long loop of arc computing **************/
OG.Dump(stdout);
// Note: the s and t of the OG translate into fix and mov, respectively.
/*************** The long loop of arc computing **************/ // count existing arcs within current error threshold
float percentileThr = 0;
if (!resultList.empty()) {
vcg::Distribution<float> H;
for (auto& li : resultList) {
H.Add(li.err);
}
// count existing arcs within current error threshold percentileThr = H.Percentile(1.0f - mtp.recalcThreshold);
float percentileThr = 0; }
if (!resultList.empty()) {
vcg::Distribution<float> H; std::size_t totalArcNum = 0;
for (auto& li : resultList) { int preservedArcNum = 0, recalcArcNum = 0;
H.Add(li.err);
}
percentileThr = H.Percentile(1.0f - mtp.recalcThreshold); while (totalArcNum < OG.SVA.size() &&
} OG.SVA[totalArcNum].norm_area > mtp.arcThreshold) {
AlignPair::Result* curResult =
findResult(OG.SVA[totalArcNum].s, OG.SVA[totalArcNum].t);
if (curResult) {
if (curResult->err < percentileThr) {
++preservedArcNum;
}
else {
++recalcArcNum;
}
}
else {
resultList.push_back(AlignPair::Result());
resultList.back().FixName = OG.SVA[totalArcNum].s;
resultList.back().MovName = OG.SVA[totalArcNum].t;
resultList.back().err = std::numeric_limits<double>::max();
}
++totalArcNum;
}
std::size_t totalArcNum = 0; // if there are no arcs at all complain and return
int preservedArcNum = 0, recalcArcNum = 0; if (totalArcNum == 0) {
buf.fill('\0');
std::sprintf(
buf.data(),
"\n Failure. There are no overlapping meshes?\n No candidate alignment arcs. "
"Nothing Done.\n");
cb(0, buf.data());
return;
}
while(totalArcNum < OG.SVA.size() && OG.SVA[totalArcNum].norm_area > mtp.arcThreshold) int num_max_thread = 1;
{
AlignPair::Result *curResult = findResult(OG.SVA[totalArcNum].s, OG.SVA[totalArcNum].t);
if (curResult) {
if (curResult->err < percentileThr) {
++preservedArcNum;
}
else {
++recalcArcNum;
}
}
else {
resultList.push_back(AlignPair::Result());
resultList.back().FixName = OG.SVA[totalArcNum].s;
resultList.back().MovName = OG.SVA[totalArcNum].t;
resultList.back().err = std::numeric_limits<double>::max();
}
++totalArcNum;
}
//if there are no arcs at all complain and return
if (totalArcNum == 0) {
std::memset(buf, '\0', 1024);
std::sprintf(buf, "\n Failure. There are no overlapping meshes?\n No candidate alignment arcs. Nothing Done.\n");
cb(0, buf);
return;
}
int num_max_thread = 1;
#ifdef _OPENMP #ifdef _OPENMP
if (totalArcNum > 32) num_max_thread = omp_get_max_threads(); if (totalArcNum > 32)
num_max_thread = omp_get_max_threads();
#endif #endif
std::memset(buf, '\0', 1024); buf.fill('\0');
std::sprintf(buf, "Arc with good overlap %6zu (on %6zu)\n", totalArcNum, OG.SVA.size()); std::sprintf(
cb(0, buf); buf.data(), "Arc with good overlap %6zu (on %6zu)\n", totalArcNum, OG.SVA.size());
cb(0, buf.data());
std::memset(buf, '\0', 1024); buf.fill('\0');
std::sprintf(buf, " %6i preserved %i Recalc \n", preservedArcNum, recalcArcNum); std::sprintf(buf.data(), " %6i preserved %i Recalc \n", preservedArcNum, recalcArcNum);
cb(0, buf); cb(0, buf.data());
bool hasValidAlign = false; bool hasValidAlign = false;
#pragma omp parallel for schedule(dynamic, 1) num_threads(num_max_thread) #pragma omp parallel for schedule(dynamic, 1) num_threads(num_max_thread)
// on windows, omp does not support unsigned types for indices on cycles // on windows, omp does not support unsigned types for indices on cycles
for (int i = 0 ;i < static_cast<int>(totalArcNum); ++i) { for (int i = 0; i < static_cast<int>(totalArcNum); ++i) {
std::fprintf(
stdout,
"%4i -> %4i Area:%5i NormArea:%5.3f\n",
OG.SVA[i].s,
OG.SVA[i].t,
OG.SVA[i].area,
OG.SVA[i].norm_area);
AlignPair::Result* curResult = findResult(OG.SVA[i].s, OG.SVA[i].t);
std::fprintf(stdout,"%4i -> %4i Area:%5i NormArea:%5.3f\n",OG.SVA[i].s,OG.SVA[i].t,OG.SVA[i].area,OG.SVA[i].norm_area); // // missing arc and arc with great error must be recomputed.
AlignPair::Result *curResult = findResult(OG.SVA[i].s,OG.SVA[i].t); if (curResult->err >= percentileThr) {
ProcessArc(OG.SVA[i].s, OG.SVA[i].t, *curResult, ap);
curResult->area = OG.SVA[i].norm_area;
// // missing arc and arc with great error must be recomputed. if (curResult->isValid()) {
if (curResult->err >= percentileThr) { hasValidAlign = true;
std::pair<double, double> dd = curResult->computeAvgErr();
ProcessArc(OG.SVA[i].s, OG.SVA[i].t, *curResult, ap);
curResult->area = OG.SVA[i].norm_area;
if (curResult->isValid()) {
hasValidAlign = true;
std::pair<double, double> dd = curResult->computeAvgErr();
#pragma omp critical #pragma omp critical
std::memset(buf, '\0', 1024); buf.fill('\0');
std::sprintf(buf, "(%3i/%3zu) %2i -> %2i Aligned AvgErr dd=%f -> dd=%f \n", i+1,totalArcNum,OG.SVA[i].s,OG.SVA[i].t,dd.first,dd.second); std::sprintf(
cb(0, buf); buf.data(),
} "(%3i/%3zu) %2i -> %2i Aligned AvgErr dd=%f -> dd=%f \n",
else { i + 1,
totalArcNum,
OG.SVA[i].s,
OG.SVA[i].t,
dd.first,
dd.second);
cb(0, buf.data());
}
else {
#pragma omp critical #pragma omp critical
std::memset(buf, '\0', 1024); buf.fill('\0');
std::sprintf(buf, "(%3i/%3zu) %2i -> %2i Failed Alignment of one arc %s\n",i+1,totalArcNum,OG.SVA[i].s,OG.SVA[i].t,vcg::AlignPair::errorMsg(curResult->status)); std::sprintf(
cb(0, buf); buf.data(),
} "(%3i/%3zu) %2i -> %2i Failed Alignment of one arc %s\n",
} i + 1,
} totalArcNum,
OG.SVA[i].s,
OG.SVA[i].t,
vcg::AlignPair::errorMsg(curResult->status));
cb(0, buf.data());
}
}
}
//if there are no valid arcs complain and return // if there are no valid arcs complain and return
if (!hasValidAlign) { if (!hasValidAlign) {
std::memset(buf, '\0', 1024); buf.fill('\0');
std::sprintf(buf, "\n Failure. No successful arc among candidate Alignment arcs. Nothing Done.\n"); std::sprintf(
cb(0, buf); buf.data(),
return; "\n Failure. No successful arc among candidate Alignment arcs. Nothing "
} "Done.\n");
cb(0, buf.data());
return;
}
vcg::Distribution<float> H; // stat for printing vcg::Distribution<float> H; // stat for printing
for (auto& li : resultList) { for (auto& li : resultList) {
if (li.isValid()) H.Add(li.err); if (li.isValid())
} H.Add(li.err);
}
std::memset(buf, '\0', 1024); buf.fill('\0');
std::sprintf(buf, "Completed Mesh-Mesh Alignment: Avg Err %5.3f; Median %5.3f; 90%% %5.3f\n", H.Avg(), H.Percentile(0.5f), H.Percentile(0.9f)); std::sprintf(
cb(0, buf); buf.data(),
"Completed Mesh-Mesh Alignment: Avg Err %5.3f; Median %5.3f; 90%% %5.3f\n",
H.Avg(),
H.Percentile(0.5f),
H.Percentile(0.9f));
cb(0, buf.data());
ProcessGlobal(ap); ProcessGlobal(ap);
} }
void ProcessGlobal(vcg::AlignPair::Param &ap) { void ProcessGlobal(vcg::AlignPair::Param& ap)
{
/************** Preparing Matrices for global alignment *************/
std::vector<int> GluedIdVec;
std::vector<vcg::Matrix44d> GluedTrVec;
char buff[1024]; std::map<int, std::string> names;
std::memset(buff, '\0', 1024);
/************** Preparing Matrices for global alignment *************/ for (auto& ni : nodeMap) {
std::vector<int> GluedIdVec; MeshTree::MeshNode* mn = ni.second;
std::vector<vcg::Matrix44d> GluedTrVec; if (mn->glued) {
GluedIdVec.push_back(mn->Id());
GluedTrVec.push_back(vcg::Matrix44d::Construct(mn->tr()));
names[mn->Id()] = qUtf8Printable(mn->m->label());
}
}
std::map<int, std::string> names; vcg::AlignGlobal AG;
std::vector<vcg::AlignPair::Result*> ResVecPtr;
for (auto& li : resultList) {
if (li.isValid()) {
ResVecPtr.push_back(&li);
}
}
for (auto& ni : nodeMap) { AG.BuildGraph(ResVecPtr, GluedTrVec, GluedIdVec);
MeshTree::MeshNode *mn = ni.second; float StartGlobErr = 0.001f;
if (mn->glued) { while (!AG.GlobalAlign(
GluedIdVec.push_back(mn->Id()); names,
GluedTrVec.push_back(vcg::Matrix44d::Construct(mn->tr())); StartGlobErr,
names[mn->Id()] = qUtf8Printable(mn->m->label()); 100,
} ap.MatchMode == vcg::AlignPair::Param::MMRigid,
} stdout,
cb)) {
StartGlobErr *= 2;
AG.BuildGraph(ResVecPtr, GluedTrVec, GluedIdVec);
}
vcg::AlignGlobal AG; std::vector<vcg::Matrix44d> GluedTrVecOut(GluedTrVec.size());
std::vector<vcg::AlignPair::Result *> ResVecPtr; AG.GetMatrixVector(GluedTrVecOut, GluedIdVec);
for (auto& li : resultList) {
if (li.isValid()) {
ResVecPtr.push_back(&li);
}
}
AG.BuildGraph(ResVecPtr, GluedTrVec, GluedIdVec); // Now get back the results!
for (std::size_t ii = 0; ii < GluedTrVecOut.size(); ++ii) {
float StartGlobErr = 0.001f; MM(GluedIdVec[ii])->cm.Tr.Import(GluedTrVecOut[ii]);
while (!AG.GlobalAlign(names, StartGlobErr, 100, ap.MatchMode == vcg::AlignPair::Param::MMRigid, stdout, cb)) { }
StartGlobErr *= 2; std::string str =
AG.BuildGraph(ResVecPtr,GluedTrVec, GluedIdVec); "Completed Global Alignment (error bound " + std::to_string(StartGlobErr) + ")\n";
} cb(0, str.c_str());
}
std::vector<vcg::Matrix44d> GluedTrVecOut(GluedTrVec.size());
AG.GetMatrixVector(GluedTrVecOut,GluedIdVec);
// Now get back the results!
for (std::size_t ii = 0; ii < GluedTrVecOut.size(); ++ii) {
MM(GluedIdVec[ii])->cm.Tr.Import(GluedTrVecOut[ii]);
}
std::sprintf(buff, "Completed Global Alignment (error bound %6.4f)\n", StartGlobErr);
cb(0, buff);
}
void ProcessArc(int fixId, int movId, vcg::AlignPair::Result &result, vcg::AlignPair::Param ap) { void ProcessArc(int fixId, int movId, vcg::AlignPair::Result &result, vcg::AlignPair::Param ap) {