Flow123d  jenkins-Flow123d-windows32-release-multijob-51
polygon.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <vector>
3 
7 
8 using namespace mathfce;
9 
11 
13  return TPolygon::numberInstance++;
14 }
15 
17  id = generateId();
18 
19  area_is_actual = false;
20  center_is_actual = false;
21  center.SetCoord(0, 0, 0);
22  area = 0;
23 }
24 
26  TPolygon *pol = this;
28 
29  FOR_POL_VERTECES(pol, iv) {
30  delete (*iv);
31  }
32  verteces.clear();
33 }
34 
35 std::ostream & operator <<(std::ostream& stream, const TPolygon& p) {
37  int i = 0;
38 
39  // in the following, the explicit typecast is neccessary, since otherwise
40  // the operators '=' and '!=' are not defined (I wander why constantness
41  // of the Polygon instance can lead to this
42 
43  FOR_POL_VERTECES((TPolygon*) & p, iv) {
44  stream << "V" << i << " = " << (*iv)->GetPoint() << "\n";
45  i++;
46  }
47  return stream;
48 }
49 
50 void TPolygon::Add(const TPoint& P) {
52 
53  FOR_POL_VERTECES(this, iv) {
54  if ((*iv)->GetPoint() == P)
55  return;
56  }
57  TVertex* V = new TVertex(P);
58  int insertPos = InsertPosition(*V);
59  area_is_actual = false;
60  center_is_actual = false;
61  verteces.insert(verteces.begin() + insertPos, V);
62  return;
63 }
64 
66  if (!center_is_actual)
67  ComputeCenter();
68  if (area_is_actual)
69  return area;
70  ComputeArea();
71  return area;
72 }
73 
75  int i;
76  double D[ 3 ] = {0, 0, 0};
78  if (verteces.size() < 3) {
79  center.SetCoord(D[ 0 ], D[ 1 ], D[ 2 ]);
80  return;
81  }
82 
83  FOR_POL_VERTECES(this, iv) {
84  for (i = 0; i < 3; i++)
85  D[ i ] += (*iv)->GetPoint().Get(i + 1);
86  }
87  for (i = 0; i < 3; i++)
88  D[ i ] /= verteces.size();
89  center.SetCoord(D[ 0 ], D[ 1 ], D[ 2 ]);
90  center_is_actual = true;
91  return;
92 }
93 
95  if (center_is_actual)
96  return center;
97  ComputeCenter();
98  return center;
99 }
100 
102  TVertex *V1, *V2;
103  TVector U1, U2, N;
104  int pos = 0;
105 
106  // if size < 2 new vertex is insert at the end
107  if (verteces.size() < 2)
108  return verteces.size();
109 
110  // if size == 2 new vertex is insert at the end and normal vector is computed
111  if (verteces.size() == 2) {
112  V1 = (*(verteces.begin()));
113  V2 = (*(verteces.begin() + 1));
114 
115  U1 = V2->GetPoint() - V1->GetPoint();
116  U2 = Vx.GetPoint() - V1->GetPoint();
117  normal_vector = Cross(U1, U2);
118  return verteces.size();
119  }
120 
121  for (unsigned int i=0; i<verteces.size(); ++i) {
122  V1 = (*(verteces.begin() + i));
123  if (i == verteces.size() - 1)
124  V2 = (*(verteces.begin()));
125  else
126  V2 = (*(verteces.begin() + i + 1));
127  U1 = V2->GetPoint() - V1->GetPoint();
128  U2 = Vx.GetPoint() - V1->GetPoint();
129  N = Cross(U1, U2);
130  if (Dot(N, normal_vector) < 0) {
131  if (pos > 0) xprintf(Msg, " - TPolygon::InsertPosition - ERROR: only one vector product must be negative\n");
132  pos = i+1;
133  }
134  }
135 
136  if (pos == 0) xprintf(Msg, " - TPolygon::InsertPosition - ERROR: no vector product is negative\n");
137  return pos;
138 }
139 
142 
143  TTriangle T;
144  TPoint P1, P2, P3;
145 
146  area = 0;
147  area_is_actual = true;
148  if (verteces.size() < 3)
149  return;
150 
151  FOR_POL_VERTECES(this, iv) {
152  P1 = center;
153  P2 = (*iv)->GetPoint();
154  if (iv == verteces.end() - 1)
155  P3 = (*verteces.begin())->GetPoint();
156  else
157  P3 = (*(iv + 1))->GetPoint();
158  T.SetPoints(P1, P2, P3);
159  area += T.GetArea();
160  }
161  return;
162 }
163 
Definition: system.hh:72
void ComputeCenter()
Definition: polygon.cpp:74
TPoint GetPoint() const
Definition: vertex.cpp:20
double GetArea()
Definition: triangle.cpp:130
TPoint GetCenter()
Definition: polygon.cpp:94
void ComputeArea()
Definition: polygon.cpp:140
Definition: point.h:10
static int numberInstance
Definition: polygon.h:13
void SetPoints(const TPoint &, const TPoint &, const TPoint &)
Definition: triangle.cpp:111
#define xprintf(...)
Definition: system.hh:100
TVector Cross(const TVector &, const TVector &)
Definition: vector.cpp:163
~TPolygon()
Definition: polygon.cpp:25
int generateId()
Definition: polygon.cpp:12
int InsertPosition(const TVertex &Vx)
Definition: polygon.cpp:101
#define FOR_POL_VERTECES(i, j)
Definition: polygon.h:8
double GetArea()
Definition: polygon.cpp:65
void Add(const TPoint &)
Definition: polygon.cpp:50
TPolygon()
Definition: polygon.cpp:16
std::ostream & operator<<(std::ostream &stream, const TPolygon &p)
Definition: polygon.cpp:35
Definition: vertex.h:6
double Dot(const TVector &, const TVector &)
Definition: vector.cpp:180