Blender  V2.93
MOD_lineart.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  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #pragma once
25 
26 #include "BLI_listbase.h"
27 #include "BLI_math.h" /* Needed here for inline functions. */
28 #include "BLI_threads.h"
29 
30 #include <math.h>
31 
32 typedef struct LineartStaticMemPoolNode {
34  size_t size;
35  size_t used_byte;
36  /* User memory starts here */
38 
39 typedef struct LineartStaticMemPool {
43 
44 typedef struct LineartTriangleAdjacent {
45  struct LineartEdge *e[3];
47 
48 typedef struct LineartTriangle {
49  struct LineartVert *v[3];
50 
51  /* first culled in line list to use adjacent triangle info, then go through triangle list. */
52  double gn[3];
53 
54  /* Material flag is removed to save space. */
55  unsigned char transparency_mask;
56  unsigned char flags; /* #eLineartTriangleFlags */
57 
65 
66 typedef struct LineartTriangleThread {
67  struct LineartTriangle base;
76  struct LineartEdge *testing_e[1];
78 
84 
85 typedef struct LineartElementLinkNode {
87  void *pointer;
89  void *object_ref;
91 
95 
96 typedef struct LineartLineSegment {
99  double at;
101  unsigned char occlusion;
102 
109  unsigned char transparency_mask;
111 
112 typedef struct LineartVert {
113  double gloc[3];
114  double fbcoord[4];
115 
116  /* Scene global index. */
117  int index;
118 
124  char flag;
125 
127 
128 typedef struct LineartVertIntersection {
129  struct LineartVert base;
131  int isec1, isec2;
134 
135 typedef enum eLineArtVertFlags {
137  LRT_VERT_EDGE_USED = (1 << 1),
139 
140 typedef struct LineartEdge {
142  struct LineartEdge *next;
143  struct LineartVert *v1, *v2;
149  struct LineartTriangle *t1, *t2;
151  char min_occ;
152 
154  unsigned char flags;
155 
165 
166 typedef struct LineartLineChain {
169 
171  float length;
172 
174  char picked;
175  char level;
176 
178  int type;
179  unsigned char transparency_mask;
180 
183 
184 typedef struct LineartLineChainItem {
187  float pos[3];
189  float gpos[3];
190  float normal[3];
191  char line_type;
192  char occlusion;
193  unsigned char transparency_mask;
194  size_t index;
196 
201  char picked;
202 
203  /* left/right mark.
204  * Because we revert list in chaining so we need the flag. */
205  char is_left;
207 
208 typedef struct LineartRenderBuffer {
210 
212 
213  int w, h;
217  double view_projection[4][4];
218 
220  unsigned int bounding_area_count;
221 
225 
228 
234 
235  /* Render status */
236  double view_vector[3];
237 
239 
240  unsigned int contour_count;
241  unsigned int contour_processed;
245 
246  unsigned int intersection_count;
250 
251  unsigned int crease_count;
252  unsigned int crease_processed;
255 
256  unsigned int material_line_count;
257  unsigned int material_processed;
260 
261  unsigned int edge_mark_count;
262  unsigned int edge_mark_processed;
265 
267 
268  /* For managing calculation tasks for multiple threads. */
270 
271  /* settings */
272 
274  double crease_angle;
275  double crease_cos;
276 
279 
290 
291  /* Keep an copy of these data so when line art is running it's self-contained. */
293  float cam_obmat[4][4];
294  double camera_pos[3];
296  float shift_x, shift_y;
300 
301  /* FIXME(Yiming): Temporary solution for speeding up calculation by not including lines that
302  * are not in the selected source. This will not be needed after we have a proper scene-wise
303  * cache running because multiple modifiers can then select results from that without further
304  * calculation. */
308 
310 
311 #define DBL_TRIANGLE_LIM 1e-8
312 #define DBL_EDGE_LIM 1e-9
313 
314 #define LRT_MEMORY_POOL_64MB (1 << 26)
315 
316 typedef enum eLineartTriangleFlags {
318  LRT_CULL_USED = (1 << 0),
319  LRT_CULL_DISCARD = (1 << 1),
320  LRT_CULL_GENERATED = (1 << 2),
324 
328 #define LRT_THREAD_EDGE_COUNT 1000
329 
330 typedef struct LineartRenderTaskInfo {
332 
334 
337 
340 
343 
346 
349 
351 
376 typedef struct LineartBoundingArea {
377  double l, r, u, b;
378  double cx, cy;
379 
382 
387 
389 
392 
396 
397 #define LRT_TILE(tile, r, c, CCount) tile[r * CCount + c]
398 
399 #define LRT_CLAMP(a, Min, Max) a = a < Min ? Min : (a > Max ? Max : a)
400 
401 #define LRT_MAX3_INDEX(a, b, c) (a > b ? (a > c ? 0 : (b > c ? 1 : 2)) : (b > c ? 1 : 2))
402 
403 #define LRT_MIN3_INDEX(a, b, c) (a < b ? (a < c ? 0 : (b < c ? 1 : 2)) : (b < c ? 1 : 2))
404 
405 #define LRT_MAX3_INDEX_ABC(x, y, z) (x > y ? (x > z ? a : (y > z ? b : c)) : (y > z ? b : c))
406 
407 #define LRT_MIN3_INDEX_ABC(x, y, z) (x < y ? (x < z ? a : (y < z ? b : c)) : (y < z ? b : c))
408 
409 #define LRT_ABC(index) (index == 0 ? a : (index == 1 ? b : c))
410 
411 #define LRT_DOUBLE_CLOSE_ENOUGH(a, b) (((a) + DBL_EDGE_LIM) >= (b) && ((a)-DBL_EDGE_LIM) <= (b))
412 
414  const double *a1, const double *a2, const double *b1, const double *b2, double *aRatio)
415 {
416 /* Legacy intersection math aligns better with occlusion function quirks. */
417 /* #define USE_VECTOR_LINE_INTERSECTION */
418 #ifdef USE_VECTOR_LINE_INTERSECTION
419 
420  /* from isect_line_line_v2_point() */
421 
422  double s10[2], s32[2];
423  double div;
424 
425  sub_v2_v2v2_db(s10, a2, a1);
426  sub_v2_v2v2_db(s32, b2, b1);
427 
428  div = cross_v2v2_db(s10, s32);
429  if (div != 0.0f) {
430  const double u = cross_v2v2_db(a2, a1);
431  const double v = cross_v2v2_db(b2, b1);
432 
433  const double rx = ((s32[0] * u) - (s10[0] * v)) / div;
434  const double ry = ((s32[1] * u) - (s10[1] * v)) / div;
435  double rr;
436 
437  if (fabs(a2[0] - a1[0]) > fabs(a2[1] - a1[1])) {
438  *aRatio = ratiod(a1[0], a2[0], rx);
439  if (fabs(b2[0] - b1[0]) > fabs(b2[1] - b1[1])) {
440  rr = ratiod(b1[0], b2[0], rx);
441  }
442  else {
443  rr = ratiod(b1[1], b2[1], ry);
444  }
445  if ((*aRatio) > 0 && (*aRatio) < 1 && rr > 0 && rr < 1) {
446  return 1;
447  }
448  return 0;
449  }
450 
451  *aRatio = ratiod(a1[1], a2[1], ry);
452  if (fabs(b2[0] - b1[0]) > fabs(b2[1] - b1[1])) {
453  rr = ratiod(b1[0], b2[0], rx);
454  }
455  else {
456  rr = ratiod(b1[1], b2[1], ry);
457  }
458  if ((*aRatio) > 0 && (*aRatio) < 1 && rr > 0 && rr < 1) {
459  return 1;
460  }
461  return 0;
462  }
463  return 0;
464 
465 #else
466  double k1, k2;
467  double x;
468  double y;
469  double ratio;
470  double x_diff = (a2[0] - a1[0]);
471  double x_diff2 = (b2[0] - b1[0]);
472 
473  if (LRT_DOUBLE_CLOSE_ENOUGH(x_diff, 0)) {
474  if (LRT_DOUBLE_CLOSE_ENOUGH(x_diff2, 0)) {
475  *aRatio = 0;
476  return 0;
477  }
478  double r2 = ratiod(b1[0], b2[0], a1[0]);
479  x = interpd(b2[0], b1[0], r2);
480  y = interpd(b2[1], b1[1], r2);
481  *aRatio = ratio = ratiod(a1[1], a2[1], y);
482  }
483  else {
484  if (LRT_DOUBLE_CLOSE_ENOUGH(x_diff2, 0)) {
485  ratio = ratiod(a1[0], a2[0], b1[0]);
486  x = interpd(a2[0], a1[0], ratio);
487  *aRatio = ratio;
488  }
489  else {
490  k1 = (a2[1] - a1[1]) / x_diff;
491  k2 = (b2[1] - b1[1]) / x_diff2;
492 
493  if ((k1 == k2))
494  return 0;
495 
496  x = (a1[1] - b1[1] - k1 * a1[0] + k2 * b1[0]) / (k2 - k1);
497 
498  ratio = (x - a1[0]) / x_diff;
499 
500  *aRatio = ratio;
501  }
502  }
503 
504  if (LRT_DOUBLE_CLOSE_ENOUGH(b1[0], b2[0])) {
505  y = interpd(a2[1], a1[1], ratio);
506  if (y > MAX2(b1[1], b2[1]) || y < MIN2(b1[1], b2[1]))
507  return 0;
508  }
509  else if (ratio <= 0 || ratio > 1 || (b1[0] > b2[0] && x > b1[0]) ||
510  (b1[0] < b2[0] && x < b1[0]) || (b2[0] > b1[0] && x > b2[0]) ||
511  (b2[0] < b1[0] && x < b2[0]))
512  return 0;
513 
514  return 1;
515 #endif
516 }
517 
518 struct Depsgraph;
519 struct Scene;
520 struct LineartRenderBuffer;
522 
524 
528 void MOD_lineart_chain_discard_short(LineartRenderBuffer *rb, const float threshold);
529 void MOD_lineart_chain_split_angle(LineartRenderBuffer *rb, float angle_threshold_rad);
530 
533 
535  struct LineartGpencilModifierData *lmd);
536 
537 struct Scene;
538 
540  double x,
541  double y);
542 
544 
545 struct bGPDlayer;
546 struct bGPDframe;
547 
549  struct Depsgraph *depsgraph,
550  struct Object *ob,
551  struct bGPDlayer *gpl,
552  struct bGPDframe *gpf,
553  char source_type,
554  void *source_reference,
555  int level_start,
556  int level_end,
557  int mat_nr,
558  short edge_types,
559  unsigned char transparency_flags,
560  unsigned char transparency_mask,
561  short thickness,
562  float opacity,
563  const char *source_vgname,
564  const char *vgname,
565  int modifier_flags);
566 
568 
#define BLI_INLINE
MINLINE double interpd(double a, double b, double t)
MINLINE double ratiod(double min, double max, double pos)
MINLINE double cross_v2v2_db(const double a[2], const double b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v2_v2v2_db(double r[2], const double a[2], const double b[2])
pthread_spinlock_t SpinLock
Definition: BLI_threads.h:111
#define MAX2(a, b)
#define MIN2(a, b)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
_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 y
struct LineartStaticMemPool LineartStaticMemPool
struct LineartBoundingArea LineartBoundingArea
#define LRT_DOUBLE_CLOSE_ENOUGH(a, b)
Definition: MOD_lineart.h:411
struct LineartEdge LineartEdge
struct LineartRenderTaskInfo LineartRenderTaskInfo
void MOD_lineart_chain_discard_short(LineartRenderBuffer *rb, const float threshold)
struct LineartChainRegisterEntry LineartChainRegisterEntry
struct LineartLineChainItem LineartLineChainItem
struct LineartLineChain LineartLineChain
struct LineartRenderBuffer LineartRenderBuffer
void MOD_lineart_gpencil_generate(LineartRenderBuffer *rb, struct Depsgraph *depsgraph, struct Object *ob, struct bGPDlayer *gpl, struct bGPDframe *gpf, char source_type, void *source_reference, int level_start, int level_end, int mat_nr, short edge_types, unsigned char transparency_flags, unsigned char transparency_mask, short thickness, float opacity, const char *source_vgname, const char *vgname, int modifier_flags)
Definition: lineart_cpu.c:3932
struct LineartVertIntersection LineartVertIntersection
struct LineartTriangleAdjacent LineartTriangleAdjacent
void MOD_lineart_destroy_render_data(struct LineartGpencilModifierData *lmd)
Definition: lineart_cpu.c:2629
void MOD_lineart_chain_clear_picked_flag(struct LineartRenderBuffer *rb)
void ED_operatortypes_lineart(void)
void MOD_lineart_chain_connect(LineartRenderBuffer *rb)
struct LineartStaticMemPoolNode LineartStaticMemPoolNode
eLineArtVertFlags
Definition: MOD_lineart.h:135
@ LRT_VERT_EDGE_USED
Definition: MOD_lineart.h:137
@ LRT_VERT_HAS_INTERSECTION_DATA
Definition: MOD_lineart.h:136
int MOD_lineart_chain_count(const LineartLineChain *rlc)
BLI_INLINE int lineart_LineIntersectTest2d(const double *a1, const double *a2, const double *b1, const double *b2, double *aRatio)
Definition: MOD_lineart.h:413
void MOD_lineart_chain_split_for_fixed_occlusion(LineartRenderBuffer *rb)
LineartBoundingArea * MOD_lineart_get_parent_bounding_area(LineartRenderBuffer *rb, double x, double y)
Definition: lineart_cpu.c:3268
struct LineartTriangleThread LineartTriangleThread
bool MOD_lineart_compute_feature_lines(struct Depsgraph *depsgraph, struct LineartGpencilModifierData *lmd)
Definition: lineart_cpu.c:3651
eLineartTriangleFlags
Definition: MOD_lineart.h:316
@ LRT_TRIANGLE_NO_INTERSECTION
Definition: MOD_lineart.h:322
@ LRT_CULL_GENERATED
Definition: MOD_lineart.h:320
@ LRT_CULL_DISCARD
Definition: MOD_lineart.h:319
@ LRT_CULL_DONT_CARE
Definition: MOD_lineart.h:317
@ LRT_TRIANGLE_INTERSECTION_ONLY
Definition: MOD_lineart.h:321
@ LRT_CULL_USED
Definition: MOD_lineart.h:318
void MOD_lineart_chain_feature_lines(LineartRenderBuffer *rb)
LineartBoundingArea * MOD_lineart_get_bounding_area(LineartRenderBuffer *rb, double x, double y)
Definition: lineart_cpu.c:3342
eLineArtElementNodeFlag
Definition: MOD_lineart.h:79
@ LRT_ELEMENT_NO_INTERSECTION
Definition: MOD_lineart.h:82
@ LRT_ELEMENT_BORDER_ONLY
Definition: MOD_lineart.h:81
@ LRT_ELEMENT_IS_ADDITIONAL
Definition: MOD_lineart.h:80
struct LineartElementLinkNode LineartElementLinkNode
struct LineartLineSegment LineartLineSegment
float MOD_lineart_chain_compute_length(LineartLineChain *rlc)
void MOD_lineart_chain_split_angle(LineartRenderBuffer *rb, float angle_threshold_rad)
struct LineartTriangle LineartTriangle
struct LineartVert LineartVert
ATTR_WARN_UNUSED_RESULT const BMVert * v
const Depsgraph * depsgraph
struct LineartBoundingArea * child
Definition: MOD_lineart.h:381
ListBase linked_triangles
Definition: MOD_lineart.h:390
ListBase linked_chains
Definition: MOD_lineart.h:394
struct LineartChainRegisterEntry * next
Definition: MOD_lineart.h:198
LineartLineChain * rlc
Definition: MOD_lineart.h:199
struct LineartChainRegisterEntry * prev
Definition: MOD_lineart.h:198
LineartLineChainItem * rlci
Definition: MOD_lineart.h:200
struct LineartVert * v2
Definition: MOD_lineart.h:143
ListBase segments
Definition: MOD_lineart.h:150
struct LineartEdge * next
Definition: MOD_lineart.h:142
unsigned char flags
Definition: MOD_lineart.h:154
struct LineartTriangle * t2
Definition: MOD_lineart.h:149
struct LineartTriangle * t1
Definition: MOD_lineart.h:149
struct Object * object_ref
Definition: MOD_lineart.h:163
struct LineartVert * v1
Definition: MOD_lineart.h:143
eLineArtElementNodeFlag flags
Definition: MOD_lineart.h:90
struct LineartElementLinkNode * prev
Definition: MOD_lineart.h:86
struct LineartElementLinkNode * next
Definition: MOD_lineart.h:86
struct LineartLineChainItem * next
Definition: MOD_lineart.h:185
struct LineartLineChainItem * prev
Definition: MOD_lineart.h:185
unsigned char transparency_mask
Definition: MOD_lineart.h:193
struct LineartLineChain * next
Definition: MOD_lineart.h:167
struct LineartLineChain * prev
Definition: MOD_lineart.h:167
unsigned char transparency_mask
Definition: MOD_lineart.h:179
struct Object * object_ref
Definition: MOD_lineart.h:181
struct LineartLineSegment * prev
Definition: MOD_lineart.h:97
struct LineartLineSegment * next
Definition: MOD_lineart.h:97
unsigned char transparency_mask
Definition: MOD_lineart.h:109
unsigned char occlusion
Definition: MOD_lineart.h:101
ListBase line_buffer_pointers
Definition: MOD_lineart.h:223
struct LineartRenderBuffer * next
Definition: MOD_lineart.h:209
LineartEdge * edge_mark_managed
Definition: MOD_lineart.h:263
float cam_obmat[4][4]
Definition: MOD_lineart.h:293
struct LineartBoundingArea * initial_bounding_areas
Definition: MOD_lineart.h:219
unsigned int edge_mark_count
Definition: MOD_lineart.h:261
unsigned int bounding_area_count
Definition: MOD_lineart.h:220
unsigned int intersection_count
Definition: MOD_lineart.h:246
LineartEdge * edge_marks
Definition: MOD_lineart.h:264
unsigned int material_line_count
Definition: MOD_lineart.h:256
ListBase triangle_buffer_pointers
Definition: MOD_lineart.h:224
LineartEdge * material_lines
Definition: MOD_lineart.h:259
LineartEdge * intersection_managed
Definition: MOD_lineart.h:248
LineartStaticMemPool render_data_pool
Definition: MOD_lineart.h:231
ListBase intersecting_vertex_buffer
Definition: MOD_lineart.h:229
struct LineartRenderBuffer * prev
Definition: MOD_lineart.h:209
double view_projection[4][4]
Definition: MOD_lineart.h:217
unsigned int intersection_processed
Definition: MOD_lineart.h:247
unsigned int crease_count
Definition: MOD_lineart.h:251
LineartEdge * material_managed
Definition: MOD_lineart.h:258
double camera_pos[3]
Definition: MOD_lineart.h:294
unsigned int contour_count
Definition: MOD_lineart.h:240
struct Collection * _source_collection
Definition: MOD_lineart.h:306
ListBase triangle_adjacent_pointers
Definition: MOD_lineart.h:227
unsigned int edge_mark_processed
Definition: MOD_lineart.h:262
LineartEdge * intersection_lines
Definition: MOD_lineart.h:249
float chaining_image_threshold
Definition: MOD_lineart.h:298
unsigned int material_processed
Definition: MOD_lineart.h:257
double material_transparency
Definition: MOD_lineart.h:278
LineartEdge * crease_lines
Definition: MOD_lineart.h:254
float angle_splitting_threshold
Definition: MOD_lineart.h:299
LineartEdge * contours
Definition: MOD_lineart.h:244
unsigned int crease_processed
Definition: MOD_lineart.h:252
struct Object * _source_object
Definition: MOD_lineart.h:307
ListBase vertex_buffer_pointers
Definition: MOD_lineart.h:222
LineartEdge * crease_managed
Definition: MOD_lineart.h:253
double view_vector[3]
Definition: MOD_lineart.h:236
unsigned int contour_processed
Definition: MOD_lineart.h:241
LineartEdge * contour_managed
Definition: MOD_lineart.h:242
LineartEdge * material
Definition: MOD_lineart.h:344
LineartEdge * edge_mark_end
Definition: MOD_lineart.h:348
LineartEdge * intersection_end
Definition: MOD_lineart.h:339
LineartEdge * crease
Definition: MOD_lineart.h:341
LineartEdge * intersection
Definition: MOD_lineart.h:338
LineartEdge * material_end
Definition: MOD_lineart.h:345
LineartEdge * edge_mark
Definition: MOD_lineart.h:347
LineartEdge * contour_end
Definition: MOD_lineart.h:336
LineartEdge * crease_end
Definition: MOD_lineart.h:342
struct LineartRenderBuffer * rb
Definition: MOD_lineart.h:331
LineartEdge * contour
Definition: MOD_lineart.h:335
struct LineartEdge * e[3]
Definition: MOD_lineart.h:45
struct LineartEdge * testing_e[1]
Definition: MOD_lineart.h:76
struct LineartTriangle base
Definition: MOD_lineart.h:67
unsigned char flags
Definition: MOD_lineart.h:56
struct LinkNode * intersecting_verts
Definition: MOD_lineart.h:63
double gn[3]
Definition: MOD_lineart.h:52
unsigned char transparency_mask
Definition: MOD_lineart.h:55
struct LineartVert * v[3]
Definition: MOD_lineart.h:49
struct LineartTriangle * intersecting_with
Definition: MOD_lineart.h:132
struct LineartVert base
Definition: MOD_lineart.h:129
double fbcoord[4]
Definition: MOD_lineart.h:114
double gloc[3]
Definition: MOD_lineart.h:113
ccl_device_inline float2 fabs(const float2 &a)