gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
VertexArray.h
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef VERTEX_ARRAY_H
7 #define VERTEX_ARRAY_H
8 
9 #include <vector>
10 #include <set>
11 #include "SVector3.h"
12 #include "SBoundingBox3d.h"
13 
14 #if defined(HAVE_VISUDEV)
15 typedef float normal_type;
16 #else
17 typedef char normal_type;
18 #endif
19 
20 class MElement;
21 
22 template <int N> class ElementData {
23 private:
24  float _x[N], _y[N], _z[N], _nx[N], _ny[N], _nz[N];
25  unsigned char _r[N], _g[N], _b[N], _a[N];
27 
28 public:
29  ElementData(double *x, double *y, double *z, SVector3 *n, unsigned char *r,
30  unsigned char *g, unsigned char *b, unsigned char *a,
31  MElement *ele)
32  {
33  for(int i = 0; i < N; i++) {
34  _x[i] = (float)x[i];
35  _y[i] = (float)y[i];
36  _z[i] = (float)z[i];
37  if(n) {
38  _nx[i] = (float)n[i].x();
39  _ny[i] = (float)n[i].y();
40  _nz[i] = (float)n[i].z();
41  }
42  else
43  _nx[i] = _ny[i] = _nz[i] = 0.;
44  if(r && g && b && a) {
45  _r[i] = r[i];
46  _g[i] = g[i];
47  _b[i] = b[i];
48  _a[i] = a[i];
49  }
50  else
51  _r[i] = _g[i] = _b[i] = _a[i] = 0;
52  }
53  _ele = ele;
54  }
55  inline float x(int i) const { return _x[i]; }
56  inline float y(int i) const { return _y[i]; }
57  inline float z(int i) const { return _z[i]; }
58  inline float nx(int i) const { return _nx[i]; }
59  inline float ny(int i) const { return _ny[i]; }
60  inline float nz(int i) const { return _nz[i]; }
61  inline unsigned char r(int i) const { return _r[i]; }
62  inline unsigned char g(int i) const { return _g[i]; }
63  inline unsigned char b(int i) const { return _b[i]; }
64  inline unsigned char a(int i) const { return _a[i]; }
65  inline MElement *ele() const { return _ele; }
67  {
68  SPoint3 p(0., 0., 0.);
69  for(int i = 0; i < N; i++) {
70  p[0] += _x[i];
71  p[1] += _y[i];
72  p[2] += _z[i];
73  }
74  p[0] /= (double)N;
75  p[1] /= (double)N;
76  p[2] /= (double)N;
77  return p;
78  }
79 };
80 
81 template <int N> class ElementDataLessThan {
82 public:
83  static float tolerance;
84  bool operator()(const ElementData<N> &e1, const ElementData<N> &e2) const
85  {
86  SPoint3 p1 = e1.barycenter();
87  SPoint3 p2 = e2.barycenter();
88  if(p1.x() - p2.x() > tolerance) return true;
89  if(p1.x() - p2.x() < -tolerance) return false;
90  if(p1.y() - p2.y() > tolerance) return true;
91  if(p1.y() - p2.y() < -tolerance) return false;
92  if(p1.z() - p2.z() > tolerance) return true;
93  return false;
94  }
95 };
96 
97 class Barycenter {
98 private:
99  float _x, _y, _z;
100 
101 public:
102  Barycenter(double x, double y, double z)
103  : _x((float)x), _y((float)y), _z((float)z)
104  {
105  }
106  inline float x() const { return _x; }
107  inline float y() const { return _y; }
108  inline float z() const { return _z; }
109  void operator+=(const Barycenter &p)
110  {
111  _x += p.x();
112  _y += p.y();
113  _z += p.z();
114  }
115 };
116 
118 public:
119  static float tolerance;
120  bool operator()(const Barycenter &p1, const Barycenter &p2) const
121  {
122  if(p1.x() - p2.x() > tolerance) return true;
123  if(p1.x() - p2.x() < -tolerance) return false;
124  if(p1.y() - p2.y() > tolerance) return true;
125  if(p1.y() - p2.y() < -tolerance) return false;
126  if(p1.z() - p2.z() > tolerance) return true;
127  return false;
128  }
129 };
130 
132 public:
133  std::size_t operator()(const Barycenter &b) const
134  {
135  return (std::size_t)(b.x() + b.y() + b.z());
136  }
137 };
138 
140 public:
141  bool operator()(const Barycenter &a, const Barycenter &b) const
142  {
143  return (fabs(a.x() - b.x()) < BarycenterLessThan::tolerance &&
144  fabs(a.y() - b.y()) < BarycenterLessThan::tolerance &&
145  fabs(a.z() - b.z()) < BarycenterLessThan::tolerance);
146  }
147 };
148 
149 //#include <tr1/unordered_set>
150 
151 class VertexArray {
152 private:
154  std::vector<float> _vertices;
155  std::vector<normal_type> _normals;
156  std::vector<unsigned char> _colors;
157  std::vector<MElement *> _elements;
158  std::set<ElementData<3>, ElementDataLessThan<3> > _data3;
159  std::set<Barycenter, BarycenterLessThan> _barycenters;
160  // std::tr1::unordered_set<Barycenter, BarycenterHash, BarycenterEqual>
161  // _barycenters;
162 
163  // add stuff in the arrays
164  void _addVertex(float x, float y, float z);
165  void _addNormal(float nx, float ny, float nz);
166  void _addColor(unsigned char r, unsigned char g, unsigned char b,
167  unsigned char a);
168  void _addElement(MElement *ele);
169 
170 public:
171  VertexArray(int numVerticesPerElement, int numElements);
173  // return the number of vertices in the array
174  int getNumVertices() { return (int)_vertices.size() / 3; }
175  // return the number of vertices per element
177  // return the number of element pointers
178  int getNumElementPointers() { return (int)_elements.size(); }
179  // return a pointer to the raw vertex array (warning: 1) we don't
180  // range check 2) calling this if _vertices.size() == 0 will cause
181  // some compilers to throw an exception)
182  float *getVertexArray(int i = 0) { return &_vertices[i]; }
183  std::vector<float>::iterator firstVertex() { return _vertices.begin(); }
184  std::vector<float>::iterator lastVertex() { return _vertices.end(); }
185 
186  // return a pointer to the raw normal array
187  normal_type *getNormalArray(int i = 0) { return &_normals[i]; }
188  std::vector<normal_type>::iterator firstNormal() { return _normals.begin(); }
189  std::vector<normal_type>::iterator lastNormal() { return _normals.end(); }
190 
191  // return a pointer to the raw color array
192  unsigned char *getColorArray(int i = 0) { return &_colors[i]; }
193  std::vector<unsigned char>::iterator firstColor() { return _colors.begin(); }
194  std::vector<unsigned char>::iterator lastColor() { return _colors.end(); }
195 
196  // return a pointer to the raw element array
197  MElement **getElementPointerArray(int i = 0) { return &_elements[i]; }
198  std::vector<MElement *>::iterator firstElementPointer()
199  {
200  return _elements.begin();
201  }
202  std::vector<MElement *>::iterator lastElementPointer()
203  {
204  return _elements.end();
205  }
206 
207  // add element data in the arrays (if unique is set, only add the
208  // element if another one with the same barycenter is not already
209  // present)
210  void add(double *x, double *y, double *z, SVector3 *n, unsigned int *col,
211  MElement *ele = nullptr, bool unique = true, bool boundary = false);
212  void add(double *x, double *y, double *z, SVector3 *n, unsigned char *r = nullptr,
213  unsigned char *g = nullptr, unsigned char *b = nullptr, unsigned char *a = nullptr,
214  MElement *ele = nullptr, bool unique = true, bool boundary = false);
215  // finalize the arrays
216  void finalize();
217  // sort the arrays with elements back to front wrt the eye position
218  void sort(double x, double y, double z);
219  // estimate the size of the vertex array in megabytes
220  double getMemoryInMb();
221  // serialize the vertex array into a string (for sending over the
222  // network)
223  char *toChar(int num, const std::string &name, int type, double min,
224  double max, int numsteps, double time,
225  const SBoundingBox3d &bbox, int &len);
226  void fromChar(int length, const char *bytes, int swap);
227  static int decodeHeader(int length, const char *bytes, int swap,
228  std::string &name, int &tag, int &type, double &min,
229  double &max, int &numSteps, double &time,
230  double &xmin, double &ymin, double &zmin,
231  double &xmax, double &ymax, double &zmax);
232  // merge another vertex array into this one
233  void merge(VertexArray *va);
234 };
235 
236 #endif
BarycenterLessThan::operator()
bool operator()(const Barycenter &p1, const Barycenter &p2) const
Definition: VertexArray.h:120
Barycenter::x
float x() const
Definition: VertexArray.h:106
ElementData::_y
float _y[N]
Definition: VertexArray.h:24
VertexArray::firstNormal
std::vector< normal_type >::iterator firstNormal()
Definition: VertexArray.h:188
ElementDataLessThan::operator()
bool operator()(const ElementData< N > &e1, const ElementData< N > &e2) const
Definition: VertexArray.h:84
ElementData::r
unsigned char r(int i) const
Definition: VertexArray.h:61
VertexArray::_addColor
void _addColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
Definition: VertexArray.cpp:67
VertexArray::~VertexArray
~VertexArray()
Definition: VertexArray.h:172
VertexArray::merge
void merge(VertexArray *va)
Definition: VertexArray.cpp:343
VertexArray::fromChar
void fromChar(int length, const char *bytes, int swap)
Definition: VertexArray.cpp:313
ElementData::ElementData
ElementData(double *x, double *y, double *z, SVector3 *n, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a, MElement *ele)
Definition: VertexArray.h:29
VertexArray::getMemoryInMb
double getMemoryInMb()
Definition: VertexArray.cpp:34
VertexArray::toChar
char * toChar(int num, const std::string &name, int type, double min, double max, int numsteps, double time, const SBoundingBox3d &bbox, int &len)
Definition: VertexArray.cpp:234
VertexArray::getNumVerticesPerElement
int getNumVerticesPerElement()
Definition: VertexArray.h:176
VertexArray::getElementPointerArray
MElement ** getElementPointerArray(int i=0)
Definition: VertexArray.h:197
ElementData::b
unsigned char b(int i) const
Definition: VertexArray.h:63
Barycenter::_y
float _y
Definition: VertexArray.h:99
VertexArray::lastColor
std::vector< unsigned char >::iterator lastColor()
Definition: VertexArray.h:194
SPoint3
Definition: SPoint3.h:14
ElementData::_nz
float _nz[N]
Definition: VertexArray.h:24
SVector3
Definition: SVector3.h:16
VertexArray::sort
void sort(double x, double y, double z)
Definition: VertexArray.cpp:185
VertexArray
Definition: VertexArray.h:151
ElementData::x
float x(int i) const
Definition: VertexArray.h:55
VertexArray::lastNormal
std::vector< normal_type >::iterator lastNormal()
Definition: VertexArray.h:189
SVector3.h
VertexArray::lastVertex
std::vector< float >::iterator lastVertex()
Definition: VertexArray.h:184
VertexArray::getNumVertices
int getNumVertices()
Definition: VertexArray.h:174
ElementData::ny
float ny(int i) const
Definition: VertexArray.h:59
VertexArray::_colors
std::vector< unsigned char > _colors
Definition: VertexArray.h:156
ElementDataLessThan
Definition: VertexArray.h:81
VertexArray::_addVertex
void _addVertex(float x, float y, float z)
Definition: VertexArray.cpp:42
VertexArray::firstVertex
std::vector< float >::iterator firstVertex()
Definition: VertexArray.h:183
BarycenterHash
Definition: VertexArray.h:131
VertexArray::decodeHeader
static int decodeHeader(int length, const char *bytes, int swap, std::string &name, int &tag, int &type, double &min, double &max, int &numSteps, double &time, double &xmin, double &ymin, double &zmin, double &xmax, double &ymax, double &zmax)
Definition: VertexArray.cpp:273
ElementData::ele
MElement * ele() const
Definition: VertexArray.h:65
SPoint3::x
double x(void) const
Definition: SPoint3.h:125
ElementData::_ny
float _ny[N]
Definition: VertexArray.h:24
VertexArray::_normals
std::vector< normal_type > _normals
Definition: VertexArray.h:155
VertexArray::_elements
std::vector< MElement * > _elements
Definition: VertexArray.h:157
Barycenter::z
float z() const
Definition: VertexArray.h:108
ElementData::_b
unsigned char _b[N]
Definition: VertexArray.h:25
VertexArray::lastElementPointer
std::vector< MElement * >::iterator lastElementPointer()
Definition: VertexArray.h:202
Barycenter::operator+=
void operator+=(const Barycenter &p)
Definition: VertexArray.h:109
ElementData::_z
float _z[N]
Definition: VertexArray.h:24
VertexArray::firstColor
std::vector< unsigned char >::iterator firstColor()
Definition: VertexArray.h:193
SBoundingBox3d.h
VertexArray::getColorArray
unsigned char * getColorArray(int i=0)
Definition: VertexArray.h:192
swap
void swap(double &a, double &b)
Definition: meshTriangulation.cpp:27
ElementData::_a
unsigned char _a[N]
Definition: VertexArray.h:25
VertexArray::getNormalArray
normal_type * getNormalArray(int i=0)
Definition: VertexArray.h:187
Barycenter::_z
float _z
Definition: VertexArray.h:99
BarycenterEqual
Definition: VertexArray.h:139
Barycenter
Definition: VertexArray.h:97
ElementData::_g
unsigned char _g[N]
Definition: VertexArray.h:25
ElementData::barycenter
SPoint3 barycenter() const
Definition: VertexArray.h:66
SPoint3::y
double y(void) const
Definition: SPoint3.h:127
normal_type
char normal_type
Definition: VertexArray.h:17
BarycenterEqual::operator()
bool operator()(const Barycenter &a, const Barycenter &b) const
Definition: VertexArray.h:141
MElement
Definition: MElement.h:30
VertexArray::_numVerticesPerElement
int _numVerticesPerElement
Definition: VertexArray.h:153
ElementData::_nx
float _nx[N]
Definition: VertexArray.h:24
VertexArray::firstElementPointer
std::vector< MElement * >::iterator firstElementPointer()
Definition: VertexArray.h:198
VertexArray::_addNormal
void _addNormal(float nx, float ny, float nz)
Definition: VertexArray.cpp:49
BarycenterLessThan::tolerance
static float tolerance
Definition: VertexArray.h:119
BarycenterHash::operator()
std::size_t operator()(const Barycenter &b) const
Definition: VertexArray.h:133
ElementData::z
float z(int i) const
Definition: VertexArray.h:57
length
double length(Quaternion &q)
Definition: Camera.cpp:346
VertexArray::getVertexArray
float * getVertexArray(int i=0)
Definition: VertexArray.h:182
VertexArray::_data3
std::set< ElementData< 3 >, ElementDataLessThan< 3 > > _data3
Definition: VertexArray.h:158
ElementData::y
float y(int i) const
Definition: VertexArray.h:56
ElementData::nx
float nx(int i) const
Definition: VertexArray.h:58
z
const double z
Definition: GaussQuadratureQuad.cpp:56
VertexArray::getNumElementPointers
int getNumElementPointers()
Definition: VertexArray.h:178
ElementDataLessThan::tolerance
static float tolerance
Definition: VertexArray.h:83
ElementData::nz
float nz(int i) const
Definition: VertexArray.h:60
VertexArray::_barycenters
std::set< Barycenter, BarycenterLessThan > _barycenters
Definition: VertexArray.h:159
ElementData::_r
unsigned char _r[N]
Definition: VertexArray.h:25
SPoint3::z
double z(void) const
Definition: SPoint3.h:129
VertexArray::finalize
void finalize()
Definition: VertexArray.cpp:138
ElementData::_x
float _x[N]
Definition: VertexArray.h:24
BarycenterLessThan
Definition: VertexArray.h:117
Barycenter::_x
float _x
Definition: VertexArray.h:99
ElementData::g
unsigned char g(int i) const
Definition: VertexArray.h:62
ElementData::_ele
MElement * _ele
Definition: VertexArray.h:26
VertexArray::_vertices
std::vector< float > _vertices
Definition: VertexArray.h:154
VertexArray::add
void add(double *x, double *y, double *z, SVector3 *n, unsigned int *col, MElement *ele=nullptr, bool unique=true, bool boundary=false)
Definition: VertexArray.cpp:81
Barycenter::y
float y() const
Definition: VertexArray.h:107
VertexArray::VertexArray
VertexArray(int numVerticesPerElement, int numElements)
Definition: VertexArray.cpp:17
SBoundingBox3d
Definition: SBoundingBox3d.h:21
Barycenter::Barycenter
Barycenter(double x, double y, double z)
Definition: VertexArray.h:102
ElementData
Definition: VertexArray.h:22
ElementData::a
unsigned char a(int i) const
Definition: VertexArray.h:64
VertexArray::_addElement
void _addElement(MElement *ele)
Definition: VertexArray.cpp:76