1999-07-10 05:52:00 +08:00
|
|
|
/* math.c: define some simple array operations, and other functions.
|
2007-04-12 23:17:30 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 1992 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
1999-07-10 05:52:00 +08:00
|
|
|
|
app/appenv.h New file. Includes <math.h>. Move G_PI, RINT(), ROUND() etc
1999-09-01 Tor Lillqvist <tml@iki.fi>
* app/appenv.h
* libgimp/gimpmath.h: New file. Includes <math.h>. Move G_PI,
RINT(), ROUND() etc from app/appenv.h here, so plug-ins can
use them, too. Remove some commented-out old stuff in appenv.h.
* libgimp/gimp.h: Include gimpmath.h.
* libgimp/gimp.c (gimp_main): Win32: Don't install signal
handlers, we can't do anything useful in the handler ourselves
anyway (it would be nice to print out a backtrace, but that seems
pretty hard to do, even if not impossible). Let Windows inform the
user about the crash. If the plug-in was compiled with MSVC, and
the user also has it, she is offered a chance to start the
debugger automatically anyway.
* app/*several*.c: Include gimpmath.h for G_PI etc. Don't include
<math.h>, as gimpmath.h includes it.
* plug-ins/*/*many*.c: Include config.h. Don't include <math.h>.
Remove all the duplicated definitions of G_PI and rint(). Use
RINT() instead of rint().
* app/app_procs.[ch]: app_exit() takes a gboolean.
* app/batch.c
* app/commands.c
* app/interface.c: Call app_exit() with FALSE or TRUE.
* app/main.c (on_error): Call gimp_fatal_error. (main): Don't
install any signal handler on Win32 here, either.
* app/errors.c (gimp_fatal_error, gimp_terminate): Win32: Format
the message and call MessageBox with it. g_on_error_query doesn't
do anything useful on Win32, and printf'ing a message to stdout or
stderr doesn't do anything, either, in a windowing application.
1999-09-02 04:30:56 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
1999-07-10 05:52:00 +08:00
|
|
|
#include <errno.h>
|
2005-03-04 21:23:32 +08:00
|
|
|
#include <math.h>
|
1999-07-10 05:52:00 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
1999-11-15 05:56:01 +08:00
|
|
|
#include "libgimp/gimp.h"
|
1999-07-15 00:02:32 +08:00
|
|
|
|
1999-07-10 05:52:00 +08:00
|
|
|
#include "types.h"
|
|
|
|
#include "global.h"
|
|
|
|
|
|
|
|
/* Numerical errors sometimes make a floating point number just slightly
|
|
|
|
larger or smaller than its true value. When it matters, we need to
|
|
|
|
compare with some tolerance, REAL_EPSILON, defined in kbase.h. */
|
|
|
|
|
2000-07-31 02:37:33 +08:00
|
|
|
boolean
|
1999-07-10 05:52:00 +08:00
|
|
|
epsilon_equal (real v1, real v2)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
v1 == v2 /* Usually they'll be exactly equal, anyway. */
|
|
|
|
|| fabs (v1 - v2) <= REAL_EPSILON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return the Euclidean distance between P1 and P2. */
|
|
|
|
|
2000-07-31 02:37:33 +08:00
|
|
|
real
|
1999-07-10 05:52:00 +08:00
|
|
|
distance (real_coordinate_type p1, real_coordinate_type p2)
|
|
|
|
{
|
|
|
|
return hypot (p1.x - p2.x, p1.y - p2.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Same thing, for integer points. */
|
2000-07-31 02:37:33 +08:00
|
|
|
real
|
1999-07-10 05:52:00 +08:00
|
|
|
int_distance (coordinate_type p1, coordinate_type p2)
|
|
|
|
{
|
|
|
|
return hypot ((double) p1.x - p2.x, (double) p1.y - p2.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return the arc cosine of V, in degrees in the range zero to 180. V
|
|
|
|
is taken to be in radians. */
|
|
|
|
|
2000-07-31 02:37:33 +08:00
|
|
|
real
|
1999-09-15 03:44:51 +08:00
|
|
|
my_acosd (real v)
|
1999-07-10 05:52:00 +08:00
|
|
|
{
|
|
|
|
real a;
|
|
|
|
|
|
|
|
if (epsilon_equal (v, 1.0))
|
|
|
|
v = 1.0;
|
|
|
|
else if (epsilon_equal (v, -1.0))
|
|
|
|
v = -1.0;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
a = acos (v);
|
|
|
|
if (errno == ERANGE || errno == EDOM)
|
|
|
|
FATAL_PERROR ("acosd");
|
|
|
|
|
app/appenv.h New file. Includes <math.h>. Move G_PI, RINT(), ROUND() etc
1999-09-01 Tor Lillqvist <tml@iki.fi>
* app/appenv.h
* libgimp/gimpmath.h: New file. Includes <math.h>. Move G_PI,
RINT(), ROUND() etc from app/appenv.h here, so plug-ins can
use them, too. Remove some commented-out old stuff in appenv.h.
* libgimp/gimp.h: Include gimpmath.h.
* libgimp/gimp.c (gimp_main): Win32: Don't install signal
handlers, we can't do anything useful in the handler ourselves
anyway (it would be nice to print out a backtrace, but that seems
pretty hard to do, even if not impossible). Let Windows inform the
user about the crash. If the plug-in was compiled with MSVC, and
the user also has it, she is offered a chance to start the
debugger automatically anyway.
* app/*several*.c: Include gimpmath.h for G_PI etc. Don't include
<math.h>, as gimpmath.h includes it.
* plug-ins/*/*many*.c: Include config.h. Don't include <math.h>.
Remove all the duplicated definitions of G_PI and rint(). Use
RINT() instead of rint().
* app/app_procs.[ch]: app_exit() takes a gboolean.
* app/batch.c
* app/commands.c
* app/interface.c: Call app_exit() with FALSE or TRUE.
* app/main.c (on_error): Call gimp_fatal_error. (main): Don't
install any signal handler on Win32 here, either.
* app/errors.c (gimp_fatal_error, gimp_terminate): Win32: Format
the message and call MessageBox with it. g_on_error_query doesn't
do anything useful on Win32, and printf'ing a message to stdout or
stderr doesn't do anything, either, in a windowing application.
1999-09-02 04:30:56 +08:00
|
|
|
return a * 180.0 / G_PI;
|
1999-07-10 05:52:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The slope of the line defined by COORD1 and COORD2. */
|
|
|
|
|
2000-07-31 02:37:33 +08:00
|
|
|
real
|
1999-07-10 05:52:00 +08:00
|
|
|
slope (real_coordinate_type coord1, real_coordinate_type coord2)
|
|
|
|
{
|
2005-03-04 21:23:32 +08:00
|
|
|
g_assert (coord2.x - coord1.x != 0);
|
1999-07-10 05:52:00 +08:00
|
|
|
|
|
|
|
return (coord2.y - coord1.y) / (coord2.x - coord1.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Turn an integer point into a real one, and vice versa. */
|
|
|
|
|
2000-07-31 02:37:33 +08:00
|
|
|
real_coordinate_type
|
1999-07-10 05:52:00 +08:00
|
|
|
int_to_real_coord (coordinate_type int_coord)
|
|
|
|
{
|
|
|
|
real_coordinate_type real_coord;
|
|
|
|
|
|
|
|
real_coord.x = int_coord.x;
|
|
|
|
real_coord.y = int_coord.y;
|
|
|
|
|
|
|
|
return real_coord;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-31 02:37:33 +08:00
|
|
|
coordinate_type
|
1999-07-10 05:52:00 +08:00
|
|
|
real_to_int_coord (real_coordinate_type real_coord)
|
|
|
|
{
|
|
|
|
coordinate_type int_coord;
|
|
|
|
|
2000-01-28 21:12:50 +08:00
|
|
|
int_coord.x = SROUND (real_coord.x);
|
|
|
|
int_coord.y = SROUND (real_coord.y);
|
1999-07-10 05:52:00 +08:00
|
|
|
|
|
|
|
return int_coord;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* See if two points (described by their row and column) are adjacent. */
|
|
|
|
|
2000-07-31 02:37:33 +08:00
|
|
|
boolean
|
1999-07-10 05:52:00 +08:00
|
|
|
points_adjacent_p (int row1, int col1, int row2, int col2)
|
|
|
|
{
|
|
|
|
int row_diff = abs (row1 - row2);
|
|
|
|
int col_diff = abs (col1 - col2);
|
|
|
|
|
|
|
|
return
|
|
|
|
(row_diff == 1 && col_diff == 1)
|
|
|
|
|| (row_diff == 0 && col_diff == 1)
|
|
|
|
|| (row_diff == 1 && col_diff == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Find the largest and smallest elements in an array of reals. */
|
|
|
|
|
|
|
|
void
|
|
|
|
find_bounds (real *values, unsigned value_count, real *min, real *max)
|
|
|
|
{
|
|
|
|
unsigned this_value;
|
|
|
|
|
|
|
|
/* We must use FLT_MAX and FLT_MIN, instead of the corresponding
|
|
|
|
values for double, because gcc uses the native atof to parse
|
|
|
|
floating point constants, and many atof's choke on the extremes. */
|
|
|
|
*min = FLT_MAX;
|
|
|
|
*max = FLT_MIN;
|
|
|
|
|
|
|
|
for (this_value = 0; this_value < value_count; this_value++)
|
|
|
|
{
|
|
|
|
if (values[this_value] < *min)
|
|
|
|
*min = values[this_value];
|
|
|
|
|
|
|
|
if (values[this_value] > *max)
|
|
|
|
*max = values[this_value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map a range of numbers, some positive and some negative, into all
|
|
|
|
positive, with the greatest being at one and the least at zero.
|
|
|
|
|
|
|
|
This allocates new memory. */
|
|
|
|
|
|
|
|
real *
|
|
|
|
map_to_unit (real *values, unsigned value_count)
|
|
|
|
{
|
|
|
|
real smallest, largest;
|
|
|
|
int this_value;
|
2000-03-10 19:29:24 +08:00
|
|
|
real *mapped_values = g_new (real, value_count);
|
1999-07-10 05:52:00 +08:00
|
|
|
|
|
|
|
find_bounds (values, value_count, &smallest, &largest);
|
|
|
|
|
|
|
|
largest -= smallest; /* We never care about largest itself. */
|
|
|
|
|
|
|
|
for (this_value = 0; this_value < value_count; this_value++)
|
|
|
|
mapped_values[this_value] = (values[this_value] - smallest) / largest;
|
|
|
|
|
|
|
|
return mapped_values;
|
|
|
|
}
|