add SuperBuilder

pull/249/head
edgarriba 2015-11-11 19:39:50 +01:00
rodzic fd338c40c3
commit 0631a68173
42 zmienionych plików z 2 dodań i 6624 usunięć

Wyświetl plik

@ -17,7 +17,7 @@ find_package(OpenCV HINTS "${OPENCV_DIR}" REQUIRED)
# Only link with required opencv modules.
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui)
# Add the PCL, Eigen and OpenCV include dirs.
# Add the PCL, Eigen and OpenCV include dirs.
# Necessary since the PCL_INCLUDE_DIR variable set by find_package is broken.)
include_directories(${PCL_ROOT}/include/pcl-${PCL_VERSION_MAJOR}.${PCL_VERSION_MINOR})
include_directories(${EIGEN_ROOT})
@ -30,5 +30,4 @@ aux_source_directory("./src" SRC_LIST)
# Add exectuteable
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(odm_orthophoto ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES} ${PCL_SURFACE_LIBRARIES} ${OpenCV_LIBS})
target_link_libraries(odm_orthophoto ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES} ${PCL_SURFACE_LIBRARIES} ${OpenCV_LIBS})

Wyświetl plik

@ -1,31 +0,0 @@
#include "Logger.hpp"
Logger::Logger(bool isPrintingInCout) : isPrintingInCout_(isPrintingInCout)
{
}
Logger::~Logger()
{
}
void Logger::printToFile(std::string filePath)
{
std::ofstream file(filePath.c_str(), std::ios::binary);
file << logStream_.str();
file.close();
}
bool Logger::isPrintingInCout() const
{
return isPrintingInCout_;
}
void Logger::setIsPrintingInCout(bool isPrintingInCout)
{
isPrintingInCout_ = isPrintingInCout;
}

Wyświetl plik

@ -1,68 +0,0 @@
#pragma once
// STL
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
/*!
* \brief The Logger class is used to store program messages in a log file.
* \details By using the << operator while printInCout is set, the class writes both to
* cout and to file, if the flag is not set, output is written to file only.
*/
class Logger
{
public:
/*!
* \brief Logger Contains functionality for printing and displaying log information.
* \param printInCout Flag toggling if operator << also writes to cout.
*/
Logger(bool isPrintingInCout = true);
/*!
* \brief Destructor.
*/
~Logger();
/*!
* \brief print Prints the contents of the log to file.
* \param filePath Path specifying where to write the log.
*/
void printToFile(std::string filePath);
/*!
* \brief isPrintingInCout Check if console printing flag is set.
* \return Console printing flag.
*/
bool isPrintingInCout() const;
/*!
* \brief setIsPrintingInCout Set console printing flag.
* \param isPrintingInCout Value, if true, messages added to the log are also printed in cout.
*/
void setIsPrintingInCout(bool isPrintingInCout);
/*!
* Operator for printing messages to log and in the standard output stream if desired.
*/
template<class T>
friend Logger& operator<< (Logger &log, T t)
{
// If console printing is enabled.
if (log.isPrintingInCout_)
{
std::cout << t;
std::cout.flush();
}
// Write to log.
log.logStream_ << t;
return log;
}
private:
bool isPrintingInCout_; /*!< If flag is set, log is printed in cout and written to the log. */
std::stringstream logStream_; /*!< Stream for storing the log. */
};

Wyświetl plik

@ -1,383 +0,0 @@
// STL
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <stdio.h>
#include <ctype.h>
#include <sstream>
#include <math.h>
// Proj4
#include <proj_api.h>
// This
#include "UtmExtractor.hpp"
UtmExtractor::UtmExtractor() : log_(false)
{
logFile_ = "odm_extracting_utm_log.txt";
}
UtmExtractor::~UtmExtractor()
{
}
int UtmExtractor::run(int argc, char **argv)
{
if (argc <= 1)
{
printHelp();
return EXIT_SUCCESS;
}
try
{
parseArguments(argc, argv);
extractUtm();
}
catch (const UtmExtractorException& e)
{
log_.setIsPrintingInCout(true);
log_ << e.what() << "\n";
log_.printToFile(logFile_);
log_ << "For more detailed information, see log file." << "\n";
return EXIT_FAILURE;
}
catch (const std::exception& e)
{
log_.setIsPrintingInCout(true);
log_ << "Error in OdmExtractUtm:\n";
log_ << e.what() << "\n";
log_.printToFile(logFile_);
log_ << "For more detailed information, see log file." << "\n";
return EXIT_FAILURE;
}
catch (...)
{
log_.setIsPrintingInCout(true);
log_ << "Unknown error in OdmExtractUtm:\n";
log_.printToFile(logFile_);
log_ << "For more detailed information, see log file." << "\n";
return EXIT_FAILURE;
}
log_.printToFile(logFile_);
return EXIT_SUCCESS;
}
void UtmExtractor::parseArguments(int argc, char **argv)
{
for(int argIndex = 1; argIndex < argc; ++argIndex)
{
// The argument to be parsed
std::string argument = std::string(argv[argIndex]);
if (argument == "-help")
{
printHelp();
}
else if (argument == "-verbose")
{
log_.setIsPrintingInCout(true);
}
else if (argument == "-imageListFile")
{
++argIndex;
if (argIndex >= argc)
{
throw UtmExtractorException("Missing argument for '" + argument + "'.");
}
imageListFileName_ = std::string(argv[argIndex]);
std::ifstream testFile(imageListFileName_.c_str(), std::ios_base::binary);
if (!testFile.is_open())
{
throw UtmExtractorException("Argument '" + argument + "' has a bad value (file not accessible).");
}
log_ << "imageListFile was set to: " << imageListFileName_ << "\n";
}
else if (argument == "-imagesPath")
{
++argIndex;
if (argIndex >= argc)
{
throw UtmExtractorException("Missing argument for '" + argument + "'.");
}
std::stringstream ss(argv[argIndex]);
ss >> imagesPath_;
if (ss.bad())
{
throw UtmExtractorException("Argument '" + argument + "' has a bad value. (wrong type)");
}
log_ << "imagesPath was set to: " << imagesPath_ << "\n";
}
else if (argument == "-outputCoordFile")
{
++argIndex;
if (argIndex >= argc)
{
throw UtmExtractorException("Missing argument for '" + argument + "'.");
}
std::stringstream ss(argv[argIndex]);
ss >> outputCoordFileName_;
if (ss.bad())
{
throw UtmExtractorException("Argument '" + argument + "' has a bad value. (wrong type)");
}
log_ << "outputCoordFile was set to: " << outputCoordFileName_ << "\n";
}
else if (argument == "-logFile")
{
++argIndex;
if (argIndex >= argc)
{
throw UtmExtractorException("Missing argument for '" + argument + "'.");
}
std::stringstream ss(argv[argIndex]);
ss >> logFile_;
if (ss.bad())
{
throw UtmExtractorException("Argument '" + argument + "' has a bad value. (wrong type)");
}
log_ << "logFile_ was set to: " << logFile_ << "\n";
}
else
{
printHelp();
throw UtmExtractorException("Unrecognised argument '" + argument + "'.");
}
}
}
void UtmExtractor::extractUtm()
{
// Open file listing all used camera images
std::ifstream imageListStream(imageListFileName_.c_str());
if (!imageListStream.good()) {
throw UtmExtractorException("Failed to open " + imageListFileName_ + " for reading.");
}
// Traverse images
int utmZone = 99; // for auto-select
char hemisphere;
std::string imageFilename;
std::vector<Coord> coords;
while (getline(imageListStream, imageFilename)) {
// Run jhead on image to extract EXIF data to temporary file
std::string commandLine = "jhead -v " + imagesPath_ + "/" + imageFilename + " > extract_utm_output.txt";
system(commandLine.c_str());
// Read temporary EXIF data file
std::ifstream jheadDataStream;
jheadDataStream.open("extract_utm_output.txt");
if (!jheadDataStream.good()) {
throw UtmExtractorException("Failed to open temporary jhead data file extract_utm_output.txt");
}
// Delete temporary file
remove("extract_utm_output.txt");
// Parse jhead output
double lon, lat, alt;
if (!parsePosition(jheadDataStream, lon, lat, alt)) {
throw UtmExtractorException("Failed parsing GPS position.");
jheadDataStream.close();
}
jheadDataStream.close();
// Convert to UTM
double x, y, z;
convert(lon, lat, alt, x, y, z, utmZone, hemisphere);
coords.push_back(Coord(x, y, z));
}
imageListStream.close();
// Calculate average
double dx = 0.0, dy = 0.0;
double num = static_cast<double>(coords.size());
for (std::vector<Coord>::iterator iter = coords.begin(); iter != coords.end(); ++iter) {
dx += iter->x/num;
dy += iter->y/num;
}
dx = floor(dx);
dy = floor(dy);
// Open output file
std::ofstream outputCoordStream(outputCoordFileName_.c_str());
if (!outputCoordStream.good()) {
throw UtmExtractorException("Failed to openg " + outputCoordFileName_ + " for writing.");
}
outputCoordStream.precision(10);
// Write coordinate file
outputCoordStream << "WGS84 UTM " << utmZone << hemisphere << std::endl;
outputCoordStream << dx << " " << dy << std::endl;
for (std::vector<Coord>::iterator iter = coords.begin(); iter != coords.end(); ++iter) {
outputCoordStream << (iter->x - dx) << " " << (iter->y - dy) << " " << iter->z << std::endl;
}
outputCoordStream.close();
}
bool UtmExtractor::convert(const double &lon, const double &lat, const double &alt, double &x, double &y, double &z, int &utmZone, char &hemisphere)
{
x = y = z = 0.0;
// Create WGS84 longitude/latitude coordinate system
projPJ pjLatLon = pj_init_plus("+proj=latlong +datum=WGS84");
if (!pjLatLon) {
throw UtmExtractorException("Couldn't create WGS84 coordinate system with PROJ.4.");
return false;
}
// Calculate UTM zone if it's set to magic 99
// NOTE: Special UTM cases in Norway/Svalbard not supported here
if (utmZone == 99) {
utmZone = ((static_cast<int>(floor((lon + 180.0)/6.0)) % 60) + 1);
if (lat < 0)
hemisphere = 'S';
else
hemisphere = 'N';
}
std::ostringstream ostr;
ostr << utmZone;
if (hemisphere == 'S')
ostr << " +south";
// Create UTM coordinate system
projPJ pjUtm = pj_init_plus(("+proj=utm +datum=WGS84 +zone=" + ostr.str()).c_str());
if (!pjUtm) {
throw UtmExtractorException("Couldn't create UTM coordinate system with PROJ.4.");
return false;
}
// Convert to radians
x = lon * DEG_TO_RAD;
y = lat * DEG_TO_RAD;
z = alt;
// Transform
int res = pj_transform(pjLatLon, pjUtm, 1, 1, &x, &y, &z);
if (res != 0) {
throw UtmExtractorException("Failed to transform coordinates");
return false;
}
return true;
}
bool UtmExtractor::parsePosition(std::ifstream &jheadStream, double &lon, double &lat, double &alt)
{
lon = lat = alt = 0.0;
// Parse position
std::string str;
std::string latStr, lonStr, altStr;
while (std::getline(jheadStream, str))
{
size_t index = str.find("GPS Latitude : ");
if (index != std::string::npos)
{
latStr = str.substr(index + 15);
size_t find = latStr.find_first_of("0123456789");
if(std::string::npos == find)
{
throw UtmExtractorException("Image is missing GPS Latitude data");
}
}
index = str.find("GPS Longitude: ");
if (index != std::string::npos)
{
lonStr = str.substr(index + 15);
size_t find = lonStr.find_first_of("0123456789");
if(std::string::npos == find)
{
throw UtmExtractorException("Image is missing GPS Latitude data");
}
}
index = str.find("GPSAltitude");
if (index != std::string::npos)
{
altStr = str.substr(index + 12);
size_t find = altStr.find_first_of("0123456789");
if(std::string::npos == find)
{
throw UtmExtractorException("Image is missing GPS Latitude data");
}
}
}
if (lonStr.empty() || latStr.empty()) {
throw UtmExtractorException("No valid GPS position found");
return false;
}
// Parse longitude
std::string hemisphere;
double degrees, minutes, seconds;
std::istringstream istr(lonStr);
char degChar = 'd', minChar = 'm', secChar = 's';
istr >> hemisphere >> degrees >> degChar >> minutes >> minChar >> seconds >> secChar;
lon = (hemisphere == "W" ? -1 : 1) * (degrees + minutes/60.0 + seconds/3600.0);
// Parse latitude
istr.clear();
istr.str(latStr);
istr >> hemisphere >> degrees >> degChar >> minutes >> minChar >> seconds >> secChar;
lat = (hemisphere == "S" ? -1 : 1) * (degrees + minutes/60.0 + seconds/3600.0);
if (!altStr.empty())
{
size_t index = altStr.find_last_of("=");
if (index != std::string::npos)
{
altStr = altStr.substr(index + 1);
istr.clear();
istr.str(altStr);
char dummyChar;
int nominator, denominator;
istr >> nominator;
istr >> dummyChar;
istr >> denominator;
alt = static_cast<double>(nominator)/static_cast<double>(denominator);
}
}
return true;
}
void UtmExtractor::printHelp()
{
log_.setIsPrintingInCout(true);
log_ << "Purpose:\n";
log_ << "Create a coordinate file containing the GPS positions of all cameras to be used later in the ODM toolchain for automatic georeferecing.\n";
log_ << "Usage:\n";
log_ << "The program requires paths to a image list file, a image folder path and an output textfile to store the results.\n";
log_ << "The following flags are available:\n";
log_ << "Call the program with flag \"-help\", or without parameters to print this message, or check any generated log file.\n";
log_ << "Call the program with flag \"-verbose\", to print log messages in the standard output.\n\n";
log_ << "Parameters are specified as: \"-<argument name> <argument>\", (without <>), and the following parameters are configurable:\n";
log_ << "\"-imageListFile <path>\" (mandatory)\n";
log_ << "Path to the list containing the image names used in the bundle.out file.\n";
log_ << "\"-imagesPath <path>\" (mandatory)\n";
log_ << "Path folder containing all images in the imageListFile.\n";
log_ << "\"-outputCoordFile <path>\" (mandatory)\n";
log_ << "Path output textfile.\n";
log_.setIsPrintingInCout(false);
}

Wyświetl plik

@ -1,96 +0,0 @@
#pragma once
// Logging
#include "Logger.hpp"
/*!
* \breif The Coord struct Class used in UtmExtractor to extract GPS positions from images and ODM output
*/
struct Coord
{
double x, y, z;
Coord(double ix, double iy, double iz) : x(ix), y(iy), z(iz) {}
};
class UtmExtractor
{
public:
UtmExtractor();
~UtmExtractor();
/*!
* \brief run Runs the texturing functionality using the provided input arguments.
* For a list of the accepted arguments, please see the main page documentation or
* call the program with parameter "-help".
* \param argc Application argument count.
* \param argv Argument values.
* \return 0 if successful.
*/
int run (int argc, char **argv);
private:
/*!
* \brief parseArguments Parses command line arguments.
* \param argc Application argument count.
* \param argv Argument values.
*/
void parseArguments(int argc, char **argv);
/*!
* \breif extractUtm Performs the extraction of coordinates inside the run function.
*/
void extractUtm();
/*!
* /brief Static method that converts a WGS84 longitude/latitude coordinate in decimal degrees to UTM.
*
* \param lon The longitude in decimal degrees (negative if western hemisphere).
* \param lat The latitude in decimal degrees (negative if southern hemisphere).
* \param alt The altitude in meters.
* \param x Output parameter, the easting UTM value in meters.
* \param y Output parameter, the northing UTM value in meters.
* \param utmZone Input or output parameter, the UTM zone. Set to 99 for automatic selection.
* \param hemisphere Input or output parameter, 'N' for norther hemisphere, 'S' for southern. Automatically selected if utmZone is 99.
*
* \returns True if successful (otherwise output parameters are 0)
*/
static bool convert(const double &lon, const double &lat, const double &alt, double &x, double &y, double &z, int &utmZone, char &hemisphere);
/*!
* \brief Static method that parses a GPS position from jhead data.
*
* \param jheadDataStream Jhead data stream with EXIF information.
* \param lon Output parameter, the longitude in decimal degrees.
* \param lat Output parameter, the latitude in decimal degrees.
* \param alt Output parameter, the altitude in meters.
*
* \returns True if successful (otherwise output parameters are 0)
*/
static bool parsePosition(std::ifstream &jheadStream, double &lon, double &lat, double &alt);
/*!
* \brief printHelp Prints help, explaining usage. Can be shown by calling the program with arguments: "-help".
*/
void printHelp();
std::string imageListFileName_; /**< Path to the image list. */
std::string outputCoordFileName_; /**< Path to the file to store the output textfile. */
std::string imagesPath_; /**< Path to the folder with all images in the image list. */
Logger log_; /**< Logging object. */
std::string logFile_; /**< Path to store the log file. */
};
class UtmExtractorException : public std::exception
{
public:
UtmExtractorException() : message("Error in OdmExtractUtm") {}
UtmExtractorException(std::string msgInit) : message("Error in OdmExtractUtm:\n" + msgInit) {}
~UtmExtractorException() throw() {}
virtual const char* what() const throw() {return message.c_str(); }
private:
std::string message; /**< The error message. */
};

Wyświetl plik

@ -1,9 +0,0 @@
#include "UtmExtractor.hpp"
int main (int argc, char **argv)
{
UtmExtractor utmExtractor;
return utmExtractor.run(argc, argv);
}

Wyświetl plik

@ -1,149 +0,0 @@
// This
#include "FindTransform.hpp"
Vec3::Vec3(double x, double y, double z) :x_(x), y_(y), z_(z)
{
}
Vec3::Vec3(const Vec3 &o) : x_(o.x_), y_(o.y_), z_(o.z_)
{
}
Vec3 Vec3::cross(Vec3 o) const
{
Vec3 res;
res.x_ = y_*o.z_ - z_*o.y_;
res.y_ = z_*o.x_ - x_*o.z_;
res.z_ = x_*o.y_ - y_*o.x_;
return res;
}
double Vec3::dot(Vec3 o) const
{
return x_*o.x_ + y_*o.y_ + z_*o.z_;
}
double Vec3::length() const
{
return sqrt(x_*x_ + y_*y_ + z_*z_);
}
Vec3 Vec3::norm() const
{
Vec3 res;
double l = length();
res.x_ = x_ / l;
res.y_ = y_ / l;
res.z_ = z_ / l;
return res;
}
Vec3 Vec3::operator*(double d) const
{
return Vec3(x_*d, y_*d, z_*d);
}
Vec3 Vec3::operator+(Vec3 o) const
{
return Vec3(x_ + o.x_, y_ + o.y_,z_ + o.z_);
}
Vec3 Vec3::operator-(Vec3 o) const
{
return Vec3(x_ - o.x_, y_ - o.y_,z_ - o.z_);
}
OnMat3::OnMat3(Vec3 r1, Vec3 r2, Vec3 r3) : r1_(r1), r2_(r2), r3_(r3)
{
c1_.x_ = r1_.x_; c2_.x_ = r1_.y_; c3_.x_ = r1_.z_;
c1_.y_ = r2_.x_; c2_.y_ = r2_.y_; c3_.y_ = r2_.z_;
c1_.z_ = r3_.x_; c2_.z_ = r3_.y_; c3_.z_ = r3_.z_;
}
OnMat3::OnMat3(const OnMat3 &o) : r1_(o.r1_), r2_(o.r2_), r3_(o.r3_)
{
c1_.x_ = r1_.x_; c2_.x_ = r1_.y_; c3_.x_ = r1_.z_;
c1_.y_ = r2_.x_; c2_.y_ = r2_.y_; c3_.y_ = r2_.z_;
c1_.z_ = r3_.x_; c2_.z_ = r3_.y_; c3_.z_ = r3_.z_;
}
double OnMat3::det() const
{
return r1_.x_*r2_.y_*r3_.z_ + r1_.y_*r2_.z_*r3_.x_ + r1_.z_*r2_.x_*r3_.y_ - r1_.z_*r2_.y_*r3_.x_ - r1_.y_*r2_.x_*r3_.z_ - r1_.x_*r2_.z_*r3_.y_;
}
OnMat3 OnMat3::transpose() const
{
return OnMat3(Vec3(r1_.x_, r2_.x_, r3_.x_), Vec3(r1_.y_, r2_.y_, r3_.y_), Vec3(r1_.z_, r2_.z_, r3_.z_));
}
OnMat3 OnMat3::operator*(OnMat3 o) const
{
return OnMat3( Vec3(r1_.dot(o.c1_), r1_.dot(o.c2_), r1_.dot(o.c3_)),
Vec3(r2_.dot(o.c1_), r2_.dot(o.c2_), r2_.dot(o.c3_)),
Vec3(r3_.dot(o.c1_), r3_.dot(o.c2_), r3_.dot(o.c3_)));
}
Vec3 OnMat3::operator*(Vec3 o)
{
return Vec3(r1_.dot(o), r2_.dot(o), r3_.dot(o));
}
Mat4::Mat4()
{
r1c1_ = 1.0; r1c2_ = 0.0; r1c3_ = 0.0; r1c4_ = 0.0;
r2c1_ = 0.0; r2c2_ = 1.0; r2c3_ = 0.0; r2c4_ = 0.0;
r3c1_ = 0.0; r3c2_ = 0.0; r3c3_ = 1.0; r3c4_ = 0.0;
r4c1_ = 0.0; r4c2_ = 0.0; r4c3_ = 0.0; r4c4_ = 1.0;
}
Mat4::Mat4(OnMat3 rotation, Vec3 translation, double scaling)
{
r1c1_ = scaling * rotation.r1_.x_; r1c2_ = scaling * rotation.r1_.y_; r1c3_ = scaling * rotation.r1_.z_; r1c4_ = translation.x_;
r2c1_ = scaling * rotation.r2_.x_; r2c2_ = scaling * rotation.r2_.y_; r2c3_ = scaling * rotation.r2_.z_; r2c4_ = translation.y_;
r3c1_ = scaling * rotation.r3_.x_; r3c2_ = scaling * rotation.r3_.y_; r3c3_ = scaling * rotation.r3_.z_; r3c4_ = translation.z_;
r4c1_ = 0.0; r4c2_ = 0.0; r4c3_ = 0.0; r4c4_ = 1.0;
}
Vec3 Mat4::operator*(Vec3 o)
{
return Vec3(
r1c1_ * o.x_ + r1c2_* o.y_ + r1c3_* o.z_ + r1c4_,
r2c1_ * o.x_ + r2c2_* o.y_ + r2c3_* o.z_ + r2c4_,
r3c1_ * o.x_ + r3c2_* o.y_ + r3c3_* o.z_ + r3c4_
);
}
void FindTransform::findTransform(Vec3 fromA, Vec3 fromB, Vec3 fromC, Vec3 toA, Vec3 toB, Vec3 toC)
{
Vec3 a1 = toA;
Vec3 b1 = toB;
Vec3 c1 = toC;
Vec3 a2 = fromA;
Vec3 b2 = fromB;
Vec3 c2 = fromC;
Vec3 y1 = (a1 - c1).cross(b1 - c1).norm();
Vec3 z1 = (a1 - c1).norm();
Vec3 x1 = y1.cross(z1);
Vec3 y2 = (a2 - c2).cross(b2 - c2).norm();
Vec3 z2 = (a2 - c2).norm();
Vec3 x2 = y2.cross(z2);
OnMat3 mat1 = OnMat3(x1, y1, z1).transpose();
OnMat3 mat2 = OnMat3(x2, y2, z2).transpose();
OnMat3 rotation = mat1 * mat2.transpose();
Vec3 translation = c1 - c2;
double scale = (a1 - c1).length() / (a2 - c2).length();
translation = rotation * c2 * (-scale) + c1;
Mat4 transformation(rotation, translation, scale);
transform_ = transformation;
}
double FindTransform::error(Vec3 fromA, Vec3 toA)
{
return (transform_*fromA - toA).length();
}

Wyświetl plik

@ -1,165 +0,0 @@
// C++
#include <math.h>
#include <string>
#include <iomanip>
#include <sstream>
#include <iostream>
/*!
* \brief Handles basic 3d vector math.
**/
struct Vec3
{
Vec3(double x = 0.0, double y = 0.0, double z = 0.0);
Vec3(const Vec3 &o);
double x_,y_,z_; /**< The x, y and z values of the vector. **/
/*!
* \brief cross The cross product between two vectors.
**/
Vec3 cross(Vec3 o) const;
/*!
* \brief dot The scalar product between two vectors.
**/
double dot(Vec3 o) const;
/*!
* \brief length The length of the vector.
**/
double length() const;
/*!
* \brief norm Returns a normalized version of this vector.
**/
Vec3 norm() const;
/*!
* \brief Scales this vector.
**/
Vec3 operator*(double d) const;
/*!
* \brief Addition between two vectors.
**/
Vec3 operator+(Vec3 o) const;
/*!
* \brief Subtraction between two vectors.
**/
Vec3 operator-(Vec3 o) const;
friend std::ostream & operator<<(std::ostream &os, Vec3 v)
{
return os << "[" << std::setprecision(8) << v.x_ << ", " << std::setprecision(4) << v.y_ << ", " << v.z_ << "]";
}
};
/*!
* \brief Describes a 3d orthonormal matrix.
**/
class OnMat3
{
public:
OnMat3(Vec3 r1, Vec3 r2, Vec3 r3);
OnMat3(const OnMat3 &o);
Vec3 r1_; /**< The first row of the matrix. **/
Vec3 r2_; /**< The second row of the matrix. **/
Vec3 r3_; /**< The third row of the matrix. **/
Vec3 c1_; /**< The first column of the matrix. **/
Vec3 c2_; /**< The second column of the matrix. **/
Vec3 c3_; /**< The third column of the matrix. **/
/*!
* \brief The determinant of the matrix.
**/
double det() const;
/*!
* \brief The transpose of the OnMat3 (equal to inverse).
**/
OnMat3 transpose() const;
/*!
* \brief Matrix multiplication between two ON matrices.
**/
OnMat3 operator*(OnMat3 o) const;
/*!
* \brief Right side multiplication with a 3d vector.
**/
Vec3 operator*(Vec3 o);
friend std::ostream & operator<<(std::ostream &os, OnMat3 m)
{
return os << "[" << std::endl << m.r1_ << std::endl << m.r2_ << std::endl << m.r3_ << std::endl << "]" << std::endl;
}
};
/*!
* \brief Describes an affine transformation.
**/
class Mat4
{
public:
Mat4();
Mat4(OnMat3 rotation, Vec3 translation, double scaling);
/*!
* \brief Right side multiplication with a 3d vector.
**/
Vec3 operator*(Vec3 o);
double r1c1_; /**< Matrix element 0 0 **/
double r1c2_; /**< Matrix element 0 1 **/
double r1c3_; /**< Matrix element 0 2 **/
double r1c4_; /**< Matrix element 0 3 **/
double r2c1_; /**< Matrix element 1 0 **/
double r2c2_; /**< Matrix element 1 1 **/
double r2c3_; /**< Matrix element 1 2 **/
double r2c4_; /**< Matrix element 1 3 **/
double r3c1_; /**< Matrix element 2 0 **/
double r3c2_; /**< Matrix element 2 1 **/
double r3c3_; /**< Matrix element 2 2 **/
double r3c4_; /**< Matrix element 2 3 **/
double r4c1_; /**< Matrix element 3 0 **/
double r4c2_; /**< Matrix element 3 1 **/
double r4c3_; /**< Matrix element 3 2 **/
double r4c4_; /**< Matrix element 3 3 **/
friend std::ostream & operator<<(std::ostream &os, Mat4 m)
{
std::stringstream ss;
ss.precision(8);
ss.setf(std::ios::fixed, std::ios::floatfield);
ss << "[ " << m.r1c1_ << ",\t" << m.r1c2_ << ",\t" << m.r1c3_ << ",\t" << m.r1c4_ << " ]" << std::endl <<
"[ " << m.r2c1_ << ",\t" << m.r2c2_ << ",\t" << m.r2c3_ << ",\t" << m.r2c4_ << " ]" << std::endl <<
"[ " << m.r3c1_ << ",\t" << m.r3c2_ << ",\t" << m.r3c3_ << ",\t" << m.r3c4_ << " ]" << std::endl <<
"[ " << m.r4c1_ << ",\t" << m.r4c2_ << ",\t" << m.r4c3_ << ",\t" << m.r4c4_ << " ]";
return os << ss.str();
}
};
class FindTransform
{
public:
/*!
* \brief findTransform Generates an affine transform from the three 'from' vector to the three 'to' vectors.
* The transform is such that transform * fromA = toA,
* transform * fromB = toB,
* transform * fromC = toC,
**/
void findTransform(Vec3 fromA, Vec3 fromB, Vec3 fromC, Vec3 toA, Vec3 toB, Vec3 toC);
/*!
* \brief error Returns the distance beteween the 'from' and 'to' vectors, after the transform has been applied.
**/
double error(Vec3 fromA, Vec3 toA);
Mat4 transform_; /**< The affine transform. **/
};

Plik diff jest za duży Load Diff

Wyświetl plik

@ -1,249 +0,0 @@
#pragma once
// C++
#include <string>
#include <sstream>
#include <fstream>
// PCL
#include <pcl/common/eigen.h>
#include <pcl/common/common.h>
// Logger
#include "Logger.hpp"
// Transformation
#include "FindTransform.hpp"
/*!
* \brief The GeorefSystem struct is used to store information about a georeference system.
*/
struct GeorefSystem
{
std::string system_; /**< The name of the system. **/
double eastingOffset_; /**< The easting offset for the georeference system. **/
double northingOffset_; /**< The northing offset for the georeference system. **/
friend std::ostream& operator<<(std::ostream &os, const GeorefSystem &geo);
};
/*!
* \brief The GeorefGCP struct used to store information about a GCP.
*/
struct GeorefGCP
{
double x_; /**< The X coordinate of the GCP **/
double y_; /**< The Y coordinate of the GCP **/
double z_; /**< The Z coordinate of the GCP **/
bool use_; /**< Bool to check if the GCP is corresponding in the local model **/
double localX_; /**< The corresponding X coordinate in the model **/
double localY_; /**< The corresponding Y coordinate in the model **/
double localZ_; /**< The corresponding Z coordinate in the model **/
size_t cameraIndex_; /**< The index to the corresponding camera for the image. **/
int pixelX_; /**< The pixels x-position for the GCP in the corresponding image **/
int pixelY_; /**< The pixels y-position for the GCP in the corresponding image **/
std::string image_; /**< The corresponding image for the GCP **/
GeorefGCP();
~GeorefGCP();
void extractGCP(std::istringstream &gcpStream);
/*!
* \brief getPos Get the local position of the GCP.
*/
Vec3 getPos();
/*!
* \brief getReferencedPos Get the georeferenced position of the GCP.
*/
Vec3 getReferencedPos();
};
/*!
* \brief The GeorefCamera struct is used to store information about a camera.
*/
struct GeorefCamera
{
GeorefCamera();
GeorefCamera(const GeorefCamera &other);
~GeorefCamera();
/*!
* \brief extractCamera Extracts a camera's intrinsic and extrinsic parameters from a stream.
*/
void extractCamera(std::ifstream &bundleStream);
/*!
* \brief extractCameraGeoref Extracts a camera's world position from a stream.
*/
void extractCameraGeoref(std::istringstream &coordStream);
/*!
* \brief getPos Get the local position of the camera.
*/
Vec3 getPos();
/*!
* \brief getReferencedPos Get the georeferenced position of the camera.
*/
Vec3 getReferencedPos();
double focalLength_; /**< The focal length of the camera. */
double k1_; /**< The k1 lens distortion parameter. **/
double k2_; /**< The k2 lens distortion parameter. **/
double easting_; /**< The easting of the camera. **/
double northing_; /**< The northing of the camera. **/
double altitude_; /**< The altitude of the camera. **/
Eigen::Affine3f* transform_; /**< The rotation of the camera. **/
Eigen::Vector3f* position_; /**< The position of the camera. **/
Eigen::Affine3f* pose_; /**< The pose of the camera. **/
friend std::ostream& operator<<(std::ostream &os, const GeorefCamera &cam);
};
/*!
* \brief The Georef class is used to transform a mesh into a georeferenced system.
* The class reads camera positions from a bundle file.
* The class reads the georefenced camera positions from a coords file.
* The class reads a textured mesh from an OBJ-file.
* The class writes the georeferenced textured mesh to an OBJ-file.
* The class uses file read and write from pcl.
*/
class Georef
{
public:
Georef();
~Georef();
int run(int argc, char* argv[]);
private:
/*!
* \brief parseArguments Parses command line arguments.
* \param argc Application argument count.
* \param argv Argument values.
*/
void parseArguments(int argc, char* argv[]);
/*!
* \brief printHelp Prints help, explaining usage. Can be shown by calling the program with argument: "-help".
*/
void printHelp();
/*!
* \brief setDefaultOutput Setup the output file name given the input file name.
*/
void setDefaultOutput();
/*!
* \brief setDefaultPointCloudOutput Setup the output file name given the input file name.
*/
void setDefaultPointCloudOutput();
/*!
* \brief createGeoreferencedModel Makes the input file georeferenced and saves it to the output file.
*/
void createGeoreferencedModel();
/*!
* \brief readCameras Reads the camera information from the bundle file.
*/
void readCameras();
/*!
* \brief readGCP Reads the ground control points from the gcp file.
*/
void readGCPs();
/*!
* \brief calculateGCPOffset Calculates an offset weighted from the ground control points read in the readGCP function.
*/
void calculateGCPOffset();
/*!
* \brief barycentricCoordinates Returns the world position of a point inside a 2d triangle by using the triangle vertex positions.
*/
pcl::PointXYZ barycentricCoordinates(pcl::PointXY point, pcl::PointXYZ vert0, pcl::PointXYZ vert1, pcl::PointXYZ vert2, pcl::PointXY p0, pcl::PointXY p1, pcl::PointXY p2);
/*!
* \brief performGeoreferencingWithGCP Performs the georeferencing of the model with the ground control points.
*/
void performGeoreferencingWithGCP();
/*!
* \brief createGeoreferencedModelFromGCPData Makes the input file georeferenced and saves it to the output file.
*/
void createGeoreferencedModelFromGCPData();
/*!
* \brief createGeoreferencedModelFromExifData Makes the input file georeferenced and saves it to the output file.
*/
void createGeoreferencedModelFromExifData();
/*!
* \brief chooseBestGCPTriplet Chooses the best triplet of GCPs to use when making the model georeferenced.
*/
void chooseBestGCPTriplet(size_t &gcp0, size_t &gcp1, size_t &gcp2);
/*!
* \brief chooseBestCameraTriplet Chooses the best triplet of cameras to use when making the model georeferenced.
*/
void chooseBestCameraTriplet(size_t &cam0, size_t &cam1, size_t &cam2);
/*!
* \brief printGeorefSystem Prints a file containing information about the georeference system, next to the ouptut file.
**/
void printGeorefSystem();
Logger log_; /**< Logging object. */
std::string logFile_; /**< The path to the output log file. */
std::string bundleFilename_; /**< The path to the cameras bundle file. **/
std::string inputCoordFilename_; /**< The path to the cameras exif gps positions file. **/
std::string outputCoordFilename_; /**< The path to the cameras georeferenced gps positions file. **/
std::string gcpFilename_; /**< The path to the GCP file **/
std::string imagesListPath_; /**< Path to the image list. **/
std::string imagesLocation_; /**< The folder containing the images in the image list. **/
std::string inputObjFilename_; /**< The path to the input mesh obj file. **/
std::string outputObjFilename_; /**< The path to the output mesh obj file. **/
std::string inputPointCloudFilename_; /**< The path to the input point cloud file. **/
std::string outputPointCloudFilename_; /**< The path to the output point cloud file. **/
std::string georefFilename_; /**< The path to the output offset file. **/
bool georeferencePointCloud_;
bool exportCoordinateFile_;
bool exportGeorefSystem_;
bool useGCP_; /**< Check if GCP-file is present and use this to georeference the model. **/
double bundleResizedTo_; /**< The size used in the previous steps to calculate the camera focal_length. */
std::vector<GeorefCamera> cameras_; /**< A vector of all cameras. **/
std::vector<GeorefGCP> gcps_; /**< A vector of all GCPs. **/
std::vector<std::string> imageList_; /**< A vector containing the names of the corresponding cameras. **/
GeorefSystem georefSystem_; /**< Contains the georeference system. **/
};
/*!
* \brief The Georef class
*/
class GeorefException : public std::exception
{
public:
GeorefException() : message("Error in Georef") {}
GeorefException(std::string msgInit) : message("Error in Georef:\n" + msgInit) {}
~GeorefException() throw() {}
virtual const char* what() const throw() {return message.c_str(); }
private:
std::string message; /**< The error message **/
};

Wyświetl plik

@ -1,31 +0,0 @@
#include "Logger.hpp"
Logger::Logger(bool isPrintingInCout) : isPrintingInCout_(isPrintingInCout)
{
}
Logger::~Logger()
{
}
void Logger::print(std::string filePath)
{
std::ofstream file(filePath.c_str(), std::ios::binary);
file << logStream_.str();
file.close();
}
bool Logger::isPrintingInCout() const
{
return isPrintingInCout_;
}
void Logger::setIsPrintingInCout(bool isPrintingInCout)
{
isPrintingInCout_ = isPrintingInCout;
}

Wyświetl plik

@ -1,68 +0,0 @@
#pragma once
// STL
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
/*!
* \brief The Logger class is used to store program messages in a log file.
* \details By using the << operator while printInCout is set, the class writes both to
* cout and to file, if the flag is not set, output is written to file only.
*/
class Logger
{
public:
/*!
* \brief Logger Contains functionality for printing and displaying log information.
* \param printInCout Flag toggling if operator << also writes to cout.
*/
Logger(bool isPrintingInCout = true);
/*!
* \brief Destructor.
*/
~Logger();
/*!
* \brief print Prints the contents of the log to file.
* \param filePath Path specifying where to write the log.
*/
void print(std::string filePath);
/*!
* \brief isPrintingInCout Check if console printing flag is set.
* \return Console printing flag.
*/
bool isPrintingInCout() const;
/*!
* \brief setIsPrintingInCout Set console printing flag.
* \param isPrintingInCout Value, if true, messages added to the log are also printed in cout.
*/
void setIsPrintingInCout(bool isPrintingInCout);
/*!
* Operator for printing messages to log and in the standard output stream if desired.
*/
template<class T>
friend Logger& operator<< (Logger &log, T t)
{
// If console printing is enabled.
if (log.isPrintingInCout_)
{
std::cout << t;
std::cout.flush();
}
// Write to log.
log.logStream_ << t;
return log;
}
private:
bool isPrintingInCout_; /*!< If flag is set, log is printed in cout and written to the log. */
std::stringstream logStream_; /*!< Stream for storing the log. */
};

Wyświetl plik

@ -1,8 +0,0 @@
#include "Georef.hpp"
int main(int argc, char* argv[])
{
Georef ref;
return ref.run(argc, argv);
}

Wyświetl plik

@ -1,336 +0,0 @@
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2012-, Open Perception, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "modifiedPclFunctions.hpp"
int saveOBJFile(const std::string &file_name, const pcl::TextureMesh &tex_mesh, unsigned precision)
{
if (tex_mesh.cloud.data.empty ())
{
PCL_ERROR ("[pcl::io::saveOBJFile] Input point cloud has no data!\n");
return (-1);
}
// Open file
std::ofstream fs;
fs.precision (precision);
fs.open (file_name.c_str ());
// Define material file
std::string mtl_file_name = file_name.substr (0, file_name.find_last_of (".")) + ".mtl";
// Strip path for "mtllib" command
std::string mtl_file_name_nopath = mtl_file_name;
//std::cout << mtl_file_name_nopath << std::endl;
mtl_file_name_nopath.erase (0, mtl_file_name.find_last_of ('/') + 1);
/* Write 3D information */
// number of points
int nr_points = tex_mesh.cloud.width * tex_mesh.cloud.height;
int point_size = tex_mesh.cloud.data.size () / nr_points;
// mesh size
int nr_meshes = tex_mesh.tex_polygons.size ();
// number of faces for header
int nr_faces = 0;
for (int m = 0; m < nr_meshes; ++m)
nr_faces += tex_mesh.tex_polygons[m].size ();
// Write the header information
fs << "####" << std::endl;
fs << "# OBJ dataFile simple version. File name: " << file_name << std::endl;
fs << "# Vertices: " << nr_points << std::endl;
fs << "# Faces: " <<nr_faces << std::endl;
fs << "# Material information:" << std::endl;
fs << "mtllib " << mtl_file_name_nopath << std::endl;
fs << "####" << std::endl;
// Write vertex coordinates
fs << "# Vertices" << std::endl;
for (int i = 0; i < nr_points; ++i)
{
int xyz = 0;
// "v" just be written one
bool v_written = false;
for (size_t d = 0; d < tex_mesh.cloud.fields.size (); ++d)
{
int count = tex_mesh.cloud.fields[d].count;
if (count == 0)
count = 1; // we simply cannot tolerate 0 counts (coming from older converter code)
int c = 0;
// adding vertex
if ((tex_mesh.cloud.fields[d].datatype == pcl::PCLPointField::FLOAT32) /*sensor_msgs::PointField::FLOAT32)*/ && (
tex_mesh.cloud.fields[d].name == "x" ||
tex_mesh.cloud.fields[d].name == "y" ||
tex_mesh.cloud.fields[d].name == "z"))
{
if (!v_written)
{
// write vertices beginning with v
fs << "v ";
v_written = true;
}
float value;
memcpy (&value, &tex_mesh.cloud.data[i * point_size + tex_mesh.cloud.fields[d].offset + c * sizeof (float)], sizeof (float));
fs << value;
if (++xyz == 3)
break;
fs << " ";
}
}
if (xyz != 3)
{
PCL_ERROR ("[pcl::io::saveOBJFile] Input point cloud has no XYZ data!\n");
return (-2);
}
fs << std::endl;
}
fs << "# "<< nr_points <<" vertices" << std::endl;
// // Write vertex normals
// for (int i = 0; i < nr_points; ++i)
// {
// int xyz = 0;
// // "vn" just be written one
// bool v_written = false;
// for (size_t d = 0; d < tex_mesh.cloud.fields.size (); ++d)
// {
// int count = tex_mesh.cloud.fields[d].count;
// if (count == 0)
// count = 1; // we simply cannot tolerate 0 counts (coming from older converter code)
// int c = 0;
// // adding vertex
// if ((tex_mesh.cloud.fields[d].datatype == pcl::PCLPointField::FLOAT32) && (
// tex_mesh.cloud.fields[d].name == "normal_x" ||
// tex_mesh.cloud.fields[d].name == "normal_y" ||
// tex_mesh.cloud.fields[d].name == "normal_z"))
// {
// if (!v_written)
// {
// // write vertices beginning with vn
// fs << "vn ";
// v_written = true;
// }
// float value;
// memcpy (&value, &tex_mesh.cloud.data[i * point_size + tex_mesh.cloud.fields[d].offset + c * sizeof (float)], sizeof (float));
// fs << value;
// if (++xyz == 3)
// break;
// fs << " ";
// }
// }
// if (xyz != 3)
// {
// //PCL_ERROR ("[pcl::io::saveOBJFile] Input point cloud has no normals!\n");
// //return (-2);
// }
// fs << std::endl;
// }
// Write vertex texture with "vt" (adding latter)
for (int m = 0; m < nr_meshes; ++m)
{
if(tex_mesh.tex_coordinates.size() == 0)
continue;
//PCL_INFO ("%d vertex textures in submesh %d\n", tex_mesh.tex_coordinates[m].size (), m);
fs << "# " << tex_mesh.tex_coordinates[m].size() << " vertex textures in submesh " << m << std::endl;
for (size_t i = 0; i < tex_mesh.tex_coordinates[m].size (); ++i)
{
fs << "vt ";
fs << tex_mesh.tex_coordinates[m][i][0] << " " << tex_mesh.tex_coordinates[m][i][1] << std::endl;
}
}
int f_idx = 0;
// int idx_vt =0;
//PCL_INFO ("Writting faces...\n");
for (int m = 0; m < nr_meshes; ++m)
{
if (m > 0)
f_idx += tex_mesh.tex_polygons[m-1].size ();
if(tex_mesh.tex_materials.size() !=0)
{
fs << "# The material will be used for mesh " << m << std::endl;
//TODO pbl here with multi texture and unseen faces
fs << "usemtl " << tex_mesh.tex_materials[m].tex_name << std::endl;
fs << "# Faces" << std::endl;
}
for (size_t i = 0; i < tex_mesh.tex_polygons[m].size(); ++i)
{
// Write faces with "f"
fs << "f";
size_t j = 0;
// There's one UV per vertex per face, i.e., the same vertex can have
// different UV depending on the face.
for (j = 0; j < tex_mesh.tex_polygons[m][i].vertices.size (); ++j)
{
unsigned int idx = tex_mesh.tex_polygons[m][i].vertices[j] + 1;
fs << " " << idx
<< "/" << 3*(i+f_idx) +j+1;
//<< "/" << idx; // vertex index in obj file format starting with 1
}
fs << std::endl;
}
//PCL_INFO ("%d faces in mesh %d \n", tex_mesh.tex_polygons[m].size () , m);
fs << "# "<< tex_mesh.tex_polygons[m].size() << " faces in mesh " << m << std::endl;
}
fs << "# End of File";
// Close obj file
//PCL_INFO ("Closing obj file\n");
fs.close ();
/* Write material defination for OBJ file*/
// Open file
//PCL_INFO ("Writing material files\n");
//dont do it if no material to write
if(tex_mesh.tex_materials.size() ==0)
return (0);
std::ofstream m_fs;
m_fs.precision (precision);
m_fs.open (mtl_file_name.c_str ());
//std::cout << "MTL file is located at_ " << mtl_file_name << std::endl;
// default
m_fs << "#" << std::endl;
m_fs << "# Wavefront material file" << std::endl;
m_fs << "#" << std::endl;
for(int m = 0; m < nr_meshes; ++m)
{
m_fs << "newmtl " << tex_mesh.tex_materials[m].tex_name << std::endl;
m_fs << "Ka "<< tex_mesh.tex_materials[m].tex_Ka.r << " " << tex_mesh.tex_materials[m].tex_Ka.g << " " << tex_mesh.tex_materials[m].tex_Ka.b << std::endl; // defines the ambient color of the material to be (r,g,b).
m_fs << "Kd "<< tex_mesh.tex_materials[m].tex_Kd.r << " " << tex_mesh.tex_materials[m].tex_Kd.g << " " << tex_mesh.tex_materials[m].tex_Kd.b << std::endl; // defines the diffuse color of the material to be (r,g,b).
m_fs << "Ks "<< tex_mesh.tex_materials[m].tex_Ks.r << " " << tex_mesh.tex_materials[m].tex_Ks.g << " " << tex_mesh.tex_materials[m].tex_Ks.b << std::endl; // defines the specular color of the material to be (r,g,b). This color shows up in highlights.
m_fs << "d " << tex_mesh.tex_materials[m].tex_d << std::endl; // defines the transparency of the material to be alpha.
m_fs << "Ns "<< tex_mesh.tex_materials[m].tex_Ns << std::endl; // defines the shininess of the material to be s.
m_fs << "illum "<< tex_mesh.tex_materials[m].tex_illum << std::endl; // denotes the illumination model used by the material.
// illum = 1 indicates a flat material with no specular highlights, so the value of Ks is not used.
// illum = 2 denotes the presence of specular highlights, and so a specification for Ks is required.
m_fs << "map_Kd " << tex_mesh.tex_materials[m].tex_file << std::endl;
m_fs << "###" << std::endl;
}
m_fs.close ();
return (0);
}
bool getPixelCoordinates(const pcl::PointXYZ &pt, const pcl::TextureMapping<pcl::PointXYZ>::Camera &cam, pcl::PointXY &UV_coordinates)
{
if (pt.z > 0)
{
// compute image center and dimension
double sizeX = cam.width;
double sizeY = cam.height;
double cx, cy;
if (cam.center_w > 0)
cx = cam.center_w;
else
cx = sizeX / 2.0;
if (cam.center_h > 0)
cy = cam.center_h;
else
cy = sizeY / 2.0;
double focal_x, focal_y;
if (cam.focal_length_w > 0)
focal_x = cam.focal_length_w;
else
focal_x = cam.focal_length;
if (cam.focal_length_h > 0)
focal_y = cam.focal_length_h;
else
focal_y = cam.focal_length;
// project point on camera's image plane
UV_coordinates.x = static_cast<float> ((focal_x * (pt.x / pt.z) + cx)); //horizontal
UV_coordinates.y = static_cast<float> ((focal_y * (pt.y / pt.z) + cy)); //vertical
// point is visible!
if (UV_coordinates.x >= 1.0 && UV_coordinates.x <= (sizeX - 1.0) && UV_coordinates.y >= 1.0 && UV_coordinates.y <= (sizeY - 1.0))
{
return (true); // point was visible by the camera
}
}
// point is NOT visible by the camera
UV_coordinates.x = -1.0f;
UV_coordinates.y = -1.0f;
return (false); // point was not visible by the camera
}
bool isFaceProjected (const pcl::TextureMapping<pcl::PointXYZ>::Camera &camera, const pcl::PointXYZ &p1, const pcl::PointXYZ &p2, const pcl::PointXYZ &p3, pcl::PointXY &proj1, pcl::PointXY &proj2, pcl::PointXY &proj3)
{
return (getPixelCoordinates(p1, camera, proj1) && getPixelCoordinates(p2, camera, proj2) && getPixelCoordinates(p3, camera, proj3));
}
void getTriangleCircumscribedCircleCentroid( const pcl::PointXY &p1, const pcl::PointXY &p2, const pcl::PointXY &p3, pcl::PointXY &circumcenter, double &radius)
{
// compute centroid's coordinates (translate back to original coordinates)
circumcenter.x = static_cast<float> (p1.x + p2.x + p3.x ) / 3;
circumcenter.y = static_cast<float> (p1.y + p2.y + p3.y ) / 3;
double r1 = (circumcenter.x - p1.x) * (circumcenter.x - p1.x) + (circumcenter.y - p1.y) * (circumcenter.y - p1.y) ;
double r2 = (circumcenter.x - p2.x) * (circumcenter.x - p2.x) + (circumcenter.y - p2.y) * (circumcenter.y - p2.y) ;
double r3 = (circumcenter.x - p3.x) * (circumcenter.x - p3.x) + (circumcenter.y - p3.y) * (circumcenter.y - p3.y) ;
// radius
radius = std::sqrt( std::max( r1, std::max( r2, r3) )) ;
}
bool checkPointInsideTriangle(const pcl::PointXY &p1, const pcl::PointXY &p2, const pcl::PointXY &p3, const pcl::PointXY &pt)
{
// Compute vectors
Eigen::Vector2d v0, v1, v2;
v0(0) = p3.x - p1.x; v0(1) = p3.y - p1.y; // v0= C - A
v1(0) = p2.x - p1.x; v1(1) = p2.y - p1.y; // v1= B - A
v2(0) = pt.x - p1.x; v2(1) = pt.y - p1.y; // v2= P - A
// Compute dot products
double dot00 = v0.dot(v0); // dot00 = dot(v0, v0)
double dot01 = v0.dot(v1); // dot01 = dot(v0, v1)
double dot02 = v0.dot(v2); // dot02 = dot(v0, v2)
double dot11 = v1.dot(v1); // dot11 = dot(v1, v1)
double dot12 = v1.dot(v2); // dot12 = dot(v1, v2)
// Compute barycentric coordinates
double invDenom = 1.0 / (dot00*dot11 - dot01*dot01);
double u = (dot11*dot02 - dot01*dot12) * invDenom;
double v = (dot00*dot12 - dot01*dot02) * invDenom;
// Check if point is in triangle
return ((u >= 0) && (v >= 0) && (u + v < 1));
}

Wyświetl plik

@ -1,20 +0,0 @@
#pragma once
// STL
#include <iostream>
#include <fstream>
// PCL
#include <pcl/point_types.h>
#include <pcl/surface/texture_mapping.h>
#include <pcl/io/ply_io.h>
int saveOBJFile(const std::string &file_name, const pcl::TextureMesh &tex_mesh, unsigned precision);
bool getPixelCoordinates(const pcl::PointXYZ &pt, const pcl::TextureMapping<pcl::PointXYZ>::Camera &cam, pcl::PointXY &UV_coordinates);
bool isFaceProjected (const pcl::TextureMapping<pcl::PointXYZ>::Camera &camera, const pcl::PointXYZ &p1, const pcl::PointXYZ &p2, const pcl::PointXYZ &p3, pcl::PointXY &proj1, pcl::PointXY &proj2, pcl::PointXY &proj3);
void getTriangleCircumscribedCircleCentroid(const pcl::PointXY &p1, const pcl::PointXY &p2, const pcl::PointXY &p3, pcl::PointXY &circumcenter, double &radius);
bool checkPointInsideTriangle(const pcl::PointXY &p1, const pcl::PointXY &p2, const pcl::PointXY &p3, const pcl::PointXY &pt);

Wyświetl plik

@ -1,31 +0,0 @@
#include "Logger.hpp"
Logger::Logger(bool isPrintingInCout) : isPrintingInCout_(isPrintingInCout)
{
}
Logger::~Logger()
{
}
void Logger::printToFile(std::string filePath)
{
std::ofstream file(filePath.c_str(), std::ios::binary);
file << logStream_.str();
file.close();
}
bool Logger::isPrintingInCout() const
{
return isPrintingInCout_;
}
void Logger::setIsPrintingInCout(bool isPrintingInCout)
{
isPrintingInCout_ = isPrintingInCout;
}

Wyświetl plik

@ -1,68 +0,0 @@
#pragma once
// STL
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
/*!
* \brief The Logger class is used to store program messages in a log file.
* \details By using the << operator while printInCout is set, the class writes both to
* cout and to file, if the flag is not set, output is written to file only.
*/
class Logger
{
public:
/*!
* \brief Logger Contains functionality for printing and displaying log information.
* \param printInCout Flag toggling if operator << also writes to cout.
*/
Logger(bool isPrintingInCout = true);
/*!
* \brief Destructor.
*/
~Logger();
/*!
* \brief print Prints the contents of the log to file.
* \param filePath Path specifying where to write the log.
*/
void printToFile(std::string filePath);
/*!
* \brief isPrintingInCout Check if console printing flag is set.
* \return Console printing flag.
*/
bool isPrintingInCout() const;
/*!
* \brief setIsPrintingInCout Set console printing flag.
* \param isPrintingInCout Value, if true, messages added to the log are also printed in cout.
*/
void setIsPrintingInCout(bool isPrintingInCout);
/*!
* Operator for printing messages to log and in the standard output stream if desired.
*/
template<class T>
friend Logger& operator<< (Logger &log, T t)
{
// If console printing is enabled.
if (log.isPrintingInCout_)
{
std::cout << t;
std::cout.flush();
}
// Write to log.
log.logStream_ << t;
return log;
}
private:
bool isPrintingInCout_; /*!< If flag is set, log is printed in cout and written to the log. */
std::stringstream logStream_; /*!< Stream for storing the log. */
};

Wyświetl plik

@ -1,361 +0,0 @@
#include "OdmMeshing.hpp"
OdmMeshing::OdmMeshing() : log_(false)
{
meshCreator_ = pcl::Poisson<pcl::PointNormal>::Ptr(new pcl::Poisson<pcl::PointNormal>());
points_ = pcl::PointCloud<pcl::PointNormal>::Ptr(new pcl::PointCloud<pcl::PointNormal>());
mesh_ = pcl::PolygonMeshPtr(new pcl::PolygonMesh);
decimatedMesh_ = pcl::PolygonMeshPtr(new pcl::PolygonMesh);
// Set default values
outputFile_ = "";
logFilePath_ = "";
maxVertexCount_ = 0;
treeDepth_ = 0;
solverDivide_ = 9.0;
samplesPerNode_ = 1.0;
decimationFactor_ = 0.0;
logFilePath_ = "odm_meshing_log.txt";
log_ << logFilePath_ << "\n";
}
OdmMeshing::~OdmMeshing()
{
}
int OdmMeshing::run(int argc, char **argv)
{
// If no arguments were passed, print help and return early.
if (argc <= 1)
{
printHelp();
return EXIT_SUCCESS;
}
try
{
parseArguments(argc, argv);
loadPoints();
createMesh();
decimateMesh();
writePlyFile();
}
catch (const OdmMeshingException& e)
{
log_.setIsPrintingInCout(true);
log_ << e.what() << "\n";
log_.printToFile(logFilePath_);
log_ << "For more detailed information, see log file." << "\n";
return EXIT_FAILURE;
}
catch (const std::exception& e)
{
log_.setIsPrintingInCout(true);
log_ << "Error in OdmMeshing:\n";
log_ << e.what() << "\n";
log_.printToFile(logFilePath_);
log_ << "For more detailed information, see log file." << "\n";
return EXIT_FAILURE;
}
catch (...)
{
log_.setIsPrintingInCout(true);
log_ << "Unknwon error in OdmMeshing:\n";
log_.printToFile(logFilePath_);
log_ << "For more detailed information, see log file." << "\n";
return EXIT_FAILURE;
}
log_.printToFile(logFilePath_);
return EXIT_SUCCESS;
}
void OdmMeshing::parseArguments(int argc, char **argv)
{
for(int argIndex = 1; argIndex < argc; ++argIndex)
{
// The argument to be parsed.
std::string argument = std::string(argv[argIndex]);
if(argument == "-help")
{
printHelp();
}
else if(argument == "-verbose")
{
log_.setIsPrintingInCout(true);
}
else if(argument == "-maxVertexCount" && argIndex < argc)
{
++argIndex;
if (argIndex >= argc)
{
throw OdmMeshingException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
std::stringstream ss(argv[argIndex]);
ss >> maxVertexCount_;
if (ss.bad())
{
throw OdmMeshingException("Argument '" + argument + "' has a bad value (wrong type).");
}
log_ << "Vertex count was manually set to: " << maxVertexCount_ << "\n";
}
else if(argument == "-octreeDepth" && argIndex < argc)
{
++argIndex;
if (argIndex >= argc)
{
throw OdmMeshingException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
std::stringstream ss(argv[argIndex]);
ss >> treeDepth_;
if (ss.bad())
{
throw OdmMeshingException("Argument '" + argument + "' has a bad value (wrong type).");
}
log_ << "Octree depth was manually set to: " << treeDepth_ << "\n";
}
else if(argument == "-solverDivide" && argIndex < argc)
{
++argIndex;
if (argIndex >= argc)
{
throw OdmMeshingException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
std::stringstream ss(argv[argIndex]);
ss >> solverDivide_;
if (ss.bad())
{
throw OdmMeshingException("Argument '" + argument + "' has a bad value (wrong type).");
}
log_ << "Numerical solver divisions was manually set to: " << treeDepth_ << "\n";
}
else if(argument == "-samplesPerNode" && argIndex < argc)
{
++argIndex;
if (argIndex >= argc)
{
throw OdmMeshingException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
std::stringstream ss(argv[argIndex]);
ss >> samplesPerNode_;
if (ss.bad())
{
throw OdmMeshingException("Argument '" + argument + "' has a bad value (wrong type).");
}
log_ << "The number of samples per octree node was manually set to: " << samplesPerNode_ << "\n";
}
else if(argument == "-inputFile" && argIndex < argc)
{
++argIndex;
if (argIndex >= argc)
{
throw OdmMeshingException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
inputFile_ = std::string(argv[argIndex]);
std::ifstream testFile(inputFile_.c_str(), std::ios::binary);
if (!testFile.is_open())
{
throw OdmMeshingException("Argument '" + argument + "' has a bad value. (file not accessible)");
}
testFile.close();
log_ << "Reading point cloud at: " << inputFile_ << "\n";
}
else if(argument == "-outputFile" && argIndex < argc)
{
++argIndex;
if (argIndex >= argc)
{
throw OdmMeshingException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
outputFile_ = std::string(argv[argIndex]);
std::ofstream testFile(outputFile_.c_str());
if (!testFile.is_open())
{
throw OdmMeshingException("Argument '" + argument + "' has a bad value.");
}
testFile.close();
log_ << "Writing output to: " << outputFile_ << "\n";
}
else if(argument == "-logFile" && argIndex < argc)
{
++argIndex;
if (argIndex >= argc)
{
throw OdmMeshingException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
logFilePath_ = std::string(argv[argIndex]);
std::ofstream testFile(outputFile_.c_str());
if (!testFile.is_open())
{
throw OdmMeshingException("Argument '" + argument + "' has a bad value.");
}
testFile.close();
log_ << "Writing log information to: " << logFilePath_ << "\n";
}
else
{
printHelp();
throw OdmMeshingException("Unrecognised argument '" + argument + "'");
}
}
}
void OdmMeshing::loadPoints()
{
if(pcl::io::loadPLYFile<pcl::PointNormal> (inputFile_.c_str(), *points_.get()) == -1) {
throw OdmMeshingException("Error when reading points and normals from:\n" + inputFile_ + "\n");
}
else
{
log_ << "Successfully loaded " << points_->size() << " points with corresponding normals from file.\n";
}
}
void OdmMeshing::printHelp()
{
bool printInCoutPop = log_.isPrintingInCout();
log_.setIsPrintingInCout(true);
log_ << "OpenDroneMapMeshing.exe\n\n";
log_ << "Purpose:" << "\n";
log_ << "Create a mesh from an oriented point cloud (points with normals) using the Poisson surface reconstruction method." << "\n";
log_ << "Usage:" << "\n";
log_ << "The program requires a path to an input PLY point cloud file, all other input parameters are optional." << "\n\n";
log_ << "The following flags are available\n";
log_ << "Call the program with flag \"-help\", or without parameters to print this message, or check any generated log file.\n";
log_ << "Call the program with flag \"-verbose\", to print log messages in the standard output stream as well as in the log file.\n\n";
log_ << "Parameters are specified as: \"-<argument name> <argument>\", (without <>), and the following parameters are configureable: " << "\n";
log_ << "\"-inputFile <path>\" (mandatory)" << "\n";
log_ << "\"Input ascii ply file that must contain a point cloud with normals.\n\n";
log_ << "\"-outputFile <path>\" (optional, default: odm_mesh.ply)" << "\n";
log_ << "\"Target file in which the mesh is saved.\n\n";
log_ << "\"-logFile <path>\" (optional, default: odm_meshing_log.txt)" << "\n";
log_ << "\"Target file in which the mesh is saved.\n\n";
log_ << "\"-maxVertexCount <integer>\" (optional, default: 100,000)" << "\n";
log_ << "Desired final vertex count (after decimation), set to 0 to disable decimation.\n\n";
log_ << "\"-treeDepth <integer>\" (optional, default: 0 (automatic))" << "\n";
log_ << "Controls octree depth used for poisson reconstruction. Recommended values (9-11).\n"
<< "Increasing the value on this parameter will raise initial vertex count."
<< "If omitted or zero, the depth is calculated automatically from the input point count.\n\n";
log_ << "\"-samplesPerNode <float>\" (optional, default: 1)" << "\n";
log_ << "Average number of samples (points) per octree node. Increasing this value might help if data is very noisy.\n\n";
log_ << "\"-solverDivide <integer>\" (optional, default: 9)" << "\n";
log_ << "Ocree depth at which the Laplacian equation is solved in the surface reconstruction step.\n";
log_ << "Increasing this value increases computation times slightly but helps reduce memory usage.\n\n";
log_.setIsPrintingInCout(printInCoutPop);
}
void OdmMeshing::createMesh()
{
// Attempt to calculate the depth of the tree if unspecified
if (treeDepth_ == 0)
{
treeDepth_ = calcTreeDepth(points_->size());
}
log_ << "Octree depth used for reconstruction is: " << treeDepth_ << "\n";
log_ << "Estimated initial vertex count: " << pow(4, treeDepth_) << "\n\n";
meshCreator_->setDepth(treeDepth_);
meshCreator_->setSamplesPerNode(samplesPerNode_);
meshCreator_->setInputCloud(points_);
// Guarantee manifold mesh.
meshCreator_->setManifold(true);
// Begin reconstruction
meshCreator_->reconstruct(*mesh_.get());
log_ << "Reconstruction complete:\n";
log_ << "Vertex count: " << mesh_->cloud.width << "\n";
log_ << "Triangle count: " << mesh_->polygons.size() << "\n\n";
}
void OdmMeshing::decimateMesh()
{
if (maxVertexCount_ <= 0)
{
log_ << "Vertex count not specified, decimation cancelled.\n";
return;
}
if (maxVertexCount_ > mesh_->cloud.height*mesh_->cloud.width)
{
log_ << "Vertex count in mesh lower than initially generated mesh, unable to decimate.\n";
return;
}
else
{
decimatedMesh_ = pcl::PolygonMeshPtr(new pcl::PolygonMesh);
double reductionFactor = 1.0 - double(maxVertexCount_)/double(mesh_->cloud.height*mesh_->cloud.width);
log_ << "Decimating mesh, removing " << reductionFactor*100 << " percent of vertices.\n";
pcl::MeshQuadricDecimationVTK decimator;
decimator.setInputMesh(mesh_);
decimator.setTargetReductionFactor(reductionFactor);
decimator.process(*decimatedMesh_.get());
log_ << "Decimation complete.\n";
log_ << "Decimated vertex count: " << decimatedMesh_->cloud.width << "\n";
log_ << "Decimated triangle count: " << decimatedMesh_->polygons.size() << "\n\n";
mesh_ = decimatedMesh_;
}
}
int OdmMeshing::calcTreeDepth(size_t nPoints)
{
// Assume points are located (roughly) in a plane.
double squareSide = std::sqrt(double(nPoints));
// Calculate octree depth such that if points were equally distributed in
// a quadratic plane, there would be at least 1 point per octree node.
int depth = 0;
while(std::pow<double>(2,depth) < squareSide/2)
{
depth++;
}
return depth;
}
void OdmMeshing::writePlyFile()
{
log_ << "Saving mesh to file.\n";
if (pcl::io::savePLYFile(outputFile_.c_str(), *mesh_.get()) == -1) {
throw OdmMeshingException("Error when saving mesh to file:\n" + outputFile_ + "\n");
}
else
{
log_ << "Successfully wrote mesh to:\n"
<< outputFile_ << "\n";
}
}

Wyświetl plik

@ -1,117 +0,0 @@
#pragma once
// STL
#include <string>
#include <iostream>
// PCL
#include <pcl/io/ply_io.h>
#include <pcl/surface/poisson.h>
#include <pcl/surface/vtk_smoothing/vtk_mesh_quadric_decimation.h>
// Logging
#include "Logger.hpp"
/*!
* \brief The OdmMeshing class is used to create a triangulated mesh using the Poisson method.
* The class reads an oriented point cloud (coordinates and normals) from a PLY ascii
* file and outputs the resulting welded manifold mesh on the form of an ASCII PLY-file.
* The class uses file read and write functions from pcl.
*/
class OdmMeshing
{
public:
OdmMeshing();
~OdmMeshing();
/*!
* \brief run Runs the meshing functionality using the provided input arguments.
* For a list of accepted arguments, please see the main page documentation or
* call the program with parameter "-help".
* \param argc Application argument count.
* \param argv Argument values.
* \return 0 If successful.
*/
int run(int argc, char **argv);
private:
/*!
* \brief parseArguments Parses command line arguments.
* \param argc Application argument count.
* \param argv Argument values.
*/
void parseArguments(int argc, char** argv);
/*!
* \brief createMesh Sets up the pcl::Poisson meshing class using provided arguments and calls
* it to start the meshing.
*/
void createMesh();
/*!
* \brief loadPoints Loads a PLY ascii file with points and normals from file.
*/
void loadPoints();
/*!
* \brief decimateMesh Performs post-processing on the form of quadric decimation to generate a mesh
* that has a higher density in areas with a lot of structure.
*/
void decimateMesh();
/*!
* \brief writePlyFile Writes the mesh to file on the Ply format.
*/
void writePlyFile();
/*!
* \brief printHelp Prints help, explaining usage. Can be shown by calling the program with argument: "-help".
*/
void printHelp();
/*!
* \brief calcTreeDepth Attepts to calculate the depth of the tree using the point cloud.
* The function makes the assumption points are located roughly in a plane
* (fairly reasonable for ortho-terrain photos) and tries to generate a mesh using
* an octree with an appropriate resolution.
* \param nPoints The total number of points in the input point cloud.
* \return The calcualated octree depth.
*/
int calcTreeDepth(size_t nPoints);
Logger log_; /**< Logging object. */
pcl::Poisson<pcl::PointNormal>::Ptr meshCreator_; /**< PCL poisson meshing class. */
pcl::PointCloud<pcl::PointNormal>::Ptr points_; /**< Input point and normals. */
pcl::PolygonMeshPtr mesh_; /**< PCL polygon mesh. */
pcl::PolygonMeshPtr decimatedMesh_; /**< Decimated polygon mesh. */
std::string inputFile_; /**< Path to a file containing points and normals. */
std::string outputFile_; /**< Path to the destination file. */
std::string logFilePath_; /**< Path to the log file. */
unsigned int maxVertexCount_; /**< Desired output vertex count. */
unsigned int treeDepth_; /**< Depth of octree used for reconstruction. */
double samplesPerNode_; /**< Samples per octree node.*/
double solverDivide_; /**< Depth at which the Laplacian equation solver is run during surface estimation.*/
double decimationFactor_; /**< Percentage of points to remove when decimating the mesh. */
};
/*!
* \brief The OdmMeshingException class
*/
class OdmMeshingException : public std::exception
{
public:
OdmMeshingException() : message("Error in OdmMeshing") {}
OdmMeshingException(std::string msgInit) : message("Error in OdmMeshing:\n" + msgInit) {}
~OdmMeshingException() throw() {}
virtual const char* what() const throw() {return message.c_str(); }
private:
std::string message; /**< The error message **/
};

Wyświetl plik

@ -1,20 +0,0 @@
// Insert license here.
// Include meshing source code.
#include "OdmMeshing.hpp"
/*!
* \mainpage main OpenDroneMap Meshing Module
*
* The OpenDroneMap Meshing Module generates a welded, manifold mesh using the Poisson
* surface reconstruction algorithm from any oriented point cloud (points with corresponding normals).
*
*/
int main(int argc, char** argv)
{
OdmMeshing meshCreator;
return meshCreator.run(argc, argv);
}

Wyświetl plik

@ -1,29 +0,0 @@
#include "Logger.hpp"
Logger::Logger(bool isPrintingInCout) : isPrintingInCout_(isPrintingInCout)
{
}
Logger::~Logger()
{
}
void Logger::print(std::string filePath)
{
std::ofstream file(filePath.c_str(), std::ios::binary);
file << logStream_.str();
file.close();
}
bool Logger::isPrintingInCout() const
{
return isPrintingInCout_;
}
void Logger::setIsPrintingInCout(bool isPrintingInCout)
{
isPrintingInCout_ = isPrintingInCout;
}

Wyświetl plik

@ -1,68 +0,0 @@
#pragma once
// STL
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
/*!
* \brief The Logger class is used to store program messages in a log file.
* \details By using the << operator while printInCout is set, the class writes both to
* cout and to file, if the flag is not set, output is written to file only.
*/
class Logger
{
public:
/*!
* \brief Logger Contains functionality for printing and displaying log information.
* \param printInCout Flag toggling if operator << also writes to cout.
*/
Logger(bool isPrintingInCout = true);
/*!
* \brief Destructor.
*/
~Logger();
/*!
* \brief print Prints the contents of the log to file.
* \param filePath Path specifying where to write the log.
*/
void print(std::string filePath);
/*!
* \brief isPrintingInCout Check if console printing flag is set.
* \return Console printing flag.
*/
bool isPrintingInCout() const;
/*!
* \brief setIsPrintingInCout Set console printing flag.
* \param isPrintingInCout Value, if true, messages added to the log are also printed in cout.
*/
void setIsPrintingInCout(bool isPrintingInCout);
/*!
* Operator for printing messages to log and in the standard output stream if desired.
*/
template<class T>
friend Logger& operator<< (Logger &log, T t)
{
// If console printing is enabled.
if (log.isPrintingInCout_)
{
std::cout << t;
std::cout.flush();
}
// Write to log.
log.logStream_ << t;
return log;
}
private:
bool isPrintingInCout_; /*!< If flag is set, log is printed in cout and written to the log. */
std::stringstream logStream_; /*!< Stream for storing the log. */
};

Wyświetl plik

@ -1,996 +0,0 @@
// C++
#include <math.h>
#include <sstream>
#include <fstream>
// This
#include "OdmOrthoPhoto.hpp"
std::ostream & operator<< (std::ostream &os, const WorldPoint &worldPoint)
{
return os << worldPoint.eastInteger_ + worldPoint.eastFractional_ << " " << worldPoint.northInteger_ + worldPoint.northFractional_;
}
std::istream & operator>> (std::istream &is, WorldPoint &worldPoint)
{
is >> worldPoint.eastInteger_;
// Check if east coordinate is given as rational.
if('.' == is.peek())
{
is >> worldPoint.eastFractional_;
}
else
{
worldPoint.eastFractional_ = 0.0f;
}
is >> worldPoint.northInteger_;
// Check if north coordinate is given as rational.
if('.' == is.peek())
{
is >> worldPoint.northFractional_;
}
else
{
worldPoint.northFractional_ = 0.0f;
}
return is;
}
OdmOrthoPhoto::OdmOrthoPhoto()
:log_(false)
{
inputFile_ = "";
inputGeoRefFile_ = "";
outputFile_ = "ortho.jpg";
logFile_ = "log.txt";
outputCornerFile_ = "";
resolution_ = 0.0f;
boundaryDefined_ = false;
boundaryPoint1_[0] = 0.0f; boundaryPoint1_[1] = 0.0f;
boundaryPoint2_[0] = 0.0f; boundaryPoint2_[1] = 0.0f;
boundaryPoint3_[0] = 0.0f; boundaryPoint3_[1] = 0.0f;
boundaryPoint4_[0] = 0.0f; boundaryPoint4_[1] = 0.0f;
}
OdmOrthoPhoto::~OdmOrthoPhoto()
{
}
int OdmOrthoPhoto::run(int argc, char *argv[])
{
try
{
parseArguments(argc, argv);
createOrthoPhoto();
}
catch (const OdmOrthoPhotoException& e)
{
log_.setIsPrintingInCout(true);
log_ << e.what() << "\n";
log_.print(logFile_);
return EXIT_FAILURE;
}
catch (const std::exception& e)
{
log_.setIsPrintingInCout(true);
log_ << "Error in OdmOrthoPhoto:\n";
log_ << e.what() << "\n";
log_.print(logFile_);
return EXIT_FAILURE;
}
catch (...)
{
log_.setIsPrintingInCout(true);
log_ << "Unknown error, terminating:\n";
log_.print(logFile_);
return EXIT_FAILURE;
}
log_.print(logFile_);
return EXIT_SUCCESS;
}
void OdmOrthoPhoto::parseArguments(int argc, char *argv[])
{
logFile_ = std::string(argv[0]) + "_log.txt";
log_ << logFile_ << "\n\n";
// If no arguments were passed, print help.
if (argc == 1)
{
printHelp();
}
log_ << "Arguments given\n";
for(int argIndex = 1; argIndex < argc; ++argIndex)
{
log_ << argv[argIndex] << '\n';
}
log_ << '\n';
for(int argIndex = 1; argIndex < argc; ++argIndex)
{
// The argument to be parsed.
std::string argument = std::string(argv[argIndex]);
if(argument == "-help")
{
printHelp();
}
else if(argument == "-resolution")
{
++argIndex;
if (argIndex >= argc)
{
throw OdmOrthoPhotoException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
std::stringstream ss(argv[argIndex]);
ss >> resolution_;
log_ << "Resolution count was set to: " << resolution_ << "pixels/meter\n";
}
else if(argument == "-boundary")
{
if(argIndex+8 >= argc)
{
throw OdmOrthoPhotoException("Argument '" + argument + "' expects 8 more input following it, but no more inputs were provided.");
}
std::stringstream ss;
ss << argv[argIndex+1] << " " << argv[argIndex+2] << " " << argv[argIndex+3] << " " << argv[argIndex+4] << " " << argv[argIndex+5] << " " << argv[argIndex+6] << " " << argv[argIndex+7] << " " << argv[argIndex+8];
ss >> worldPoint1_ >> worldPoint2_ >> worldPoint3_ >> worldPoint4_;
boundaryDefined_ = true;
argIndex += 8;
log_ << "Boundary point 1 was set to: " << worldPoint1_ << '\n';
log_ << "Boundary point 2 was set to: " << worldPoint2_ << '\n';
log_ << "Boundary point 3 was set to: " << worldPoint3_ << '\n';
log_ << "Boundary point 4 was set to: " << worldPoint4_ << '\n';
}
else if(argument == "-boundaryMinMax")
{
if(argIndex+4 >= argc)
{
throw OdmOrthoPhotoException("Argument '" + argument + "' expects 4 more input following it, but no more inputs were provided.");
}
std::stringstream ss;
ss << argv[argIndex+1] << " " << argv[argIndex+2] << " " << argv[argIndex+3] << " " << argv[argIndex+4];
ss >> worldPoint1_ >> worldPoint3_;
boundaryDefined_ = true;
// Set the other world points as the other two corners.
worldPoint2_.eastFractional_ = worldPoint1_.eastFractional_;
worldPoint2_.eastInteger_ = worldPoint1_.eastInteger_;
worldPoint2_.northFractional_ = worldPoint3_.northFractional_;
worldPoint2_.northInteger_ = worldPoint3_.northInteger_;
worldPoint4_.eastFractional_ = worldPoint3_.eastFractional_;
worldPoint4_.eastInteger_ = worldPoint3_.eastInteger_;
worldPoint4_.northFractional_ = worldPoint1_.northFractional_;
worldPoint4_.northInteger_ = worldPoint1_.northInteger_;
argIndex += 4;
log_ << "Boundary point 1 was set to: " << worldPoint1_ << '\n';
log_ << "Boundary point 2 was set to: " << worldPoint2_ << '\n';
log_ << "Boundary point 3 was set to: " << worldPoint3_ << '\n';
log_ << "Boundary point 4 was set to: " << worldPoint4_ << '\n';
}
else if(argument == "-verbose")
{
log_.setIsPrintingInCout(true);
}
else if (argument == "-logFile")
{
++argIndex;
if (argIndex >= argc)
{
throw OdmOrthoPhotoException("Missing argument for '" + argument + "'.");
}
logFile_ = std::string(argv[argIndex]);
std::ofstream testFile(logFile_.c_str());
if (!testFile.is_open())
{
throw OdmOrthoPhotoException("Argument '" + argument + "' has a bad value.");
}
log_ << "Log file path was set to: " << logFile_ << "\n";
}
else if(argument == "-inputFile")
{
argIndex++;
if (argIndex >= argc)
{
throw OdmOrthoPhotoException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
inputFile_ = std::string(argv[argIndex]);
log_ << "Reading textured mesh from: " << inputFile_ << "\n";
}
else if(argument == "-inputGeoRefFile")
{
argIndex++;
if (argIndex >= argc)
{
throw OdmOrthoPhotoException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
inputGeoRefFile_ = std::string(argv[argIndex]);
log_ << "Reading georef from: " << inputGeoRefFile_ << "\n";
}
else if(argument == "-outputFile")
{
argIndex++;
if (argIndex >= argc)
{
throw OdmOrthoPhotoException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
outputFile_ = std::string(argv[argIndex]);
log_ << "Writing output to: " << outputFile_ << "\n";
}
else if(argument == "-outputCornerFile")
{
argIndex++;
if (argIndex >= argc)
{
throw OdmOrthoPhotoException("Argument '" + argument + "' expects 1 more input following it, but no more inputs were provided.");
}
outputCornerFile_ = std::string(argv[argIndex]);
log_ << "Writing corners to: " << outputCornerFile_ << "\n";
}
else
{
printHelp();
throw OdmOrthoPhotoException("Unrecognised argument '" + argument + "'");
}
}
log_ << "\n";
}
void OdmOrthoPhoto::printHelp()
{
log_.setIsPrintingInCout(true);
log_ << "OpenDroneMapOrthoPhoto.exe\n\n";
log_ << "Purpose\n";
log_ << "Create an orthograpical photo from an oriented textured mesh.\n\n";
log_ << "Usage:\n";
log_ << "The program requires a path to an input OBJ mesh file and a resolution, as pixels/m. All other input parameters are optional.\n\n";
log_ << "The following flags are available\n";
log_ << "Call the program with flag \"-help\", or without parameters to print this message, or check any generated log file.\n";
log_ << "Call the program with flag \"-verbose\", to print log messages in the standard output stream as well as in the log file.\n\n";
log_ << "Parameters are specified as: \"-<argument name> <argument>\", (without <>), and the following parameters are configureable:n";
log_ << "\"-inputFile <path>\" (mandatory)\n";
log_ << "\"Input obj file that must contain a textured mesh.\n\n";
log_ << "\"-inputGeoRefFile <path>\" (optional, if specified boundary points are assumed to be given as world coordinates. If not specified, the boundary points are assumed to be local coordinates)\n";
log_ << "\"Input geograpical reference system file that describes the world position of the model's origin.\n\n";
log_ << "\"-outputFile <path>\" (optional, default: ortho.jpg)\n";
log_ << "\"Target file in which the orthophoto is saved.\n\n";
log_ << "\"-outputCornerFile <path>\" (optional)\n";
log_ << "\"Target text file for boundary corner points, written as \"xmin ymin xmax ymax\".\n\n";
log_ << "\"-resolution <pixels/m>\" (mandatory)\n";
log_ << "\"The number of pixels used per meter.\n\n";
log_ << "\"-boundary <Point1x Point1y Point2x Point2y Point3x Point3y Point4x Point4y>\" (optional, if not specified the entire model will be rendered)\n";
log_ << "\"Describes the area which should be covered in the ortho photo. The area will be a bounding box containing all four points. The points should be given in the same georeference system as the model.\n\n";
log_ << "\"-boundaryMinMax <MinX MinY MaxX MaxY>\" (optional, if not specified the entire model will be rendered.)\n";
log_ << "\"Describes the area which should be covered in the ortho photo. The area will be a bounding box with corners at MinX, MinY and MaxX, MaxY. The points should be given in the same georeference system as the model.\n\n";
log_.setIsPrintingInCout(false);
}
void OdmOrthoPhoto::createOrthoPhoto()
{
if(inputFile_.empty())
{
throw OdmOrthoPhotoException("Failed to create ortho photo, no texture mesh given.");
}
if(boundaryDefined_)
{
if(inputGeoRefFile_.empty())
{
// Points are assumed to be given in as local points.
adjustBoundsForLocal();
}
else
{
// Points are assumed to be given in as world points.
adjustBoundsForGeoRef();
}
}
else if(!inputGeoRefFile_.empty())
{
// No boundary points specified, but georeference system file was given.
log_ << "Warning:\n";
log_ << "\tSpecified -inputGeoRefFile, but no boundary points. The georeference system will be ignored.\n";
}
log_ << "Reading mesh file...\n";
// The textureds mesh.
pcl::TextureMesh mesh;
pcl::io::loadOBJFile(inputFile_, mesh);
log_ << ".. mesh file read.\n\n";
// Does the model have more than one material?
multiMaterial_ = 1 < mesh.tex_materials.size();
if(multiMaterial_)
{
// Need to check relationship between texture coordinates and faces.
if(!isModelOk(mesh))
{
throw OdmOrthoPhotoException("Could not generate ortho photo: The given mesh has multiple textures, but the number of texture coordinates is NOT equal to 3 times the number of faces.");
}
}
if(!boundaryDefined_)
{
// Determine boundary from model.
adjustBoundsForEntireModel(mesh);
}
// The minimum and maximum boundary values.
float xMax, xMin, yMax, yMin;
xMin = std::min(std::min(boundaryPoint1_[0], boundaryPoint2_[0]), std::min(boundaryPoint3_[0], boundaryPoint4_[0]));
xMax = std::max(std::max(boundaryPoint1_[0], boundaryPoint2_[0]), std::max(boundaryPoint3_[0], boundaryPoint4_[0]));
yMin = std::min(std::min(boundaryPoint1_[1], boundaryPoint2_[1]), std::min(boundaryPoint3_[1], boundaryPoint4_[1]));
yMax = std::max(std::max(boundaryPoint1_[1], boundaryPoint2_[1]), std::max(boundaryPoint3_[1], boundaryPoint4_[1]));
log_ << "Ortho photo bounds x : " << xMin << " -> " << xMax << '\n';
log_ << "Ortho photo bounds y : " << yMin << " -> " << yMax << '\n';
// The size of the area.
float xDiff = xMax - xMin;
float yDiff = yMax - yMin;
log_ << "Ortho photo area : " << xDiff*yDiff << "m2\n";
// The resolution neccesary to fit the area with the given resolution.
int rowRes = static_cast<int>(std::ceil(resolution_*yDiff));
int colRes = static_cast<int>(std::ceil(resolution_*xDiff));
log_ << "Ortho photo resolution, width x height : " << colRes << "x" << rowRes << '\n';
// Check size of photo.
if(0 >= rowRes*colRes)
{
if(0 >= rowRes)
{
log_ << "Warning: ortho photo has zero area, height = " << rowRes << ". Forcing height = 1.\n";
rowRes = 1;
}
if(0 >= colRes)
{
log_ << "Warning: ortho photo has zero area, width = " << colRes << ". Forcing width = 1.\n";
colRes = 1;
}
log_ << "New ortho photo resolution, width x height : " << colRes << "x" << rowRes << '\n';
}
// Init ortho photo
photo_ = cv::Mat::zeros(rowRes, colRes, CV_8UC4) + cv::Scalar(255, 255, 255, 0);
depth_ = cv::Mat::zeros(rowRes, colRes, CV_32F) - std::numeric_limits<float>::infinity();
// Contains the vertices of the mesh.
pcl::PointCloud<pcl::PointXYZ>::Ptr meshCloud (new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromPCLPointCloud2 (mesh.cloud, *meshCloud);
// Creates a transformation which aligns the area for the ortho photo.
Eigen::Transform<float, 3, Eigen::Affine> transform = getROITransform(xMin, -yMax);
log_ << "Translating and scaling mesh...\n";
// Move the mesh into position.
pcl::transformPointCloud(*meshCloud, *meshCloud, transform);
log_ << ".. mesh translated and scaled.\n\n";
// Flatten texture coordiantes.
std::vector<Eigen::Vector2f> uvs;
for(size_t t = 0; t < mesh.tex_coordinates.size(); ++t)
{
uvs.insert(uvs.end(), mesh.tex_coordinates[t].begin(), mesh.tex_coordinates[t].end());
}
// The current material texture
cv::Mat texture;
// Used to keep track of the global face index.
size_t faceOff = 0;
log_ << "Rendering the ortho photo...\n";
// Iterate over each part of the mesh (one per material).
for(size_t t = 0; t < mesh.tex_materials.size(); ++t)
{
// The material of the current submesh.
pcl::TexMaterial material = mesh.tex_materials[t];
texture = cv::imread(material.tex_file);
// Check for missing files.
if(texture.empty())
{
log_ << "Material texture could not be read:\n";
log_ << material.tex_file << '\n';
log_ << "Could not be read as image, does the file exist?\n";
continue; // Skip to next material.
}
// The faces of the current submesh.
std::vector<pcl::Vertices> faces = mesh.tex_polygons[t];
// Iterate over each face...
for(size_t faceIndex = 0; faceIndex < faces.size(); ++faceIndex)
{
// The current polygon.
pcl::Vertices polygon = faces[faceIndex];
// ... and draw it into the ortho photo.
drawTexturedTriangle(texture, polygon, meshCloud, uvs, faceIndex+faceOff);
}
faceOff += faces.size();
log_ << "Material " << t << " rendered.\n";
}
log_ << "...ortho photo rendered\n";
log_ << '\n';
log_ << "Writing ortho photo to " << outputFile_ << "\n";
cv::imwrite(outputFile_, photo_);
if (!outputCornerFile_.empty())
{
log_ << "Writing corner coordinates to " << outputCornerFile_ << "\n";
std::ofstream cornerStream(outputCornerFile_.c_str());
if (!cornerStream.is_open())
{
throw OdmOrthoPhotoException("Failed opening output corner file " + outputCornerFile_ + ".");
}
cornerStream.setf(std::ios::scientific, std::ios::floatfield);
cornerStream.precision(17);
cornerStream << xMin << " " << yMin << " " << xMax << " " << yMax;
cornerStream.close();
}
log_ << "Orthophoto generation done.\n";
}
void OdmOrthoPhoto::adjustBoundsForGeoRef()
{
log_ << "Adjusting bounds for world coordinates\n";
// A stream of the georef system.
std::ifstream geoRefStream(inputGeoRefFile_.c_str());
// The system name
std::string system;
// The east and north offsets
int eastOffset, northOffset;
// Parse file
std::getline(geoRefStream, system);
if(!(geoRefStream >> eastOffset))
{
throw OdmOrthoPhotoException("Could not extract geographical reference system from \n" + inputGeoRefFile_ + "\nCould not extract east offset.");
}
if(!(geoRefStream >> northOffset))
{
throw OdmOrthoPhotoException("Could not extract geographical reference system from \n" + inputGeoRefFile_ + "\nCould not extract north offset.");
}
log_ << "Georeference system:\n";
log_ << system << "\n";
log_ << "East offset: " << eastOffset << "\n";
log_ << "North offset: " << northOffset << "\n";
// Adjust boundary points.
boundaryPoint1_[0] = static_cast<float>(worldPoint1_.eastInteger_ - eastOffset) + worldPoint1_.eastFractional_;
boundaryPoint1_[1] = static_cast<float>(worldPoint1_.northInteger_ - northOffset) + worldPoint1_.northFractional_;
boundaryPoint2_[0] = static_cast<float>(worldPoint2_.eastInteger_ - eastOffset) + worldPoint2_.eastFractional_;
boundaryPoint2_[1] = static_cast<float>(worldPoint2_.northInteger_ - northOffset) + worldPoint2_.northFractional_;
boundaryPoint3_[0] = static_cast<float>(worldPoint3_.eastInteger_ - eastOffset) + worldPoint3_.eastFractional_;
boundaryPoint3_[1] = static_cast<float>(worldPoint3_.northInteger_ - northOffset) + worldPoint3_.northFractional_;
boundaryPoint4_[0] = static_cast<float>(worldPoint4_.eastInteger_ - eastOffset) + worldPoint4_.eastFractional_;
boundaryPoint4_[1] = static_cast<float>(worldPoint4_.northInteger_ - northOffset) + worldPoint4_.northFractional_;
log_ << "Local boundary points:\n";
log_ << "Point 1: " << boundaryPoint1_[0] << " " << boundaryPoint1_[1] << "\n";
log_ << "Point 2: " << boundaryPoint2_[0] << " " << boundaryPoint2_[1] << "\n";
log_ << "Point 3: " << boundaryPoint3_[0] << " " << boundaryPoint3_[1] << "\n";
log_ << "Point 4: " << boundaryPoint4_[0] << " " << boundaryPoint4_[1] << "\n";
}
void OdmOrthoPhoto::adjustBoundsForLocal()
{
log_ << "Adjusting bounds for local coordinates\n";
// Set boundary points from world points.
boundaryPoint1_[0] = static_cast<float>(worldPoint1_.eastInteger_ ) + worldPoint1_.eastFractional_;
boundaryPoint1_[1] = static_cast<float>(worldPoint1_.northInteger_) + worldPoint1_.northFractional_;
boundaryPoint2_[0] = static_cast<float>(worldPoint2_.eastInteger_ ) + worldPoint2_.eastFractional_;
boundaryPoint2_[1] = static_cast<float>(worldPoint2_.northInteger_) + worldPoint2_.northFractional_;
boundaryPoint3_[0] = static_cast<float>(worldPoint3_.eastInteger_ ) + worldPoint3_.eastFractional_;
boundaryPoint3_[1] = static_cast<float>(worldPoint3_.northInteger_) + worldPoint3_.northFractional_;
boundaryPoint4_[0] = static_cast<float>(worldPoint4_.eastInteger_ ) + worldPoint4_.eastFractional_;
boundaryPoint4_[1] = static_cast<float>(worldPoint4_.northInteger_) + worldPoint4_.northFractional_;
log_ << "Local boundary points:\n";
log_ << "Point 1: " << boundaryPoint1_[0] << " " << boundaryPoint1_[1] << "\n";
log_ << "Point 2: " << boundaryPoint2_[0] << " " << boundaryPoint2_[1] << "\n";
log_ << "Point 3: " << boundaryPoint3_[0] << " " << boundaryPoint3_[1] << "\n";
log_ << "Point 4: " << boundaryPoint4_[0] << " " << boundaryPoint4_[1] << "\n";
log_ << "\n";
}
void OdmOrthoPhoto::adjustBoundsForEntireModel(const pcl::TextureMesh &mesh)
{
log_ << "Set boundary to contain entire model.\n";
// The boundary of the model.
float xMin, xMax, yMin, yMax;
xMin = std::numeric_limits<float>::infinity();
xMax = -std::numeric_limits<float>::infinity();
yMin = std::numeric_limits<float>::infinity();
yMax = -std::numeric_limits<float>::infinity();
// Contains the vertices of the mesh.
pcl::PointCloud<pcl::PointXYZ>::Ptr meshCloud (new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromPCLPointCloud2 (mesh.cloud, *meshCloud);
for(size_t t = 0; t < mesh.tex_materials.size(); ++t)
{
// The faces of the current submesh.
std::vector<pcl::Vertices> faces = mesh.tex_polygons[t];
// Iterate over each face...
for(size_t faceIndex = 0; faceIndex < faces.size(); ++faceIndex)
{
// The current polygon.
pcl::Vertices polygon = faces[faceIndex];
// The index to the vertices of the polygon.
size_t v1i = polygon.vertices[0];
size_t v2i = polygon.vertices[1];
size_t v3i = polygon.vertices[2];
// The polygon's points.
pcl::PointXYZ v1 = meshCloud->points[v1i];
pcl::PointXYZ v2 = meshCloud->points[v2i];
pcl::PointXYZ v3 = meshCloud->points[v3i];
xMin = std::min(std::min(xMin, v1.x), std::min(v2.x, v3.x));
xMax = std::max(std::max(xMax, v1.x), std::max(v2.x, v3.x));
yMin = std::min(std::min(yMin, v1.y), std::min(v2.y, v3.y));
yMax = std::max(std::max(yMax, v1.y), std::max(v2.y, v3.y));
}
}
// Create dummy boundary points.
boundaryPoint1_[0] = xMin; boundaryPoint1_[1] = yMin;
boundaryPoint2_[0] = xMin; boundaryPoint2_[1] = yMax;
boundaryPoint3_[0] = xMax; boundaryPoint3_[1] = yMax;
boundaryPoint4_[0] = xMax; boundaryPoint4_[1] = yMin;
log_ << "Local boundary points:\n";
log_ << "Point 1: " << boundaryPoint1_[0] << " " << boundaryPoint1_[1] << "\n";
log_ << "Point 2: " << boundaryPoint2_[0] << " " << boundaryPoint2_[1] << "\n";
log_ << "Point 3: " << boundaryPoint3_[0] << " " << boundaryPoint3_[1] << "\n";
log_ << "Point 4: " << boundaryPoint4_[0] << " " << boundaryPoint4_[1] << "\n";
log_ << "\n";
}
Eigen::Transform<float, 3, Eigen::Affine> OdmOrthoPhoto::getROITransform(float xMin, float yMin) const
{
// The tranform used to move the chosen area into the ortho photo.
Eigen::Transform<float, 3, Eigen::Affine> transform;
transform(0, 0) = resolution_; // x Scaling.
transform(1, 0) = 0.0f;
transform(2, 0) = 0.0f;
transform(3, 0) = 0.0f;
transform(0, 1) = 0.0f;
transform(1, 1) = -resolution_; // y Scaling, mirrored for easier rendering.
transform(2, 1) = 0.0f;
transform(3, 1) = 0.0f;
transform(0, 2) = 0.0f;
transform(1, 2) = 0.0f;
transform(2, 2) = 1.0f;
transform(3, 2) = 0.0f;
transform(0, 3) = -xMin*resolution_; // x Translation
transform(1, 3) = -yMin*resolution_; // y Translation
transform(2, 3) = 0.0f;
transform(3, 3) = 1.0f;
return transform;
}
void OdmOrthoPhoto::drawTexturedTriangle(const cv::Mat &texture, const pcl::Vertices &polygon, const pcl::PointCloud<pcl::PointXYZ>::Ptr &meshCloud, const std::vector<Eigen::Vector2f> &uvs, size_t faceIndex)
{
// The index to the vertices of the polygon.
size_t v1i = polygon.vertices[0];
size_t v2i = polygon.vertices[1];
size_t v3i = polygon.vertices[2];
// The polygon's points.
pcl::PointXYZ v1 = meshCloud->points[v1i];
pcl::PointXYZ v2 = meshCloud->points[v2i];
pcl::PointXYZ v3 = meshCloud->points[v3i];
if(isSliverPolygon(v1, v2, v3))
{
log_ << "Warning: Sliver polygon found at face index " << faceIndex << '\n';
return;
}
// The face data. Position v*{x,y,z}. Texture coordinate v*{u,v}. * is the vertex number in the polygon.
float v1x, v1y, v1z, v1u, v1v;
float v2x, v2y, v2z, v2u, v2v;
float v3x, v3y, v3z, v3u, v3v;
// Barycentric coordinates of the currently rendered point.
float l1, l2, l3;
// The size of the photo, as float.
float fRows, fCols;
fRows = static_cast<float>(texture.rows);
fCols = static_cast<float>(texture.cols);
// Get vertex position.
v1x = v1.x; v1y = v1.y; v1z = v1.z;
v2x = v2.x; v2y = v2.y; v2z = v2.z;
v3x = v3.x; v3y = v3.y; v3z = v3.z;
// Get texture coorinates. (Special cases for PCL when using multiple materials vs one material)
if(multiMaterial_)
{
v1u = uvs[3*faceIndex][0]; v1v = uvs[3*faceIndex][1];
v2u = uvs[3*faceIndex+1][0]; v2v = uvs[3*faceIndex+1][1];
v3u = uvs[3*faceIndex+2][0]; v3v = uvs[3*faceIndex+2][1];
}
else
{
v1u = uvs[v1i][0]; v1v = uvs[v1i][1];
v2u = uvs[v2i][0]; v2v = uvs[v2i][1];
v3u = uvs[v3i][0]; v3v = uvs[v3i][1];
}
// Check bounding box overlap.
int xMin = static_cast<int>(std::min(std::min(v1x, v2x), v3x));
if(xMin > photo_.cols)
{
return; // Completly outside to the right.
}
int xMax = static_cast<int>(std::max(std::max(v1x, v2x), v3x));
if(xMax < 0)
{
return; // Completly outside to the left.
}
int yMin = static_cast<int>(std::min(std::min(v1y, v2y), v3y));
if(yMin > photo_.rows)
{
return; // Completly outside to the top.
}
int yMax = static_cast<int>(std::max(std::max(v1y, v2y), v3y));
if(yMax < 0)
{
return; // Completly outside to the bottom.
}
// Top point row and column positions
float topR, topC;
// Middle point row and column positions
float midR, midC;
// Bottom point row and column positions
float botR, botC;
// Find top, middle and bottom points.
if(v1y < v2y)
{
if(v1y < v3y)
{
if(v2y < v3y)
{
// 1 -> 2 -> 3
topR = v1y; topC = v1x;
midR = v2y; midC = v2x;
botR = v3y; botC = v3x;
}
else
{
// 1 -> 3 -> 2
topR = v1y; topC = v1x;
midR = v3y; midC = v3x;
botR = v2y; botC = v2x;
}
}
else
{
// 3 -> 1 -> 2
topR = v3y; topC = v3x;
midR = v1y; midC = v1x;
botR = v2y; botC = v2x;
}
}
else // v2y <= v1y
{
if(v2y < v3y)
{
if(v1y < v3y)
{
// 2 -> 1 -> 3
topR = v2y; topC = v2x;
midR = v1y; midC = v1x;
botR = v3y; botC = v3x;
}
else
{
// 2 -> 3 -> 1
topR = v2y; topC = v2x;
midR = v3y; midC = v3x;
botR = v1y; botC = v1x;
}
}
else
{
// 3 -> 2 -> 1
topR = v3y; topC = v3x;
midR = v2y; midC = v2x;
botR = v1y; botC = v1x;
}
}
// General appreviations:
// ---------------------
// tm : Top(to)Middle.
// mb : Middle(to)Bottom.
// tb : Top(to)Bottom.
// c : column.
// r : row.
// dr : DeltaRow, step value per row.
// The step along column for every step along r. Top to middle.
float ctmdr;
// The step along column for every step along r. Top to bottom.
float ctbdr;
// The step along column for every step along r. Middle to bottom.
float cmbdr;
ctbdr = (botC-topC)/(botR-topR);
// The current column position, from top to middle.
float ctm = topC;
// The current column position, from top to bottom.
float ctb = topC;
// Check for vertical line between middle and top.
if(FLT_EPSILON < midR-topR)
{
ctmdr = (midC-topC)/(midR-topR);
// The first pixel row for the bottom part of the triangle.
int rqStart = std::max(static_cast<int>(std::floor(topR+0.5f)), 0);
// The last pixel row for the top part of the triangle.
int rqEnd = std::min(static_cast<int>(std::floor(midR+0.5f)), photo_.rows);
// Travers along row from top to middle.
for(int rq = rqStart; rq < rqEnd; ++rq)
{
// Set the current column positions.
ctm = topC + ctmdr*(static_cast<float>(rq)+0.5f-topR);
ctb = topC + ctbdr*(static_cast<float>(rq)+0.5f-topR);
// The first pixel column for the current row.
int cqStart = std::max(static_cast<int>(std::floor(0.5f+std::min(ctm, ctb))), 0);
// The last pixel column for the current row.
int cqEnd = std::min(static_cast<int>(std::floor(0.5f+std::max(ctm, ctb))), photo_.cols);
for(int cq = cqStart; cq < cqEnd; ++cq)
{
// Get barycentric coordinates for the current point.
getBarycentricCoordiantes(v1, v2, v3, static_cast<float>(cq)+0.5f, static_cast<float>(rq)+0.5f, l1, l2, l3);
if(0.f > l1 || 0.f > l2 || 0.f > l3)
{
//continue;
}
// The z value for the point.
float z = v1z*l1+v2z*l2+v3z*l3;
// Check depth
float depthValue = depth_.at<float>(rq, cq);
if(z < depthValue)
{
// Current is behind another, don't draw.
continue;
}
// The uv values of the point.
float u, v;
u = v1u*l1+v2u*l2+v3u*l3;
v = v1v*l1+v2v*l2+v3v*l3;
renderPixel(rq, cq, u*fCols, (1.0f-v)*fRows, texture);
// Update depth buffer.
depth_.at<float>(rq, cq) = z;
}
}
}
if(FLT_EPSILON < botR-midR)
{
cmbdr = (botC-midC)/(botR-midR);
// The current column position, from middle to bottom.
float cmb = midC;
// The first pixel row for the bottom part of the triangle.
int rqStart = std::max(static_cast<int>(std::floor(midR+0.5f)), 0);
// The last pixel row for the bottom part of the triangle.
int rqEnd = std::min(static_cast<int>(std::floor(botR+0.5f)), photo_.rows);
// Travers along row from middle to bottom.
for(int rq = rqStart; rq < rqEnd; ++rq)
{
// Set the current column positions.
ctb = topC + ctbdr*(static_cast<float>(rq)+0.5f-topR);
cmb = midC + cmbdr*(static_cast<float>(rq)+0.5f-midR);
// The first pixel column for the current row.
int cqStart = std::max(static_cast<int>(std::floor(0.5f+std::min(cmb, ctb))), 0);
// The last pixel column for the current row.
int cqEnd = std::min(static_cast<int>(std::floor(0.5f+std::max(cmb, ctb))), photo_.cols);
for(int cq = cqStart; cq < cqEnd; ++cq)
{
// Get barycentric coordinates for the current point.
getBarycentricCoordiantes(v1, v2, v3, static_cast<float>(cq)+0.5f, static_cast<float>(rq)+0.5f, l1, l2, l3);
if(0.f > l1 || 0.f > l2 || 0.f > l3)
{
//continue;
}
// The z value for the point.
float z = v1z*l1+v2z*l2+v3z*l3;
// Check depth
float depthValue = depth_.at<float>(rq, cq);
if(z < depthValue)
{
// Current is behind another, don't draw.
continue;
}
// The uv values of the point.
float u, v;
u = v1u*l1+v2u*l2+v3u*l3;
v = v1v*l1+v2v*l2+v3v*l3;
renderPixel(rq, cq, u*fCols, (1.0f-v)*fRows, texture);
// Update depth buffer.
depth_.at<float>(rq, cq) = z;
}
}
}
}
void OdmOrthoPhoto::renderPixel(int row, int col, float s, float t, const cv::Mat &texture)
{
// The colors of the texture pixels. tl : top left, tr : top right, bl : bottom left, br : bottom right.
cv::Vec3b tl, tr, bl, br;
// The offset of the texture coordinate from its pixel positions.
float leftF, topF;
// The position of the top left pixel.
int left, top;
// The distance to the left and right pixel from the texture coordinate.
float dl, dt;
// The distance to the top and bottom pixel from the texture coordinate.
float dr, db;
dl = modff(s, &leftF);
dr = 1.0f - dl;
dt = modff(t, &topF);
db = 1.0f - dt;
left = static_cast<int>(leftF);
top = static_cast<int>(topF);
tl = texture.at<cv::Vec3b>(top, left);
tr = texture.at<cv::Vec3b>(top, left+1);
bl = texture.at<cv::Vec3b>(top+1, left);
br = texture.at<cv::Vec3b>(top+1, left+1);
// The interpolated color values.
float r = 0.0f, g = 0.0f, b = 0.0f;
// Red
r += static_cast<float>(tl[2]) * dr * db;
r += static_cast<float>(tr[2]) * dl * db;
r += static_cast<float>(bl[2]) * dr * dt;
r += static_cast<float>(br[2]) * dl * dt;
// Green
g += static_cast<float>(tl[1]) * dr * db;
g += static_cast<float>(tr[1]) * dl * db;
g += static_cast<float>(bl[1]) * dr * dt;
g += static_cast<float>(br[1]) * dl * dt;
// Blue
b += static_cast<float>(tl[0]) * dr * db;
b += static_cast<float>(tr[0]) * dl * db;
b += static_cast<float>(bl[0]) * dr * dt;
b += static_cast<float>(br[0]) * dl * dt;
photo_.at<cv::Vec4b>(row,col) = cv::Vec4b(static_cast<unsigned char>(b), static_cast<unsigned char>(g), static_cast<unsigned char>(r), 255);
}
void OdmOrthoPhoto::getBarycentricCoordiantes(pcl::PointXYZ v1, pcl::PointXYZ v2, pcl::PointXYZ v3, float x, float y, float &l1, float &l2, float &l3) const
{
// Diff along y.
float y2y3 = v2.y-v3.y;
float y1y3 = v1.y-v3.y;
float y3y1 = v3.y-v1.y;
float yy3 = y -v3.y;
// Diff along x.
float x3x2 = v3.x-v2.x;
float x1x3 = v1.x-v3.x;
float xx3 = x -v3.x;
// Normalization factor.
float norm = (y2y3*x1x3 + x3x2*y1y3);
l1 = (y2y3*(xx3) + x3x2*(yy3)) / norm;
l2 = (y3y1*(xx3) + x1x3*(yy3)) / norm;
l3 = 1 - l1 - l2;
}
bool OdmOrthoPhoto::isSliverPolygon(pcl::PointXYZ v1, pcl::PointXYZ v2, pcl::PointXYZ v3) const
{
// Calculations are made using doubles, to minize rounding errors.
Eigen::Vector3d a = Eigen::Vector3d(static_cast<double>(v1.x), static_cast<double>(v1.y), static_cast<double>(v1.z));
Eigen::Vector3d b = Eigen::Vector3d(static_cast<double>(v2.x), static_cast<double>(v2.y), static_cast<double>(v2.z));
Eigen::Vector3d c = Eigen::Vector3d(static_cast<double>(v3.x), static_cast<double>(v3.y), static_cast<double>(v3.z));
Eigen::Vector3d dummyVec = (a-b).cross(c-b);
// Area smaller than, or equal to, floating-point epsilon.
return std::numeric_limits<float>::epsilon() >= static_cast<float>(std::sqrt(dummyVec.dot(dummyVec))/2.0);
}
bool OdmOrthoPhoto::isModelOk(const pcl::TextureMesh &mesh)
{
// The number of texture coordinates in the model.
size_t nTextureCoordinates = 0;
// The number of faces in the model.
size_t nFaces = 0;
for(size_t t = 0; t < mesh.tex_coordinates.size(); ++t)
{
nTextureCoordinates += mesh.tex_coordinates[t].size();
}
for(size_t t = 0; t < mesh.tex_polygons.size(); ++t)
{
nFaces += mesh.tex_polygons[t].size();
}
log_ << "Number of faces in the model " << nFaces << '\n';
return 3*nFaces == nTextureCoordinates;
}

Wyświetl plik

@ -1,217 +0,0 @@
#pragma once
// C++
#include <limits.h>
#include <istream>
#include <ostream>
// PCL
#include <pcl/io/obj_io.h>
#include <pcl/common/transforms.h>
// OpenCV
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
// PCL
#include <pcl/common/eigen.h>
#include <pcl/common/common.h>
// OpenCV
#include <opencv2/core/core.hpp>
// Logger
#include "Logger.hpp"
/*!
* \brief The WorldPoint struct encapsules world coordiantes used for the orhto photo boundary.
* Points are separated into integersand fractional parts for high numerical stability.
*/
struct WorldPoint
{
int eastInteger_; /**< The inger part of the east point. */
float eastFractional_; /**< The farctional part of the east point. */
int northInteger_; /**< The inger part of the east point. */
float northFractional_; /**< The farctional part of the east point. */
/*!
* \brief Overloads operator '<<' for WorldPoint.
*
* \param os The output stream in which the WorldPoint should be printed.
* \param worldPoint The WorldPoint should be printed.
* \return A reference to the given output stream.
*/
friend std::ostream & operator<< (std::ostream &os, const WorldPoint &worldPoint);
/*!
* \brief Overloads operator '>>' for WorldPoint.
*
* \param is The input stream from which the WorldPoint should be extracted
* \param worldPoint The modified WorldPoint.
* \return A reference to the given input stream.
*/
friend std::istream & operator>> (std::istream &os, WorldPoint &worldPoint);
};
/*!
* \brief The OdmOrthoPhoto class is used to create an orthograpic photo over a given area.
* The class reads an oriented textured mesh from an OBJ-file.
* The class uses file read from pcl.
* The class uses image read and write from opencv.
*/
class OdmOrthoPhoto
{
public:
OdmOrthoPhoto();
~OdmOrthoPhoto();
/*!
* \brief run Runs the ortho photo functionality using the provided input arguments.
* For a list of accepted arguments, pleas see the main page documentation or
* call the program with parameter "-help".
* \param argc Application argument count.
* \param argv Argument values.
* \return 0 if successful.
*/
int run(int argc, char* argv[]);
private:
/*!
* \brief parseArguments Parses command line arguments.
*
* \param argc Application argument count.
* \param argv Argument values.
*/
void parseArguments(int argc, char* argv[]);
/*!
* \brief printHelp Prints help, explaining usage. Can be shown by calling the program with argument: "-help".
*/
void printHelp();
/*!
* \brief Create the ortho photo using the current settings.
*/
void createOrthoPhoto();
/*!
* \brief Adjusts the boundary points according to the given georef system.
*/
void adjustBoundsForGeoRef();
/*!
* \brief Adjusts the boundary points assuming the wolrd points are relative the local coordinate system.
*/
void adjustBoundsForLocal();
/*!
* \brief Adjusts the boundary points so that the entire model fits inside the photo.
*
* \param mesh The model which decides the boundary.
*/
void adjustBoundsForEntireModel(const pcl::TextureMesh &mesh);
/*!
* \brief Creates a transformation which aligns the area for the orthophoto.
*/
Eigen::Transform<float, 3, Eigen::Affine> getROITransform(float xMin, float yMin) const;
/*!
* \brief Renders a triangle into the ortho photo.
*
* Pixel center defined as middle of pixel for triangle rasterisation, and in lower left corner for texture look-up.
*
* \param texture The texture of the polygon.
* \param polygon The polygon as athree indices relative meshCloud.
* \param meshCloud Contains all vertices.
* \param uvs Contains the texture coordiantes for the active material.
* \param faceIndex The index of the face.
*/
void drawTexturedTriangle(const cv::Mat &texture, const pcl::Vertices &polygon, const pcl::PointCloud<pcl::PointXYZ>::Ptr &meshCloud, const std::vector<Eigen::Vector2f> &uvs, size_t faceIndex);
/*!
* \brief Sets the color of a pixel in the photo.
*
* \param row The row index of the pixel.
* \param col The column index of the pixel.
* \param s The u texture-coordinate, multiplied with the number of columns in the texture.
* \param t The v texture-coordinate, multiplied with the number of rows in the texture.
* \param texture The texture from which to get the color.
**/
void renderPixel(int row, int col, float u, float v, const cv::Mat &texture);
/*!
* \brief Calcualtes the barycentric coordinates of a point in a triangle.
*
* \param v1 The first triangle vertex.
* \param v2 The second triangle vertex.
* \param v3 The third triangle vertex.
* \param x The x coordinate of the point.
* \param y The y coordinate of the point.
* \param l1 The first vertex weight.
* \param l2 The second vertex weight.
* \param l3 The third vertex weight.
*/
void getBarycentricCoordiantes(pcl::PointXYZ v1, pcl::PointXYZ v2, pcl::PointXYZ v3, float x, float y, float &l1, float &l2, float &l3) const;
/*!
* \brief Check if a given polygon is a sliver polygon.
*
* \param v1 The first vertex of the polygon.
* \param v2 The second vertex of the polygon.
* \param v3 The third vertex of the polygon.
*/
bool isSliverPolygon(pcl::PointXYZ v1, pcl::PointXYZ v2, pcl::PointXYZ v3) const;
/*!
* \brief Check if the model is suitable for ortho photo generation.
*
* \param mesh The model.
* \return True if the model is ok for generating ortho photo.
*/
bool isModelOk(const pcl::TextureMesh &mesh);
Logger log_; /**< Logging object. */
std::string inputFile_; /**< Path to the textured mesh as an obj-file. */
std::string inputGeoRefFile_; /**< Path to the georeference system file. */
std::string outputFile_; /**< Path to the destination file. */
std::string outputCornerFile_; /**< Path to the output corner file. */
std::string logFile_; /**< Path to the log file. */
float resolution_; /**< The number of pixels per meter in the ortho photo. */
bool boundaryDefined_; /**< True if the user has defined a boundary. */
WorldPoint worldPoint1_; /**< The first boundary point for the ortho photo, in world coordiantes. */
WorldPoint worldPoint2_; /**< The second boundary point for the ortho photo, in world coordiantes. */
WorldPoint worldPoint3_; /**< The third boundary point for the ortho photo, in world coordiantes. */
WorldPoint worldPoint4_; /**< The fourth boundary point for the ortho photo, in world coordiantes. */
Eigen::Vector2f boundaryPoint1_; /**< The first boundary point for the ortho photo, in local coordinates. */
Eigen::Vector2f boundaryPoint2_; /**< The second boundary point for the ortho photo, in local coordinates. */
Eigen::Vector2f boundaryPoint3_; /**< The third boundary point for the ortho photo, in local coordinates. */
Eigen::Vector2f boundaryPoint4_; /**< The fourth boundary point for the ortho photo, in local coordinates. */
cv::Mat photo_; /**< The ortho photo as an OpenCV matrix, CV_8UC3. */
cv::Mat depth_; /**< The depth of the ortho photo as an OpenCV matrix, CV_32F. */
bool multiMaterial_; /**< True if the mesh has multiple materials. **/
};
/*!
* \brief The OdmOrthoPhoto class
*/
class OdmOrthoPhotoException : public std::exception
{
public:
OdmOrthoPhotoException() : message("Error in OdmOrthoPhoto") {}
OdmOrthoPhotoException(std::string msgInit) : message("Error in OdmOrthoPhoto:\n" + msgInit) {}
~OdmOrthoPhotoException() throw() {}
virtual const char* what() const throw() {return message.c_str(); }
private:
std::string message; /**< The error message **/
};

Wyświetl plik

@ -1,8 +0,0 @@
// Ortho photo generator.
#include "OdmOrthoPhoto.hpp"
int main(int argc, char* argv[])
{
OdmOrthoPhoto orthoPhotoGenerator;
return orthoPhotoGenerator.run(argc, argv);
}

Wyświetl plik

@ -1,31 +0,0 @@
#include "Logger.hpp"
Logger::Logger(bool isPrintingInCout) : isPrintingInCout_(isPrintingInCout)
{
}
Logger::~Logger()
{
}
void Logger::printToFile(std::string filePath)
{
std::ofstream file(filePath.c_str(), std::ios::binary);
file << logStream_.str();
file.close();
}
bool Logger::isPrintingInCout() const
{
return isPrintingInCout_;
}
void Logger::setIsPrintingInCout(bool isPrintingInCout)
{
isPrintingInCout_ = isPrintingInCout;
}

Wyświetl plik

@ -1,68 +0,0 @@
#pragma once
// STL
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
/*!
* \brief The Logger class is used to store program messages in a log file.
* \details By using the << operator while printInCout is set, the class writes both to
* cout and to file, if the flag is not set, output is written to file only.
*/
class Logger
{
public:
/*!
* \brief Logger Contains functionality for printing and displaying log information.
* \param printInCout Flag toggling if operator << also writes to cout.
*/
Logger(bool isPrintingInCout = true);
/*!
* \brief Destructor.
*/
~Logger();
/*!
* \brief print Prints the contents of the log to file.
* \param filePath Path specifying where to write the log.
*/
void printToFile(std::string filePath);
/*!
* \brief isPrintingInCout Check if console printing flag is set.
* \return Console printing flag.
*/
bool isPrintingInCout() const;
/*!
* \brief setIsPrintingInCout Set console printing flag.
* \param isPrintingInCout Value, if true, messages added to the log are also printed in cout.
*/
void setIsPrintingInCout(bool isPrintingInCout);
/*!
* Operator for printing messages to log and in the standard output stream if desired.
*/
template<class T>
friend Logger& operator<< (Logger &log, T t)
{
// If console printing is enabled.
if (log.isPrintingInCout_)
{
std::cout << t;
std::cout.flush();
}
// Write to log.
log.logStream_ << t;
return log;
}
private:
bool isPrintingInCout_; /*!< If flag is set, log is printed in cout and written to the log. */
std::stringstream logStream_; /*!< Stream for storing the log. */
};

Wyświetl plik

@ -1,211 +0,0 @@
#pragma once
// STL
#include <iostream>
#include <fstream>
// PCL
#include <pcl/point_types.h>
#include <pcl/search/kdtree.h>
#include <pcl/surface/texture_mapping.h>
// OpenCV
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
// Modified PCL functions
#include "modifiedPclFunctions.hpp"
// Logging
#include "Logger.hpp"
/*!
* \brief The Coords struct Coordinate class used in recursiveFindCoordinates for OdmTexturing::sortPatches().
*/
struct Coords
{
// Coordinates for row and column
float r_, c_;
// If coordinates have been placed
bool success_;
Coords()
{
r_ = 0.0;
c_ = 0.0;
success_ = false;
}
};
/*!
* \brief The Patch struct Struct to hold all faces connected and with the same optimal camera.
*/
struct Patch
{
std::vector<size_t> faces_;
float minu_, minv_, maxu_, maxv_;
Coords c_;
bool placed_;
int materialIndex_;
int optimalCameraIndex_;
Patch()
{
placed_ = false;
faces_ = std::vector<size_t>(0);
minu_ = std::numeric_limits<double>::infinity();
minv_ = std::numeric_limits<double>::infinity();
maxu_ = 0.0;
maxv_ = 0.0;
optimalCameraIndex_ = -1;
materialIndex_ = 0;
}
};
/*!
* \brief The Node struct Node class for acceleration structure in OdmTexturing::sortPatches().
*/
struct Node
{
float r_, c_, width_, height_;
bool used_;
Node* rgt_;
Node* lft_;
Node()
{
r_ = 0.0;
c_ = 0.0;
width_ = 1.0;
height_ = 1.0;
used_ = false;
rgt_ = NULL;
lft_ = NULL;
}
Node(const Node &n)
{
r_ = n.r_;
c_ = n.c_;
used_ = n.used_;
width_ = n.width_;
height_ = n.height_;
rgt_ = n.rgt_;
lft_ = n.lft_;
}
};
/*!
* \brief The OdmTexturing class is used to create textures to a welded ply-mesh using the camera
* positions from pmvs as input. The result is stored in an obj-file with corresponding
* mtl-file and the textures saved as jpg.
*/
class OdmTexturing
{
public:
OdmTexturing();
~OdmTexturing();
/*!
* \brief run Runs the texturing functionality using the provided input arguments.
* For a list of the accepted arguments, please see the main page documentation or
* call the program with parameter "-help".
* \param argc Application argument count.
* \param argv Argument values.
* \return 0 if successful.
*/
int run(int argc, char **argv);
private:
/*!
* \brief parseArguments Parses command line arguments.
* \param argc Application argument count.
* \param argv Argument values.
*/
void parseArguments(int argc, char** argv);
/*!
* \brief loadMesh Loads a PLY-file containing vertices and faces.
*/
void loadMesh();
/*!
* \brief loadCameras Loads cameras from a bundle.out file with corresponding image list file.
*/
void loadCameras();
/*!
* \brief triangleToImageAssignment Assigns optimal camera to faces for the faces that are visible.
*/
void triangleToImageAssignment();
/*!
* \brief calculatePatches Arrange faces into patches as a prestep to arranging UV-mapping.
*/
void calculatePatches();
/*!
* \brief recursiveFindCoords Recursive function used in sortPatches() to find free area to place patch.
* \param n The container in which to check for free space in.
* \param w The width of the box to place.
* \param h The height of the box to place.
* \return The coordinates where the patch has been placed.
*/
Coords recursiveFindCoords(Node &n, float w, float h);
/*!
* \brief sortPatches Sorts patches into UV-containers to be used in createTextures() using a rectangle packer approach.
*/
void sortPatches();
/*!
* \brief createTextures Creates textures to the mesh.
*/
void createTextures();
/*!
* \brief writeObjFile Writes the textured mesh to file on the OBJ format.
*/
void writeObjFile();
/*!
* \brief printHelp Prints help, explaining usage. Can be shown by calling the program with arguments: "-help".
*/
void printHelp();
Logger log_; /**< Logging object. */
std::string logFilePath_; /**< Path to store the log file. */
std::string bundlePath_; /**< Path to the bundle.out file. */
std::string imagesPath_; /**< Path to the folder with all images in the image list. */
std::string imagesListPath_; /**< Path to the image list. */
std::string inputModelPath_; /**< Path to the ply-file containing the mesh to be textured. */
std::string outputFolder_; /**< Path to the folder to store the output mesh and textures. */
double bundleResizedTo_; /**< The size used in the previous steps to calculate the camera focal_length. */
double textureWithSize_; /**< The desired size of the images to texture with. */
double textureResolution_; /**< The resolution of each texture. */
double padding_; /**< A padding used to handle edge cases. */
int nrTextures_; /**< The number of textures created. */
pcl::TextureMesh::Ptr mesh_; /**< PCL Texture Mesh */
std::vector<Patch> patches_; /**< The vector containing all patches */
pcl::texture_mapping::CameraVector cameras_; /**< The vector containing all cameras. */
std::vector<int> tTIA_; /**< The vector containing the optimal cameras for all faces. */
};
class OdmTexturingException : public std::exception
{
public:
OdmTexturingException() : message("Error in OdmTexturing") {}
OdmTexturingException(std::string msgInit) : message("Error in OdmTexturing:\n" + msgInit) {}
~OdmTexturingException() throw() {}
virtual const char* what() const throw() {return message.c_str(); }
private:
std::string message; /**< The error message. */
};

Wyświetl plik

@ -1,15 +0,0 @@
// Include texturing source code
#include "OdmTexturing.hpp"
/*!
* \mainpage main OpenDroneMap Texturing Module
*
*
*
*/
int main (int argc, char** argv)
{
OdmTexturing textureCreator;
return textureCreator.run(argc, argv);
}

Wyświetl plik

@ -1,336 +0,0 @@
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2012-, Open Perception, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "modifiedPclFunctions.hpp"
int saveOBJFile(const std::string &file_name, const pcl::TextureMesh &tex_mesh, unsigned precision)
{
if (tex_mesh.cloud.data.empty ())
{
PCL_ERROR ("[pcl::io::saveOBJFile] Input point cloud has no data!\n");
return (-1);
}
// Open file
std::ofstream fs;
fs.precision (precision);
fs.open (file_name.c_str ());
// Define material file
std::string mtl_file_name = file_name.substr (0, file_name.find_last_of (".")) + ".mtl";
// Strip path for "mtllib" command
std::string mtl_file_name_nopath = mtl_file_name;
//std::cout << mtl_file_name_nopath << std::endl;
mtl_file_name_nopath.erase (0, mtl_file_name.find_last_of ('/') + 1);
/* Write 3D information */
// number of points
int nr_points = tex_mesh.cloud.width * tex_mesh.cloud.height;
int point_size = tex_mesh.cloud.data.size () / nr_points;
// mesh size
int nr_meshes = tex_mesh.tex_polygons.size ();
// number of faces for header
int nr_faces = 0;
for (int m = 0; m < nr_meshes; ++m)
nr_faces += tex_mesh.tex_polygons[m].size ();
// Write the header information
fs << "####" << std::endl;
fs << "# OBJ dataFile simple version. File name: " << file_name << std::endl;
fs << "# Vertices: " << nr_points << std::endl;
fs << "# Faces: " <<nr_faces << std::endl;
fs << "# Material information:" << std::endl;
fs << "mtllib " << mtl_file_name_nopath << std::endl;
fs << "####" << std::endl;
// Write vertex coordinates
fs << "# Vertices" << std::endl;
for (int i = 0; i < nr_points; ++i)
{
int xyz = 0;
// "v" just be written one
bool v_written = false;
for (size_t d = 0; d < tex_mesh.cloud.fields.size (); ++d)
{
int count = tex_mesh.cloud.fields[d].count;
if (count == 0)
count = 1; // we simply cannot tolerate 0 counts (coming from older converter code)
int c = 0;
// adding vertex
if ((tex_mesh.cloud.fields[d].datatype == pcl::PCLPointField::FLOAT32) /*sensor_msgs::PointField::FLOAT32)*/ && (
tex_mesh.cloud.fields[d].name == "x" ||
tex_mesh.cloud.fields[d].name == "y" ||
tex_mesh.cloud.fields[d].name == "z"))
{
if (!v_written)
{
// write vertices beginning with v
fs << "v ";
v_written = true;
}
float value;
memcpy (&value, &tex_mesh.cloud.data[i * point_size + tex_mesh.cloud.fields[d].offset + c * sizeof (float)], sizeof (float));
fs << value;
if (++xyz == 3)
break;
fs << " ";
}
}
if (xyz != 3)
{
PCL_ERROR ("[pcl::io::saveOBJFile] Input point cloud has no XYZ data!\n");
return (-2);
}
fs << std::endl;
}
fs << "# "<< nr_points <<" vertices" << std::endl;
// // Write vertex normals
// for (int i = 0; i < nr_points; ++i)
// {
// int xyz = 0;
// // "vn" just be written one
// bool v_written = false;
// for (size_t d = 0; d < tex_mesh.cloud.fields.size (); ++d)
// {
// int count = tex_mesh.cloud.fields[d].count;
// if (count == 0)
// count = 1; // we simply cannot tolerate 0 counts (coming from older converter code)
// int c = 0;
// // adding vertex
// if ((tex_mesh.cloud.fields[d].datatype == pcl::PCLPointField::FLOAT32) && (
// tex_mesh.cloud.fields[d].name == "normal_x" ||
// tex_mesh.cloud.fields[d].name == "normal_y" ||
// tex_mesh.cloud.fields[d].name == "normal_z"))
// {
// if (!v_written)
// {
// // write vertices beginning with vn
// fs << "vn ";
// v_written = true;
// }
// float value;
// memcpy (&value, &tex_mesh.cloud.data[i * point_size + tex_mesh.cloud.fields[d].offset + c * sizeof (float)], sizeof (float));
// fs << value;
// if (++xyz == 3)
// break;
// fs << " ";
// }
// }
// if (xyz != 3)
// {
// //PCL_ERROR ("[pcl::io::saveOBJFile] Input point cloud has no normals!\n");
// //return (-2);
// }
// fs << std::endl;
// }
// Write vertex texture with "vt" (adding latter)
for (int m = 0; m < nr_meshes; ++m)
{
if(tex_mesh.tex_coordinates.size() == 0)
continue;
//PCL_INFO ("%d vertex textures in submesh %d\n", tex_mesh.tex_coordinates[m].size (), m);
fs << "# " << tex_mesh.tex_coordinates[m].size() << " vertex textures in submesh " << m << std::endl;
for (size_t i = 0; i < tex_mesh.tex_coordinates[m].size (); ++i)
{
fs << "vt ";
fs << tex_mesh.tex_coordinates[m][i][0] << " " << tex_mesh.tex_coordinates[m][i][1] << std::endl;
}
}
int f_idx = 0;
// int idx_vt =0;
//PCL_INFO ("Writting faces...\n");
for (int m = 0; m < nr_meshes; ++m)
{
if (m > 0)
f_idx += tex_mesh.tex_polygons[m-1].size ();
if(tex_mesh.tex_materials.size() !=0)
{
fs << "# The material will be used for mesh " << m << std::endl;
//TODO pbl here with multi texture and unseen faces
fs << "usemtl " << tex_mesh.tex_materials[m].tex_name << std::endl;
fs << "# Faces" << std::endl;
}
for (size_t i = 0; i < tex_mesh.tex_polygons[m].size(); ++i)
{
// Write faces with "f"
fs << "f";
size_t j = 0;
// There's one UV per vertex per face, i.e., the same vertex can have
// different UV depending on the face.
for (j = 0; j < tex_mesh.tex_polygons[m][i].vertices.size (); ++j)
{
unsigned int idx = tex_mesh.tex_polygons[m][i].vertices[j] + 1;
fs << " " << idx
<< "/" << 3*(i+f_idx) +j+1;
//<< "/" << idx; // vertex index in obj file format starting with 1
}
fs << std::endl;
}
//PCL_INFO ("%d faces in mesh %d \n", tex_mesh.tex_polygons[m].size () , m);
fs << "# "<< tex_mesh.tex_polygons[m].size() << " faces in mesh " << m << std::endl;
}
fs << "# End of File";
// Close obj file
//PCL_INFO ("Closing obj file\n");
fs.close ();
/* Write material defination for OBJ file*/
// Open file
//PCL_INFO ("Writing material files\n");
//dont do it if no material to write
if(tex_mesh.tex_materials.size() ==0)
return (0);
std::ofstream m_fs;
m_fs.precision (precision);
m_fs.open (mtl_file_name.c_str ());
//std::cout << "MTL file is located at_ " << mtl_file_name << std::endl;
// default
m_fs << "#" << std::endl;
m_fs << "# Wavefront material file" << std::endl;
m_fs << "#" << std::endl;
for(int m = 0; m < nr_meshes; ++m)
{
m_fs << "newmtl " << tex_mesh.tex_materials[m].tex_name << std::endl;
m_fs << "Ka "<< tex_mesh.tex_materials[m].tex_Ka.r << " " << tex_mesh.tex_materials[m].tex_Ka.g << " " << tex_mesh.tex_materials[m].tex_Ka.b << std::endl; // defines the ambient color of the material to be (r,g,b).
m_fs << "Kd "<< tex_mesh.tex_materials[m].tex_Kd.r << " " << tex_mesh.tex_materials[m].tex_Kd.g << " " << tex_mesh.tex_materials[m].tex_Kd.b << std::endl; // defines the diffuse color of the material to be (r,g,b).
m_fs << "Ks "<< tex_mesh.tex_materials[m].tex_Ks.r << " " << tex_mesh.tex_materials[m].tex_Ks.g << " " << tex_mesh.tex_materials[m].tex_Ks.b << std::endl; // defines the specular color of the material to be (r,g,b). This color shows up in highlights.
m_fs << "d " << tex_mesh.tex_materials[m].tex_d << std::endl; // defines the transparency of the material to be alpha.
m_fs << "Ns "<< tex_mesh.tex_materials[m].tex_Ns << std::endl; // defines the shininess of the material to be s.
m_fs << "illum "<< tex_mesh.tex_materials[m].tex_illum << std::endl; // denotes the illumination model used by the material.
// illum = 1 indicates a flat material with no specular highlights, so the value of Ks is not used.
// illum = 2 denotes the presence of specular highlights, and so a specification for Ks is required.
m_fs << "map_Kd " << tex_mesh.tex_materials[m].tex_file << std::endl;
m_fs << "###" << std::endl;
}
m_fs.close ();
return (0);
}
bool getPixelCoordinates(const pcl::PointXYZ &pt, const pcl::TextureMapping<pcl::PointXYZ>::Camera &cam, pcl::PointXY &UV_coordinates)
{
if (pt.z > 0)
{
// compute image center and dimension
double sizeX = cam.width;
double sizeY = cam.height;
double cx, cy;
if (cam.center_w > 0)
cx = cam.center_w;
else
cx = sizeX / 2.0;
if (cam.center_h > 0)
cy = cam.center_h;
else
cy = sizeY / 2.0;
double focal_x, focal_y;
if (cam.focal_length_w > 0)
focal_x = cam.focal_length_w;
else
focal_x = cam.focal_length;
if (cam.focal_length_h > 0)
focal_y = cam.focal_length_h;
else
focal_y = cam.focal_length;
// project point on camera's image plane
UV_coordinates.x = static_cast<float> ((focal_x * (pt.x / pt.z) + cx)); //horizontal
UV_coordinates.y = static_cast<float> ((focal_y * (pt.y / pt.z) + cy)); //vertical
// point is visible!
if (UV_coordinates.x >= 15.0 && UV_coordinates.x <= (sizeX - 15.0) && UV_coordinates.y >= 15.0 && UV_coordinates.y <= (sizeY - 15.0))
{
return (true); // point was visible by the camera
}
}
// point is NOT visible by the camera
UV_coordinates.x = -1.0f;
UV_coordinates.y = -1.0f;
return (false); // point was not visible by the camera
}
bool isFaceProjected (const pcl::TextureMapping<pcl::PointXYZ>::Camera &camera, const pcl::PointXYZ &p1, const pcl::PointXYZ &p2, const pcl::PointXYZ &p3, pcl::PointXY &proj1, pcl::PointXY &proj2, pcl::PointXY &proj3)
{
return (getPixelCoordinates(p1, camera, proj1) && getPixelCoordinates(p2, camera, proj2) && getPixelCoordinates(p3, camera, proj3));
}
void getTriangleCircumscribedCircleCentroid( const pcl::PointXY &p1, const pcl::PointXY &p2, const pcl::PointXY &p3, pcl::PointXY &circumcenter, double &radius)
{
// compute centroid's coordinates (translate back to original coordinates)
circumcenter.x = static_cast<float> (p1.x + p2.x + p3.x ) / 3;
circumcenter.y = static_cast<float> (p1.y + p2.y + p3.y ) / 3;
double r1 = (circumcenter.x - p1.x) * (circumcenter.x - p1.x) + (circumcenter.y - p1.y) * (circumcenter.y - p1.y) ;
double r2 = (circumcenter.x - p2.x) * (circumcenter.x - p2.x) + (circumcenter.y - p2.y) * (circumcenter.y - p2.y) ;
double r3 = (circumcenter.x - p3.x) * (circumcenter.x - p3.x) + (circumcenter.y - p3.y) * (circumcenter.y - p3.y) ;
// radius
radius = std::sqrt( std::max( r1, std::max( r2, r3) )) ;
}
bool checkPointInsideTriangle(const pcl::PointXY &p1, const pcl::PointXY &p2, const pcl::PointXY &p3, const pcl::PointXY &pt)
{
// Compute vectors
Eigen::Vector2d v0, v1, v2;
v0(0) = p3.x - p1.x; v0(1) = p3.y - p1.y; // v0= C - A
v1(0) = p2.x - p1.x; v1(1) = p2.y - p1.y; // v1= B - A
v2(0) = pt.x - p1.x; v2(1) = pt.y - p1.y; // v2= P - A
// Compute dot products
double dot00 = v0.dot(v0); // dot00 = dot(v0, v0)
double dot01 = v0.dot(v1); // dot01 = dot(v0, v1)
double dot02 = v0.dot(v2); // dot02 = dot(v0, v2)
double dot11 = v1.dot(v1); // dot11 = dot(v1, v1)
double dot12 = v1.dot(v2); // dot12 = dot(v1, v2)
// Compute barycentric coordinates
double invDenom = 1.0 / (dot00*dot11 - dot01*dot01);
double u = (dot11*dot02 - dot01*dot12) * invDenom;
double v = (dot00*dot12 - dot01*dot02) * invDenom;
// Check if point is in triangle
return ((u >= 0) && (v >= 0) && (u + v < 1));
}

Wyświetl plik

@ -1,20 +0,0 @@
#pragma once
// STL
#include <iostream>
#include <fstream>
// PCL
#include <pcl/point_types.h>
#include <pcl/surface/texture_mapping.h>
#include <pcl/io/ply_io.h>
int saveOBJFile(const std::string &file_name, const pcl::TextureMesh &tex_mesh, unsigned precision);
bool getPixelCoordinates(const pcl::PointXYZ &pt, const pcl::TextureMapping<pcl::PointXYZ>::Camera &cam, pcl::PointXY &UV_coordinates);
bool isFaceProjected (const pcl::TextureMapping<pcl::PointXYZ>::Camera &camera, const pcl::PointXYZ &p1, const pcl::PointXYZ &p2, const pcl::PointXYZ &p3, pcl::PointXY &proj1, pcl::PointXY &proj2, pcl::PointXY &proj3);
void getTriangleCircumscribedCircleCentroid(const pcl::PointXY &p1, const pcl::PointXY &p2, const pcl::PointXY &p3, pcl::PointXY &circumcenter, double &radius);
bool checkPointInsideTriangle(const pcl::PointXY &p1, const pcl::PointXY &p2, const pcl::PointXY &p3, const pcl::PointXY &pt);

@ -1 +0,0 @@
Subproject commit b34cc5dcd54345272028b68f82fa4d0ae8a103c9

Wyświetl plik

@ -1 +0,0 @@
add_subdirectory(parallel)

Wyświetl plik

@ -1,26 +0,0 @@
set(URL http://ftp.gnu.org/gnu/parallel/parallel-20141022.tar.bz2)
set(FILE_NAME parallel.tar.bz2)
set(FILE_DIR ${CMAKE_CURRENT_DIR})
set(FILE_PATH ${FILE_DIR}/${FILE_NAME})
message(STATUS "downloading: ${FILE_NAME}")
file(DOWNLOAD ${URL} ${FILE_PATH}
EXPECTED_MD5 c01f53f9f6cc721a81591308f9e689c4
STATUS status
LOG log)
message(STATUS "downloading: ${FILE_NAME} - done")
message(STATUS "extracting ... ${FILE_PATH}")
if(NOT EXISTS "${FILE_PATH}")
message(FATAL_ERROR "error: file to extract does not exist: '${FILE_NAME}'")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} -E tar xjf ${FILE_PATH}
WORKING_DIRECTORY ${FILE_DIR}
RESULT_VARIABLE rv)
message(STATUS "extracting ... done")