Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RGB to Triangle, ITMMesh and the generated OBJ files. Fix #21 #22

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions InfiniTAM/Engine/UIEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ void UIEngine::glutKeyUpFunction(unsigned char key, int x, int y)
else uiEngine->mainEngine->turnOffIntegration();
break;
case 'w':
printf("saving mesh to disk ...");
uiEngine->SaveSceneToMesh("mesh.stl");
printf("saving colorful mesh to disk ...");
uiEngine->SaveSceneToMesh("mesh.obj");
printf(" done\n");
break;
default:
Expand Down
95 changes: 57 additions & 38 deletions InfiniTAM/ITMLib/Engine/DeviceAgnostic/ITMRepresentationAccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ _CPU_AND_GPU_CODE_ inline int findVoxel(const CONSTPTR(ITMLib::Objects::ITMPlain
Vector3i point = point_orig - voxelIndex->offset;

if ((point.x < 0) || (point.x >= voxelIndex->size.x) ||
(point.y < 0) || (point.y >= voxelIndex->size.y) ||
(point.z < 0) || (point.z >= voxelIndex->size.z)) {
(point.y < 0) || (point.y >= voxelIndex->size.y) ||
(point.z < 0) || (point.z >= voxelIndex->size.z)) {
isFound = false;
return -1;
}
Expand Down Expand Up @@ -245,91 +245,91 @@ _CPU_AND_GPU_CODE_ inline Vector3f computeSingleNormalFromSDF(const CONSTPTR(TVo
float p1, p2, v1;
// gradient x
p1 = front.x * ncoeff.y * ncoeff.z +
front.z * coeff.y * ncoeff.z +
back.x * ncoeff.y * coeff.z +
back.z * coeff.y * coeff.z;
front.z * coeff.y * ncoeff.z +
back.x * ncoeff.y * coeff.z +
back.z * coeff.y * coeff.z;
tmp.x = readVoxel(voxelData, voxelIndex, pos + Vector3i(-1, 0, 0), isFound).sdf;
tmp.y = readVoxel(voxelData, voxelIndex, pos + Vector3i(-1, 1, 0), isFound).sdf;
tmp.z = readVoxel(voxelData, voxelIndex, pos + Vector3i(-1, 0, 1), isFound).sdf;
tmp.w = readVoxel(voxelData, voxelIndex, pos + Vector3i(-1, 1, 1), isFound).sdf;
p2 = tmp.x * ncoeff.y * ncoeff.z +
tmp.y * coeff.y * ncoeff.z +
tmp.z * ncoeff.y * coeff.z +
tmp.w * coeff.y * coeff.z;
tmp.y * coeff.y * ncoeff.z +
tmp.z * ncoeff.y * coeff.z +
tmp.w * coeff.y * coeff.z;
v1 = p1 * coeff.x + p2 * ncoeff.x;

p1 = front.y * ncoeff.y * ncoeff.z +
front.w * coeff.y * ncoeff.z +
back.y * ncoeff.y * coeff.z +
back.w * coeff.y * coeff.z;
front.w * coeff.y * ncoeff.z +
back.y * ncoeff.y * coeff.z +
back.w * coeff.y * coeff.z;
tmp.x = readVoxel(voxelData, voxelIndex, pos + Vector3i(2, 0, 0), isFound).sdf;
tmp.y = readVoxel(voxelData, voxelIndex, pos + Vector3i(2, 1, 0), isFound).sdf;
tmp.z = readVoxel(voxelData, voxelIndex, pos + Vector3i(2, 0, 1), isFound).sdf;
tmp.w = readVoxel(voxelData, voxelIndex, pos + Vector3i(2, 1, 1), isFound).sdf;
p2 = tmp.x * ncoeff.y * ncoeff.z +
tmp.y * coeff.y * ncoeff.z +
tmp.z * ncoeff.y * coeff.z +
tmp.w * coeff.y * coeff.z;
tmp.y * coeff.y * ncoeff.z +
tmp.z * ncoeff.y * coeff.z +
tmp.w * coeff.y * coeff.z;

ret.x = TVoxel::SDF_valueToFloat(p1 * ncoeff.x + p2 * coeff.x - v1);

// gradient y
p1 = front.x * ncoeff.x * ncoeff.z +
front.y * coeff.x * ncoeff.z +
back.x * ncoeff.x * coeff.z +
back.y * coeff.x * coeff.z;
front.y * coeff.x * ncoeff.z +
back.x * ncoeff.x * coeff.z +
back.y * coeff.x * coeff.z;
tmp.x = readVoxel(voxelData, voxelIndex, pos + Vector3i(0, -1, 0), isFound).sdf;
tmp.y = readVoxel(voxelData, voxelIndex, pos + Vector3i(1, -1, 0), isFound).sdf;
tmp.z = readVoxel(voxelData, voxelIndex, pos + Vector3i(0, -1, 1), isFound).sdf;
tmp.w = readVoxel(voxelData, voxelIndex, pos + Vector3i(1, -1, 1), isFound).sdf;
p2 = tmp.x * ncoeff.x * ncoeff.z +
tmp.y * coeff.x * ncoeff.z +
tmp.z * ncoeff.x * coeff.z +
tmp.w * coeff.x * coeff.z;
tmp.y * coeff.x * ncoeff.z +
tmp.z * ncoeff.x * coeff.z +
tmp.w * coeff.x * coeff.z;
v1 = p1 * coeff.y + p2 * ncoeff.y;

p1 = front.z * ncoeff.x * ncoeff.z +
front.w * coeff.x * ncoeff.z +
back.z * ncoeff.x * coeff.z +
back.w * coeff.x * coeff.z;
front.w * coeff.x * ncoeff.z +
back.z * ncoeff.x * coeff.z +
back.w * coeff.x * coeff.z;
tmp.x = readVoxel(voxelData, voxelIndex, pos + Vector3i(0, 2, 0), isFound).sdf;
tmp.y = readVoxel(voxelData, voxelIndex, pos + Vector3i(1, 2, 0), isFound).sdf;
tmp.z = readVoxel(voxelData, voxelIndex, pos + Vector3i(0, 2, 1), isFound).sdf;
tmp.w = readVoxel(voxelData, voxelIndex, pos + Vector3i(1, 2, 1), isFound).sdf;
p2 = tmp.x * ncoeff.x * ncoeff.z +
tmp.y * coeff.x * ncoeff.z +
tmp.z * ncoeff.x * coeff.z +
tmp.w * coeff.x * coeff.z;
tmp.y * coeff.x * ncoeff.z +
tmp.z * ncoeff.x * coeff.z +
tmp.w * coeff.x * coeff.z;

ret.y = TVoxel::SDF_valueToFloat(p1 * ncoeff.y + p2 * coeff.y - v1);

// gradient z
p1 = front.x * ncoeff.x * ncoeff.y +
front.y * coeff.x * ncoeff.y +
front.z * ncoeff.x * coeff.y +
front.w * coeff.x * coeff.y;
front.y * coeff.x * ncoeff.y +
front.z * ncoeff.x * coeff.y +
front.w * coeff.x * coeff.y;
tmp.x = readVoxel(voxelData, voxelIndex, pos + Vector3i(0, 0, -1), isFound).sdf;
tmp.y = readVoxel(voxelData, voxelIndex, pos + Vector3i(1, 0, -1), isFound).sdf;
tmp.z = readVoxel(voxelData, voxelIndex, pos + Vector3i(0, 1, -1), isFound).sdf;
tmp.w = readVoxel(voxelData, voxelIndex, pos + Vector3i(1, 1, -1), isFound).sdf;
p2 = tmp.x * ncoeff.x * ncoeff.y +
tmp.y * coeff.x * ncoeff.y +
tmp.z * ncoeff.x * coeff.y +
tmp.w * coeff.x * coeff.y;
tmp.y * coeff.x * ncoeff.y +
tmp.z * ncoeff.x * coeff.y +
tmp.w * coeff.x * coeff.y;
v1 = p1 * coeff.z + p2 * ncoeff.z;

p1 = back.x * ncoeff.x * ncoeff.y +
back.y * coeff.x * ncoeff.y +
back.z * ncoeff.x * coeff.y +
back.w * coeff.x * coeff.y;
back.y * coeff.x * ncoeff.y +
back.z * ncoeff.x * coeff.y +
back.w * coeff.x * coeff.y;
tmp.x = readVoxel(voxelData, voxelIndex, pos + Vector3i(0, 0, 2), isFound).sdf;
tmp.y = readVoxel(voxelData, voxelIndex, pos + Vector3i(1, 0, 2), isFound).sdf;
tmp.z = readVoxel(voxelData, voxelIndex, pos + Vector3i(0, 1, 2), isFound).sdf;
tmp.w = readVoxel(voxelData, voxelIndex, pos + Vector3i(1, 1, 2), isFound).sdf;
p2 = tmp.x * ncoeff.x * ncoeff.y +
tmp.y * coeff.x * ncoeff.y +
tmp.z * ncoeff.x * coeff.y +
tmp.w * coeff.x * coeff.y;
tmp.y * coeff.x * ncoeff.y +
tmp.z * ncoeff.x * coeff.y +
tmp.w * coeff.x * coeff.y;

ret.z = TVoxel::SDF_valueToFloat(p1 * ncoeff.z + p2 * coeff.z - v1);

Expand All @@ -343,6 +343,12 @@ struct VoxelColorReader<false,TVoxel,TIndex> {
_CPU_AND_GPU_CODE_ static Vector4f interpolate(const CONSTPTR(TVoxel) *voxelData, const CONSTPTR(typename TIndex::IndexData) *voxelIndex,
const THREADPTR(Vector3f) & point)
{ return Vector4f(0.0f,0.0f,0.0f,0.0f); }

_CPU_AND_GPU_CODE_ static Vector4f uninterpolate(const CONSTPTR(TVoxel) *voxelData, const CONSTPTR(typename TIndex::IndexData) *voxelIndex,
const THREADPTR(Vector3f) & point)
{
return Vector4f(0.0f, 0.0f, 0.0f, 0.0f);
}
};

template<class TVoxel, class TIndex>
Expand All @@ -353,4 +359,17 @@ struct VoxelColorReader<true,TVoxel,TIndex> {
typename TIndex::IndexCache cache;
return readFromSDF_color4u_interpolated<TVoxel,TIndex>(voxelData, voxelIndex, point, cache);
}

_CPU_AND_GPU_CODE_ static Vector3f uninterpolate(const CONSTPTR(TVoxel) *voxelData, const CONSTPTR(typename TIndex::IndexData) *voxelIndex,
const THREADPTR(Vector3i) & point)
{
typename TIndex::IndexCache cache;
bool isFound;
TVoxel resn = readVoxel(voxelData, voxelIndex, point, isFound, cache);

if (isFound)
return Vector3f(resn.clr.r / 255.0f, resn.clr.g / 255.0f, resn.clr.b / 255.0f);
else
return Vector3f(0.0f, 0.0f, 0.0f);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ void ITMMeshingEngine_CPU<TVoxel, ITMVoxelBlockHash>::MeshScene(ITMMesh *mesh, c

if (cubeIndex < 0) continue;

Vector3f clr = VoxelColorReader<TVoxel::hasColorInformation, TVoxel, ITMVoxelBlockHash>::
uninterpolate(localVBA, hashTable, globalPos + Vector3i(x, y, z));

for (int i = 0; triangleTable[cubeIndex][i] != -1; i += 3)
{
triangles[noTriangles].p0 = vertList[triangleTable[cubeIndex][i]] * factor;
triangles[noTriangles].p1 = vertList[triangleTable[cubeIndex][i + 1]] * factor;
triangles[noTriangles].p2 = vertList[triangleTable[cubeIndex][i + 2]] * factor;
triangles[noTriangles].clr = clr;

if (noTriangles < noMaxTriangles - 1) noTriangles++;
}
Expand Down
2 changes: 1 addition & 1 deletion InfiniTAM/ITMLib/Engine/ITMMainEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void ITMMainEngine::SaveSceneToMesh(const char *objFileName)
{
if (mesh == NULL) return;
meshingEngine->MeshScene(mesh, scene);
mesh->WriteSTL(objFileName);
mesh->WriteOBJ(objFileName);
}

void ITMMainEngine::ProcessFrame(ITMUChar4Image *rgbImage, ITMShortImage *rawDepthImage, ITMIMUMeasurement *imuMeasurement)
Expand Down
11 changes: 6 additions & 5 deletions InfiniTAM/ITMLib/Objects/ITMMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ITMLib
class ITMMesh
{
public:
struct Triangle { Vector3f p0, p1, p2; };
struct Triangle { Vector3f p0, p1, p2, clr; };

MemoryDeviceType memoryType;

Expand Down Expand Up @@ -49,10 +49,11 @@ namespace ITMLib
{
for (uint i = 0; i < noTotalTriangles; i++)
{
fprintf(f, "v %f %f %f\n", triangleArray[i].p0.x, triangleArray[i].p0.y, triangleArray[i].p0.z);
fprintf(f, "v %f %f %f\n", triangleArray[i].p1.x, triangleArray[i].p1.y, triangleArray[i].p1.z);
fprintf(f, "v %f %f %f\n", triangleArray[i].p2.x, triangleArray[i].p2.y, triangleArray[i].p2.z);
}
const auto& clr = triangleArray[i].clr;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vinjn: Please can you replace auto with Vector3f? The former breaks the build on older compilers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, what about other changes.

fprintf(f, "v %f %f %f %f %f %f\n", triangleArray[i].p0.x, triangleArray[i].p0.y, triangleArray[i].p0.z, clr.r, clr.g, clr.b);
fprintf(f, "v %f %f %f %f %f %f\n", triangleArray[i].p1.x, triangleArray[i].p1.y, triangleArray[i].p1.z, clr.r, clr.g, clr.b);
fprintf(f, "v %f %f %f %f %f %f\n", triangleArray[i].p2.x, triangleArray[i].p2.y, triangleArray[i].p2.z, clr.r, clr.g, clr.b);
}

for (uint i = 0; i<noTotalTriangles; i++) fprintf(f, "f %d %d %d\n", i * 3 + 2 + 1, i * 3 + 1 + 1, i * 3 + 0 + 1);
fclose(f);
Expand Down
2 changes: 1 addition & 1 deletion InfiniTAM/ITMLib/Utils/ITMLibDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ struct ITMVoxel_f
/** This chooses the information stored at each voxel. At the moment, valid
options are ITMVoxel_s, ITMVoxel_f, ITMVoxel_s_rgb and ITMVoxel_f_rgb
*/
typedef ITMVoxel_s ITMVoxel;
typedef ITMVoxel_s_rgb ITMVoxel;

/** This chooses the way the voxels are addressed and indexed. At the moment,
valid options are ITMVoxelBlockHash and ITMPlainVoxelArray.
Expand Down
4 changes: 3 additions & 1 deletion InfiniTAM/InfiniTAM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ static void CreateDefaultImageSource(ImageSourceEngine* & imageSource, IMUSource
if (imageSource == NULL)
{
printf("trying OpenNI device: %s\n", (filename1==NULL)?"<OpenNI default device>":filename1);
imageSource = new OpenNIEngine(calibFile, filename1);

// Set useInternalCalibration to true if ITMVoxel::hasColorInformation is true
imageSource = new OpenNIEngine(calibFile, filename1, ITMVoxel::hasColorInformation);
if (imageSource->getDepthImageSize().x == 0)
{
delete imageSource;
Expand Down