cmtkVolumeClipping.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2398 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkVolumeClipping.h"
00034 
00035 #include <Base/cmtkMathUtil.h>
00036 
00037 #include <string.h>
00038 
00039 namespace
00040 cmtk
00041 {
00042 
00045 
00046 void 
00047 VolumeClipping::SetClippingBoundaries
00048 (  const UniformVolume::CoordinateRegionType& region )
00049 {
00050   this->m_ClippingRegion = region;
00051 }
00052 
00053 int VolumeClipping::ClipX 
00054 ( Types::Coordinate& fromFactor, Types::Coordinate& toFactor,
00055   const Vector3D& offset, 
00056   const Types::Coordinate initFromFactor, const Types::Coordinate initToFactor,
00057   const bool lowerClosed, const bool upperClosed ) const
00058 {
00059   fromFactor = initFromFactor;
00060   toFactor = initToFactor;
00061   
00062   for ( int dim=0; dim<3; ++dim ) 
00063     {
00064     if ( DeltaX[dim] > 0 ) 
00065       {
00066       fromFactor = std::max( fromFactor, (this->m_ClippingRegion.From()[dim] - offset[dim]) / DeltaX[dim] );
00067       toFactor = std::min( toFactor, (this->m_ClippingRegion.To()[dim] - offset[dim]) / DeltaX[dim] );
00068       } 
00069     else
00070       if ( DeltaX[dim] < 0 ) 
00071         {
00072         fromFactor = std::max( fromFactor, (this->m_ClippingRegion.To()[dim] - offset[dim]) / DeltaX[dim] );
00073         toFactor = std::min( toFactor, (this->m_ClippingRegion.From()[dim]-offset[dim]) / DeltaX[dim] );
00074         } 
00075       else
00076         {
00077         if ( (offset[dim] < this->m_ClippingRegion.From()[dim]) || 
00078              ( (offset[dim] == this->m_ClippingRegion.From()[dim] ) && lowerClosed ) ||
00079              (offset[dim] > this->m_ClippingRegion.To()[dim]) ||
00080              ( (offset[dim] == this->m_ClippingRegion.To()[dim]) && upperClosed ) ) {
00081         fromFactor = toFactor = 0;
00082         return 0;
00083         }
00084         }
00085     }
00086   return !( fromFactor > toFactor );
00087 }
00088 
00089 int VolumeClipping::ClipY
00090 ( Types::Coordinate& fromFactor, Types::Coordinate& toFactor,
00091   const Vector3D& offset, 
00092   const Types::Coordinate initFromFactor, const Types::Coordinate initToFactor ) const
00093 {
00094   fromFactor = initFromFactor;
00095   toFactor = initToFactor;
00096 
00097   for ( int dim=0; dim<3; ++dim ) 
00098     {
00099 #ifdef _MSC_VER
00100     const Types::Coordinate axmin = offset[dim] + std::min( (Types::Coordinate) 0, DeltaX[dim] );
00101     const Types::Coordinate axmax = offset[dim] + std::max( (Types::Coordinate) 0, DeltaX[dim] );
00102 #else
00103     const Types::Coordinate axmin = offset[dim] + std::min<Types::Coordinate>( 0, DeltaX[dim] );
00104     const Types::Coordinate axmax = offset[dim]+ std::max<Types::Coordinate>( 0, DeltaX[dim] );
00105 #endif
00106 
00107     if ( DeltaY[dim] > 0 ) 
00108       {
00109       fromFactor = std::max( fromFactor, ( this->m_ClippingRegion.From()[dim] - axmax) / DeltaY[dim] );
00110       toFactor = std::min( toFactor, ( this->m_ClippingRegion.To()[dim] - axmin) / DeltaY[dim] );
00111       } 
00112     else
00113       if ( DeltaY[dim] < 0 ) 
00114         {
00115         fromFactor = std::max( fromFactor, (this->m_ClippingRegion.To()[dim] - axmin) / DeltaY[dim] );
00116         toFactor = std::min( toFactor, (this->m_ClippingRegion.From()[dim] - axmax) / DeltaY[dim] );
00117         } 
00118       else
00119         {
00120         if ( (axmax < this->m_ClippingRegion.From()[dim]) || (axmin > this->m_ClippingRegion.To()[dim]) ) 
00121           {
00122           fromFactor = toFactor = 0;
00123           return 0;
00124           }
00125         }
00126     }
00127   return !( fromFactor > toFactor );
00128 }
00129 
00130 int VolumeClipping::ClipZ
00131 ( Types::Coordinate& fromFactor, Types::Coordinate& toFactor, 
00132   const Vector3D& offset, 
00133   const Types::Coordinate initFromFactor, const Types::Coordinate initToFactor ) const
00134 {
00135   fromFactor = initFromFactor;
00136   toFactor = initToFactor;
00137 
00138   for ( int dim=0; dim<3; ++dim ) 
00139     {
00140 #ifdef _MSC_VER
00141     const Types::Coordinate axbymin = offset[dim] + std::min( (Types::Coordinate) 0, DeltaX[dim] ) + std::min( (Types::Coordinate) 0, DeltaY[dim] );
00142     const Types::Coordinate axbymax = offset[dim] + std::max( (Types::Coordinate) 0, DeltaX[dim] ) + std::max( (Types::Coordinate) 0, DeltaY[dim] );
00143 #else
00144     const Types::Coordinate axbymin = offset[dim] + std::min<Types::Coordinate>( 0, DeltaX[dim] ) + std::min<Types::Coordinate>( 0, DeltaY[dim] );
00145     const Types::Coordinate axbymax = offset[dim] + std::max<Types::Coordinate>( 0, DeltaX[dim] ) + std::max<Types::Coordinate>( 0, DeltaY[dim] );
00146 #endif
00147 
00148     if ( DeltaZ[dim] > 0 ) 
00149       {
00150       fromFactor = std::max( fromFactor, (this->m_ClippingRegion.From()[dim] - axbymax) / DeltaZ[dim] );
00151       toFactor = std::min( toFactor, (this->m_ClippingRegion.To()[dim] - axbymin) / DeltaZ[dim] );
00152       } 
00153     else
00154       if ( DeltaZ[dim] < 0 ) 
00155         {
00156         fromFactor = std::max( fromFactor, (this->m_ClippingRegion.To()[dim] - axbymin) / DeltaZ[dim] );
00157         toFactor = std::min( toFactor, (this->m_ClippingRegion.From()[dim] - axbymax) / DeltaZ[dim] );
00158         } 
00159       else
00160         {
00161         if ( (axbymax < this->m_ClippingRegion.From()[dim]) || (axbymin > this->m_ClippingRegion.To()[dim]) ) 
00162           {
00163           fromFactor = toFactor = 0;
00164           return 0;
00165           }
00166         }
00167     }
00168   return !( fromFactor > toFactor );
00169 }
00170 
00171 }  // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines