Blender V4.3
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
12#include <vector>
13
14#include "Geom.h"
15#include "GeomUtils.h"
16
17#ifdef WITH_CXX_GUARDEDALLOC
18# include "MEM_guardedalloc.h"
19#endif
20
21using namespace std;
22
23namespace Freestyle {
24
25namespace Geometry {
26
27template<class Point> class Polygon {
28 public:
29 inline Polygon()
30 {
31 _id = 0;
32 userdata = 0;
33 userdata2 = 0;
34 }
35
36 inline Polygon(const vector<Point> &vertices)
37 {
38 _vertices = vertices;
40 _id = 0;
41 userdata = 0;
42 userdata2 = 0;
43 }
44
45 inline Polygon(const Polygon<Point> &poly)
46 {
47 Point p;
48 for (typename vector<Point>::const_iterator it = poly.getVertices().begin();
49 it != poly.getVertices().end();
50 it++)
51 {
52 p = *it;
53 _vertices.push_back(p);
54 }
55
56 _id = poly.getId();
57 poly.getBBox(_min, _max);
58 userdata = 0;
59 userdata2 = 0;
60 }
61
62 virtual ~Polygon() {}
63
64 //
65 // Accessors
66 //
68 inline const vector<Point> &getVertices() const
69 {
70 return _vertices;
71 }
72
73 inline void getBBox(Point &min, Point &max) const
74 {
75 min = _min;
76 max = _max;
77 }
78
79 inline Point getBBoxCenter()
80 {
81 Point result;
82 result = (_min + _max) / 2;
83 return result;
84 }
85
86 inline Point getCenter()
87 {
88 Point result;
89 for (typename vector<Point>::iterator it = _vertices.begin(); it != _vertices.end(); it++) {
90 result += *it;
91 }
92 result /= _vertices.size();
93 return result;
94 }
95
96 inline uint getId() const
97 {
98 return _id;
99 }
100
101 //
102 // Modifiers
103 //
105 inline void setVertices(const vector<Point> &vertices)
106 {
107 _vertices.clear();
108 Point p;
109 for (typename vector<Point>::const_iterator it = vertices.begin(); it != vertices.end(); it++)
110 {
111 p = *it;
112 _vertices.push_back(p);
113 }
114 computeBBox();
115 }
116
117 inline void setId(uint id)
118 {
119 _id = id;
120 }
121
122 //
123 // Other methods
124 //
126 inline void computeBBox()
127 {
128 if (_vertices.empty()) {
129 return;
130 }
131
132 _max = _vertices[0];
133 _min = _vertices[0];
134
135 for (typename vector<Point>::iterator it = _vertices.begin(); it != _vertices.end(); it++) {
136 for (uint i = 0; i < Point::dim(); i++) {
137 if ((*it)[i] > _max[i]) {
138 _max[i] = (*it)[i];
139 }
140 if ((*it)[i] < _min[i]) {
141 _min[i] = (*it)[i];
142 }
143 }
144 }
145 }
146
147 // FIXME Is it possible to get rid of userdatas ?
148 void *userdata;
149 void *userdata2; // Used during ray casting
150
151 protected:
152 vector<Point> _vertices;
153 Point _min;
154 Point _max;
156
157#ifdef WITH_CXX_GUARDEDALLOC
158 MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Geometry:Polygon")
159#endif
160};
161
162//
163// Polygon3r class
164//
166class Polygon3r : public Polygon<Vec3r> {
167 public:
168 inline Polygon3r() : Polygon<Vec3r>() {}
169
170 inline Polygon3r(const vector<Vec3r> &vertices, const Vec3r &normal) : Polygon<Vec3r>(vertices)
171 {
172 setNormal(normal);
173 }
174
175 inline Polygon3r(const Polygon3r &poly) : Polygon<Vec3r>(poly), _normal(poly._normal) {}
176
177 virtual ~Polygon3r() {}
178
179 void setNormal(const Vec3r &normal)
180 {
181 _normal = normal;
182 }
183
184 inline Vec3r getNormal() const
185 {
186 return _normal;
187 }
188
190 inline bool rayIntersect(const Vec3r &orig,
191 const Vec3r &dir,
192 real &t,
193 real &u,
194 real &v,
195 real epsilon = M_EPSILON) const
196 {
197#if 0
198 if (_vertices.size() < 3) {
199 return false;
200 }
201#endif
203 orig, dir, _vertices[0], _vertices[1], _vertices[2], t, u, v, epsilon);
204 }
205
206 private:
207 Vec3r _normal;
208};
209
210} // end of namespace Geometry
211
212} /* 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:179
Polygon3r(const vector< Vec3r > &vertices, const Vec3r &normal)
Definition Polygon.h:170
Polygon3r(const Polygon3r &poly)
Definition Polygon.h:175
bool rayIntersect(const Vec3r &orig, const Vec3r &dir, real &t, real &u, real &v, real epsilon=M_EPSILON) const
Definition Polygon.h:190
Polygon(const vector< Point > &vertices)
Definition Polygon.h:36
void setVertices(const vector< Point > &vertices)
Definition Polygon.h:105
vector< Point > _vertices
Definition Polygon.h:152
void getBBox(Point &min, Point &max) const
Definition Polygon.h:73
Polygon(const Polygon< Point > &poly)
Definition Polygon.h:45
const vector< Point > & getVertices() const
Definition Polygon.h:68
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)
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.c:32