2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @file VoxelComplex.ih
19 * @author Pablo Hernandez-Cerdan (\c pablo.hernandez.cerdan@outlook.com)
20 * Insitute of Fundamental Sciences, Massey University, New Zealand.
24 * Implementation of inline methods defined in VoxelComplex.h
26 * This file is part of the DGtal library.
29 //////////////////////////////////////////////////////////////////////////////
30 #include <DGtal/graph/ObjectBoostGraphInterface.h>
31 #include <DGtal/topology/CubicalComplexFunctions.h>
32 #include <DGtal/topology/NeighborhoodConfigurations.h>
33 #include <DGtal/helpers/StdDefs.h>
34 #include <boost/graph/connected_components.hpp>
35 #include <boost/graph/filtered_graph.hpp>
36 #include <boost/property_map/property_map.hpp>
41 //////////////////////////////////////////////////////////////////////////////
42 // Default constructor:
43 template <typename TKSpace, typename TCellContainer>
44 inline DGtal::VoxelComplex<TKSpace, TCellContainer>::VoxelComplex()
46 myTablePtr(nullptr), myPointToMaskPtr(nullptr),
47 myIsTableLoaded(false) {}
50 template <typename TKSpace, typename TCellContainer>
51 inline DGtal::VoxelComplex<TKSpace, TCellContainer>::VoxelComplex(
52 const VoxelComplex &other)
54 myTablePtr(other.myTablePtr),
55 myPointToMaskPtr(other.myPointToMaskPtr),
56 myIsTableLoaded(other.myIsTableLoaded) {}
58 ///////////////////////////////////////////////////////////////////////////////
59 // IMPLEMENTATION of inline methods.
60 ///////////////////////////////////////////////////////////////////////////////
61 //-----------------------------------------------------------------------------
62 template <typename TKSpace, typename TCellContainer>
63 inline DGtal::VoxelComplex<TKSpace, TCellContainer> &
64 DGtal::VoxelComplex<TKSpace, TCellContainer>::
65 operator=(const Self &other)
68 this->myKSpace = other.myKSpace;
69 this->myCells = other.myCells;
70 myTablePtr = other.myTablePtr;
71 myPointToMaskPtr = other.myPointToMaskPtr;
72 myIsTableLoaded = other.myIsTableLoaded;
76 //---------------------------------------------------------------------------
77 ///////////////////////////////////////////////////////////////////////////////
79 ///////////////////////////////////////////////////////////////////////////////
81 ///////////////////////////////////////////////////////////////////////////////
82 // Interface - Voxel :
83 //---------------------------------------------------------------------------
84 // template <typename TKSpace, typename TCellContainer>
85 // const typename DGtal::VoxelComplex<TKSpace,
86 // TCellContainer>::Object::Point &
87 // DGtal::VoxelComplex<TKSpace, TCellContainer>::objPointFromVoxel(
88 // const Cell &voxel) const {
89 // ASSERT(isSpel(voxel) == true);
90 // ASSERT(this->belongs(voxel));
91 // const auto &ks = this->space();
92 // return *(myObject.pointSet().find(ks.uCoords(voxel)));
96 //-----------------------------------------------------------------------------
98 template <typename TKSpace, typename TCellContainer>
99 template <typename TDigitalSet>
100 inline void DGtal::VoxelComplex<TKSpace, TCellContainer>::construct(
101 const TDigitalSet &input_set,
102 const Alias<ConfigMap> input_table)
104 Parent::construct(input_set);
105 setSimplicityTable(input_table);
108 template <typename TKSpace, typename TCellContainer>
109 void DGtal::VoxelComplex<TKSpace, TCellContainer>::setSimplicityTable(
110 const Alias<ConfigMap> input_table)
112 this->myTablePtr = input_table;
113 this->myPointToMaskPtr =
114 functions::mapZeroPointNeighborhoodToConfigurationMask<Point>();
115 this->myIsTableLoaded = true;
118 template <typename TKSpace, typename TCellContainer>
119 void DGtal::VoxelComplex<TKSpace, TCellContainer>::copySimplicityTable(
122 myTablePtr = other.myTablePtr;
123 myPointToMaskPtr = other.myPointToMaskPtr;
124 myIsTableLoaded = other.myIsTableLoaded;
127 template <typename TKSpace, typename TCellContainer>
128 const typename DGtal::VoxelComplex<TKSpace,
129 TCellContainer>::ConfigMap &
130 DGtal::VoxelComplex<TKSpace, TCellContainer>::table() const
135 template <typename TKSpace, typename TCellContainer>
137 DGtal::VoxelComplex<TKSpace, TCellContainer>::isTableLoaded() const
139 return myIsTableLoaded;
142 template <typename TKSpace, typename TCellContainer>
143 const typename DGtal::VoxelComplex<TKSpace,
144 TCellContainer>::PointToMaskMap &
145 DGtal::VoxelComplex<TKSpace, TCellContainer>::pointToMask() const
147 return *myPointToMaskPtr;
149 //---------------------------------------------------------------------------
150 template <typename TKSpace, typename TCellContainer>
151 inline void DGtal::VoxelComplex<TKSpace, TCellContainer>::voxelClose(
154 const auto &ks = this->space();
155 ASSERT(ks.uDim(kcell) == 3);
157 auto direct_faces = ks.uLowerIncident(kcell);
158 for (typename Cells::const_iterator cells_it = direct_faces.begin(),
159 cells_it_end = direct_faces.end();
160 cells_it != cells_it_end; ++cells_it) {
161 this->insertCell(l, *cells_it);
163 cellsClose(l, direct_faces);
165 //---------------------------------------------------------------------------
166 template <typename TKSpace, typename TCellContainer>
167 inline void DGtal::VoxelComplex<TKSpace, TCellContainer>::cellsClose(
168 Dimension k, const Cells &cells)
172 if (cells.size() == 0)
174 const auto &ks = this->space();
176 for (auto const &kcell : cells) {
177 auto direct_faces = ks.uLowerIncident(kcell);
178 for (typename Cells::const_iterator cells_it = direct_faces.begin(),
179 cells_it_end = direct_faces.end();
180 cells_it != cells_it_end; ++cells_it) {
181 this->insertCell(l, *cells_it);
183 cellsClose(l, direct_faces);
186 //---------------------------------------------------------------------------
187 template <typename TKSpace, typename TCellContainer>
189 DGtal::VoxelComplex<TKSpace, TCellContainer>::insertVoxelCell(
190 const Cell &kcell, const bool &close_it, const Data &data)
192 ASSERT(this->space().uDim(kcell) == 3);
193 this->insertCell(3, kcell, data);
198 //---------------------------------------------------------------------------
199 template <typename TKSpace, typename TCellContainer>
201 DGtal::VoxelComplex<TKSpace, TCellContainer>::insertVoxelPoint(
202 const Point &dig_point, const bool &close_it, const Data &data)
204 const auto &ks = this->space();
205 insertVoxelCell(ks.uSpel(dig_point), close_it, data);
208 //---------------------------------------------------------------------------
209 template <typename TKSpace, typename TCellContainer>
210 template <typename TDigitalSet>
212 DGtal::VoxelComplex<TKSpace, TCellContainer>::dumpVoxels(
213 TDigitalSet & in_out_set) const
215 const auto &ks = this->space();
216 for (auto it = this->begin(3), itE = this->end(3) ; it != itE ; ++it ){
217 in_out_set.insertNew(ks.uCoords(it->first));
220 //-----------------------------------------------------------------------------
222 template <typename TKSpace, typename TCellContainer>
223 void DGtal::VoxelComplex<TKSpace, TCellContainer>::pointelsFromCell(
224 std::set<Cell> &pointels_out, const Cell &input_cell) const
226 const auto input_dim = this->space().uDim(input_cell);
227 if (input_dim == 0) {
228 pointels_out.emplace(input_cell);
231 auto ufaces = this->space().uFaces(input_cell);
232 for (auto &&f : ufaces)
233 this->pointelsFromCell(pointels_out, f);
237 //---------------------------------------------------------------------------
238 template <typename TKSpace, typename TCellContainer>
239 void DGtal::VoxelComplex<TKSpace, TCellContainer>::spelsFromCell(
240 std::set<Cell> &spels_out, const Cell &input_cell) const
242 const auto input_dim = this->space().uDim(input_cell);
243 if (input_dim == this->dimension) {
244 if (this->belongs(input_cell))
245 spels_out.emplace(input_cell);
248 auto co_faces = this->space().uCoFaces(input_cell);
249 for (auto &&f : co_faces) {
250 auto f_dim = this->space().uDim(f);
251 if (f_dim >= input_dim)
252 this->spelsFromCell(spels_out, f);
256 //---------------------------------------------------------------------------
257 template <typename TKSpace, typename TCellContainer>
258 typename DGtal::VoxelComplex<TKSpace, TCellContainer>::Clique
259 DGtal::VoxelComplex<TKSpace, TCellContainer>::Kneighborhood(
260 const Cell &input_cell) const
262 auto spels_out = neighborhoodVoxels(input_cell);
264 const auto &ks = this->space();
266 for (const auto &v : spels_out)
267 clique.insertCell(v);
272 template <typename TKSpace, typename TCellContainer>
273 std::set<typename TKSpace::Cell>
274 DGtal::VoxelComplex<TKSpace, TCellContainer>::neighborhoodVoxels(
275 const Cell &input_spel) const
277 std::set<Cell> pointels_out;
278 std::set<Cell> spels_out;
279 pointelsFromCell(pointels_out, input_spel);
280 for (const auto &p : pointels_out)
281 spelsFromCell(spels_out, p);
285 template <typename TKSpace, typename TCellContainer>
286 std::set<typename TKSpace::Cell>
287 DGtal::VoxelComplex<TKSpace, TCellContainer>::properNeighborhoodVoxels(
288 const Cell &input_spel) const
291 auto spels_out = neighborhoodVoxels(input_spel);
292 auto search = spels_out.find(input_spel);
293 if (search != spels_out.end()) {
294 spels_out.erase(search);
299 //---------------------------------------------------------------------------
300 template <typename TKSpace, typename TCellContainer>
301 bool DGtal::VoxelComplex<TKSpace, TCellContainer>::isSpel(
304 return (this->space().uDim(b) == this->space().DIM);
307 //---------------------------------------------------------------------------
308 template <typename TKSpace, typename TCellContainer>
309 typename DGtal::VoxelComplex<TKSpace, TCellContainer>::Cell
310 DGtal::VoxelComplex<TKSpace,
311 TCellContainer>::surfelBetweenAdjacentSpels(const Cell &A, const Cell &B)
314 ASSERT(isSpel(A) == true);
315 ASSERT(isSpel(B) == true);
316 const auto &ks = this->space();
317 // Digital coordinates
318 auto &&orientation_BA = ks.uCoords(B) - ks.uCoords(A);
319 ASSERT(orientation_BA.norm1() == 1);
320 // Khalimsky Coordinates
321 return ks.uCell(A.preCell().coordinates + orientation_BA);
323 //---------------------------------------------------------------------------
324 ///////////////////////////////////////////////////////////////////////////////
326 template <typename TKSpace, typename TCellContainer>
327 std::pair<bool, typename DGtal::VoxelComplex<TKSpace,
328 TCellContainer>::Clique>
329 DGtal::VoxelComplex<TKSpace, TCellContainer>::K_2(
330 const typename KSpace::Point &A,
331 const typename KSpace::Point &B,
334 const auto &ks = this->space();
335 using KPreSpace = typename TKSpace::PreCellularGridSpace;
336 auto orientation_vector = B - A;
337 ASSERT(orientation_vector.norm1() == 1);
339 for (auto i = 0; i != ks.DIM; ++i)
340 if (orientation_vector[i] == 1 || orientation_vector[i] == -1) {
346 if (direction == 0) {
349 } else if (direction == 1) {
357 const PreCell x0 = KPreSpace::uSpel(A + right);
358 const PreCell x4 = KPreSpace::uSpel(A - right);
359 const PreCell x2 = KPreSpace::uSpel(A + up);
360 const PreCell x6 = KPreSpace::uSpel(A - up);
362 const PreCell x1 = KPreSpace::uSpel(A + up + right);
363 const PreCell x3 = KPreSpace::uSpel(A + up - right);
364 const PreCell x7 = KPreSpace::uSpel(A - up + right);
365 const PreCell x5 = KPreSpace::uSpel(A - up - right);
367 const PreCell y0 = KPreSpace::uSpel(B + right);
368 const PreCell y4 = KPreSpace::uSpel(B - right);
369 const PreCell y2 = KPreSpace::uSpel(B + up);
370 const PreCell y6 = KPreSpace::uSpel(B - up);
372 const PreCell y1 = KPreSpace::uSpel(B + up + right);
373 const PreCell y3 = KPreSpace::uSpel(B + up - right);
374 const PreCell y7 = KPreSpace::uSpel(B - up + right);
375 const PreCell y5 = KPreSpace::uSpel(B - up - right);
377 const auto bx0 = this->belongs(KSpace::DIM, x0);
378 const auto bx1 = this->belongs(KSpace::DIM, x1);
379 const auto bx2 = this->belongs(KSpace::DIM, x2);
380 const auto bx3 = this->belongs(KSpace::DIM, x3);
381 const auto bx4 = this->belongs(KSpace::DIM, x4);
382 const auto bx5 = this->belongs(KSpace::DIM, x5);
383 const auto bx6 = this->belongs(KSpace::DIM, x6);
384 const auto bx7 = this->belongs(KSpace::DIM, x7);
386 const auto by0 = this->belongs(KSpace::DIM, y0);
387 const auto by1 = this->belongs(KSpace::DIM, y1);
388 const auto by2 = this->belongs(KSpace::DIM, y2);
389 const auto by3 = this->belongs(KSpace::DIM, y3);
390 const auto by4 = this->belongs(KSpace::DIM, y4);
391 const auto by5 = this->belongs(KSpace::DIM, y5);
392 const auto by6 = this->belongs(KSpace::DIM, y6);
393 const auto by7 = this->belongs(KSpace::DIM, y7);
397 k2_crit.insertCell(ks.uCell(x0));
399 k2_crit.insertCell(ks.uCell(x1));
401 k2_crit.insertCell(ks.uCell(x2));
403 k2_crit.insertCell(ks.uCell(x3));
405 k2_crit.insertCell(ks.uCell(x4));
407 k2_crit.insertCell(ks.uCell(x5));
409 k2_crit.insertCell(ks.uCell(x6));
411 k2_crit.insertCell(ks.uCell(x7));
414 k2_crit.insertCell(ks.uCell(y0));
416 k2_crit.insertCell(ks.uCell(y1));
418 k2_crit.insertCell(ks.uCell(y2));
420 k2_crit.insertCell(ks.uCell(y3));
422 k2_crit.insertCell(ks.uCell(y4));
424 k2_crit.insertCell(ks.uCell(y5));
426 k2_crit.insertCell(ks.uCell(y6));
428 k2_crit.insertCell(ks.uCell(y7));
429 // Note that input spels A,B are ommited.
431 /////////////////////////////////
432 // Critical Clique Conditions:
433 using namespace DGtal::functions;
434 // Intersection of k2-neighborhood with the object:
435 // (i) k2_clique must be empty or NOT 0-connected
436 bool is_empty{k2_crit.nbCells(KSpace::DIM) == 0};
438 // Check connectedness using object if not empty
439 bool is_disconnected{false};
441 using DigitalTopology = DGtal::Z3i::DT26_6;
442 using DigitalSet = DGtal::DigitalSetByAssociativeContainer<
444 std::unordered_set<typename DGtal::Z3i::Domain::Point>>;
445 using NewObject = DGtal::Object<DigitalTopology, DigitalSet>;
446 auto new_obj = objectFromSpels<NewObject, KSpace, CellContainer>(k2_crit);
447 auto con = new_obj->computeConnectedness();
448 is_disconnected = (con == DISCONNECTED);
451 bool conditionI = is_empty || is_disconnected;
453 // (ii) Xi or Yi belongs to this for i={0,2,4,6}
454 std::vector<bool> bb(4);
460 bool conditionII = bb[0] && bb[1] && bb[2] && bb[3];
461 // is_critical if any condition is true.
462 bool is_critical = conditionI || conditionII;
465 trace.beginBlock(" K2 critical conditions ");
466 trace.info() << " conditionI = " << conditionI
467 << " : is_empty || is_disconnected = " << is_empty
468 << " && " << is_disconnected << std::endl;
469 trace.info() << " conditionII = " << conditionII << " : " << bb[0]
470 << " && " << bb[1] << " && " << bb[2] << " && " << bb[3]
472 trace.info() << " is_critical = " << is_critical
473 << " conditionI || conditionII : " << conditionI << " || "
474 << conditionII << std::endl;
478 // Return the clique (A,B), not the mask k2_crit
479 Clique k2_clique(ks);
480 Cell Ac = ks.uSpel(A);
481 Cell Bc = ks.uSpel(B);
482 k2_clique.insertCell(Ac);
483 k2_clique.insertCell(Bc);
484 return std::make_pair(is_critical, k2_clique);
487 //---------------------------------------------------------------------------
488 template <typename TKSpace, typename TCellContainer>
489 std::pair<bool, typename DGtal::VoxelComplex<TKSpace, TCellContainer>::Clique>
490 DGtal::VoxelComplex<TKSpace, TCellContainer>::K_2(const Cell &A,
495 // A and B are contiguous spels.
496 ASSERT(isSpel(A) == true);
497 ASSERT(isSpel(B) == true);
498 const auto &ks = this->space();
499 auto B_coord = ks.uCoords(B);
500 auto A_coord = ks.uCoords(A);
501 return this->K_2(A_coord, B_coord, verbose);
503 //---------------------------------------------------------------------------
505 template <typename TKSpace, typename TCellContainer>
506 std::pair<bool, typename DGtal::VoxelComplex<TKSpace, TCellContainer>::Clique>
507 DGtal::VoxelComplex<TKSpace, TCellContainer>::K_2(const Cell &face2,
510 const auto &ks = this->space();
511 ASSERT(ks.uIsSurfel(face2));
512 using KPreSpace = typename TKSpace::PreCellularGridSpace;
513 const auto co_faces = KPreSpace::uCoFaces(face2);
514 ASSERT(co_faces.size() == 2);
515 const auto &cf0 = co_faces[0];
516 const auto &cf1 = co_faces[1];
517 // spels must belong to complex.
518 if (this->belongs(cf0) && this->belongs(cf1))
519 return this->K_2(ks.uCell(cf0), ks.uCell(cf1), verbose);
521 return std::make_pair(false, Clique(ks));
523 //---------------------------------------------------------------------------
525 template <typename TKSpace, typename TCellContainer>
526 std::pair<bool, typename DGtal::VoxelComplex<TKSpace, TCellContainer>::Clique>
527 DGtal::VoxelComplex<TKSpace, TCellContainer>::K_1(const Cell &face1,
530 const auto &ks = this->space();
531 ASSERT(ks.uDim(face1) == 1);
532 using KPreSpace = typename TKSpace::PreCellularGridSpace;
533 // Get 2 orth dirs in orient_orth
534 std::vector<Point> dirs_orth;
535 for (auto q = KPreSpace::uOrthDirs(face1); q != 0; ++q) {
536 const Dimension dir = *q;
537 Point positive_orth{0, 0, 0};
538 for (Dimension i = 0; i != ks.DIM; ++i)
540 positive_orth[i] = 1;
542 dirs_orth.push_back(positive_orth);
545 auto &kface = face1.preCell().coordinates;
546 const Point a{kface + dirs_orth[0] + dirs_orth[1]};
547 const Point b{kface + dirs_orth[0] - dirs_orth[1]};
548 const Point c{kface - dirs_orth[0] + dirs_orth[1]};
549 const Point d{kface - dirs_orth[0] - dirs_orth[1]};
551 const PreCell A = KPreSpace::uCell(a);
552 const PreCell B = KPreSpace::uCell(b);
553 const PreCell C = KPreSpace::uCell(c);
554 const PreCell D = KPreSpace::uCell(d);
556 // Now we need the other spels forming the mask
557 // Get the direction (positive) linel spans.
558 Point dir_parallel{0, 0, 0};
559 for (auto q = KPreSpace::uDirs(face1); q != 0; ++q) {
560 const Dimension dir = *q;
561 for (Dimension i = 0; i != ks.DIM; ++i)
565 // Note that C, B are interchangeable. Same in A,D. Same between X and Y
566 // sets Changed notation from paper: XA=X0, XB=X1, XC=X2, XD=X3
568 const Point xa{a + 2 * dir_parallel};
569 const Point xb{b + 2 * dir_parallel};
570 const Point xc{c + 2 * dir_parallel};
571 const Point xd{d + 2 * dir_parallel};
573 const Point ya{a - 2 * dir_parallel};
574 const Point yb{b - 2 * dir_parallel};
575 const Point yc{c - 2 * dir_parallel};
576 const Point yd{d - 2 * dir_parallel};
578 // Cell of the mask from KCoords
579 const PreCell XA = KPreSpace::uCell(xa);
580 const PreCell XB = KPreSpace::uCell(xb);
581 const PreCell XC = KPreSpace::uCell(xc);
582 const PreCell XD = KPreSpace::uCell(xd);
584 const PreCell YA = KPreSpace::uCell(ya);
585 const PreCell YB = KPreSpace::uCell(yb);
586 const PreCell YC = KPreSpace::uCell(yc);
587 const PreCell YD = KPreSpace::uCell(yd);
589 /////////////////////////////////
590 // Critical Clique Conditions:
592 /** is_critical = ConditionI && ConditionII
594 * At least one the sets {A,D},{B,C} is subset of this complex
596 /** (ii) ConditionII = B1 OR B2
597 * B1) (U & *this != empty) AND (V & *this != empty)
598 * B2) (U & *this == empty) AND (V & *this == empty)
601 const bool A1{this->belongs(KSpace::DIM, A) &&
602 this->belongs(KSpace::DIM, D)};
603 const bool A2{this->belongs(KSpace::DIM, B) &&
604 this->belongs(KSpace::DIM, C)};
605 const bool conditionI{A1 || A2};
607 const bool u_not_empty{
608 this->belongs(KSpace::DIM, XA) || this->belongs(KSpace::DIM, XB) ||
609 this->belongs(KSpace::DIM, XC) || this->belongs(KSpace::DIM, XD)};
611 const bool v_not_empty{
612 this->belongs(KSpace::DIM, YA) || this->belongs(KSpace::DIM, YB) ||
613 this->belongs(KSpace::DIM, YC) || this->belongs(KSpace::DIM, YD)};
615 const bool B1{u_not_empty && v_not_empty};
616 const bool B2{!u_not_empty && !v_not_empty};
617 const bool conditionII{B1 || B2};
619 const bool is_critical{conditionI && conditionII};
622 trace.beginBlock(" K1 critical conditions ");
623 trace.info() << "input linel: " << face1 << std::endl;
624 trace.info() << "is_critical = " << is_critical
625 << " conditionI || condition II " << conditionI << " || "
626 << conditionII << std::endl;
627 trace.info() << " conditionI = " << conditionI << " = A1 || A2 : " << A1
628 << " || " << A2 << std::endl;
629 trace.info() << " conditionII = " << conditionII
630 << " = B1 || B2 : " << B1 << " || " << B2 << std::endl;
634 // out clique is the intersection between mask and object
636 if (this->belongs(KSpace::DIM, A))
637 k1.insert(ks.uCell(A));
638 if (this->belongs(KSpace::DIM, B))
639 k1.insert(ks.uCell(B));
640 if (this->belongs(KSpace::DIM, C))
641 k1.insert(ks.uCell(C));
642 if (this->belongs(KSpace::DIM, D))
643 k1.insert(ks.uCell(D));
644 return std::make_pair(is_critical, k1);
646 //---------------------------------------------------------------------------
648 template <typename TKSpace, typename TCellContainer>
649 std::pair<bool, typename DGtal::VoxelComplex<TKSpace, TCellContainer>::Clique>
650 DGtal::VoxelComplex<TKSpace, TCellContainer>::K_0(const Cell &face0,
653 const auto &ks = this->space();
654 ASSERT(ks.uDim(face0) == 0);
655 using KPreSpace = typename TKSpace::PreCellularGridSpace;
656 auto &kface = face0.preCell().coordinates;
657 const Point z{0, 0, 1};
658 const Point y{0, 1, 0};
659 const Point x{1, 0, 0};
661 const Point a{kface + x - y - z};
662 const Point b{kface - x - y - z};
663 const Point c{kface + x - y + z};
664 const Point d{kface - x - y + z};
666 const Point e{kface + x + y - z};
667 const Point f{kface - x + y - z};
668 const Point g{kface + x + y + z};
669 const Point h{kface - x + y + z};
671 const PreCell A{KPreSpace::uCell(a)};
672 const PreCell B{KPreSpace::uCell(b)};
673 const PreCell C{KPreSpace::uCell(c)};
674 const PreCell D{KPreSpace::uCell(d)};
676 const PreCell E{KPreSpace::uCell(e)};
677 const PreCell F{KPreSpace::uCell(f)};
678 const PreCell G{KPreSpace::uCell(g)};
679 const PreCell H{KPreSpace::uCell(h)};
681 /////////////////////////////////
682 // Critical Clique Conditions:
683 /** is_critical = B1 || B2 || B3 || B4
685 * B1 = isSubset{A, H}
686 * B2 = isSubset{B, G}
687 * B3 = isSubset{C, F}
688 * B4 = isSubset{D, E}
689 * @note that the subsets define the 4 longest diagonals between the 8
692 const bool bA = this->belongs(KSpace::DIM, A);
693 const bool bB = this->belongs(KSpace::DIM, B);
694 const bool bC = this->belongs(KSpace::DIM, C);
695 const bool bD = this->belongs(KSpace::DIM, D);
696 const bool bE = this->belongs(KSpace::DIM, E);
697 const bool bF = this->belongs(KSpace::DIM, F);
698 const bool bG = this->belongs(KSpace::DIM, G);
699 const bool bH = this->belongs(KSpace::DIM, H);
701 const bool B1{bA && bH};
702 const bool B2{bB && bG};
703 const bool B3{bC && bF};
704 const bool B4{bD && bE};
705 const bool is_critical{B1 || B2 || B3 || B4};
708 trace.beginBlock(" K0 critical conditions ");
709 trace.info() << "input pointel: " << face0 << std::endl;
710 trace.info() << "is_critical = B1 || B2 || B3 || B4 " << std::endl;
711 trace.info() << is_critical << " = " << B1 << " || " << B2 << " || "
712 << B3 << " || " << B4 << std::endl;
715 // out clique is the intersection between mask and object
718 k0_out.insert(ks.uCell(A));
720 k0_out.insert(ks.uCell(B));
722 k0_out.insert(ks.uCell(C));
724 k0_out.insert(ks.uCell(D));
726 k0_out.insert(ks.uCell(E));
728 k0_out.insert(ks.uCell(F));
730 k0_out.insert(ks.uCell(G));
732 k0_out.insert(ks.uCell(H));
734 return std::make_pair(is_critical, k0_out);
736 //---------------------------------------------------------------------------
738 template <typename TKSpace, typename TCellContainer>
739 std::pair<bool, typename DGtal::VoxelComplex<TKSpace, TCellContainer>::Clique>
740 DGtal::VoxelComplex<TKSpace, TCellContainer>::K_3(const Cell &voxel,
743 const auto &ks = this->space();
744 ASSERT(ks.uDim(voxel) == 3);
745 const bool is_critical = !isSimple(voxel);
748 trace.beginBlock(" K3 critical conditions ");
749 trace.info() << "input voxel: " << voxel << std::endl;
750 trace.info() << "is_critical = " << is_critical << std::endl;
755 clique.insertCell(voxel);
756 return std::make_pair(is_critical, clique);
758 //---------------------------------------------------------------------------
760 /* BUG workaround: MSVC compiler error C2244.
761 * It doesn't see the definition of these declarations (Moved to header)
762 template <typename TKSpace, typename TCellContainer>
764 typename DGtal::VoxelComplex<TKSpace,
765 TCellContainer>::CliqueContainer, DGtal::VoxelComplex<TKSpace,
766 TCellContainer>::dimension + 1
768 DGtal::VoxelComplex<TKSpace, TCellContainer>::criticalCliques(
769 const Parent & cubical,
773 template <typename TKSpace, typename TCellContainer>
775 typename DGtal::VoxelComplex<TKSpace,
776 TCellContainer>::CliqueContainer, DGtal::VoxelComplex<TKSpace,
777 TCellContainer>::dimension + 1
779 DGtal::VoxelComplex<TKSpace, TCellContainer>::criticalCliques(
783 //---------------------------------------------------------------------------
785 template <typename TKSpace, typename TCellContainer>
786 std::pair<bool, typename DGtal::VoxelComplex<TKSpace, TCellContainer>::Clique>
787 DGtal::VoxelComplex<TKSpace, TCellContainer>::criticalCliquePair(
788 const Dimension d, const CellMapConstIterator &cellMapIterator) const
790 const auto &it = cellMapIterator;
791 const auto &cell = it->first;
792 // auto &cell_data = it->second;
793 auto clique_p = std::make_pair(false, Clique(this->space()));
795 clique_p = K_0(cell);
797 clique_p = K_1(cell);
799 clique_p = K_2(cell);
801 clique_p = K_3(cell);
803 throw std::runtime_error("Wrong dimension: " + std::to_string(d));
807 //---------------------------------------------------------------------------
809 template <typename TKSpace, typename TCellContainer>
810 typename DGtal::VoxelComplex<TKSpace, TCellContainer>::CliqueContainer
811 DGtal::VoxelComplex<TKSpace, TCellContainer>::criticalCliquesForD(
812 const Dimension d, const Parent &cubical, bool verbose) const
814 #if defined(WITH_OPENMP) && !defined(WIN32)
815 ASSERT(dimension >= 0 && dimension <= 3);
816 CliqueContainer critical;
818 const auto nthreads = omp_get_num_procs();
819 omp_set_num_threads(nthreads);
820 std::vector<CliqueContainer> p_critical;
821 p_critical.resize(nthreads);
824 #pragma omp single nowait
825 {for (auto it = cubical.begin(d), itE = cubical.end(d); it != itE; ++it)
826 #pragma omp task firstprivate(it)
827 {auto clique_p = criticalCliquePair(d, it);
828 const auto &is_critical = clique_p.first;
829 const auto &clique = clique_p.second;
831 auto th = omp_get_thread_num();
833 p_critical[th].push_back(clique);
839 std::size_t total_size = 0;
840 for (const auto &sub : p_critical)
841 total_size += sub.size();
843 critical.reserve(total_size);
844 for (const auto &sub : p_critical)
845 critical.insert(critical.end(), sub.begin(), sub.end());
848 trace.info() << " d:" << d << " ncrit: " << critical.size();
853 ASSERT(dimension >= 0 && dimension <= 3);
854 CliqueContainer critical;
855 for (auto it = cubical.begin(d), itE = cubical.end(d); it != itE; ++it) {
856 const auto clique_p = criticalCliquePair(d, it);
857 auto &is_critical = clique_p.first;
858 auto &clique = clique_p.second;
860 critical.push_back(clique);
863 trace.info() << " d:" << d << " ncrit: " << critical.size();
868 //---------------------------------------------------------------------------
869 ///////////////////////////////////////////////////////////////////////////////
870 template <typename TKSpace, typename TCellContainer>
871 bool DGtal::VoxelComplex<TKSpace, TCellContainer>::isSimpleByThinning(
872 const Cell &input_spel) const
874 // x = input_spel ; X = VoxelComplex ~ occupancy of khalimsky space
875 // a) Get the neighborhood (voxels) of input_spel intersected
876 // with the voxel complex. -- N^{*}(x) intersection X --
877 ASSERT(this->space().uDim(input_spel) == 3);
878 const auto spels_out = this->properNeighborhoodVoxels(input_spel);
879 const auto &ks = this->space();
881 for (const auto &v : spels_out)
882 clique.insertCell(v);
884 // b) Apply a thinning on the result of a)
885 typename Parent::DefaultCellMapIteratorPriority default_priority;
886 bool clique_is_closed = true;
887 functions::collapse( clique, spels_out.begin(), spels_out.end(), default_priority, false /* spels_out is not closed */, clique_is_closed, false /*verbose*/);
888 // c) If the result is a single pointel, it is reducible
889 return clique.size() == 1;
893 template <typename TKSpace, typename TCellContainer>
894 bool DGtal::VoxelComplex<TKSpace, TCellContainer>::isSimple(
895 const Cell &input_cell) const
897 ASSERT(isSpel(input_cell) == true);
899 if (myIsTableLoaded) {
900 auto conf = functions::getSpelNeighborhoodConfigurationOccupancy<Self>(
901 *this, this->space().uCoords(input_cell), this->pointToMask());
902 return (*myTablePtr)[conf];
904 return isSimpleByThinning(input_cell);
906 //---------------------------------------------------------------------------
907 ///////////////////////////////////////////////////////////////////////////////
908 // Interface - public :
911 * Writes/Displays the object on an output stream.
912 * @param out the output stream where the object is written.
914 template <typename TKSpace, typename TCellContainer>
915 inline void DGtal::VoxelComplex<TKSpace, TCellContainer>::selfDisplay(
916 std::ostream &out) const
918 out << "[VoxelComplex dim=" << this->dim() << " chi=" << this->euler();
919 out << " isTableLoaded? " << ((isTableLoaded()) ? "True" : "False");
921 //---------------------------------------------------------------------------
924 * Checks the validity/consistency of the object.
925 * @return 'true' if the object is valid, 'false' otherwise.
927 template <typename TKSpace, typename TCellContainer>
929 DGtal::VoxelComplex<TKSpace, TCellContainer>::isValid() const
934 //-----------------------------------------------------------------------------
935 template <typename TKSpace, typename TCellContainer>
937 DGtal::VoxelComplex<TKSpace, TCellContainer>::className() const
939 return "VoxelComplex";
942 ///////////////////////////////////////////////////////////////////////////////
943 // Implementation of inline functions //
945 template <typename TKSpace, typename TCellContainer>
946 inline std::ostream &DGtal::
947 operator<<(std::ostream &out,
948 const VoxelComplex<TKSpace, TCellContainer> &object)
950 object.selfDisplay(out);
955 ///////////////////////////////////////////////////////////////////////////////