Skip to content

Commit f5aaa3b

Browse files
committed
added assm surface meshing in relightlab
1 parent 5f1139f commit f5aaa3b

File tree

5 files changed

+80
-7
lines changed

5 files changed

+80
-7
lines changed

relightlab/CMakeLists.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ if (APPLE)
2020
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
2121
endif()
2222

23+
24+
2325
set (RELIGHT_HEADERS
2426
processqueue.h
2527
../src/align.h
@@ -155,13 +157,18 @@ set (RELIGHTLAB_RESOURCES
155157
res.qrc
156158
)
157159

158-
add_executable(relightlab ${MACOSX_EXE_TARGET_OPTION} ${RELIGHTLAB_HEADERS} ${RELIGHTLAB_SOURCES} ${RELIGHTLAB_RESOURCES})
160+
file(GLOB ASSM_SOURCES ../external/assm/SurfaceMesh.cpp ../external/assm/algorithms/*.cpp)
161+
file(GLOB ASSM_HEADERS ../external/assm/*.h ../external/assm/algorithms/*.h)
162+
163+
164+
add_executable(relightlab ${MACOSX_EXE_TARGET_OPTION} ${RELIGHTLAB_HEADERS} ${RELIGHTLAB_SOURCES} ${ASSM_SOURCES} ${ASSM_HEADERS} ${RELIGHTLAB_RESOURCES})
159165
target_include_directories(
160166
relightlab PUBLIC
161167
${CMAKE_CURRENT_SOURCE_DIR}
162168
${JPEG_INCLUDE_DIR}
163169
TIFF::TIFF
164170
${EIGEN3_INCLUDE_DIR}
171+
../external
165172
)
166173

167174
target_link_libraries(

relightlab/normalstask.cpp

+55-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include "../src/bni_normal_integration.h"
77
#include "../src/flatnormals.h"
88

9+
#include <assm/Grid.h>
10+
#include <assm/algorithms/PhotometricRemeshing.h>
11+
#include <assm/algorithms/Integration.h>
12+
913
#include <Eigen/Eigen>
1014
#include <Eigen/Core>
1115
#include <Eigen/Dense>
@@ -129,6 +133,10 @@ void NormalsTask::run() {
129133
bool proceed = progressed("Integrating normals...", 0);
130134
if(!proceed)
131135
return;
136+
QString filename = output.left(output.size() -4) + ".obj";
137+
138+
float precision = 0.1f;
139+
assm(filename, normals, precision);
132140
std::vector<float> z;
133141
bni_integrate(callback, imageset.width, imageset.height, normals, z, bni_k);
134142
if(z.size() == 0) {
@@ -137,14 +145,60 @@ void NormalsTask::run() {
137145
return;
138146
}
139147
//TODO remove extension properly
140-
QString filename = output.left(output.size() -4) + ".ply";
141148

142149
progressed("Saving surface...", 99);
150+
filename = output.left(output.size() -4) + ".ply";
143151
savePly(filename, imageset.width, imageset.height, z);
144152
}
145153
progressed("Done", 100);
146154
}
147155

156+
bool saveObj(const char *filename, pmp::SurfaceMesh &mesh) {
157+
FILE *fp = fopen(filename, "wb");
158+
if(!fp) {
159+
// cerr << "Could not open file: " << filename << endl;
160+
return false;
161+
}
162+
int nvertices = 0;
163+
for(auto vertex: mesh.vertices()) {
164+
auto p = mesh.position(vertex);
165+
fprintf(fp, "v %f %f %f\n", p[0], p[1], p[2]);
166+
nvertices++;
167+
}
168+
for (auto face : mesh.faces()) {
169+
int indexes[3];
170+
int count =0 ;
171+
for (auto vertex : mesh.vertices(face)) {
172+
auto p = mesh.position(vertex);
173+
int v = indexes[count++] = vertex.idx() + 1;
174+
assert(v > 0 && v <= nvertices);
175+
}
176+
fprintf(fp, "f %d %d %d\n", indexes[0], indexes[1], indexes[2]);
177+
}
178+
fclose(fp);
179+
return true;
180+
}
181+
182+
void NormalsTask::assm(QString filename, std::vector<float> &_normals, float approx_error) {
183+
Grid<Eigen::Vector3f> normals(imageset.width, imageset.height, Eigen::Vector3f(0.0f, 0.0f, 0.0f));
184+
for(int i = 0; i < _normals.size()/3; i++) {
185+
normals[i] = Eigen::Vector3f(_normals[i*3], _normals[i*3+1], _normals[i*3+2]);
186+
}
187+
Grid<unsigned char> mask(imageset.width, imageset.height, 0);
188+
mask.fill(255);
189+
190+
float l_min = 1;
191+
float l_max = 100;
192+
193+
PhotometricRemeshing<pmp::Orthographic> remesher(normals, mask);
194+
remesher.run(l_min, l_max, approx_error);
195+
196+
pmp::Integration<double, pmp::Orthographic> integrator(remesher.mesh(), normals, mask);
197+
integrator.run();
198+
199+
saveObj(filename.toStdString().c_str(), remesher.mesh());
200+
}
201+
148202

149203
void NormalsWorker::run() {
150204
switch (solver)

relightlab/normalstask.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ class NormalsTask : public Task {
3636
}
3737

3838
virtual ~NormalsTask(){};
39-
void initFromProject(Project &project);
4039
virtual void run() override;
40+
41+
void initFromProject(Project &project);
42+
void assm(QString filename, std::vector<float> &normals, float precision);
43+
4144
};
4245

4346
class NormalsWorker

relightlab/relightlab.pro

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ DEFINES += QT_DEPRECATED_WARNINGS
99
DEFINES += _USE_MATH_DEFINES
1010
DEFINES += NOMINMAX
1111

12+
INCLUDEPATH += ../external/
13+
1214
win32:INCLUDEPATH += ../external/libjpeg-turbo-2.0.6/include \
1315
../external/eigen-3.3.9/ \
1416
../src/
@@ -36,6 +38,11 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
3638
!isEmpty(target.path): INSTALLS += target
3739

3840
SOURCES += main.cpp \
41+
../external/assm/SurfaceMesh.cpp \
42+
../external/assm/algorithms/DifferentialGeometry.cpp \
43+
../external/assm/algorithms/Rasterizer.cpp \
44+
../external/assm/algorithms/ScreenRemeshing.cpp \
45+
../external/assm/algorithms/Triangulation.cpp \
3946
../relight-cli/convert_rti.cpp \
4047
../relight-cli/rtibuilder.cpp \
4148
../src/flatnormals.cpp \
@@ -108,6 +115,8 @@ RESOURCES += \
108115

109116

110117
HEADERS += \
118+
../external/assm/algorithms/Integration.h \
119+
../external/assm/algorithms/PhotometricRemeshing.h \
111120
../relight-cli/rtibuilder.h \
112121
../src/flatnormals.h \
113122
processqueue.h \

src/bni_normal_integration.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ bool saveTiff(const QString &filename, int w, int h, std::vector<float> &depthma
106106
max = std::max(h, max);
107107
}
108108

109-
uint32 tileWidth = 256;
110-
uint32 tileLength = 256;
109+
uint32_t tileWidth = 256;
110+
uint32_t tileLength = 256;
111111

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

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

132132

133133

0 commit comments

Comments
 (0)