cmtkStudy.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 "cmtkStudy.h"
00034 
00035 #include <IO/cmtkFileFormat.h>
00036 #include <IO/cmtkVolumeIO.h>
00037 #include <IO/cmtkClassStream.h>
00038 
00039 #include <string>
00040 #include <limits.h>
00041 
00042 namespace
00043 cmtk
00044 {
00045 
00048 
00049 Study::Study()
00050   : m_FileSystemPath( NULL ),
00051     m_Name( NULL ),
00052     m_Description( NULL ),
00053     m_Modality( NULL ),
00054     m_Volume( NULL ),
00055     m_MinimumValue( 0.0 ),
00056     m_MaximumValue( 0.0 ),
00057     m_Padding( false ),
00058     m_HaveUserColorMap( false ),
00059     m_Black( 0.0 ),
00060     m_White( 0.0 )
00061 {
00062 }
00063 
00064 Study::Study( const char* fileSystemPath, const char *name )
00065   : m_FileSystemPath( NULL ),
00066     m_Name( NULL ),
00067     m_Description( NULL ),
00068     m_Modality( NULL ),
00069     m_Volume( NULL ),
00070     m_MinimumValue( 0.0 ),
00071     m_MaximumValue( 0.0 ),
00072     m_Padding( false ),
00073     m_HaveUserColorMap( false ),
00074     m_Black( 0.0 ),
00075     m_White( 0.0 )
00076 {
00077   if ( fileSystemPath ) 
00078     {
00079     this->m_FileSystemPath = strdup( fileSystemPath );
00080     this->m_Description = strdup( FileFormat::Describe( this->m_FileSystemPath ) );
00081 
00082     // cut trailing '/'s off the study path.
00083     char *endp = this->m_FileSystemPath + strlen( this->m_FileSystemPath ) - 1;
00084     while ( endp > this->m_FileSystemPath ) 
00085       {
00086       if ( *endp == '/' ) 
00087         *endp = 0;
00088       else
00089         break;
00090       }
00091     
00092     this->SetMakeName( name );
00093   }
00094 }
00095 
00096 void
00097 Study::UpdateFromVolume()
00098 {
00099   const TypedArray *dataArray = this->m_Volume->GetData();
00100   if ( dataArray ) 
00101     {
00102     const Types::DataItemRange range = dataArray->GetRange();
00103     this->m_MinimumValue = range.m_LowerBound;
00104     this->m_MaximumValue = range.m_UpperBound;
00105 
00106     const Types::DataItem perc01 = dataArray->GetPercentile( 0.01, 1024 );
00107     const Types::DataItem perc99 = dataArray->GetPercentile( 0.99, 1024 );
00108     
00109     this->m_Black = std::min( std::max( this->m_Black, perc01 ), this->m_MaximumValue );
00110     this->m_White = std::max( std::min( this->m_White, perc99 ), this->m_MinimumValue );
00111     }
00112 }
00113 
00114 const char*
00115 Study::SetMakeName( const char* name, const int suffix )
00116 {
00117   if ( name )
00118     {
00119     if ( suffix )
00120       {
00121       char fullname[PATH_MAX];
00122       snprintf( fullname, sizeof( fullname ), "%s <%d>", name, suffix );
00123       this->SetName( fullname );
00124       }
00125     else
00126       {
00127       this->SetName( name );
00128       }
00129     }
00130   else
00131     {
00132     char buffer[PATH_MAX];
00133     strncpy( buffer, this->m_FileSystemPath, PATH_MAX );
00134     
00135     char* lastChar = buffer + strlen( buffer ) - 1;
00136     while ( (lastChar != buffer) && // not yet empty string
00137             (*lastChar =='/') ) 
00138       {
00139       *lastChar = 0;
00140       --lastChar;
00141       }
00142     
00143     const char *slash = strrchr( buffer, '/' );
00144     if ( slash )
00145       strcpy( buffer, slash+1 );
00146     else 
00147       strcpy( buffer, this->m_FileSystemPath );
00148     
00149     char* dot = strchr( buffer, '.' );
00150     if ( dot )
00151       *dot = 0;
00152     else
00153       dot = buffer + strlen(buffer);
00154     
00155     if ( suffix ) 
00156       {
00157       snprintf( dot, sizeof( buffer ) - (dot-buffer), "<%d>", suffix );
00158       }
00159 
00160     this->SetName( buffer );
00161     }
00162 
00163   return this->m_Name;
00164 }
00165 
00166 Study::~Study() 
00167 {
00168   free( this->m_FileSystemPath );
00169   free( this->m_Description );
00170   free( this->m_Name );
00171 }
00172 
00173 bool 
00174 Study::ReadVolume( const bool reRead, const char* orientation )
00175 {
00176   UniformVolume::SmartPtr oldVolume( NULL );
00177 
00178   if ( this->m_Volume && reRead ) 
00179     {
00180     oldVolume = this->m_Volume;
00181     this->m_Volume = UniformVolume::SmartPtr( NULL );
00182     }
00183   
00184   if ( !this->m_Volume ) 
00185     {
00186     if ( orientation )
00187       this->m_Volume = UniformVolume::SmartPtr( VolumeIO::ReadOriented( this->m_FileSystemPath, orientation ) );
00188     else
00189       this->m_Volume = UniformVolume::SmartPtr( VolumeIO::Read( this->m_FileSystemPath ) );
00190     
00191     if ( this->m_Volume ) 
00192       {
00193       this->m_Dims = this->m_Volume->GetDims();
00194       this->m_DisplayedImageIndex = this->m_Dims[AXIS_Z] / 2 ;
00195       this->m_ZoomFactor = 1;
00196       const TypedArray *dataArray = this->m_Volume->GetData();
00197       if ( dataArray ) 
00198         {
00199         const Types::DataItemRange range = dataArray->GetRange();
00200         this->m_MinimumValue = range.m_LowerBound;
00201         this->m_MaximumValue = range.m_UpperBound;
00202 
00203         this->m_Black = dataArray->GetPercentile( 0.01, 1024 );
00204         this->m_White = dataArray->GetPercentile( 0.99, 1024 );
00205 
00206         this->m_StandardColormap = 0;
00207         this->m_ReverseColormap = false;
00208         }
00209       }
00210     }
00211   
00212   if ( this->m_Volume ) 
00213     {
00214     if ( this->m_LandmarkList ) 
00215       {
00216       this->m_Volume->m_LandmarkList = this->m_LandmarkList;
00217       }
00218     if ( this->m_Volume->GetData() ) 
00219       {
00220       return true;
00221       }
00222     }
00223   
00224   this->m_Volume = oldVolume;
00225   return false;
00226 }
00227 
00228 Study* 
00229 Study::Read( const char* path )
00230 {
00231   return new Study( path );
00232 }
00233 
00234 void
00235 Study::CopyColormap( const Study* other )
00236 {
00237   this->m_MinimumValue = other->m_MinimumValue;
00238   this->m_MaximumValue = other->m_MaximumValue;
00239   this->m_StandardColormap = other->m_StandardColormap;
00240   this->m_ReverseColormap = other->m_ReverseColormap;
00241   this->m_Black = other->m_Black;
00242   this->m_White = other->m_White;
00243   this->m_Gamma = other->m_Gamma;
00244 }
00245 
00246 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines