Structural deformable models
Model.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _GEOMETRY_H_
3 #define _GEOMETRY_H_
4 
5 #include <vector>
6 #include <list>
7 
8 #include "Node.h"
9 #include "Edge.h"
10 #include "VVector.h"
11 #include "ParseFile.h"
12 #include "DMatrix.h"
13 #include "PropVec.h"
14 
15 //-----------------------------------------------------------------------------
16 #define __SHAPEWEIGHT 0.1
17 
18 //-----------------------------------------------------------------------------
19 typedef std::vector<Node> NodeArray;
20 typedef std::vector<Edge> EdgeArray;
21 
22 //--- for spring model
23 typedef struct {
27 
28 //---
29 
30 class CAMgraphicsProcess;
31 class SensorCollection;
32 
33 class Model {
34 protected:
35  enum PreCompF {PC_CENTER=0x01, PC_SCALE=0x02, PC_DIR=0x04, PC_QOF=0x08,
36  PC_LIVE=0x10, PC_NOTHING=0x00, PC_ALL=0x1f,
39 public:
42  //enum TTimeStamp { TS_CREATE=0, TS_WIN, TS_LOOSE, TS_LAST};
46  Model(dataset_cptr _dataset=NULL, SensorCollection *sensors=NULL);
48  Model(const Model& rhs);
50  ~Model();
51 
53  void clear();
55  operator bool() const { return !nodes.empty(); }
57  void reset();
60  int addNode(const Node &node);
63  int addEdge(const Edge &edge);
66  int addEdge(const Node &from, const Node &to);
69  int addEdge(int ifrom, int ito);
70 
71  const Edge& getEdge(int index) const { return edges[index]; }
72  Edge& getEdge(int index) { return edges[index]; }
73 
74  const Node& getNode(int index) const { return nodes[index]; }
75  Node& getNode(int index) { return nodes[index]; }
76 
77  int getNNodes() const { return nodes.size(); }
78  int getNEdges() const { return edges.size(); }
79  float getNodeAngle(const Node &n) const;
80  void setName(const std::string &name);
81  const std::string& getName() const { return m_Name; }
82  dword getCRC() const { return m_CRC; }
83  dword getID() const { return m_ID; }
84  void setID(dword id) { m_ID = id; }
85 
87  void draw(bool drawPoints=false) const;
89  void drawPS(CAMgraphicsProcess& gp, dword mode=0) const;
90 
91  // stream operation
93  bool readEdge(ParseFile &is);
95  bool readNode(ParseFile &is);
97  bool readSensor(ParseFile &is);
99  bool readParameter(ParseFile &is);
107  friend ParseFile& operator>>(ParseFile &is, Model &g);
109  friend std::ostream& operator<<(std::ostream &is, const Model &g);
110  Model& operator=(const Model& rhs);
112  bool readFile(const char* filename, bool fullread=true);
114  bool writeFile(const char* filename) const;
115  void attachDataset(dataset_cptr dataset);
116 
117  float getQualityOfFit() const;
118  float getLiveliness() const;
119  void translate(const Point &t);
120  void addNoise(float r);
121  void freeze();
122  void push(const Point &t);
123  void rotate(float angle, const Point &c);
124  void rotate(float angle) {
126  void pushRotate(const Point &c, float angle);
127  void attract(const Point &c, float factor);
129  float getFullLength() const;
131  float getStdRadius(const Point& center) const;
132  float getStdRadius() const { return getStdRadius(getCenter());}
134  float getMaxRadius(const Point& center) const;
135  float getMaxRadius() const { return getMaxRadius(getCenter());}
136 
138  void scale(float factor, bool movepoints=false);
140  void scaleSel(float factor, const DMatrixf& selm);
142  void adaptRestLength(float ratio);
144  void adaptRestLengthSel(float ratio, const DMatrixf& selm);
146  void adaptProportion(float ratio);
148  float getLengthRatio() const;
150  float getLengthVariation() const;
152  float getDeformation() const;
154  float getSensorFit(dword* npts=NULL) const;
156  float getEdgeSensorFit(dword* npts=NULL) const;
157 
158 // functions for spring model calculations
159  void updateParticles(float dt,int method=0);
160  void resetForces();
161  void calculateForces(float dt);
162  void calculateDerivatives(std::vector<NodeDerivative> &deriv);
163  float addImageForces(float dt);
164  void addTorque(float dt);
165  void prepareTorque();
166 
170  void reattachSensors();
172  dword createEdgeSensors(float dist=0.1);
173 
174  const Point getCenter() const;
175  float getDirection() const;
176  const Node& getDirNode() const;
177  void setDirNode(int id) { m_DirNodeInd = id; invalidatePC(PC_DIR); }
178 
179  void setWinner(bool iswin=true, float ts=0.) {
180  if(iswin) m_Flags |= ST_WINNER;
181  else m_Flags &= ~ST_WINNER;
182  //if(ts!=0.) m_TimeStamp[TS_WIN]=ts;
183  }
184  bool isWinner() { return m_Flags&ST_WINNER; }
185  void setLooser(bool isloose=true, float ts=0.) {
186  if(isloose) m_Flags |= ST_LOOSER;
187  else m_Flags &= ~ST_LOOSER;
188  //if(ts!=0.) m_TimeStamp[TS_LOOSE]=ts;
189  }
190  bool isLooser() { return m_Flags&ST_LOOSER; }
191  void setOldState(bool isoldstate=true, float ts=0.) {
192  if(isoldstate) m_Flags |= ST_OLDSTATE;
193  else m_Flags &= ~ST_OLDSTATE;
194  }
195  bool isOldState() { return m_Flags&ST_OLDSTATE; }
196  dword& getFlags() { return m_Flags; }
197  bool hasFlags(dword flags) const { return m_Flags & flags; }
198  dword& setFlags(dword flags) { return m_Flags|=flags; }
199  dword& unsetFlags(dword flags) { return m_Flags&=~flags; }
200  dword& switchFlags(dword flags) { return m_Flags^=flags; }
201  void setShapeWeight(float weight) {
202  m_ShapeWeight = weight; invalidatePC(PC_QOF); }
203  void adaptDataScale(dword ppmm=0);
204  dword getDataScale() const { return m_ppmm; }
205 
206  void adaptProperties(const PropVec &prop);
207  const PropVec& getProperties() const;
209  PropVec getPropertiesMM() const;
211  void adaptPropertiesMM(PropVec prop);
212  Point2D& convertPointFromMM(Point2D& pt) const;
213  Point2D& convertPointToMM(Point2D& pt) const;
214  PropVec& convertPropFromMM(PropVec& prop) const;
215  PropVec& convertPropToMM(PropVec& prop) const;
216 
217  int nearestNode(const Point& pos, float& dist=*(float*)(NULL)) const;
218  float distance(const Model& rhs,
219  enum Model::DistType kind=DIST_POINTS) const;
220  void invalidatePC(dword pcflags=PC_ALL) { m_PCFlags &= ~pcflags; }
221 
222  dword getInstCount() const { return m_InstCount; }
223  void setInstCount(dword ic) { m_InstCount=ic; }
224  dword mergeModel( Model& mergemod);
225  const std::string& getFilename() const { return m_Filename; }
226 
229  void setHLNode(int hlnode);
231  int getHLNode() const { return m_HLNode; }
234 
238  dword removeEdges();
240  void removeNode(int nid);
242  DMatrixf adjMat() const;
244  DMatrixf selMaskMat() const;
245 
246 protected:
250  dword createByAdjMat(const DMatrixf& adjm);
252  void rebuildIndexTables();
255 
256 public:
258  //std::vector<float> m_TimeStamp; // time stamps (see. TTimeStamp)
259  float m_TimeStamp;
260 
261 protected:
264 
267  std::string m_SensorFile;
270  std::string m_Name;
273  mutable int m_DirNodeInd;
274  mutable dword m_PCFlags;
276  mutable float m_QOF;
277  mutable float m_Liveliness;
278  mutable float m_Scale;
279  mutable float m_Direction;
281  std::string m_Filename;
282  //const static PropVec s_PropWeights;
283  mutable int m_HLNode;
284  mutable dword m_ID;
285  //std::vector<NodeDerivative> m_Deriv;
286  //float m_LastDT;
287 };
288 
289 #endif
float getLiveliness() const
Definition: Model.cpp:1222
float m_QOF
Definition: Model.h:276
std::vector< Edge > EdgeArray
Definition: Model.h:20
#define NULL
Definition: simpletypes.h:9
Point dvdt
Definition: Model.h:25
dataset_cptr m_Dataset
Definition: Model.h:265
bool writeFile(const char *filename) const
Definition: Model.cpp:381
PropVec & convertPropFromMM(PropVec &prop) const
Definition: Model.cpp:1297
float getQualityOfFit() const
Definition: Model.cpp:1204
ModelState
Definition: Model.h:43
void prepareTorque()
Definition: Model.cpp:713
std::string m_SensorFile
Definition: Model.h:267
int getNEdges() const
Definition: Model.h:78
PropVec & convertPropToMM(PropVec &prop) const
Definition: Model.cpp:1304
void attachDataset(dataset_cptr dataset)
Definition: Model.cpp:85
void rotate(float angle, const Point &c)
Definition: Model.cpp:946
void addTorque(float dt)
Definition: Model.cpp:719
Implements a Node used by Model and Edge.
Definition: Node.h:13
void calculateForces(float dt)
Definition: Model.cpp:648
float getEdgeSensorFit(dword *npts=NULL) const
get average fit of sensors along edges
Definition: Model.cpp:1191
int getHLNode() const
Returns ID of highlighted node. Value is -1 if none is selected.
Definition: Model.h:231
void mergeSensorCollection(SensorCollection *sensors)
Definition: Model.cpp:596
friend ParseFile & operator>>(ParseFile &is, Model &g)
Definition: Model.cpp:299
PropVec getPropertiesMM() const
return property vector using millimeter scale
Definition: Model.cpp:1274
const PropVec & getProperties() const
Definition: Model.cpp:1263
void pushRotate(const Point &c, float angle)
Definition: Model.cpp:954
~Model()
Destructor.
Definition: Model.cpp:45
void setDirNode(int id)
Definition: Model.h:177
void setWinner(bool iswin=true, float ts=0.)
Definition: Model.h:179
bool readSensor(ParseFile &is)
read sensor from stream
Definition: Model.cpp:244
void adaptDataScale(dword ppmm=0)
Definition: Model.cpp:1240
std::vector< Node > NodeArray
Definition: Model.h:19
bool isOldState()
Definition: Model.h:195
void setHLNode(int hlnode)
Definition: Model.cpp:405
void adaptProportion(float ratio)
overall rest lengths sum is adapted to overall edge lengths by ratio
Definition: Model.cpp:1164
void setID(dword id)
Definition: Model.h:84
void rotate(float angle)
Definition: Model.h:124
void setShapeWeight(float weight)
Definition: Model.h:201
const Node & getDirNode() const
Definition: Model.cpp:870
std::string m_Filename
Definition: Model.h:281
void calculateDerivatives(std::vector< NodeDerivative > &deriv)
Definition: Model.cpp:839
dword createEdgeSensors(float dist=0.1)
auto-create appropriate edge sensors (dist*stdRadius is distance)
Definition: Model.cpp:622
void rebuildIndexTables()
rebuild cross indexing between nodes and edges
Definition: Model.cpp:527
int m_DirNodeInd
Definition: Model.h:273
void drawPS(CAMgraphicsProcess &gp, dword mode=0) const
Draw to postscrip output.
std::string m_Name
Definition: Model.h:270
float getDirection() const
Definition: Model.cpp:890
Implements an Edge with spring functionality.
Definition: Edge.h:14
dword createByAdjMat(const DMatrixf &adjm)
create edges mentioned in adjacency matrix
Definition: Model.cpp:468
ParticleParam phys
physical parameter set
Definition: Model.h:257
float m_ShapeWeight
Definition: Model.h:268
std::shared_ptr< const Dataset > dataset_cptr
Definition: types_fwd.h:21
Model(dataset_cptr _dataset=NULL, SensorCollection *sensors=NULL)
Default constructor.
Definition: Model.cpp:26
void scale(float factor, bool movepoints=false)
multiply all rest lengths by factor
Definition: Model.cpp:1140
dword connectNodes()
Connect selected nodes.
Definition: Model.cpp:483
int nearestNode(const Point &pos, float &dist=*(float *)(NULL)) const
Definition: Model.cpp:1108
void adaptRestLength(float ratio)
adapt restlengths towards current lengths by ratio
Definition: Model.cpp:1126
int m_HLNode
Definition: Model.h:283
dword m_CRC
Definition: Model.h:271
bool isLooser()
Definition: Model.h:190
void attachSensorColl(SensorCollection *sc)
set reference to and from sensor collection for correct updates
Definition: Model.cpp:588
SensorCollection * m_Sensors
Definition: Model.h:266
dword getInstCount() const
Definition: Model.h:222
dword & getFlags()
Definition: Model.h:196
const Point getCenter() const
Definition: Model.cpp:852
float getSensorFit(dword *npts=NULL) const
get average sensor value
Definition: Model.cpp:1173
float getFullLength() const
Returns overall sum of edge lengths.
Definition: Model.cpp:1010
void reattachSensors()
get sensors from sensor collection
Definition: Model.cpp:616
void resetForces()
Definition: Model.cpp:640
int getNNodes() const
Definition: Model.h:77
void attract(const Point &c, float factor)
Definition: Model.cpp:961
PropVec m_PropVec
Definition: Model.h:275
dword mergeModel(Model &mergemod)
Definition: Model.cpp:1312
float getStdRadius() const
Definition: Model.h:132
EdgeArray edges
indexed list of edges
Definition: Model.h:263
Point dpdt
Definition: Model.h:24
dword removeEdges()
Remove edges between selected nodes.
Definition: Model.cpp:498
int addNode(const Node &node)
Definition: Model.cpp:120
void setLooser(bool isloose=true, float ts=0.)
Definition: Model.h:185
dword m_PCFlags
Definition: Model.h:274
Point2D & convertPointToMM(Point2D &pt) const
Definition: Model.cpp:1291
const Node & getNode(int index) const
Definition: Model.h:74
dword & unsetFlags(dword flags)
Definition: Model.h:199
float getNodeAngle(const Node &n) const
Definition: Model.cpp:605
Definition: Model.h:33
NodeArray nodes
indexed list of nodes
Definition: Model.h:262
void setName(const std::string &name)
Definition: Model.cpp:399
void reset()
reset all model information (including geometry)
Definition: Model.cpp:103
dword getID() const
Definition: Model.h:83
float getLengthRatio() const
relation of overall edge length by overall rest length sum
Definition: Model.cpp:968
float addImageForces(float dt)
Definition: Model.cpp:701
friend std::ostream & operator<<(std::ostream &is, const Model &g)
Definition: Model.cpp:338
dword getSelectedNodesN(dword state=Node::ST_SELECT) const
get number of selected nodes
Definition: Model.cpp:1334
Edge & getEdge(int index)
Definition: Model.h:72
unsigned long dword
Definition: simpletypes.h:6
dword getCRC() const
Definition: Model.h:82
void adaptRestLengthSel(float ratio, const DMatrixf &selm)
adapt restlengths of selected edges towards current lengths by ratio
Definition: Model.cpp:1133
void adaptProperties(const PropVec &prop)
Definition: Model.cpp:1249
SensorCollection * getSensorCollection()
Definition: Model.h:168
dword & switchFlags(dword flags)
Definition: Model.h:200
void adaptPropertiesMM(PropVec prop)
adapt to a property vector using millimeter scale
Definition: Model.cpp:1281
void translate(const Point &t)
Definition: Model.cpp:923
float m_Direction
Definition: Model.h:279
void draw(bool drawPoints=false) const
Draw using OpenGL.
Definition: Model.cpp:542
bool readNode(ParseFile &is)
read node from stream
Definition: Model.cpp:210
bool readParameter(ParseFile &is)
read model parameters
Definition: Model.cpp:263
const Edge & getEdge(int index) const
Definition: Model.h:71
int addEdge(const Edge &edge)
Definition: Model.cpp:138
void removeNode(int nid)
Removes a node and its adjacent edges.
Definition: Model.cpp:508
DMatrixf adjMat() const
compute undirected adjacency matrix
Definition: Model.cpp:415
dword m_ppmm
Definition: Model.h:272
bool isWinner()
Definition: Model.h:184
void setOldState(bool isoldstate=true, float ts=0.)
Definition: Model.h:191
float distance(const Model &rhs, enum Model::DistType kind=DIST_POINTS) const
Definition: Model.cpp:1043
void updateParticles(float dt, int method=0)
Definition: Model.cpp:752
bool hasFlags(dword flags) const
Definition: Model.h:197
void freeze()
Definition: Model.cpp:938
const std::string & getFilename() const
Definition: Model.h:225
float getLengthVariation() const
mean squared difference from restlength normalized by restlength sum
Definition: Model.cpp:984
Model & operator=(const Model &rhs)
Definition: Model.cpp:50
void invalidatePC(dword pcflags=PC_ALL)
Definition: Model.h:220
PreCompF
Definition: Model.h:35
float getMaxRadius() const
Definition: Model.h:135
bool readFile(const char *filename, bool fullread=true)
Definition: Model.cpp:365
void setInstCount(dword ic)
Definition: Model.h:223
dword & setFlags(dword flags)
Definition: Model.h:198
void clear()
remove and destroy all geometry information (nodes and edges)
Definition: Model.cpp:95
dword m_Flags
Definition: Model.h:269
float m_Liveliness
Definition: Model.h:277
Definition: Point.h:16
float m_Scale
Definition: Model.h:278
DMatrixf selMaskMat() const
compute mask matrix of selected nodes
Definition: Model.cpp:431
void scaleSel(float factor, const DMatrixf &selm)
multiply all rest lengths of selected edges by factor
Definition: Model.cpp:1156
bool readEdge(ParseFile &is)
read edge from stream
Definition: Model.cpp:167
DistType
Definition: Model.h:40
const std::string & getName() const
Definition: Model.h:81
Point2D & convertPointFromMM(Point2D &pt) const
Definition: Model.cpp:1285
void addNoise(float r)
Definition: Model.cpp:901
float getDeformation() const
normalized length variation to compensate for different sizes
Definition: Model.cpp:995
dword m_InstCount
number of merged instances
Definition: Model.h:280
dword m_ID
Definition: Model.h:284
dword getDataScale() const
Definition: Model.h:204
dword removeByAdjMat(DMatrixf &adjm)
remove edged not mentioned in (non-directional) adjacency matrix
Definition: Model.cpp:447
float m_TimeStamp
time stamps (see. TTimeStamp)
Definition: Model.h:259
Node & getNode(int index)
Definition: Model.h:75
void push(const Point &t)
Definition: Model.cpp:931