Simplify geometry

From ObjectVision

Jump to: navigation, search

Configuration examples Simplify geometry

Introduction

The script presents an example how you can simplify the geometry of a polygon. It will delete convex points from the geometry. Compare it with putting a rubber band around the shape, see also convex hull

To what extent the simplifying is executed depends on the amount of iterations implemented.


Tip: use the sequence2points function and take the maximum value for ordinal, you'll get the maximum amount of points for a single polygon. This is the theoretical maximum amount of iterations necessary.

Example

container Simplified := Simplify(iter_domain, 50);  // domain and number of iterations 
 
Template Simplify
{
   //  begin case parameters
   unit<uint32> iter_domain;
   parameter<uint32> nrIterations;
   //  end case parameters
	
   unit<uint32> iter := range(uint32, 0, nrIterations)
   {
      attribute<string> name         := 'I' + string(id(.));
      attribute<string> PrevName := MakeDefined(name[id(.)-1], 'StartingState');
   }

   container StartingState 
   {
      unit<uint32> NextValue := sequence2points(iter_domain/geometry_rd);
   }

   container Iters      := for_each_ne(Iter/name, 'IterT('+Iter/PrevName+', iter_domain)');
   container LastIter := =last('Iters/'+Iter/name);
			
  template IterT 
  {
       //  begin case parameters
       container PrevIterator;
       unit<uint32> domain;
       //  end case parameters

       unit<uint32>  PrevValue       := PrevIterator/NextValue;
       container       _ConvexHullT := MakeConvexHullT(domain, PrevValue);
       unit<uint32>  NextValue       := _ConvexHullT/sequence/Convex_hull_points;
  }

  container MakeFinal 
   {
      unit <uint32> domain_with_endpoints := union_unit(iter_domain, LastIter/NextValue)
      {
         attribute<geometries/rdc>  point             := union_data(., LastIter/NextValue/point, first(LastIter/NextValue/point, LastIter/NextValue/SequenceNr));
         attribute<uint32>                SequenceNr := union_data(., LastIter/NextValue/SequenceNr, ID(Iteratie_domain));
       }
      attribute<geometries/rdc> convex_hull (iter_domain, poly) := points2sequence(domain_with_endpoints /point, domain_with_endpoints /SequenceNr);
    }
 	
  Template MakeConvexHullT
   {
       //  begin case parameters
        unit<uint32> domain;
        unit<uint32> seq;
       //  end case parameters

        unit<uint32> sequence := seq
        {
           attribute<geometries/rdc>  point             := seq/point;
           attribute<uint32>                SequenceNr := seq/SequenceNr;

           attribute<uint32>                 min_index  (domain)  := min_index(id(.), SequenceNr);
           attribute<uint32>                 max_index (domain)  := max_index(id(.), SequenceNr);

           attribute<bool>                     IsFirst   := id(.) == min_index[SequenceNr];
           attribute<bool>                     IsLast   := id(.) == max_index[SequenceNr];
           attribute<uint32>                  prev_id := IsFirst ? rjoin(SequenceNr, id(domain), max_index) : ID(.)-1;
           attribute<uint32>                  next_id := Islast  ? rjoin(SequenceNr, id(domain), min_index) : ID(.)+1;
           attribute<geometries/rdc>    A          := point[prev_id];
           attribute<geometries/rdc>    B          := point;
           attribute<geometries/rdc>    C          := point[next_id];
           attribute<geometries/rdc>    p           := B - A;
           attribute<geometries/rdc>    q           := C - B;

           attribute<float32>                 Px         := pointcol(P);
           attribute<float32>                 Py         := pointrow(P);
           attribute<float32>                 Qx         := pointcol(Q);
           attribute<float32>                 Qy         := pointrow(Q);
			
           attribute<float32>                 det         := Px*Qy - Py*Qx;
			
           unit<uint32> Convex_hull_points := subset(det <= 0f && (point != rjoin(prev_id, id(.), point)))
           {
              attribute<geometries/rdc>  point              := ../point[nr_OrgEntity];
              attribute<uint32>                SequenceNr  := ../SequenceNr[nr_OrgEntity];
           }
      }
   }
}
Personal tools