da0070f0c09d9e520c68d920ba8dcd5748311d52
[blender.git] / source / blender / blenkernel / intern / mask_rasterize.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2012 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation,
22  *                 Campbell Barton
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/blenkernel/intern/mask_rasterize.c
28  *  \ingroup bke
29  */
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_vec_types.h"
34 #include "DNA_mask_types.h"
35 #include "DNA_scene_types.h"
36
37 #include "BLI_utildefines.h"
38 #include "BLI_scanfill.h"
39 #include "BLI_memarena.h"
40
41 #include "BLI_math.h"
42 #include "BLI_rect.h"
43 #include "BLI_listbase.h"
44 #include "BLI_linklist.h"
45
46 #include "BKE_mask.h"
47
48 #ifndef USE_RASKTER
49
50 /* this is rather and annoying hack, use define to isolate it.
51  * problem is caused by scanfill removing edges on us. */
52 #define USE_SCANFILL_EDGE_WORKAROUND
53
54 #define SPLINE_RESOL_CAP_PER_PIXEL 2
55 #define SPLINE_RESOL_CAP_MIN 8
56 #define SPLINE_RESOL_CAP_MAX 64
57
58 /* found this gives best performance for high detail masks, values between 2 and 8 work best */
59 #define BUCKET_PIXELS_PER_CELL 4
60
61 #define SF_EDGE_IS_BOUNDARY 0xff
62 #define SF_KEYINDEX_TEMP_ID ((unsigned int) -1)
63
64 #define TRI_TERMINATOR_ID   ((unsigned int) -1)
65 #define TRI_VERT            ((unsigned int) -1)
66
67 /* for debugging add... */
68 #ifndef NDEBUG
69 /*      printf("%u %u %u %u\n", _t[0], _t[1], _t[2], _t[3]); \ */
70 #  define FACE_ASSERT(face, vert_max)                    \
71 {                                                        \
72         unsigned int *_t = face;                             \
73         BLI_assert(_t[0] < vert_max);                        \
74         BLI_assert(_t[1] < vert_max);                        \
75         BLI_assert(_t[2] < vert_max);                        \
76         BLI_assert(_t[3] < vert_max || _t[3] == TRI_VERT);   \
77 } (void)0
78 #else
79    /* do nothing */
80 #  define FACE_ASSERT(face, vert_max)
81 #endif
82
83 static void rotate_point_v2(float r_p[2], const float p[2], const float cent[2], const float angle, const float asp[2])
84 {
85         const float s = sinf(angle);
86         const float c = cosf(angle);
87         float p_new[2];
88
89         /* translate point back to origin */
90         r_p[0] = (p[0] - cent[0]) / asp[0];
91         r_p[1] = (p[1] - cent[1]) / asp[1];
92
93         /* rotate point */
94         p_new[0] = ((r_p[0] * c) - (r_p[1] * s)) * asp[0];
95         p_new[1] = ((r_p[0] * s) + (r_p[1] * c)) * asp[1];
96
97         /* translate point back */
98         r_p[0] = p_new[0] + cent[0];
99         r_p[1] = p_new[1] + cent[1];
100 }
101
102 BLI_INLINE unsigned int clampis_uint(const unsigned int v, const unsigned int min, const unsigned int max)
103 {
104         return v < min ? min : (v > max ? max : v);
105 }
106
107 /* --------------------------------------------------------------------- */
108 /* local structs for mask rasterizeing                                   */
109 /* --------------------------------------------------------------------- */
110
111 /**
112  * A single #MaskRasterHandle contains multiple #MaskRasterLayer's,
113  * each #MaskRasterLayer does its own lookup which contributes to
114  * the final pixel with its own blending mode and the final pixel
115  * is blended between these.
116  */
117
118 /* internal use only */
119 typedef struct MaskRasterLayer {
120         /* geometry */
121         unsigned int   face_tot;
122         unsigned int (*face_array)[4];  /* access coords tri/quad */
123         float        (*face_coords)[3]; /* xy, z 0-1 (1.0 == filled) */
124
125
126         /* 2d bounds (to quickly skip bucket lookup) */
127         rctf bounds;
128
129
130         /* buckets */
131         unsigned int **buckets_face;
132         /* cache divide and subtract */
133         float buckets_xy_scalar[2]; /* (1.0 / (buckets_width + FLT_EPSILON)) * buckets_x */
134         unsigned int buckets_x;
135         unsigned int buckets_y;
136
137
138         /* copied direct from #MaskLayer.--- */
139         /* blending options */
140         float  alpha;
141         char   blend;
142         char   blend_flag;
143         char   falloff;
144
145 } MaskRasterLayer;
146
147 typedef struct MaskRasterSplineInfo {
148         /* body of the spline */
149         unsigned int vertex_offset;
150         unsigned int vertex_total;
151
152         /* capping for non-filled, non cyclic splines */
153         unsigned int vertex_total_cap_head;
154         unsigned int vertex_total_cap_tail;
155
156         unsigned int is_cyclic;
157 } MaskRasterSplineInfo;
158
159 /**
160  * opaque local struct for mask pixel lookup, each MaskLayer needs one of these
161  */
162 struct MaskRasterHandle {
163         MaskRasterLayer *layers;
164         unsigned int     layers_tot;
165
166         /* 2d bounds (to quickly skip bucket lookup) */
167         rctf bounds;
168 };
169
170 /* --------------------------------------------------------------------- */
171 /* alloc / free functions                                                */
172 /* --------------------------------------------------------------------- */
173
174 MaskRasterHandle *BKE_maskrasterize_handle_new(void)
175 {
176         MaskRasterHandle *mr_handle;
177
178         mr_handle = MEM_callocN(sizeof(MaskRasterHandle), STRINGIFY(MaskRasterHandle));
179
180         return mr_handle;
181 }
182
183 void BKE_maskrasterize_handle_free(MaskRasterHandle *mr_handle)
184 {
185         const unsigned int layers_tot = mr_handle->layers_tot;
186         unsigned int i;
187         MaskRasterLayer *layer = mr_handle->layers;
188
189         for (i = 0; i < layers_tot; i++, layer++) {
190
191                 if (layer->face_array) {
192                         MEM_freeN(layer->face_array);
193                 }
194
195                 if (layer->face_coords) {
196                         MEM_freeN(layer->face_coords);
197                 }
198
199                 if (layer->buckets_face) {
200                         const unsigned int   bucket_tot = layer->buckets_x * layer->buckets_y;
201                         unsigned int bucket_index;
202                         for (bucket_index = 0; bucket_index < bucket_tot; bucket_index++) {
203                                 unsigned int *face_index = layer->buckets_face[bucket_index];
204                                 if (face_index) {
205                                         MEM_freeN(face_index);
206                                 }
207                         }
208
209                         MEM_freeN(layer->buckets_face);
210                 }
211         }
212
213         MEM_freeN(mr_handle->layers);
214         MEM_freeN(mr_handle);
215 }
216
217
218 void maskrasterize_spline_differentiate_point_outset(float (*diff_feather_points)[2], float (*diff_points)[2],
219                                                      const unsigned int tot_diff_point, const float ofs,
220                                                      const short do_test)
221 {
222         unsigned int k_prev = tot_diff_point - 2;
223         unsigned int k_curr = tot_diff_point - 1;
224         unsigned int k_next = 0;
225
226         unsigned int k;
227
228         float d_prev[2];
229         float d_next[2];
230         float d[2];
231
232         const float *co_prev;
233         const float *co_curr;
234         const float *co_next;
235
236         const float ofs_squared = ofs * ofs;
237
238         co_prev = diff_points[k_prev];
239         co_curr = diff_points[k_curr];
240         co_next = diff_points[k_next];
241
242         /* precalc */
243         sub_v2_v2v2(d_prev, co_prev, co_curr);
244         normalize_v2(d_prev);
245
246         for (k = 0; k < tot_diff_point; k++) {
247
248                 /* co_prev = diff_points[k_prev]; */ /* precalc */
249                 co_curr = diff_points[k_curr];
250                 co_next = diff_points[k_next];
251
252                 /* sub_v2_v2v2(d_prev, co_prev, co_curr); */ /* precalc */
253                 sub_v2_v2v2(d_next, co_curr, co_next);
254
255                 /* normalize_v2(d_prev); */ /* precalc */
256                 normalize_v2(d_next);
257
258                 if ((do_test == FALSE) ||
259                     (len_squared_v2v2(diff_feather_points[k], diff_points[k]) < ofs_squared))
260                 {
261
262                         add_v2_v2v2(d, d_prev, d_next);
263
264                         normalize_v2(d);
265
266                         diff_feather_points[k][0] = diff_points[k][0] + ( d[1] * ofs);
267                         diff_feather_points[k][1] = diff_points[k][1] + (-d[0] * ofs);
268                 }
269
270                 /* use next iter */
271                 copy_v2_v2(d_prev, d_next);
272
273                 /* k_prev = k_curr; */ /* precalc */
274                 k_curr = k_next;
275                 k_next++;
276         }
277 }
278
279 /* this function is not exact, sometimes it returns false positives,
280  * the main point of it is to clear out _almost_ all bucket/face non-intersections,
281  * returning TRUE in corner cases is ok but missing an intersection is NOT.
282  *
283  * method used
284  * - check if the center of the buckets bounding box is intersecting the face
285  * - if not get the max radius to a corner of the bucket and see how close we
286  *   are to any of the triangle edges.
287  */
288 static int layer_bucket_isect_test(MaskRasterLayer *layer, unsigned int face_index,
289                                    const unsigned int bucket_x, const unsigned int bucket_y,
290                                    const float bucket_size_x, const float bucket_size_y,
291                                    const float bucket_max_rad_squared)
292 {
293         unsigned int *face = layer->face_array[face_index];
294         float (*cos)[3] = layer->face_coords;
295
296         const float xmin = layer->bounds.xmin + (bucket_size_x * bucket_x);
297         const float ymin = layer->bounds.ymin + (bucket_size_y * bucket_y);
298         const float xmax = xmin + bucket_size_x;
299         const float ymax = ymin + bucket_size_y;
300
301         const float cent[2] = {(xmin + xmax) * 0.5f,
302                                (ymin + ymax) * 0.5f};
303
304         if (face[3] == TRI_VERT) {
305                 const float *v1 = cos[face[0]];
306                 const float *v2 = cos[face[1]];
307                 const float *v3 = cos[face[2]];
308
309                 if (isect_point_tri_v2(cent, v1, v2, v3)) {
310                         return TRUE;
311                 }
312                 else {
313                         if ((dist_squared_to_line_segment_v2(cent, v1, v2) < bucket_max_rad_squared) ||
314                                 (dist_squared_to_line_segment_v2(cent, v2, v3) < bucket_max_rad_squared) ||
315                                 (dist_squared_to_line_segment_v2(cent, v3, v1) < bucket_max_rad_squared))
316                         {
317                                 return TRUE;
318                         }
319                         else {
320                                 // printf("skip tri\n");
321                                 return FALSE;
322                         }
323                 }
324
325         }
326         else {
327                 const float *v1 = cos[face[0]];
328                 const float *v2 = cos[face[1]];
329                 const float *v3 = cos[face[2]];
330                 const float *v4 = cos[face[3]];
331
332                 if (isect_point_tri_v2(cent, v1, v2, v3)) {
333                         return TRUE;
334                 }
335                 else if (isect_point_tri_v2(cent, v1, v3, v4)) {
336                         return TRUE;
337                 }
338                 else {
339                         if ((dist_squared_to_line_segment_v2(cent, v1, v2) < bucket_max_rad_squared) ||
340                             (dist_squared_to_line_segment_v2(cent, v2, v3) < bucket_max_rad_squared) ||
341                             (dist_squared_to_line_segment_v2(cent, v3, v4) < bucket_max_rad_squared) ||
342                             (dist_squared_to_line_segment_v2(cent, v4, v1) < bucket_max_rad_squared))
343                         {
344                                 return TRUE;
345                         }
346                         else {
347                                 // printf("skip quad\n");
348                                 return FALSE;
349                         }
350                 }
351         }
352 }
353
354 static void layer_bucket_init_dummy(MaskRasterLayer *layer)
355 {
356         layer->face_tot = 0;
357         layer->face_coords = NULL;
358         layer->face_array  = NULL;
359
360         layer->buckets_x = 0;
361         layer->buckets_y = 0;
362
363         layer->buckets_xy_scalar[0] = 0.0f;
364         layer->buckets_xy_scalar[1] = 0.0f;
365
366         layer->buckets_face = NULL;
367
368         BLI_rctf_init(&layer->bounds, -1.0f, -1.0f, -1.0f, -1.0f);
369 }
370
371 static void layer_bucket_init(MaskRasterLayer *layer, const float pixel_size)
372 {
373         MemArena *arena = BLI_memarena_new(1 << 16, __func__);
374
375         const float bucket_dim_x = layer->bounds.xmax - layer->bounds.xmin;
376         const float bucket_dim_y = layer->bounds.ymax - layer->bounds.ymin;
377
378         layer->buckets_x = (bucket_dim_x / pixel_size) / (float)BUCKET_PIXELS_PER_CELL;
379         layer->buckets_y = (bucket_dim_y / pixel_size) / (float)BUCKET_PIXELS_PER_CELL;
380
381 //              printf("bucket size %ux%u\n", layer->buckets_x, layer->buckets_y);
382
383         CLAMP(layer->buckets_x, 8, 512);
384         CLAMP(layer->buckets_y, 8, 512);
385
386         layer->buckets_xy_scalar[0] = (1.0f / (bucket_dim_x + FLT_EPSILON)) * layer->buckets_x;
387         layer->buckets_xy_scalar[1] = (1.0f / (bucket_dim_y + FLT_EPSILON)) * layer->buckets_y;
388
389         {
390                 /* width and height of each bucket */
391                 const float bucket_size_x = (bucket_dim_x + FLT_EPSILON) / layer->buckets_x;
392                 const float bucket_size_y = (bucket_dim_y + FLT_EPSILON) / layer->buckets_y;
393                 const float bucket_max_rad = (maxf(bucket_size_x, bucket_size_y) * M_SQRT2) + FLT_EPSILON;
394                 const float bucket_max_rad_squared = bucket_max_rad * bucket_max_rad;
395
396                 unsigned int *face = &layer->face_array[0][0];
397                 float (*cos)[3] = layer->face_coords;
398
399                 const unsigned int  bucket_tot = layer->buckets_x * layer->buckets_y;
400                 LinkNode     **bucketstore     = MEM_callocN(bucket_tot * sizeof(LinkNode *),  __func__);
401                 unsigned int  *bucketstore_tot = MEM_callocN(bucket_tot * sizeof(unsigned int), __func__);
402
403                 unsigned int face_index;
404
405                 for (face_index = 0; face_index < layer->face_tot; face_index++, face += 4) {
406                         float xmin;
407                         float xmax;
408                         float ymin;
409                         float ymax;
410
411                         if (face[3] == TRI_VERT) {
412                                 const float *v1 = cos[face[0]];
413                                 const float *v2 = cos[face[1]];
414                                 const float *v3 = cos[face[2]];
415
416                                 xmin = minf(v1[0], minf(v2[0], v3[0]));
417                                 xmax = maxf(v1[0], maxf(v2[0], v3[0]));
418                                 ymin = minf(v1[1], minf(v2[1], v3[1]));
419                                 ymax = maxf(v1[1], maxf(v2[1], v3[1]));
420                         }
421                         else {
422                                 const float *v1 = cos[face[0]];
423                                 const float *v2 = cos[face[1]];
424                                 const float *v3 = cos[face[2]];
425                                 const float *v4 = cos[face[3]];
426
427                                 xmin = minf(v1[0], minf(v2[0], minf(v3[0], v4[0])));
428                                 xmax = maxf(v1[0], maxf(v2[0], maxf(v3[0], v4[0])));
429                                 ymin = minf(v1[1], minf(v2[1], minf(v3[1], v4[1])));
430                                 ymax = maxf(v1[1], maxf(v2[1], maxf(v3[1], v4[1])));
431                         }
432
433
434                         /* not essential but may as will skip any faces outside the view */
435                         if (!((xmax < 0.0f) || (ymax < 0.0f) || (xmin > 1.0f) || (ymin > 1.0f))) {
436
437                                 CLAMP(xmin, 0.0f,  1.0f);
438                                 CLAMP(ymin, 0.0f,  1.0f);
439                                 CLAMP(xmax, 0.0f,  1.0f);
440                                 CLAMP(ymax, 0.0f,  1.0f);
441
442                                 {
443                                         unsigned int xi_min = (unsigned int) ((xmin - layer->bounds.xmin) * layer->buckets_xy_scalar[0]);
444                                         unsigned int xi_max = (unsigned int) ((xmax - layer->bounds.xmin) * layer->buckets_xy_scalar[0]);
445                                         unsigned int yi_min = (unsigned int) ((ymin - layer->bounds.ymin) * layer->buckets_xy_scalar[1]);
446                                         unsigned int yi_max = (unsigned int) ((ymax - layer->bounds.ymin) * layer->buckets_xy_scalar[1]);
447                                         void *face_index_void = SET_UINT_IN_POINTER(face_index);
448
449                                         unsigned int xi, yi;
450
451                                         /* this should _almost_ never happen but since it can in extreme cases,
452                                          * we have to clamp the values or we overrun the buffer and crash */
453                                         CLAMP(xi_min, 0, layer->buckets_x - 1);
454                                         CLAMP(xi_max, 0, layer->buckets_x - 1);
455                                         CLAMP(yi_min, 0, layer->buckets_y - 1);
456                                         CLAMP(yi_max, 0, layer->buckets_y - 1);
457
458                                         for (yi = yi_min; yi <= yi_max; yi++) {
459                                                 unsigned int bucket_index = (layer->buckets_x * yi) + xi_min;
460                                                 for (xi = xi_min; xi <= xi_max; xi++, bucket_index++) {
461                                                         // unsigned int bucket_index = (layer->buckets_x * yi) + xi; /* correct but do in outer loop */
462
463                                                         BLI_assert(xi < layer->buckets_x);
464                                                         BLI_assert(yi < layer->buckets_y);
465                                                         BLI_assert(bucket_index < bucket_tot);
466
467                                                         /* check if the bucket intersects with the face */
468                                                         /* note: there is a tradeoff here since checking box/tri intersections isn't
469                                                          * as optimal as it could be, but checking pixels against faces they will never intersect
470                                                          * with is likely the greater slowdown here - so check if the cell intersects the face */
471                                                         if (layer_bucket_isect_test(layer, face_index,
472                                                                                     xi, yi,
473                                                                                     bucket_size_x, bucket_size_y,
474                                                                                     bucket_max_rad_squared))
475                                                         {
476                                                                 BLI_linklist_prepend_arena(&bucketstore[bucket_index], face_index_void, arena);
477                                                                 bucketstore_tot[bucket_index]++;
478                                                         }
479                                                 }
480                                         }
481                                 }
482                         }
483                 }
484
485                 if (1) {
486                         /* now convert linknodes into arrays for faster per pixel access */
487                         unsigned int  **buckets_face = MEM_mallocN(bucket_tot * sizeof(unsigned int **), __func__);
488                         unsigned int bucket_index;
489
490                         for (bucket_index = 0; bucket_index < bucket_tot; bucket_index++) {
491                                 if (bucketstore_tot[bucket_index]) {
492                                         unsigned int  *bucket = MEM_mallocN((bucketstore_tot[bucket_index] + 1) * sizeof(unsigned int),
493                                                                             __func__);
494                                         LinkNode *bucket_node;
495
496                                         buckets_face[bucket_index] = bucket;
497
498                                         for (bucket_node = bucketstore[bucket_index]; bucket_node; bucket_node = bucket_node->next) {
499                                                 *bucket = GET_UINT_FROM_POINTER(bucket_node->link);
500                                                 bucket++;
501                                         }
502                                         *bucket = TRI_TERMINATOR_ID;
503                                 }
504                                 else {
505                                         buckets_face[bucket_index] = NULL;
506                                 }
507                         }
508
509                         layer->buckets_face = buckets_face;
510                 }
511
512                 MEM_freeN(bucketstore);
513                 MEM_freeN(bucketstore_tot);
514         }
515
516         BLI_memarena_free(arena);
517 }
518
519 void BKE_maskrasterize_handle_init(MaskRasterHandle *mr_handle, struct Mask *mask,
520                                    const int width, const int height,
521                                    const short do_aspect_correct, const short do_mask_aa,
522                                    const short do_feather)
523 {
524         const rctf default_bounds = {0.0f, 1.0f, 0.0f, 1.0f};
525         const float pixel_size = 1.0f / MIN2(width, height);
526         const float asp_xy[2] = {(do_aspect_correct && width > height) ? (float)height / (float)width  : 1.0f,
527                                  (do_aspect_correct && width < height) ? (float)width  / (float)height : 1.0f};
528
529         const float zvec[3] = {0.0f, 0.0f, 1.0f};
530         MaskLayer *masklay;
531         unsigned int masklay_index;
532
533         mr_handle->layers_tot = BLI_countlist(&mask->masklayers);
534         mr_handle->layers = MEM_mallocN(sizeof(MaskRasterLayer) * mr_handle->layers_tot, STRINGIFY(MaskRasterLayer));
535         BLI_rctf_init_minmax(&mr_handle->bounds);
536
537         for (masklay = mask->masklayers.first, masklay_index = 0; masklay; masklay = masklay->next, masklay_index++) {
538
539                 /* we need to store vertex ranges for open splines for filling */
540                 unsigned int tot_splines;
541                 MaskRasterSplineInfo *open_spline_ranges;
542                 unsigned int   open_spline_index = 0;
543
544                 MaskSpline *spline;
545
546                 /* scanfill */
547                 ScanFillContext sf_ctx;
548                 ScanFillVert *sf_vert = NULL;
549                 ScanFillVert *sf_vert_next = NULL;
550                 ScanFillFace *sf_tri;
551
552                 unsigned int sf_vert_tot = 0;
553                 unsigned int tot_feather_quads = 0;
554
555 #ifdef USE_SCANFILL_EDGE_WORKAROUND
556                 unsigned int tot_boundary_used = 0;
557                 unsigned int tot_boundary_found = 0;
558 #endif
559
560                 if (masklay->restrictflag & MASK_RESTRICT_RENDER) {
561                         /* skip the layer */
562                         mr_handle->layers_tot--;
563                         masklay_index--;
564                         continue;
565                 }
566
567                 tot_splines = BLI_countlist(&masklay->splines);
568                 open_spline_ranges = MEM_callocN(sizeof(*open_spline_ranges) * tot_splines, __func__);
569
570                 BLI_scanfill_begin(&sf_ctx);
571
572                 for (spline = masklay->splines.first; spline; spline = spline->next) {
573                         const unsigned int is_cyclic = (spline->flag & MASK_SPLINE_CYCLIC) != 0;
574                         const unsigned int is_fill = (spline->flag & MASK_SPLINE_NOFILL) == 0;
575
576                         float (*diff_points)[2];
577                         int tot_diff_point;
578
579                         float (*diff_feather_points)[2];
580                         int tot_diff_feather_points;
581
582                         const int resol_a = BKE_mask_spline_resolution(spline, width, height) / 4;
583                         const int resol_b = BKE_mask_spline_feather_resolution(spline, width, height) / 4;
584                         const int resol = CLAMPIS(MAX2(resol_a, resol_b), 4, 512);
585
586                         diff_points = BKE_mask_spline_differentiate_with_resolution_ex(
587                                           spline, &tot_diff_point, resol);
588
589                         if (do_feather) {
590                                 diff_feather_points = BKE_mask_spline_feather_differentiated_points_with_resolution_ex(
591                                                           spline, &tot_diff_feather_points, resol, TRUE);
592                                 BLI_assert(diff_feather_points);
593                         }
594                         else {
595                                 tot_diff_feather_points = 0;
596                                 diff_feather_points = NULL;
597                         }
598
599                         if (tot_diff_point > 3) {
600                                 ScanFillVert *sf_vert_prev;
601                                 int j;
602
603                                 float co[3];
604                                 co[2] = 0.0f;
605
606                                 if (do_aspect_correct) {
607                                         if (width != height) {
608                                                 float *fp;
609                                                 float *ffp;
610                                                 int i;
611                                                 float asp;
612
613                                                 if (width < height) {
614                                                         fp = &diff_points[0][0];
615                                                         ffp = tot_diff_feather_points ? &diff_feather_points[0][0] : NULL;
616                                                         asp = (float)width / (float)height;
617                                                 }
618                                                 else {
619                                                         fp = &diff_points[0][1];
620                                                         ffp = tot_diff_feather_points ? &diff_feather_points[0][1] : NULL;
621                                                         asp = (float)height / (float)width;
622                                                 }
623
624                                                 for (i = 0; i < tot_diff_point; i++, fp += 2) {
625                                                         (*fp) = (((*fp) - 0.5f) / asp) + 0.5f;
626                                                 }
627
628                                                 if (tot_diff_feather_points) {
629                                                         for (i = 0; i < tot_diff_feather_points; i++, ffp += 2) {
630                                                                 (*ffp) = (((*ffp) - 0.5f) / asp) + 0.5f;
631                                                         }
632                                                 }
633                                         }
634                                 }
635
636                                 /* fake aa, using small feather */
637                                 if (do_mask_aa == TRUE) {
638                                         if (do_feather == FALSE) {
639                                                 tot_diff_feather_points = tot_diff_point;
640                                                 diff_feather_points = MEM_mallocN(sizeof(*diff_feather_points) * tot_diff_feather_points,
641                                                                                   __func__);
642                                                 /* add single pixel feather */
643                                                 maskrasterize_spline_differentiate_point_outset(diff_feather_points, diff_points,
644                                                                                                tot_diff_point, pixel_size, FALSE);
645                                         }
646                                         else {
647                                                 /* ensure single pixel feather, on any zero feather areas */
648                                                 maskrasterize_spline_differentiate_point_outset(diff_feather_points, diff_points,
649                                                                                                tot_diff_point, pixel_size, TRUE);
650                                         }
651                                 }
652
653                                 if (is_fill) {
654                                         copy_v2_v2(co, diff_points[0]);
655                                         sf_vert_prev = BLI_scanfill_vert_add(&sf_ctx, co);
656                                         sf_vert_prev->tmp.u = sf_vert_tot;
657                                         sf_vert_prev->keyindex = sf_vert_tot + tot_diff_point; /* absolute index of feather vert */
658                                         sf_vert_tot++;
659
660                                         /* TODO, an alternate functions so we can avoid double vector copy! */
661                                         for (j = 1; j < tot_diff_point; j++) {
662                                                 copy_v2_v2(co, diff_points[j]);
663                                                 sf_vert = BLI_scanfill_vert_add(&sf_ctx, co);
664                                                 sf_vert->tmp.u = sf_vert_tot;
665                                                 sf_vert->keyindex = sf_vert_tot + tot_diff_point; /* absolute index of feather vert */
666                                                 sf_vert_tot++;
667                                         }
668
669                                         sf_vert = sf_vert_prev;
670                                         sf_vert_prev = sf_ctx.fillvertbase.last;
671
672                                         for (j = 0; j < tot_diff_point; j++) {
673                                                 ScanFillEdge *sf_edge = BLI_scanfill_edge_add(&sf_ctx, sf_vert_prev, sf_vert);
674
675 #ifdef USE_SCANFILL_EDGE_WORKAROUND
676                                                 if (diff_feather_points) {
677                                                         sf_edge->tmp.c = SF_EDGE_IS_BOUNDARY;
678                                                         tot_boundary_used++;
679                                                 }
680 #else
681                                                 (void)sf_edge;
682 #endif
683                                                 sf_vert_prev = sf_vert;
684                                                 sf_vert = sf_vert->next;
685                                         }
686
687                                         if (diff_feather_points) {
688                                                 float co_feather[3];
689                                                 co_feather[2] = 1.0f;
690
691                                                 BLI_assert(tot_diff_feather_points == tot_diff_point);
692
693                                                 /* note: only added for convenience, we don't infact use these to scanfill,
694                                                  * only to create feather faces after scanfill */
695                                                 for (j = 0; j < tot_diff_feather_points; j++) {
696                                                         copy_v2_v2(co_feather, diff_feather_points[j]);
697                                                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, co_feather);
698
699                                                         /* no need for these attrs */
700         #if 0
701                                                         sf_vert->tmp.u = sf_vert_tot;
702                                                         sf_vert->keyindex = sf_vert_tot + tot_diff_point; /* absolute index of feather vert */
703         #endif
704                                                         sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
705                                                         sf_vert_tot++;
706                                                 }
707
708                                                 tot_feather_quads += tot_diff_point;
709                                         }
710                                 }
711                                 else {
712                                         /* unfilled spline */
713                                         if (diff_feather_points) {
714
715                                                 float co_diff[3];
716
717                                                 float co_feather[3];
718                                                 co_feather[2] = 1.0f;
719
720                                                 open_spline_ranges[open_spline_index].vertex_offset = sf_vert_tot;
721                                                 open_spline_ranges[open_spline_index].vertex_total = tot_diff_point;
722
723                                                 /* TODO, an alternate functions so we can avoid double vector copy! */
724                                                 for (j = 0; j < tot_diff_point; j++) {
725
726                                                         /* center vert */
727                                                         copy_v2_v2(co, diff_points[j]);
728                                                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, co);
729                                                         sf_vert->tmp.u = sf_vert_tot;
730                                                         sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
731                                                         sf_vert_tot++;
732
733
734                                                         /* feather vert A */
735                                                         copy_v2_v2(co_feather, diff_feather_points[j]);
736                                                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, co_feather);
737                                                         sf_vert->tmp.u = sf_vert_tot;
738                                                         sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
739                                                         sf_vert_tot++;
740
741
742                                                         /* feather vert B */
743                                                         sub_v2_v2v2(co_diff, co, co_feather);
744                                                         add_v2_v2v2(co_feather, co, co_diff);
745                                                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, co_feather);
746                                                         sf_vert->tmp.u = sf_vert_tot;
747                                                         sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
748                                                         sf_vert_tot++;
749
750                                                         tot_feather_quads += 2;
751                                                 }
752
753                                                 if (!is_cyclic) {
754                                                         tot_feather_quads -= 2;
755                                                 }
756
757                                                 /* cap ends */
758
759                                                 /* dummy init value */
760                                                 open_spline_ranges[open_spline_index].vertex_total_cap_head = 0;
761                                                 open_spline_ranges[open_spline_index].vertex_total_cap_tail = 0;
762
763                                                 if (!is_cyclic) {
764                                                         float *fp_cent;
765                                                         float *fp_turn;
766
767                                                         unsigned int k;
768
769                                                         fp_cent = diff_points[0];
770                                                         fp_turn = diff_feather_points[0];
771
772 #define CALC_CAP_RESOL                                                                      \
773         clampis_uint((len_v2v2(fp_cent, fp_turn) / (pixel_size * SPLINE_RESOL_CAP_PER_PIXEL)),  \
774                      SPLINE_RESOL_CAP_MIN,                                                      \
775                      SPLINE_RESOL_CAP_MAX)
776
777                                                         {
778                                                                 const unsigned int vertex_total_cap = CALC_CAP_RESOL;
779
780                                                                 for (k = 1; k < vertex_total_cap; k++) {
781                                                                         const float angle = (float)k * (1.0f / vertex_total_cap) * (float)M_PI;
782                                                                         rotate_point_v2(co_feather, fp_turn, fp_cent, angle, asp_xy);
783
784                                                                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, co_feather);
785                                                                         sf_vert->tmp.u = sf_vert_tot;
786                                                                         sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
787                                                                         sf_vert_tot++;
788                                                                 }
789                                                                 tot_feather_quads += vertex_total_cap;
790
791                                                                 open_spline_ranges[open_spline_index].vertex_total_cap_head = vertex_total_cap;
792                                                         }
793
794                                                         fp_cent = diff_points[tot_diff_point - 1];
795                                                         fp_turn = diff_feather_points[tot_diff_point - 1];
796
797                                                         {
798                                                                 const unsigned int vertex_total_cap = CALC_CAP_RESOL;
799
800                                                                 for (k = 1; k < vertex_total_cap; k++) {
801                                                                         const float angle = (float)k * (1.0f / vertex_total_cap) * (float)M_PI;
802                                                                         rotate_point_v2(co_feather, fp_turn, fp_cent, -angle, asp_xy);
803
804                                                                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, co_feather);
805                                                                         sf_vert->tmp.u = sf_vert_tot;
806                                                                         sf_vert->keyindex = SF_KEYINDEX_TEMP_ID;
807                                                                         sf_vert_tot++;
808                                                                 }
809                                                                 tot_feather_quads += vertex_total_cap;
810
811                                                                 open_spline_ranges[open_spline_index].vertex_total_cap_tail = vertex_total_cap;
812                                                         }
813                                                 }
814
815                                                 open_spline_ranges[open_spline_index].is_cyclic = is_cyclic;
816                                                 open_spline_index++;
817
818 #undef CALC_CAP_RESOL
819                                                 /* end capping */
820
821                                         }
822                                 }
823                         }
824
825                         if (diff_points) {
826                                 MEM_freeN(diff_points);
827                         }
828
829                         if (diff_feather_points) {
830                                 MEM_freeN(diff_feather_points);
831                         }
832                 }
833
834                 {
835                         unsigned int (*face_array)[4], *face;  /* access coords */
836                         float        (*face_coords)[3], *cos; /* xy, z 0-1 (1.0 == filled) */
837                         int sf_tri_tot;
838                         rctf bounds;
839                         int face_index;
840
841                         /* now we have all the splines */
842                         face_coords = MEM_mallocN((sizeof(float) * 3) * sf_vert_tot, "maskrast_face_coords");
843
844                         /* init bounds */
845                         BLI_rctf_init_minmax(&bounds);
846
847                         /* coords */
848                         cos = (float *)face_coords;
849                         for (sf_vert = sf_ctx.fillvertbase.first; sf_vert; sf_vert = sf_vert_next) {
850                                 sf_vert_next = sf_vert->next;
851                                 copy_v3_v3(cos, sf_vert->co);
852
853                                 /* remove so as not to interfere with fill (called after) */
854                                 if (sf_vert->keyindex == SF_KEYINDEX_TEMP_ID) {
855                                         BLI_remlink(&sf_ctx.fillvertbase, sf_vert);
856                                 }
857
858                                 /* bounds */
859                                 BLI_rctf_do_minmax_v(&bounds, cos);
860
861                                 cos += 3;
862                         }
863
864                         /* main scan-fill */
865                         sf_tri_tot = BLI_scanfill_calc_ex(&sf_ctx, FALSE, zvec);
866
867                         face_array = MEM_mallocN(sizeof(*face_array) * (sf_tri_tot + tot_feather_quads), "maskrast_face_index");
868                         face_index = 0;
869
870                         /* faces */
871                         face = (unsigned int *)face_array;
872                         for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
873                                 *(face++) = sf_tri->v3->tmp.u;
874                                 *(face++) = sf_tri->v2->tmp.u;
875                                 *(face++) = sf_tri->v1->tmp.u;
876                                 *(face++) = TRI_VERT;
877                                 face_index++;
878                                 FACE_ASSERT(face - 4, sf_vert_tot);
879                         }
880
881                         /* start of feather faces... if we have this set,
882                          * 'face_index' is kept from loop above */
883
884                         BLI_assert(face_index == sf_tri_tot);
885
886                         if (tot_feather_quads) {
887                                 ScanFillEdge *sf_edge;
888
889                                 for (sf_edge = sf_ctx.filledgebase.first; sf_edge; sf_edge = sf_edge->next) {
890                                         if (sf_edge->tmp.c == SF_EDGE_IS_BOUNDARY) {
891                                                 *(face++) = sf_edge->v1->tmp.u;
892                                                 *(face++) = sf_edge->v2->tmp.u;
893                                                 *(face++) = sf_edge->v2->keyindex;
894                                                 *(face++) = sf_edge->v1->keyindex;
895                                                 face_index++;
896                                                 FACE_ASSERT(face - 4, sf_vert_tot);
897
898 #ifdef USE_SCANFILL_EDGE_WORKAROUND
899                                                 tot_boundary_found++;
900 #endif
901                                         }
902                                 }
903                         }
904
905 #ifdef USE_SCANFILL_EDGE_WORKAROUND
906                         if (tot_boundary_found != tot_boundary_used) {
907                                 BLI_assert(tot_boundary_found < tot_boundary_used);
908                         }
909 #endif
910
911                         /* feather only splines */
912                         while (open_spline_index > 0) {
913                                 const unsigned int vertex_offset         = open_spline_ranges[--open_spline_index].vertex_offset;
914                                 unsigned int       vertex_total          = open_spline_ranges[  open_spline_index].vertex_total;
915                                 unsigned int       vertex_total_cap_head = open_spline_ranges[  open_spline_index].vertex_total_cap_head;
916                                 unsigned int       vertex_total_cap_tail = open_spline_ranges[  open_spline_index].vertex_total_cap_tail;
917                                 unsigned int k, j;
918
919                                 j = vertex_offset;
920
921                                 /* subtract one since we reference next vertex triple */
922                                 for (k = 0; k < vertex_total - 1; k++, j += 3) {
923
924                                         BLI_assert(j == vertex_offset + (k * 3));
925
926                                         *(face++) = j + 3; /* next span */ /* z 1 */
927                                         *(face++) = j + 0;                 /* z 1 */
928                                         *(face++) = j + 1;                 /* z 0 */
929                                         *(face++) = j + 4; /* next span */ /* z 0 */
930                                         face_index++;
931                                         FACE_ASSERT(face - 4, sf_vert_tot);
932
933                                         *(face++) = j + 0;                 /* z 1 */
934                                         *(face++) = j + 3; /* next span */ /* z 1 */
935                                         *(face++) = j + 5; /* next span */ /* z 0 */
936                                         *(face++) = j + 2;                 /* z 0 */
937                                         face_index++;
938                                         FACE_ASSERT(face - 4, sf_vert_tot);
939                                 }
940
941                                 if (open_spline_ranges[open_spline_index].is_cyclic) {
942                                         *(face++) = vertex_offset + 0; /* next span */ /* z 1 */
943                                         *(face++) = j             + 0;                 /* z 1 */
944                                         *(face++) = j             + 1;                 /* z 0 */
945                                         *(face++) = vertex_offset + 1; /* next span */ /* z 0 */
946                                         face_index++;
947                                         FACE_ASSERT(face - 4, sf_vert_tot);
948
949                                         *(face++) = j          + 0;                    /* z 1 */
950                                         *(face++) = vertex_offset + 0; /* next span */ /* z 1 */
951                                         *(face++) = vertex_offset + 2; /* next span */ /* z 0 */
952                                         *(face++) = j          + 2;                    /* z 0 */
953                                         face_index++;
954                                         FACE_ASSERT(face - 4, sf_vert_tot);
955                                 }
956                                 else {
957                                         unsigned int midvidx = vertex_offset;
958
959                                         /***************
960                                          * cap end 'a' */
961                                         j = midvidx + (vertex_total * 3);
962
963                                         for (k = 0; k < vertex_total_cap_head - 2; k++, j++) {
964                                                 *(face++) = midvidx + 0;  /* z 1 */
965                                                 *(face++) = midvidx + 0;  /* z 1 */
966                                                 *(face++) = j + 0;        /* z 0 */
967                                                 *(face++) = j + 1;        /* z 0 */
968                                                 face_index++;
969                                                 FACE_ASSERT(face - 4, sf_vert_tot);
970                                         }
971
972                                         j = vertex_offset + (vertex_total * 3);
973
974                                         /* 2 tris that join the original */
975                                         *(face++) = midvidx + 0;  /* z 1 */
976                                         *(face++) = midvidx + 0;  /* z 1 */
977                                         *(face++) = midvidx + 1;  /* z 0 */
978                                         *(face++) = j + 0;        /* z 0 */
979                                         face_index++;
980                                         FACE_ASSERT(face - 4, sf_vert_tot);
981
982                                         *(face++) = midvidx + 0;                    /* z 1 */
983                                         *(face++) = midvidx + 0;                    /* z 1 */
984                                         *(face++) = j + vertex_total_cap_head - 2;  /* z 0 */
985                                         *(face++) = midvidx + 2;                    /* z 0 */
986                                         face_index++;
987                                         FACE_ASSERT(face - 4, sf_vert_tot);
988
989
990                                         /***************
991                                          * cap end 'b' */
992                                         /* ... same as previous but v 2-3 flipped, and different initial offsets */
993
994                                         j = vertex_offset + (vertex_total * 3) + (vertex_total_cap_head - 1);
995
996                                         midvidx = vertex_offset + (vertex_total * 3) - 3;
997
998                                         for (k = 0; k < vertex_total_cap_tail - 2; k++, j++) {
999                                                 *(face++) = midvidx;  /* z 1 */
1000                                                 *(face++) = midvidx;  /* z 1 */
1001                                                 *(face++) = j + 1;    /* z 0 */
1002                                                 *(face++) = j + 0;    /* z 0 */
1003                                                 face_index++;
1004                                                 FACE_ASSERT(face - 4, sf_vert_tot);
1005                                         }
1006
1007                                         j = vertex_offset + (vertex_total * 3) + (vertex_total_cap_head - 1);
1008
1009                                         /* 2 tris that join the original */
1010                                         *(face++) = midvidx + 0;  /* z 1 */
1011                                         *(face++) = midvidx + 0;  /* z 1 */
1012                                         *(face++) = j + 0;        /* z 0 */
1013                                         *(face++) = midvidx + 1;  /* z 0 */
1014                                         face_index++;
1015                                         FACE_ASSERT(face - 4, sf_vert_tot);
1016
1017                                         *(face++) = midvidx + 0;                    /* z 1 */
1018                                         *(face++) = midvidx + 0;                    /* z 1 */
1019                                         *(face++) = midvidx + 2;                    /* z 0 */
1020                                         *(face++) = j + vertex_total_cap_tail - 2;  /* z 0 */
1021                                         face_index++;
1022                                         FACE_ASSERT(face - 4, sf_vert_tot);
1023
1024                                 }
1025                         }
1026
1027                         MEM_freeN(open_spline_ranges);
1028
1029 //                      fprintf(stderr, "%u %u (%u %u), %u\n", face_index, sf_tri_tot + tot_feather_quads, sf_tri_tot, tot_feather_quads, tot_boundary_used - tot_boundary_found);
1030
1031 #ifdef USE_SCANFILL_EDGE_WORKAROUND
1032                         BLI_assert(face_index + (tot_boundary_used - tot_boundary_found) == sf_tri_tot + tot_feather_quads);
1033 #else
1034                         BLI_assert(face_index == sf_tri_tot + tot_feather_quads);
1035 #endif
1036                         {
1037                                 MaskRasterLayer *layer = &mr_handle->layers[masklay_index];
1038
1039                                 if (BLI_rctf_isect(&default_bounds, &bounds, &bounds)) {
1040 #ifdef USE_SCANFILL_EDGE_WORKAROUND
1041                                         layer->face_tot = (sf_tri_tot + tot_feather_quads) - (tot_boundary_used - tot_boundary_found);
1042 #else
1043                                         layer->face_tot = (sf_tri_tot + tot_feather_quads);
1044 #endif
1045                                         layer->face_coords = face_coords;
1046                                         layer->face_array  = face_array;
1047                                         layer->bounds = bounds;
1048
1049                                         layer_bucket_init(layer, pixel_size);
1050
1051                                         BLI_rctf_union(&mr_handle->bounds, &bounds);
1052                                 }
1053                                 else {
1054                                         MEM_freeN(face_coords);
1055                                         MEM_freeN(face_array);
1056
1057                                         layer_bucket_init_dummy(layer);
1058                                 }
1059
1060                                 /* copy as-is */
1061                                 layer->alpha = masklay->alpha;
1062                                 layer->blend = masklay->blend;
1063                                 layer->blend_flag = masklay->blend_flag;
1064                                 layer->falloff = masklay->falloff;
1065                         }
1066
1067                         /* printf("tris %d, feather tris %d\n", sf_tri_tot, tot_feather_quads); */
1068                 }
1069
1070                 /* add trianges */
1071                 BLI_scanfill_end(&sf_ctx);
1072         }
1073 }
1074
1075
1076 /* --------------------------------------------------------------------- */
1077 /* functions that run inside the sampling thread (keep fast!)            */
1078 /* --------------------------------------------------------------------- */
1079
1080 /* 2D ray test */
1081 #if 0
1082 static float maskrasterize_layer_z_depth_tri(const float pt[2],
1083                                              const float v1[3], const float v2[3], const float v3[3])
1084 {
1085         float w[3];
1086         barycentric_weights_v2(v1, v2, v3, pt, w);
1087         return (v1[2] * w[0]) + (v2[2] * w[1]) + (v3[2] * w[2]);
1088 }
1089 #endif
1090
1091 #if 1
1092 static float maskrasterize_layer_z_depth_quad(const float pt[2],
1093                                               const float v1[3], const float v2[3], const float v3[3], const float v4[3])
1094 {
1095         float w[4];
1096         barycentric_weights_v2_quad(v1, v2, v3, v4, pt, w);
1097         //return (v1[2] * w[0]) + (v2[2] * w[1]) + (v3[2] * w[2]) + (v4[2] * w[3]);
1098         return w[2] + w[3];  /* we can make this assumption for small speedup */
1099 }
1100 #endif
1101
1102 static float maskrasterize_layer_isect(unsigned int *face, float (*cos)[3], const float dist_orig, const float xy[2])
1103 {
1104         /* we always cast from same place only need xy */
1105         if (face[3] == TRI_VERT) {
1106                 /* --- tri --- */
1107
1108 #if 0
1109                 /* not essential but avoids unneeded extra lookups */
1110                 if ((cos[0][2] < dist_orig) ||
1111                     (cos[1][2] < dist_orig) ||
1112                     (cos[2][2] < dist_orig))
1113                 {
1114                         if (isect_point_tri_v2_cw(xy, cos[face[0]], cos[face[1]], cos[face[2]])) {
1115                                 /* we know all tris are close for now */
1116                                 return maskrasterize_layer_z_depth_tri(xy, cos[face[0]], cos[face[1]], cos[face[2]]);
1117                         }
1118                 }
1119 #else
1120                 /* we know all tris are close for now */
1121                 if (1) {
1122                         if (isect_point_tri_v2_cw(xy, cos[face[0]], cos[face[1]], cos[face[2]])) {
1123                                 return 0.0f;
1124                         }
1125                 }
1126 #endif
1127         }
1128         else {
1129                 /* --- quad --- */
1130
1131                 /* not essential but avoids unneeded extra lookups */
1132                 if ((cos[0][2] < dist_orig) ||
1133                     (cos[1][2] < dist_orig) ||
1134                     (cos[2][2] < dist_orig) ||
1135                     (cos[3][2] < dist_orig))
1136                 {
1137
1138                         /* needs work */
1139 #if 1
1140                         /* quad check fails for bowtie, so keep using 2 tri checks */
1141                         //if (isect_point_quad_v2(xy, cos[face[0]], cos[face[1]], cos[face[2]], cos[face[3]]))
1142                         if (isect_point_tri_v2(xy, cos[face[0]], cos[face[1]], cos[face[2]]) ||
1143                             isect_point_tri_v2(xy, cos[face[0]], cos[face[2]], cos[face[3]]))
1144                         {
1145                                 return maskrasterize_layer_z_depth_quad(xy, cos[face[0]], cos[face[1]], cos[face[2]], cos[face[3]]);
1146                         }
1147 #elif 1
1148                         /* don't use isect_point_tri_v2_cw because we could have bowtie quads */
1149
1150                         if (isect_point_tri_v2(xy, cos[face[0]], cos[face[1]], cos[face[2]])) {
1151                                 return maskrasterize_layer_z_depth_tri(xy, cos[face[0]], cos[face[1]], cos[face[2]]);
1152                         }
1153                         else if (isect_point_tri_v2(xy, cos[face[0]], cos[face[2]], cos[face[3]])) {
1154                                 return maskrasterize_layer_z_depth_tri(xy, cos[face[0]], cos[face[2]], cos[face[3]]);
1155                         }
1156 #else
1157                         /* cheat - we know first 2 verts are z0.0f and second 2 are z 1.0f */
1158                         /* ... worth looking into */
1159 #endif
1160                 }
1161         }
1162
1163         return 1.0f;
1164 }
1165
1166 BLI_INLINE unsigned int layer_bucket_index_from_xy(MaskRasterLayer *layer, const float xy[2])
1167 {
1168         BLI_assert(BLI_in_rctf_v(&layer->bounds, xy));
1169
1170         return ( (unsigned int)((xy[0] - layer->bounds.xmin) * layer->buckets_xy_scalar[0])) +
1171                (((unsigned int)((xy[1] - layer->bounds.ymin) * layer->buckets_xy_scalar[1])) * layer->buckets_x);
1172 }
1173
1174 static float layer_bucket_depth_from_xy(MaskRasterLayer *layer, const float xy[2])
1175 {
1176         unsigned int index = layer_bucket_index_from_xy(layer, xy);
1177         unsigned int *face_index = layer->buckets_face[index];
1178
1179         if (face_index) {
1180                 unsigned int (*face_array)[4] = layer->face_array;
1181                 float        (*cos)[3]        = layer->face_coords;
1182                 float best_dist = 1.0f;
1183                 while (*face_index != TRI_TERMINATOR_ID) {
1184                         const float test_dist = maskrasterize_layer_isect(face_array[*face_index], cos, best_dist, xy);
1185                         if (test_dist < best_dist) {
1186                                 best_dist = test_dist;
1187                                 /* comparing with 0.0f is OK here because triangles are always zero depth */
1188                                 if (best_dist == 0.0f) {
1189                                         /* bail early, we're as close as possible */
1190                                         return 0.0f;
1191                                 }
1192                         }
1193                         face_index++;
1194                 }
1195                 return best_dist;
1196         }
1197         else {
1198                 return 1.0f;
1199         }
1200 }
1201
1202 float BKE_maskrasterize_handle_sample(MaskRasterHandle *mr_handle, const float xy[2])
1203 {
1204         /* can't do this because some layers may invert */
1205         /* if (BLI_in_rctf_v(&mr_handle->bounds, xy)) */
1206
1207         const unsigned int layers_tot = mr_handle->layers_tot;
1208         unsigned int i;
1209         MaskRasterLayer *layer = mr_handle->layers;
1210
1211         /* return value */
1212         float value = 0.0f;
1213
1214         for (i = 0; i < layers_tot; i++, layer++) {
1215                 float value_layer;
1216
1217                 /* also used as signal for unused layer (when render is disabled) */
1218                 if (layer->alpha != 0.0f && BLI_in_rctf_v(&layer->bounds, xy)) {
1219                         value_layer = 1.0f - layer_bucket_depth_from_xy(layer, xy);
1220
1221                         switch (layer->falloff) {
1222                                 case PROP_SMOOTH:
1223                                         /* ease - gives less hard lines for dilate/erode feather */
1224                                         value_layer = (3.0f * value_layer * value_layer - 2.0f * value_layer * value_layer * value_layer);
1225                                         break;
1226                                 case PROP_SPHERE:
1227                                         value_layer = sqrtf(2.0f * value_layer - value_layer * value_layer);
1228                                         break;
1229                                 case PROP_ROOT:
1230                                         value_layer = sqrtf(value_layer);
1231                                         break;
1232                                 case PROP_SHARP:
1233                                         value_layer = value_layer * value_layer;
1234                                         break;
1235                                 case PROP_LIN:
1236                                 default:
1237                                         /* nothing */
1238                                         break;
1239                         }
1240
1241                         if (layer->blend != MASK_BLEND_REPLACE) {
1242                                 value_layer *= layer->alpha;
1243                         }
1244                 }
1245                 else {
1246                         value_layer = 0.0f;
1247                 }
1248
1249                 if (layer->blend_flag & MASK_BLENDFLAG_INVERT) {
1250                         value_layer = 1.0f - value_layer;
1251                 }
1252
1253                 switch (layer->blend) {
1254                         case MASK_BLEND_MERGE:
1255                                 value += value_layer * (1.0f - value);
1256                                 break;
1257                         case MASK_BLEND_ADD:
1258                                 value += value_layer;
1259                                 break;
1260                         case MASK_BLEND_SUBTRACT:
1261                                 value -= value_layer;
1262                                 break;
1263                         case MASK_BLEND_LIGHTEN:
1264                                 value = maxf(value, value_layer);
1265                                 break;
1266                         case MASK_BLEND_DARKEN:
1267                                 value = minf(value, value_layer);
1268                                 break;
1269                         case MASK_BLEND_MUL:
1270                                 value *= value_layer;
1271                                 break;
1272                         case MASK_BLEND_REPLACE:
1273                                 value = (value * (1.0f - layer->alpha)) + (value_layer * layer->alpha);
1274                                 break;
1275                         case MASK_BLEND_DIFFERENCE:
1276                                 value = fabsf(value - value_layer);
1277                                 break;
1278                         default: /* same as add */
1279                                 BLI_assert(0);
1280                                 value += value_layer;
1281                                 break;
1282                 }
1283
1284                 /* clamp after applying each layer so we don't get
1285                  * issues subtracting after accumulating over 1.0f */
1286                 CLAMP(value, 0.0f, 1.0f);
1287         }
1288
1289         return value;
1290 }
1291
1292 #endif /* USE_RASKTER */