📄 ltkpreprocessor.cpp
字号:
/*****************************************************************************************
* Copyright (c) 2006 Hewlett-Packard Development Company, L.P.
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*****************************************************************************************/
/************************************************************************
* SVN MACROS
*
* $LastChangedDate: 2008-08-07 17:21:00 +0530 (Thu, 07 Aug 2008) $
* $Revision: 597 $
* $Author: mnab $
*
************************************************************************/
/************************************************************************
* FILE DESCR: Implementation of LTKPreprocessor which is a library of standard pre-processing functions
*
* CONTENTS:
* LTKPreprocessor
* ~LTKPreprocessor
* normalizeSize
* normalizeOrientation
* resampleTrace
* smoothenTraceGroup
* setFilterLength
* centerTraces
* dehookTraces
* orderTraces
* reverseTrace
* duplicatePoints
* getNormalizedSize
* getSizeThreshold
* getLoopThreshold
* getAspectRatioThreshold
* getDotThreshold
* setNormalizedSize
* setSizeThreshold
* setLoopThreshold
* setAspectRatioThreshold
* setDotThreshold
* setHookLengthThreshold1
* setHookLengthThreshold2
* setHookAngleThreshold
* calculateSlope
* calculateEuclidDist
* getQuantisedSlope
* determineDominantPoints
* computeTraceLength
* removeDuplicatePoints
* calculateSweptAngle
* initFunAddrMap
* getPreprocptr
* setPreProcAttributes
* initPreprocFactoryDefaults
*
* AUTHOR: Balaji R.
*
* DATE: December 23, 2004
* CHANGE HISTORY:
* Author Date Description of change
************************************************************************/
#include "LTKTypes.h"
#include "LTKPreprocessor.h"
#include "LTKStringUtil.h"
#include "LTKLoggerUtil.h"
#include "LTKPreprocDefaults.h"
#include "LTKTraceGroup.h"
#include "LTKShapeRecoConfig.h"
#include "LTKInkUtils.h"
#include "LTKTrace.h"
#include "LTKErrors.h"
#include "LTKErrorsList.h"
#include "LTKConfigFileReader.h"
#include "LTKException.h"
#include "LTKChannel.h"
/**********************************************************************************
* AUTHOR : Balaji R.
* DATE : 23-DEC-2004
* NAME : LTKPreprocessor
* DESCRIPTION : Copy Constructor
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
LTKPreprocessor:: LTKPreprocessor(const LTKPreprocessor& preprocessor)
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "Entered copy constructor of LTKPreprocessor" <<endl;
initFunAddrMap();
initPreprocFactoryDefaults();
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "Exiting copy constructor of LTKPreprocessor" <<endl;
}
/**********************************************************************************
* AUTHOR : Balaji R.
* DATE : 23-DEC-2004
* NAME : ~LTKPreprocessor
* DESCRIPTION : destructor
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
LTKPreprocessor:: ~LTKPreprocessor()
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "Entered destructor of LTKPreprocessor" <<endl;
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "Exiting destructor of LTKPreprocessor" <<endl;
}
/**********************************************************************************
* AUTHOR : Dinesh M
* DATE : 13-Oct-2006
* NAME : getCaptureDevice
* DESCRIPTION : get the value of the member variable m_captureDevice
* ARGUMENTS :
* RETURNS : returns m_captureDevice
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
const LTKCaptureDevice& LTKPreprocessor::getCaptureDevice() const
{
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) <<"Entered getCaptureDevice" <<endl;
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting getCaptureDevice" <<endl;
return m_captureDevice;
}
/**********************************************************************************
* AUTHOR : Dinesh M
* DATE : 13-Oct-2006
* NAME : getScreenContext
* DESCRIPTION : get the value of the member variable m_screenContext
* ARGUMENTS :
* RETURNS : returns m_screenContext
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
const LTKScreenContext& LTKPreprocessor::getScreenContext() const
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "Entered getScreenContext" <<endl;
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "Exiting getScreenContext" <<endl;
return m_screenContext;
}
/**********************************************************************************
* AUTHOR : Dinesh M
* DATE : 13-Oct-2006
* NAME : setCaptureDevice
* DESCRIPTION : sets the member variable m_captureDevice
* ARGUMENTS :
*
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
void LTKPreprocessor::setCaptureDevice(const LTKCaptureDevice& captureDevice)
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "Entered setCaptureDevice" <<endl;
m_captureDevice = captureDevice;
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "Exiting setCaptureDevice" <<endl;
}
/**********************************************************************************
* AUTHOR : Dinesh M
* DATE : 13-Oct-2006
* NAME : setScreenContext
* DESCRIPTION : sets the member variable m_screenContext
* ARGUMENTS :
*
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
void LTKPreprocessor::setScreenContext(const LTKScreenContext& screenContext)
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "Entered setScreenContext" <<endl;
m_screenContext = screenContext;
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "Exiting setScreenContext" <<endl;
}
/**********************************************************************************
* AUTHOR : Dinesh M
* DATE : 24-Aug-2006
* NAME : normalizeSize
* DESCRIPTION : normalizes the size of the incoming trace group
* ARGUMENTS : inTraceGroup - incoming tracegroup which is to be size normalized
* outTraceGroup - size noramlized inTraceGroup
* RETURNS : SUCCESS on successful size normalization
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
int LTKPreprocessor::normalizeSize(const LTKTraceGroup& inTraceGroup, LTKTraceGroup& outTraceGroup)
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "Entered LTKPreprocessor::normalizeSize" <<endl;
float xScale; // scale factor along the x direction
float yScale; // scale factor along the y direction
float aspectRatio; // aspect ratio of the trace group
vector<LTKTrace> normalizedTracesVec; // holds the normalized traces
LTKTrace trace; // a trace of the trace group
floatVector xVec; // x channel values of a trace
floatVector yVec; // y channel values of a trace
floatVector normalizedXVec; // normalized x channel values of a trace
floatVector normalizedYVec; // normalized y channel values of a trace
float scaleX, scaleY, offsetX, offsetY;
float xMin,yMin,xMax,yMax;
int errorCode;
// getting the bounding box information of the input trace group
if( (errorCode = inTraceGroup.getBoundingBox(xMin,yMin,xMax,yMax)) != SUCCESS)
{
LOG(LTKLogger::LTK_LOGLEVEL_ERR)
<<"Error: LTKPreprocessor::normalizeSize"<<endl;
LTKReturnError(errorCode);
}
outTraceGroup = inTraceGroup;
// width of the bounding box at scalefactor = 1
xScale = ((float)fabs(xMax - xMin))/inTraceGroup.getXScaleFactor();
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "xScale = " << xScale <<endl;
// height of the bounding box at scalefactor = 1
yScale = ((float)fabs(yMax - yMin))/inTraceGroup.getYScaleFactor();
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<< "yScale = " << yScale <<endl;
if(m_preserveAspectRatio)
{
if(yScale > xScale)
{
aspectRatio = (xScale > EPS) ? (yScale/xScale) : m_aspectRatioThreshold + EPS;
}
else
{
aspectRatio = (yScale > EPS) ? (xScale/yScale) : m_aspectRatioThreshold + EPS;
}
if(aspectRatio > m_aspectRatioThreshold)
{
if(yScale > xScale)
xScale = yScale;
else
yScale = xScale;
}
}
offsetY = 0.0f;
if(m_preserveRelativeYPosition )
{
offsetY = (yMin+yMax)/2.0f;
}
if(xScale <= (m_dotThreshold * m_captureDevice.getXDPI()) && yScale <= (m_dotThreshold * m_captureDevice.getYDPI()))
{
offsetX = PREPROC_DEF_NORMALIZEDSIZE/2;
offsetY += PREPROC_DEF_NORMALIZEDSIZE/2;
outTraceGroup.emptyAllTraces();
for(int traceIndex=0;traceIndex<inTraceGroup.getNumTraces();++traceIndex)
{
LTKTrace tempTrace;
inTraceGroup.getTraceAt(traceIndex,tempTrace);
vector<float> newXChannel(tempTrace.getNumberOfPoints(),offsetX);
vector<float> newYChannel(tempTrace.getNumberOfPoints(),offsetY);
tempTrace.reassignChannelValues(X_CHANNEL_NAME, newXChannel);
tempTrace.reassignChannelValues(Y_CHANNEL_NAME, newYChannel);
outTraceGroup.addTrace(tempTrace);
}
return SUCCESS;
}
// finding the final scale and offset values for the x channel
if((!m_preserveAspectRatio )&&(xScale < (m_sizeThreshold*m_captureDevice.getXDPI())))
{
scaleX = 1.0f;
offsetX = PREPROC_DEF_NORMALIZEDSIZE/2.0 ;
}
else
{
scaleX = PREPROC_DEF_NORMALIZEDSIZE / xScale ;
offsetX = 0.0;
}
// finding the final scale and offset values for the y channel
if((!m_preserveAspectRatio )&&(yScale < (m_sizeThreshold*m_captureDevice.getYDPI())))
{
offsetY += PREPROC_DEF_NORMALIZEDSIZE/2;
scaleY = 1.0f;
}
else
{
scaleY = PREPROC_DEF_NORMALIZEDSIZE / yScale ;
}
//scaling the copy of the inTraceGroup in outTraceGroup according to new scale factors
//and translating xmin_ymin of the trace group bounding box to the point (offsetX,offsetY).
//Even though absolute location has to be specified for translateToX and translateToY,
//since offsetX and offsetY are computed with respect to origin, they serve as absolute values
if( (errorCode = outTraceGroup.affineTransform(scaleX,scaleY,offsetX,offsetY,XMIN_YMIN)) != SUCCESS)
{
LOG(LTKLogger::LTK_LOGLEVEL_ERR)
<<"Error: LTKPreprocessor::normalizeSize"<<endl;
LTKReturnError(errorCode);
}
return SUCCESS;
}
/**********************************************************************************
* AUTHOR : Balaji R.
* DATE : 23-DEC-2004
* NAME : normalizeOrientation
* DESCRIPTION : normalizes the orientation of the incoming trace group
* ARGUMENTS : inTraceGroup - incoming tracegroup which is to be orientation normalized
* outTraceGroup - orientation noramlized inTraceGroup
* RETURNS : SUCCESS on successful orientation normalization
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
int LTKPreprocessor::normalizeOrientation(const LTKTraceGroup& inTraceGroup, LTKTraceGroup& outTraceGroup)
{
int traceIndex;
int numTraces;
float bboxDiagonalLength;
float initialXCoord, finalXCoord;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -