[GRASS-SVN] r67348 - grass/trunk/vector/v.in.lidar

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Dec 23 13:32:16 PST 2015


Author: wenzeslaus
Date: 2015-12-23 13:32:16 -0800 (Wed, 23 Dec 2015)
New Revision: 67348

Added:
   grass/trunk/vector/v.in.lidar/vector_mask.c
   grass/trunk/vector/v.in.lidar/vector_mask.h
Modified:
   grass/trunk/vector/v.in.lidar/main.c
Log:
v.in.lidar: refactor out vector mask

Modified: grass/trunk/vector/v.in.lidar/main.c
===================================================================
--- grass/trunk/vector/v.in.lidar/main.c	2015-12-23 19:02:45 UTC (rev 67347)
+++ grass/trunk/vector/v.in.lidar/main.c	2015-12-23 21:32:16 UTC (rev 67348)
@@ -32,6 +32,7 @@
 #include "lidar.h"
 #include "attributes.h"
 #include "info.h"
+#include "vector_mask.h"
 
 #ifndef MAX
 #  define MIN(a,b)      ((a<b) ? a : b)
@@ -593,21 +594,10 @@
                                &Fi, have_time, have_color);
     }
 
-    struct Map_info vector_mask;
-    int naareas;
-    int aarea;
-    struct bound_box *mask_area_boxes;
-    int invert_mask = 0;
+    struct VectorMask vector_mask;
     if (vector_mask_opt->answer) {
-        if (Vect_open_old2(&vector_mask, vector_mask_opt->answer, "", vector_mask_field_opt->answer) < 2)
-            G_fatal_error(_("Failed to open vector <%s>"), vector_mask_opt->answer);
-        naareas = Vect_get_num_areas(&vector_mask);
-        mask_area_boxes = G_malloc(naareas * sizeof(struct bound_box));
-        for (aarea = 1; aarea <= naareas; aarea++) {
-            Vect_get_area_box(&vector_mask, aarea, &mask_area_boxes[aarea - 1]);
-        }
-        if (invert_mask_flag->answer)
-            invert_mask = 1;
+        VectorMask_init(&vector_mask, vector_mask_opt->answer,
+                        vector_mask_field_opt->answer, (int)invert_mask_flag->answer);
     }
 
     /* Import feature */
@@ -662,14 +652,7 @@
 	    }
 	}
     if (vector_mask_opt->answer) {
-        skipme = TRUE;
-        for (aarea = 1; aarea <= naareas; aarea++) {
-            if (Vect_point_in_area(x, y, &vector_mask, aarea, &mask_area_boxes[aarea - 1])) {
-                skipme = FALSE;
-                break;
-            }
-        }
-        if (invert_mask ^ skipme) {
+        if (!VectorMask_point_in(&vector_mask, x, y)) {
             n_outside_mask++;
             continue;
         }
@@ -778,8 +761,7 @@
     }
     
     if (vector_mask_opt->answer) {
-        Vect_close(&vector_mask);
-        G_free(mask_area_boxes);
+        VectorMask_destroy(&vector_mask);
     }
     
     LASSRS_Destroy(LAS_srs);

Added: grass/trunk/vector/v.in.lidar/vector_mask.c
===================================================================
--- grass/trunk/vector/v.in.lidar/vector_mask.c	                        (rev 0)
+++ grass/trunk/vector/v.in.lidar/vector_mask.c	2015-12-23 21:32:16 UTC (rev 67348)
@@ -0,0 +1,59 @@
+/*
+ * v.in.lidar vector mask
+ *
+ * Copyright 2011-2015 by Vaclav Petras, and The GRASS Development Team
+ *
+ * This program is free software licensed under the GPL (>=v2).
+ * Read the COPYING file that comes with GRASS for details.
+ *
+ */
+
+#include <grass/vector.h>
+#include <grass/glocale.h>
+
+#include "vector_mask.h"
+
+void VectorMask_init(struct VectorMask *vector_mask, const char *name, const char *layer, int invert_mask)
+{
+    vector_mask->map_info = G_malloc(sizeof(struct Map_info));
+    if (Vect_open_old2(vector_mask->map_info, name, "", layer) < 2)
+        G_fatal_error(_("Failed to open vector <%s>"), name);
+    vector_mask->nareas = Vect_get_num_areas(vector_mask->map_info);
+    vector_mask->area_bboxes = G_malloc(vector_mask->nareas * sizeof(struct bound_box));
+    int i;
+    for (i = 1; i <= vector_mask->nareas; i++) {
+        Vect_get_area_box(vector_mask->map_info, i, &vector_mask->area_bboxes[i - 1]);
+    }
+    if (invert_mask)
+        vector_mask->inverted = 1;
+    else
+        vector_mask->inverted = 0;
+}
+
+void VectorMask_destroy(struct VectorMask *vector_mask)
+{
+    G_free(vector_mask->area_bboxes);
+    Vect_close(vector_mask->map_info);
+    G_free(vector_mask->map_info);
+}
+
+int VectorMask_point_in(struct VectorMask *vector_mask, double x, double y)
+{
+    int is_out = TRUE;
+    int i;
+    for (i = 1; i <= vector_mask->nareas; i++) {
+        if (Vect_point_in_area(x, y, vector_mask->map_info, i, &vector_mask->area_bboxes[i - 1])) {
+            is_out = FALSE;
+            break;
+        }
+    }
+    /* inv out res
+     *  F   T   F
+     *  F   F   T
+     *  T   T   T
+     *  T   F   F
+     */
+    if (vector_mask->inverted ^ is_out)
+        return FALSE;
+    return TRUE;
+}


Property changes on: grass/trunk/vector/v.in.lidar/vector_mask.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Added: grass/trunk/vector/v.in.lidar/vector_mask.h
===================================================================
--- grass/trunk/vector/v.in.lidar/vector_mask.h	                        (rev 0)
+++ grass/trunk/vector/v.in.lidar/vector_mask.h	2015-12-23 21:32:16 UTC (rev 67348)
@@ -0,0 +1,33 @@
+
+/****************************************************************************
+ *
+ * MODULE:       v.in.lidar
+ * AUTHOR(S):    Vaclav Petras
+ * PURPOSE:      vector mask
+ * COPYRIGHT:    (C) 2015 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *               License (>=v2). Read the COPYING file that comes with GRASS
+ *               for details.
+ *
+ *****************************************************************************/
+
+
+#ifndef VECTOR_MASK_H
+#define VECTOR_MASK_H
+
+struct Map_info;
+struct bound_box;
+
+struct VectorMask {
+    struct Map_info *map_info;
+    struct bound_box *area_bboxes;
+    int nareas;
+    int inverted;
+};
+
+void VectorMask_init(struct VectorMask *vector_mask, const char *name, const char *layer, int invert_mask);
+void VectorMask_destroy(struct VectorMask *vector_mask);
+int VectorMask_point_in(struct VectorMask *vector_mask, double x, double y);
+
+#endif /* VECTOR_MASK_H */


Property changes on: grass/trunk/vector/v.in.lidar/vector_mask.h
___________________________________________________________________
Added: svn:mime-type
   + text/x-chdr
Added: svn:eol-style
   + native



More information about the grass-commit mailing list