ShapeOp
 All Classes Files Functions Variables Typedefs Macros Pages
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>
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"
17 
19 namespace ShapeOp {
22 
23 class SHAPEOP_API Constraint {
24  public:
26  Constraint(const std::vector<int> &idI, Scalar weight);
27  virtual ~Constraint() {}
29  virtual void project(const Matrix3X &positions, Matrix3X &projections) const = 0;
31  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const = 0;
33  Scalar error(const Matrix3X &positions) const { return 0; }
34  protected:
36  std::vector<int> idI_;
38  Scalar weight_;
40  mutable int idO_;
41 };
43 
44 class SHAPEOP_API EdgeStrainConstraint : public Constraint {
45  public:
47  EdgeStrainConstraint(const std::vector<int> &idI,
48  Scalar weight,
49  const Matrix3X &positions,
50  Scalar rangeMin = 1.0,
51  Scalar rangeMax = 1.0);
52  virtual ~EdgeStrainConstraint() {}
54  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
56  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
58  void setEdgeLength(Scalar length);
59  private:
60  Scalar rest_;
61  Scalar rangeMin_;
62  Scalar rangeMax_;
63 };
65 
66 class SHAPEOP_API TriangleStrainConstraint : public Constraint {
67  public:
69  TriangleStrainConstraint(const std::vector<int> &idI,
70  Scalar weight,
71  const Matrix3X &positions,
72  Scalar rangeMin = 1.0,
73  Scalar rangeMax = 1.0);
74  virtual ~TriangleStrainConstraint() {}
76  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
78  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
79  private:
80  Matrix22 rest_;
81  Scalar rangeMin_;
82  Scalar rangeMax_;
83 };
85 
86 class SHAPEOP_API TetrahedronStrainConstraint : public Constraint {
87  public:
89  TetrahedronStrainConstraint(const std::vector<int> &idI,
90  Scalar weight,
91  const Matrix3X &positions,
92  Scalar rangeMin = 1.0,
93  Scalar rangeMax = 1.0);
94  virtual ~TetrahedronStrainConstraint() {}
96  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
98  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
99  private:
100  Matrix33 rest_;
101  Scalar rangeMin_;
102  Scalar rangeMax_;
103 };
105 
106 class SHAPEOP_API AreaConstraint : public Constraint {
107  public:
109  AreaConstraint(const std::vector<int> &idI,
110  Scalar weight,
111  const Matrix3X &positions,
112  Scalar rangeMin = 1.0,
113  Scalar rangeMax = 1.0);
114  virtual ~AreaConstraint() {}
116  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
118  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
119  private:
120  Matrix22 rest_;
121  Scalar rangeMin_;
122  Scalar rangeMax_;
123 };
125 
126 class SHAPEOP_API VolumeConstraint : public Constraint {
127  public:
129  VolumeConstraint(const std::vector<int> &idI,
130  Scalar weight,
131  const Matrix3X &positions,
132  Scalar rangeMin = 1.0,
133  Scalar rangeMax = 1.0);
134  virtual ~VolumeConstraint() {}
136  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
138  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
139  private:
140  Matrix33 rest_;
141  Scalar rangeMin_;
142  Scalar rangeMax_;
143 };
145 
146 class SHAPEOP_API BendingConstraint : public Constraint {
147  public:
149  BendingConstraint(const std::vector<int> &idI,
150  Scalar weight,
151  const Matrix3X &positions,
152  Scalar rangeMin = 1.0,
153  Scalar rangeMax = 1.0);
154  virtual ~BendingConstraint() {}
156  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
158  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
159  private:
160  VectorX w_;
161  Scalar n_;
162  Scalar rangeMin_;
163  Scalar rangeMax_;
164 };
166 class SHAPEOP_API ClosenessConstraint : public Constraint {
167  public:
169  ClosenessConstraint(const std::vector<int> &idI,
170  Scalar weight,
171  const Matrix3X &positions);
172  virtual ~ClosenessConstraint() {}
174  virtual void project(const Matrix3X & /*positions*/, Matrix3X &projections) const override final;
176  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
178  void setPosition(const Vector3 &position);
179  private:
180  Vector3 rest_;
181 };
183 
184 class SHAPEOP_API LineConstraint : public Constraint {
185  public:
187  LineConstraint(const std::vector<int> &idI,
188  Scalar weight,
189  const Matrix3X &positions);
190  virtual ~LineConstraint() {}
192  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
194  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
195  private:
196  mutable Matrix3X input;
197 };
199 
200 class SHAPEOP_API PlaneConstraint : public Constraint {
201  public:
203  PlaneConstraint(const std::vector<int> &idI,
204  Scalar weight,
205  const Matrix3X &positions);
206  virtual ~PlaneConstraint() {}
208  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
210  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
211  private:
212  mutable Matrix3X input;
213 };
215 
216 class SHAPEOP_API CircleConstraint : public Constraint {
217  public:
219  CircleConstraint(const std::vector<int> &idI,
220  Scalar weight,
221  const Matrix3X &positions);
222  virtual ~CircleConstraint() {}
224  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
226  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
227  private:
228  mutable Matrix3X input;
229 };
231 
232 class SHAPEOP_API SphereConstraint : public Constraint {
233  public:
235  SphereConstraint(const std::vector<int> &idI,
236  Scalar weight,
237  const Matrix3X &positions);
238  virtual ~SphereConstraint() {}
240  virtual void project(const Matrix3X &positions, Matrix3X &projections) const override final;
242  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
243  private:
244  mutable Matrix3X input;
245 };
247 class SHAPEOP_API UniformLaplacianConstraint : public Constraint {
248  public:
250  UniformLaplacianConstraint(const std::vector<int> &idI,
251  Scalar weight,
252  const Matrix3X &positions,
253  bool displacement_lap);
254  virtual ~UniformLaplacianConstraint() {}
256  virtual void project(const Matrix3X & /*positions*/, Matrix3X &projections) const override final;
258  virtual void addConstraint(std::vector<Triplet> &triplets, int &idO) const override final;
259  private:
260  Vector3 weighted_rest_;
261 };
263 } // namespace ShapeOp
265 #ifdef SHAPEOP_HEADER_ONLY
266 #include "Constraint.cpp"
267 #endif
268 #endif // CONSTRAINT_H
270 
Circle constraint. See for more details.
Definition: Constraint.h:216
Base class of any constraints. This class defines the interface of a ShapeOp constraint.
Definition: Constraint.h:23
Tetrahedron strain constraint. See for more details.
Definition: Constraint.h:86
Sphere constraint. See for more details.
Definition: Constraint.h:232
Triangle strain constraint. See for more details.
Definition: Constraint.h:66
std::vector< int > idI_
ids of the vertices involved in this constraint.
Definition: Constraint.h:36
Area constraint. See for more details.
Definition: Constraint.h:106
Plane constraint. See for more details.
Definition: Constraint.h:200
Scalar error(const Matrix3X &positions) const
Compute the constraint violation for the given input positions.
Definition: Constraint.h:33
int idO_
location of this constraint in the linear system.
Definition: Constraint.h:40
Definition: Constraint.h:166
Bending constraint. See for more details.
Definition: Constraint.h:146
Line constraint. See for more details.
Definition: Constraint.h:184
Scalar weight_
weight for the constraint.
Definition: Constraint.h:38
Definition: Constraint.h:247
Volume constraint. See for more details.
Definition: Constraint.h:126
Edge strain constraint. See for more details.
Definition: Constraint.h:44