Structural deformable models
Edge.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _EDGE_H_
3 #define _EDGE_H_
4 
5 #include <iostream>
6 #include <math.h>
7 #include "PartParam.h"
8 #include "Node.h"
9 #include "Sensor.h"
10 #include "Errors.h"
11 
12 class Model;
14 class Edge {
15  friend class Model;
16 public:
17 // Edge() {
18 // from = -1;
19 // to = -1;
20 // restlength = 0;
21 // springconstant = PART_SPRINGCONST;
22 // dampingconstant = PART_DAMPING;
23 // nodes = NULL;
24 // }
25  Edge(const Edge &rhs )
26  {
27  operator=(rhs);
28  }
29  Edge(int _from, int _to, std::vector<Node> &_nodes,
31  int _index=-1)
32  {
33  from = _from;
34  to = _to;
35  nodes = &_nodes;
39  edgesensor = 0;
40  index = _index;
41  }
42  Edge(int _from, int _to, std::vector<Node> &_nodes,
43  float restlength, float spring_k=1, int _index=-1)
44  {
45  from = _from;
46  to = _to;
47  nodes = &_nodes;
48  this->restlength = restlength;
49  this->springconstant = spring_k;
50  edgesensor = 0;
51  this->index = _index;
52  }
53 
54  virtual ~Edge() {}
55 
56  void setNodesList(std::vector<Node> &nodes) {
57  this->nodes = &nodes;
58  }
59 
60  Edge& operator=(const Edge &rhs) {
61  if(&rhs == this) return *this;
62  from = rhs.from;
63  to = rhs.to;
64  nodes = rhs.nodes;
65  restlength = rhs.restlength;
68  edgesensor = rhs.edgesensor;
69  index = rhs.index;
70  return *this;
71  }
72 
73  const Point dir() const {
74  if(from==-1 || to==-1) return Point();
75  return toNode()-fromNode();
76  }
77  const Point idir() const {
78  if(from==-1 || to==-1) return Point();
79  return fromNode()-toNode();
80  }
81 
82  float length2() const {
83  if(from==-1 || to==-1) return 0;
84  return dir().norm2();
85  }
86 
87  float length() const {
88  return (float)sqrt(length2());
89  }
90 
91  float lengthRatio() const {
92  return length()/restlength;
93  }
94 
95  float adaptRestLength() {
96  return restlength = length();
97  }
98 
99  float adaptRestLength(float ratio) {
100  return restlength -= (restlength-length())*ratio;
101  }
102  float getAngle() const {
103  return dir().angleX();
104  }
105  float adaptAngle() {
106  return xangle = getAngle();
107  }
108  friend std::ostream& operator<<(std::ostream &os, const Edge &p) {
109  os << "e " << p.from+1 << " " << p.to+1;
111  os << " sc " << p.springconstant;
113  os << " dc " << p.dampingconstant;
114  if(p.length() != p.restlength) os << " rl " << p.restlength;
115  if(p.edgesensor>0) os << " es f " << p.edgesensor;
116  if(p.edgesensor<0) os << " es t " << -p.edgesensor;
117  os << std::endl;
118  return os;
119  }
120 
121  Node& fromNode() { return (*nodes)[from]; }
122  Node& toNode() { return (*nodes)[to]; }
123  const Node& fromNode() const { return (*nodes)[from]; }
124  const Node& toNode() const { return (*nodes)[to]; }
125 
126  dword getSensorValue(float& sum, dword& nsamples) const;
127 public:
128  int from, to;
129  float xangle; // angle to x-axis
130  float cxangle; // current angle to x-axis
131  std::vector<Node> *nodes;
132  float restlength; // consider using a restlength2 instead
138 
139 protected:
141  int index;
142 };
143 
144 inline dword Edge::getSensorValue(float& sum, dword& nsamples) const
145 {
146  if(!edgesensor) return 0;
147  const Node& from = fromNode();
148  const Node& to = toNode();
149  sensor_cptr sensor = (edgesensor > 0) ? from.sensor : to.sensor;
150  if(!sensor) return 0;
151  dword ntimes = edgesensor > 0 ? dword(edgesensor) : dword(-edgesensor);
152  Point x(from);
153  Point xinc(to); xinc -= from; xinc /= float(ntimes+1);
154  x += xinc;
155  for(dword i=0; i<ntimes; i++, x+=xinc) {
156  sum += sensor->getWeightedValue((int)x.x, (int)x.y);
157  }
158  nsamples += ntimes;
159  return ntimes;
160 }
161 
162 #endif
int index
index in the model (Model.edges)
Definition: Edge.h:141
static ParticleParam global
Definition: PartParam.h:72
const Node & toNode() const
Definition: Edge.h:124
Implements a Node used by Model and Edge.
Definition: Node.h:13
float length2() const
Definition: Edge.h:82
float adaptRestLength()
Definition: Edge.h:95
const Node & fromNode() const
Definition: Edge.h:123
float adaptRestLength(float ratio)
Definition: Edge.h:99
float cxangle
Definition: Edge.h:130
Edge(int _from, int _to, std::vector< Node > &_nodes, float restlength, float spring_k=1, int _index=-1)
Definition: Edge.h:42
float adaptAngle()
Definition: Edge.h:105
float dampingconstant
damping constant
Definition: Edge.h:136
void setNodesList(std::vector< Node > &nodes)
Definition: Edge.h:56
virtual ~Edge()
Definition: Edge.h:54
float angleX() const
Definition: Point.h:123
Implements an Edge with spring functionality.
Definition: Edge.h:14
ParticleParam phys
physical parameter set
Definition: Model.h:257
Point2D Point
Definition: Point.h:251
int edgesensor
0-none, >0-use from sensor n times, <0 use to sensor
Definition: Edge.h:137
sensor_cptr sensor
Definition: Node.h:43
dword getSensorValue(float &sum, dword &nsamples) const
Definition: Edge.h:144
float springconstant
spring constant
Definition: Edge.h:134
Edge(const Edge &rhs)
Definition: Edge.h:25
Node & fromNode()
Definition: Edge.h:121
friend std::ostream & operator<<(std::ostream &os, const Edge &p)
Definition: Edge.h:108
DMatrix< T > sum(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:59
const Point dir() const
Definition: Edge.h:73
Definition: Model.h:33
int from
Definition: Edge.h:128
float xangle
Definition: Edge.h:129
float length() const
Definition: Edge.h:87
float damping
Definition: PartParam.h:65
float lengthRatio() const
Definition: Edge.h:91
int to
Definition: Edge.h:128
unsigned long dword
Definition: simpletypes.h:6
float getAngle() const
Definition: Edge.h:102
Edge & operator=(const Edge &rhs)
Definition: Edge.h:60
float restlength
Definition: Edge.h:132
std::vector< Node > * nodes
Definition: Edge.h:131
std::shared_ptr< const Sensor > sensor_cptr
Definition: types_fwd.h:17
Definition: Point.h:16
float springconst
Definition: PartParam.h:64
float norm2() const
returns squared 2-norm
Definition: Point.h:165
const Point idir() const
Definition: Edge.h:77
Edge(int _from, int _to, std::vector< Node > &_nodes, const ParticleParam *phys=&ParticleParam::global, int _index=-1)
Definition: Edge.h:29
Node & toNode()
Definition: Edge.h:122
DMatrix< T > & sqrt(DMatrix< T > &mat)
Definition: DMatrixUtil.h:81