Quellcode durchsuchen

r.mapcalc: +ceil, +floor (fixes https://trac.osgeo.org/grass/ticket/769)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@72940 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz vor 6 Jahren
Ursprung
Commit
18c3330907

+ 2 - 0
include/defs/calc.h

@@ -16,6 +16,8 @@ extern args_t c_binop;
 
 extern func_t f_neg;
 extern func_t f_abs;
+extern func_t f_ceil;
+extern func_t f_floor;
 extern args_t c_unop;
 
 extern func_t f_gt;

+ 2 - 0
lib/calc/function.c

@@ -11,6 +11,8 @@ func_desc calc_func_descs[] = {
 
     {"neg", c_unop, f_neg},
     {"abs", c_unop, f_abs},
+    {"ceil", c_unop, f_ceil},
+    {"floor", c_unop, f_floor},
 
     {"gt", c_cmpop, f_gt},
     {"ge", c_cmpop, f_ge},

+ 66 - 0
lib/calc/xceil.c

@@ -0,0 +1,66 @@
+
+#include <math.h>
+
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/calc.h>
+
+/**********************************************************************
+ceil(x)
+
+   the smallest integral value that is not less than x
+**********************************************************************/
+
+int f_ceil(int argc, const int *argt, void **args)
+{
+    int i;
+
+    if (argc < 1)
+	return E_ARG_LO;
+    if (argc > 1)
+	return E_ARG_HI;
+
+    if (argt[0] != argt[1])
+	return E_RES_TYPE;
+
+    switch (argt[1]) {
+    case CELL_TYPE:
+	{
+	    CELL *res = args[0];
+	    CELL *arg1 = args[1];
+
+	    for (i = 0; i < columns; i++)
+		if (IS_NULL_C(&arg1[i]))
+		    SET_NULL_C(&res[i]);
+		else
+		    res[i] = arg1[i];
+	    return 0;
+	}
+    case FCELL_TYPE:
+	{
+	    FCELL *res = args[0];
+	    FCELL *arg1 = args[1];
+
+	    for (i = 0; i < columns; i++)
+		if (IS_NULL_F(&arg1[i]))
+		    SET_NULL_F(&res[i]);
+		else
+		    res[i] = (FCELL) ceil(arg1[i]);
+	    return 0;
+	}
+    case DCELL_TYPE:
+	{
+	    DCELL *res = args[0];
+	    DCELL *arg1 = args[1];
+
+	    for (i = 0; i < columns; i++)
+		if (IS_NULL_D(&arg1[i]))
+		    SET_NULL_D(&res[i]);
+		else
+		    res[i] = ceil(arg1[i]);
+	    return 0;
+	}
+    default:
+	return E_INV_TYPE;
+    }
+}

+ 66 - 0
lib/calc/xfloor.c

@@ -0,0 +1,66 @@
+
+#include <math.h>
+
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/calc.h>
+
+/**********************************************************************
+floor(x)
+
+   the largest integral value that is not greater than x
+**********************************************************************/
+
+int f_floor(int argc, const int *argt, void **args)
+{
+    int i;
+
+    if (argc < 1)
+	return E_ARG_LO;
+    if (argc > 1)
+	return E_ARG_HI;
+
+    if (argt[0] != argt[1])
+	return E_RES_TYPE;
+
+    switch (argt[1]) {
+    case CELL_TYPE:
+	{
+	    CELL *res = args[0];
+	    CELL *arg1 = args[1];
+
+	    for (i = 0; i < columns; i++)
+		if (IS_NULL_C(&arg1[i]))
+		    SET_NULL_C(&res[i]);
+		else
+		    res[i] = arg1[i];
+	    return 0;
+	}
+    case FCELL_TYPE:
+	{
+	    FCELL *res = args[0];
+	    FCELL *arg1 = args[1];
+
+	    for (i = 0; i < columns; i++)
+		if (IS_NULL_F(&arg1[i]))
+		    SET_NULL_F(&res[i]);
+		else
+		    res[i] = (FCELL) floor(arg1[i]);
+	    return 0;
+	}
+    case DCELL_TYPE:
+	{
+	    DCELL *res = args[0];
+	    DCELL *arg1 = args[1];
+
+	    for (i = 0; i < columns; i++)
+		if (IS_NULL_D(&arg1[i]))
+		    SET_NULL_D(&res[i]);
+		else
+		    res[i] = floor(arg1[i]);
+	    return 0;
+	}
+    default:
+	return E_INV_TYPE;
+    }
+}

+ 2 - 0
raster/r.mapcalc/r.mapcalc.html

@@ -333,12 +333,14 @@ acos(x)                 inverse cosine of x (result is in degrees)      F
 asin(x)                 inverse sine of x (result is in degrees)        F
 atan(x)                 inverse tangent of x (result is in degrees)     F
 atan(x,y)               inverse tangent of y/x (result is in degrees)   F
+ceil(x)                 the smallest integral value not less than x     *
 cos(x)                  cosine of x (x is in degrees)                   F
 double(x)               convert x to double-precision floating point    F
 eval([x,y,...,]z)       evaluate values of listed expr, pass results to z
 exp(x)                  exponential function of x                       F
 exp(x,y)                x to the power y                                F
 float(x)                convert x to single-precision floating point    F
+floor(x)                the largest integral value not greater than x   *
 graph(x,x1,y1[x2,y2..]) convert the x to a y based on points in a graph F
 graph2(x,x1[,x2,..],y1[,y2..])
                         alternative form of graph()                     F

+ 2 - 0
raster/r.mapcalc/r3.mapcalc.html

@@ -201,12 +201,14 @@ acos(x)                 inverse cosine of x (result is in degrees)      F
 asin(x)                 inverse sine of x (result is in degrees)        F
 atan(x)                 inverse tangent of x (result is in degrees)     F
 atan(x,y)               inverse tangent of y/x (result is in degrees)   F
+ceil(x)                 the smallest integral value not less than x     *
 cos(x)                  cosine of x (x is in degrees)                   F
 double(x)               convert x to double-precision floating point    F
 eval([x,y,...,]z)       evaluate values of listed expr, pass results to z
 exp(x)                  exponential function of x                       F
 exp(x,y)                x to the power y                                F
 float(x)                convert x to single-precision floating point    F
+floor(x)                the largest integral value not greater than x   *
 graph(x,x1,y1[x2,y2..]) convert the x to a y based on points in a graph F
 graph2(x,x1[,x2,..],y1[,y2..])
                         alternative form of graph()                     F