Skip to content

Commit 9629116

Browse files
authored
Merge pull request #2085 from SCIInstitute/cleanup_constraints
Cleanup Constraints, added comments, docs, and a roadmap
2 parents db6c9f1 + ab75cbb commit 9629116

19 files changed

Lines changed: 119 additions & 772 deletions

.github/workflows/build-windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ jobs:
115115
# Note the current convention is to use the -S and -B options here to specify source
116116
# and build directories, but this is only available with CMake 3.13 and higher.
117117
# The CMake binaries on the Github Actions machines are (as of this writing) 3.12
118-
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
118+
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
119119

120120
- name: Build
121121
working-directory: "C:/build"

Libs/Optimize/Constraints/Constraint.cpp

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,6 @@
22
#include <iostream>
33
namespace shapeworks {
44

5-
void Constraint::updateZ(const Eigen::Vector3d &pt, double C) {
6-
/*double a = 2*C;
7-
double c = 2*mu + 2*C*ConstraintEval(pt);
8-
if(c >= 0){
9-
z = 0;
10-
//std::cout << "if z: " << z << std::endl;
11-
}
12-
else{
13-
double z1 = std::sqrt(-c/a);
14-
double z2 = -z1;
15-
//std::cout << "else z: " << z1 << std::endl;
16-
17-
double pteval = ConstraintEval(pt);
18-
19-
double res1 = mu*(pteval + z1*z1) + C/2*std::abs(pteval + z1*z1)*std::abs(pteval + z1*z1);
20-
double res2 = mu*(pteval + z2*z2) + C/2*std::abs(pteval + z2*z2)*std::abs(pteval + z2*z2);
21-
22-
if(res1 < res2){
23-
z = z1;
24-
}
25-
else{
26-
z = z2;
27-
}
28-
}*/
29-
30-
// Augmented lagrangian inequality equation: f(x) = mu*(g(x)+z^2) + C/2|g(x)+z^2|^2
31-
// f'(x) = mu*g'(x) + C*y' where by substitution
32-
// y = √(u^2) where by substitution
33-
// u = g(x) + z^2
34-
// u' = 2*z
35-
//
36-
// Then we compute y'
37-
// y' = (dy / du) (du / dx)
38-
// = (1/2)*(2 * u) / √(u^2) (du / dx)
39-
// = u * u' / | u |
40-
// = sgn(u) * u'
41-
//
42-
// So we substitute
43-
// f'(x) = 2*mu*z + 2*C*sgn(g(x)+z^2)*z
44-
45-
// z iterative update as explained above
46-
/*double update = 1000;
47-
size_t count = 1000;
48-
while(update < 0.1|| count > 0){
49-
std::cout << "pt: " << pt.transpose() << " count: " << count << " z: " << z << std::endl;
50-
update = 2*mu*z + 2*C*sgn(ConstraintEval(pt)+z*z)*z;
51-
z = z + update;
52-
count--;
53-
}*/
54-
// std::cout << "z: " << z << std::endl;
55-
}
56-
575
void Constraint::updateMu(const Eigen::Vector3d &pt, double C, size_t index) {
586
double eval = -constraintEval(pt);
597
double maxterm = mus_[index] + C * eval;

Libs/Optimize/Constraints/Constraint.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,48 @@
1010

1111
namespace shapeworks {
1212

13+
/**
14+
* \class Constraint
15+
* \ingroup Group-Constraints
16+
*
17+
* 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.
18+
* NOTE: Not actually using the augmented lagrangian. We are using quadratic penalty and not lagrangian because it works better.
19+
*
20+
*/
21+
1322
class Constraint {
1423
public:
24+
/// Returns if pt in vnl_vector format is violated by the constraint
1525
bool isViolated(const vnl_vector<double> &pt) const { return isViolated(Eigen::Vector3d(pt[0], pt[1], pt[2])); }
26+
/// Returns if pt in Eigen format is violated by the constraint
1627
virtual bool isViolated(const Eigen::Vector3d &pt) const = 0;
28+
/// Prints the constraint neatly
1729
virtual void print() const = 0;
1830

1931
// For augmented lagrangian
20-
void setZ(double inz) { z_ = inz; }
21-
double getZ() { return z_; }
32+
/// Initializes mu
2233
void setMus(std::vector<double> inmu) { mus_ = inmu; }
34+
/// Gets mu
2335
std::vector<double> getMus() { return mus_; }
24-
void setLambda(double inLambda) { lambda_ = inLambda; }
25-
double getLambda() { return lambda_; }
2636

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

30-
void updateZ(const Eigen::Vector3d &pt, double C);
31-
42+
/// Updates the value of mu according to the augmented lagrangian update
3243
void updateMu(const Eigen::Vector3d &pt, double C, size_t index);
3344

45+
/// 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.
3446
Eigen::Vector3d lagragianGradient(const Eigen::Vector3d &pt, double C, size_t index) const;
3547

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

3952
// For augmented lagrangian
53+
/// Mu is the lagrangian momentum term
4054
std::vector<double> mus_;
41-
double z_;
42-
double lambda_;
4355
};
4456

4557
} // namespace shapeworks

Libs/Optimize/Constraints/ConstraintType.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace shapeworks {
66
enum class ConstraintType : char {
7-
Sphere = 'S',
87
CuttingPlane = 'C',
98
FreeForm = 'F'
109
};

0 commit comments

Comments
 (0)