Image Component Library (ICL)
PhysicsPaper3.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2014 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLPhysics/src/ICLPhysics/PhysicsPaper3.h **
10 ** Module : ICLPhysics **
11 ** Author : Christof Elbrechter, Matthias Esau **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 #pragma once
31 
32 #include <ICLPhysics/SoftObject.h>
33 #include <ICLCore/Img.h>
34 #include <ICLGeom/Camera.h>
35 #include <ICLUtils/Array2D.h>
37 #include <ICLGeom/PlaneEquation.h>
38 #include <ICLGeom/ViewRay.h>
39 #include <ICLUtils/Function.h>
41 
42 
43 #include <map>
44 #include <set>
45 
47 class btSoftBody;
50 namespace icl{
51  namespace physics {
52 
54  class PhysicsWorld;
57  class ICLPhysics_API PhysicsPaper3 : public SoftObject{
58  protected:
59  struct Data;
60  Data *m_data;
61 
62  struct ICLPhysics_API LinkState{
63  LinkState(bool isFirstOrder=true, bool isFold=false,
64  bool hasMemorizedRestDist=false, bool isOriginal=false):
65  isFirstOrder(isFirstOrder),isFold(isFold),
66  hasMemorizedRestDist(hasMemorizedRestDist),
67  isOriginal(isOriginal){}
68 
69  bool isFirstOrder;
70  bool isFold;
71  bool hasMemorizedRestDist;
72  bool isOriginal;
73 
74  LinkState *p() const { return new LinkState(*this); }
75 
76  static bool is_first_order(void *tag){
77  return ((LinkState*)tag)->isFirstOrder;
78  }
79  static bool is_fold(void *tag){
80  return ((LinkState*)tag)->isFold;
81  }
82  static bool has_memorized_rest_dist(void *tag){
83  return ((LinkState*)tag)->hasMemorizedRestDist;
84  }
85  static bool is_original(void *tag){
86  return ((LinkState*)tag)->isOriginal;
87  }
88  };
89 
90  public:
91 
92  typedef std::pair<utils::Point32f,utils::Point32f> LinkCoords;
93 
94  void updateSceneObject(btSoftBody *soft);
95 
97  bool enableSelfCollision,
98  const utils::Size &cellsInit,
99  const geom::Vec corners[4]=0,
100  const core::Img8u *front_texture=0,
101  const core::Img8u *back_texture=0,
102  float initialStiffness=-1,
103  float initialMaxLinkDistnace=0.5);
104 
105  PhysicsPaper3(PhysicsWorld *world, const PhysicsPaper3 &other);
106 
107  virtual ~PhysicsPaper3();
108 
109  PhysicsPaper3 *clone(PhysicsWorld *world) const;
110 
112 
117  void takeSoftBodyFrom(PhysicsPaper3 *other);
118 
119  void saveState(const std::string &filename);
120 
121  void restoreState(const std::string &filename);
122 
123  void setFaceAlpha(float alpha01);
124 
125  float getFaceAlpha() const;
126 
127  int getNumNodes() const;
128 
129  void splitAlongLine(const utils::Point32f &a, const utils::Point32f &b, const geom::Camera &currCam);
130 
131  void splitAlongLineInPaperCoords(const utils::Point32f &a, const utils::Point32f &b, bool autoExtendLineToEdges=true);
132 
133 
134  geom::Vec interpolatePosition(const utils::Point32f &p) const;
135 
137  utils::Point32f hit(const geom::ViewRay &ray) const;
138 
139 
143  float alpha;
144  };
145 
146  void movePosition(const utils::Point32f &coords, const geom::Vec &target, float streangth=1, float radius=0.1,
147  std::vector<NodeMovement> *dst=0);
148 
149 
150 
151  virtual void complexCustomRender(icl::geom::ShaderUtil* util);
152 
153 
154  SceneObject *approximateSurface(int nx=100, int ny=150) const;
155 
156  core::Img32f paperCoordinateTest(const geom::Camera &cam) const;
157 
159  void createBendingConstraints(float maxDistance, float fixedStiffness=-1);
160 
161  void setLinksVisible(bool visible);
162 
163  void simulateSelfCollision();
164 
165  static inline void free_link_state(void *p) { delete (LinkState*)p; }
166 
167  const core::Img32f &getFoldMap() const;
168 
169  void setFoldMapChangedCallback(utils::Function<void,const core::Img32f &> cb);
170 
171  void setFacesVisible(bool visible);
172 
173  virtual void lock();
174 
175  virtual void unlock();
176 
177  const std::vector<utils::Point32f> getTexCoords() const;
178 
180  utils::SmartPtr<LinkCoords> getLinkCoords(const utils::Point32f &pix, const geom::Camera &cam) const;
181 
183  void adaptFoldStiffness(const LinkCoords &coords, float stiffness, bool memorize=false);
184 
186  utils::VisualizationDescription getFoldLineHighlight(const LinkCoords &coords,
187  const geom::Camera &currCam) const;
188 
190  void setStraightenFolds(bool enabled);
191 
193  void setDoubleFolds(bool enabled);
194 
195 
196  struct ICLPhysics_API Structure{
197  struct Face { int a,b,c; };
198  std::vector<utils::Point32f> texCoords;
199  std::vector<geom::Vec> vertices;
200  std::vector<Face> faces;
201  geom::Vec interpolatePosition(const utils::Point32f &p) const;
202 
203  void deserializeFrom(std::istream &str);
204  void updateToSceneObject(SceneObject *obj);
205  utils::Point32f hit(const geom::ViewRay &ray) const;
206  };
207 
208  void serializeStructureTo(std::ostream &str);
209 
210  void setLinkColors(const geom::GeomColor &originalLinks,
211  const geom::GeomColor &insertedLinks,
212  const geom::GeomColor &creaseLines,
213  const geom::GeomColor &bendingConstraints);
214 
215  protected:
216 
217 
218 
219 
220  void updateCollisionClusters();
221 
222  void updateNodeAreas();
223  //void updateConstants();
224 
225  bool hitLink(btSoftBody::Link *l, const utils::Point32f &a, const utils::Point32f &b);
226 
227  bool hitTriangle(btSoftBody::Face *f, const utils::Point32f &a, const utils::Point32f &b);
228 
229  void addTriangle(int a, int b, int c);
230 
231  void addLink(int a, int b, float stiffness=1.0f, const LinkState &state=LinkState());
232 
233  void addVertexOrReuseOldOne(utils::Point32f &t, btVector3 &v, int &idx);
234 
235  bool replaceTriangle(btSoftBody::Face *f, const utils::Point32f &a, const utils::Point32f &b);
236 
237  void lockWorld();
238 
239  void unlockWorld();
240 
241  void updateSmoothNormalGraph();
242 
243  void computeSmoothNormals();
244  };
245  } // end of namespace physics
246 
247 } // end of namespace icl
The General Function Template.
Definition: Function.h:284
static bool is_first_order(void *tag)
Definition: PhysicsPaper3.h:76
Definition: PhysicsPaper3.h:140
undocument this line if you encounter any issues!
Definition: Any.h:37
static bool is_original(void *tag)
Definition: PhysicsPaper3.h:85
A physical world that handles physical objects.
Definition: PhysicsWorld.h:56
static bool has_memorized_rest_dist(void *tag)
Definition: PhysicsPaper3.h:82
Definition: PhysicsPaper3 (copy).h:29
Camera class.
Definition: Camera.h:132
std::pair< utils::Point32f, utils::Point32f > LinkCoords
Definition: PhysicsPaper3.h:92
LinkState * p() const
Definition: PhysicsPaper3.h:74
Size class of the ICL.
Definition: Size.h:61
Definition: ShaderUtil.h:47
#define ICLPhysics_API
Definition: CompatMacros.h:181
ICLQt_API void pix(ImgQ &image, int x, int y)
draw a single pixel into an image
std::string str(const T &t)
convert a data type into a string using an std::ostringstream instance
Definition: StringUtils.h:136
Abstract class for visualization tasks.
Definition: VisualizationDescription.h:73
This is a view-ray's line equation in parameter form.
Definition: ViewRay.h:49
geom::Vec curr
Definition: PhysicsPaper3.h:141
Single precission 3D Vectors Point class of the ICL.
Definition: Point32f.h:41
static bool is_fold(void *tag)
Definition: PhysicsPaper3.h:79
bool isOriginal
Definition: PhysicsPaper3.h:72
core::Color4D32f GeomColor
color for geometry primitives
Definition: GeomDefs.h:45
LinkState(bool isFirstOrder=true, bool isFold=false, bool hasMemorizedRestDist=false, bool isOriginal=false)
Definition: PhysicsPaper3.h:63
static void free_link_state(void *p)
Definition: PhysicsPaper3.h:165
float alpha
Definition: PhysicsPaper3.h:143
Specialization of the SmartPtrBase class for Pointers.
Definition: SmartPtr.h:75
Definition: PhysicsPaper3 (copy).h:34
geom::Vec target
Definition: PhysicsPaper3.h:142