Skip to content

Commit

Permalink
fixed overflow for large normalmaps integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
ponchio committed Jan 14, 2025
1 parent e0c8676 commit 8b58e50
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
3 changes: 2 additions & 1 deletion relightlab/normalsframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ void NormalsFrame::save() {
NormalsTask *task = new NormalsTask();
task->setParameters(parameters);
task->output = parameters.path;
task->initFromProject(qRelightApp->project());
if(parameters.compute)
task->initFromProject(qRelightApp->project());

ProcessQueue &queue = ProcessQueue::instance();
queue.addTask(task);
Expand Down
18 changes: 15 additions & 3 deletions relightlab/normalstask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ void NormalsTask::run() {
case FLAT_FOURIER:
//convert radius to frequencies
double sigma = width*parameters.flatPercentage/100;
flattenFourierNormals(width, height, normals, 0.2, sigma);
try {
flattenFourierNormals(width, height, normals, 0.2, sigma);
} catch(std::length_error e) {
return;
}
break;
}
}
Expand Down Expand Up @@ -194,8 +198,16 @@ void NormalsTask::run() {
vector<float> z;
if(parameters.surface_integration == SURFACE_BNI)
bni_integrate(callback, width, height, normals, z, parameters.bni_k);
else
fft_integrate(callback, width, height, normals, z);
else {

try {
fft_integrate(callback, width, height, normals, z);
} catch(std::length_error e) {
error = "Failed to integrate normals, length error.";
status = FAILED;
return;
}
}

if(z.size() == 0) {
error = "Failed to integrate normals";
Expand Down
30 changes: 15 additions & 15 deletions src/bni_normal_integration.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <Eigen/Core>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/SparseCore>
#include <Eigen/IterativeLinearSolvers>
Expand All @@ -23,15 +23,15 @@ double sigmoid(const double x, const double k = 1.0) {
return 1.0 / (1.0 + exp(-x*k));
}

bool saveDepthMap(const QString &filename, int w, int h, std::vector<float> &z) {
bool saveDepthMap(const QString &filename, size_t w, size_t h, std::vector<float> &z) {
return false;
}

bool saveNormalMap(const QString &filename, int w, int h, std::vector<float> &normals) {
bool saveNormalMap(const QString &filename, size_t w, size_t h, std::vector<float> &normals) {
QImage img(w, h, QImage::Format_ARGB32);
for(int y = 0; y < h; y++) {
for(size_t y = 0; y < h; y++) {
uint8_t *line = img.scanLine(y);
for(int x = 0; x < w; x++) {
for(size_t x = 0; x < w; x++) {
float *n = &normals[3*(x + y*w)];
line[4*x + 0] = 255;
line[4*x + 1] = floor(n[0]*255.0f);
Expand All @@ -43,7 +43,7 @@ bool saveNormalMap(const QString &filename, int w, int h, std::vector<float> &no
return true;
}

bool savePly(const QString &filename, int w, int h, std::vector<float> &z) {
bool savePly(const QString &filename, size_t w, size_t h, std::vector<float> &z) {
QFile file(filename);
bool success = file.open(QFile::WriteOnly);
if(!success)
Expand All @@ -63,9 +63,9 @@ bool savePly(const QString &filename, int w, int h, std::vector<float> &z) {
}

std::vector<float> vertices(w*h*3);
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
int pos = x + y*w;
for(size_t y = 0; y < h; y++) {
for(size_t x = 0; x < w; x++) {
size_t pos = x + y*w;
float *start = &vertices[3*pos];
start[0] = x;
start[1] = h - y -1;
Expand All @@ -74,9 +74,9 @@ bool savePly(const QString &filename, int w, int h, std::vector<float> &z) {
}
}
std::vector<uint8_t> indices(13*2*(w-1)*(h-1));
for(int y = 0; y < h-1; y++) {
for(int x = 0; x < w-1; x++) {
int pos = x + y*w;
for(size_t y = 0; y < h-1; y++) {
for(size_t x = 0; x < w-1; x++) {
size_t pos = x + y*w;
uint8_t *start = &indices[26*(x + y*(w-1))];
start[0] = 3;
int *face = (int *)(start + 1);
Expand All @@ -99,7 +99,7 @@ bool savePly(const QString &filename, int w, int h, std::vector<float> &z) {
return true;
}

bool saveTiff(const QString &filename, int w, int h, std::vector<float> &depthmap) {
bool saveTiff(const QString &filename, size_t w, size_t h, std::vector<float> &depthmap) {
float min = 1e20;
float max = -1e20;
for(float h: depthmap) {
Expand Down Expand Up @@ -140,8 +140,8 @@ bool saveTiff(const QString &filename, int w, int h, std::vector<float> &depthma

for(uint32_t dy = 0; dy < tileLength; dy++) {
for(uint32_t dx = 0; dx < tileWidth; dx++) {
uint32_t x = tx*tileWidth + dx;
uint32_t y = ty*tileLength + dy;
size_t x = tx*tileWidth + dx;
size_t y = ty*tileLength + dy;
if(x < w && y < h)
data[dx + dy*tileWidth] = depthmap[x + y*w];
}
Expand Down
6 changes: 3 additions & 3 deletions src/bni_normal_integration.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ std::vector<float> bni_pyramid(std::function<bool(QString s, int n)> progressed,
int max_solver_iterations = 5000,
int scale = 0);

bool savePly(const QString &filename, int w, int h, std::vector<float> &z);
bool saveTiff(const QString &filename, int w, int h, std::vector<float> &z);
bool saveDepthMap(const QString &filename, int w, int h, std::vector<float> &z);
bool savePly(const QString &filename, size_t w, size_t h, std::vector<float> &z);
bool saveTiff(const QString &filename, size_t w, size_t h, std::vector<float> &z);
bool saveDepthMap(const QString &filename, size_t w, size_t h, std::vector<float> &z);

#endif // BNI_NORMAL_INTEGRATION_H
6 changes: 3 additions & 3 deletions src/fft_normal_integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void depad(int &w, int &h, std::vector<float> &heights, int padding) {
swap(elev, heights);
}

bool savePly(const QString &filename, int w, int h, std::vector<float> &z);
bool savePly(const QString &filename, size_t w, size_t h, std::vector<float> &z);

void fft_integrate(std::function<bool(QString s, int n)> progressed,
int cols, int rows, std::vector<float> &normals, std::vector<float> &heights) {
Expand Down Expand Up @@ -213,9 +213,9 @@ void fft_integrate(std::function<bool(QString s, int n)> progressed,
heights[i * cols + j] = static_cast<float>(z(i, j));
}
}

savePly("test.ply", cols, rows, heights);
depad(cols, rows, heights, padding);
savePly("test.ply", cols, rows, heights);


/*
[wx, wy] = meshgrid(([1:cols]-(fix(cols/2)+1))/(cols-mod(cols,2)), ...
Expand Down

0 comments on commit 8b58e50

Please sign in to comment.