Blender  V3.3
SilhouetteGeomEngine.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 #include <cstdio>
10 #include <cstring>
11 
12 #include "Silhouette.h"
13 #include "SilhouetteGeomEngine.h"
14 
15 #include "../geometry/GeomUtils.h"
16 
17 #include "BKE_global.h"
18 
19 using namespace std;
20 
21 namespace Freestyle {
22 
23 Vec3r SilhouetteGeomEngine::_Viewpoint = Vec3r(0, 0, 0);
24 real SilhouetteGeomEngine::_translation[3] = {0, 0, 0};
25 real SilhouetteGeomEngine::_modelViewMatrix[4][4] = {
26  {1, 0, 0, 0},
27  {0, 1, 0, 0},
28  {0, 0, 1, 0},
29  {0, 0, 0, 1},
30 };
31 real SilhouetteGeomEngine::_projectionMatrix[4][4] = {
32  {1, 0, 0, 0},
33  {0, 1, 0, 0},
34  {0, 0, 1, 0},
35  {0, 0, 0, 1},
36 };
37 real SilhouetteGeomEngine::_transform[4][4] = {
38  {1, 0, 0, 0},
39  {0, 1, 0, 0},
40  {0, 0, 1, 0},
41  {0, 0, 0, 1},
42 };
43 int SilhouetteGeomEngine::_viewport[4] = {1, 1, 1, 1};
44 real SilhouetteGeomEngine::_Focal = 0.0;
45 
46 real SilhouetteGeomEngine::_glProjectionMatrix[4][4] = {
47  {1, 0, 0, 0},
48  {0, 1, 0, 0},
49  {0, 0, 1, 0},
50  {0, 0, 0, 1},
51 };
52 real SilhouetteGeomEngine::_glModelViewMatrix[4][4] = {
53  {1, 0, 0, 0},
54  {0, 1, 0, 0},
55  {0, 0, 1, 0},
56  {0, 0, 0, 1},
57 };
58 real SilhouetteGeomEngine::_znear = 0.0;
59 real SilhouetteGeomEngine::_zfar = 100.0;
60 bool SilhouetteGeomEngine::_isOrthographicProjection = false;
61 
62 SilhouetteGeomEngine *SilhouetteGeomEngine::_pInstance = nullptr;
63 
64 void SilhouetteGeomEngine::setTransform(const real iModelViewMatrix[4][4],
65  const real iProjectionMatrix[4][4],
66  const int iViewport[4],
67  real iFocal)
68 {
69  unsigned int i, j;
70  _translation[0] = iModelViewMatrix[3][0];
71  _translation[1] = iModelViewMatrix[3][1];
72  _translation[2] = iModelViewMatrix[3][2];
73 
74  for (i = 0; i < 4; i++) {
75  for (j = 0; j < 4; j++) {
76  _modelViewMatrix[i][j] = iModelViewMatrix[j][i];
77  _glModelViewMatrix[i][j] = iModelViewMatrix[i][j];
78  }
79  }
80 
81  for (i = 0; i < 4; i++) {
82  for (j = 0; j < 4; j++) {
83  _projectionMatrix[i][j] = iProjectionMatrix[j][i];
84  _glProjectionMatrix[i][j] = iProjectionMatrix[i][j];
85  }
86  }
87 
88  for (i = 0; i < 4; i++) {
89  for (j = 0; j < 4; j++) {
90  _transform[i][j] = 0;
91  for (unsigned int k = 0; k < 4; k++) {
92  _transform[i][j] += _projectionMatrix[i][k] * _modelViewMatrix[k][j];
93  }
94  }
95  }
96 
97  for (i = 0; i < 4; i++) {
98  _viewport[i] = iViewport[i];
99  }
100  _Focal = iFocal;
101 
102  _isOrthographicProjection = (iProjectionMatrix[3][3] != 0.0);
103 }
104 
105 void SilhouetteGeomEngine::setFrustum(real iZNear, real iZFar)
106 {
107  _znear = iZNear;
108  _zfar = iZFar;
109 }
110 
111 void SilhouetteGeomEngine::retrieveViewport(int viewport[4])
112 {
113  memcpy(viewport, _viewport, sizeof(int[4]));
114 }
115 
116 void SilhouetteGeomEngine::ProjectSilhouette(vector<SVertex *> &ioVertices)
117 {
118  Vec3r newPoint;
119  vector<SVertex *>::iterator sv, svend;
120  for (sv = ioVertices.begin(), svend = ioVertices.end(); sv != svend; sv++) {
122  (*sv)->point3D(), newPoint, _modelViewMatrix, _projectionMatrix, _viewport);
123  (*sv)->setPoint2D(newPoint);
124  }
125 }
126 
127 void SilhouetteGeomEngine::ProjectSilhouette(SVertex *ioVertex)
128 {
129  Vec3r newPoint;
131  ioVertex->point3D(), newPoint, _modelViewMatrix, _projectionMatrix, _viewport);
132  ioVertex->setPoint2D(newPoint);
133 }
134 
135 real SilhouetteGeomEngine::ImageToWorldParameter(FEdge *fe, real t)
136 {
137  if (_isOrthographicProjection) {
138  return t;
139  }
140 
141  // we need to compute for each parameter t the corresponding parameter T which gives the
142  // intersection in 3D.
143  real T;
144 
145  // suffix w for world, c for camera, r for retina, i for image
146  Vec3r Aw = (fe)->vertexA()->point3D();
147  Vec3r Bw = (fe)->vertexB()->point3D();
148  Vec3r Ac, Bc;
149  GeomUtils::fromWorldToCamera(Aw, Ac, _modelViewMatrix);
150  GeomUtils::fromWorldToCamera(Bw, Bc, _modelViewMatrix);
151  Vec3r ABc = Bc - Ac;
152 #if 0
153  if (G.debug & G_DEBUG_FREESTYLE) {
154  cout << "Ac " << Ac << endl;
155  cout << "Bc " << Bc << endl;
156  cout << "ABc " << ABc << endl;
157  }
158 #endif
159  Vec3r Ai = (fe)->vertexA()->point2D();
160  Vec3r Bi = (fe)->vertexB()->point2D();
161  Vec3r Ii = Ai + t * (Bi - Ai); // the intersection point in the 2D image space
162  Vec3r Ir, Ic;
163  GeomUtils::fromImageToRetina(Ii, Ir, _viewport);
164 
165  real alpha, beta, denom;
166  real m11 = _projectionMatrix[0][0];
167  real m13 = _projectionMatrix[0][2];
168  real m22 = _projectionMatrix[1][1];
169  real m23 = _projectionMatrix[1][2];
170 
171  if (fabs(ABc[0]) > 1.0e-6) {
172  alpha = ABc[2] / ABc[0];
173  beta = Ac[2] - alpha * Ac[0];
174  denom = alpha * (Ir[0] + m13) + m11;
175  if (fabs(denom) < 1.0e-6) {
176  goto iter;
177  }
178  Ic[0] = -beta * (Ir[0] + m13) / denom;
179 #if 0
180  Ic[1] = -(Ir[1] + m23) * (alpha * Ic[0] + beta) / m22;
181  Ic[2] = alpha * (Ic[0] - Ac[0]) + Ac[2];
182 #endif
183  T = (Ic[0] - Ac[0]) / ABc[0];
184  }
185  else if (fabs(ABc[1]) > 1.0e-6) {
186  alpha = ABc[2] / ABc[1];
187  beta = Ac[2] - alpha * Ac[1];
188  denom = alpha * (Ir[1] + m23) + m22;
189  if (fabs(denom) < 1.0e-6) {
190  goto iter;
191  }
192  Ic[1] = -beta * (Ir[1] + m23) / denom;
193 #if 0
194  Ic[0] = -(Ir[0] + m13) * (alpha * Ic[1] + beta) / m11;
195  Ic[2] = alpha * (Ic[1] - Ac[1]) + Ac[2];
196 #endif
197  T = (Ic[1] - Ac[1]) / ABc[1];
198  }
199  else {
200  iter:
201  bool x_coords, less_than;
202  if (fabs(Bi[0] - Ai[0]) > 1.0e-6) {
203  x_coords = true;
204  less_than = Ai[0] < Bi[0];
205  }
206  else {
207  x_coords = false;
208  less_than = Ai[1] < Bi[1];
209  }
210  Vec3r Pc, Pr, Pi;
211  real T_sta = 0.0;
212  real T_end = 1.0;
213  real delta_x, delta_y, dist, dist_threshold = 1.0e-6;
214  int i, max_iters = 100;
215  for (i = 0; i < max_iters; i++) {
216  T = T_sta + 0.5 * (T_end - T_sta);
217  Pc = Ac + T * ABc;
218  GeomUtils::fromCameraToRetina(Pc, Pr, _projectionMatrix);
219  GeomUtils::fromRetinaToImage(Pr, Pi, _viewport);
220  delta_x = Ii[0] - Pi[0];
221  delta_y = Ii[1] - Pi[1];
222  dist = sqrt(delta_x * delta_x + delta_y * delta_y);
223  if (dist < dist_threshold) {
224  break;
225  }
226  if (x_coords) {
227  if (less_than) {
228  if (Pi[0] < Ii[0]) {
229  T_sta = T;
230  }
231  else {
232  T_end = T;
233  }
234  }
235  else {
236  if (Pi[0] > Ii[0]) {
237  T_sta = T;
238  }
239  else {
240  T_end = T;
241  }
242  }
243  }
244  else {
245  if (less_than) {
246  if (Pi[1] < Ii[1]) {
247  T_sta = T;
248  }
249  else {
250  T_end = T;
251  }
252  }
253  else {
254  if (Pi[1] > Ii[1]) {
255  T_sta = T;
256  }
257  else {
258  T_end = T;
259  }
260  }
261  }
262  }
263 #if 0
264  if (G.debug & G_DEBUG_FREESTYLE) {
265  cout << "SilhouetteGeomEngine::ImageToWorldParameter(): #iters = " << i
266  << ", dist = " << dist << "\n";
267  }
268 #endif
269  if (i == max_iters && G.debug & G_DEBUG_FREESTYLE) {
270  cout << "SilhouetteGeomEngine::ImageToWorldParameter(): reached to max_iters (dist = "
271  << dist << ")\n";
272  }
273  }
274 
275  return T;
276 }
277 
278 Vec3r SilhouetteGeomEngine::WorldToImage(const Vec3r &M)
279 {
280  Vec3r newPoint;
281  GeomUtils::fromWorldToImage(M, newPoint, _transform, _viewport);
282  return newPoint;
283 }
284 
285 Vec3r SilhouetteGeomEngine::CameraToImage(const Vec3r &M)
286 {
287  Vec3r newPoint, p;
288  GeomUtils::fromCameraToRetina(M, p, _projectionMatrix);
289  GeomUtils::fromRetinaToImage(p, newPoint, _viewport);
290  return newPoint;
291 }
292 
293 } /* namespace Freestyle */
@ G_DEBUG_FREESTYLE
Definition: BKE_global.h:181
sqrt(x)+1/max(0
_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
Class to perform all geometric operations dedicated to silhouette. That, for example,...
Classes to define a silhouette structure.
void setPoint2D(const Vec3r &iPoint2D)
Definition: Silhouette.h:289
const Vec3r & point3D() const
Definition: Silhouette.h:223
ccl_device_inline float2 fabs(const float2 &a)
Definition: math_float2.h:222
#define M
#define T
#define G(x, y, z)
void fromRetinaToImage(const Vec3r &p, Vec3r &q, const int viewport[4])
Definition: GeomUtils.cpp:657
void fromWorldToImage(const Vec3r &p, Vec3r &q, const real model_view_matrix[4][4], const real projection_matrix[4][4], const int viewport[4])
Definition: GeomUtils.cpp:669
void fromWorldToCamera(const Vec3r &p, Vec3r &q, const real model_view_matrix[4][4])
Definition: GeomUtils.cpp:647
void fromCameraToRetina(const Vec3r &p, Vec3r &q, const real projection_matrix[4][4])
Definition: GeomUtils.cpp:652
void fromImageToRetina(const Vec3r &p, Vec3r &q, const int viewport[4])
Definition: GeomUtils.cpp:693
VecMat::Vec3< real > Vec3r
Definition: Geom.h:28
inherits from class Rep
Definition: AppCanvas.cpp:18
double real
Definition: Precision.h:12
ccl_device_inline float beta(float x, float y)
Definition: util/math.h:775