Blender  V2.93
GridHelpers.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 "FRS_freestyle.h"
27 
28 #include "GeomUtils.h"
29 #include "Polygon.h"
30 
31 #include "../winged_edge/WEdge.h"
32 
33 #ifdef WITH_CXX_GUARDEDALLOC
34 # include "MEM_guardedalloc.h"
35 #endif
36 
37 namespace Freestyle {
38 
39 namespace GridHelpers {
40 
42 template<class T> T closestPointToSegment(const T &P, const T &A, const T &B, real &distance)
43 {
44  T AB, AP, BP;
45  AB = B - A;
46  AP = P - A;
47  BP = P - B;
48 
49  real c1(AB * AP);
50  if (c1 <= 0) {
51  distance = AP.norm();
52  return A; // A is closest point
53  }
54 
55  real c2(AB * AB);
56  if (c2 <= c1) {
57  distance = BP.norm();
58  return B; // B is closest point
59  }
60 
61  real b = c1 / c2;
62  T Pb, PPb;
63  Pb = A + b * AB;
64  PPb = P - Pb;
65 
66  distance = PPb.norm();
67  return Pb; // closest point lies on AB
68 }
69 
70 inline Vec3r closestPointOnPolygon(const Vec3r &point, const Polygon3r &poly)
71 {
72  // First cast a ray from the point onto the polygon plane
73  // If the ray intersects the polygon, then the intersection point
74  // is the closest point on the polygon
75  real t, u, v;
76  if (poly.rayIntersect(point, poly.getNormal(), t, u, v)) {
77  return point + poly.getNormal() * t;
78  }
79 
80  // Otherwise, get the nearest point on each edge, and take the closest
81  real distance;
83  point, poly.getVertices()[2], poly.getVertices()[0], distance);
84  for (unsigned int i = 0; i < 2; ++i) {
85  real t;
86  Vec3r p = closestPointToSegment(point, poly.getVertices()[i], poly.getVertices()[i + 1], t);
87  if (t < distance) {
88  distance = t;
89  closest = p;
90  }
91  }
92  return closest;
93 }
94 
95 inline real distancePointToPolygon(const Vec3r &point, const Polygon3r &poly)
96 {
97  // First cast a ray from the point onto the polygon plane
98  // If the ray intersects the polygon, then the intersection point
99  // is the closest point on the polygon
100  real t, u, v;
101  if (poly.rayIntersect(point, poly.getNormal(), t, u, v)) {
102  return (t > 0.0) ? t : -t;
103  }
104 
105  // Otherwise, get the nearest point on each edge, and take the closest
106  real distance = GeomUtils::distPointSegment(point, poly.getVertices()[2], poly.getVertices()[0]);
107  for (unsigned int i = 0; i < 2; ++i) {
108  real t = GeomUtils::distPointSegment(point, poly.getVertices()[i], poly.getVertices()[i + 1]);
109  if (t < distance) {
110  distance = t;
111  }
112  }
113  return distance;
114 }
115 
116 class Transform {
117  public:
118  virtual ~Transform() = 0;
119  virtual Vec3r operator()(const Vec3r &point) const = 0;
120 
121 #ifdef WITH_CXX_GUARDEDALLOC
122  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:GridHelpers:Transform")
123 #endif
124 };
125 
126 inline bool insideProscenium(const real proscenium[4], const Polygon3r &polygon)
127 {
128  // N.B. The bounding box check is redundant for inserting occluders into cells, because the cell
129  // selection code in insertOccluders has already guaranteed that the bounding boxes will overlap.
130  // First check the viewport edges, since they are the easiest case
131  // Check if the bounding box is entirely outside the proscenium
132  Vec3r bbMin, bbMax;
133  polygon.getBBox(bbMin, bbMax);
134  if (bbMax[0] < proscenium[0] || bbMin[0] > proscenium[1] || bbMax[1] < proscenium[2] ||
135  bbMin[1] > proscenium[3]) {
136  return false;
137  }
138 
139  Vec3r boxCenter(proscenium[0] + (proscenium[1] - proscenium[0]) / 2.0,
140  proscenium[2] + (proscenium[3] - proscenium[2]) / 2.0,
141  0.0);
142  Vec3r boxHalfSize(
143  (proscenium[1] - proscenium[0]) / 2.0, (proscenium[3] - proscenium[2]) / 2.0, 1.0);
144  Vec3r triverts[3] = {
145  Vec3r(polygon.getVertices()[0][0], polygon.getVertices()[0][1], 0.0),
146  Vec3r(polygon.getVertices()[1][0], polygon.getVertices()[1][1], 0.0),
147  Vec3r(polygon.getVertices()[2][0], polygon.getVertices()[2][1], 0.0),
148  };
149  return GeomUtils::overlapTriangleBox(boxCenter, boxHalfSize, triverts);
150 }
151 
152 inline vector<Vec3r> enumerateVertices(const vector<WOEdge *> &fedges)
153 {
154  vector<Vec3r> points;
155  // Iterate over vertices, storing projections in points
156  for (vector<WOEdge *>::const_iterator woe = fedges.begin(), woend = fedges.end(); woe != woend;
157  woe++) {
158  points.push_back((*woe)->GetaVertex()->GetVertex());
159  }
160 
161  return points;
162 }
163 
164 void getDefaultViewProscenium(real viewProscenium[4]);
165 
166 inline void expandProscenium(real proscenium[4], const Polygon3r &polygon)
167 {
168  Vec3r bbMin, bbMax;
169  polygon.getBBox(bbMin, bbMax);
170 
171  const real epsilon = 1.0e-6;
172 
173  if (bbMin[0] <= proscenium[0]) {
174  proscenium[0] = bbMin[0] - epsilon;
175  }
176 
177  if (bbMin[1] <= proscenium[2]) {
178  proscenium[2] = bbMin[1] - epsilon;
179  }
180 
181  if (bbMax[0] >= proscenium[1]) {
182  proscenium[1] = bbMax[0] + epsilon;
183  }
184 
185  if (bbMax[1] >= proscenium[3]) {
186  proscenium[3] = bbMax[1] + epsilon;
187  }
188 }
189 
190 inline void expandProscenium(real proscenium[4], const Vec3r &point)
191 {
192  const real epsilon = 1.0e-6;
193 
194  if (point[0] <= proscenium[0]) {
195  proscenium[0] = point[0] - epsilon;
196  }
197 
198  if (point[1] <= proscenium[2]) {
199  proscenium[2] = point[1] - epsilon;
200  }
201 
202  if (point[0] >= proscenium[1]) {
203  proscenium[1] = point[0] + epsilon;
204  }
205 
206  if (point[1] >= proscenium[3]) {
207  proscenium[3] = point[1] + epsilon;
208  }
209 }
210 
211 }; // namespace GridHelpers
212 
213 } /* 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.
Read Guarded memory(de)allocation.
Class to define a polygon.
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define A
bool closest(btVector3 &v)
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
void getBBox(Point &min, Point &max) const
Definition: Polygon.h:86
virtual Vec3r operator()(const Vec3r &point) const =0
static float P(float k)
Definition: math_interp.c:41
#define T
#define B
real distPointSegment(const T &P, const T &A, const T &B)
Definition: GeomUtils.h:44
bool overlapTriangleBox(Vec3r &boxcenter, Vec3r &boxhalfsize, Vec3r triverts[3])
Definition: GeomUtils.cpp:359
VecMat::Vec3< real > Vec3r
Definition: Geom.h:42
void getDefaultViewProscenium(real viewProscenium[4])
Definition: GridHelpers.cpp:26
void expandProscenium(real proscenium[4], const Polygon3r &polygon)
Definition: GridHelpers.h:166
real distancePointToPolygon(const Vec3r &point, const Polygon3r &poly)
Definition: GridHelpers.h:95
T closestPointToSegment(const T &P, const T &A, const T &B, real &distance)
Definition: GridHelpers.h:42
bool insideProscenium(const real proscenium[4], const Polygon3r &polygon)
Definition: GridHelpers.h:126
Vec3r closestPointOnPolygon(const Vec3r &point, const Polygon3r &poly)
Definition: GridHelpers.h:70
vector< Vec3r > enumerateVertices(const vector< WOEdge * > &fedges)
Definition: GridHelpers.h:152
inherits from class Rep
Definition: AppCanvas.cpp:32
double real
Definition: Precision.h:26
static double epsilon
ccl_device_inline float distance(const float2 &a, const float2 &b)