00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "stdafx.h"
00037
00038 #include <cfl/resource.h>
00039
00040 #include <Camera/InterfaceDll.h>
00041
00042 #include <cfl/ScanFilterLinearRegression.h>
00043 #include <cfl/FilterDlg_LinearRegression.h>
00044
00045 #pragma warning( push, 3 ) // header file limits requires warning level 3
00046
00047 #include <limits>
00048
00049 #pragma warning( pop )
00050
00051 #ifdef _DEBUG
00052 #undef THIS_FILE
00053 static char THIS_FILE[]=__FILE__;
00054 #define new DEBUG_NEW
00055 #endif
00056
00057 #define CFL_SCHEMAVERSION_FILTERLINEARREGRESSION 0
00058
00059
00060 LPCTSTR CScanFilterLinearRegression::m_lpcsFilterName = _T( "Linear-Regression" );
00061
00062 LPCTSTR CScanFilterLinearRegression::m_lpcsShortFilterName = _T( "LinReg" );
00063
00064 IMPLEMENT_SERIAL( CScanFilterLinearRegression, CScanFilter, CFL_SCHEMAVERSION_FILTERLINEARREGRESSION )
00065
00066
00067
00068
00069 const int CScanFilterLinearRegression::def_iXY = 9;
00070
00071
00072
00073
00074 const double CScanFilterLinearRegression::def_dFactor = 100;
00075
00076
00077
00078
00079 const LinearRegressionImageFilter::FeatureSelector CScanFilterLinearRegression::def_eParameter = LinearRegressionImageFilter::AlphaPi;
00080
00081
00082
00083
00084
00085 CScanFilterLinearRegression::CScanFilterLinearRegression() : CScanFilter( ),
00086 m_iSizeX ( def_iXY ),
00087 m_iSizeY ( def_iXY ),
00088 m_dFactor ( def_dFactor ),
00089 m_eParameter( def_eParameter )
00090 {
00091 m_csFilterName = m_lpcsFilterName;
00092
00093 ReadFilterSettings();
00094 }
00095
00096
00097
00098
00099
00100 CScanFilterLinearRegression::~CScanFilterLinearRegression()
00101 {
00102 ;
00103 }
00104
00105
00106
00107
00108
00109
00110 void CScanFilterLinearRegression::Serialize( CArchive& ar )
00111 {
00112
00113
00114 CScanFilter::Serialize( ar );
00115
00116 if ( ar.IsStoring() )
00117 {
00118 ar << m_iSizeX << m_iSizeY << static_cast<int>( m_eParameter ) << m_dFactor;
00119 }
00120 else
00121 {
00122 int iParameter;
00123 ar >> m_iSizeX >> m_iSizeY >> iParameter >> m_dFactor;
00124 m_eParameter = static_cast<FeatureSelector>( iParameter );
00125 }
00126 }
00127
00128 #if 0
00129
00130
00131
00132 BOOL CScanFilterLinearRegression::CanDoPartialData() const
00133 {
00134 return TRUE;
00135 }
00136 #endif
00137
00138 #if 0
00139
00140
00141
00142 BOOL CScanFilterLinearRegression::CanDoDialogEntry() const
00143 {
00144 return FALSE;
00145 }
00146 #endif
00147
00148
00149
00150
00151
00152 void CScanFilterLinearRegression::ReadFilterSettings( )
00153 {
00154
00155
00156 #ifdef NO_FILTER_SETTINGS
00157
00158
00159
00160 #else
00161 CWinApp* pApp = AfxGetApp();
00162
00163 if ( Q_INVALID( NULL == pApp ) )
00164 return ;
00165
00166
00167
00168
00169 SetParameters( pApp->GetProfileString( gCflRegistrySubkey, m_lpcsFilterName, _T( "5,5,1,100" ) ) );
00170 #endif
00171 }
00172
00173
00174
00175
00176
00177 void CScanFilterLinearRegression::WriteFilterSettings() const
00178 {
00179 #ifdef NO_FILTER_SETTINGS
00180
00181
00182
00183 #else
00184 CWinApp* pApp = AfxGetApp();
00185
00186 if ( Q_INVALID( NULL == pApp ) )
00187 return ;
00188
00189
00190
00191
00192 LPCTSTR pStr = GetParameters();
00193
00194 if ( pStr )
00195 {
00196 pApp->WriteProfileString( gCflRegistrySubkey, m_lpcsFilterName, pStr );
00197 delete const_cast<LPTSTR>( pStr );
00198 }
00199 #endif
00200 }
00201
00202
00203
00204
00205
00206
00207 LPCTSTR CScanFilterLinearRegression::GetParameters( ) const
00208 {
00209
00210
00211
00212
00213
00214 CString csParameters;
00215
00216 csParameters.Format( _T( "%d,%d,%d,%g,%g" ),
00217 m_iSizeX, m_iSizeY, m_eParameter, m_dTheta, m_dFactor );
00218
00219 return strcpy( new TCHAR[ csParameters.GetLength() + 1 ], csParameters );
00220 }
00221
00222
00223
00224
00225
00226 BOOL CScanFilterLinearRegression::SetParameters( LPCTSTR lpParameters )
00227 {
00228
00229
00230 if ( Q_INVALID( NULL == lpParameters ) &&
00231 "CScanFilterLinearRegression (SetParameters): parameters expected, none provided (NULL).")
00232 {
00233 return FALSE;
00234 }
00235
00236
00237
00238
00239 m_iSizeX = def_iXY;
00240 m_iSizeY = def_iXY;
00241 m_dFactor = def_dFactor;
00242 m_eParameter = def_eParameter;
00243
00244 int nfields = _stscanf( lpParameters, _T( "%d,%d,%d,%lg,%lg" ),
00245 &m_iSizeX, &m_iSizeY, &m_eParameter, &m_dTheta, &m_dFactor );
00246
00247 if ( Q_INVALID( 4 > nfields &&
00248 "CScanFilterLinearRegression (SetParameters): X- and Y-size, parameter and factor expected, got fewer." ) )
00249 {
00250 return FALSE;
00251 }
00252
00253 return TRUE;
00254 }
00255
00256
00257
00258
00259
00260 BOOL CScanFilterLinearRegression::RunModeless( CWnd* pParentWnd, CDocument* pDoc )
00261 {
00262
00263
00264 CFilterDlg_LinearRegressionPtr pDlg = new CFilterDlg_LinearRegression();
00265
00266 if ( Q_INVALID( NULL == pDlg ) )
00267 return FALSE;
00268
00269 pDlg->SetParameters( this, pDoc );
00270
00271
00272
00273
00274
00275
00276
00277
00278 pDlg->Create( IDD_FILTERDLG_LINEARREGRESSION, pParentWnd );
00279
00280 m_pDlg = pDlg;
00281
00282
00283
00284
00285
00286 BOOL bRet = PreApply();
00287
00288 if ( bRet )
00289 {
00290 pDlg->UpdateView();
00291 }
00292
00293 return bRet;
00294 }
00295
00296
00297
00298
00299
00300 BOOL CScanFilterLinearRegression::PreApply()
00301 {
00302
00303
00304 BOOL bRet = FALSE;
00305
00306 try
00307 {
00308 bRet = PreApplyCore();
00309 }
00310 catch ( CException* e )
00311 {
00312 Q_MFC_EXCEPTION( e );
00313 e->Delete();
00314 }
00315 catch ( const std::exception& e )
00316 {
00317 Q_STD_EXCEPTION( e );
00318 }
00319
00320 return bRet;
00321 }
00322
00323
00324
00325
00326
00327 BOOL CScanFilterLinearRegression::Apply()
00328 {
00329
00330
00331 BOOL bRet = FALSE;
00332
00333 try
00334 {
00335 bRet = ApplyCore();
00336 }
00337 catch ( CException* e )
00338 {
00339 Q_MFC_EXCEPTION( e );
00340 e->Delete();
00341 }
00342 catch ( const std::exception& e )
00343 {
00344 Q_STD_EXCEPTION( e );
00345 #if 0
00346 if ( m_pDlg )
00347
00348 {
00349 ::MessageBox(
00350 AfxGetMainWnd()->m_hWnd, e.what(), _T("Linear Regression Filter Failure"), MB_OK | MB_ICONWARNING );
00351 }
00352 #endif
00353 }
00354
00355 return bRet;
00356 }
00357
00358
00359
00360
00361 CharCptr CScanFilterLinearRegression::ParamName()
00362 {
00363 switch( Parameter() )
00364 {
00365 case LinearRegressionImageFilter::A: return "A";
00366 case LinearRegressionImageFilter::B: return "B";
00367 case LinearRegressionImageFilter::C: return "C";
00368 case LinearRegressionImageFilter::AlphaPi: return "Alpha[Pi]";
00369 case LinearRegressionImageFilter::AlphaDeg: return "Alpha[deg]";
00370 case LinearRegressionImageFilter::BetaPi: return "Beta[Pi]";
00371 case LinearRegressionImageFilter::BetaDeg: return "Beta[deg]";
00372 case LinearRegressionImageFilter::AngleLength: return "Length";
00373 case LinearRegressionImageFilter::AngleLengthNeg: return "Length (Neg)";
00374 case LinearRegressionImageFilter::AngleLengthPi: return "Length[Pi]";
00375 case LinearRegressionImageFilter::AngleLengthPiNeg: return "Length[Pi] (Neg)";
00376 case LinearRegressionImageFilter::AngleLengthDeg: return "Length[deg]";
00377 case LinearRegressionImageFilter::AngleLengthDegNeg: return "Length[deg] (Neg)";
00378 case LinearRegressionImageFilter::AngleSum: return "Sum";
00379 case LinearRegressionImageFilter::AngleSumPi: return "Sum[Pi]";
00380 case LinearRegressionImageFilter::AngleSumDeg: return "Sum[deg]";
00381 default: return "[unrcognized]";
00382 }
00383 }
00384
00385
00386
00387
00388
00389
00390 BOOL CScanFilterLinearRegression::PreApplyCore()
00391 {
00392 if ( Q_INVALID( NULL == m_lpsbIn ) && "ensure valid input buffer" )
00393 return FALSE;
00394
00395 if ( Q_INVALID( NULL == m_lpsbOut ) && "ensure valid output buffer" )
00396 return FALSE;
00397
00398
00399
00400
00401
00402 Q_RETURN( m_lpsbOut->CreateOutputBufferFor( *m_lpsbIn ) );
00403
00404 switch ( Parameter() )
00405 {
00406 case LinearRegressionImageFilter::C:
00407 m_lpsbOut->m_csBufferName.Format( _T("%s - %s %dx%d %s"),
00408 m_lpsbIn->m_csBufferName, m_lpcsShortFilterName, Ncols(), Nrows(), ParamName() );
00409 break;
00410
00411 case LinearRegressionImageFilter::AngleSum:
00412 case LinearRegressionImageFilter::AngleSumPi:
00413 case LinearRegressionImageFilter::AngleSumDeg:
00414 m_lpsbOut->m_csBufferName.Format( _T("%s - %s %dx%d %g·%s(%g\xB0)" ),
00415 m_lpsbIn->m_csBufferName, m_lpcsShortFilterName, Ncols(), Nrows(), Factor(), ParamName(), Theta() );
00416 break;
00417
00418 default:
00419 m_lpsbOut->m_csBufferName.Format( _T("%s - %s %dx%d %g·%s"),
00420 m_lpsbIn->m_csBufferName, m_lpcsShortFilterName, Ncols(), Nrows(), Factor(), ParamName() );
00421 break;
00422 }
00423
00424
00425
00426
00427 if ( IsInteractive() )
00428 {
00429 m_pDlg->SetDlgItemText( IDC_BUFFEROUTNAME, m_lpsbOut->m_csBufferName );
00430 }
00431
00432 return TRUE;
00433 }
00434
00435
00436
00437
00438
00439 BOOL CScanFilterLinearRegression::ApplyCore()
00440 {
00441
00442
00443
00444 if ( !PreApplyCore() )
00445 {
00446 return FALSE;
00447 }
00448
00449
00450
00451
00452
00453
00454 LinearRegressionImageFilter filter( ToRadius( Ncols(), Nrows() ) );
00455
00456 filter.Select ( Parameter() );
00457 filter.SetTheta ( Theta() );
00458 filter.SetFactor( Factor() );
00459
00460
00461 if ( std::numeric_limits< CScanBaseBuffer::SpacingType >::epsilon() < m_lpsbIn->GetSpacingX() )
00462 {
00463 filter.SetNorm( m_lpsbIn->GetSpacingZ() / m_lpsbIn->GetSpacingX() );
00464 }
00465 else
00466 {
00467 filter.SetNorm( 1.0 );
00468 }
00469
00470
00471 filter.Apply( ToImage( m_lpsbIn ), ToImage( m_lpsbOut ) );
00472
00473 if ( m_lpsbIn->IsBidirectionalScan() && m_lpsbOut->IsBidirectionalScan() )
00474 {
00475
00476 filter.Apply( ToImage( m_lpsbIn, true ), ToImage( m_lpsbOut, true ) );
00477 }
00478
00479
00480
00481
00482
00483 m_lpsbOut->m_dwParameterMask = 0;
00484
00485 return TRUE;
00486 }
00487
00488
00489
00490
00491 SPerformanceTestResult CScanFilterLinearRegression::GetPerformanceTestResult()
00492 {
00493 SPerformanceTestResult result;
00494
00495 typedef long Count;
00496
00497 CPerfTimer Timer;
00498
00499
00500
00501
00502
00503 ;
00504
00505
00506
00507
00508 Time timeToProcess = 0.4;
00509 Count countProcessed = 0;
00510
00511 Timer.Start( true );
00512 while ( Timer.Elapsed() < timeToProcess )
00513 {
00514 LinearRegressionImageFilter filter( ToRadius( Ncols(), Nrows() ) );
00515
00516 filter.Select ( Parameter() );
00517 filter.SetFactor( Factor() );
00518
00519
00520 filter.Apply( ToImage( m_lpsbIn ), ToImage( m_lpsbOut ) );
00521
00522 ++countProcessed;
00523 }
00524
00525 Timer.Stop();
00526 result.timeBuffer = Timer.Elapsed() / countProcessed;
00527
00528 if ( m_lpsbIn->IsBidirectionalScan() && m_lpsbOut->IsBidirectionalScan() )
00529 {
00530 result.timeBuffer *= 2;
00531 }
00532
00533 result.totalTime = result.timePrepare + result.timeBuffer;
00534
00535 return result;
00536 }
00537
00538
00539
00540