File: itkQuadEdgeMeshCleanFilter.h

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,652 kB
  • sloc: cpp: 458,133; ansic: 196,223; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 430; csh: 220; perl: 193; xml: 20
file content (232 lines) | stat: -rw-r--r-- 8,652 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkQuadEdgeMeshCleanFilter.h
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#ifndef __itkQuadEdgeMeshCleanFilter_h
#define __itkQuadEdgeMeshCleanFilter_h

#include "itkQuadEdgeMeshToQuadEdgeMeshFilter.h"
#include "itkBoundingBox.h"

#include "itkQuadEdgeMeshSquaredEdgeLengthDecimation.h"
#include "itkQuadEdgeMeshDecimationCriteria.h"

namespace itk
{
/**
 * \class QuadEdgeMeshCleanFilter
 * \brief
 */
template< class TInput, class TOutput >
class QuadEdgeMeshCleanFilter :
  public QuadEdgeMeshToQuadEdgeMeshFilter< TInput, TOutput >
{
public:
  typedef QuadEdgeMeshCleanFilter                                 Self;
  typedef SmartPointer< Self >                                    Pointer;
  typedef SmartPointer< const Self >                              ConstPointer;
  typedef QuadEdgeMeshToQuadEdgeMeshFilter< TInput, TOutput >     Superclass;

  /** Run-time type information (and related methods).   */
  itkTypeMacro( QuadEdgeMeshCleanFilter, QuadEdgeMeshToQuadEdgeMeshFilter );

  /** New macro for creation of through a Smart Pointer   */
  itkNewMacro( Self );

  typedef TInput                                                InputMeshType;
  typedef typename Superclass::InputMeshPointer                 InputMeshPointer;
  typedef typename Superclass::InputCoordRepType                InputCoordRepType;
  typedef typename Superclass::InputPointType                   InputPointType;
  typedef typename Superclass::InputPointIdentifier             InputPointIdentifier;
  typedef typename Superclass::InputQEPrimal                    InputQEPrimal;
  typedef typename Superclass::InputVectorType                  InputVectorType;

  typedef typename Superclass::InputEdgeCellType                InputEdgeCellType;
  typedef typename Superclass::InputPolygonCellType             InputPolygonCellType;
  typedef typename Superclass::InputPointIdList                 InputPointIdList;
  typedef typename Superclass::InputCellTraits                  InputCellTraits;
  typedef typename Superclass::InputPointsIdInternalIterator    InputPointsIdInternalIterator;
  typedef typename Superclass::InputQEIterator                  InputQEIterator;

  typedef typename InputMeshType::PointsContainer               InputPointsContainer;
  typedef typename InputMeshType::PointsContainerPointer        InputPointsContainerPointer;
  typedef typename InputMeshType::PointsContainerIterator       InputPointsContainerIterator;

  typedef typename InputMeshType::CellsContainerIterator        InputCellsContainerIterator;

  itkStaticConstMacro( PointDimension, unsigned int, InputMeshType::PointDimension );

  typedef TOutput                                               OutputMeshType;
  typedef typename Superclass::OutputMeshPointer                OutputMeshPointer;
  typedef typename Superclass::OutputCoordRepType               OutputCoordRepType;
  typedef typename Superclass::OutputPointType                  OutputPointType;
  typedef typename Superclass::OutputPointIdentifier            OutputPointIdentifier;
  typedef typename Superclass::OutputQEPrimal                   OutputQEPrimal;
  typedef typename Superclass::OutputVectorType                 OutputVectorType;

  typedef typename OutputMeshType::QEType                       OutputQEType;
  typedef typename OutputMeshType::PointsContainer              OutputPointsContainer;
  typedef typename OutputMeshType::PointsContainerPointer       OutputPointsContainerPointer;
  typedef typename OutputMeshType::PointsContainerIterator      OutputPointsContainerIterator;

  typedef typename OutputMeshType::CellsContainerIterator      OutputCellsContainerIterator;

  typedef BoundingBox< InputPointIdentifier, itkGetStaticConstMacro(PointDimension),
    InputCoordRepType, InputPointsContainer > BoundingBoxType;

  typedef typename BoundingBoxType::Pointer                 BoundingBoxPointer;

  typedef MaxMeasureBoundCriterion< OutputMeshType >        CriterionType;
  typedef typename CriterionType::Pointer                   CriterionPointer;

  typedef QuadEdgeMeshSquaredEdgeLengthDecimation< InputMeshType,
    InputMeshType, CriterionType >                          DecimationType;
  typedef typename DecimationType::Pointer                  DecimationPointer;

  itkSetMacro( AbsoluteTolerance, InputCoordRepType );
  itkSetMacro( RelativeTolerance, InputCoordRepType );

protected:
  QuadEdgeMeshCleanFilter()
    {
    this->m_AbsoluteTolerance2 = itk::NumericTraits< InputCoordRepType >::Zero;
    this->m_AbsoluteTolerance  = itk::NumericTraits< InputCoordRepType >::Zero;
    this->m_RelativeTolerance  = itk::NumericTraits< InputCoordRepType >::Zero;
    }

  virtual ~QuadEdgeMeshCleanFilter() {}

  InputCoordRepType m_AbsoluteTolerance2;
  InputCoordRepType m_AbsoluteTolerance;
  InputCoordRepType m_RelativeTolerance;

  void GenerateData()
    {
    InputCoordRepType zeroValue = itk::NumericTraits< InputCoordRepType >::Zero;

    if( ( m_AbsoluteTolerance == zeroValue ) && ( m_RelativeTolerance != zeroValue ) )
      {
      itkAssertOrThrowMacro( ( m_RelativeTolerance > zeroValue ) && ( m_RelativeTolerance < 1. ),
        "Relative tolerance out of range" );
      BoundingBoxPointer bounding_box = BoundingBoxType::New();
      bounding_box->SetPoints( this->GetInput()->GetPoints() );
      bounding_box->ComputeBoundingBox();

      m_AbsoluteTolerance2 = m_RelativeTolerance * m_RelativeTolerance *
        bounding_box->GetDiagonalLength2();
      }

    if( m_AbsoluteTolerance != zeroValue )
      {
      m_AbsoluteTolerance2 = m_AbsoluteTolerance * m_AbsoluteTolerance;
      }

    this->MergePoints();
    this->CleanPoints();
    }

  void MergePoints()
    {
    OutputMeshPointer output = this->GetOutput();

    CriterionPointer criterion = CriterionType::New();
    criterion->SetTopologicalChange( false );
    criterion->SetMeasureBound( m_AbsoluteTolerance2 );

    DecimationPointer decimate = DecimationType::New();
    decimate->SetInput( this->GetInput() );
    decimate->SetCriterion( criterion );
    decimate->Update();

    InputMeshPointer temp = decimate->GetOutput();

    InputPointsContainerIterator p_it = temp->GetPoints()->Begin();
    InputPointsContainerIterator p_end = temp->GetPoints()->End();

    OutputPointType pOut;

    while( p_it != p_end )
      {
      pOut.CastFrom( p_it.Value() );
      output->SetPoint( p_it.Index(), pOut );
      ++p_it;
      }

    // Copy Edge Cells
    InputCellsContainerIterator  c_it = temp->GetEdgeCells()->Begin();
    InputCellsContainerIterator  c_end = temp->GetEdgeCells()->End();
    InputEdgeCellType* qe;
    InputQEPrimal* QEGeom;

    while( c_it != c_end )
      {
      qe = dynamic_cast< InputEdgeCellType* >( c_it.Value());
      QEGeom = qe->GetQEGeom( );
      output->AddEdgeWithSecurePointList( QEGeom->GetOrigin(),
                                          QEGeom->GetDestination() );
      ++c_it;
      }

    // Copy cells
    c_it = temp->GetCells()->Begin();
    c_end = temp->GetCells()->End();
    InputPolygonCellType* pe;

    while( c_it != c_end )
      {
      pe = dynamic_cast< InputPolygonCellType* >( c_it.Value());
      if( pe )
        {
        InputPointIdList points;

        for( InputPointsIdInternalIterator pit = pe->InternalPointIdsBegin();
             pit != pe->InternalPointIdsEnd( ); ++pit )
          {
          points.push_back( ( *pit ) );
          }
        output->AddFaceWithSecurePointList( points );
        }
      ++c_it;
      }
    }

  void CleanPoints()
    {
    OutputMeshPointer output = this->GetOutput();

    OutputPointsContainerIterator p_it = output->GetPoints()->Begin();
    OutputPointsContainerIterator p_end = output->GetPoints()->End();
    OutputPointIdentifier id( 0 );

    while( p_it != p_end )
      {
      id = p_it->Index();
      if( output->FindEdge( id ) == 0 )
        {
        output->DeletePoint( id );
        }
      ++p_it;
      }

    output->SqueezePointsIds( );
    }

private:
  QuadEdgeMeshCleanFilter( const Self& );
  void operator = ( const Self& );
};
}
#endif