Filter Library Camera Interface Physics

ScanBaseBuffer.h

00001 /*
00002  * CFiltLib/ScanBaseBuffer.h - scanbuffer base class.
00003  *
00004  * This file is part of the Camera Filter Library.
00005  * Computer Aided Measurement Environment for Realtime Atomic imaging (Camera)
00006  *
00007  * Copyright (C) 2004, Leiden Probe Microscopy.
00008  * Copyright (C) 2004, Universiteit Leiden.
00009  *
00010  * Authors: M. Seynen (original), Martin J. Moene
00011  *
00012  * $Id: ScanBaseBuffer.h 447 2006-12-13 12:58:11Z moene $
00013  */
00014 
00015 #ifndef CFILTLIB_SCANBASEBUFFER_H
00016 #define CFILTLIB_SCANBASEBUFFER_H
00017 
00018 #if _MSC_VER > 1000
00019 #pragma once
00020 #endif // _MSC_VER > 1000
00021 
00022 #ifdef _AFXEXT
00023    //#pragma message("CFiltLib DLL export" )
00024    #define CLASS_DECL  _declspec(dllexport)
00025 #else
00026    //#pragma message("CFiltLib DLL import" )
00027    #define CLASS_DECL  _declspec(dllimport)
00028 #endif
00029 
00030 /**
00031  * the basic scan base buffer.
00032  * \ingroup cfl_scanbasebuffer
00033  */
00034 
00035 class CLASS_DECL CScanBaseBuffer : public CObject
00036 {
00037 public:
00038    /**
00039     * the parent class.
00040     */
00041    typedef CObject Super;
00042 
00043    /**
00044     * this type.
00045     */
00046    typedef CScanBaseBuffer Self;
00047 
00048    /**
00049     * the buffer's value type.
00050     */
00051    typedef short ValueType;
00052 
00053    /**
00054     * the buffer's pointer-to value type.
00055     */
00056    typedef ValueType* Pointer;
00057 
00058    /**
00059     * the buffer's const pointer-to value type.
00060     */
00061    typedef const ValueType* ConstPointer;
00062 
00063    /**
00064     * the buffer's real type.
00065     */
00066    typedef double RealType;
00067 
00068    /**
00069     * the forier buffer's data type.
00070     */
00071    typedef RealType FourierElementType;
00072 
00073    /**
00074     * the buffer's size type.
00075     */
00076    typedef DWORD SizeType;
00077 
00078    /**
00079     * the buffer's flags type.
00080     */
00081    typedef DWORD FlagType;
00082 
00083    /**
00084     * the buffer's mask type.
00085     */
00086    typedef DWORD MaskType;
00087 
00088    /**
00089     * the x, y and z spacing type.
00090     */
00091    typedef RealType SpacingType;
00092 
00093    /**
00094     * the x, y and z length type.
00095     */
00096    typedef RealType LengthType;
00097 
00098    ///
00099    /// \name Construction
00100    /// @{
00101 
00102    /**
00103     * destructor.
00104     */
00105    virtual ~CScanBaseBuffer();
00106 
00107    /**
00108     * default constuctor.
00109     */
00110    CScanBaseBuffer();
00111 
00112    /// @}
00113    /// \name Predicates
00114    /// @{
00115 
00116    /**
00117     * true if buffer contains a left--right scan.
00118     */
00119    bool HasLeftToRightScan() const;
00120 
00121    /**
00122     * true if buffer contains a right--left scan.
00123     */
00124    bool HasRightToLeftScan() const;
00125 
00126    /**
00127     * true if buffer contains both a left--right and a right--left scan.
00128     */
00129    bool IsBidirectionalScan() const;
00130 
00131    /**
00132     * true if all pixels of the frame are present.
00133     */
00134    bool IsCompleteFrame() const;
00135 
00136    /**
00137     * true if this buffer has not yet been used.
00138     */
00139    bool IsNewBuffer() const;
00140 
00141    /**
00142     * true if data not yet loaded.
00143     */
00144    bool IsEmptyBuffer() const;
00145 
00146    /**
00147     * true if data present in buffer.
00148     */
00149    bool IsLoadedBuffer() const;
00150 
00151    /**
00152     * true if data in buffer has been modified.
00153     */
00154    bool IsModifiedBuffer() const;
00155 
00156    /**
00157     * true if buffer is used for in-memory filtering.
00158     */
00159    bool IsInMemoryBuffer() const;
00160 
00161    /// @}
00162    /// \name Accessors
00163    /// @{
00164 
00165    /**
00166     * X-size in pixels.
00167     */
00168    SizeType Columns() const;
00169 
00170    /**
00171     * Y-size in pixels.
00172     */
00173    SizeType Rows() const;
00174 
00175    /**
00176     * the buffer data (non-const).
00177     */
00178    Pointer Data();
00179 
00180    /**
00181     * the buffer data (const).
00182     */
00183    ConstPointer Data() const;
00184 
00185    /**
00186     * the fourier left-right data (non-const).
00187     */
00188    FourierElementType** FourierDataL2R();
00189 
00190    /**
00191     * the fourier left-right data (const).
00192     */
00193    FourierElementType const* const* FourierDataL2R() const;
00194 
00195    /**
00196     * the fourier right-left data (non-const).
00197     */
00198    FourierElementType** FourierDataR2L();
00199 
00200    /**
00201     * the fourier right-left data (const).
00202     */
00203    FourierElementType const* const* FourierDataR2L() const;
00204 
00205    /**
00206     * Y-size by X-size number of pixels.
00207     */
00208    SizeType GetFrameSize() const;
00209 
00210    /*
00211     * number of pixels from the start of the L2R or R2L frame.
00212     */
00213    SizeType GetPartialFrameOffset() const;
00214 
00215    /**
00216     * number of pixels of the partial frame.
00217     */
00218    SizeType GetPartialFrameSize() const;
00219 
00220    /**
00221     * the frame width in [m].
00222     */
00223    LengthType GetFrameWidth() const;
00224 
00225    /**
00226     * the frame height in [m].
00227     */
00228    LengthType GetFrameHeight() const;
00229 
00230    /**
00231     * the pixel spacing in the x-direction in [m].
00232     */
00233    SpacingType GetSpacingX() const;
00234 
00235    /**
00236     * the pixel spacing in the y-direction in [m].
00237     */
00238    SpacingType GetSpacingY() const;
00239 
00240    /**
00241     * the bit spacing in the z-direction in [m].
00242     */
00243    SpacingType GetSpacingZ() const;
00244 
00245    /**
00246     * the length for the given number of pixels in the x-direction in [m].
00247     */
00248    LengthType GetLengthX( SizeType dx ) const;
00249 
00250    /**
00251     * the length for the given number of pixels in the y-direction in [m].
00252     */
00253    LengthType GetLengthY( SizeType dy ) const;
00254 
00255    /**
00256     * the absolute height in [m] for given ADC value: scale * (adc + offset).
00257     */
00258    LengthType GetAbsoluteHeight( ValueType adcValue ) const;
00259 
00260    /**
00261     * the rows required for FFT.
00262     */
00263    SizeType GetRowsForFFT() const;
00264 
00265    /**
00266     * the columns required for FFT.
00267     */
00268    SizeType GetColumnsForFFT() const;
00269 
00270    /**
00271     * the size required for FFT.
00272     */
00273    static SizeType GetSizeForFFT( SizeType size );
00274 
00275    /// @}
00276    /// \name Mutators
00277    /// @{
00278 
00279    /**
00280     * make sure a buffer of appropriate size is available;
00281     * \deprecated Replace with CreateOutputBuffer( const Self * ).
00282     */
00283    BOOL CheckAndSetBufferSize();
00284 
00285    /**
00286     * clear contents of data buffer.
00287     */
00288    void ClearDataBuffer();
00289 
00290    /**
00291     * clear contents of data buffer.
00292     */
00293    void ClearFourierBuffer();
00294 
00295    /**
00296     * create this buffer for input with the given size.
00297     */
00298    BOOL CreateInputBuffer( const CSize& size );
00299 
00300    /**
00301     * create this buffer for output based on the given input buffer; if specified, allocate extra elements.
00302     */
00303    BOOL CreateOutputBufferFor( const Self& input, SizeType extra = 0 );
00304 
00305    /**
00306     * resize this data buffer to the given size, on reallocation add extra size.
00307     */
00308    BOOL ResizeDataBuffer( const CSize& size, SizeType extra = 0 );
00309 
00310    /**
00311     * create the fourier buffer(s) based on the current databuffer size.
00312     */
00313    BOOL CreateFourierBuffer();
00314 
00315    /**
00316     * delete the fourier buffer(s).
00317     */
00318    BOOL DeleteFourierBuffer();
00319 
00320    /**
00321     * allocate fourier buffer(s) based on the current databuffer size and 
00322     * copy contents from other buffer.
00323     */
00324    BOOL CopyFourierBufferFrom( const Self& other );
00325 
00326    /// @}
00327 
00328 protected:
00329    /// @}
00330    /// \name Buffer Creation
00331    /// @{
00332 
00333    /**
00334     * shallow copy; set data and fourier buffer pointers to 0.
00335     */
00336    void Copy( const Self& rhs );
00337 
00338    /**
00339     * allocate data buffer, but not fourier buffers.
00340     */
00341    void AllocateDataBuffer( SizeType extra = 0 );
00342 
00343    /**
00344     * allocate fourier buffer.
00345     */
00346    void AllocateFourierBuffer();
00347 
00348    /**
00349     * release all buffers.
00350     */
00351    void ReleaseAllBuffers();
00352 
00353    /**
00354     * release data buffer.
00355     */
00356    void ReleaseDataBuffer();
00357 
00358    /**
00359     * release fourier buffer.
00360     */
00361    void ReleaseFourierBuffer();
00362 
00363    /**
00364     * true if buffer has the right size and settings to use as output buffer
00365     * for the given input buffer.
00366     */
00367    BOOL IsUsableOutputBufferFor( const Self& input );
00368 
00369    /// @}
00370 
00371 private:
00372    /// @}
00373    /// \name Prohibited
00374    /// @{
00375 
00376    /**
00377     * shallow copy-assignment constructor; do not copy data and fourier buffers.
00378     */
00379    CScanBaseBuffer( const Self& rhs );
00380 
00381    /**
00382     * shallo copy-assignment; do not copy data and fourier buffers.
00383     */
00384    Self& operator= ( const Self& rhs );
00385 
00386    /// @}
00387 
00388 public:
00389    /**
00390     * data L2R/R2L flags; see InterfaceDll.h, "Scandata Buffer Flags:".
00391     */
00392    FlagType m_dwFlags;
00393 
00394    /**
00395     * X size buffer, see Columns().
00396     */
00397    SizeType m_dwSizeX;
00398 
00399    /**
00400     * Y size buffer, see Rows().
00401     */
00402    SizeType m_dwSizeY;
00403 
00404    /**
00405     * number of pixels in buffer, see also IsCompleteFrame().
00406     */
00407    SizeType m_dwPixels;
00408 
00409    /**
00410     * pointer to the data, see Data().
00411     */
00412    Pointer m_pwData;
00413 
00414    /**
00415     * FFT data for L2R.
00416     */
00417    FourierElementType** m_pdFourierL2R;
00418 
00419    /**
00420     * FFT data for R2L.
00421     */
00422    FourierElementType** m_pdFourierR2L;
00423 
00424    /**
00425     * name of the buffer.
00426     */
00427    CString m_csBufferName;
00428 
00429    /**
00430     * name of the data set.
00431     */
00432    CString m_csDataName;
00433 
00434    /**
00435     * name of the data unit.
00436     */
00437    CString m_csDataUnit;
00438 
00439    /**
00440     * scale factor between 1 unit and 1 LSB; see AbsoluteHeight().
00441     */
00442    RealType m_dDataScaleFactor;
00443 
00444    /**
00445     * offset for ADC=0; see AbsoluteHeight().
00446     */
00447    int m_nDataOffset;
00448 
00449    /**
00450     * mask register for optional parameters; see InterfaceDll.h,
00451     * "Scandata Buffer Parameter Mask".
00452     */
00453    MaskType m_dwParameterMask;
00454 
00455    /**
00456     * X slope of backgroundplane.
00457     */
00458    RealType m_dSlopeX;
00459 
00460    /**
00461     * Y slope of backgroundplane.
00462     */
00463    RealType m_dSlopeY;
00464 
00465    /**
00466     * average (RealType).
00467     */
00468    RealType m_dAvg;
00469 
00470    /**
00471     * min Z.
00472     */
00473    int m_nMinZ;
00474 
00475    /**
00476     * max Z.
00477     */
00478    int m_nMaxZ;
00479 
00480    /**
00481     * sum of all data squared; extra, not in cyclic/raw.
00482     */
00483    RealType m_dSecMom;
00484 
00485    /**
00486     * partial data in buffer, valid from Offset for Count pixels.
00487     */
00488    SizeType m_dwPixelOffset;
00489 
00490    /**
00491     * for dual l2r/r2l scans these values are valid for both areas.
00492     */
00493    SizeType m_dwPixelCount;
00494 
00495    /**
00496     * X Step size of area in picometer.
00497     */
00498    RealType m_dXStepSize;
00499 
00500    /**
00501     * Y Step size of area in picometer.
00502     */
00503    RealType m_dYStepSize;
00504 
00505    /**
00506     * original X Size in pixels.
00507     */
00508    SizeType m_dwOrgScanXSize;
00509 
00510    /**
00511     * original Y Size in pixels.
00512     */
00513    SizeType m_dwOrgScanYSize;
00514 
00515    /**
00516     * Rotation of scan around center in rad.
00517     */
00518    RealType m_dScanAngle;
00519 
00520    /**
00521     * the current state of this buffer.
00522     *
00523     * The state of the buffer may be obtained via IsNewBuffer(), IsEmptyBuffer(),
00524     * IsLoadedBuffer(), IsModifiedBuffer() and IsInMemoryBuffer().
00525     *
00526     * See also \ref m_eStatus.
00527     */
00528    enum tStatus
00529    {
00530       SB_NOTLOADED=0,   ///< data has not yet been loaded into this buffer from disk
00531       SB_LOADED,        ///< this is a buffer loaded with data from disk
00532       SB_NEW,           ///< this is a fresh buffer, there is no link to disk yet
00533       SB_TEMP,          ///< this is a temporary buffer used for in-memory filtering
00534       SB_MODIFIED,      ///< buffer with data obtained from disk has been modified and must still be updated on disk
00535    };
00536 
00537    /**
00538     * buffer status type.
00539     */
00540    typedef tStatus StatusType;
00541 
00542 protected:
00543    DECLARE_DYNCREATE( CScanBaseBuffer );// buffers can be serialized
00544 
00545    /*
00546     * internal data members:
00547     */
00548 
00549    /**
00550     * \brief status of the buffer contents (loaded, modified etc.).
00551     *
00552     * This status is used by the application to handle the data buffers. 
00553     * There are two types of buffer usages
00554     * - in-memory filters (on-line and viewing filters)
00555     * - normal filters
00556     *
00557     * For in-memory filters, the application sets the status for the <b>output
00558     * buffer</b> to \c SB_TEMP. The application deletes this temporary buffer
00559     * when it moves to another frame.
00560     *
00561     * For normal (interactive) filters, the application sets the status for the
00562     * <b>output buffer</b> to \c SB_NEW. After closing the filter's dialog with 
00563     * OK, the application writes the buffer's contents to disk.
00564     *
00565     * See also \ref tStatus.
00566     */
00567    StatusType    m_eStatus;             
00568 
00569    SizeType      m_dwMemoryAllocated;   ///< number of data points allocated in buffer
00570    LARGE_INTEGER m_liRAWIndex;          ///< file position in RAW data file
00571    LARGE_INTEGER m_liEditIndex;         ///< file position in edited data file
00572 };
00573 
00574 /*------------------------------------------------------------------------
00575 ** inline methods:
00576 */
00577 
00578 /*
00579  * true if buffer contains a left--right scan.
00580  */
00581 inline bool CScanBaseBuffer::HasLeftToRightScan() const
00582 {
00583    return 0 != ( m_dwFlags & SPM_SDBF_LEFT2RIGHT );
00584 }
00585 
00586 /*
00587  * true if buffer contains a right--left scan.
00588  */
00589 inline bool CScanBaseBuffer::HasRightToLeftScan() const
00590 {
00591    return 0 != ( m_dwFlags & SPM_SDBF_RIGHT2LEFT );
00592 }
00593 
00594 /*
00595  * true if buffer contains both a left--right and a right--left scan.
00596  */
00597 inline bool CScanBaseBuffer::IsBidirectionalScan() const
00598 {
00599    return HasLeftToRightScan() && HasRightToLeftScan();
00600 }
00601 
00602 /*
00603  * X-size in pixels.
00604  */
00605 inline CScanBaseBuffer::SizeType CScanBaseBuffer::Columns() const
00606 {
00607    return m_dwSizeX;
00608 }
00609 
00610 /*
00611  * Y-size in pixels.
00612  */
00613 inline CScanBaseBuffer::SizeType CScanBaseBuffer::Rows() const
00614 {
00615    return m_dwSizeY;
00616 }
00617 
00618 /*
00619  * the buffer's data (no-const).
00620  */
00621 inline CScanBaseBuffer::Pointer CScanBaseBuffer::Data()
00622 {
00623    return m_pwData;
00624 }
00625 
00626 /*
00627  * the buffer's data (const).
00628  */
00629 inline CScanBaseBuffer::ConstPointer CScanBaseBuffer::Data() const
00630 {
00631    return m_pwData;
00632 }
00633 
00634 /*
00635  * the fourier left-right data (non-const).
00636  */
00637 inline CScanBaseBuffer::FourierElementType** CScanBaseBuffer::FourierDataL2R()
00638 {
00639    return m_pdFourierL2R;
00640 }
00641 
00642 /*
00643  * the fourier left-right data (const).
00644  */
00645 inline CScanBaseBuffer::FourierElementType const* const * CScanBaseBuffer::FourierDataL2R() const
00646 {
00647    return m_pdFourierL2R;
00648 }
00649 
00650 /*
00651  * the fourier right-left data (non-const).
00652  */
00653 inline CScanBaseBuffer::FourierElementType** CScanBaseBuffer::FourierDataR2L()
00654 {
00655    return m_pdFourierR2L;
00656 }
00657 
00658 /*
00659  * the fourier right-left data (const).
00660  */
00661 inline CScanBaseBuffer::FourierElementType const* const* CScanBaseBuffer::FourierDataR2L() const
00662 {
00663    return m_pdFourierR2L;
00664 }
00665 
00666 /*
00667  * Y-size by X-size number of pixels.
00668  */
00669 inline CScanBaseBuffer::SizeType CScanBaseBuffer::GetFrameSize() const
00670 {
00671    return m_dwSizeY * m_dwSizeX;
00672 }
00673 
00674 /*
00675  * number of pixels from the start of the L2R or R2L frame.
00676  */
00677 inline CScanBaseBuffer::SizeType CScanBaseBuffer::GetPartialFrameOffset() const
00678 {
00679    return m_dwPixelOffset;
00680 }
00681 
00682 /*
00683  * number of pixels of the partial frame.
00684  */
00685 inline CScanBaseBuffer::SizeType CScanBaseBuffer::GetPartialFrameSize() const
00686 {
00687    return m_dwPixelCount;
00688 }
00689 
00690 /*
00691  * true if all pixels of the frame are present.
00692  */
00693 inline bool CScanBaseBuffer::IsCompleteFrame() const
00694 {
00695 #if 0
00696    CString s;
00697    s.Format( "IN: PixelCount:%d Pixels:%d FrameSize:%d",
00698    m_dwPixelCount, m_dwPixels, GetFrameSize() );
00699 
00700    Q_LOG( _T(s) );
00701 #endif
00702 
00703    return m_dwPixels == ( 1 + IsBidirectionalScan() ) * GetFrameSize();
00704 
00705 //    return m_dwPixelCount == GetFrameSize()
00706 //        && m_dwPixels     == ( 1 + IsBidirectionalScan() ) * GetFrameSize();
00707 }
00708 
00709 /*
00710  * true if this buffer has not yet been used.
00711  */
00712 inline bool CScanBaseBuffer::IsNewBuffer() const
00713 {
00714    return SB_NEW == m_eStatus;
00715 }
00716 
00717 /*
00718  * true if data not yet loaded.
00719  */
00720 inline bool CScanBaseBuffer::IsEmptyBuffer() const
00721 {
00722    return SB_NOTLOADED == m_eStatus;
00723 }
00724 
00725 /*
00726  * true if data present in buffer.
00727  */
00728 inline bool CScanBaseBuffer::IsLoadedBuffer() const
00729 {
00730    return SB_LOADED == m_eStatus;
00731 }
00732 
00733 /*
00734  * true if data in buffer has been modified.
00735  */
00736 inline bool CScanBaseBuffer::IsModifiedBuffer() const
00737 {
00738    return SB_MODIFIED == m_eStatus;
00739 }
00740 
00741 /*
00742  * true if buffer is used for in-memory filtering.
00743  */
00744 inline bool CScanBaseBuffer::IsInMemoryBuffer() const
00745 {
00746    return SB_TEMP == m_eStatus;
00747 }
00748 
00749 /**
00750  * the pixel spacing in the x-direction in [m].
00751  */
00752 inline CScanBaseBuffer::SpacingType CScanBaseBuffer::GetSpacingX() const
00753 {
00754    return m_dXStepSize * 1e-12;
00755 }
00756 
00757 /**
00758  * the pixel spacing in the y-direction in [m].
00759  */
00760 inline CScanBaseBuffer::SpacingType CScanBaseBuffer::GetSpacingY() const
00761 {
00762    return m_dYStepSize * 1e-12;
00763 }
00764 
00765 /**
00766  * the bit spacing in the z-direction in [m].
00767  */
00768 inline CScanBaseBuffer::SpacingType CScanBaseBuffer::GetSpacingZ() const
00769 {
00770    /*
00771     * assume nano, unless unit is valid:
00772     */
00773    RealType unit = 1e-9;
00774 
00775    if ( m_csDataUnit.IsEmpty() )
00776       return m_dDataScaleFactor * unit;
00777 
00778    switch ( m_csDataUnit.GetAt(0) )
00779    {
00780       case 'p': unit = 1e-12; break;
00781       case 'n': unit = 1e-9; break;
00782       case 'u': unit = 1e-6; break;
00783       case 'm': unit = 1e-3; break;
00784       case 'k': unit = 1e+3; break;
00785       case 'M': unit = 1e+6; break;
00786       case 'G': unit = 1e+9; break;
00787    }
00788 
00789    return m_dDataScaleFactor * unit;
00790 }
00791 
00792 /**
00793  * the length for the given number of pixels in the x-direction in [m].
00794  */
00795 inline CScanBaseBuffer::LengthType CScanBaseBuffer::GetLengthX( SizeType dx ) const
00796 {
00797    return dx * GetSpacingX();
00798 }
00799 
00800 /**
00801  * the length for the given number of pixels in the y-direction in [m].
00802  */
00803 inline CScanBaseBuffer::LengthType CScanBaseBuffer::GetLengthY( SizeType dy ) const
00804 {
00805    return dy * GetSpacingY();
00806 }
00807 
00808 /*
00809  * the absolute height in [m] for given ADC value: scale * (adc + offset).
00810  */
00811 inline CScanBaseBuffer::LengthType CScanBaseBuffer::GetAbsoluteHeight( ValueType adcValue ) const
00812 {
00813    return ( adcValue + m_nDataOffset ) * GetSpacingZ();
00814 }
00815 
00816 /**
00817  * the rows required for FFT.
00818  */
00819 inline CScanBaseBuffer::SizeType CScanBaseBuffer::GetRowsForFFT() const
00820 {
00821    return GetSizeForFFT( Rows() );
00822 }
00823 
00824 /**
00825  * the columns required for FFT.
00826  */
00827 inline CScanBaseBuffer::SizeType CScanBaseBuffer::GetColumnsForFFT() const
00828 {
00829    return GetSizeForFFT( Columns() );
00830 }
00831 
00832 /**
00833  * the size required for FFT.
00834  */
00835 inline CScanBaseBuffer::SizeType CScanBaseBuffer::GetSizeForFFT( SizeType size )
00836 {
00837    return static_cast< SizeType >( pow( 2.0, ceil( log( static_cast<double>( size ) ) / log( 2.0 ) ) ) );
00838 }
00839 
00840 #endif // ifndef CFILTLIB_SCANBASEBUFFER_H
00841 
00842 /*
00843  * end of file
00844  */
00845 

Camera Filter Library documentation © 2004-2007 by Leiden Probe Microscopy