You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

585 lines
24 KiB

#include "rgooseNodeSensor.h"
#include <opencv2/opencv.hpp>
#define LOG( X ) m_pLog->Log( "NODSEN", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "NODSEN", this, _LOG_ERROR, X )
argusNodeSensor::argusNodeSensor( uint32_t id ):argusNetNode( id ),m_oPosition(){
m_pTargetFilter = NULL;
m_pLog = basicLogCollection::getInstance();
memset( &m_sLRF_DATA, 0, sizeof(DBC_LRF_DATA));
if( m_pDBC ){
DBC_LRF_DATA *tDATA = m_pDBC->getLRFData(m_sNodeData.aux_id);
if( tDATA != NULL ) {
memcpy( &m_sLRF_DATA, tDATA, sizeof(DBC_LRF_DATA));
free( tDATA );
};
};
};
argusNodeSensor::argusNodeSensor( DBC_NODE_DATA *Data ):argusNetNode( Data ),m_oPosition(){
m_pTargetFilter = NULL;
m_pLog = basicLogCollection::getInstance();
memset( &m_sLRF_DATA, 0, sizeof(DBC_LRF_DATA));
if( m_pDBC ){
DBC_LRF_DATA *tDATA = m_pDBC->getLRFData(m_sNodeData.aux_id);
if( tDATA != NULL ) {
memcpy( &m_sLRF_DATA, tDATA, sizeof(DBC_LRF_DATA));
free( tDATA );
};
};
};
argusNodeSensor::~argusNodeSensor(){
};
#define SKIP_LINE_PIXELS 28
#define HEIGHT 512
#define WIDTH 1024
// scale factor -> 4/9
uint8_t *argusNodeSensor::create2dDraw( uint32_t *out_size, uint32_t *th_data, size_t th_size, uint16_t max_digital, int radial_shift, CSTR tpl, bool byMinimum ){
uint8_t *RetVal = NULL;
*out_size = 0;
LOG("C2DD");
if( th_data != NULL && th_size != 0 ) {
if ( fileExist( tpl ) ) {
LOG(tpl);
cv::Mat tImage = cv::imread( tpl, cv::IMREAD_UNCHANGED );
int psize = th_size / ( sizeof( uint32_t ));
float stp = 2.0f * ( M_PI / (float) psize );
//float MR = m_sLRF_DATA.max_range;
float S = ( M_PI / 3000.0f) * (float) radial_shift;
int x0 = (tImage.cols >> 1);
int y0 = (tImage.rows >> 1);
float maxR = ((float) x0 ) * 0.98f;
LOG("C2DD - Start");
cv::Point pts[(psize << 1)];
cv::Point ptt[(psize+2)];
const cv::Point * ppt[1];
int npp[1];
const cv::Point * ppz[1];
int npz[1];
float rst;
int rpt;
uint16_t RANGE = 0;
ptt[0].x = x0;
ptt[0].y = y0;
rst = maxR / ((float)(max_digital+1));
rpt = 1;
for( int i=0; i < psize; i++ ){
if( byMinimum ) {
RANGE = ( th_data[i] >> 16 ) & 0xFFFF;
} else {
RANGE = th_data[i] & 0xFFFF;
};
float ca = 0.0 - ( S + i * stp );
if( RANGE > 0 ){
pts[psize+i].x = x0 + maxR * cosf( ca );
pts[psize+i].y = y0 + maxR * sinf( ca );
if( RANGE > max_digital ){
pts[i].x = pts[psize+i].x;
pts[i].y = pts[psize+i].y;
} else {
float cr = (rst * RANGE);
pts[i].x = x0 + cr * cosf( ca );
pts[i].y = y0 + cr * sinf( ca );
};
} else {
ptt[rpt].x = pts[psize+i].x = pts[i].x = x0 + maxR * cosf( ca );
ptt[rpt].y = pts[psize+i].y = pts[i].y = y0 + maxR * sinf( ca );
rpt++;
};
};
ppt[0] = pts;
npp[0] = ( psize<<1 );
ppz[0] = ptt;
npz[0] = ( rpt - 1);
LOG("C2DD -> draw finish");
cv::Mat ttMOMO = cv::imread( tpl, cv::IMREAD_UNCHANGED );
LOG("C2DD -> read tpl2 finish");
cv::fillPoly( ttMOMO, ppt, npp, 1, cv::Scalar( 0, 128, 255, 50 ), cv::LINE_8 );
LOG("C2DD -> poly1 finish");
//try {
// cv::fillPoly(ttMOMO, ppz, npz, 1, cv::Scalar(0, 0, 0, 100), cv::LINE_8);
// LOG("C2DD -> poly2 finish");
//} catch ( cv::Exception& e ) {
// LOG("C2DD -> poly2 Error!");
// LOG( e.what() );
//};
cv::addWeighted( tImage, 0.5, ttMOMO, 0.5, 0.0, tImage );
LOG("C2DD -> add img finish");
ttMOMO.release();
LOG("C2DD -> img release finish");
vector<int> compression_params;
compression_params.push_back( cv::IMWRITE_PNG_COMPRESSION );
compression_params.push_back(9);
vector<uchar> buf;
cv::imencode( ".png", tImage, buf, compression_params );
tImage.release();
LOG("C2DD -> compress finish");
*out_size = buf.size();
RetVal = (uint8_t *) malloc( *out_size );
if( RetVal ) {
memcpy( RetVal, buf.data(), *out_size );
} else *out_size =0;
buf.clear();
};
};
return RetVal;
};
bool argusNodeSensor::handleDeviceDraw( uint8_t kID, void *th_data, size_t th_size, bool byMinimum ){
if( th_data == NULL ) return false;
if( th_size == 0 ) return false;
if( m_pDBC == NULL ) return false;
m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_RAW_DATA, kID, th_data, th_size );
/*
//Clear Old Draw
CSTR old = m_pDBC->getLRFDraw( getNodeID() );
if ( old ) {
// delete old draw
char filename[256];
sprintf( filename, "data/device_draws/%s_100.png", old );
if( fileExist( filename ) ){
unlink( filename );
};
sprintf( filename, "data/device_draws/%s_50.png", old );
if( fileExist( filename ) ){
unlink( filename );
};
sprintf( filename, "data/device_draws/%s_25.png", old );
if( fileExist( filename ) ){
unlink( filename );
};
sprintf( filename, "data/device_draws/%s_12.png", old );
if( fileExist( filename ) ){
unlink( filename );
};
sprintf( filename, "data/device_draws/%s_map.png", old );
if( fileExist( filename ) ){
unlink( filename );
};
free( old );
};
*/
CSTR uid = getUniqID_MD5();
if( uid == NULL ) return false;
switch( getNodeType() ){
case ARGUS_ALS_SENSOR:
case ARGUS_LFS_SENSOR:
case ARGUS_PLS_200_SENSOR:
case ARGUS_PLS_360_SENSOR:
case ARGUS_NET_PLS_200:{
uint32_t tSize;
uint8_t *tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 1024, m_sLRF_DATA.base_shift, "data/assets/images/lfs_draw_empty_100.png" );
if ( tBuff ) {
m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_100, kID, tBuff, tSize );
free( tBuff );
};
tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 512, m_sLRF_DATA.base_shift, "data/assets/images/lfs_draw_empty_50.png" );
if ( tBuff ) {
m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_50, kID, tBuff, tSize );
free( tBuff );
};
tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 256, m_sLRF_DATA.base_shift, "data/assets/images/lfs_draw_empty_25.png" );
if ( tBuff ) {
m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_25, kID, tBuff, tSize );
free( tBuff );
};
tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 128, m_sLRF_DATA.base_shift, "data/assets/images/lfs_draw_empty_12.png" );
if ( tBuff ) {
m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_12, kID, tBuff, tSize );
free( tBuff );
};
tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 1024, m_sLRF_DATA.base_shift, "data/assets/images/2d_MAP.png" );
if ( tBuff ) {
m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_MAP, kID, tBuff, tSize );
free( tBuff );
};
break;
};
case ARGUS_3D_SENSOR:{
char * emptyBG = "data/assets/images/empty_3d.png";
thresholdNode * ptr = (thresholdNode *) th_data;
if ( fileExist( emptyBG ) ) {
cv::Mat tImage = cv::imread( emptyBG, cv::IMREAD_UNCHANGED );
if( tImage.rows != 512 || tImage.cols != 1024 ){
LOG_ERROR( "Background Image wrong size (should be 1024x512)!" );
free( uid );
return false;
};
uint8_t * tMemory = tImage.data;
thresholdNode * cNode = NULL;
uint8_t * cPixel = NULL;
int pix = 0;
int mem = 0;
bool s_bit = false;
bool no_data = false;
float tVal;
bool master = ( kID < 16 );
for( int idx = 0; idx < ( TH_SIZE_NODES << 2 ); idx++ ) { // tik-tak
if( ( idx % 4 ) == 0 ){
mem++;
if ( master ) {
cNode = ptr + TH_SIZE_NODES - mem - 1;
} else {
cNode = ptr + mem - 1;
};
if( !s_bit ) s_bit = cNode->values.sbit;
if( !no_data ) no_data = ( cNode->values.cnt == 0 );
if( !s_bit && !no_data ){
if( byMinimum ) {
tVal = ((float)cNode->values.min) / 4096.f;
} else {
tVal = ((float)cNode->values.max) / 4096.f;
};
};
};
if( ( idx % 9 ) == 0 ){
if( mem > ( SKIP_LINE_PIXELS * TH_LINE_NODES ) ) {
cPixel = tMemory + (pix << 2);
pix++;
if( s_bit ) { // Set Red
cPixel[0] = 0x00; cPixel[1] = 0x00; cPixel[2] = 0xff; cPixel[3] = 0xff;
} else {
if( no_data ) { // Set Blue
cPixel[0] = 0xff; cPixel[1] = 0x00; cPixel[2] = 0x00; cPixel[3] = 0xff;
} else {
uint32_t tColor;
tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
cPixel[0] = ( tColor >> 24 ) & 0xFF;
cPixel[1] = ( tColor >> 16 ) & 0xFF;
cPixel[2] = ( tColor >> 8 ) & 0xFF;
cPixel[3] = tColor & 0xFF;
};
};
s_bit = false;
no_data = false;
};
};
};
char filename[256];
sprintf( filename, "data/device_draws/%s_100.png", uid );
//int RetVal = stbi_write_png( filename, 1024, 512, cSpace, tMemory, 1024 * cSpace );
vector<int> compression_params;
compression_params.push_back( cv::IMWRITE_PNG_COMPRESSION );
compression_params.push_back(9);
if(! cv::imwrite( filename, tImage, compression_params ) ){
tImage.release();
free( uid );
return false;
};
tImage.release();
#ifdef DEBUG_EXTRA
LOG("Draw Saved!");
#endif
cv::Mat tMap( Q_SCAN_COUNT, TH_LINE_NODES, CV_8UC4 );
#ifdef DEBUG_EXTRA
LOG("Map Creating");
#endif
tMemory = tMap.data;
for( int j = 0; j < Q_SCAN_COUNT; j++ ){
for( int i = 0; i < TH_LINE_NODES; i++ ){
//cNode = ptr+(j*i);
//cNode = &ptr[ ( TH_LINE_NODES * j ) + i ];
int ids = 0;
int idd = 0;
if ( master ) {
//cNode = ptr+makeIds( i, j , true );
//cPixel = tMemory+( cSpace * makeIds( i, j , false ));
ids = makeIds( i, j , true );
idd = 4 * makeIds( i, j , false );
//cNode = ptr + ( i * j );
//cPixel = tMemory + ( cSpace * i * j );
} else {
//cNode = ptr+makeIds( i, j , false );
//cPixel = tMemory+( cSpace * makeIds( i, j , false ));
ids = makeIds( i, j , false );
idd = 4 * makeIds( i, j , false );
};
cNode = ptr + ids ;
cPixel = tMemory + idd;
if( 0 ) { // Monochrome
#ifdef CHECK_SUN_BIT
if( cNode->values.sbit ) {
cPixel[0] = 0;
} else {
cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
};
#else
cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
#endif
} else {
#ifdef CHECK_SUN_BIT
if( cNode->values.sbit ) {
// Set Red
cPixel[0] = 0x00;
cPixel[1] = 0x00;
cPixel[2] = 0xff;
cPixel[3] = 0xff;
} else {
if( cNode->values.cnt == 0 ) {
// Set Blue
cPixel[0] = 0xff;
cPixel[1] = 0x00;
cPixel[2] = 0x00;
cPixel[3] = 0xff;
} else {
uint32_t tColor;
float tVal = ((float) cNode->values.min ) / 4096.f;
tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
cPixel[0] = ( tColor >> 24 ) & 0xFF;
cPixel[1] = ( tColor >> 16 ) & 0xFF;
cPixel[2] = ( tColor >> 8 ) & 0xFF;
cPixel[3] = tColor & 0xFF;
};
};
#else
if( cNode->values.cnt == 0 ) {
// Set Blue
cPixel[0] = 0xff;
cPixel[1] = 0x00;
cPixel[2] = 0x00;
cPixel[3] = 0xff;
} else {
uint32_t tColor;
float tVal = ((float) cNode->values.min ) / 4096.f;
tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
cPixel[0] = ( tColor >> 24 ) & 0xFF;
cPixel[1] = ( tColor >> 16 ) & 0xFF;
cPixel[2] = ( tColor >> 8 ) & 0xFF;
cPixel[3] = tColor & 0xFF;
};
#endif
};
};
};
sprintf( filename, "data/device_draws/%s_map.png", uid );
LOG(filename);
if(! cv::imwrite( filename, tMap, compression_params ) ){
tMap.release();
free( uid );
return false;
};
tMap.release();
m_pDBC->addLRFDraw( getNodeID(), TH_SIZE_NODES, uid, kID, 1, true );
} else {
LOG_ERROR( "Background Image is Not Found!" );
free( uid );
return false;
};
break;
};
};
free( uid );
return true;
};
/*
#define STB_IMAGE_IMPLEMENTATION
#include "../data/stbi/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../data/stbi/stb_image_write.h"
#undef STB_IMAGE_WRITE_IMPLEMENTATION
#undef STB_IMAGE_IMPLEMENTATION
bool argus3DLIDAR::saveTHPicture( thresholdNode *ptr, CSTR uid, bool master, bool byMinimum, bool monochrome ){
uint8_t *tMemory = NULL;
thresholdNode *cNode = NULL;
uint8_t *cPixel = NULL;
int cSpace;
int x, y;
tMemory = stbi_load( "data/assets/images/empty_3d.png", &x, &y, &cSpace, STBI_default );
if( tMemory == NULL ){
LOG_ERROR("Draw Memory Alloc Eror!");
return false;
};
if( x != 1024 || y != 512 ){
LOG("Wrong IMG Size!");
free( tMemory );
return NULL;
};
int pix = 0;
int mem = 0;
bool s_bit = false;
bool no_data = false;
float tVal;
for ( int idx = 0; idx < ( TH_SIZE_NODES << 2 ); idx++ ) { // tik-tak
if( ( idx % 4 ) == 0 ){
mem++;
if ( master ) {
cNode = ptr + TH_SIZE_NODES - mem - 1;
} else {
cNode = ptr + mem - 1;
};
if( !s_bit ) s_bit = cNode->values.sbit;
if( !no_data ) no_data = ( cNode->values.cnt == 0 );
if( !s_bit && !no_data ){
if( byMinimum ) {
tVal = ((float)cNode->values.min) / 4096.f;
} else {
tVal = ((float)cNode->values.max) / 4096.f;
};
};
};
if( ( idx % 9 ) == 0 ){
if( mem > ( SKIP_LINE_PIXELS * TH_LINE_NODES ) ) {
cPixel = tMemory + (pix << 2);
pix++;
if( s_bit ) { // Set Red
cPixel[0] = 0x00; cPixel[1] = 0xff; cPixel[2] = 0x00; cPixel[3] = 0xff;
} else {
if( no_data ) { // Set Blue
cPixel[0] = 0x00; cPixel[1] = 0x00; cPixel[2] = 0xff; cPixel[3] = 0xff;
} else {
uint32_t tColor;
tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
cPixel[0] = ( tColor >> 24 ) & 0xFF;
cPixel[1] = ( tColor >> 16 ) & 0xFF;
cPixel[2] = ( tColor >> 8 ) & 0xFF;
cPixel[3] = tColor & 0xFF;
};
};
s_bit = false;
no_data = false;
}
};
};
char filename[256];
sprintf( filename, "data/device_draws/%s_100.png", uid );
int RetVal = stbi_write_png( filename, 1024, 512, cSpace, tMemory, 1024 * cSpace );
free( tMemory );
if( RetVal == 0 ) return false;
if( monochrome ) {
cSpace = 1;
} else {
cSpace = 4;
};
tMemory = (uint8_t *) malloc( TH_SIZE_NODES * cSpace );
if( tMemory == NULL ){
LOG_ERROR("Map Memory Alloc Eror!");
return false;
};
for (int j = 0; j < Q_SCAN_COUNT; j++) {
for ( int i = 0; i < TH_LINE_NODES; i++ ) {
//cNode = ptr+(j*i);
//cNode = &ptr[ ( TH_LINE_NODES * j ) + i ];
int ids = 0;
int idd = 0;
if ( master ) {
//cNode = ptr+makeIds( i, j , true );
//cPixel = tMemory+( cSpace * makeIds( i, j , false ));
ids = makeIds( i, j , true );
idd = cSpace * makeIds( i, j , false );
//cNode = ptr + ( i * j );
//cPixel = tMemory + ( cSpace * i * j );
} else {
//cNode = ptr+makeIds( i, j , false );
//cPixel = tMemory+( cSpace * makeIds( i, j , false ));
ids = makeIds( i, j , false );
idd = cSpace * makeIds( i, j , false );
};
cNode = ptr + ids ;
cPixel = tMemory + idd;
if( monochrome ) {
#ifdef CHECK_SUN_BIT
if( cNode->values.sbit ) {
cPixel[0] = 0;
} else {
cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
};
#else
cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
#endif
} else {
#ifdef CHECK_SUN_BIT
if( cNode->values.sbit ) {
// Set Red
cPixel[0] = 0x00;
cPixel[1] = 0xff;
cPixel[2] = 0x00;
cPixel[3] = 0xff;
} else {
if( cNode->values.cnt == 0 ) {
// Set Blue
cPixel[0] = 0x00;
cPixel[1] = 0x00;
cPixel[2] = 0xff;
cPixel[3] = 0xff;
} else {
uint32_t tColor;
float tVal = ((float) cNode->values.min ) / 4096.f;
tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
cPixel[0] = ( tColor >> 24 ) & 0xFF;
cPixel[1] = ( tColor >> 16 ) & 0xFF;
cPixel[2] = ( tColor >> 8 ) & 0xFF;
cPixel[3] = tColor & 0xFF;
};
};
#else
if( cNode->values.cnt == 0 ) {
// Set Blue
cPixel[0] = 0x00;
cPixel[1] = 0x00;
cPixel[2] = 0xff;
cPixel[3] = 0xff;
} else {
uint32_t tColor;
float tVal = ((float) cNode->values.min ) / 4096.f;
tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
cPixel[0] = ( tColor >> 24 ) & 0xFF;
cPixel[1] = ( tColor >> 16 ) & 0xFF;
cPixel[2] = ( tColor >> 8 ) & 0xFF;
cPixel[3] = tColor & 0xFF;
};
#endif
};
};
};
sprintf( filename, "data/device_draws/%s_map.png", uid );
RetVal = stbi_write_png( filename, TH_LINE_NODES, Q_SCAN_COUNT, cSpace, tMemory, TH_LINE_NODES * cSpace );
free( tMemory );
if( RetVal == 0 ) return false;
return true;
};
*/
#undef LOG
#undef LOG_ERROR