|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: Shadow.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of Shadow.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "shadow.h"
#include "detector.h"
//#include "..\LgLib\version.h"
bool CShadow::AddShadow( int16_t nAngle, uint8_t bType, int16_t nMin, int16_t nMax ){
if( LGS_MAX_SHADOW_ANGLES <= nAngle ) return false;
// range should be in the same system ( 0..2047 )
LPRAY_LINE pShadowLine = &m_pShadowLine[ (int16_t)nAngle ];
// realloc memory for shadows
pShadowLine->lpShadow = ( LPRAY ) realloc( pShadowLine->lpShadow, sizeof( RAY ) * ( pShadowLine->nCount + 1 ) ); if( !pShadowLine->lpShadow ){ pShadowLine->nCount = 0; return false; }
// store shadow
pShadowLine->lpShadow[ pShadowLine->nCount ].bType = bType; pShadowLine->lpShadow[ pShadowLine->nCount ].nMin = nMin; pShadowLine->lpShadow[ pShadowLine->nCount ].nMax = nMax; pShadowLine->nCount++;
return true;};
void CShadow::Destroy(){ #ifdef DEBUG_PRINTOUT_MSGS
debugProcBegin("Shadow Destroy..."); #endif
for( int16_t i = 0; i < LGS_MAX_SHADOW_ANGLES; i++ ){
if( m_pShadowLine[i].lpShadow ){ free( m_pShadowLine[i].lpShadow ); m_pShadowLine[i].lpShadow = NULL; }; m_pShadowLine[i].nCount = 0; };
for( int16_t i = 0; i < m_nPolygon; i++ ) if( m_lpPolygon[i].lpPoint ) free( m_lpPolygon[i].lpPoint );
m_nPolygon = 0;
if( m_lpPolygon ){ free( m_lpPolygon ); m_lpPolygon = NULL; }; #ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd(); #endif
};
bool CShadow::LoadRays( PCHAR pFileName ){
FILE *pFile = fopen( pFileName, "rt" ); if( !pFile ) return true;
// while not end-of-file
while( !feof( pFile ) ) {
const int16_t nMaxLineLength = 128;
CHAR pBuffer[ nMaxLineLength ];
// get line string from file
if( fgets( pBuffer, nMaxLineLength, pFile ) ){ int16_t nAngle; int16_t bType; int16_t nMin; int16_t nMax;
// load shadow from line
if( sscanf( pBuffer, "%d %d %d %d", &nAngle, &bType, &nMin, &nMax ) == 4 ) // add shadow
AddShadow( nAngle, bType, nMin, nMax ); }; };
fclose( pFile ); return true;};
bool CShadow::LoadPolygons( PCHAR pFileName ){
FILE *pFile = fopen( pFileName, "rt" ); if( !pFile ) return true;
int16_t nPolygon = 0; int16_t nPolygonPoint = 0;
// while not end-of-file
while( !feof( pFile ) ) {
const int16_t nMaxLineLength = 1024;
CHAR pBuffer[ nMaxLineLength ];
// get line string from file
if( fgets( pBuffer, nMaxLineLength, pFile ) ){ // load shadow from line
if( strstr( pBuffer, "type" ) ){ DWORD dwTypeOn; if( sscanf( pBuffer, "type %lu", &dwTypeOn ) == 1 ){ m_dwTypeOn = dwTypeOn; for( int16_t i = 0; i < LGS_MAX_SHADOW_TYPES; i++ ){ DWORD bBit = 1 << i; m_pTypeOn[i] = ( m_dwTypeOn & bBit ) == bBit; }; }; } else { // load shadow from line
if( strstr( pBuffer, "polygon" ) ) { int16_t nType; if( sscanf( pBuffer, "polygon %d", &nType ) == 1 ){ nPolygon++; nPolygonPoint = 0;
if( m_nPolygon < nPolygon ){ m_lpPolygon = (LPHZ_POLYGON) realloc( m_lpPolygon, nPolygon * sizeof(HZ_POLYGON) ); if( !m_lpPolygon ){ m_nPolygon = 0; fclose( pFile ); return true; }; m_lpPolygon[nPolygon-1].lpPoint = NULL; m_lpPolygon[nPolygon-1].wCount = 0; m_lpPolygon[nPolygon-1].bType = nType; m_nPolygon = nPolygon; }; }; } else if( nPolygon ) { int16_t nX,nY; if( sscanf( pBuffer, "%d %d", &nX, &nY ) == 2 ) { nPolygonPoint++;
if( m_lpPolygon[nPolygon-1].wCount < nPolygonPoint ) { m_lpPolygon[nPolygon-1].lpPoint = (LGS::LPHZ_POINT) realloc( m_lpPolygon[nPolygon-1].lpPoint, nPolygonPoint * sizeof(LGS::HZ_POINT) ); if( !m_lpPolygon[nPolygon-1].lpPoint ) { m_lpPolygon[nPolygon-1].wCount = 0; fclose( pFile ); return true; }; m_lpPolygon[nPolygon-1].lpPoint[nPolygonPoint-1].wX = nX; m_lpPolygon[nPolygon-1].lpPoint[nPolygonPoint-1].wY = nY; m_lpPolygon[nPolygon-1].wCount = nPolygonPoint; }; }; }; }; }; };
fclose( pFile ); return true;};
bool CShadow::SaveRays( PCHAR pFileName ){
FILE *pFile = fopen( pFileName, "wt" );
if( !pFile ) return true;
// while not end-of-file
for( int16_t i = 0; i < LGS_MAX_SHADOW_ANGLES; i++ ){ for( int16_t j = 0; j < m_pShadowLine[i].nCount; j++ ) {#ifdef ALS_300
if ( m_pShadowLine[i].lpShadow[j].bType < 5 )#endif
fprintf( pFile, "%d %d %d %d\n", i, m_pShadowLine[i].lpShadow[j].bType, m_pShadowLine[i].lpShadow[j].nMin, m_pShadowLine[i].lpShadow[j].nMax ); }; };
fclose( pFile ); return true;};
bool CShadow::SavePolygons( PCHAR pFileName ) {
FILE *pFile = fopen( pFileName, "wt" );
if( !pFile ) return true;
// save shadow from line
fprintf( pFile, "type %lu\n", m_dwTypeOn );
// while not end-of-file
for( int16_t i = 0; i < m_nPolygon; i++ ) { // save shadow from line
fprintf( pFile, "polygon %d\n", m_lpPolygon[i].bType );
for( int16_t j = 0; j < m_lpPolygon[i].wCount; j++ ) { // load shadow from line
fprintf( pFile, "%d %d\n", m_lpPolygon[i].lpPoint[j].wX, m_lpPolygon[i].lpPoint[j].wY ); }; };
fclose( pFile ); return true;};
bool CShadow::Load(){
Destroy();
if( !LoadRays( _T("hidden.txt") ) ) return false;#ifdef ALS_300
if( m_bIsSlave ){ LoadRays( _T("hiddens.txt") ); } else { LoadRays( _T("hiddenm.txt") ); };#endif
if( !LoadPolygons( _T("polygons.txt") ) ) return false;
return true;};
bool CShadow::Save() {
if( !SaveRays( _T("hidden.txt") ) ) return false;
if( !SavePolygons( _T("polygons.txt") ) ) return false;
return true;};
bool CShadow::IsOutside( int16_t nAngle, int16_t nRange ){
#ifdef COMPILE_AS_SICK_PROXY
return true;#endif
int32_t nShadow = nAngle;
// convert angle to shadow system
#ifdef LGS_MIRROR
nShadow <<= 1; #ifdef LGS_MND
nShadow = ( nShadow * LGS_MAX_SHADOW_ANGLES ) / LGS_REAL_DETECTOR_ANGLES; #else
nShadow = ( nShadow * LGS_MAX_SHADOW_ANGLES ) / (LGS_MAX_DETECTOR_ANGLES * LGS_MIRROR ); #endif
#else
nShadow = ( nShadow * LGS_MAX_SHADOW_ANGLES ) / LGS_REAL_DETECTOR_ANGLES;#endif
// range should be in the same system ( 0..2047 )
LPRAY_LINE pShadowLine = &m_pShadowLine[ (int16_t)nShadow ]; if( !pShadowLine->nCount ) return true;
LPRAY pShadow = pShadowLine->lpShadow; for( int16_t i = 0; i < pShadowLine->nCount; i++ ) {#ifdef SHADOW_ALWAYS_ON
if( nRange >= pShadow->nMin && nRange <= pShadow->nMax ) return false;#else
if( pShadow->bType < 6 ){ if( m_pTypeOn[pShadow->bType] && nRange >= pShadow->nMin && nRange <= pShadow->nMax ) return false; } else { if( nRange >= pShadow->nMin && nRange <= pShadow->nMax ) return false; };#endif
pShadow++; };
return true;};
void CShadow::OnCmdGetHzData( PBYTE& pReadBufferPtr, int16_t& nReadCount, PBYTE& pSendBufferPtr, int16_t& nSendCount ) { union { LGS::PHZ_PACKET pPacket; LGS::PHZ_CMD_GET_HEADER pHzCmdHeader; LGS::PHZ_CMD_GET_POLYGON pHzCmdPolygon; LGS::PHZ_CMD_GET_POLYGON_POINT pHzCmdPolygonPoint; LGS::PHZ_CMD_GET_RAY pHzCmdRay; LGS::PHZ_CMD_GET_RAY_LINE pHzCmdRayLine; };
union { LGS::HZ_PACKET HzPacket; LGS::HZ_EVT_GET_HEADER HzEvtHeader; LGS::HZ_EVT_GET_POLYGON HzEvtPolygon; LGS::HZ_EVT_GET_POLYGON_POINT HzEvtPolygonPoint; LGS::HZ_EVT_GET_RAY HzEvtRay; LGS::HZ_EVT_GET_RAY_LINE HzEvtRayLine; LGS::HZ_EVT_ERROR HzEvtError; };
// read packet
nReadCount -= LGS::GetCmdHzGetData( pReadBufferPtr, &pPacket );
// set sequence
HzPacket.bSequence = pPacket->bSequence;
uint8_t bError = LGS::HZER_NONE; switch( pPacket->bType ) { case LGS::HZIT_CMD_GET_HEADER: HzEvtHeader.bType = LGS::HZIT_EVT_GET_HEADER; HzEvtHeader.bSubSize = sizeof(LGS::HZ_EVT_GET_HEADER) - sizeof(LGS::HZ_PACKET); HzEvtHeader.wPolygonCount = m_nPolygon; HzEvtHeader.wRayCount = LGS_MAX_SHADOW_ANGLES; HzEvtHeader.dwTypeOn = m_dwTypeOn; break;
case LGS::HZIT_CMD_GET_POLYGON: if( pHzCmdPolygon->wPolygonIndex >= m_nPolygon ) { bError = LGS::HZER_POLYGON_INDEX; break; }
HzEvtPolygon.bType = LGS::HZIT_EVT_GET_POLYGON; HzEvtPolygon.bSubSize = sizeof(LGS::HZ_EVT_GET_POLYGON) - sizeof(LGS::HZ_PACKET); HzEvtPolygon.bPolygonType = m_lpPolygon[pHzCmdPolygon->wPolygonIndex].bType; HzEvtPolygon.wPointCount = m_lpPolygon[pHzCmdPolygon->wPolygonIndex].wCount; break;
case LGS::HZIT_CMD_GET_POLYGON_POINT: if( pHzCmdPolygonPoint->wPolygonIndex >= m_nPolygon ) { bError = LGS::HZER_POLYGON_INDEX; break; }
if( pHzCmdPolygonPoint->wPointIndex >= m_lpPolygon[pHzCmdPolygonPoint->wPolygonIndex].wCount ) { bError = LGS::HZER_POLYGON_POINT_INDEX; break; }
HzEvtPolygonPoint.bType = LGS::HZIT_EVT_GET_POLYGON_POINT; HzEvtPolygonPoint.bSubSize = sizeof(LGS::HZ_EVT_GET_POLYGON_POINT) - sizeof(LGS::HZ_PACKET); HzEvtPolygonPoint.Point = m_lpPolygon[pHzCmdPolygonPoint->wPolygonIndex].lpPoint[pHzCmdPolygonPoint->wPointIndex]; break;
case LGS::HZIT_CMD_GET_RAY: if( pHzCmdRay->wRayIndex >= LGS_MAX_SHADOW_ANGLES ) { bError = LGS::HZER_RAY_INDEX; break; }
HzEvtRay.bType = LGS::HZIT_EVT_GET_RAY; HzEvtRay.bSubSize = sizeof(LGS::HZ_EVT_GET_RAY) - sizeof(LGS::HZ_PACKET); HzEvtRay.wLineCount = m_pShadowLine[pHzCmdRay->wRayIndex].nCount; break;
case LGS::HZIT_CMD_GET_RAY_LINE: if( pHzCmdRayLine->wRayIndex >= LGS_MAX_SHADOW_ANGLES ) { bError = LGS::HZER_RAY_INDEX; break; } if( pHzCmdRayLine->wLineIndex >= m_pShadowLine[pHzCmdRayLine->wRayIndex].nCount ) { bError = LGS::HZER_RAY_LINE_INDEX; break; }
HzEvtRayLine.bType = LGS::HZIT_EVT_GET_RAY_LINE; HzEvtRayLine.bSubSize = sizeof(LGS::HZ_EVT_GET_RAY_LINE) - sizeof(LGS::HZ_PACKET); HzEvtRayLine.Line.bType = m_pShadowLine[pHzCmdRayLine->wRayIndex].lpShadow[pHzCmdRayLine->wLineIndex].bType; HzEvtRayLine.Line.wMin = m_pShadowLine[pHzCmdRayLine->wRayIndex].lpShadow[pHzCmdRayLine->wLineIndex].nMin; HzEvtRayLine.Line.wMax = m_pShadowLine[pHzCmdRayLine->wRayIndex].lpShadow[pHzCmdRayLine->wLineIndex].nMax; break; }
// fill error
if( bError != LGS::HZER_NONE ) { HzEvtError.bType = LGS::HZIT_EVT_ERROR; HzEvtError.bSubSize = sizeof(LGS::HZ_EVT_ERROR) - sizeof(LGS::HZ_PACKET); HzEvtError.bCmdType = pPacket->bType; HzEvtError.bErrorId = bError; }
// build CMD_SET_WIDHT_TABLE command
*pSendBufferPtr++ = LGS::CMD_GET_HZ_DATA;
// write command
nSendCount += LGS::BuildEvtHzGetData( pSendBufferPtr, &HzPacket );}
bool CShadow::OnCmdSetHzData( PBYTE& pReadBufferPtr, int16_t& nReadCount, PBYTE& pSendBufferPtr, int16_t& nSendCount ){ union { LGS::PHZ_PACKET pPacket; LGS::PHZ_CMD_SET_HEADER pHzCmdHeader; LGS::PHZ_CMD_SET_POLYGON pHzCmdPolygon; LGS::PHZ_CMD_SET_POLYGON_POINT pHzCmdPolygonPoint; LGS::PHZ_CMD_SET_RAY pHzCmdRay; LGS::PHZ_CMD_SET_RAY_LINE pHzCmdRayLine; LGS::PHZ_CMD_SET_DONE pHzCmdDone; };
union { LGS::HZ_PACKET HzPacket; LGS::HZ_EVT_SET_HEADER HzEvtHeader; LGS::HZ_EVT_SET_POLYGON HzEvtPolygon; LGS::HZ_EVT_SET_POLYGON_POINT HzEvtPolygonPoint; LGS::HZ_EVT_SET_RAY HzEvtRay; LGS::HZ_EVT_SET_RAY_LINE HzEvtRayLine; LGS::HZ_EVT_SET_DONE HzEvtDone; LGS::HZ_EVT_ERROR HzEvtError; };
//
bool bPendingSave = false;
// read packet
nReadCount -= LGS::GetCmdHzSetData( pReadBufferPtr, &pPacket );
// set sequence
HzPacket.bSequence = pPacket->bSequence;
uint8_t bError = LGS::HZER_NONE; switch( pPacket->bType ) { case LGS::HZIT_CMD_SET_HEADER: // destroy old content
Destroy(); // check shadow rays count
if( pHzCmdHeader->wRayCount != LGS_MAX_SHADOW_ANGLES ) { bError = LGS::HZER_RAY_COUNT; break; } // create empty polygons
m_lpPolygon = (LPHZ_POLYGON) malloc( pHzCmdHeader->wPolygonCount * sizeof(HZ_POLYGON) ); if( pHzCmdHeader->wPolygonCount && !m_lpPolygon ) { bError = LGS::HZER_OUT_OF_MEMORY; break; } memset( m_lpPolygon, 0, pHzCmdHeader->wPolygonCount * sizeof(HZ_POLYGON) ); m_nPolygon = pHzCmdHeader->wPolygonCount; m_dwTypeOn = pHzCmdHeader->dwTypeOn; { for( int16_t i = 0; i < LGS_MAX_SHADOW_TYPES; i++ ) { DWORD bBit = 1 << i; m_pTypeOn[i] = ( m_dwTypeOn & bBit ) == bBit; } } // prepare reply
HzEvtHeader.bType = LGS::HZIT_EVT_SET_HEADER; HzEvtHeader.bSubSize = sizeof(LGS::HZ_EVT_SET_HEADER) - sizeof(LGS::HZ_PACKET); break;
case LGS::HZIT_CMD_SET_POLYGON: // check index
if( pHzCmdPolygon->wPolygonIndex >= m_nPolygon ) { bError = LGS::HZER_POLYGON_INDEX; break; } // reallocate memory
m_lpPolygon[pHzCmdPolygon->wPolygonIndex].wCount = 0; m_lpPolygon[pHzCmdPolygon->wPolygonIndex].lpPoint = (LGS::LPHZ_POINT) realloc( m_lpPolygon[pHzCmdPolygon->wPolygonIndex].lpPoint, sizeof(LGS::HZ_POINT) * pHzCmdPolygon->wPointCount ); if( pHzCmdPolygon->wPointCount && !m_lpPolygon[pHzCmdPolygon->wPolygonIndex].lpPoint ) { bError = LGS::HZER_OUT_OF_MEMORY; break; } m_lpPolygon[pHzCmdPolygon->wPolygonIndex].wCount = pHzCmdPolygon->wPointCount; m_lpPolygon[pHzCmdPolygon->wPolygonIndex].bType = pHzCmdPolygon->bPolygonType;
// prepare reply
HzEvtPolygon.bType = LGS::HZIT_EVT_SET_POLYGON; HzEvtPolygon.bSubSize = sizeof(LGS::HZ_EVT_SET_POLYGON) - sizeof(LGS::HZ_PACKET); break;
case LGS::HZIT_CMD_SET_POLYGON_POINT: // check indexes
if( pHzCmdPolygonPoint->wPolygonIndex >= m_nPolygon ) { bError = LGS::HZER_POLYGON_INDEX; break; } if( pHzCmdPolygonPoint->wPointIndex >= m_lpPolygon[pHzCmdPolygonPoint->wPolygonIndex].wCount ) { bError = LGS::HZER_POLYGON_POINT_INDEX; break; } m_lpPolygon[pHzCmdPolygonPoint->wPolygonIndex].lpPoint[pHzCmdPolygonPoint->wPointIndex] = pHzCmdPolygonPoint->Point;
// prepare reply
HzEvtPolygonPoint.bType = LGS::HZIT_EVT_SET_POLYGON_POINT; HzEvtPolygonPoint.bSubSize = sizeof(LGS::HZ_EVT_SET_POLYGON_POINT) - sizeof(LGS::HZ_PACKET); break;
case LGS::HZIT_CMD_SET_RAY: // check indexes
if( pHzCmdRay->wRayIndex >= LGS_MAX_SHADOW_ANGLES ) { bError = LGS::HZER_RAY_INDEX; break; } m_pShadowLine[pHzCmdRay->wRayIndex].nCount = 0; m_pShadowLine[pHzCmdRay->wRayIndex].lpShadow = (LPRAY) realloc( m_pShadowLine[pHzCmdRay->wRayIndex].lpShadow, pHzCmdRay->wLineCount * sizeof(RAY) ); if( pHzCmdRay->wLineCount && !m_pShadowLine[pHzCmdRay->wRayIndex].lpShadow ) { bError = LGS::HZER_OUT_OF_MEMORY; break; } m_pShadowLine[pHzCmdRay->wRayIndex].nCount = pHzCmdRay->wLineCount;
// prepare reply
HzEvtRay.bType = LGS::HZIT_EVT_SET_RAY; HzEvtRay.bSubSize = sizeof(LGS::HZ_EVT_SET_RAY) - sizeof(LGS::HZ_PACKET); break;
case LGS::HZIT_CMD_SET_RAY_LINE: if( pHzCmdRayLine->wRayIndex >= LGS_MAX_SHADOW_ANGLES ) { bError = LGS::HZER_RAY_INDEX; break; } if( pHzCmdRayLine->wLineIndex >= m_pShadowLine[pHzCmdRayLine->wRayIndex].nCount ) { bError = LGS::HZER_RAY_LINE_INDEX; break; } m_pShadowLine[pHzCmdRayLine->wRayIndex].lpShadow[pHzCmdRayLine->wLineIndex].bType = pHzCmdRayLine->Line.bType; m_pShadowLine[pHzCmdRayLine->wRayIndex].lpShadow[pHzCmdRayLine->wLineIndex].nMin = pHzCmdRayLine->Line.wMin; m_pShadowLine[pHzCmdRayLine->wRayIndex].lpShadow[pHzCmdRayLine->wLineIndex].nMax = pHzCmdRayLine->Line.wMax;
HzEvtRayLine.bType = LGS::HZIT_EVT_SET_RAY_LINE; HzEvtRayLine.bSubSize = sizeof(LGS::HZ_EVT_SET_RAY_LINE) - sizeof(LGS::HZ_PACKET); break;
case LGS::HZIT_CMD_SET_DONE: HzEvtDone.bType = LGS::HZIT_EVT_SET_DONE; HzEvtDone.bSubSize = sizeof(LGS::HZ_EVT_SET_DONE) - sizeof(LGS::HZ_PACKET);
bPendingSave = true; break; }
// fill error
if( bError != LGS::HZER_NONE ) { HzEvtError.bType = LGS::HZIT_EVT_ERROR; HzEvtError.bSubSize = sizeof(LGS::HZ_EVT_ERROR) - sizeof(LGS::HZ_PACKET); HzEvtError.bCmdType = pPacket->bType; HzEvtError.bErrorId = bError; }
// build CMD_SET_WIDHT_TABLE command
*pSendBufferPtr++ = LGS::CMD_SET_HZ_DATA;
// write command
nSendCount += LGS::BuildEvtHzSetData( pSendBufferPtr, &HzPacket );
return bPendingSave;}
void CShadow::OnCmdGetHzEnable( PBYTE& pReadBufferPtr, int16_t& nReadCount, PBYTE& pSendBufferPtr, int16_t& nSendCount ){ // build CMD_SET_WIDHT_TABLE command
*pSendBufferPtr++ = LGS::CMD_GET_HZ_ENABLE;
// write command
nSendCount += LGS::BuildEvtGetHzEnable( pSendBufferPtr, m_dwTypeOn );
}
bool CShadow::OnCmdSetHzEnable( PBYTE& pReadBufferPtr, int16_t& nReadCount, PBYTE& pSendBufferPtr, int16_t& nSendCount ){ // read packet
nReadCount -= LGS::GetCmdSetHzEnable( pReadBufferPtr, &m_dwTypeOn ); for( int16_t i = 0; i < LGS_MAX_SHADOW_TYPES; i++ ){ DWORD bBit = 1 << i; m_pTypeOn[i] = ( m_dwTypeOn & bBit ) == bBit; }
// build CMD_SET_WIDHT_TABLE command
*pSendBufferPtr++ = LGS::CMD_SET_HZ_ENABLE;
// write command
nSendCount += LGS::BuildEvtSetHzEnable( pSendBufferPtr, m_dwTypeOn ); return true;}
|