1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkCellInterfaceVisitor_h
19 #define itkCellInterfaceVisitor_h
20 
21 #include "itkLightObject.h"
22 #include "itkObjectFactory.h"
23 
24 namespace itk
25 {
26 // Forward reference of CellInterface because of circular #include dependencies
27 template<
28   typename TPixelType,
29   typename TCellTraits
30   >
31 class ITK_TEMPLATE_EXPORT CellInterface;
32 
33 /** \class CellInterfaceVisitor
34  *  \brief Abstract interface for a visitor class that can visit the
35  *         cells in a Mesh.
36  *
37  * Define the abstract interface for a visitor class that can visit the
38  * cells in a Mesh.  This follows the Visitor Design Pattern.   To make
39  * this class easier to use, the CellInterfaceVisitorImplementation is
40  * provided as a templated class to implement the pure virtual functions
41  * of CellInterfaceVisitor.
42  *
43  * \ingroup MeshAccess
44  * \ingroup ITKCommon
45  */
46 template<
47   typename TPixelType,
48   typename TCellTraits
49   >
50 class ITK_TEMPLATE_EXPORT CellInterfaceVisitor:public LightObject
51 {
52 public:
53   ITK_DISALLOW_COPY_AND_ASSIGN(CellInterfaceVisitor);
54 
55   /** Standard class type aliases. */
56   using Self = CellInterfaceVisitor;
57   using Superclass = LightObject;
58   using Pointer = SmartPointer< Self >;
59   using ConstPointer = SmartPointer< const Self >;
60   using CellIdentifier = typename TCellTraits::CellIdentifier;
61 
62   /** Run-time type information (and related methods). */
63   itkTypeMacro(CellInterfaceVisitor, LightObject);
64 
65   /** This method is called by each cell as it visits this visitor. */
66   virtual void VisitFromCell(CellIdentifier cellId, CellInterface< TPixelType,
67                                                                    TCellTraits > *) = 0;
68 
69   /**  Return the index of the CellTopology. */
70   virtual int GetCellTopologyId() = 0;
71 
72 protected:
73   CellInterfaceVisitor() = default;
74   ~CellInterfaceVisitor() override = default;
75 };
76 
77 /** \class CellInterfaceVisitorImplementation
78  *  \brief A template class used to implement a visitor object.
79  *
80  * A template class used to implement a visitor object.
81  *
82  * The Visitor implementation does the down cast to
83  * the specific cell type that is being visited.  After the
84  * cast, a member of the UserVisitor type called Visit is
85  * passed the exact cell type being visited.  To use this
86  * class, write a class that implements a function
87  * Visit(int id, CellTopology*).   Then, use that as the UserVisitor
88  * template parameter.
89  *
90  * Template parameters for CellInterfaceVisitorImplementation:
91  * TPixelType = see CellInterface
92  *
93  * TCellTraits = see CellInterface
94  *
95  * CellTopology = The specific type of cell that needs to be visited.
96  *
97  * UserVisitor = A user supplied class that implements the function
98  *               Visit(int id, CellTopology*)
99  *
100  * \ingroup MeshAccess
101  * \ingroup ITKCommon
102  */
103 template<
104   typename TPixelType,
105   typename TCellTraits,
106   typename CellTopology,
107   typename UserVisitor
108   >
109 class CellInterfaceVisitorImplementation:
110   public CellInterfaceVisitor< TPixelType, TCellTraits >, public UserVisitor
111 {
112 public:
113   ITK_DISALLOW_COPY_AND_ASSIGN(CellInterfaceVisitorImplementation);
114 
115   /** Standard class type aliases. */
116   using Self = CellInterfaceVisitorImplementation;
117   using Pointer = SmartPointer< Self >;
118   using CellIdentifier = typename TCellTraits::CellIdentifier;
119 
120   /** Method for creation through the object factory. */
121   itkNewMacro(Self);
122 
123   /** Run-time type information (and related methods). */
124   itkTypeMacro(CellInterfaceVisitorImplementation, LightObject);
125 
126   /** Call the static method GetTopologyId for the CellTopology type that
127    * we are templated over. */
GetCellTopologyId()128   int GetCellTopologyId() override { return CellTopology::GetTopologyId(); }
129 
130   /** Call the method Visit from the UserVisitor template parameter that
131    * this class inherits from.  I am my own gradpa... */
VisitFromCell(CellIdentifier cellId,CellInterface<TPixelType,TCellTraits> * c)132   void VisitFromCell(CellIdentifier cellId, CellInterface< TPixelType,
133                                                            TCellTraits > *c) override
134   {
135     this->UserVisitor::Visit(cellId, (CellTopology *)c);
136   }
137 
138 protected:
139   CellInterfaceVisitorImplementation() = default;
140   ~CellInterfaceVisitorImplementation() override = default;
141 };
142 } // end namespace itk
143 
144 #endif
145