Commit 5d771014 authored by Michael Muré's avatar Michael Muré

GimpCageConfig: use a GArray to store cage's point, to make easier

further improvement
parent 9b3e1c91
This diff is collapsed.
......@@ -48,14 +48,11 @@ struct _GimpCageConfig
{
GimpImageMapConfig parent_instance;
gint n_cage_vertices; /* vertices used by the cage */
gint max_cage_vertices; /* vertices allocated */
GArray *cage_points;
gdouble displacement_x;
gdouble displacement_y;
GimpCageMode cage_mode; /* Cage mode, used to commit displacement */
GimpCagePoint *cage_points;
};
struct _GimpCageConfigClass
......@@ -66,6 +63,7 @@ struct _GimpCageConfigClass
GType gimp_cage_config_get_type (void) G_GNUC_CONST;
guint gimp_cage_config_get_n_points (GimpCageConfig *gcc);
void gimp_cage_config_add_cage_point (GimpCageConfig *gcc,
gdouble x,
gdouble y);
......@@ -96,6 +94,8 @@ void gimp_cage_config_select_add_area (GimpCageConfig *gcc,
void gimp_cage_config_toggle_point_selection (GimpCageConfig *gcc,
gint point_number);
void gimp_cage_config_deselect_points (GimpCageConfig *gcc);
gboolean gimp_cage_config_point_is_selected (GimpCageConfig *gcc,
gint point_number);
#endif /* __GIMP_CAGE_CONFIG_H__ */
......@@ -96,7 +96,7 @@ gimp_operation_cage_coef_calc_prepare (GeglOperation *operation)
gegl_operation_set_format (operation,
"output",
babl_format_n (babl_type ("float"),
2 * config->n_cage_vertices));
2 * gimp_cage_config_get_n_points (config)));
}
static void
......@@ -193,13 +193,17 @@ gimp_operation_cage_coef_calc_process (GeglOperation *operation,
GimpOperationCageCoefCalc *occc = GIMP_OPERATION_CAGE_COEF_CALC (operation);
GimpCageConfig *config = GIMP_CAGE_CONFIG (occc->config);
Babl *format = babl_format_n (babl_type ("float"), 2 * config->n_cage_vertices);
Babl *format = babl_format_n (babl_type ("float"), 2 * gimp_cage_config_get_n_points (config));
GeglBufferIterator *it;
guint n_cage_vertices;
GimpCagePoint *current, *last;
if (! config)
return FALSE;
n_cage_vertices = gimp_cage_config_get_n_points (config);
it = gegl_buffer_iterator_new (output, roi, format, GEGL_BUFFER_READWRITE);
while (gegl_buffer_iterator_next (it))
......@@ -216,13 +220,16 @@ gimp_operation_cage_coef_calc_process (GeglOperation *operation,
{
if (gimp_cage_config_point_inside(config, x, y))
{
for( j = 0; j < config->n_cage_vertices; j++)
last = &(g_array_index (config->cage_points, GimpCagePoint, 0));
for( j = 0; j < n_cage_vertices; j++)
{
GimpVector2 v1,v2,a,b,p;
gdouble BA,SRT,L0,L1,A0,A1,A10,L10, Q,S,R, absa;
v1 = config->cage_points[j].src_point;
v2 = config->cage_points[(j+1)%config->n_cage_vertices].src_point;
current = &(g_array_index (config->cage_points, GimpCagePoint, (j+1) % n_cage_vertices));
v1 = last->src_point;
v2 = current->src_point;
p.x = x;
p.y = y;
a.x = v2.x - v1.x;
......@@ -245,23 +252,25 @@ gimp_operation_cage_coef_calc_process (GeglOperation *operation,
L10 = L1 - L0;
/* edge coef */
coef[j + config->n_cage_vertices] = (-absa / (4.0 * M_PI)) * ((4.0*S-(R*R)/Q) * A10 + (R / (2.0 * Q)) * L10 + L1 - 2.0);
coef[j + n_cage_vertices] = (-absa / (4.0 * M_PI)) * ((4.0*S-(R*R)/Q) * A10 + (R / (2.0 * Q)) * L10 + L1 - 2.0);
if (isnan(coef[j + config->n_cage_vertices]))
if (isnan(coef[j + n_cage_vertices]))
{
coef[j + config->n_cage_vertices] = 0.0;
coef[j + n_cage_vertices] = 0.0;
}
/* vertice coef */
if (!gimp_operation_cage_coef_calc_is_on_straight (&v1, &v2, &p))
{
coef[j] += (BA / (2.0 * M_PI)) * (L10 /(2.0*Q) - A10 * (2.0 + R / Q));
coef[(j+1)%config->n_cage_vertices] -= (BA / (2.0 * M_PI)) * (L10 / (2.0 * Q) - A10 * (R / Q));
coef[(j+1)%n_cage_vertices] -= (BA / (2.0 * M_PI)) * (L10 / (2.0 * Q) - A10 * (R / Q));
}
last = current;
}
}
coef += 2 * config->n_cage_vertices;
coef += 2 * n_cage_vertices;
/* update x and y coordinates */
x++;
......
......@@ -210,7 +210,7 @@ gimp_operation_cage_transform_prepare (GeglOperation *operation)
gegl_operation_set_format (operation, "input",
babl_format_n (babl_type ("float"),
2 * config->n_cage_vertices));
2 * gimp_cage_config_get_n_points (config)));
gegl_operation_set_format (operation, "output",
babl_format_n (babl_type ("float"), 2));
}
......@@ -232,13 +232,18 @@ gimp_operation_cage_transform_process (GeglOperation *operation,
GeglBufferIterator *it;
gint x, y;
gboolean output_set;
GimpCagePoint *point;
guint n_cage_vertices;
/* pre-fill the out buffer with no-displacement coordinate */
it = gegl_buffer_iterator_new (out_buf, roi, NULL, GEGL_BUFFER_WRITE);
cage_bb = gimp_cage_config_get_bounding_box (config);
plain_color.x = (gint) config->cage_points[0].src_point.x;
plain_color.y = (gint) config->cage_points[0].src_point.y;
point = &(g_array_index (config->cage_points, GimpCagePoint, 0));
plain_color.x = (gint) point->src_point.x;
plain_color.y = (gint) point->src_point.y;
n_cage_vertices = gimp_cage_config_get_n_points (config);
while (gegl_buffer_iterator_next (it))
{
......@@ -290,8 +295,8 @@ gimp_operation_cage_transform_process (GeglOperation *operation,
/* pre-allocate memory outside of the loop */
coords = g_slice_alloc (2 * sizeof (gfloat));
coef = g_malloc (config->n_cage_vertices * 2 * sizeof (gfloat));
format_coef = babl_format_n (babl_type ("float"), 2 * config->n_cage_vertices);
coef = g_malloc (n_cage_vertices * 2 * sizeof (gfloat));
format_coef = babl_format_n (babl_type ("float"), 2 * n_cage_vertices);
/* compute, reverse and interpolate the transformation */
for (y = cage_bb.y; y < cage_bb.y + cage_bb.height - 1; y++)
......@@ -536,14 +541,14 @@ gimp_cage_transform_compute_destination (GimpCageConfig *config,
GeglBuffer *coef_buf,
GimpVector2 coords)
{
gdouble pos_x, pos_y;
GimpVector2 result;
gint cvn = config->n_cage_vertices;
GimpVector2 result = {0, 0};
gint n_cage_vertices = gimp_cage_config_get_n_points (config);
gint i;
GimpCagePoint *point;
/* When Gegl bug #645810 will be solved, this should be a good optimisation */
#ifdef GEGL_BUG_645810_SOLVED
gegl_buffer_sample (coef_buf, coords.x, coords.y, 1.0, coef, format_coef, GEGL_INTERPOLATION_LANCZOS);
gegl_buffer_sample (coef_buf, coords.x, coords.y, 1.0, coef, format_coef, GEGL_INTERPOLATION_NEAREST);
#else
GeglRectangle rect;
......@@ -555,20 +560,16 @@ gimp_cage_transform_compute_destination (GimpCageConfig *config,
gegl_buffer_get (coef_buf, 1, &rect, format_coef, coef, GEGL_AUTO_ROWSTRIDE);
#endif
pos_x = 0;
pos_y = 0;
for (i = 0; i < cvn; i++)
for (i = 0; i < n_cage_vertices; i++)
{
pos_x += coef[i] * config->cage_points[i].dest_point.x;
pos_y += coef[i] * config->cage_points[i].dest_point.y;
point = &g_array_index (config->cage_points, GimpCagePoint, i);
pos_x += coef[i + cvn] * config->cage_points[i].edge_scaling_factor * config->cage_points[i].edge_normal.x;
pos_y += coef[i + cvn] * config->cage_points[i].edge_scaling_factor * config->cage_points[i].edge_normal.y;
}
result.x += coef[i] * point->dest_point.x;
result.y += coef[i] * point->dest_point.y;
result.x = pos_x;
result.y = pos_y;
result.x += coef[i + n_cage_vertices] * point->edge_scaling_factor * point->edge_normal.x;
result.y += coef[i + n_cage_vertices] * point->edge_scaling_factor * point->edge_normal.y;
}
return result;
}
......
......@@ -547,10 +547,10 @@ gimp_cage_tool_button_press (GimpTool *tool,
coords->x - ct->offset_x,
coords->y - ct->offset_y);
gimp_cage_config_select_point (ct->config,
ct->config->n_cage_vertices - 1);
gimp_cage_config_get_n_points (ct->config) - 1);
ct->tool_state = CAGE_STATE_MOVE_HANDLE;
}
else if (handle == 0 && ct->config->n_cage_vertices > 2)
else if (handle == 0 && gimp_cage_config_get_n_points (ct->config) > 2)
{
/* User clicked on the first handle, we wait for
* release for closing the cage and switching to
......@@ -573,7 +573,7 @@ gimp_cage_tool_button_press (GimpTool *tool,
{
/* New selection */
if (! ct->config->cage_points[handle].selected)
if (! gimp_cage_config_point_is_selected (ct->config, handle))
{
gimp_cage_config_select_point (ct->config, handle);
}
......@@ -610,7 +610,7 @@ gimp_cage_tool_button_press (GimpTool *tool,
{
/* New selection */
if (! ct->config->cage_points[handle].selected)
if (! gimp_cage_config_point_is_selected (ct->config, handle))
{
gimp_cage_config_select_point (ct->config, handle);
}
......@@ -646,7 +646,7 @@ gimp_cage_tool_button_press (GimpTool *tool,
{
/* New selection */
if (! ct->config->cage_points[handle].selected)
if (! gimp_cage_config_point_is_selected (ct->config, handle))
{
gimp_cage_config_select_point (ct->config, handle);
}
......@@ -819,7 +819,10 @@ gimp_cage_tool_draw (GimpDrawTool *draw_tool)
gint i;
GimpHandleType handle;
n_vertices = config->n_cage_vertices;
n_vertices = gimp_cage_config_get_n_points (config);
if (n_vertices == 0)
return;
if (ct->tool_state == CAGE_STATE_INIT)
return;
......@@ -896,7 +899,7 @@ gimp_cage_tool_draw (GimpDrawTool *draw_tool)
GIMP_TOOL_HANDLE_SIZE_CIRCLE,
GIMP_HANDLE_ANCHOR_CENTER);
if (ct->config->cage_points[i].selected)
if (gimp_cage_config_point_is_selected (ct->config, i))
{
gimp_draw_tool_add_handle (draw_tool,
GIMP_HANDLE_SQUARE,
......@@ -933,13 +936,16 @@ gimp_cage_tool_is_on_handle (GimpCageTool *ct,
gdouble dist = G_MAXDOUBLE;
gint i;
GimpVector2 cage_point;
guint n_cage_vertices;
g_return_val_if_fail (GIMP_IS_CAGE_TOOL (ct), -1);
if (config->n_cage_vertices == 0)
n_cage_vertices = gimp_cage_config_get_n_points (config);
if (n_cage_vertices == 0)
return -1;
for (i = 0; i < config->n_cage_vertices; i++)
for (i = 0; i < n_cage_vertices; i++)
{
cage_point = gimp_cage_config_get_point_coordinate (config,
options->cage_mode,
......@@ -996,7 +1002,7 @@ gimp_cage_tool_compute_coef (GimpCageTool *ct,
}
format = babl_format_n (babl_type ("float"),
config->n_cage_vertices * 2);
gimp_cage_config_get_n_points (config) * 2);
gegl = gegl_node_new ();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment