NIREP

RelativeOverlap.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003 Program:   vtkINRIA3D
00004 Module:    $Id: RelativeOverlap.cxx,v 1.3 2009/08/03 18:57:38 hawle Exp $
00005 Language:  C++
00006 Author:    $Author: hawle $
00007 Date:      $Date: 2009/08/03 18:57:38 $
00008 Version:   $Revision: 1.3 $
00009 
00010 Copyright (c) 2007 INRIA - Asclepios Project. All rights reserved.
00011 See Copyright.txt for details.
00012 
00013 This software is distributed WITHOUT ANY WARRANTY; without even
00014 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015 PURPOSE.  See the above copyright notices for more information.
00016 
00017 =========================================================================*/
00018 #include "RelativeOverlap.h"
00019 
00020 #include "vtkImageData.h"
00021 #include "vtkInformation.h"
00022 #include "vtkInformationVector.h"
00023 #include "vtkObjectFactory.h"
00024 #include "vtkStreamingDemandDrivenPipeline.h"
00025 
00026 vtkCxxRevisionMacro (RelativeOverlap, "$Revision: 1.3 $");
00027 vtkStandardNewMacro (RelativeOverlap);
00028 
00029 RelativeOverlap::RelativeOverlap()
00030 {
00031   LookupTable=0;
00032   this->SetNumberOfInputPorts (2);
00033 }
00034 
00035 RelativeOverlap::~RelativeOverlap()
00036 {
00037   if( LookupTable )
00038     LookupTable->Delete();
00039 }
00040 
00041 //----------------------------------------------------------------------------
00042 void RelativeOverlap::SetImageInput(vtkImageData *in)
00043 {
00044   this->SetInput1(in);
00045 }
00046 
00047 //----------------------------------------------------------------------------
00048 void RelativeOverlap::SetMaskInput(vtkImageData *in) 
00049 {
00050   this->SetInput2(in);
00051 }
00052 
00053 //----------------------------------------------------------------------------
00054 // The output extent is the intersection.
00055 int RelativeOverlap::RequestInformation (
00056   vtkInformation * vtkNotUsed(request),
00057   vtkInformationVector **inputVector,
00058   vtkInformationVector *outputVector)
00059 {
00060   // get the info objects
00061   vtkInformation* outInfo = outputVector->GetInformationObject(0);
00062   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
00063   vtkInformation *inInfo2 = inputVector[1]->GetInformationObject(0);
00064 
00065   int ext[6], ext2[6], idx;
00066 
00067   inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),ext);
00068   inInfo2->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),ext2);
00069   for (idx = 0; idx < 3; ++idx)
00070     {
00071     if (ext2[idx*2] > ext[idx*2])
00072       {
00073       ext[idx*2] = ext2[idx*2];
00074       }
00075     if (ext2[idx*2+1] < ext[idx*2+1])
00076       {
00077       ext[idx*2+1] = ext2[idx*2+1];
00078       }
00079     }
00080   
00081   outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),ext,6);
00082 
00083   return 1;
00084 }
00085 
00086 
00087 void RelativeOverlap::PrintSelf(ostream& os, vtkIndent indent)
00088 {
00089   this->Superclass::PrintSelf(os,indent);
00090   if( LookupTable )
00091   {
00092     os << indent << "LookupTable: \n";
00093     os << indent << *LookupTable << endl;
00094   }  
00095 }
00096 
00097 
00098 
00099 
00100 //----------------------------------------------------------------------------
00101 // This templated function executes the filter for any type of data.
00102 template <class T>
00103 void vtkRelativeOverlapExecute(RelativeOverlap *self, int ext[6],
00104                            vtkImageData *in1Data, T *in1Ptr,
00105                            vtkImageData *in2Data, T *in2Ptr,
00106                            vtkImageData *outData, T *outPtr, int id)
00107 {
00108   int num0, num1, num2, numC, numM, pixSize;
00109   int idx0, idx1, idx2;
00110   vtkIdType in1Inc0, in1Inc1, in1Inc2;
00111   vtkIdType in2Inc0, in2Inc1, in2Inc2;
00112   vtkIdType outInc0, outInc1, outInc2;
00113   //double maskAlpha, oneMinusMaskAlpha;
00114   unsigned long count = 0;
00115   unsigned long target;
00116   
00117   numC = outData->GetNumberOfScalarComponents();
00118   pixSize = numC * sizeof(T);
00119   //maskAlpha = 0.5;
00120   //oneMinusMaskAlpha = 0.5;
00121 
00122   numM = in2Data->GetNumberOfScalarComponents();
00123   
00124   // Get information to march through data 
00125   in1Data->GetContinuousIncrements(ext, in1Inc0, in1Inc1, in1Inc2);
00126   in2Data->GetContinuousIncrements(ext, in2Inc0, in2Inc1, in2Inc2);
00127   outData->GetContinuousIncrements(ext, outInc0, outInc1, outInc2);
00128   num0 = ext[1] - ext[0] + 1;
00129   num1 = ext[3] - ext[2] + 1;
00130   num2 = ext[5] - ext[4] + 1;
00131 
00132   
00133   target = (unsigned long)(num2*num1/50.0);
00134   target++;
00135 
00136   // Loop through ouput pixels
00137   for (idx2 = 0; idx2 < num2; ++idx2)
00138   {
00139     for (idx1 = 0; !self->AbortExecute && idx1 < num1; ++idx1)
00140     {
00141       if (!id) 
00142       {
00143         if (!(count%target))
00144           self->UpdateProgress(count/(50.0*target));
00145         count++;
00146       }
00147       
00148       for (idx0 = 0; idx0 < num0; ++idx0)
00149       {
00150         //Figure out if in1Ptr equal in2Ptr, if they do then set
00151         //the output to 0 else figure out whether we need to set
00152         //the output to in1ptr or in2ptr.
00153         if(*in1Ptr == *in2Ptr)
00154         {
00155           *outPtr = 0;
00156         }
00157         else
00158         {
00159           //As long as in1Ptr does not equal 0 then we set the output
00160           //to in1Ptr.  Else we set the output to in2Ptr.
00161           if(*in1Ptr != 0)
00162           {
00163             *outPtr = *in1Ptr;
00164           }
00165           else
00166           {
00167             *outPtr = *in2Ptr;
00168           }
00169         }
00170         ++outPtr;
00171         ++in1Ptr;
00172         ++in2Ptr;
00173                
00174       }
00175       in1Ptr += in1Inc1;
00176       in2Ptr += in2Inc1;
00177       outPtr += outInc1;
00178     }
00179     in1Ptr += in1Inc2;
00180     in2Ptr += in2Inc2;
00181     outPtr += outInc2;
00182     }
00183   
00184 }
00185 
00186 
00187 
00188 
00189 
00190 
00191 
00192 //----------------------------------------------------------------------------
00193 // This method is passed a input and output Datas, and executes the filter
00194 // algorithm to fill the output from the inputs.
00195 // It just executes a switch statement to call the correct function for
00196 // the Datas data types.
00197 void RelativeOverlap::ThreadedRequestData(
00198   vtkInformation * vtkNotUsed( request ), 
00199   vtkInformationVector ** vtkNotUsed( inputVector ), 
00200   vtkInformationVector * vtkNotUsed( outputVector ),
00201   vtkImageData ***inData, 
00202   vtkImageData **outData,
00203   int outExt[6], int id)
00204 {
00205   void *inPtr1;
00206   void *inPtr2;
00207   void *outPtr;
00208   int *tExt;
00209 
00210 
00211   vtkImageData* mask = vtkImageData::SafeDownCast (inData[1][0]);
00212 
00213   if( !mask )
00214   {
00215     vtkErrorMacro("Mask is not set");
00216     return;
00217   }
00218   
00219   
00220   inPtr1 = inData[0][0]->GetScalarPointerForExtent(outExt);
00221   inPtr2 = inData[1][0]->GetScalarPointerForExtent(outExt);
00222   outPtr = outData[0]->GetScalarPointerForExtent(outExt);
00223 
00224   tExt = inData[1][0]->GetExtent();
00225   if (tExt[0] > outExt[0] || tExt[1] < outExt[1] || 
00226       tExt[2] > outExt[2] || tExt[3] < outExt[3] ||
00227       tExt[4] > outExt[4] || tExt[5] < outExt[5])
00228     {
00229     vtkErrorMacro("Mask extent not large enough");
00230     return;
00231     }
00232     
00233   if (inData[0][0]->GetScalarType() != outData[0]->GetScalarType() ||
00234       inData[0][0]->GetScalarType() != VTK_UNSIGNED_CHAR )
00235     {
00236       vtkErrorMacro(<< "Execute: image ScalarType (" 
00237                     << inData[0][0]->GetScalarType() << ") must match out ScalarType (" 
00238                     << outData[0]->GetScalarType() << "), and mask scalar type (" 
00239                     << inData[1][0]->GetScalarType() << ") must be unsigned char."
00240                     << "Number of input components: " << inData[0][0]->GetNumberOfScalarComponents());
00241     return;
00242     }
00243   
00244   switch (inData[0][0]->GetScalarType())
00245     {
00246     vtkTemplateMacro(
00247                      vtkRelativeOverlapExecute(this, outExt,
00248                                                   inData[0][0],(VTK_TT *)(inPtr1),
00249                                                   inData[1][0],(VTK_TT *)(inPtr2),
00250                                                   outData[0], (VTK_TT *)(outPtr),id));
00251         default:
00252           vtkErrorMacro(<< "Execute: Unknown ScalarType");
00253           return;
00254     }
00255 }
00256 
00257 
00258 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines