Skip to content

Commit

Permalink
Merge branch 'master' into 1881-deepssm-implement-variants-and-losses
Browse files Browse the repository at this point in the history
  • Loading branch information
zahidemon committed Jul 24, 2023
2 parents 7bd8848 + 38be981 commit 5985657
Show file tree
Hide file tree
Showing 32 changed files with 585 additions and 909 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ jobs:
# Note the current convention is to use the -S and -B options here to specify source
# and build directories, but this is only available with CMake 3.13 and higher.
# The CMake binaries on the Github Actions machines are (as of this writing) 3.12
run: conda activate shapeworks && cmake $GITHUB_WORKSPACE -DCMAKE_CXX_FLAGS="-FS" -DCMAKE_C_FLAGS="-FS" -DCMAKE_CXX_FLAGS_RELEASE="-FS /Zm250 /Zi /GL /MD /O2 /Ob3 /DNDEBUG /EHsc" -DCMAKE_C_FLAGS_RELEASE="-FS /Zi /GL /MD /O2 /Ob3 /DNDEBUG /EHsc" -DCMAKE_SHARED_LINKER_FLAGS_RELEASE="-LTCG /DEBUG" -DCMAKE_EXE_LINKER_FLAGS_RELEASE="-LTCG /DEBUG" -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DITK_DIR="C:\deps\lib\cmake\ITK-5.2" -DVTK_DIR="C:\deps\lib\cmake\vtk-9.1" -DXLNT_DIR="C:\deps" -DLIBIGL_DIR="C:\deps" -DJKQTCommonSharedLib_DIR="C:/deps/lib/cmake/JKQTCommonSharedLib" -DJKQTMathTextSharedLib_DIR="C:/deps/lib/cmake/JKQTMathTextSharedLib" -DJKQTPlotterSharedLib_DIR="C:/deps/lib/cmake/JKQTPlotterSharedLib" -DOpenVDB_DIR="C:\deps\lib\cmake\OpenVDB" -DGEOMETRYCENTRAL_DIR="C:\deps" -DACVD_DIR="C:\deps" -DBuild_Studio=ON -DGA_MEASUREMENT_ID=$GA_MEASUREMENT_ID -DGA_API_SECRET=$GA_API_SECRET
run: conda activate shapeworks && cmake $GITHUB_WORKSPACE -DCMAKE_CXX_FLAGS="-FS" -DCMAKE_C_FLAGS="-FS" -DCMAKE_CXX_FLAGS_RELEASE="-FS /Zm500 /Zi /GL /MD /O2 /Ob3 /DNDEBUG /EHsc" -DCMAKE_C_FLAGS_RELEASE="-FS /Zi /GL /MD /O2 /Ob3 /DNDEBUG /EHsc" -DCMAKE_SHARED_LINKER_FLAGS_RELEASE="-LTCG /DEBUG" -DCMAKE_EXE_LINKER_FLAGS_RELEASE="-LTCG /DEBUG" -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DITK_DIR="C:\deps\lib\cmake\ITK-5.2" -DVTK_DIR="C:\deps\lib\cmake\vtk-9.1" -DXLNT_DIR="C:\deps" -DLIBIGL_DIR="C:\deps" -DJKQTCommonSharedLib_DIR="C:/deps/lib/cmake/JKQTCommonSharedLib" -DJKQTMathTextSharedLib_DIR="C:/deps/lib/cmake/JKQTMathTextSharedLib" -DJKQTPlotterSharedLib_DIR="C:/deps/lib/cmake/JKQTPlotterSharedLib" -DOpenVDB_DIR="C:\deps\lib\cmake\OpenVDB" -DGEOMETRYCENTRAL_DIR="C:\deps" -DACVD_DIR="C:\deps" -DBuild_Studio=ON -DGA_MEASUREMENT_ID=$GA_MEASUREMENT_ID -DGA_API_SECRET=$GA_API_SECRET

- name: Build
working-directory: "C:/build"
Expand Down
6 changes: 5 additions & 1 deletion Applications/shapeworks/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,17 @@ void AnalyzeCommand::buildParser() {

parser.add_option("--name").action("store").type("string").set_default("").help("Path to project file.");
parser.add_option("--output").action("store").type("string").set_default("").help("Path to output file.");
parser.add_option("--range").action("store").type("float").set_default(3.0f).help("Standard deviation range for PCA [default: 3.0].");
parser.add_option("--steps").action("store").type("int").set_default(21).help("Number of steps to use for PCA [default: 21].");

Command::buildParser();
}

bool AnalyzeCommand::execute(const optparse::Values& options, SharedCommandData& sharedData) {
const std::string& projectFile(static_cast<std::string>(options.get("name")));
const std::string& outputFile(static_cast<std::string>(options.get("output")));
const float range = static_cast<float>(options.get("range"));
const int steps = static_cast<int>(options.get("steps"));

if (projectFile.length() == 0) {
std::cerr << "Must specify project name with --name <project.xlsx|.swproj>\n";
Expand Down Expand Up @@ -268,7 +272,7 @@ bool AnalyzeCommand::execute(const optparse::Values& options, SharedCommandData&
boost::filesystem::path dir(oldBasePath);
boost::filesystem::path file(outputFile);
boost::filesystem::path full_output = dir / file;
analyze.run_offline_analysis(full_output.string());
analyze.run_offline_analysis(full_output.string(), range, steps);

boost::filesystem::current_path(oldBasePath);

Expand Down
4 changes: 1 addition & 3 deletions Libs/Analyze/Analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Analyze::Analyze(ProjectHandle project) : project_(project), mesh_manager_(new M
}

//---------------------------------------------------------------------------
void Analyze::run_offline_analysis(std::string outfile) {
void Analyze::run_offline_analysis(std::string outfile, float range, float steps) {
SW_LOG("ShapeWorks Offline Analysis");
if (!project_->get_particles_present()) {
throw std::runtime_error("Project has not been optimized, please run optimize first");
Expand All @@ -140,8 +140,6 @@ void Analyze::run_offline_analysis(std::string outfile) {

json j;

double range = 2.0; // TODO: make this configurable
double steps = 11; // TODO: make this configurable
int half_steps = (steps / 2.0);
double increment = range / half_steps;

Expand Down
2 changes: 1 addition & 1 deletion Libs/Analyze/Analyze.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Analyze {
Analyze(ProjectHandle project);

/// Run offline analysis, saving results to outfile
void run_offline_analysis(std::string outfile);
void run_offline_analysis(std::string outfile, float range, float steps);

/// Return the list of shapes
ShapeList get_shapes();
Expand Down
52 changes: 0 additions & 52 deletions Libs/Optimize/Constraints/Constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,6 @@
#include <iostream>
namespace shapeworks {

void Constraint::updateZ(const Eigen::Vector3d &pt, double C) {
/*double a = 2*C;
double c = 2*mu + 2*C*ConstraintEval(pt);
if(c >= 0){
z = 0;
//std::cout << "if z: " << z << std::endl;
}
else{
double z1 = std::sqrt(-c/a);
double z2 = -z1;
//std::cout << "else z: " << z1 << std::endl;
double pteval = ConstraintEval(pt);
double res1 = mu*(pteval + z1*z1) + C/2*std::abs(pteval + z1*z1)*std::abs(pteval + z1*z1);
double res2 = mu*(pteval + z2*z2) + C/2*std::abs(pteval + z2*z2)*std::abs(pteval + z2*z2);
if(res1 < res2){
z = z1;
}
else{
z = z2;
}
}*/

// Augmented lagrangian inequality equation: f(x) = mu*(g(x)+z^2) + C/2|g(x)+z^2|^2
// f'(x) = mu*g'(x) + C*y' where by substitution
// y = √(u^2) where by substitution
// u = g(x) + z^2
// u' = 2*z
//
// Then we compute y'
// y' = (dy / du) (du / dx)
// = (1/2)*(2 * u) / √(u^2) (du / dx)
// = u * u' / | u |
// = sgn(u) * u'
//
// So we substitute
// f'(x) = 2*mu*z + 2*C*sgn(g(x)+z^2)*z

// z iterative update as explained above
/*double update = 1000;
size_t count = 1000;
while(update < 0.1|| count > 0){
std::cout << "pt: " << pt.transpose() << " count: " << count << " z: " << z << std::endl;
update = 2*mu*z + 2*C*sgn(ConstraintEval(pt)+z*z)*z;
z = z + update;
count--;
}*/
// std::cout << "z: " << z << std::endl;
}

void Constraint::updateMu(const Eigen::Vector3d &pt, double C, size_t index) {
double eval = -constraintEval(pt);
double maxterm = mus_[index] + C * eval;
Expand Down
28 changes: 20 additions & 8 deletions Libs/Optimize/Constraints/Constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,48 @@

namespace shapeworks {

/**
* \class Constraint
* \ingroup Group-Constraints
*
* This class is the general constraint class. Each instance represents a single constraint, either cutting-plane, sphere or free-form. They all inherit from this class. This class containts all the infrastructure to handle gradients and evaluations, which is shared among all constraint types.
* NOTE: Not actually using the augmented lagrangian. We are using quadratic penalty and not lagrangian because it works better.
*
*/

class Constraint {
public:
/// Returns if pt in vnl_vector format is violated by the constraint
bool isViolated(const vnl_vector<double> &pt) const { return isViolated(Eigen::Vector3d(pt[0], pt[1], pt[2])); }
/// Returns if pt in Eigen format is violated by the constraint
virtual bool isViolated(const Eigen::Vector3d &pt) const = 0;
/// Prints the constraint neatly
virtual void print() const = 0;

// For augmented lagrangian
void setZ(double inz) { z_ = inz; }
double getZ() { return z_; }
/// Initializes mu
void setMus(std::vector<double> inmu) { mus_ = inmu; }
/// Gets mu
std::vector<double> getMus() { return mus_; }
void setLambda(double inLambda) { lambda_ = inLambda; }
double getLambda() { return lambda_; }

/// Returns the gradient of the constraint
virtual Eigen::Vector3d constraintGradient(const Eigen::Vector3d &pt) const = 0;
/// Returns the evaluation on the constraint, i.e. the signed distance to the constraint boundary
virtual double constraintEval(const Eigen::Vector3d &pt) const = 0;

void updateZ(const Eigen::Vector3d &pt, double C);

/// Updates the value of mu according to the augmented lagrangian update
void updateMu(const Eigen::Vector3d &pt, double C, size_t index);

/// Computes the lagrangian gradient based on lagrangian inequality equations. NOTE: Not actually lagrangian. We are using quadratic penalty and not lagrangian because it works better.
Eigen::Vector3d lagragianGradient(const Eigen::Vector3d &pt, double C, size_t index) const;

protected:
/// Returns the sign of the double
int sgn(double val) { return (double(0) < val) - (val < double(0)); }

// For augmented lagrangian
/// Mu is the lagrangian momentum term
std::vector<double> mus_;
double z_;
double lambda_;
};

} // namespace shapeworks
1 change: 0 additions & 1 deletion Libs/Optimize/Constraints/ConstraintType.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace shapeworks {
enum class ConstraintType : char {
Sphere = 'S',
CuttingPlane = 'C',
FreeForm = 'F'
};
Expand Down
Loading

0 comments on commit 5985657

Please sign in to comment.