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 233 234 235 236 237 238 239 240 241
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkQuadEdgeMeshNormalFilter.txx
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 __itkQuadEdgeMeshNormalFilter_txx
#define __itkQuadEdgeMeshNormalFilter_txx
#include "itkQuadEdgeMeshNormalFilter.h"
namespace itk
{
template< class TInputMesh, class TOutputMesh >
QuadEdgeMeshNormalFilter< TInputMesh, TOutputMesh >
::QuadEdgeMeshNormalFilter( )
{
this->m_Weight = THURMER;
}
template< class TInputMesh, class TOutputMesh >
QuadEdgeMeshNormalFilter< TInputMesh, TOutputMesh >
::~QuadEdgeMeshNormalFilter( )
{
}
template< class TInputMesh, class TOutputMesh >
typename QuadEdgeMeshNormalFilter< TInputMesh, TOutputMesh >::OutputFaceNormalType
QuadEdgeMeshNormalFilter< TInputMesh, TOutputMesh >
::ComputeFaceNormal( OutputPolygonType* iPoly )
{
OutputMeshPointer output = this->GetOutput( );
OutputPointType pt[3];
int k( 0 );
OutputQEType* edge = iPoly->GetEdgeRingEntry( );
OutputQEType* temp = edge;
do
{
pt[k++] = output->GetPoint( temp->GetOrigin( ) );
temp = temp->GetLnext( );
} while( temp != edge );
return TriangleType::ComputeNormal( pt[0], pt[1], pt[2] );
}
template< class TInputMesh, class TOutputMesh >
void
QuadEdgeMeshNormalFilter< TInputMesh, TOutputMesh >
::ComputeAllFaceNormals( )
{
OutputMeshPointer output = this->GetOutput( );
OutputPolygonType* poly;
for( OutputCellsContainerConstIterator
cell_it = output->GetCells( )->Begin( );
cell_it != output->GetCells( )->End( );
++cell_it )
{
poly = dynamic_cast< OutputPolygonType* >( cell_it.Value( ) );
if( poly != 0 )
{
if( poly->GetNumberOfPoints( ) == 3 )
{
output->SetCellData( cell_it->Index( ), ComputeFaceNormal( poly ) );
}
}
}
}
template< class TInputMesh, class TOutputMesh >
void
QuadEdgeMeshNormalFilter< TInputMesh, TOutputMesh >
::ComputeAllVertexNormals( )
{
OutputMeshPointer output = this->GetOutput( );
OutputPointsContainerPointer points = output->GetPoints( );
OutputPointIdentifier id;
for( OutputPointsContainerIterator it = points->Begin( );
it != points->End( );
++it )
{
id = it->Index( );
output->SetPointData( id, ComputeVertexNormal( id ) );
}
}
template< class TInputMesh, class TOutputMesh >
typename QuadEdgeMeshNormalFilter< TInputMesh, TOutputMesh >::OutputVertexNormalType
QuadEdgeMeshNormalFilter< TInputMesh, TOutputMesh >
::ComputeVertexNormal( const OutputPointIdentifier& iId )
{
OutputMeshPointer output = this->GetOutput( );
OutputQEType* edge = output->FindEdge( iId );
OutputQEType* temp = edge;
OutputCellIdentifier cell_id( 0 );
OutputVertexNormalType n( 0. );
OutputFaceNormalType face_normal( 0. );
do
{
cell_id = temp->GetLeft( );
if( cell_id != OutputMeshType::m_NoFace )
{
output->GetCellData( cell_id, &face_normal );
n += face_normal * Weight( iId, cell_id );
}
temp = temp->GetOnext( );
} while( temp != edge );
n.Normalize( );
return n;
}
template< class TInputMesh, class TOutputMesh >
typename QuadEdgeMeshNormalFilter< TInputMesh, TOutputMesh >::OutputVertexNormalComponentType
QuadEdgeMeshNormalFilter< TInputMesh, TOutputMesh >
::Weight( const OutputPointIdentifier& iPId, const OutputCellIdentifier& iCId )
{
if( m_Weight == GOURAUD )
{
return static_cast< OutputVertexNormalComponentType >( 1.0 );
}
else
{
OutputMeshPointer output = this->GetOutput( );
OutputPolygonType* poly = dynamic_cast< OutputPolygonType* >(
output->GetCells( )->GetElement( iCId ) );
if( poly != 0 ) // this test should be removed...
{
// this test should be removed...
if( poly->GetNumberOfPoints( ) == 3 )
{
int internal_id( 0 ), k( 0 );
OutputPointType pt[3];
OutputVectorType u, v;
OutputQEType* edge = poly->GetEdgeRingEntry( );
OutputQEType* temp = edge;
do
{
pt[k] = output->GetPoint( temp->GetOrigin( ) );
if( temp->GetOrigin( ) == iPId )
internal_id = k;
temp = temp->GetLnext( );
k++;
} while( temp != edge );
switch( m_Weight )
{
default:
case GOURAUD:
{
return static_cast< OutputVertexNormalComponentType >( 1. );
}
case THURMER:
{
// this implementation may be included inside itkTriangle
switch( internal_id )
{
case 0:
u = pt[1] - pt[0];
v = pt[2] - pt[0];
break;
case 1:
u = pt[0] - pt[1];
v = pt[2] - pt[1];
break;
case 2:
u = pt[0] - pt[2];
v = pt[1] - pt[2];
break;
}
typename OutputVectorType::RealValueType norm2_u = u.GetSquaredNorm();
if( norm2_u > vnl_math::eps )
{
norm2_u = 1. / norm2_u;
u *= norm2_u;
}
typename OutputVectorType::RealValueType norm2_v = v.GetSquaredNorm();
if( norm2_v > vnl_math::eps )
{
norm2_v = 1. / norm2_v;
v *= norm2_v;
}
return static_cast< OutputVertexNormalComponentType >(
vcl_acos( u * v ) );
}
case AREA:
{
return static_cast< OutputVertexNormalComponentType >(
TriangleType::ComputeArea( pt[0], pt[1], pt[2] ) );
}
}
}
else
{
std::cout <<"Input should be a triangular mesh!!!" <<std::endl;
return static_cast< OutputVertexNormalComponentType >( 0. );
}
}
else
{
return static_cast< OutputVertexNormalComponentType >( 0. );
}
}
}
template< class TInputMesh, class TOutputMesh >
void QuadEdgeMeshNormalFilter< TInputMesh, TOutputMesh >
::GenerateData( )
{
this->CopyInputMeshToOutputMesh( );
this->ComputeAllFaceNormals( );
this->ComputeAllVertexNormals( );
}
}
#endif
|