Blender V5.0
Polygon.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
11
12#include <vector>
13
14#include "Geom.h"
15#include "GeomUtils.h"
16
17#include "MEM_guardedalloc.h"
18
19using namespace std;
20
21namespace Freestyle {
22
23namespace Geometry {
24
25template<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;
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 {
50 p = *it;
51 _vertices.push_back(p);
52 }
53
54 _id = poly.getId();
55 poly.getBBox(_min, _max);
56 userdata = 0;
57 userdata2 = 0;
58 }
59
60 virtual ~Polygon() {}
61
62 //
63 // Accessors
64 //
66 inline const vector<Point> &getVertices() const
67 {
68 return _vertices;
69 }
70
71 inline void getBBox(Point &min, Point &max) const
72 {
73 min = _min;
74 max = _max;
75 }
76
77 inline Point getBBoxCenter()
78 {
79 Point result;
80 result = (_min + _max) / 2;
81 return result;
82 }
83
84 inline Point getCenter()
85 {
86 Point result;
87 for (typename vector<Point>::iterator it = _vertices.begin(); it != _vertices.end(); it++) {
88 result += *it;
89 }
90 result /= _vertices.size();
91 return result;
92 }
93
94 inline uint getId() const
95 {
96 return _id;
97 }
98
99 //
100 // Modifiers
101 //
103 inline void setVertices(const vector<Point> &vertices)
104 {
105 _vertices.clear();
106 Point p;
107 for (typename vector<Point>::const_iterator it = vertices.begin(); it != vertices.end(); it++)
108 {
109 p = *it;
110 _vertices.push_back(p);
111 }
112 computeBBox();
113 }
114
115 inline void setId(uint id)
116 {
117 _id = id;
118 }
119
120 //
121 // Other methods
122 //
124 inline void computeBBox()
125 {
126 if (_vertices.empty()) {
127 return;
128 }
129
130 _max = _vertices[0];
131 _min = _vertices[0];
132
133 for (typename vector<Point>::iterator it = _vertices.begin(); it != _vertices.end(); it++) {
134 for (uint i = 0; i < Point::dim(); i++) {
135 if ((*it)[i] > _max[i]) {
136 _max[i] = (*it)[i];
137 }
138 if ((*it)[i] < _min[i]) {
139 _min[i] = (*it)[i];
140 }
141 }
142 }
143 }
144
145 // FIXME Is it possible to get rid of userdatas ?
146 void *userdata;
147 void *userdata2; // Used during ray casting
148
149 protected:
150 vector<Point> _vertices;
151 Point _min;
152 Point _max;
154
155 MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Geometry:Polygon")
156};
157
158//
159// Polygon3r class
160//
162class Polygon3r : public Polygon<Vec3r> {
163 public:
164 inline Polygon3r() : Polygon<Vec3r>() {}
165
166 inline Polygon3r(const vector<Vec3r> &vertices, const Vec3r &normal) : Polygon<Vec3r>(vertices)
167 {
168 setNormal(normal);
169 }
170
171 inline Polygon3r(const Polygon3r &poly) : Polygon<Vec3r>(poly), _normal(poly._normal) {}
172
173 virtual ~Polygon3r() {}
174
175 void setNormal(const Vec3r &normal)
176 {
177 _normal = normal;
178 }
179
180 inline Vec3r getNormal() const
181 {
182 return _normal;
183 }
184
186 inline bool rayIntersect(const Vec3r &orig,
187 const Vec3r &dir,
188 real &t,
189 real &u,
190 real &v,
191 real epsilon = M_EPSILON) const
192 {
193#if 0
194 if (_vertices.size() < 3) {
195 return false;
196 }
197#endif
199 orig, dir, _vertices[0], _vertices[1], _vertices[2], t, u, v, epsilon);
200 }
201
202 private:
203 Vec3r _normal;
204};
205
206} // end of namespace Geometry
207
208} /* namespace Freestyle */
unsigned int uint
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:175
Polygon3r(const vector< Vec3r > &vertices, const Vec3r &normal)
Definition Polygon.h:166
Polygon3r(const Polygon3r &poly)
Definition Polygon.h:171
bool rayIntersect(const Vec3r &orig, const Vec3r &dir, real &t, real &u, real &v, real epsilon=M_EPSILON) const
Definition Polygon.h:186
Polygon(const vector< Point > &vertices)
Definition Polygon.h:34
void setVertices(const vector< Point > &vertices)
Definition Polygon.h:103
vector< Point > _vertices
Definition Polygon.h:150
void getBBox(Point &min, Point &max) const
Definition Polygon.h:71
Polygon(const Polygon< Point > &poly)
Definition Polygon.h:43
const vector< Point > & getVertices() const
Definition Polygon.h:66
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)
VecMat::Vec3< real > Vec3r
Definition Geom.h:30
inherits from class Rep
Definition AppCanvas.cpp:20
static const real M_EPSILON
Definition Precision.h:17
double real
Definition Precision.h:14
#define min(a, b)
Definition sort.cc:36
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251