Blender  V2.93
Polygon.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
24 #include <vector>
25 
26 #include "Geom.h"
27 #include "GeomUtils.h"
28 
29 #ifdef WITH_CXX_GUARDEDALLOC
30 # include "MEM_guardedalloc.h"
31 #endif
32 
33 using namespace std;
34 
35 namespace Freestyle {
36 
37 namespace Geometry {
38 
39 template<class Point> class Polygon {
40  public:
41  inline Polygon()
42  {
43  _id = 0;
44  userdata = 0;
45  userdata2 = 0;
46  }
47 
48  inline Polygon(const vector<Point> &vertices)
49  {
50  _vertices = vertices;
51  computeBBox();
52  _id = 0;
53  userdata = 0;
54  userdata2 = 0;
55  }
56 
57  inline Polygon(const Polygon<Point> &poly)
58  {
59  Point p;
60  for (typename vector<Point>::const_iterator it = poly.getVertices().begin();
61  it != poly.getVertices().end();
62  it++) {
63  p = *it;
64  _vertices.push_back(p);
65  }
66 
67  _id = poly.getId();
68  poly.getBBox(_min, _max);
69  userdata = 0;
70  userdata2 = 0;
71  }
72 
73  virtual ~Polygon()
74  {
75  }
76 
77  //
78  // Accessors
79  //
81  inline const vector<Point> &getVertices() const
82  {
83  return _vertices;
84  }
85 
86  inline void getBBox(Point &min, Point &max) const
87  {
88  min = _min;
89  max = _max;
90  }
91 
92  inline Point getBBoxCenter()
93  {
94  Point result;
95  result = (_min + _max) / 2;
96  return result;
97  }
98 
99  inline Point getCenter()
100  {
101  Point result;
102  for (typename vector<Point>::iterator it = _vertices.begin(); it != _vertices.end(); it++) {
103  result += *it;
104  }
105  result /= _vertices.size();
106  return result;
107  }
108 
109  inline unsigned getId() const
110  {
111  return _id;
112  }
113 
114  //
115  // Modifiers
116  //
118  inline void setVertices(const vector<Point> &vertices)
119  {
120  _vertices.clear();
121  Point p;
122  for (typename vector<Point>::const_iterator it = vertices.begin(); it != vertices.end();
123  it++) {
124  p = *it;
125  _vertices.push_back(p);
126  }
127  computeBBox();
128  }
129 
130  inline void setId(unsigned id)
131  {
132  _id = id;
133  }
134 
135  //
136  // Other methods
137  //
139  inline void computeBBox()
140  {
141  if (_vertices.empty()) {
142  return;
143  }
144 
145  _max = _vertices[0];
146  _min = _vertices[0];
147 
148  for (typename vector<Point>::iterator it = _vertices.begin(); it != _vertices.end(); it++) {
149  for (unsigned int i = 0; i < Point::dim(); i++) {
150  if ((*it)[i] > _max[i]) {
151  _max[i] = (*it)[i];
152  }
153  if ((*it)[i] < _min[i]) {
154  _min[i] = (*it)[i];
155  }
156  }
157  }
158  }
159 
160  // FIXME Is it possible to get rid of userdatas ?
161  void *userdata;
162  void *userdata2; // Used during ray casting
163 
164  protected:
165  vector<Point> _vertices;
166  Point _min;
167  Point _max;
168  unsigned _id;
169 
170 #ifdef WITH_CXX_GUARDEDALLOC
171  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Geometry:Polygon")
172 #endif
173 };
174 
175 //
176 // Polygon3r class
177 //
179 class Polygon3r : public Polygon<Vec3r> {
180  public:
181  inline Polygon3r() : Polygon<Vec3r>()
182  {
183  }
184 
185  inline Polygon3r(const vector<Vec3r> &vertices, const Vec3r &normal) : Polygon<Vec3r>(vertices)
186  {
187  setNormal(normal);
188  }
189 
190  inline Polygon3r(const Polygon3r &poly) : Polygon<Vec3r>(poly), _normal(poly._normal)
191  {
192  }
193 
194  virtual ~Polygon3r()
195  {
196  }
197 
198  void setNormal(const Vec3r &normal)
199  {
200  _normal = normal;
201  }
202 
203  inline Vec3r getNormal() const
204  {
205  return _normal;
206  }
207 
209  inline bool rayIntersect(const Vec3r &orig,
210  const Vec3r &dir,
211  real &t,
212  real &u,
213  real &v,
214  real epsilon = M_EPSILON) const
215  {
216 #if 0
217  if (_vertices.size() < 3) {
218  return false;
219  }
220 #endif
222  orig, dir, _vertices[0], _vertices[1], _vertices[2], t, u, v, epsilon);
223  }
224 
225  private:
226  Vec3r _normal;
227 };
228 
229 } // end of namespace Geometry
230 
231 } /* 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:198
Polygon3r(const vector< Vec3r > &vertices, const Vec3r &normal)
Definition: Polygon.h:185
Polygon3r(const Polygon3r &poly)
Definition: Polygon.h:190
bool rayIntersect(const Vec3r &orig, const Vec3r &dir, real &t, real &u, real &v, real epsilon=M_EPSILON) const
Definition: Polygon.h:209
const vector< Point > & getVertices() const
Definition: Polygon.h:81
unsigned getId() const
Definition: Polygon.h:109
Polygon(const vector< Point > &vertices)
Definition: Polygon.h:48
void setVertices(const vector< Point > &vertices)
Definition: Polygon.h:118
vector< Point > _vertices
Definition: Polygon.h:165
void setId(unsigned id)
Definition: Polygon.h:130
void getBBox(Point &min, Point &max) const
Definition: Polygon.h:86
Polygon(const Polygon< Point > &poly)
Definition: Polygon.h:57
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:444
inherits from class Rep
Definition: AppCanvas.cpp:32
static const real M_EPSILON
Definition: Precision.h:29
double real
Definition: Precision.h:26
static double epsilon
#define min(a, b)
Definition: sort.c:51
float max