Blender  V3.3
Polygon.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
10 #include <vector>
11 
12 #include "Geom.h"
13 #include "GeomUtils.h"
14 
15 #ifdef WITH_CXX_GUARDEDALLOC
16 # include "MEM_guardedalloc.h"
17 #endif
18 
19 using namespace std;
20 
21 namespace Freestyle {
22 
23 namespace Geometry {
24 
25 template<class Point> class Polygon {
26  public:
27  inline Polygon()
28  {
29  _id = 0;
30  userdata = 0;
31  userdata2 = 0;
32  }
33 
34  inline Polygon(const vector<Point> &vertices)
35  {
36  _vertices = vertices;
37  computeBBox();
38  _id = 0;
39  userdata = 0;
40  userdata2 = 0;
41  }
42 
43  inline Polygon(const Polygon<Point> &poly)
44  {
45  Point p;
46  for (typename vector<Point>::const_iterator it = poly.getVertices().begin();
47  it != poly.getVertices().end();
48  it++) {
49  p = *it;
50  _vertices.push_back(p);
51  }
52 
53  _id = poly.getId();
54  poly.getBBox(_min, _max);
55  userdata = 0;
56  userdata2 = 0;
57  }
58 
59  virtual ~Polygon()
60  {
61  }
62 
63  //
64  // Accessors
65  //
67  inline const vector<Point> &getVertices() const
68  {
69  return _vertices;
70  }
71 
72  inline void getBBox(Point &min, Point &max) const
73  {
74  min = _min;
75  max = _max;
76  }
77 
78  inline Point getBBoxCenter()
79  {
80  Point result;
81  result = (_min + _max) / 2;
82  return result;
83  }
84 
85  inline Point getCenter()
86  {
87  Point result;
88  for (typename vector<Point>::iterator it = _vertices.begin(); it != _vertices.end(); it++) {
89  result += *it;
90  }
91  result /= _vertices.size();
92  return result;
93  }
94 
95  inline unsigned getId() const
96  {
97  return _id;
98  }
99 
100  //
101  // Modifiers
102  //
104  inline void setVertices(const vector<Point> &vertices)
105  {
106  _vertices.clear();
107  Point p;
108  for (typename vector<Point>::const_iterator it = vertices.begin(); it != vertices.end();
109  it++) {
110  p = *it;
111  _vertices.push_back(p);
112  }
113  computeBBox();
114  }
115 
116  inline void setId(unsigned id)
117  {
118  _id = id;
119  }
120 
121  //
122  // Other methods
123  //
125  inline void computeBBox()
126  {
127  if (_vertices.empty()) {
128  return;
129  }
130 
131  _max = _vertices[0];
132  _min = _vertices[0];
133 
134  for (typename vector<Point>::iterator it = _vertices.begin(); it != _vertices.end(); it++) {
135  for (unsigned int i = 0; i < Point::dim(); i++) {
136  if ((*it)[i] > _max[i]) {
137  _max[i] = (*it)[i];
138  }
139  if ((*it)[i] < _min[i]) {
140  _min[i] = (*it)[i];
141  }
142  }
143  }
144  }
145 
146  // FIXME Is it possible to get rid of userdatas ?
147  void *userdata;
148  void *userdata2; // Used during ray casting
149 
150  protected:
151  vector<Point> _vertices;
152  Point _min;
153  Point _max;
154  unsigned _id;
155 
156 #ifdef WITH_CXX_GUARDEDALLOC
157  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Geometry:Polygon")
158 #endif
159 };
160 
161 //
162 // Polygon3r class
163 //
165 class Polygon3r : public Polygon<Vec3r> {
166  public:
167  inline Polygon3r() : Polygon<Vec3r>()
168  {
169  }
170 
171  inline Polygon3r(const vector<Vec3r> &vertices, const Vec3r &normal) : Polygon<Vec3r>(vertices)
172  {
173  setNormal(normal);
174  }
175 
176  inline Polygon3r(const Polygon3r &poly) : Polygon<Vec3r>(poly), _normal(poly._normal)
177  {
178  }
179 
180  virtual ~Polygon3r()
181  {
182  }
183 
184  void setNormal(const Vec3r &normal)
185  {
186  _normal = normal;
187  }
188 
189  inline Vec3r getNormal() const
190  {
191  return _normal;
192  }
193 
195  inline bool rayIntersect(const Vec3r &orig,
196  const Vec3r &dir,
197  real &t,
198  real &u,
199  real &v,
200  real epsilon = M_EPSILON) const
201  {
202 #if 0
203  if (_vertices.size() < 3) {
204  return false;
205  }
206 #endif
208  orig, dir, _vertices[0], _vertices[1], _vertices[2], t, u, v, epsilon);
209  }
210 
211  private:
212  Vec3r _normal;
213 };
214 
215 } // end of namespace Geometry
216 
217 } /* namespace Freestyle */
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
Various tools for geometry.
Vectors and Matrices (useful type definitions)
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMVert * v
void setNormal(const Vec3r &normal)
Definition: Polygon.h:184
Polygon3r(const vector< Vec3r > &vertices, const Vec3r &normal)
Definition: Polygon.h:171
Polygon3r(const Polygon3r &poly)
Definition: Polygon.h:176
bool rayIntersect(const Vec3r &orig, const Vec3r &dir, real &t, real &u, real &v, real epsilon=M_EPSILON) const
Definition: Polygon.h:195
const vector< Point > & getVertices() const
Definition: Polygon.h:67
unsigned getId() const
Definition: Polygon.h:95
Polygon(const vector< Point > &vertices)
Definition: Polygon.h:34
void setVertices(const vector< Point > &vertices)
Definition: Polygon.h:104
vector< Point > _vertices
Definition: Polygon.h:151
void setId(unsigned id)
Definition: Polygon.h:116
void getBBox(Point &min, Point &max) const
Definition: Polygon.h:72
Polygon(const Polygon< Point > &poly)
Definition: Polygon.h:43
IconTextureDrawCall normal
bool intersectRayTriangle(const Vec3r &orig, const Vec3r &dir, const Vec3r &v0, const Vec3r &v1, const Vec3r &v2, real &t, real &u, real &v, const real epsilon)
Definition: GeomUtils.cpp:430
inherits from class Rep
Definition: AppCanvas.cpp:18
static const real M_EPSILON
Definition: Precision.h:15
double real
Definition: Precision.h:12
static double epsilon
#define min(a, b)
Definition: sort.c:35
float max