Blender  V3.3
GridHelpers.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 "FRS_freestyle.h"
13 
14 #include "GeomUtils.h"
15 #include "Polygon.h"
16 
17 #include "../winged_edge/WEdge.h"
18 
19 #ifdef WITH_CXX_GUARDEDALLOC
20 # include "MEM_guardedalloc.h"
21 #endif
22 
23 namespace Freestyle {
24 
25 namespace GridHelpers {
26 
28 template<class T> T closestPointToSegment(const T &P, const T &A, const T &B, real &distance)
29 {
30  T AB, AP, BP;
31  AB = B - A;
32  AP = P - A;
33  BP = P - B;
34 
35  real c1(AB * AP);
36  if (c1 <= 0) {
37  distance = AP.norm();
38  return A; // A is closest point
39  }
40 
41  real c2(AB * AB);
42  if (c2 <= c1) {
43  distance = BP.norm();
44  return B; // B is closest point
45  }
46 
47  real b = c1 / c2;
48  T Pb, PPb;
49  Pb = A + b * AB;
50  PPb = P - Pb;
51 
52  distance = PPb.norm();
53  return Pb; // closest point lies on AB
54 }
55 
56 inline Vec3r closestPointOnPolygon(const Vec3r &point, const Polygon3r &poly)
57 {
58  // First cast a ray from the point onto the polygon plane
59  // If the ray intersects the polygon, then the intersection point
60  // is the closest point on the polygon
61  real t, u, v;
62  if (poly.rayIntersect(point, poly.getNormal(), t, u, v)) {
63  return point + poly.getNormal() * t;
64  }
65 
66  // Otherwise, get the nearest point on each edge, and take the closest
67  real distance;
69  point, poly.getVertices()[2], poly.getVertices()[0], distance);
70  for (unsigned int i = 0; i < 2; ++i) {
71  real t;
72  Vec3r p = closestPointToSegment(point, poly.getVertices()[i], poly.getVertices()[i + 1], t);
73  if (t < distance) {
74  distance = t;
75  closest = p;
76  }
77  }
78  return closest;
79 }
80 
81 inline real distancePointToPolygon(const Vec3r &point, const Polygon3r &poly)
82 {
83  // First cast a ray from the point onto the polygon plane
84  // If the ray intersects the polygon, then the intersection point
85  // is the closest point on the polygon
86  real t, u, v;
87  if (poly.rayIntersect(point, poly.getNormal(), t, u, v)) {
88  return (t > 0.0) ? t : -t;
89  }
90 
91  // Otherwise, get the nearest point on each edge, and take the closest
93  for (unsigned int i = 0; i < 2; ++i) {
94  real t = GeomUtils::distPointSegment(point, poly.getVertices()[i], poly.getVertices()[i + 1]);
95  if (t < distance) {
96  distance = t;
97  }
98  }
99  return distance;
100 }
101 
102 class Transform {
103  public:
104  virtual ~Transform() = 0;
105  virtual Vec3r operator()(const Vec3r &point) const = 0;
106 
107 #ifdef WITH_CXX_GUARDEDALLOC
108  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:GridHelpers:Transform")
109 #endif
110 };
111 
112 inline bool insideProscenium(const real proscenium[4], const Polygon3r &polygon)
113 {
114  // N.B. The bounding box check is redundant for inserting occluders into cells, because the cell
115  // selection code in insertOccluders has already guaranteed that the bounding boxes will overlap.
116  // First check the viewport edges, since they are the easiest case
117  // Check if the bounding box is entirely outside the proscenium
118  Vec3r bbMin, bbMax;
119  polygon.getBBox(bbMin, bbMax);
120  if (bbMax[0] < proscenium[0] || bbMin[0] > proscenium[1] || bbMax[1] < proscenium[2] ||
121  bbMin[1] > proscenium[3]) {
122  return false;
123  }
124 
125  Vec3r boxCenter(proscenium[0] + (proscenium[1] - proscenium[0]) / 2.0,
126  proscenium[2] + (proscenium[3] - proscenium[2]) / 2.0,
127  0.0);
128  Vec3r boxHalfSize(
129  (proscenium[1] - proscenium[0]) / 2.0, (proscenium[3] - proscenium[2]) / 2.0, 1.0);
130  Vec3r triverts[3] = {
131  Vec3r(polygon.getVertices()[0][0], polygon.getVertices()[0][1], 0.0),
132  Vec3r(polygon.getVertices()[1][0], polygon.getVertices()[1][1], 0.0),
133  Vec3r(polygon.getVertices()[2][0], polygon.getVertices()[2][1], 0.0),
134  };
135  return GeomUtils::overlapTriangleBox(boxCenter, boxHalfSize, triverts);
136 }
137 
138 inline vector<Vec3r> enumerateVertices(const vector<WOEdge *> &fedges)
139 {
140  vector<Vec3r> points;
141  // Iterate over vertices, storing projections in points
142  for (vector<WOEdge *>::const_iterator woe = fedges.begin(), woend = fedges.end(); woe != woend;
143  woe++) {
144  points.push_back((*woe)->GetaVertex()->GetVertex());
145  }
146 
147  return points;
148 }
149 
150 void getDefaultViewProscenium(real viewProscenium[4]);
151 
152 inline void expandProscenium(real proscenium[4], const Polygon3r &polygon)
153 {
154  Vec3r bbMin, bbMax;
155  polygon.getBBox(bbMin, bbMax);
156 
157  const real epsilon = 1.0e-6;
158 
159  if (bbMin[0] <= proscenium[0]) {
160  proscenium[0] = bbMin[0] - epsilon;
161  }
162 
163  if (bbMin[1] <= proscenium[2]) {
164  proscenium[2] = bbMin[1] - epsilon;
165  }
166 
167  if (bbMax[0] >= proscenium[1]) {
168  proscenium[1] = bbMax[0] + epsilon;
169  }
170 
171  if (bbMax[1] >= proscenium[3]) {
172  proscenium[3] = bbMax[1] + epsilon;
173  }
174 }
175 
176 inline void expandProscenium(real proscenium[4], const Vec3r &point)
177 {
178  const real epsilon = 1.0e-6;
179 
180  if (point[0] <= proscenium[0]) {
181  proscenium[0] = point[0] - epsilon;
182  }
183 
184  if (point[1] <= proscenium[2]) {
185  proscenium[2] = point[1] - epsilon;
186  }
187 
188  if (point[0] >= proscenium[1]) {
189  proscenium[1] = point[0] + epsilon;
190  }
191 
192  if (point[1] >= proscenium[3]) {
193  proscenium[3] = point[1] + epsilon;
194  }
195 }
196 
197 }; // namespace GridHelpers
198 
199 } /* 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.
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a point
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:195
const vector< Point > & getVertices() const
Definition: Polygon.h:67
void getBBox(Point &min, Point &max) const
Definition: Polygon.h:72
virtual Vec3r operator()(const Vec3r &point) const =0
static float P(float k)
Definition: math_interp.c:25
#define T
#define B
real distPointSegment(const T &P, const T &A, const T &B)
Definition: GeomUtils.h:30
bool overlapTriangleBox(Vec3r &boxcenter, Vec3r &boxhalfsize, Vec3r triverts[3])
Definition: GeomUtils.cpp:345
VecMat::Vec3< real > Vec3r
Definition: Geom.h:28
void getDefaultViewProscenium(real viewProscenium[4])
Definition: GridHelpers.cpp:12
void expandProscenium(real proscenium[4], const Polygon3r &polygon)
Definition: GridHelpers.h:152
real distancePointToPolygon(const Vec3r &point, const Polygon3r &poly)
Definition: GridHelpers.h:81
T closestPointToSegment(const T &P, const T &A, const T &B, real &distance)
Definition: GridHelpers.h:28
bool insideProscenium(const real proscenium[4], const Polygon3r &polygon)
Definition: GridHelpers.h:112
Vec3r closestPointOnPolygon(const Vec3r &point, const Polygon3r &poly)
Definition: GridHelpers.h:56
vector< Vec3r > enumerateVertices(const vector< WOEdge * > &fedges)
Definition: GridHelpers.h:138
inherits from class Rep
Definition: AppCanvas.cpp:18
double real
Definition: Precision.h:12
T distance(const T &a, const T &b)
static double epsilon
static const pxr::TfToken b("b", pxr::TfToken::Immortal)