diff --git a/Modules/ToFHardware/mitkToFCameraMESAController.cpp b/Modules/ToFHardware/mitkToFCameraMESAController.cpp new file mode 100644 index 0000000000..5650fff8c3 --- /dev/null +++ b/Modules/ToFHardware/mitkToFCameraMESAController.cpp @@ -0,0 +1,145 @@ +/*========================================================================= + +Program: Medical Imaging & Interaction Toolkit +Module: $RCSfile$ +Language: C++ +Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ +Version: $Revision: $ + +Copyright (c) German Cancer Research Center, Division of Medical and +Biological Informatics. All rights reserved. +See MITKCopyright.txt or http://www.mitk.org/copyright.html 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. + +=========================================================================*/ +#include "mitkToFCameraMESAController.h" +#include +#include + +CMesaDevice* m_MESAHandle=0; + +namespace mitk +{ + ToFCameraMESAController::ToFCameraMESAController(): m_MESARes(0), m_PixelNumber(40000), m_NumberOfBytes(0), + m_CaptureWidth(0), m_CaptureHeight(0), m_NumImg(0), m_MaxRangeFactor(0.0), m_ConnectionCheck(false), + m_InputFileName("") + { + } + + ToFCameraMESAController::~ToFCameraMESAController() + { + } + + bool ToFCameraMESAController::CloseCameraConnection() + { + m_MESARes = SR_Close(m_MESAHandle); //res=SR_Close(srCam); + m_ConnectionCheck = ErrorText(m_MESARes); + m_MESAHandle = 0; + return m_ConnectionCheck; + } + + bool ToFCameraMESAController::ErrorText(int error) + { + //if(error != MESA_OK) + //{ + // //pmdGetLastError (m_MESAHandle, m_MESAError, 128); + // //MITK_ERROR << "Camera Error " << m_MESAError; + // return false; + //} + //else return true; + return true; + } + + bool ToFCameraMESAController::UpdateCamera() + { + m_MESARes = SR_Acquire(m_MESAHandle); //res=SR_Acquire(srCam); + //return ErrorText(m_MESARes); + return true; + } + + bool ToFCameraMESAController::GetAmplitudes(float* amplitudeArray) + { + unsigned short* data; + data = (unsigned short*)SR_GetImage(m_MESAHandle, 1); + for (int i=0; im_PixelNumber; i++) + { + if (data[i] & 0x8000) // bit 16 indicates saturation + { + amplitudeArray[i] = 0; + } + else + { + unsigned short value = data[i] & 0x7fff; // bit 16 indicates saturation + unsigned short value2 = value >> 2; // bits 1 and 2 are reserved/unused + amplitudeArray[i] = value2; + } + } + return true; + } + + bool ToFCameraMESAController::GetIntensities(float* intensityArray) + { + unsigned short* data; + data = (unsigned short*)SR_GetImage(m_MESAHandle, 1); + for (int i=0; im_PixelNumber; i++) + { + intensityArray[i] = data[i]; + } + return true; + } + + bool ToFCameraMESAController::GetDistances(float* distanceArray) + { + //this->m_MESARes = pmdGetDistances(m_MESAHandle, distanceArray, this->m_NumberOfBytes); + //return ErrorText(this->m_MESARes); + unsigned short* data; + /* + for(i=0; im_PixelNumber; i++) + { + if (data[i] < 0xFFF8) + { + distanceArray[i] = data[i] * this->m_MaxRangeFactor; + } + else + { + distanceArray[i] = 0.0; + } + } + + return true; + } + + int ToFCameraMESAController::SetIntegrationTime(unsigned int integrationTime) + { + return 0; + } + + int ToFCameraMESAController::GetIntegrationTime() + { + return 0; + } + + int ToFCameraMESAController::SetModulationFrequency(unsigned int modulationFrequency) + { + return 0; + } + + int ToFCameraMESAController::GetModulationFrequency() + { + return 0; + } + + void ToFCameraMESAController::SetInputFileName(std::string inputFileName) + { + this->m_InputFileName = inputFileName; + } +} diff --git a/Modules/ToFHardware/mitkToFCameraMESAController.h b/Modules/ToFHardware/mitkToFCameraMESAController.h new file mode 100644 index 0000000000..533e0fb95d --- /dev/null +++ b/Modules/ToFHardware/mitkToFCameraMESAController.h @@ -0,0 +1,146 @@ +/*========================================================================= + +Program: Medical Imaging & Interaction Toolkit +Module: $RCSfile$ +Language: C++ +Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ +Version: $Revision: $ + +Copyright (c) German Cancer Research Center, Division of Medical and +Biological Informatics. All rights reserved. +See MITKCopyright.txt or http://www.mitk.org/copyright.html 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 __mitkToFCameraMESAController_h +#define __mitkToFCameraMESAController_h + +#include "mitkToFHardwareExports.h" +#include "mitkCommon.h" +#include "mitkToFCameraMESAController.h" + +#include "itkObject.h" +#include "itkObjectFactory.h" + +namespace mitk +{ + /** + * @brief Virtual interface and base class for all MESA Time-of-Flight devices. Wraps MESA API provided in library + * Provides methods for accessing current distance, amplitude, intensity and raw data. + * Allows to set parameters like modulation frequency and integration time. + * + * @ingroup ToFHardware + */ + class MITK_TOFHARDWARE_EXPORT ToFCameraMESAController : public itk::Object + { + public: + + mitkClassMacro( ToFCameraMESAController , itk::Object ); + + /*! + \brief opens a connection to the ToF camera. Has to be implemented by the sub class + */ + //TODO return value + virtual bool OpenCameraConnection() = 0; + /*! + \brief closes the connection to the camera + */ + //TODO return value + virtual bool CloseCameraConnection(); + /*! + \brief Gets the current amplitude array from the device + \param amplitudeArray float array where the amplitude data will be saved + */ + virtual bool GetAmplitudes(float* amplitudeArray); + /*! + \brief Gets the current intensity array from the device + \param intensityArray float array where the intensity data will be saved + */ + virtual bool GetIntensities(float* intensityArray); + /*! + \brief Gets the current distance array from the device + \param distanceArray float array where the distance data will be saved + */ + virtual bool GetDistances(float* distanceArray); + /*! + \brief calls update on the camera -> a new ToF-image is aquired + */ + virtual bool UpdateCamera(); + /*! + \brief Returns the currently set modulation frequency. + \return modulation frequency + */ + virtual int GetModulationFrequency(); + /*! + \brief Sets the modulation frequency of the ToF device. + The method automatically calculates a valid value from the given frequency to + make sure that only valid frequencies are used. + \param modulationFrequency modulation frequency + \return frequency set after validation step + */ + virtual int SetModulationFrequency(unsigned int modulationFrequency); + /*! + \brief Returns the currently set integration time. + \return integration time + */ + virtual int GetIntegrationTime(); + /*! + \brief Sets the integration time of the ToF device. + The method automatically calculates a valid value from the given integration time to + make sure that only valid times are used. + \param integrationTime integration time + \return integration time set after validation step + */ + virtual int SetIntegrationTime(unsigned int integrationTime); + /*! + \brief set input file name used by MESA player classes + */ + virtual void SetInputFileName(std::string inputFileName); + /*! + \brief Access the resolution of the image in x direction + \return widht of image in pixel + */ + itkGetMacro(CaptureWidth, unsigned int); + /*! + \brief Access the resolution of the image in y direction + \return height of image in pixel + */ + itkGetMacro(CaptureHeight, unsigned int); + + protected: + + ToFCameraMESAController(); + + ~ToFCameraMESAController(); + + /*! + \brief Method printing the current error message to the console and returning whether the previous command was successful + \param error error number returned by the MESA function + \return flag indicating if an error occured (false) or not (true) + */ + bool ErrorText(int error); + + char m_MESAError[128]; ///< member holding the current error text + int m_MESARes; ///< holds the current result message provided by MESA + + int m_PixelNumber; ///< holds the number of pixels contained in the image + int m_NumberOfBytes; ///< holds the number of bytes contained in the image + unsigned int m_CaptureWidth; ///< holds the width of the image in pixel + unsigned int m_CaptureHeight; ///< holds the height of the image in pixel + + int m_NumImg; ///< holds the number of images the camera provided (distance, intensity, etc) + + float m_MaxRangeFactor; ///< holds the factor to calculate the real distance depends on the modulation frequency + + bool m_ConnectionCheck; ///< flag showing whether the camera is connected (true) or not (false) + + std::string m_InputFileName; ///< input file name used by MESA player classes + + private: + + }; +} //END mitk namespace +#endif diff --git a/Modules/ToFHardware/mitkToFCameraMESADevice.cpp b/Modules/ToFHardware/mitkToFCameraMESADevice.cpp new file mode 100644 index 0000000000..b6fc0f4429 --- /dev/null +++ b/Modules/ToFHardware/mitkToFCameraMESADevice.cpp @@ -0,0 +1,451 @@ +/*========================================================================= + +Program: Medical Imaging & Interaction Toolkit +Module: $RCSfile$ +Language: C++ +Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ +Version: $Revision: $ + +Copyright (c) German Cancer Research Center, Division of Medical and +Biological Informatics. All rights reserved. +See MITKCopyright.txt or http://www.mitk.org/copyright.html 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. + +=========================================================================*/ +#include "mitkToFCameraMESADevice.h" +#include "mitkRealTimeClock.h" + +#include "itkMultiThreader.h" +#include + +namespace mitk +{ + ToFCameraMESADevice::ToFCameraMESADevice() + { + } + + ToFCameraMESADevice::~ToFCameraMESADevice() + { + } + + bool ToFCameraMESADevice::ConnectCamera() + { + bool ok = false; + if (m_Controller) + { + ok = m_Controller->OpenCameraConnection(); + if (ok) + { + this->m_CaptureWidth = m_Controller->GetCaptureWidth(); + this->m_CaptureHeight = m_Controller->GetCaptureHeight(); + this->m_PixelNumber = this->m_CaptureWidth * this->m_CaptureHeight; + + // allocate buffer + this->m_IntensityArray = new float[this->m_PixelNumber]; + for(int i=0; im_PixelNumber; i++) {this->m_IntensityArray[i]=0.0;} + this->m_DistanceArray = new float[this->m_PixelNumber]; + for(int i=0; im_PixelNumber; i++) {this->m_DistanceArray[i]=0.0;} + this->m_AmplitudeArray = new float[this->m_PixelNumber]; + for(int i=0; im_PixelNumber; i++) {this->m_AmplitudeArray[i]=0.0;} + + this->m_DistanceDataBuffer = new float*[this->m_MaxBufferSize]; + for(int i=0; im_MaxBufferSize; i++) + { + this->m_DistanceDataBuffer[i] = new float[this->m_PixelNumber]; + } + this->m_AmplitudeDataBuffer = new float*[this->m_MaxBufferSize]; + for(int i=0; im_MaxBufferSize; i++) + { + this->m_AmplitudeDataBuffer[i] = new float[this->m_PixelNumber]; + } + this->m_IntensityDataBuffer = new float*[this->m_MaxBufferSize]; + for(int i=0; im_MaxBufferSize; i++) + { + this->m_IntensityDataBuffer[i] = new float[this->m_PixelNumber]; + } + + m_CameraConnected = true; + } + } + return ok; + } + + bool ToFCameraMESADevice::DisconnectCamera() + { + bool ok = false; + if (m_Controller) + { + ok = m_Controller->CloseCameraConnection(); + + // clean-up only if camera was connected + if (m_CameraConnected) + { + delete [] m_IntensityArray; + delete [] m_DistanceArray; + delete [] m_AmplitudeArray; + + for(int i=0; im_MaxBufferSize; i++) + { + delete[] this->m_DistanceDataBuffer[i]; + } + delete[] this->m_DistanceDataBuffer; + for(int i=0; im_MaxBufferSize; i++) + { + delete[] this->m_AmplitudeDataBuffer[i]; + } + delete[] this->m_AmplitudeDataBuffer; + for(int i=0; im_MaxBufferSize; i++) + { + delete[] this->m_IntensityDataBuffer[i]; + } + delete[] this->m_IntensityDataBuffer; + + m_CameraConnected = false; + } + + } + return ok; + } + + void ToFCameraMESADevice::StartCamera() + { + if (m_CameraConnected) + { + // get the first image + this->m_Controller->UpdateCamera(); + this->m_ImageMutex->Lock(); + this->m_Controller->GetDistances(this->m_DistanceDataBuffer[this->m_FreePos]); + this->m_Controller->GetAmplitudes(this->m_AmplitudeDataBuffer[this->m_FreePos]); + this->m_Controller->GetIntensities(this->m_IntensityDataBuffer[this->m_FreePos]); + this->m_FreePos = (this->m_FreePos+1) % this->m_BufferSize; + this->m_CurrentPos = (this->m_CurrentPos+1) % this->m_BufferSize; + this->m_ImageSequence++; + this->m_ImageMutex->Unlock(); + + this->m_CameraActiveMutex->Lock(); + this->m_CameraActive = true; + this->m_CameraActiveMutex->Unlock(); + this->m_ThreadID = this->m_MultiThreader->SpawnThread(this->Acquire, this); + // wait a little to make sure that the thread is started + itksys::SystemTools::Delay(10); + } + else + { + MITK_INFO<<"Camera not connected"; + } + } + + void ToFCameraMESADevice::StopCamera() + { + m_CameraActiveMutex->Lock(); + m_CameraActive = false; + m_CameraActiveMutex->Unlock(); + itksys::SystemTools::Delay(100); + if (m_MultiThreader.IsNotNull()) + { + m_MultiThreader->TerminateThread(m_ThreadID); + } + // wait a little to make sure that the thread is terminated + itksys::SystemTools::Delay(10); + } + + bool ToFCameraMESADevice::IsCameraActive() + { + m_CameraActiveMutex->Lock(); + bool ok = m_CameraActive; + m_CameraActiveMutex->Unlock(); + return ok; + } + + void ToFCameraMESADevice::UpdateCamera() + { + if (m_Controller) + { + m_Controller->UpdateCamera(); + } + } + + ITK_THREAD_RETURN_TYPE ToFCameraMESADevice::Acquire(void* pInfoStruct) + { + /* extract this pointer from Thread Info structure */ + struct itk::MultiThreader::ThreadInfoStruct * pInfo = (struct itk::MultiThreader::ThreadInfoStruct*)pInfoStruct; + if (pInfo == NULL) + { + return ITK_THREAD_RETURN_VALUE; + } + if (pInfo->UserData == NULL) + { + return ITK_THREAD_RETURN_VALUE; + } + ToFCameraMESADevice* toFCameraDevice = (ToFCameraMESADevice*)pInfo->UserData; + if (toFCameraDevice!=NULL) + { + mitk::RealTimeClock::Pointer realTimeClock; + realTimeClock = mitk::RealTimeClock::New(); + double t1, t2; + t1 = realTimeClock->GetCurrentStamp(); + int n = 100; + bool overflow = false; + bool printStatus = false; + while (toFCameraDevice->IsCameraActive()) + { + // update the ToF camera + toFCameraDevice->UpdateCamera(); + // get the image data from the camera and write it at the next free position in the buffer + toFCameraDevice->m_Controller->GetDistances(toFCameraDevice->m_DistanceDataBuffer[toFCameraDevice->m_FreePos]); + toFCameraDevice->m_Controller->GetAmplitudes(toFCameraDevice->m_AmplitudeDataBuffer[toFCameraDevice->m_FreePos]); + toFCameraDevice->m_Controller->GetIntensities(toFCameraDevice->m_IntensityDataBuffer[toFCameraDevice->m_FreePos]); + + // call modified to indicate that cameraDevice was modified + toFCameraDevice->Modified(); + + /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + TODO Buffer Handling currently only works for buffer size 1 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ + toFCameraDevice->m_ImageMutex->Lock(); + //toFCameraDevice->m_ImageSequence++; + toFCameraDevice->m_FreePos = (toFCameraDevice->m_FreePos+1) % toFCameraDevice->m_BufferSize; + toFCameraDevice->m_CurrentPos = (toFCameraDevice->m_CurrentPos+1) % toFCameraDevice->m_BufferSize; + toFCameraDevice->m_ImageSequence++; + if (toFCameraDevice->m_FreePos == toFCameraDevice->m_CurrentPos) + { + // buffer overflow + //MITK_INFO << "Buffer overflow!! "; + //toFCameraDevice->m_CurrentPos = (toFCameraDevice->m_CurrentPos+1) % toFCameraDevice->m_BufferSize; + //toFCameraDevice->m_ImageSequence++; + overflow = true; + } + if (toFCameraDevice->m_ImageSequence % n == 0) + { + printStatus = true; + } + toFCameraDevice->m_ImageMutex->Unlock(); + if (overflow) + { + //itksys::SystemTools::Delay(10); + overflow = false; + } + /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + END TODO Buffer Handling currently only works for buffer size 1 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ + + // print current framerate + if (printStatus) + { + t2 = realTimeClock->GetCurrentStamp() - t1; + //MITK_INFO << "t2: " << t2 <<" Time (s) for 1 image: " << (t2/1000) / n << " Framerate (fps): " << n / (t2/1000) << " Sequence: " << toFCameraDevice->m_ImageSequence; + MITK_INFO << " Framerate (fps): " << n / (t2/1000) << " Sequence: " << toFCameraDevice->m_ImageSequence; + t1 = realTimeClock->GetCurrentStamp(); + printStatus = false; + } + } // end of while loop + } + return ITK_THREAD_RETURN_VALUE; + } + + // TODO: Buffer size currently set to 1. Once Buffer handling is working correctly, method may be reactivated + // void ToFCameraMESADevice::ResetBuffer(int bufferSize) + // { + // this->m_BufferSize = bufferSize; + // this->m_CurrentPos = -1; + // this->m_FreePos = 0; + // } + + void ToFCameraMESADevice::GetAmplitudes(float* amplitudeArray, int& imageSequence) + { + m_ImageMutex->Lock(); + if (m_CameraActive) + { + // 1) copy the image buffer + // 2) Flip around y- axis (vertical axis) + + /* + this->m_Controller->GetAmplitudes(this->m_SourceDataBuffer[this->m_CurrentPos], this->m_AmplitudeArray); + for (int i=0; im_CaptureHeight; i++) + { + for (int j=0; jm_CaptureWidth; j++) + { + amplitudeArray[i*this->m_CaptureWidth+j] = this->m_AmplitudeArray[(i+1)*this->m_CaptureWidth-1-j]; + } + } + */ + for (int i=0; im_PixelNumber; i++) + { + amplitudeArray[i] = this->m_AmplitudeDataBuffer[this->m_CurrentPos][i]; + } + imageSequence = this->m_ImageSequence; + } + else + { + MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active."; + } + m_ImageMutex->Unlock(); + } + + void ToFCameraMESADevice::GetIntensities(float* intensityArray, int& imageSequence) + { + m_ImageMutex->Lock(); + if (m_CameraActive) + { + // 1) copy the image buffer + // 2) Flip around y- axis (vertical axis) + + /* + this->m_Controller->GetIntensities(this->m_SourceDataBuffer[this->m_CurrentPos], this->m_IntensityArray); + for (int i=0; im_CaptureHeight; i++) + { + for (int j=0; jm_CaptureWidth; j++) + { + intensityArray[i*this->m_CaptureWidth+j] = this->m_IntensityArray[(i+1)*this->m_CaptureWidth-1-j]; + } + } + */ + for (int i=0; im_PixelNumber; i++) + { + intensityArray[i] = this->m_IntensityDataBuffer[this->m_CurrentPos][i]; + } + imageSequence = this->m_ImageSequence; + } + else + { + MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active."; + } + m_ImageMutex->Unlock(); + } + + void ToFCameraMESADevice::GetDistances(float* distanceArray, int& imageSequence) + { + m_ImageMutex->Lock(); + if (m_CameraActive) + { + // 1) copy the image buffer + // 2) convert the distance values from m to mm + // 3) Flip around y- axis (vertical axis) + + /* + this->m_Controller->GetDistances(this->m_SourceDataBuffer[this->m_CurrentPos], this->m_DistanceArray); + for (int i=0; im_CaptureHeight; i++) + { + for (int j=0; jm_CaptureWidth; j++) + { + distanceArray[i*this->m_CaptureWidth+j] = 1000 * this->m_DistanceArray[(i+1)*this->m_CaptureWidth-1-j]; + } + } + */ + for (int i=0; im_PixelNumber; i++) + { + distanceArray[i] = this->m_DistanceDataBuffer[this->m_CurrentPos][i]; // * 1000 + } + imageSequence = this->m_ImageSequence; + } + else + { + MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active."; + } + m_ImageMutex->Unlock(); + } + + void ToFCameraMESADevice::GetAllImages(float* distanceArray, float* amplitudeArray, float* intensityArray, char* sourceDataArray, + int requiredImageSequence, int& capturedImageSequence) + { + if (m_CameraActive) + { + m_ImageMutex->Lock(); + // 1) copy the image buffer + // 2) convert the distance values from m to mm + // 3) Flip around y- axis (vertical axis) + + // check for empty buffer + if (this->m_ImageSequence < 0) + { + // buffer empty + MITK_INFO << "Buffer empty!! "; + capturedImageSequence = this->m_ImageSequence; + m_ImageMutex->Unlock(); + return; + } + // determine position of image in buffer + int pos = 0; + if ((requiredImageSequence < 0) || (requiredImageSequence > this->m_ImageSequence)) + { + capturedImageSequence = this->m_ImageSequence; + pos = this->m_CurrentPos; + //MITK_INFO << "Required image not found! Required: " << requiredImageSequence << " delivered/current: " << this->m_ImageSequence; + } + else if (requiredImageSequence <= this->m_ImageSequence - this->m_BufferSize) + { + capturedImageSequence = (this->m_ImageSequence - this->m_BufferSize) + 1; + pos = (this->m_CurrentPos + 1) % this->m_BufferSize; + //MITK_INFO << "Out of buffer! Required: " << requiredImageSequence << " delivered: " << capturedImageSequence << " current: " << this->m_ImageSequence; + } + else // (requiredImageSequence > this->m_ImageSequence - this->m_BufferSize) && (requiredImageSequence <= this->m_ImageSequence) + + { + capturedImageSequence = requiredImageSequence; + pos = (this->m_CurrentPos + (10-(this->m_ImageSequence - requiredImageSequence))) % this->m_BufferSize; + } + + // write image data to float arrays + for (int i=0; im_PixelNumber; i++) + { + distanceArray[i] = this->m_DistanceDataBuffer[pos][i] /* * 1000 */; + amplitudeArray[i] = this->m_AmplitudeDataBuffer[pos][i]; + intensityArray[i] = this->m_IntensityDataBuffer[pos][i]; + } + + m_ImageMutex->Unlock(); + + /* + this->m_Controller->GetDistances(this->m_SourceDataBuffer[pos], this->m_DistanceArray); + this->m_Controller->GetAmplitudes(this->m_SourceDataBuffer[pos], this->m_AmplitudeArray); + this->m_Controller->GetIntensities(this->m_SourceDataBuffer[pos], this->m_IntensityArray); + + int u, v; + for (int i=0; im_CaptureHeight; i++) + { + for (int j=0; jm_CaptureWidth; j++) + { + u = i*this->m_CaptureWidth+j; + v = (i+1)*this->m_CaptureWidth-1-j; + distanceArray[u] = 1000 * this->m_DistanceArray[v]; // unit in mm + //distanceArray[u] = this->m_DistanceArray[v]; // unit in meter + amplitudeArray[u] = this->m_AmplitudeArray[v]; + intensityArray[u] = this->m_IntensityArray[v]; + } + } + + memcpy(sourceDataArray, this->m_SourceDataBuffer[this->m_CurrentPos], this->m_SourceDataSize); + */ + } + else + { + MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active."; + } + } + + ToFCameraMESAController::Pointer ToFCameraMESADevice::GetController() + { + return this->m_Controller; + } + + void ToFCameraMESADevice::SetProperty( const char *propertyKey, BaseProperty* propertyValue ) + { + ToFCameraDevice::SetProperty(propertyKey,propertyValue); + this->m_PropertyList->SetProperty(propertyKey, propertyValue); + if (strcmp(propertyKey, "ModulationFrequency") == 0) + { + int modulationFrequency = 0; + GetIntProperty(propertyValue, modulationFrequency); + m_Controller->SetModulationFrequency(modulationFrequency); + } + else if (strcmp(propertyKey, "IntegrationTime") == 0) + { + int integrationTime = 0; + GetIntProperty(propertyValue, integrationTime); + m_Controller->SetIntegrationTime(integrationTime); + } + } +} diff --git a/Modules/ToFHardware/mitkToFCameraMESADevice.h b/Modules/ToFHardware/mitkToFCameraMESADevice.h new file mode 100644 index 0000000000..af9248feef --- /dev/null +++ b/Modules/ToFHardware/mitkToFCameraMESADevice.h @@ -0,0 +1,148 @@ +/*========================================================================= + +Program: Medical Imaging & Interaction Toolkit +Module: $RCSfile$ +Language: C++ +Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ +Version: $Revision: $ + +Copyright (c) German Cancer Research Center, Division of Medical and +Biological Informatics. All rights reserved. +See MITKCopyright.txt or http://www.mitk.org/copyright.html 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 __mitkToFCameraMESADevice_h +#define __mitkToFCameraMESADevice_h + +#include "mitkToFHardwareExports.h" +#include "mitkCommon.h" +#include "mitkToFCameraDevice.h" +#include "mitkToFCameraMESAController.h" + +#include "itkObject.h" +#include "itkObjectFactory.h" +#include "itkMultiThreader.h" +#include "itkFastMutexLock.h" + + +namespace mitk +{ + /** + * @brief Interface for all representations of MESA ToF devices. + * ToFCameraMESADevice internally holds an instance of ToFCameraMESAController and starts a thread + * that continuously grabs images from the controller. A buffer structure buffers the last acquired images + * to provide the image data loss-less. + * + * @ingroup ToFHardware + */ + class MITK_TOFHARDWARE_EXPORT ToFCameraMESADevice : public ToFCameraDevice + { + public: + + mitkClassMacro( ToFCameraMESADevice , ToFCameraDevice ); + + itkNewMacro( Self ); + + /*! + \brief opens a connection to the ToF camera + */ + virtual bool ConnectCamera(); + /*! + \brief closes the connection to the camera + */ + virtual bool DisconnectCamera(); + /*! + \brief starts the continuous updating of the camera. + A separate thread updates the source data, the main thread processes the source data and creates images and coordinates + */ + virtual void StartCamera(); + /*! + \brief stops the continuous updating of the camera + */ + virtual void StopCamera(); + /*! + \brief updates the camera for image acquisition + */ + virtual void UpdateCamera(); + /*! + \brief returns whether the camera is currently active or not + */ + virtual bool IsCameraActive(); + /*! + \brief gets the amplitude data from the ToF camera as the strength of the active illumination of every pixel. Caution! The user is responsible for allocating and deleting the images. + These values can be used to determine the quality of the distance values. The higher the amplitude value, the higher the accuracy of the according distance value + \param imageSequence the actually captured image sequence number + \param amplitudeArray contains the returned amplitude data as an array. + */ + virtual void GetAmplitudes(float* amplitudeArray, int& imageSequence); + /*! + \brief gets the intensity data from the ToF camera as a greyscale image. Caution! The user is responsible for allocating and deleting the images. + \param intensityArray contains the returned intensities data as an array. + \param imageSequence the actually captured image sequence number + */ + virtual void GetIntensities(float* intensityArray, int& imageSequence); + /*! + \brief gets the distance data from the ToF camera measuring the distance between the camera and the different object points in millimeters. Caution! The user is responsible for allocating and deleting the images. + \param distanceArray contains the returned distances data as an array. + \param imageSequence the actually captured image sequence number + */ + virtual void GetDistances(float* distanceArray, int& imageSequence); + /*! + \brief gets the 3 images (distance, amplitude, intensity) from the ToF camera. Caution! The user is responsible for allocating and deleting the images. + \param distanceArray contains the returned distance data as an array. + \param amplitudeArray contains the returned amplitude data as an array. + \param intensityArray contains the returned intensity data as an array. + \param sourceDataArray contains the complete source data from the camera device. + \param requiredImageSequence the required image sequence number + \param capturedImageSequence the actually captured image sequence number + */ + virtual void GetAllImages(float* distanceArray, float* amplitudeArray, float* intensityArray, char* sourceDataArray, + int requiredImageSequence, int& capturedImageSequence); +// TODO: Buffer size currently set to 1. Once Buffer handling is working correctly, method may be reactivated +// /*! +// \brief pure virtual method resetting the buffer using the specified bufferSize. Has to be implemented by sub-classes +// \param bufferSize buffer size the buffer should be reset to +// */ +// virtual void ResetBuffer(int bufferSize) = 0; + //TODO add/correct documentation for requiredImageSequence and capturedImageSequence in the GetAllImages, GetDistances, GetIntensities and GetAmplitudes methods. + + /*! + \brief returns the corresponding camera controller + */ + ToFCameraMESAController::Pointer GetController(); + + /*! + \brief set a BaseProperty + */ + virtual void SetProperty( const char *propertyKey, BaseProperty* propertyValue ); + + protected: + + ToFCameraMESADevice(); + + ~ToFCameraMESADevice(); + + /*! + \brief Thread method continuously acquiring images from the ToF hardware + */ + static ITK_THREAD_RETURN_TYPE Acquire(void* pInfoStruct); + /*! + \brief moves the position pointer m_CurrentPos to the next position in the buffer if that's not the next free position to prevent reading from an empty buffer + */ + void GetNextPos(); + + ToFCameraMESAController::Pointer m_Controller; ///< corresponding CameraController + + float** m_DistanceDataBuffer; ///< buffer holding the last distance images + float** m_AmplitudeDataBuffer; ///< buffer holding the last amplitude images + float** m_IntensityDataBuffer; ///< buffer holding the last intensity images + + private: + + }; +} //END mitk namespace +#endif diff --git a/Modules/ToFHardware/mitkToFCameraMESASR4000Controller.cpp b/Modules/ToFHardware/mitkToFCameraMESASR4000Controller.cpp new file mode 100644 index 0000000000..83df779645 --- /dev/null +++ b/Modules/ToFHardware/mitkToFCameraMESASR4000Controller.cpp @@ -0,0 +1,103 @@ +/*========================================================================= + +Program: Medical Imaging & Interaction Toolkit +Module: $RCSfile$ +Language: C++ +Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ +Version: $Revision: $ + +Copyright (c) German Cancer Research Center, Division of Medical and +Biological Informatics. All rights reserved. +See MITKCopyright.txt or http://www.mitk.org/copyright.html 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. + +=========================================================================*/ +#include "mitkToFCameraMESASR4000Controller.h" +#include "mitkToFConfig.h" +#include + +extern CMesaDevice* m_MESAHandle; + +namespace mitk +{ + ToFCameraMESASR4000Controller::ToFCameraMESASR4000Controller() + { + } + + ToFCameraMESASR4000Controller::~ToFCameraMESASR4000Controller() + { + } + + bool ToFCameraMESASR4000Controller::OpenCameraConnection() + { + if(!m_ConnectionCheck) + { + /* + m_PMDRes = pmdOpen (&m_PMDHandle , m_SourcePlugin , m_SourceParam , m_ProcPlugin , m_ProcParam ); + m_ConnectionCheck = ErrorText(m_PMDRes); + if (!m_ConnectionCheck) + { + return m_ConnectionCheck; + } + + // get image properties from camera + this->UpdateCamera(); + m_PMDRes = pmdGetSourceDataDescription(m_PMDHandle, &m_DataDescription); + ErrorText(m_PMDRes); + m_CaptureWidth = m_DataDescription.img.numColumns; + m_CaptureHeight = m_DataDescription.img.numRows; + m_PixelNumber = m_CaptureWidth*m_CaptureHeight; + m_NumberOfBytes = m_PixelNumber * sizeof(float); + m_SourceDataSize = m_DataDescription.size; + m_SourceDataStructSize = m_DataDescription.size + sizeof(PMDDataDescription); + + char serial[16]; + m_PMDRes = pmdSourceCommand (m_PMDHandle, serial, 16, "GetSerialNumber"); + ErrorText(m_PMDRes); + + MITK_INFO << "Serial-No: " << serial <m_SourceDataSize <GetIntegrationTime(); + MITK_INFO << "Modulation Frequence: " << this->GetModulationFrequency(); + return m_ConnectionCheck; + */ + //m_MESARes = SR_OpenDlg(&m_MESAHandle, 3, 0); + this->m_MESARes = SR_OpenUSB(&m_MESAHandle, 0); + this->m_ConnectionCheck = ErrorText(this->m_MESARes); + if (m_MESARes <= 0) + { + this->m_ConnectionCheck = false; + return this->m_ConnectionCheck; + } + + this->m_MESARes = SR_GetRows(m_MESAHandle); + this->m_CaptureHeight = this->m_MESARes; + this->m_MESARes = SR_GetCols(m_MESAHandle); + this->m_CaptureWidth = this->m_MESARes; + this->m_PixelNumber = this->m_CaptureWidth*this->m_CaptureHeight; + this->m_NumberOfBytes = this->m_PixelNumber * sizeof(float); + + ImgEntry* imgEntryArray; + this->m_NumImg = SR_GetImageList(m_MESAHandle, &imgEntryArray); + + float lambda[MF_LAST]={3.75f, 5.f, 7.142857f, 7.5f, 7.894737f, 2.5f, 10.f, 15.f, 5.172414f, 4.838710f, 10.344828f, 9.677419f};//MF_40MHz,MF_30MHz,MF_21MHz,MF_20MHz,MF_19MHz,... + float frequency[MF_LAST]={40.00f, 30.00f, 21.00f, 20.00f, 19.00f, 60.00f, 15.00f, 10.00f, 29.00f, 31.00f, 14.50f, 15.50f}; + ModulationFrq frq = SR_GetModulationFrequency(m_MESAHandle); + + this->m_MaxRangeFactor = (lambda[frq] * 1000.00) / (float)0xffff; + + char deviceText[1024]; + this->m_MESARes = SR_GetDeviceString(m_MESAHandle, deviceText, _countof(deviceText));//returns the device ID used in other calls + MITK_INFO << "Device ID: " << deviceText <m_NumImg <m_CaptureWidth << " x " << this->m_CaptureHeight < + +extern CMesaDevice* m_MESAHandle; + +namespace mitk +{ + ToFCameraMESASR4000Controller::ToFCameraMESASR4000Controller() + { + MITK_WARN("ToF") << "Error: MESA SR4000 currently not available"; + } + + ToFCameraMESASR4000Controller::~ToFCameraMESASR4000Controller() + { + MITK_WARN("ToF") << "Error: MESA SR4000 currently not available"; + } + + bool ToFCameraMESASR4000Controller::OpenCameraConnection() + { + MITK_WARN("ToF") << "Error: MESA SR4000 currently not available"; + return true; + } +} diff --git a/Modules/ToFHardware/mitkToFCameraMESASR4000Device.cpp b/Modules/ToFHardware/mitkToFCameraMESASR4000Device.cpp new file mode 100644 index 0000000000..a98cc70c23 --- /dev/null +++ b/Modules/ToFHardware/mitkToFCameraMESASR4000Device.cpp @@ -0,0 +1,81 @@ +/*========================================================================= + +Program: Medical Imaging & Interaction Toolkit +Module: $RCSfile$ +Language: C++ +Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ +Version: $Revision: $ + +Copyright (c) German Cancer Research Center, Division of Medical and +Biological Informatics. All rights reserved. +See MITKCopyright.txt or http://www.mitk.org/copyright.html 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. + +=========================================================================*/ +#include "mitkToFCameraMESASR4000Device.h" +#include "mitkToFCameraMESASR4000Controller.h" +#include "mitkRealTimeClock.h" + +#include "itkMultiThreader.h" + + +namespace mitk +{ + ToFCameraMESASR4000Device::ToFCameraMESASR4000Device() + { + m_Controller = ToFCameraMESASR4000Controller::New(); + } + + ToFCameraMESASR4000Device::~ToFCameraMESASR4000Device() + { + } + + void ToFCameraMESASR4000Device::SetRegionOfInterest(unsigned int leftUpperCornerX, unsigned int leftUpperCornerY, unsigned int width, unsigned int height) + { + //if (m_Controller.IsNotNull()) + //{ + // dynamic_cast(m_Controller.GetPointer())->SetRegionOfInterest(leftUpperCornerX,leftUpperCornerY,width,height); + //} + } + + void ToFCameraMESASR4000Device::SetProperty( const char *propertyKey, BaseProperty* propertyValue ) + { + this->m_PropertyList->SetProperty(propertyKey, propertyValue); + + ToFCameraMESASR4000Controller::Pointer myController = dynamic_cast(this->m_Controller.GetPointer()); + + bool boolValue = false; + GetBoolProperty(propertyValue, boolValue); + if (strcmp(propertyKey, "SetFPNCalibration") == 0) + { + //myController->SetFPNCalibration(boolValue); + } + else if (strcmp(propertyKey, "SetFPPNCalibration") == 0) + { + //myController->SetFPPNCalibration(boolValue); + } + else if (strcmp(propertyKey, "SetLinearityCalibration") == 0) + { + //myController->SetLinearityCalibration(boolValue); + } + else if (strcmp(propertyKey, "SetLensCalibration") == 0) + { + //myController->SetLensCalibration(boolValue); + } + else if (strcmp(propertyKey, "SetExposureMode") == 0) + { + if (boolValue) + { + //myController->SetExposureMode(1); + } + else + { + //myController->SetExposureMode(0); + } + } + } + +} diff --git a/Modules/ToFHardware/mitkToFCameraMESASR4000Device.h b/Modules/ToFHardware/mitkToFCameraMESASR4000Device.h new file mode 100644 index 0000000000..067f40e6af --- /dev/null +++ b/Modules/ToFHardware/mitkToFCameraMESASR4000Device.h @@ -0,0 +1,72 @@ +/*========================================================================= + +Program: Medical Imaging & Interaction Toolkit +Module: $RCSfile$ +Language: C++ +Date: $Date: 2010-05-27 16:06:53 +0200 (Do, 27 Mai 2010) $ +Version: $Revision: $ + +Copyright (c) German Cancer Research Center, Division of Medical and +Biological Informatics. All rights reserved. +See MITKCopyright.txt or http://www.mitk.org/copyright.html 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 __mitkToFCameraMESASR4000Device_h +#define __mitkToFCameraMESASR4000Device_h + +#include "mitkToFHardwareExports.h" +#include "mitkCommon.h" +#include "mitkToFCameraDevice.h" +#include "mitkToFCameraMESADevice.h" +#include "mitkToFCameraMESASR4000Controller.h" + +#include "itkObject.h" +#include "itkObjectFactory.h" +#include "itkMultiThreader.h" +#include "itkFastMutexLock.h" + + +namespace mitk +{ + /** + * @brief Device class representing a MESA CamBoard camera + * + * @ingroup ToFHardware + */ + class MITK_TOFHARDWARE_EXPORT ToFCameraMESASR4000Device : public ToFCameraMESADevice + { + public: + + mitkClassMacro( ToFCameraMESASR4000Device , ToFCameraMESADevice ); + + itkNewMacro( Self ); + + /*! + \brief set a BaseProperty + */ + virtual void SetProperty( const char *propertyKey, BaseProperty* propertyValue ); + /*! + \brief Setting the region of interest, the camera is configured to only output a certain area of the image. + \param leftUpperCornerX x value of left upper corner of region + \param leftUpperCornerX y value of left upper corner of region + \param width width of region + \param height height of region + */ + void SetRegionOfInterest( unsigned int leftUpperCornerX, unsigned int leftUpperCornerY, unsigned int width, unsigned int height ); + + protected: + + ToFCameraMESASR4000Device(); + + ~ToFCameraMESASR4000Device(); + + + private: + + }; +} //END mitk namespace +#endif