ShapeOp  0.1.0
Constraint.h
Go to the documentation of this file.
1 // This file is part of ShapeOp, a lightweight C++ library
3 // for static and dynamic geometry processing.
4 //
5 // Copyright (C) 2014 Sofien Bouaziz <sofien.bouaziz@gmail.com>, Bailin Deng <bailin.deng@epfl.ch>, Mario Deuss <mario.deuss@epfl.ch>
6 // Copyright (C) 2014 LGG EPFL
7 //
8 // This Source Code Form is subject to the terms of the Mozilla
9 // Public License v. 2.0. If a copy of the MPL was not distributed
10 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 #ifndef CONSTRAINT_H
13 #define CONSTRAINT_H
14 #include "Types.h"
16 #include <memory>
17 #include <cmath>
18 
19 #ifndef M_PI
20 
21 #define M_PI 3.14159265358979323846
22 #endif
23 
26 namespace ShapeOp {
29 
31  public:
56  static std::shared_ptr<Constraint> shapeConstraintFactory(const std::string &ConstraintType, const std::vector<int> &idI, Scalar weight, const Matrix3X &positions);
61  Constraint(const std::vector<int> &idI, Scalar weight);
62  virtual ~Constraint() {}
67  virtual void project(const Matrix3X &positions, Matrix3X &projections) const = 0;
72  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const = 0;
74  std::size_t nIndices() const { return idI_.size(); }
75  protected:
77  std::vector<int> idI_;
81  mutable int idO_;
82 };
84 
86  public:
95  EdgeStrainConstraint(const std::vector<int> &idI,
96  Scalar weight,
97  const Matrix3X &positions,
98  Scalar rangeMin = 1.0,
99  Scalar rangeMax = 1.0);
100  virtual ~EdgeStrainConstraint() {}
102  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
104  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
106  void setEdgeLength(Scalar length);
108  void setRangeMin(Scalar rMin) { rangeMin_ = rMin; }
110  void setRangeMax(Scalar rMax) { rangeMax_ = rMax; }
111  private:
112  Scalar rest_;
113  Scalar rangeMin_;
114  Scalar rangeMax_;
115 };
117 
119  public:
127  TriangleStrainConstraint(const std::vector<int> &idI,
128  Scalar weight,
129  const Matrix3X &positions,
130  Scalar rangeMin = 1.0,
131  Scalar rangeMax = 1.0);
132  virtual ~TriangleStrainConstraint() {}
134  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
136  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
138  void setRangeMin(Scalar rMin) { rangeMin_ = rMin; }
140  void setRangeMax(Scalar rMax) { rangeMax_ = rMax; }
141  private:
142  Matrix22 rest_;
143  Scalar rangeMin_;
144  Scalar rangeMax_;
145 };
147 
149  public:
158  TetrahedronStrainConstraint(const std::vector<int> &idI,
159  Scalar weight,
160  const Matrix3X &positions,
161  Scalar rangeMin = 1.0,
162  Scalar rangeMax = 1.0);
163  virtual ~TetrahedronStrainConstraint() {}
165  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
167  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
169  void setRangeMin(Scalar rMin) { rangeMin_ = rMin; }
171  void setRangeMax(Scalar rMax) { rangeMax_ = rMax; }
172  private:
173  Matrix33 rest_;
174  Scalar rangeMin_;
175  Scalar rangeMax_;
176 };
178 
180  public:
189  AreaConstraint(const std::vector<int> &idI,
190  Scalar weight,
191  const Matrix3X &positions,
192  Scalar rangeMin = 1.0,
193  Scalar rangeMax = 1.0);
194  virtual ~AreaConstraint() {}
196  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
198  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
200  void setRangeMin(Scalar rMin) { rangeMin_ = rMin; }
202  void setRangeMax(Scalar rMax) { rangeMax_ = rMax; }
203  private:
204  Matrix22 rest_;
205  Scalar rangeMin_;
206  Scalar rangeMax_;
207 };
209 
211  public:
220  VolumeConstraint(const std::vector<int> &idI,
221  Scalar weight,
222  const Matrix3X &positions,
223  Scalar rangeMin = 1.0,
224  Scalar rangeMax = 1.0);
225  virtual ~VolumeConstraint() {}
227  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
229  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
231  void setRangeMin(Scalar rMin) { rangeMin_ = rMin; }
233  void setRangeMax(Scalar rMax) { rangeMax_ = rMax; }
234  private:
235  Matrix33 rest_;
236  Scalar rangeMin_;
237  Scalar rangeMax_;
238 };
240 
242  public:
259  BendingConstraint(const std::vector<int> &idI,
260  Scalar weight,
261  const Matrix3X &positions,
262  Scalar rangeMin = 1.0,
263  Scalar rangeMax = 1.0);
264  virtual ~BendingConstraint() {}
266  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
268  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
270  void setRangeMin(Scalar rMin) { rangeMin_ = rMin; }
272  void setRangeMax(Scalar rMax) { rangeMax_ = rMax; }
273  private:
274  VectorX w_;
275  Scalar n_;
276  Scalar rangeMin_;
277  Scalar rangeMax_;
278 };
280 
282  public:
288  ClosenessConstraint(const std::vector<int> &idI,
289  Scalar weight,
290  const Matrix3X &positions);
291  virtual ~ClosenessConstraint() {}
293  virtual void project(const Matrix3X & /*positions*/, Matrix3X &projections) const override final;
295  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
297  void setPosition(const Vector3 &position);
299  Vector3 getPosition() const;
300  private:
301  Vector3 rest_;
302 };
304 
306  public:
312  LineConstraint(const std::vector<int> &idI,
313  Scalar weight,
314  const Matrix3X &positions);
315  virtual ~LineConstraint() {}
317  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
319  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
320  private:
321  mutable Matrix3X input;
322 };
324 
326  public:
332  PlaneConstraint(const std::vector<int> &idI,
333  Scalar weight,
334  const Matrix3X &positions);
335  virtual ~PlaneConstraint() {}
337  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
339  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
340  private:
341  mutable Matrix3X input;
342 };
344 
346  public:
352  CircleConstraint(const std::vector<int> &idI,
353  Scalar weight,
354  const Matrix3X &positions);
355  virtual ~CircleConstraint() {}
357  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
359  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
360  private:
361  mutable Matrix3X input;
362 };
364 
366  public:
372  SphereConstraint(const std::vector<int> &idI,
373  Scalar weight,
374  const Matrix3X &positions);
375  virtual ~SphereConstraint() {}
377  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
379  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
380  private:
381  mutable Matrix3X input;
382 };
384 
386  public:
396  SimilarityConstraint(const std::vector<int> &idI,
397  Scalar weight,
398  const Matrix3X &positions,
399  bool scaling = true,
400  bool rotate = true,
401  bool flip = true);
402  virtual ~SimilarityConstraint() {}
404  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
406  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
410  void setShapes( const std::vector<Matrix3X> &shapes );
411  private:
412  void testCandidates(Scalar &min_error) const;
413  mutable Matrix3X input;
414  mutable Matrix3X candidate;
415  mutable Matrix3X output;
416  std::vector<Matrix3X> shapes_;
417  bool scaling_;
418  bool rotate_;
419  bool flip_;
420  mutable Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic> permutation_;
421  Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic> rotateMatrix_;
422  Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic> flipMatrix_;
423 };
425 
427  public:
433  RectangleConstraint(const std::vector<int> &idI,
434  Scalar weight,
435  const Matrix3X &positions);
436  virtual ~RectangleConstraint() {}
438  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
440  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
441 };
443 
445  public:
451  ParallelogramConstraint(const std::vector<int> &idI,
452  Scalar weight,
453  const Matrix3X &positions);
454  virtual ~ParallelogramConstraint() {}
456  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
458  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
459  private:
460  MatrixT<8, 8> parallelogramMatrix;
461 };
463 
465  public:
472  UniformLaplacianConstraint(const std::vector<int> &idI,
473  Scalar weight,
474  const Matrix3X &positions,
475  bool displacement_lap);
476  virtual ~UniformLaplacianConstraint() {}
478  virtual void project(const Matrix3X & /*positions*/, Matrix3X &projections) const override final;
480  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
481  private:
482  Vector3 weighted_rest_;
483 };
485 
487  public:
501  AngleConstraint(const std::vector<int> &idI,
502  Scalar weight,
503  const Matrix3X &positions,
504  Scalar minAngle = 0.0,
505  Scalar maxAngle = M_PI);
506  virtual ~AngleConstraint() {}
508  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
510  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
512  void setMinAngle(Scalar minAngle);
514  void setMaxAngle(Scalar maxAngle);
515  private:
516  Scalar minAngle_;
517  Scalar maxAngle_;
518  Scalar minAngleCos_;
519  Scalar maxAngleCos_;
520 };
522 } // namespace ShapeOp
524 #ifdef SHAPEOP_HEADER_ONLY
525 #include "Constraint.cpp"
526 #endif
527 #endif // CONSTRAINT_H
529 
MatrixT< 3, 1 > Vector3
A 3d column vector.
Definition: Types.h:39
#define M_PI
Definition: Constraint.h:21
Circle constraint. Constrains a set of vertices to lie on the same circle. See for more details...
Definition: Constraint.h:345
MatrixT< 2, 2 > Matrix22
A 2 by 2 matrix.
Definition: Types.h:37
Base class of any constraints. This class defines the interface of a ShapeOp constraint.
Definition: Constraint.h:30
A mesh-independent tetrahedron strain-limiting constraint. See for more details. ...
Definition: Constraint.h:148
Angle range constraint. Constrains the angle between the two lines formed by three points to a range...
Definition: Constraint.h:486
Sphere constraint. Constrains a set of vertices to lie on a sphere. See for more details...
Definition: Constraint.h:365
A mesh-independent triangle strain-limiting constraint. See for more details.
Definition: Constraint.h:118
MatrixT< Eigen::Dynamic, 1 > VectorX
A nd column vector.
Definition: Types.h:47
std::vector< int > idI_
ids of the vertices involved in this constraint.
Definition: Constraint.h:77
Similarity or Rigid constraint. Perserves the relative location of a set of vertices. See for more details.
Definition: Constraint.h:385
Area constraint. Limits the area of a triangle to a range. See for more details. ...
Definition: Constraint.h:179
void setRangeMin(Scalar rMin)
Set a new range minimum.
Definition: Constraint.h:108
MatrixT< 3, Eigen::Dynamic > Matrix3X
A 3 by n matrix.
Definition: Types.h:45
void setRangeMax(Scalar rMax)
Set a new range maximum.
Definition: Constraint.h:171
std::size_t nIndices() const
Number of indices of vertices involved in the constraint.
Definition: Constraint.h:74
Plane constraint. Constrains a set of vertices to lie on the same plane. See for more details...
Definition: Constraint.h:325
void setRangeMin(Scalar rMin)
Set a new range minimum.
Definition: Constraint.h:270
void setRangeMax(Scalar rMax)
Set a new range maximum.
Definition: Constraint.h:272
void setRangeMin(Scalar rMin)
Set a new range minimum.
Definition: Constraint.h:138
void setRangeMax(Scalar rMax)
Set a new range maximum.
Definition: Constraint.h:202
int idO_
location of this constraint in the linear system.
Definition: Constraint.h:81
Closeness constraint. Constrains a vertex to a position in space. Projects onto a given rest position...
Definition: Constraint.h:281
Namespace of the ShapeOp library.
Definition: Constraint.cpp:18
Bending constraint. Limits the bending between two neighboring triangles. See for more details...
Definition: Constraint.h:241
void setRangeMax(Scalar rMax)
Set a new range maximum.
Definition: Constraint.h:140
void setRangeMin(Scalar rMin)
Set a new range minimum.
Definition: Constraint.h:200
ParallelogramConstraint constraint. Constrains the four vertices of a quad to be a parallelogram...
Definition: Constraint.h:444
Eigen::Matrix< Scalar, Rows, Cols, Options > MatrixT
A typedef of the dense matrix of Eigen.
Definition: Types.h:35
ShapeOpScalar Scalar
A scalar type, double or float, as defined in ShapeOpScalar in Common.h.
Definition: Types.h:32
MatrixT< 3, 3 > Matrix33
A 3 by 3 matrix.
Definition: Types.h:41
#define SHAPEOP_API
Defines the API prefix for the current platform.
Definition: Common.h:34
void setRangeMax(Scalar rMax)
Set a new range maximum.
Definition: Constraint.h:110
void setRangeMax(Scalar rMax)
Set a new range maximum.
Definition: Constraint.h:233
Line constraint. Constrains a set of vertices to lie on the same line. See for more details...
Definition: Constraint.h:305
Scalar weight_
weight for the constraint.
Definition: Constraint.h:79
Uniform Laplacian Constraint. Minimizes the uniform laplacian respectively the uniform laplacian of d...
Definition: Constraint.h:464
Volume constraint. Limits the volume of a tetrahedron to a range. See for more details.
Definition: Constraint.h:210
Edge strain constraint. Constrains the distance between two points to a range. See for more details...
Definition: Constraint.h:85
void setRangeMin(Scalar rMin)
Set a new range minimum.
Definition: Constraint.h:169
Rectangle constraint. Constrains the four vertices of a quad to be a rectangle. See for more details...
Definition: Constraint.h:426
void setRangeMin(Scalar rMin)
Set a new range minimum.
Definition: Constraint.h:231