Skip to content

Commit

Permalink
added assm surface meshing in relightlab
Browse files Browse the repository at this point in the history
  • Loading branch information
ponchio committed Nov 26, 2024
1 parent 5f1139f commit f5aaa3b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
9 changes: 8 additions & 1 deletion relightlab/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ if (APPLE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
endif()



set (RELIGHT_HEADERS
processqueue.h
../src/align.h
Expand Down Expand Up @@ -155,13 +157,18 @@ set (RELIGHTLAB_RESOURCES
res.qrc
)

add_executable(relightlab ${MACOSX_EXE_TARGET_OPTION} ${RELIGHTLAB_HEADERS} ${RELIGHTLAB_SOURCES} ${RELIGHTLAB_RESOURCES})
file(GLOB ASSM_SOURCES ../external/assm/SurfaceMesh.cpp ../external/assm/algorithms/*.cpp)
file(GLOB ASSM_HEADERS ../external/assm/*.h ../external/assm/algorithms/*.h)


add_executable(relightlab ${MACOSX_EXE_TARGET_OPTION} ${RELIGHTLAB_HEADERS} ${RELIGHTLAB_SOURCES} ${ASSM_SOURCES} ${ASSM_HEADERS} ${RELIGHTLAB_RESOURCES})
target_include_directories(
relightlab PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${JPEG_INCLUDE_DIR}
TIFF::TIFF
${EIGEN3_INCLUDE_DIR}
../external
)

target_link_libraries(
Expand Down
56 changes: 55 additions & 1 deletion relightlab/normalstask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "../src/bni_normal_integration.h"
#include "../src/flatnormals.h"

#include <assm/Grid.h>
#include <assm/algorithms/PhotometricRemeshing.h>
#include <assm/algorithms/Integration.h>

#include <Eigen/Eigen>
#include <Eigen/Core>
#include <Eigen/Dense>
Expand Down Expand Up @@ -129,6 +133,10 @@ void NormalsTask::run() {
bool proceed = progressed("Integrating normals...", 0);
if(!proceed)
return;
QString filename = output.left(output.size() -4) + ".obj";

float precision = 0.1f;
assm(filename, normals, precision);
std::vector<float> z;
bni_integrate(callback, imageset.width, imageset.height, normals, z, bni_k);
if(z.size() == 0) {
Expand All @@ -137,14 +145,60 @@ void NormalsTask::run() {
return;
}
//TODO remove extension properly
QString filename = output.left(output.size() -4) + ".ply";

progressed("Saving surface...", 99);
filename = output.left(output.size() -4) + ".ply";
savePly(filename, imageset.width, imageset.height, z);
}
progressed("Done", 100);
}

bool saveObj(const char *filename, pmp::SurfaceMesh &mesh) {
FILE *fp = fopen(filename, "wb");
if(!fp) {
// cerr << "Could not open file: " << filename << endl;
return false;
}
int nvertices = 0;
for(auto vertex: mesh.vertices()) {
auto p = mesh.position(vertex);
fprintf(fp, "v %f %f %f\n", p[0], p[1], p[2]);
nvertices++;
}
for (auto face : mesh.faces()) {
int indexes[3];
int count =0 ;
for (auto vertex : mesh.vertices(face)) {
auto p = mesh.position(vertex);
int v = indexes[count++] = vertex.idx() + 1;
assert(v > 0 && v <= nvertices);
}
fprintf(fp, "f %d %d %d\n", indexes[0], indexes[1], indexes[2]);
}
fclose(fp);
return true;
}

void NormalsTask::assm(QString filename, std::vector<float> &_normals, float approx_error) {
Grid<Eigen::Vector3f> normals(imageset.width, imageset.height, Eigen::Vector3f(0.0f, 0.0f, 0.0f));
for(int i = 0; i < _normals.size()/3; i++) {
normals[i] = Eigen::Vector3f(_normals[i*3], _normals[i*3+1], _normals[i*3+2]);
}
Grid<unsigned char> mask(imageset.width, imageset.height, 0);
mask.fill(255);

float l_min = 1;
float l_max = 100;

PhotometricRemeshing<pmp::Orthographic> remesher(normals, mask);
remesher.run(l_min, l_max, approx_error);

pmp::Integration<double, pmp::Orthographic> integrator(remesher.mesh(), normals, mask);
integrator.run();

saveObj(filename.toStdString().c_str(), remesher.mesh());
}


void NormalsWorker::run() {
switch (solver)
Expand Down
5 changes: 4 additions & 1 deletion relightlab/normalstask.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ class NormalsTask : public Task {
}

virtual ~NormalsTask(){};
void initFromProject(Project &project);
virtual void run() override;

void initFromProject(Project &project);
void assm(QString filename, std::vector<float> &normals, float precision);

};

class NormalsWorker
Expand Down
9 changes: 9 additions & 0 deletions relightlab/relightlab.pro
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ DEFINES += QT_DEPRECATED_WARNINGS
DEFINES += _USE_MATH_DEFINES
DEFINES += NOMINMAX

INCLUDEPATH += ../external/

win32:INCLUDEPATH += ../external/libjpeg-turbo-2.0.6/include \
../external/eigen-3.3.9/ \
../src/
Expand Down Expand Up @@ -36,6 +38,11 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

SOURCES += main.cpp \
../external/assm/SurfaceMesh.cpp \
../external/assm/algorithms/DifferentialGeometry.cpp \
../external/assm/algorithms/Rasterizer.cpp \
../external/assm/algorithms/ScreenRemeshing.cpp \
../external/assm/algorithms/Triangulation.cpp \
../relight-cli/convert_rti.cpp \
../relight-cli/rtibuilder.cpp \
../src/flatnormals.cpp \
Expand Down Expand Up @@ -108,6 +115,8 @@ RESOURCES += \


HEADERS += \
../external/assm/algorithms/Integration.h \
../external/assm/algorithms/PhotometricRemeshing.h \
../relight-cli/rtibuilder.h \
../src/flatnormals.h \
processqueue.h \
Expand Down
8 changes: 4 additions & 4 deletions src/bni_normal_integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ bool saveTiff(const QString &filename, int w, int h, std::vector<float> &depthma
max = std::max(h, max);
}

uint32 tileWidth = 256;
uint32 tileLength = 256;
uint32_t tileWidth = 256;
uint32_t tileLength = 256;

TIFF* outTiff = TIFFOpen(filename.toStdString().c_str(), "w");
if (!outTiff) {
Expand All @@ -126,8 +126,8 @@ bool saveTiff(const QString &filename, int w, int h, std::vector<float> &depthma
TIFFSetField(outTiff, TIFFTAG_TILELENGTH, tileLength);
TIFFSetField(outTiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE); // No compression

uint32 numTilesX = (w + tileWidth - 1) / tileWidth;
uint32 numTilesY = (h + tileLength - 1) / tileLength;
uint32_t numTilesX = (w + tileWidth - 1) / tileWidth;
uint32_t numTilesY = (h + tileLength - 1) / tileLength;



Expand Down

0 comments on commit f5aaa3b

Please sign in to comment.