Advertisement
romanilyin

ViewShed

Jul 4th, 2020
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. //C++ TO C# CONVERTER WARNING: The original C++ declaration of the following method implementation was not found:
  5. //ORIGINAL LINE: inline void map::_visibility_scan(const STLVector<float>& in,STLVector<float>& out,const vec_t& eye,int start_x,int stop_x,int y,int prev_y)
  6.  
  7. //C++ TO C# CONVERTER WARNING: The original C++ declaration of the following method implementation was not found:
  8. //ORIGINAL LINE: void map::visibility_add(const vec_t& eye)
  9.  
  10.  
  11. public partial class map
  12. {
  13.                 public void _visibility_scan(List<float> @in, List<float> @out, vec_t eye, int start_x, int stop_x, int y, int prev_y)
  14.                 {
  15.                 int xdir = (start_x < stop_x)? 1: -1;
  16.                 for (int x = start_x; x != stop_x; x += xdir)
  17.                 {
  18.                 int x_diff = Math.Abs(eye.x - x);
  19.                 int y_diff = Math.Abs(eye.z - y);
  20.                 bool horiz = (x_diff >= y_diff);
  21.                 int x_step = horiz? 1: x_diff / y_diff;
  22.                 int in_x = x - x_step * xdir; // where in the in buffer would we get the inner value?
  23.                 float outer_d = vec2_t(x,y).distance(vec2_t(eye.x,eye.z));
  24.                 float inner_d = vec2_t(in_x,horiz? y: prev_y).distance(vec2_t(eye.x,eye.z));
  25.                 float inner = (horiz? @out: @in).at(in_x) * (outer_d / inner_d); // get the inner value, scaling by
  26.                 distance const float outer = height_at(x,y) - eye.y; // height we are at right now in the map, eye-relative
  27.                 if (inner <= outer)
  28.                 {
  29.                 @out[x] = outer;
  30.                 vis.at(y * width + x) = VISIBLE;
  31.                 }
  32.                 else
  33.                 {
  34.                 @out[x] = inner;
  35.                 vis.at(y * width + x) = NOT_VISIBLE;
  36.                 }
  37.                 }
  38.                 }
  39.                 public void visibility_add(vec_t eye)
  40.                 {
  41.                 const float BASE = -10000F; // represents a downward vector that would always be visible
  42.                 List<float> scan_0 = new List<float>();
  43.                 List<float> scan_out = new List<float>();
  44.                 List<float> scan_in = new List<float>();
  45.                 scan_0.Resize(width);
  46.                 vis[eye.z * width + eye.x - 1] = vis[eye.z * width + eye.x] = vis[eye.z * width + eye.x + 1] = VISIBLE;
  47.                 scan_0[eye.x] = BASE;
  48.                 scan_0[eye.x - 1] = BASE;
  49.                 scan_0[eye.x + 1] = BASE;
  50.                 _visibility_scan(scan_0,scan_0,eye,eye.x + 2,width,eye.z,eye.z);
  51.                 _visibility_scan(scan_0,scan_0,eye,eye.x - 2,-1,eye.z,eye.z);
  52.                 scan_out = new List<float>(scan_0);
  53.                 for (int y = eye.z + 1; y < height; y++)
  54.                 {
  55.                 scan_in = new List<float>(scan_out);
  56.                 _visibility_scan(scan_in,scan_out,eye,eye.x,-1,y,y - 1);
  57.                 _visibility_scan(scan_in,scan_out,eye,eye.x,width,y,y - 1);
  58.                 }
  59.                 scan_out = new List<float>(scan_0);
  60.                 for (int y = eye.z - 1; y >= 0; y--)
  61.                 {
  62.                 scan_in = new List<float>(scan_out);
  63.                 _visibility_scan(scan_in,scan_out,eye,eye.x,-1,y,y + 1);
  64.                 _visibility_scan(scan_in,scan_out,eye,eye.x,width,y,y + 1);
  65.                 }
  66.                 }
  67. }
  68.  
  69. //Helper class added by C++ to C# Converter:
  70.  
  71. //----------------------------------------------------------------------------------------
  72. //  Copyright © 2006 - 2020 Tangible Software Solutions, Inc.
  73. //  This class can be used by anyone provided that the copyright notice remains intact.
  74. //
  75. //  This class is used to convert some of the C++ std::vector methods to C#.
  76. //----------------------------------------------------------------------------------------
  77.  
  78. using System.Collections.Generic;
  79.  
  80. internal static class VectorHelper
  81. {
  82.     public static void Resize<T>(this List<T> list, int newSize, T value = default(T))
  83.     {
  84.         if (list.Count > newSize)
  85.             list.RemoveRange(newSize, list.Count - newSize);
  86.         else if (list.Count < newSize)
  87.         {
  88.             for (int i = list.Count; i < newSize; i++)
  89.             {
  90.                 list.Add(value);
  91.             }
  92.         }
  93.     }
  94.  
  95.     public static void Swap<T>(this List<T> list1, List<T> list2)
  96.     {
  97.         List<T> temp = new List<T>(list1);
  98.         list1.Clear();
  99.         list1.AddRange(list2);
  100.         list2.Clear();
  101.         list2.AddRange(temp);
  102.     }
  103.  
  104.     public static List<T> InitializedList<T>(int size, T value)
  105.     {
  106.         List<T> temp = new List<T>();
  107.         for (int count = 1; count <= size; count++)
  108.         {
  109.             temp.Add(value);
  110.         }
  111.  
  112.         return temp;
  113.     }
  114.  
  115.     public static List<List<T>> NestedList<T>(int outerSize, int innerSize)
  116.     {
  117.         List<List<T>> temp = new List<List<T>>();
  118.         for (int count = 1; count <= outerSize; count++)
  119.         {
  120.             temp.Add(new List<T>(innerSize));
  121.         }
  122.  
  123.         return temp;
  124.     }
  125.  
  126.     public static List<List<T>> NestedList<T>(int outerSize, int innerSize, T value)
  127.     {
  128.         List<List<T>> temp = new List<List<T>>();
  129.         for (int count = 1; count <= outerSize; count++)
  130.         {
  131.             temp.Add(InitializedList(innerSize, value));
  132.         }
  133.  
  134.         return temp;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement