Skip to content

Commit

Permalink
Merge branch 'master' into telemetry-flask
Browse files Browse the repository at this point in the history
  • Loading branch information
akenmorris committed Feb 15, 2023
2 parents 5a4c190 + a9f6733 commit 2ef4874
Show file tree
Hide file tree
Showing 43 changed files with 471 additions and 279 deletions.
42 changes: 40 additions & 2 deletions Applications/shapeworks/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ bool Example::execute(const optparse::Values &options, SharedCommandData &shared
}
#endif

static void setup_callbacks(bool show_progress, bool xml_status) {
if (show_progress) {
auto progress_callback = [](double progress, std::string message) {
// show status message and percentage complete
std::cout << fmt::format("{} ({:.1f}%) \r", message, progress);
std::cout.flush();
};
Logging::Instance().set_progress_callback(progress_callback);
}

if (xml_status) {
auto progress_callback = [](double progress, std::string message) {
// print status message and percentage complete
std::cout << fmt::format("<xml><status>{}</status><progress>{:.1f}</progress></xml>\n", message, progress);
std::cout.flush();
};
Logging::Instance().set_progress_callback(progress_callback);

auto error_callback = [](std::string message) {
std::cout << fmt::format("<xml><error>{}</error></xml>\n", message);
std::cout.flush();
};
Logging::Instance().set_error_callback(error_callback);
}
}

///////////////////////////////////////////////////////////////////////////////
// Seed
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -75,12 +101,16 @@ void OptimizeCommand::buildParser() {
parser.prog(prog).description(desc);

parser.add_option("--name").action("store").type("string").set_default("").help("Path to project file.");
parser.add_option("--progress").action("store_true").set_default(false).help("Show progress [default: false].");
parser.add_option("--xmlconsole").action("store_true").set_default(false).help("XML console output [default: false].");

Command::buildParser();
}

bool OptimizeCommand::execute(const optparse::Values& options, SharedCommandData& sharedData) {
const std::string& projectFile(static_cast<std::string>(options.get("name")));
bool show_progress = static_cast<bool>(options.get("progress"));
bool xml_status = static_cast<bool>(options.get("xmlconsole"));

if (projectFile.length() == 0) {
std::cerr << "Must specify project name with --name <project.xlsx|.swproj>\n";
Expand All @@ -90,6 +120,8 @@ bool OptimizeCommand::execute(const optparse::Values& options, SharedCommandData
bool isProject = StringUtils::hasSuffix(projectFile, "xlsx") || StringUtils::hasSuffix(projectFile, "swproj");

Optimize app;
setup_callbacks(show_progress, xml_status);

if (isProject) {
try {
// load spreadsheet project
Expand Down Expand Up @@ -117,7 +149,7 @@ bool OptimizeCommand::execute(const optparse::Values& options, SharedCommandData

return success;
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
SW_ERROR(e.what());
return false;
}
} else {
Expand All @@ -136,18 +168,24 @@ void GroomCommand::buildParser() {
parser.prog(prog).description(desc);

parser.add_option("--name").action("store").type("string").set_default("").help("Path to project file.");
parser.add_option("--progress").action("store_true").set_default(false).help("Show progress [default: false].");
parser.add_option("--xmlconsole").action("store_true").set_default(false).help("XML console output [default: false].");

Command::buildParser();
}

bool GroomCommand::execute(const optparse::Values& options, SharedCommandData& sharedData) {
const std::string& projectFile(static_cast<std::string>(options.get("name")));
bool show_progress = static_cast<bool>(options.get("progress"));
bool xml_status = static_cast<bool>(options.get("xmlconsole"));

if (projectFile.length() == 0) {
std::cerr << "Must specify project name with --name <project.xlsx|.swproj>\n";
return false;
}

setup_callbacks(show_progress, xml_status);

try {
ProjectHandle project = std::make_shared<Project>();
project->load(projectFile);
Expand All @@ -168,7 +206,7 @@ bool GroomCommand::execute(const optparse::Values& options, SharedCommandData& s
}
return success;
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
SW_ERROR(e.what());
return false;
}
}
Expand Down
8 changes: 5 additions & 3 deletions Libs/Analyze/MeshGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ MeshHandle MeshGenerator::build_mesh_from_points(const Eigen::VectorXd& shape, i
if (!poly_data) {
std::string message = "Unable to warp mesh";
SW_ERROR(message);
poly_data = vtkSmartPointer<vtkPolyData>::New();
mesh->set_poly_data(poly_data);
mesh->set_error_message(message);
return mesh;
}
Expand Down Expand Up @@ -122,9 +124,9 @@ MeshHandle MeshGenerator::build_mesh_from_image(ImageType::Pointer image, float
mesh->set_poly_data(Mesh(marching->GetOutput()).clean().computeNormals().getVTKMesh());

} catch (itk::ExceptionObject& excep) {
std::cerr << "Exception caught!" << std::endl;
std::cerr << excep << std::endl;
mesh->set_error_message(std::string("Exception: ") + excep.what());
auto message = std::string("Exception: ") + excep.what();
SW_ERROR(message);
mesh->set_error_message(message);
}
return mesh;
}
Expand Down
2 changes: 1 addition & 1 deletion Libs/Analyze/MeshManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class MeshManager : public QObject {
void error_encountered(QString message);

void progress(int);
void status(QString);
void status(std::string);

private:
std::shared_ptr<MeshReconstructors> reconstructors_ = std::make_shared<MeshReconstructors>();
Expand Down
36 changes: 24 additions & 12 deletions Libs/Common/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace spd = spdlog;
namespace shapeworks {

//-----------------------------------------------------------------------------
static std::string create_header(const int line, const char *filename) {
const char *name = (strrchr(filename, '/') ? strrchr(filename, '/') + 1 : filename);
const char *name2 = (strrchr(name, '\\') ? strrchr(name, '\\') + 1 : name);
static std::string create_header(const int line, const char* filename) {
const char* name = (strrchr(filename, '/') ? strrchr(filename, '/') + 1 : filename);
const char* name2 = (strrchr(name, '\\') ? strrchr(name, '\\') + 1 : name);
std::string header = "[" + std::string(name2) + "|" + std::to_string(line) + "]";
return header;
}
Expand Down Expand Up @@ -53,7 +53,7 @@ bool Logging::check_log_open() const { return log_open_; }
std::string Logging::get_log_filename() const { return log_filename_; }

//-----------------------------------------------------------------------------
void Logging::log_message(const std::string& message, const int line, const char *file) const {
void Logging::log_message(const std::string& message, const int line, const char* file) const {
spd::info(message);
if (log_open_) {
spd::get("file")->info(message);
Expand All @@ -72,7 +72,7 @@ void Logging::log_stack(const std::string& message) const {
}

//-----------------------------------------------------------------------------
void Logging::log_error(const std::string& message, const int line, const char *file) const {
void Logging::log_error(const std::string& message, const int line, const char* file) const {
spd::error(message);
if (log_open_) {
spd::get("file")->error(message);
Expand All @@ -83,23 +83,22 @@ void Logging::log_error(const std::string& message, const int line, const char *
}

//-----------------------------------------------------------------------------
void Logging::show_message(const std::string& message, const int line, const char *file) const {
void Logging::show_message(const std::string& message, const int line, const char* file) const {
log_message(message, line, file);
if (message_callback_) {
message_callback_(message);
}
}

//-----------------------------------------------------------------------------
void Logging::show_status(const std::string& message, const int line, const char *file) const {
log_message(message, line, file);
if (message_callback_) {
message_callback_(message);
void Logging::show_status(const std::string& message, const int line, const char* file) const {
if (status_callback_) {
status_callback_(message);
}
}

//-----------------------------------------------------------------------------
void Logging::log_debug(const std::string& message, const int line, const char *file) const {
void Logging::log_debug(const std::string& message, const int line, const char* file) const {
std::string str = create_header(line, file) + " " + message;
spd::debug(str);
if (log_open_) {
Expand All @@ -113,7 +112,7 @@ void Logging::log_debug(const std::string& message, const int line, const char *
}

//-----------------------------------------------------------------------------
void Logging::log_warning(const std::string& message, const int line, const char *file) const {
void Logging::log_warning(const std::string& message, const int line, const char* file) const {
spd::warn(message);
if (log_open_) {
spd::get("file")->warn(message);
Expand All @@ -123,6 +122,13 @@ void Logging::log_warning(const std::string& message, const int line, const char
}
}

//-----------------------------------------------------------------------------
void Logging::show_progress(double value, const std::string& message) {
if (progress_callback_) {
progress_callback_(value, message);
}
}

//-----------------------------------------------------------------------------
void Logging::close_log() {
if (!log_open_) {
Expand All @@ -145,4 +151,10 @@ void Logging::set_warning_callback(const std::function<void(std::string)>& callb
//-----------------------------------------------------------------------------
void Logging::set_debug_callback(const std::function<void(std::string)>& callback) { debug_callback_ = callback; }

//-----------------------------------------------------------------------------
void Logging::set_status_callback(const std::function<void(std::string)>& callback) { status_callback_ = callback; }

//-----------------------------------------------------------------------------
void Logging::set_progress_callback(const std::function<void(double, std::string)>& callback) { progress_callback_ = callback; }

} // namespace shapeworks
30 changes: 27 additions & 3 deletions Libs/Common/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

#include <spdlog/fmt/fmt.h>

#include <functional>

#include <QString>
#include <functional>

template <>
struct fmt::formatter<QString> {
Expand Down Expand Up @@ -102,6 +101,9 @@ class Logging {
//! Log a message, use SW_STATUS macro
void show_status(const std::string& message, const int line, const char* file) const;

//! Display progress (0-100)
void show_progress(double value, const std::string& message);

//! Log a debug message, use SW_DEBUG macro
void log_debug(const std::string& message, const int line, const char* file) const;

Expand All @@ -120,9 +122,15 @@ class Logging {
//! Set a warning callback function to be called whenever a warning is posted
void set_warning_callback(const std::function<void(std::string)>& callback);

//! Set a debug messagecallback function to be called whenever a debug message is posted
//! Set a debug message callback function to be called whenever a debug message is posted
void set_debug_callback(const std::function<void(std::string)>& callback);

//! Set a status callback function to be called whenever a status message is posted
void set_status_callback(const std::function<void(std::string)>& callback);

//! Set a progress callback function to be called whenever a progress update is posted
void set_progress_callback(const std::function<void(double, std::string)>& callback);

private:
//! Constructor
Logging();
Expand All @@ -137,6 +145,10 @@ class Logging {
std::function<void(std::string)> warning_callback_;

std::function<void(std::string)> debug_callback_;

std::function<void(std::string)> status_callback_;

std::function<void(double, std::string)> progress_callback_;
};

//! Log stack macro
Expand Down Expand Up @@ -169,7 +181,19 @@ class Logging {
#define SW_STATUS(message, ...) \
shapeworks::Logging::Instance().show_status(fmt::format(message, ##__VA_ARGS__), __LINE__, __FILE__)

#define SW_PROGRESS(value, message, ...) \
shapeworks::Logging::Instance().show_progress(value, fmt::format(message, ##__VA_ARGS__));

//! Close session macro
#define SW_CLOSE_LOG() shapeworks::Logging::Instance().close_log();

//! Log once macro, will only log the message once
#define SW_LOG_ONCE(message, ...) \
{ \
static bool logged = false; \
if (!logged) { \
SW_LOG(message, ##__VA_ARGS__); \
logged = true; \
} \
}
} // namespace shapeworks
3 changes: 2 additions & 1 deletion Libs/Groom/Groom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vtkCenterOfMass.h>
#include <vtkLandmarkTransform.h>
#include <vtkPointSet.h>
#include <Logging.h>

#include <boost/filesystem.hpp>
#include <vector>
Expand Down Expand Up @@ -470,7 +471,7 @@ void Groom::increment_progress(int amount) {
std::scoped_lock lock(mutex);
this->progress_counter_ += amount;
this->progress_ = static_cast<float>(this->progress_counter_) / static_cast<float>(this->total_ops_) * 100.0;
this->update_progress();
SW_PROGRESS(progress_, fmt::format("Grooming ({}/{} ops)", progress_counter_, total_ops_));
}

//---------------------------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions Libs/Groom/Groom.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class Groom {
vtkSmartPointer<vtkPoints> target);

protected:
//! call to be overridden by subclasses
virtual void update_progress(){};

std::atomic<float> progress_ = 0;
std::atomic<int> total_ops_ = 0;
Expand Down
6 changes: 3 additions & 3 deletions Libs/Mesh/MeshWarper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ vtkSmartPointer<vtkPolyData> MeshWarper::build_mesh(const Eigen::MatrixXd& parti
double* p = poly_data->GetPoint(i);
if (std::isnan(p[0]) || std::isnan(p[1]) || std::isnan(p[2])) {
this->warp_available_ = false; // failed
std::cerr << "Reconstruction Failed\n";
SW_ERROR("Reconstruction failed. NaN detected in mesh.");
return nullptr;
}
}
Expand Down Expand Up @@ -438,7 +438,7 @@ bool MeshWarper::generate_warp() {
Eigen::MatrixXd vertices = referenceMesh.points();
this->faces_ = referenceMesh.faces();

// perform warp
// generate the warp
if (!MeshWarper::generate_warp_matrix(vertices, this->faces_, this->vertices_, this->warp_)) {
this->update_progress(1.0);
this->warp_available_ = false;
Expand Down Expand Up @@ -473,7 +473,7 @@ bool MeshWarper::generate_warp_matrix(Eigen::MatrixXd TV, Eigen::MatrixXi TF, co
// faster and looks OK
const int k = 2;
if (!igl::biharmonic_coordinates(TV, TF, S, k, W)) {
std::cerr << "igl:biharmonic_coordinates failed\n";
SW_ERROR("Mesh Warp Error: igl:biharmonic_coordinates failed");
return false;
}
// Throw away interior tet-vertices, keep weights and indices of boundary
Expand Down
1 change: 0 additions & 1 deletion Libs/Optimize/Constraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,6 @@ bool Constraints::hasConstraints() {
return true;
}

SW_LOG("no constraints");
return false;
}

Expand Down
Loading

0 comments on commit 2ef4874

Please sign in to comment.