is it ok ?
it add new parameter loop_support which would be 1 or 0 for loop line
support in
sliging_averaging algo
On 04/17/2013 12:46 PM, Markus Metz wrote:
On Wed, Apr 17, 2013 at 10:54 AM, Miroslav Talasek
<miroslav.tala...@seznam.cz> wrote:
hi all
i try make contours from DEM ASTER and i was successfull but contorus is
ugly i try use v.generalze but this function cant change firt and las point
, but some contrours is like loop (first and last point is the same) so
after smoothing with sliging_averaging algo part around start point
look_ahead wide was unchanged iwas so ugly , so i write patch which
implementing loop line support , check if line is loop (start and endpoint
is same ) and smooting correctly, is it usabel for others ?
Yes, I think this is usuable for others. Do you mind sending the patch?
Markus M
attachment:
black is war contours, blue is orig algo and red is with my patch
--
MSc. Miroslav Talasek
Developer, Team leader
Seznam.cz, a.s.
Prague
Czech Republic
tel.:+420 234 694 722
fax: +420 234 694 115
gsm: +420 608 934 724
jabber: mire...@jabber.cz
work-email: miroslav.tala...@firma.seznam.cz
email: miroslav.tala...@seznam.cz
web:http://photo.talasek.sk
_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev
Index: vector/v.generalize/point.h
===================================================================
--- vector/v.generalize/point.h (revision 55852)
+++ vector/v.generalize/point.h (working copy)
@@ -50,6 +50,12 @@
*/
extern inline void point_assign(struct line_pnts *Points, int index,
int with_z, POINT * res);
+/* assign point Points[index] to the res
+ * if with z = 0 then res.z = 0
+ * loop to infinite
+ */
+extern inline void point_assign_loop(struct line_pnts *Points, int index,
+ int with_z, POINT * res);
/* res = k * a */
extern inline void point_scalar(POINT a, double k, POINT * res);
Index: vector/v.generalize/smoothing.c
===================================================================
--- vector/v.generalize/smoothing.c (revision 55852)
+++ vector/v.generalize/smoothing.c (working copy)
@@ -70,7 +70,8 @@
/* mcmaster's sliding averaging algorithm. Return the number of points
* in the output line. This equals to the number of points in the
* input line */
-int sliding_averaging(struct line_pnts *Points, double slide, int look_ahead,
+
+int sliding_averaging(struct line_pnts *Points, double slide, int look_ahead, int loop_support,
int with_z)
{
@@ -78,10 +79,23 @@
double sc;
POINT p, tmp, s;
POINT *res;
+ int is_loop,count;
+ is_loop=0;
n = Points->n_points;
half = look_ahead / 2;
+
+ count = n - half;
+ /* is it loop ?*/
+ if (Points->x[0] == Points->x[n-1]
+ && Points->y[0] == Points->y[n-1]
+ && (Points->z[0] == Points->z[n-1] || with_z == 0) && loop_support){
+ is_loop = 1;
+ count = n + half;
+
+ }
+
if (look_ahead % 2 == 0) {
G_fatal_error(_("Look ahead parameter must be odd"));
return n;
@@ -90,7 +104,7 @@
if (look_ahead >= n || look_ahead == 1)
return n;
- res = G_malloc(sizeof(POINT) * n);
+ res = G_malloc(sizeof(POINT) * (n + half));
if (!res) {
G_fatal_error(_("Out of memory"));
return n;
@@ -104,27 +118,38 @@
point_add(p, tmp, &p);
}
+
/* and calculate the average of remaining points */
- for (i = half; i + half < n; i++) {
- point_assign(Points, i, with_z, &s);
+ for (i = half; i < count; i++) {
+ point_assign_loop(Points, i, with_z, &s);
point_scalar(s, 1.0 - slide, &s);
point_scalar(p, sc * slide, &tmp);
point_add(tmp, s, &res[i]);
- if (i + half + 1 < n) {
- point_assign(Points, i - half, with_z, &tmp);
+ if ((i + half + 1 < n) || is_loop ) {
+ point_assign_loop(Points, i - half, with_z, &tmp);
point_subtract(p, tmp, &p);
- point_assign(Points, i + half + 1, with_z, &tmp);
+ point_assign_loop(Points, i + half + 1, with_z, &tmp);
point_add(p, tmp, &p);
}
}
- for (i = half; i + half < n; i++) {
+ for (i = half; i < count; i++) {
Points->x[i] = res[i].x;
Points->y[i] = res[i].y;
Points->z[i] = res[i].z;
}
+ if (is_loop) {
+ for (i = 0; i < half; i++) {
+ Points->x[i] = res[n + i - 1].x;
+ Points->y[i] = res[n + i - 1].y;
+ Points->z[i] = res[n + i - 1].z;
+ }
+ }
+
+
+
G_free(res);
return Points->n_points;
Index: vector/v.generalize/operators.h
===================================================================
--- vector/v.generalize/operators.h (revision 55852)
+++ vector/v.generalize/operators.h (working copy)
@@ -12,7 +12,7 @@
/* smoothing.c */
int boyle(struct line_pnts *Points, int look_ahead, int with_z);
int sliding_averaging(struct line_pnts *Points, double slide, int look_ahead,
- int with_z);
+ int loop_support, int with_z);
int distance_weighting(struct line_pnts *Points, double slide, int look_ahead,
int with_z);
int chaiken(struct line_pnts *Points, double thresh, int with_z);
Index: vector/v.generalize/main.c
===================================================================
--- vector/v.generalize/main.c (revision 55852)
+++ vector/v.generalize/main.c (working copy)
@@ -48,7 +48,7 @@
int i, type, iter;
struct GModule *module; /* GRASS module for parsing arguments */
struct Option *map_in, *map_out, *thresh_opt, *method_opt,
- *look_ahead_opt;
+ *look_ahead_opt, *loop_support_opt;
struct Option *iterations_opt, *cat_opt, *alpha_opt, *beta_opt, *type_opt;
struct Option *field_opt, *where_opt, *reduction_opt, *slide_opt;
struct Option *angle_thresh_opt, *degree_thresh_opt,
@@ -61,6 +61,7 @@
double degree_thresh, closeness_thresh, betweeness_thresh;
int method;
int look_ahead, iterations;
+ int loop_support;
int layer;
int n_lines;
int simplification, mask_type;
@@ -145,6 +146,13 @@
look_ahead_opt->required = NO;
look_ahead_opt->answer = "7";
look_ahead_opt->description = _("Look-ahead parameter");
+
+ loop_support_opt = G_define_option();
+ loop_support_opt->key = "loop_support";
+ loop_support_opt->type = TYPE_INTEGER;
+ loop_support_opt->required = NO;
+ loop_support_opt->answer = "0";
+ loop_support_opt->description = _("Loop support parameter");
reduction_opt = G_define_option();
reduction_opt->key = "reduction";
@@ -244,6 +252,7 @@
degree_thresh = atof(degree_thresh_opt->answer);
closeness_thresh = atof(closeness_thresh_opt->answer);
betweeness_thresh = atof(betweeness_thresh_opt->answer);
+ loop_support = atoi(loop_support_opt->answer);
mask_type = type_mask(type_opt);
G_debug(3, "Method: %s", method_opt->answer);
@@ -470,7 +479,7 @@
boyle(Points, look_ahead, with_z);
break;
case SLIDING_AVERAGING:
- sliding_averaging(Points, slide, look_ahead, with_z);
+ sliding_averaging(Points, slide, look_ahead, loop_support, with_z);
break;
case DISTANCE_WEIGHTING:
distance_weighting(Points, slide, look_ahead, with_z);
@@ -487,6 +496,7 @@
}
}
+ if (method != SLIDING_AVERAGING || loop_support == 0){
/* safety check, BUG in method if not passed */
if (APoints->x[0] != Points->x[0] ||
APoints->y[0] != Points->y[0] ||
@@ -498,6 +508,8 @@
APoints->z[APoints->n_points - 1] != Points->z[Points->n_points - 1])
G_fatal_error(_("Method '%s' did not preserve last point"), method_opt->answer);
+ }
+
Vect_line_prune(Points);
/* oversimplified line */
Index: vector/v.generalize/point.c
===================================================================
--- vector/v.generalize/point.c (revision 55852)
+++ vector/v.generalize/point.c (working copy)
@@ -63,6 +63,21 @@
return;
}
+inline void point_assign_loop(struct line_pnts *Points, int index, int with_z,
+ POINT * res)
+{
+ index = index % Points->n_points;
+ res->x = Points->x[index];
+ res->y = Points->y[index];
+ if (with_z) {
+ res->z = Points->z[index];
+ }
+ else {
+ res->z = 0;
+ }
+ return;
+}
+
inline void point_scalar(POINT a, double k, POINT * res)
{
res->x = a.x * k;
_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev