Skip to content

Commit

Permalink
modify loadText function
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikaPistolesi1 committed Jan 8, 2025
1 parent b640084 commit 619ec33
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 148 deletions.
302 changes: 157 additions & 145 deletions depthmap/depthmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ void Depthmap::resizeNormals (int factorPowerOfTwo, int step) {
//depthIntegrateNormals();

}
bool Depthmap::loadPly(const char *textPath, const char *outputPath){
bool Depthmap::loadText(const char *textPath, const char *outputPath){

//apri file di testo e scarta tutto quello che non è un numero. fai un char vettore di punti
QFile file(textPath);
Expand All @@ -713,197 +713,209 @@ bool Depthmap::loadPly(const char *textPath, const char *outputPath){
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
QStringList parts = line.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);


if (parts.size() >= 3) {
bool isNumber1 = false, isNumber2 = false, isNumber3 = false;

parts[0].toDouble(&isNumber1);
parts[1].toDouble(&isNumber2);
parts[2].toDouble(&isNumber3);

if (isNumber1 && isNumber2 && isNumber3) {
validLines.append(line);
if (line.isEmpty() || !line[0].isDigit() && line[0] != '-' && line[0] != '+') {
continue;
}
QVector<QString> numbers;

bool isValid = true;
for (int i = 0; i < 3 && i < parts.size(); ++i) {
bool isNumber = false;
parts[i].toFloat(&isNumber);
if (!isNumber) {
isValid = false;
break;
}
}
if (isValid) {
for (const QString &part : parts) {
bool isNumber = false;
part.toFloat(&isNumber);
if (isNumber) {
numbers.append(part);
}
}
if (!numbers.isEmpty()) {
validLines.append(numbers.join(" "));
}
}
}

QFile outFile(outputPath);
if (outFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&outFile);

for (const QString &line : validLines) {
out << line << "\n";
}

cout << "Punti salvati in " << outputPath << endl;
} else {
if (!outFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
cerr << "Errore nell'aprire il file di output: " << outputPath << endl;
return false;
}
QTextStream out(&outFile);
for (const QString &line : validLines) {
out << line << "\n";
}

cout << "Punti salvati in " << outputPath << endl;

return true;
}


//take the x=160,14 e y=140
//the coordinates of the pixel step 0.016 are in the depth map xml Z_num etc.

/* ---------------------------------------------------------------------------------------------------------------------------*/
//start OrthoDepthMap class
//take the x=160,14 e y=140
//the coordinates of the pixel step 0.016 are in the depth map xml Z_num etc.

bool OrthoDepthmap::loadXml(const char *xmlPath){
//depthmap in Malt Z deZoom ecc
QFile file(xmlPath);
if (!file.open(QIODevice::ReadOnly)) {
cerr << "Cannot open XML file: " << xmlPath << endl;
return false;
}
/* ---------------------------------------------------------------------------------------------------------------------------*/
//start OrthoDepthMap class

QDomDocument doc;
doc.setContent(&file);
bool OrthoDepthmap::loadXml(const char *xmlPath){
//depthmap in Malt Z deZoom ecc
QFile file(xmlPath);
if (!file.open(QIODevice::ReadOnly)) {
cerr << "Cannot open XML file: " << xmlPath << endl;
return false;
}

QDomElement root = doc.documentElement();
QDomNodeList originePlaniNodes = root.elementsByTagName("OriginePlani");
QDomNodeList resolutionPlaniNodes = root.elementsByTagName("ResolutionPlani");
QDomNodeList origineAltiNodes = root.elementsByTagName("OrigineAlti");
QDomNodeList resolutionAltiNodes = root.elementsByTagName("ResolutionAlti");
QDomDocument doc;
doc.setContent(&file);

QDomElement root = doc.documentElement();
QDomNodeList originePlaniNodes = root.elementsByTagName("OriginePlani");
QDomNodeList resolutionPlaniNodes = root.elementsByTagName("ResolutionPlani");
QDomNodeList origineAltiNodes = root.elementsByTagName("OrigineAlti");
QDomNodeList resolutionAltiNodes = root.elementsByTagName("ResolutionAlti");

if (originePlaniNodes.isEmpty() || resolutionPlaniNodes.isEmpty() ||
origineAltiNodes.isEmpty() || resolutionAltiNodes.isEmpty()) {
cerr << "OriginePlani, ResolutionPlani, OrigineAlti, or ResolutionAlti not found in XML." << endl;
return false;

}
if (originePlaniNodes.isEmpty() || resolutionPlaniNodes.isEmpty() ||
origineAltiNodes.isEmpty() || resolutionAltiNodes.isEmpty()) {
cerr << "OriginePlani, ResolutionPlani, OrigineAlti, or ResolutionAlti not found in XML." << endl;
return false;

// <OriginePlani>-2.72 3.04</OriginePlani>
QStringList origineValues = originePlaniNodes.at(0).toElement().text().split(" ");
if (origineValues.size() >= 2) {
origin[0] = origineValues.at(0).toFloat();
origin[1] = origineValues.at(1).toFloat();
}
QStringList resolutionValues = resolutionPlaniNodes.at(0).toElement().text().split(" ");
if (resolutionValues.size() >= 2) {
resolution[0] = resolutionValues.at(0).toFloat();
resolution[1] = resolutionValues.at(1).toFloat();
}
}

// <ResolutionPlani>0.128 -0.128</ResolutionPlani> passo
// <OriginePlani>-2.72 3.04</OriginePlani>
QStringList origineValues = originePlaniNodes.at(0).toElement().text().split(" ");
if (origineValues.size() >= 2) {
origin[0] = origineValues.at(0).toFloat();
origin[1] = origineValues.at(1).toFloat();
}
QStringList resolutionValues = resolutionPlaniNodes.at(0).toElement().text().split(" ");
if (resolutionValues.size() >= 2) {
resolution[0] = resolutionValues.at(0).toFloat();
resolution[1] = resolutionValues.at(1).toFloat();
}

//resAlti e oriAlti
origin[2] = origineAltiNodes.at(0).toElement().text().toFloat();
resolution[2] = resolutionAltiNodes.at(0).toElement().text().toFloat();
// <ResolutionPlani>0.128 -0.128</ResolutionPlani> passo

return true;
//resAlti e oriAlti
origin[2] = origineAltiNodes.at(0).toElement().text().toFloat();
resolution[2] = resolutionAltiNodes.at(0).toElement().text().toFloat();

}
return true;

bool OrthoDepthmap::load(const char *depth_path, const char *mask_path){

QString qdepth_path = QString(depth_path);
if(!loadDepth(qdepth_path.toStdString().c_str())){
cerr << "Failed to load ortho depth tiff file: " << qdepth_path.toStdString() << endl;
return false;
}

QString qmask_path = QString(mask_path);
if(!loadMask(qmask_path.toStdString().c_str())){
cerr << "Failed to load ortho mask tiff file: " << qmask_path.toStdString() << endl;
return false;
}
bool OrthoDepthmap::load(const char *depth_path, const char *mask_path){

QString xmlPath = qdepth_path.left(qdepth_path.lastIndexOf('.')) + ".xml";
if (!loadXml(xmlPath.toStdString().c_str())) {
cerr << "Failed to load XML file: " << xmlPath.toStdString() << endl;
return false;
}
return true;
QString qdepth_path = QString(depth_path);
if(!loadDepth(qdepth_path.toStdString().c_str())){
cerr << "Failed to load ortho depth tiff file: " << qdepth_path.toStdString() << endl;
return false;
}

}
Eigen::Vector3f OrthoDepthmap::pixelToRealCoordinates(int pixelX, int pixelY, float pixelZ) {
QString qmask_path = QString(mask_path);
if(!loadMask(qmask_path.toStdString().c_str())){
cerr << "Failed to load ortho mask tiff file: " << qmask_path.toStdString() << endl;
return false;
}

// converto in punti 3d. origine dell'img + passoX * 160x
float realX = origin[0] + resolution[0] * pixelX;
float realY = realY = origin[1] + resolution[1] * pixelY;
float realZ = origin[2] + resolution[2] * pixelZ;
QString xmlPath = qdepth_path.left(qdepth_path.lastIndexOf('.')) + ".xml";
if (!loadXml(xmlPath.toStdString().c_str())) {
cerr << "Failed to load XML file: " << xmlPath.toStdString() << endl;
return false;
}
return true;

return Eigen::Vector3f(realX, realY, realZ);
}
}
Eigen::Vector3f OrthoDepthmap::pixelToRealCoordinates(int pixelX, int pixelY, float pixelZ) {

// converto in punti 3d. origine dell'img + passoX * 160x
float realX = origin[0] + resolution[0] * pixelX;
float realY = realY = origin[1] + resolution[1] * pixelY;
float realZ = origin[2] + resolution[2] * pixelZ;

void OrthoDepthmap::saveObj(const char *filename){
// use QFile for write the file and after QTextStream
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Cannot open file for writing:" << filename;
return;
return Eigen::Vector3f(realX, realY, realZ);
}
QTextStream out(&file);

for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
float z = elevation[x + y * width];
Eigen::Vector3f realPos = pixelToRealCoordinates(x, y, z);
//obj coordinates of a point v first string v second string etc. and then exit call in main
out << "v " << realPos.x() << " " << realPos.y() << " " << realPos.z() << "\n";
}
}
}
void OrthoDepthmap::projectToCameraDepthMap(const Camera& camera, const QString& outputPath) {

QImage depthMapImage(camera.width, camera.height, QImage::Format_RGB888);
depthMapImage.fill(qRgb(0, 0, 0));
//find the minimum and maximum for the Z coordinates
float minZ = std::numeric_limits<float>::max();
float maxZ = std::numeric_limits<float>::lowest();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
float pixelZ = elevation[x + y * width];

Eigen::Vector3f realCoordinates = pixelToRealCoordinates(x, y, pixelZ);
Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);
minZ = std::min(minZ, imageCoords[2]);
maxZ = std::max(maxZ, imageCoords[2]);

void OrthoDepthmap::saveObj(const char *filename){
// use QFile for write the file and after QTextStream
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Cannot open file for writing:" << filename;
return;
}
QTextStream out(&file);

for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
float z = elevation[x + y * width];
Eigen::Vector3f realPos = pixelToRealCoordinates(x, y, z);
//obj coordinates of a point v first string v second string etc. and then exit call in main
out << "v " << realPos.x() << " " << realPos.y() << " " << realPos.z() << "\n";
}
}
}
if (minZ >= maxZ) {
qWarning("MinZ and MaxZ invalid. Skip depth map generation.");
return;
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if(mask[x+ y *width]==0.0f)
continue;
float pixelZ = elevation[x + y * width];
void OrthoDepthmap::projectToCameraDepthMap(const Camera& camera, const QString& outputPath) {

Eigen::Vector3f realCoordinates = pixelToRealCoordinates(x, y, pixelZ);
Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);
int pixelValue = (int)round(((imageCoords[2] - minZ) / (maxZ - minZ)) * 255);
pixelValue = std::min(std::max(pixelValue, 0), 255);
QImage depthMapImage(camera.width, camera.height, QImage::Format_RGB888);
depthMapImage.fill(qRgb(0, 0, 0));
//find the minimum and maximum for the Z coordinates
float minZ = std::numeric_limits<float>::max();
float maxZ = std::numeric_limits<float>::lowest();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
float pixelZ = elevation[x + y * width];

int imageX = (int)round(imageCoords[0]);
int imageY = (int)round(imageCoords[1]);
Eigen::Vector3f realCoordinates = pixelToRealCoordinates(x, y, pixelZ);
Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);
minZ = std::min(minZ, imageCoords[2]);
maxZ = std::max(maxZ, imageCoords[2]);

if (imageX >= 0 && imageX < camera.width && imageY >= 0 && imageY < camera.height) {
depthMapImage.setPixel(imageX, imageY, qRgb(pixelValue, pixelValue, pixelValue));
//cout << "Pixel projected (" << x << ", " << y << ") -> (" << imageX << ", " << imageY << "), Z = "
// << pixelZ << ", pixelValue = " << pixelValue << endl;
}
}
if (minZ >= maxZ) {
qWarning("MinZ and MaxZ invalid. Skip depth map generation.");
return;
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if(mask[x+ y *width]==0.0f)
continue;
float pixelZ = elevation[x + y * width];

Eigen::Vector3f realCoordinates = pixelToRealCoordinates(x, y, pixelZ);
Eigen::Vector3f imageCoords = camera.projectionToImage(realCoordinates);
int pixelValue = (int)round(((imageCoords[2] - minZ) / (maxZ - minZ)) * 255);
pixelValue = std::min(std::max(pixelValue, 0), 255);

int imageX = (int)round(imageCoords[0]);
int imageY = (int)round(imageCoords[1]);

if (imageX >= 0 && imageX < camera.width && imageY >= 0 && imageY < camera.height) {
depthMapImage.setPixel(imageX, imageY, qRgb(pixelValue, pixelValue, pixelValue));
//cout << "Pixel projected (" << x << ", " << y << ") -> (" << imageX << ", " << imageY << "), Z = "
// << pixelZ << ", pixelValue = " << pixelValue << endl;
}
}
}
depthMapImage.save(outputPath, "png");
}
void OrthoDepthmap::resizeNormals (int factorPowerOfTwo, int step) {
int factor = 1 << factorPowerOfTwo;
Depthmap::resizeNormals(factorPowerOfTwo, step);
resolution *= factor;
origin /= factor;
}
depthMapImage.save(outputPath, "png");
}
void OrthoDepthmap::resizeNormals (int factorPowerOfTwo, int step) {
int factor = 1 << factorPowerOfTwo;
Depthmap::resizeNormals(factorPowerOfTwo, step);
resolution *= factor;
origin /= factor;
}



/* ---------------------------------------------------------------------------------------------------------------------------*/
/* ---------------------------------------------------------------------------------------------------------------------------*/


2 changes: 1 addition & 1 deletion depthmap/depthmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Depthmap {
bool loadDepth(const char *depth_path);
bool loadMask(const char *mask_path);
bool loadNormals(const char *normals_path);
bool loadPly(const char *textPath, const char *outputPath);
bool loadText(const char *textPath, const char *outputPath);
void saveDepth(const char *depth_path);
void saveMask(const char *depth_path);
void saveNormals(const char *normals_path);
Expand Down
4 changes: 2 additions & 2 deletions depthmap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main(int argc, char *argv[]) {
return 1;
}*/
//input
#define MACOS 1
//#define MACOS 1
#ifdef MACOS
QString base = "/Users/erika/Desktop/";
#else
Expand All @@ -24,7 +24,7 @@ int main(int argc, char *argv[]) {
QString depthmapPath = base + "testcenterRel_copia/photogrammetry/Malt/Z_Num7_DeZoom4_STD-MALT.tif";
QString orientationXmlPath = base + "testcenterRel_copia/photogrammetry/Ori-Relative/Orientation-L05C12.tif.xml";
QString maskPath = base + "testcenterRel_copia/photogrammetry/Malt/Masq_STD-MALT_DeZoom4.tif";
QString plyFile = base +"testcenterRel_copia/photogrammetry/AperiCloud_Relative__mini.ply";
QString plyFile = base +"testcenterRel_copia/photogrammetry/AperiCloud_Relative_mini.ply";
Depthmap depth;

//output
Expand Down

0 comments on commit 619ec33

Please sign in to comment.