Professional Documents
Culture Documents
Estructura de Frames
Estructura de Frames
#include "DataStructures/Frame.h"
#include "DataStructures/FrameMemory.h"
#include "DepthEstimation/DepthMapPixelHypothesis.h"
#include "Tracking/TrackingReference.h"
namespace lsd_slam
{
int privateFrameAllocCount = 0;
data.image[0] =
FrameMemory::getInstance().getFloatBuffer(data.width[0]*data.height[0]);
float* maxPt = data.image[0] + data.width[0]*data.height[0];
data.imageValid[0] = true;
privateFrameAllocCount++;
data.image[0] =
FrameMemory::getInstance().getFloatBuffer(data.width[0]*data.height[0]);
memcpy(data.image[0], image, data.width[0]*data.height[0] * sizeof(float));
data.imageValid[0] = true;
privateFrameAllocCount++;
Frame::~Frame()
{
FrameMemory::getInstance().deactivateFrame(this);
if(!pose->isRegisteredToGraph)
delete pose;
else
pose->frame = 0;
FrameMemory::getInstance().returnBuffer(reinterpret_cast<float*>(data.gradients[lev
el]));
FrameMemory::getInstance().returnBuffer(data.maxGradients[level]);
FrameMemory::getInstance().returnBuffer(data.idepth[level]);
FrameMemory::getInstance().returnBuffer(data.idepthVar[level]);
}
FrameMemory::getInstance().returnBuffer((float*)data.validity_reAct);
FrameMemory::getInstance().returnBuffer(data.idepth_reAct);
FrameMemory::getInstance().returnBuffer(data.idepthVar_reAct);
if(permaRef_colorAndVarData != 0)
delete permaRef_colorAndVarData;
if(permaRef_posData != 0)
delete permaRef_posData;
privateFrameAllocCount--;
if(enablePrintDebugInfo && printMemoryDebugInfo)
printf("DELETED frame %d, now there are %d\n", this->id(),
privateFrameAllocCount);
}
if(data.validity_reAct == 0)
data.validity_reAct = (unsigned char*)
FrameMemory::getInstance().getBuffer(data.width[0]*data.height[0]);
if(data.idepth_reAct == 0)
data.idepth_reAct =
FrameMemory::getInstance().getFloatBuffer((data.width[0]*data.height[0]));
if(data.idepthVar_reAct == 0)
data.idepthVar_reAct =
FrameMemory::getInstance().getFloatBuffer((data.width[0]*data.height[0]));
data.reActivationDataValid = true;
}
permaRef_mutex.lock();
if(permaRef_colorAndVarData != 0)
delete permaRef_colorAndVarData;
if(permaRef_posData != 0)
delete permaRef_posData;
permaRefNumPts = reference->numData[QUICK_KF_CHECK_LVL];
permaRef_colorAndVarData = new Eigen::Vector2f[permaRefNumPts];
permaRef_posData = new Eigen::Vector3f[permaRefNumPts];
memcpy(permaRef_colorAndVarData,
reference->colorAndVarData[QUICK_KF_CHECK_LVL],
sizeof(Eigen::Vector2f) * permaRefNumPts);
memcpy(permaRef_posData,
reference->posData[QUICK_KF_CHECK_LVL],
sizeof(Eigen::Vector3f) * permaRefNumPts);
permaRef_mutex.unlock();
}
void Frame::calculateMeanInformation()
{
return;
if(numMappablePixels < 0)
maxGradients(0);
if(data.idepth[0] == 0)
data.idepth[0] =
FrameMemory::getInstance().getFloatBuffer(data.width[0]*data.height[0]);
if(data.idepthVar[0] == 0)
data.idepthVar[0] =
FrameMemory::getInstance().getFloatBuffer(data.width[0]*data.height[0]);
float sumIdepth=0;
int numIdepth=0;
numIdepth++;
sumIdepth += newDepth->idepth_smoothed;
}
else
{
*pyrIDepth = -1;
*pyrIDepthVar = -1;
}
}
data.idepthValid[0] = true;
data.idepthVarValid[0] = true;
release(IDEPTH | IDEPTH_VAR, true, true);
data.hasIDepthBeenSet = true;
depthHasBeenUpdatedFlag = true;
}
boost::unique_lock<boost::mutex> lock2(buildMutex);
if(data.idepth[0] == 0)
data.idepth[0] =
FrameMemory::getInstance().getFloatBuffer(data.width[0]*data.height[0]);
if(data.idepthVar[0] == 0)
data.idepthVar[0] =
FrameMemory::getInstance().getFloatBuffer(data.width[0]*data.height[0]);
for(int y=0;y<height0;y++)
{
for(int x=0;x<width0;x++)
{
if (x > 0 && x < width0-1 && y > 0 && y < height0-1 && //
pyramidMaxGradient is not valid for the border
pyrMaxGradient[x+y*width0] >= MIN_ABS_GRAD_CREATE &&
!isnanf(*depth) && *depth > 0)
{
*pyrIDepth = 1.0f / *depth;
*pyrIDepthVar = VAR_GT_INIT_INITIAL * cov_scale;
}
else
{
*pyrIDepth = -1;
*pyrIDepthVar = -1;
}
++ depth;
++ pyrIDepth;
++ pyrIDepthVar;
}
}
data.idepthValid[0] = true;
data.idepthVarValid[0] = true;
// data.refIDValid[0] = true;
// Invalidate higher levels, they need to be updated with the new data
release(IDEPTH | IDEPTH_VAR, true, true);
data.hasIDepthBeenSet = true;
}
thisToOther_t = thisToOther.translation().cast<float>();
K_thisToOther_t = K * thisToOther_t;
thisToOther_R = thisToOther.rotationMatrix().cast<float>() *
thisToOther.scale();
otherToThis_R_row0 = thisToOther_R.col(0);
otherToThis_R_row1 = thisToOther_R.col(1);
otherToThis_R_row2 = thisToOther_R.col(2);
distSquared = otherToThis.translation().dot(otherToThis.translation());
referenceID = other->id();
referenceLevel = level;
}
bool Frame::minimizeInMemory()
{
if(activeMutex.timed_lock(boost::posix_time::milliseconds(10)))
{
buildMutex.lock();
if(enablePrintDebugInfo && printMemoryDebugInfo)
printf("minimizing frame %d\n",id());
clear_refPixelWasGood();
buildMutex.unlock();
activeMutex.unlock();
return true;
}
return false;
}
data.K[0] = K;
data.fx[0] = K(0,0);
data.fy[0] = K(1,1);
data.cx[0] = K(0,2);
data.cy[0] = K(1,2);
data.KInv[0] = K.inverse();
data.fxInv[0] = data.KInv[0](0,0);
data.fyInv[0] = data.KInv[0](1,1);
data.cxInv[0] = data.KInv[0](0,2);
data.cyInv[0] = data.KInv[0](1,2);
data.timestamp = timestamp;
data.hasIDepthBeenSet = false;
depthHasBeenUpdatedFlag = false;
referenceID = -1;
referenceLevel = -1;
numMappablePixels = -1;
data.imageValid[level] = false;
data.gradientsValid[level] = false;
data.maxGradientsValid[level] = false;
data.idepthValid[level] = false;
data.idepthVarValid[level] = false;
data.image[level] = 0;
data.gradients[level] = 0;
data.maxGradients[level] = 0;
data.idepth[level] = 0;
data.idepthVar[level] = 0;
data.reActivationDataValid = false;
// data.refIDValid[level] = false;
if (level > 0)
{
data.fx[level] = data.fx[level-1] * 0.5;
data.fy[level] = data.fy[level-1] * 0.5;
data.cx[level] = (data.cx[0] + 0.5) / ((int)1<<level) - 0.5;
data.cy[level] = (data.cy[0] + 0.5) / ((int)1<<level) - 0.5;
data.fxInv[level] = data.KInv[level](0,0);
data.fyInv[level] = data.KInv[level](1,1);
data.cxInv[level] = data.KInv[level](0,2);
data.cyInv[level] = data.KInv[level](1,2);
}
}
data.validity_reAct = 0;
data.idepthVar_reAct = 0;
data.idepth_reAct = 0;
data.refPixelWasGood = 0;
permaRefNumPts = 0;
permaRef_colorAndVarData = 0;
permaRef_posData = 0;
meanIdepth = 1;
numPoints = 0;
idxInKeyframes = -1;
edgeErrorSum = edgesNum = 1;
lastConstraintTrackedCamToWorld = Sim3();
isActive = false;
}
void Frame::setDepth_Allocate()
{
return;
}
if(data.imageValid[level])
return;
if (data.image[level] == 0)
data.image[level] =
FrameMemory::getInstance().getFloatBuffer(data.width[level] * data.height[level]);
float* dest = data.image[level];
#if defined(ENABLE_SSE)
// I assume all all subsampled width's are a multiple of 8.
// if this is not the case, this still works except for the last * pixel,
which will produce a segfault.
// in that case, reduce this loop and calculate the last 0-3 dest pixels by
hand....
if (width % 8 == 0)
{
__m128 p025 = _mm_setr_ps(0.25f,0.25f,0.25f,0.25f);
_mm_store_ps(dest, sum);
dest += 4;
}
}
data.imageValid[level] = true;
return;
}
#elif defined(ENABLE_NEON)
// I assume all all subsampled width's are a multiple of 8.
// if this is not the case, this still works except for the last * pixel,
which will produce a segfault.
// in that case, reduce this loop and calculate the last 0-3 dest pixels by
hand....
if (width % 8 == 0)
{
static const float p025[] = {0.25, 0.25, 0.25, 0.25};
int width_iteration_count = width / 8;
int height_iteration_count = height / 2;
const float* cur_px = source;
const float* next_row_px = source + width;
__asm__ __volatile__
(
"vldmia %[p025], {q10} \n\t" // p025(q10)
".height_loop: \n\t"
"subs %[width_iteration_count], %
[width_iteration_count], #1 \n\t"
"bne .width_loop \n\t"
"mov %[width_iteration_count], r5 \n\t" // restore
width_iteration_count
: /* outputs */ [cur_px]"+&r"(cur_px),
[next_row_px]"+&r"(next_row_px),
[width_iteration_count]"+&r"(width_iteration_count),
[height_iteration_count]"+&r"(height_iteration_count),
[dest]"+&r"(dest)
: /* inputs */ [p025]"r"(p025),
[rowSize]"r"(width * sizeof(float))
: /* clobber */ "memory", "cc", "r5",
"q0", "q1", "q2", "q3", "q10"
);
data.imageValid[level] = true;
return;
}
#endif
int wh = width*height;
const float* s;
for(int y=0;y<wh;y+=width*2)
{
for(int x=0;x<width;x+=2)
{
s = source + x + y;
*dest = (s[0] +
s[1] +
s[width] +
s[1+width]) * 0.25f;
dest++;
}
}
data.imageValid[level] = true;
}
if(data.gradientsValid[level])
return;
val_m1 = val_00;
val_00 = val_p1;
}
data.gradientsValid[level] = true;
}
FrameMemory::getInstance().returnBuffer(reinterpret_cast<float*>(data.gradients[lev
el]));
data.gradients[level] = 0;
}
if(data.maxGradientsValid[level]) return;
float numMappablePixels = 0;
// 2. smear left/right direction into real data
maxgrad_pt = data.maxGradients[level] + width+1;
maxgrad_pt_max = data.maxGradients[level] + width*(height-1)-1;
maxgrad_t_pt = maxGradTemp + width+1;
for(;maxgrad_pt<maxgrad_pt_max; maxgrad_pt++, maxgrad_t_pt++)
{
float g1 = maxgrad_t_pt[-1];
float g2 = maxgrad_t_pt[0];
if(g1 < g2) g1 = g2;
float g3 = maxgrad_t_pt[1];
if(g1 < g3)
{
*maxgrad_pt = g3;
if(g3 >= MIN_ABS_GRAD_CREATE)
numMappablePixels++;
}
else
{
*maxgrad_pt = g1;
if(g1 >= MIN_ABS_GRAD_CREATE)
numMappablePixels++;
}
}
if(level==0)
this->numMappablePixels = numMappablePixels;
FrameMemory::getInstance().returnBuffer(maxGradTemp);
data.maxGradientsValid[level] = true;
}
if (data.idepth[level] == 0)
data.idepth[level] = FrameMemory::getInstance().getFloatBuffer(width *
height);
if (data.idepthVar[level] == 0)
data.idepthVar[level] = FrameMemory::getInstance().getFloatBuffer(width
* height);
for(int y=0;y<height;y++)
{
for(int x=0;x<width;x++)
{
int idx = 2*(x+y*sw);
int idxDest = (x+y*width);
float idepthSumsSum = 0;
float ivarSumsSum = 0;
int num=0;
// build sums
float ivar;
float var = idepthVarSource[idx];
if(var > 0)
{
ivar = 1.0f / var;
ivarSumsSum += ivar;
idepthSumsSum += ivar * idepthSource[idx];
num++;
}
var = idepthVarSource[idx+1];
if(var > 0)
{
ivar = 1.0f / var;
ivarSumsSum += ivar;
idepthSumsSum += ivar * idepthSource[idx+1];
num++;
}
var = idepthVarSource[idx+sw];
if(var > 0)
{
ivar = 1.0f / var;
ivarSumsSum += ivar;
idepthSumsSum += ivar * idepthSource[idx+sw];
num++;
}
var = idepthVarSource[idx+sw+1];
if(var > 0)
{
ivar = 1.0f / var;
ivarSumsSum += ivar;
idepthSumsSum += ivar * idepthSource[idx+sw+1];
num++;
}
if(num > 0)
{
float depth = ivarSumsSum / idepthSumsSum;
idepthDest[idxDest] = 1.0f / depth;
idepthVarDest[idxDest] = num / ivarSumsSum;
}
else
{
idepthDest[idxDest] = -1;
idepthVarDest[idxDest] = -1;
}
}
}
data.idepthValid[level] = true;
data.idepthVarValid[level] = true;
}
FrameMemory::getInstance().returnBuffer(data.idepth[level]);
data.idepth[level] = 0;
}