Skip to content

Commit

Permalink
add test sample
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaurer3 committed Sep 1, 2018
1 parent b1da7c9 commit d5ca398
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 0 deletions.
230 changes: 230 additions & 0 deletions src/Test_C_Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,240 @@ using namespace lepcc;

int ReadBlobSize(lepcc_ContextHdl ctx, FILE* fp);

int HasError(ErrCode errCode, const string& fctName)
{
if (errCode != ErrCode::Ok)
printf("Error in main(): %s failed. Error code = %d\n", fctName.c_str(), errCode);
return (int)errCode;
}

// -------------------------------------------------------------------------- ;

int main(int argc, char* argv[])
{
{
// open a small test binary slpk blob which has some LEPCC blobs embedded

string fnIn = "../testData/SMALL_AUTZEN_LAS_All.slpk";
string fnGT = "../testData/SMALL_AUTZEN_LAS_All.bin"; // decoded ground truth stored as raw binary arrays
bool bWriteGT = false; // toggle between write or read the ground truth

FILE* fp = 0;
fp = fopen(fnIn.c_str(), "rb");
if (!fp)
{
printf("Error in main(): Cannot read from file %s.\n", fnIn.c_str());
return 0;
}

fseek(fp, 0, SEEK_END);
size_t len = ftell(fp);
rewind(fp);

vector<Byte> byteVec(len, 0);
fread(&byteVec[0], 1, len, fp);
fclose(fp);

FILE* fpGT = 0;
fpGT = fopen(fnGT.c_str(), bWriteGT ? "wb" : "rb");
if (!fpGT)
{
printf("Error in main(): Cannot open file %s.\n", fnGT.c_str());
return 0;
}

// search for LEPCC blobs

vector<string> magicStrings;
magicStrings.push_back("LEPCC ");
magicStrings.push_back("ClusterRGB");
magicStrings.push_back("Intensity ");

lepcc_ContextHdl ctx = lepcc_createContext();
int nInfo = lepcc_getBlobInfoSize();
ErrCode errCode;

size_t magicLen = 10;
for (size_t pos = 0; pos < len - magicLen; pos++)
{
for (size_t blobType = 0; blobType < magicStrings.size(); blobType++)
{
if (0 == memcmp(&byteVec[pos], magicStrings[blobType].c_str(), magicLen))
{
const Byte* ptr = &byteVec[pos];
uint32 blobSize = 0;
lepcc_blobType bt;
errCode = (ErrCode)lepcc_getBlobInfo(ctx, ptr, nInfo, &bt, &blobSize);
if (HasError(errCode, "lepcc_getBlobInfo()"))
return 0;

switch (blobType)
{
case 0:
{
uint32 nPts = 0;
errCode = (ErrCode)lepcc_getPointCount(ctx, ptr, len - pos, &nPts);
if (HasError(errCode, "lepcc_getPointCount()"))
return 0;

vector<Point3D> ptVec(nPts);
errCode = (ErrCode)lepcc_decodeXYZ(ctx, &ptr, len - pos, &nPts, (double*)(&ptVec[0]));
if (HasError(errCode, "lepcc_decodeXYZ()"))
return 0;

printf("decode xyz blob succeeded.\n");

if (bWriteGT)
{
fwrite(&nPts, 4, 1, fpGT);
fwrite(&ptVec[0], sizeof(Point3D), nPts, fpGT);
}
else
{
uint32 nPts2 = 0;
fread(&nPts2, 4, 1, fpGT);
if (nPts2 != nPts)
{
printf("Error in main(): mismatch in number of elements %d vs %d\n", nPts, nPts2);
return 0;
}
vector<Point3D> ptVec2(nPts);
fread(&ptVec2[0], sizeof(Point3D), nPts, fpGT);

// compare
double dxMax(0), dyMax(0), dzMax(0);
for (int i = 0; i < (int)nPts; i++)
{
const Point3D* p = &ptVec[i];
const Point3D* q = &ptVec2[i];

double dx = abs(q->x - p->x);
double dy = abs(q->y - p->y);
double dz = abs(q->z - p->z);

dxMax = max(dx, dxMax);
dyMax = max(dy, dyMax);
dzMax = max(dz, dzMax);
}

printf("number of points = %d, dxMax = %f, dyMax = %f, dzMax = %f\n", nPts, dxMax, dyMax, dzMax);
}
}
break;

case 1:
{
uint32 nPts = 0;
errCode = (ErrCode)lepcc_getRGBCount(ctx, ptr, len - pos, &nPts);
if (HasError(errCode, "lepcc_getRGBCount()"))
return 0;

vector<RGB_t> rgbVec(nPts);
errCode = (ErrCode)lepcc_decodeRGB(ctx, &ptr, len - pos, &nPts, (Byte*)(&rgbVec[0]));
if (HasError(errCode, "lepcc_decodeRGB()"))
return 0;

printf("decode RGB blob succeeded.\n");

if (bWriteGT)
{
fwrite(&nPts, 4, 1, fpGT);
fwrite(&rgbVec[0], sizeof(RGB_t), nPts, fpGT);
}
else
{
uint32 nPts2 = 0;
fread(&nPts2, 4, 1, fpGT);
if (nPts2 != nPts)
{
printf("Error in main(): mismatch in number of elements %d vs %d\n", nPts, nPts2);
return 0;
}
vector<RGB_t> rgbVec2(nPts);
fread(&rgbVec2[0], sizeof(RGB_t), nPts, fpGT);

// compare
double dxMax(0), dyMax(0), dzMax(0);
for (int i = 0; i < (int)nPts; i++)
{
const RGB_t* p = &rgbVec[i];
const RGB_t* q = &rgbVec2[i];

double dx = abs(q->r - p->r);
double dy = abs(q->g - p->g);
double dz = abs(q->b - p->b);

dxMax = max(dx, dxMax);
dyMax = max(dy, dyMax);
dzMax = max(dz, dzMax);
}

printf("number of points = %d, dxMax = %f, dyMax = %f, dzMax = %f\n", nPts, dxMax, dyMax, dzMax);
}
}
break;

case 2:
{
uint32 nPts = 0;
errCode = (ErrCode)lepcc_getIntensityCount(ctx, ptr, len - pos, &nPts);
if (HasError(errCode, "lepcc_getIntensityCount()"))
return 0;

vector<unsigned short> intensityVec(nPts);
errCode = (ErrCode)lepcc_decodeIntensity(ctx, &ptr, len - pos, &nPts, (unsigned short*)(&intensityVec[0]));
if (HasError(errCode, "lepcc_decodeIntensity()"))
return 0;

printf("decode intensity blob succeeded.\n");

if (bWriteGT)
{
fwrite(&nPts, 4, 1, fpGT);
fwrite(&intensityVec[0], sizeof(unsigned short), nPts, fpGT);
}
else
{
uint32 nPts2 = 0;
fread(&nPts2, 4, 1, fpGT);
if (nPts2 != nPts)
{
printf("Error in main(): mismatch in number of elements %d vs %d\n", nPts, nPts2);
return 0;
}
vector<unsigned short> intensityVec2(nPts);
fread(&intensityVec2[0], sizeof(unsigned short), nPts, fpGT);

// compare
double dxMax(0);
for (int i = 0; i < (int)nPts; i++)
{
int intensity0 = intensityVec[i];
int intensity1 = intensityVec2[i];
double dx = abs(intensity1 - intensity0);
dxMax = max(dx, dxMax);
}

printf("number of points = %d, dxMax = %f\n", nPts, dxMax);
}
}
break;

default:
printf("Error in main(): Test for this LEPCC blob type not implemented.\n");
}

pos += blobSize - 1;
}
}
}

fclose(fpGT);
return 0;
}

// -------------- see below for examples on how to encode and decode --------

double maxXErr = 9e-8, maxYErr = maxXErr;
double maxZErr = 0.01;

Expand Down
Binary file added testData/SMALL_AUTZEN_LAS_All.bin
Binary file not shown.
Binary file added testData/SMALL_AUTZEN_LAS_All.slpk
Binary file not shown.

0 comments on commit d5ca398

Please sign in to comment.