Merging r48981 through r48984 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenkernel / intern / mask.c
index 86838c5565aad3c0fea3bdd08514bf76b203dc6f..d86e2c72ebede9121a43e1a7b0f8882112285ff7 100644 (file)
@@ -446,7 +446,8 @@ static void feather_bucket_add_edge(FeatherEdgesBucket *bucket, int start, int e
        bucket->tot_segment++;
 }
 
-static void feather_bucket_check_intersect(float (*feather_points)[2], FeatherEdgesBucket *bucket, int cur_a, int cur_b)
+static void feather_bucket_check_intersect(float (*feather_points)[2], int tot_feather_point, FeatherEdgesBucket *bucket,
+                                           int cur_a, int cur_b)
 {
        int i;
 
@@ -464,13 +465,34 @@ static void feather_bucket_check_intersect(float (*feather_points)[2], FeatherEd
                        continue;
 
                if (isect_seg_seg_v2(v1, v2, v3, v4)) {
-                       int k;
+                       int k, len;
                        float p[2];
 
                        isect_seg_seg_v2_point(v1, v2, v3, v4, p);
 
-                       for (k = check_b; k <= cur_a; k++) {
-                               copy_v2_v2(feather_points[k], p);
+                       /* TODO: for now simply choose the shortest loop, could be made smarter in some way */
+                       len = cur_a - check_b;
+                       if (len < tot_feather_point - len) {
+                               for (k = check_b; k <= cur_a; k++) {
+                                       copy_v2_v2(feather_points[k], p);
+                               }
+                       }
+                       else {
+                               if (cur_b < check_a) {
+                                       /* special case when intersection happens with first segment */
+                                       for (k = cur_b; k <= check_a; k++) {
+                                               copy_v2_v2(feather_points[k], p);
+                                       }
+                               }
+                               else {
+                                       for (k = 0; k <= check_a; k++) {
+                                               copy_v2_v2(feather_points[k], p);
+                                       }
+
+                                       for (k = cur_b; k < tot_feather_point; k++) {
+                                               copy_v2_v2(feather_points[k], p);
+                                       }
+                               }
                        }
 
                        break;
@@ -478,15 +500,25 @@ static void feather_bucket_check_intersect(float (*feather_points)[2], FeatherEd
        }
 }
 
-static void spline_feather_collapse_inner_loops(float (*feather_points)[2], int tot_feather_point)
+static int feather_bucket_index_from_coord(float co[2], float min[2], float max[2],
+                                           const int buckets_per_side, const float bucket_size)
 {
-#define BUCKET_SIDE_INDEX(co, min, max) ((int) ((co - min) / (max - min + FLT_EPSILON) / bucket_size))
+#define BUCKET_SIDE_INDEX(co, min, max) ((int) ((co - min) / (max - min) / bucket_size))
+
+       int x = BUCKET_SIDE_INDEX(co[0], min[0], max[0]);
+       int y = BUCKET_SIDE_INDEX(co[1], min[1], max[1]);
 
-#define BUCKET_INDEX_DELTA(co, dx, dy) \
-       BUCKET_SIDE_INDEX(co[1] + dy, min[1], max[1]) * buckets_per_side + \
-               BUCKET_SIDE_INDEX(co[0] + dx, min[0], max[0])
+       x = MIN2(x, buckets_per_side - 1);
+       y = MIN2(y, buckets_per_side - 1);
 
-#define BUCKET_INDEX(co) BUCKET_INDEX_DELTA(co, 0, 0)
+       return y * buckets_per_side + x;
+#undef BUCKET_SIDE_INDEX
+}
+
+static void spline_feather_collapse_inner_loops(float (*feather_points)[2], int tot_feather_point)
+{
+#define BUCKET_INDEX(co) \
+       feather_bucket_index_from_coord(co, min, max, buckets_per_side, bucket_size)
 
        const int buckets_per_side = 10;
        const int tot_bucket = buckets_per_side * buckets_per_side;
@@ -532,10 +564,10 @@ static void spline_feather_collapse_inner_loops(float (*feather_points)[2], int
                FeatherEdgesBucket *start_bucket = &buckets[start_bucket_index];
                FeatherEdgesBucket *end_bucket = &buckets[end_bucket_index];
 
-               feather_bucket_check_intersect(feather_points, start_bucket, cur_a, cur_b);
+               feather_bucket_check_intersect(feather_points, tot_feather_point, start_bucket, cur_a, cur_b);
 
                if (start_bucket != end_bucket)
-                       feather_bucket_check_intersect(feather_points, end_bucket, cur_a, cur_b);
+                       feather_bucket_check_intersect(feather_points, tot_feather_point, end_bucket, cur_a, cur_b);
        }
 
        /* free buckets */
@@ -547,7 +579,6 @@ static void spline_feather_collapse_inner_loops(float (*feather_points)[2], int
        MEM_freeN(buckets);
 
 #undef BUCKET_INDEX
-#undef BUCKET_SIZE_INDEX
 }
 
 /**