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 itkRegionConstrainedSubsampler_h
19 #define itkRegionConstrainedSubsampler_h
20 
21 #include "itkSubsamplerBase.h"
22 #include "itkImageRegion.h"
23 
24 namespace itk {
25 namespace Statistics {
26 /** \class RegionConstrainedSubsampler
27  * \brief This an abstract subsampler that constrains subsamples
28  * to be contained within a given image region.
29  *
30  * This is an Abstract class that can not be instantiated.
31  * There are multiple subsamplers that derive from this class and
32  * provide specific implementations of subsampling strategies.
33  *
34  * This class is templatized over both the Sample and the Region
35  * with an assumed consistency between the Region and the way
36  * the Sample is generated.
37  *
38  * Use SetRegionConstraint(region) to provide the region
39  * constraint.  All returned subsamples will be contained
40  * within this region.  This assumes that the instance
41  * identifiers in the sample correspond to an offset
42  * computed with the provided region.
43  *
44  * \sa SubsamplerBase, SpatialNeighborSubsampler
45  * \sa GaussianRandomSpatialNeighborSubsampler
46  * \sa UniformRandomSpatialNeighborSubsampler
47  * \ingroup ITKStatistics
48  */
49 
50 template < typename TSample, typename TRegion >
51 class ITK_TEMPLATE_EXPORT RegionConstrainedSubsampler : public SubsamplerBase<TSample>
52 {
53 public:
54   ITK_DISALLOW_COPY_AND_ASSIGN(RegionConstrainedSubsampler);
55 
56   /** Standard class type aliases */
57   using Self = RegionConstrainedSubsampler<TSample, TRegion>;
58   using Superclass = SubsamplerBase<TSample>;
59   using Baseclass = typename Superclass::Baseclass;
60   using Pointer = SmartPointer<Self>;
61   using ConstPointer = SmartPointer<const Self>;
62 
63   /** Run-time type information (and related methods) */
64   itkTypeMacro(RegionConstrainedSubsampler, SubsamplerBase);
65 
66   /** type alias alias for the source data container */
67   using SampleType = TSample;
68   using SampleConstPointer = typename SampleType::ConstPointer;
69   using MeasurementVectorType = typename TSample::MeasurementVectorType;
70   using InstanceIdentifier = typename TSample::InstanceIdentifier;
71 
72   using SubsampleType = Subsample<TSample>;
73   using SubsamplePointer = typename SubsampleType::Pointer;
74   using SubsampleConstIterator = typename SubsampleType::ConstIterator;
75   using InstanceIdentifierHolder = typename SubsampleType::InstanceIdentifierHolder;
76 
77   /** type alias related to image region */
78   using RegionType = TRegion;
79   using IndexType = typename RegionType::IndexType;
80   using IndexValueType = typename IndexType::IndexValueType;
81   using SizeType = typename RegionType::SizeType;
82 
83   /** Method to set the sample domain.
84    * This should correspond to the entire region of the input sample. */
85   void SetSampleRegion(const RegionType& region);
86 
87   /** Method to get the sample domain. */
88   itkGetConstReferenceMacro( SampleRegion, RegionType );
89 
90   /** Method to get the flag indicating that the sample region has been initialized */
91   itkGetConstReferenceMacro(SampleRegionInitialized, bool);
92 
93   /** Method to set the region constraint.
94    * Any subsamples selected must ALSO be inside this region. */
95   void SetRegionConstraint(const RegionType& region);
96 
97   /** Method to get the region constraint. */
98   itkGetConstReferenceMacro( RegionConstraint, RegionType );
99 
100   /** Method to get the flag indicating that the region constraint has been initialized */
101   itkGetConstReferenceMacro(RegionConstraintInitialized, bool);
102 
103   /** Main Search method that MUST be implemented by each subclass
104    * The Search method will find all points similar to query and return
105    * them as a Subsample.  The definition of similar will be subclass-
106    * specific.  And could mean spatial similarity or feature similarity
107    * etc.  */
108   void Search(const InstanceIdentifier& query,
109                       SubsamplePointer& results) override = 0;
110 
111 protected:
112   /**
113    * Clone the current subsampler.
114    * This does a complete copy of the subsampler state
115    * to the new subsampler
116    */
117   typename LightObject::Pointer InternalClone() const override;
118 
119   RegionConstrainedSubsampler();
120   ~RegionConstrainedSubsampler() override = default;
121 
122   void PrintSelf(std::ostream& os, Indent indent) const override;
123 
124   RegionType m_RegionConstraint;
125   bool       m_RegionConstraintInitialized;
126   RegionType m_SampleRegion;
127   bool       m_SampleRegionInitialized;
128 }; // end of class RegionConstrainedSubsampler
129 
130 } // end of namespace Statistics
131 } // end of namespace itk
132 
133 #ifndef ITK_MANUAL_INSTANTIATION
134 #include "itkRegionConstrainedSubsampler.hxx"
135 #endif
136 
137 #endif
138