app: add utility function gimp_transform_polygon_is_convex()

and use it in GimpTransformTool and GimpCanvasTransformPreview.
This commit is contained in:
Michael Natterer 2011-03-27 22:04:33 +02:00
parent caa8cdd4c1
commit eb4f01039a
4 changed files with 111 additions and 92 deletions

View file

@ -327,3 +327,30 @@ gimp_transform_matrix_perspective (GimpMatrix3 *matrix,
gimp_matrix3_mult (&trafo, matrix);
}
gboolean
gimp_transform_polygon_is_convex (gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2,
gdouble x3,
gdouble y3,
gdouble x4,
gdouble y4)
{
gdouble z1, z2, z3, z4;
/* We test if the transformed polygon is convex. if z1 and z2 have
* the same sign as well as z3 and z4 the polygon is convex.
*/
z1 = ((x2 - x1) * (y4 - y1) -
(x4 - x1) * (y2 - y1));
z2 = ((x4 - x1) * (y3 - y1) -
(x3 - x1) * (y4 - y1));
z3 = ((x4 - x2) * (y3 - y2) -
(x3 - x2) * (y4 - y2));
z4 = ((x3 - x2) * (y1 - y2) -
(x1 - x2) * (y3 - y2));
return (z1 * z2 > 0) && (z3 * z4 > 0);
}

View file

@ -19,72 +19,81 @@
#define __GIMP_TRANSFORM_UTILS_H__
void gimp_transform_get_rotate_center (gint x,
gint y,
gint width,
gint height,
gboolean auto_center,
gdouble *center_x,
gdouble *center_y);
void gimp_transform_get_flip_axis (gint x,
gint y,
gint width,
gint height,
GimpOrientationType flip_type,
gboolean auto_center,
gdouble *axis);
void gimp_transform_get_rotate_center (gint x,
gint y,
gint width,
gint height,
gboolean auto_center,
gdouble *center_x,
gdouble *center_y);
void gimp_transform_get_flip_axis (gint x,
gint y,
gint width,
gint height,
GimpOrientationType flip_type,
gboolean auto_center,
gdouble *axis);
void gimp_transform_matrix_flip (GimpMatrix3 *matrix,
GimpOrientationType flip_type,
gdouble axis);
void gimp_transform_matrix_flip_free (GimpMatrix3 *matrix,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2);
void gimp_transform_matrix_rotate (GimpMatrix3 *matrix,
GimpRotationType rotate_type,
gdouble center_x,
gdouble center_y);
void gimp_transform_matrix_rotate_rect (GimpMatrix3 *matrix,
gint x,
gint y,
gint width,
gint height,
gdouble angle);
void gimp_transform_matrix_rotate_center (GimpMatrix3 *matrix,
gdouble center_x,
gdouble center_y,
gdouble angle);
void gimp_transform_matrix_scale (GimpMatrix3 *matrix,
gint x,
gint y,
gint width,
gint height,
gdouble t_x,
gdouble t_y,
gdouble t_width,
gdouble t_height);
void gimp_transform_matrix_shear (GimpMatrix3 *matrix,
gint x,
gint y,
gint width,
gint height,
GimpOrientationType orientation,
gdouble amount);
void gimp_transform_matrix_perspective (GimpMatrix3 *matrix,
gint x,
gint y,
gint width,
gint height,
gdouble t_x1,
gdouble t_y1,
gdouble t_x2,
gdouble t_y2,
gdouble t_x3,
gdouble t_y3,
gdouble t_x4,
gdouble t_y4);
void gimp_transform_matrix_flip (GimpMatrix3 *matrix,
GimpOrientationType flip_type,
gdouble axis);
void gimp_transform_matrix_flip_free (GimpMatrix3 *matrix,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2);
void gimp_transform_matrix_rotate (GimpMatrix3 *matrix,
GimpRotationType rotate_type,
gdouble center_x,
gdouble center_y);
void gimp_transform_matrix_rotate_rect (GimpMatrix3 *matrix,
gint x,
gint y,
gint width,
gint height,
gdouble angle);
void gimp_transform_matrix_rotate_center (GimpMatrix3 *matrix,
gdouble center_x,
gdouble center_y,
gdouble angle);
void gimp_transform_matrix_scale (GimpMatrix3 *matrix,
gint x,
gint y,
gint width,
gint height,
gdouble t_x,
gdouble t_y,
gdouble t_width,
gdouble t_height);
void gimp_transform_matrix_shear (GimpMatrix3 *matrix,
gint x,
gint y,
gint width,
gint height,
GimpOrientationType orientation,
gdouble amount);
void gimp_transform_matrix_perspective (GimpMatrix3 *matrix,
gint x,
gint y,
gint width,
gint height,
gdouble t_x1,
gdouble t_y1,
gdouble t_x2,
gdouble t_y2,
gdouble t_x3,
gdouble t_y3,
gdouble t_x4,
gdouble t_y4);
gboolean gimp_transform_polygon_is_convex (gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2,
gdouble x3,
gdouble y3,
gdouble x4,
gdouble y4);
#endif /* __GIMP_TRANSFORM_UTILS_H__ */

View file

@ -33,6 +33,7 @@
#include "core/gimpchannel.h"
#include "core/gimpimage.h"
#include "core/gimp-transform-utils.h"
#include "gimpcanvas.h"
#include "gimpcanvastransformpreview.h"
@ -354,7 +355,6 @@ gimp_canvas_transform_preview_transform (GimpCanvasItem *item,
gdouble tx2, ty2;
gdouble tx3, ty3;
gdouble tx4, ty4;
gdouble z1, z2, z3, z4;
gimp_matrix3_transform_point (&private->transform,
private->x1, private->y1,
@ -369,16 +369,10 @@ gimp_canvas_transform_preview_transform (GimpCanvasItem *item,
private->x2, private->y2,
&tx4, &ty4);
z1 = ((tx2 - tx1) * (ty4 - ty1) -
(tx4 - tx1) * (ty2 - ty1));
z2 = ((tx4 - tx1) * (ty3 - ty1) -
(tx3 - tx1) * (ty4 - ty1));
z3 = ((tx4 - tx2) * (ty3 - ty2) -
(tx3 - tx2) * (ty4 - ty2));
z4 = ((tx3 - tx2) * (ty1 - ty2) -
(tx1 - tx2) * (ty3 - ty2));
if (! ((z1 * z2 > 0) && (z3 * z4 > 0)))
if (! gimp_transform_polygon_is_convex (tx1, ty1,
tx2, ty2,
tx3, ty3,
tx4, ty4))
return FALSE;
if (extents)

View file

@ -44,6 +44,7 @@
#include "core/gimplayer.h"
#include "core/gimpprogress.h"
#include "core/gimptoolinfo.h"
#include "core/gimp-transform-utils.h"
#include "vectors/gimpvectors.h"
#include "vectors/gimpstroke.h"
@ -754,7 +755,6 @@ gimp_transform_tool_draw (GimpDrawTool *draw_tool)
if (tr_tool->use_grid)
{
GimpCanvasGroup *stroke_group;
gdouble z1, z2, z3, z4;
if (gimp_transform_options_show_preview (options))
{
@ -777,23 +777,12 @@ gimp_transform_tool_draw (GimpDrawTool *draw_tool)
gimp_draw_tool_push_group (draw_tool, stroke_group);
/* We test if the transformed polygon is convex.
* if z1 and z2 have the same sign as well as z3 and z4
* the polygon is convex.
*/
z1 = ((tr_tool->tx2 - tr_tool->tx1) * (tr_tool->ty4 - tr_tool->ty1) -
(tr_tool->tx4 - tr_tool->tx1) * (tr_tool->ty2 - tr_tool->ty1));
z2 = ((tr_tool->tx4 - tr_tool->tx1) * (tr_tool->ty3 - tr_tool->ty1) -
(tr_tool->tx3 - tr_tool->tx1) * (tr_tool->ty4 - tr_tool->ty1));
z3 = ((tr_tool->tx4 - tr_tool->tx2) * (tr_tool->ty3 - tr_tool->ty2) -
(tr_tool->tx3 - tr_tool->tx2) * (tr_tool->ty4 - tr_tool->ty2));
z4 = ((tr_tool->tx3 - tr_tool->tx2) * (tr_tool->ty1 - tr_tool->ty2) -
(tr_tool->tx1 - tr_tool->tx2) * (tr_tool->ty3 - tr_tool->ty2));
/* draw the grid */
if (tr_tool->grid_coords &&
z1 * z2 > 0 &&
z3 * z4 > 0)
gimp_transform_polygon_is_convex (tr_tool->tx1, tr_tool->ty1,
tr_tool->tx2, tr_tool->ty2,
tr_tool->tx3, tr_tool->ty3,
tr_tool->tx4, tr_tool->ty4))
{
gint k = tr_tool->ngx + tr_tool->ngy;
gint i, gci;