Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3.  
  4. namespace Color
  5. {
  6.     /// <summary>
  7.     /// Point in a 3-Dimensional space.
  8.     /// </summary>
  9.     public struct Point
  10.     {
  11.         public double x;
  12.         public double y;
  13.         public double z;
  14.     }
  15.  
  16.  
  17.     public static class Color
  18.     {
  19.         /// <summary>
  20.         /// Gets the euclidean distance between the two points without computing the square root.
  21.         /// </summary>
  22.         /// <param name="p1">First point.</param>
  23.         /// <param name="p2">Second point.</param>
  24.         /// <returns></returns>
  25.         public static double EDist3DSquare(Point p1, Point p2)
  26.         {
  27.             double dx = p1.x - p2.x;
  28.             double dy = p1.y - p2.y;
  29.             double dz = p1.z - p2.z;
  30.  
  31.             return dx * dx + dy * dy + dz * dz;
  32.         }
  33.  
  34.  
  35.         /// <summary>
  36.         /// Assigns to each point an identifier such that those points which distance is
  37.         /// less or equal to the distance specified will have a different identifier.
  38.         /// The identifier is an integer number greater or equal to 0.
  39.         /// </summary>
  40.         /// <param name="points">Points to map.</param>
  41.         /// <param name="distance">The largest distance between two points to be considered neighbors.</param>
  42.         /// <returns>Returns an array of identifiers.</returns>
  43.         public static int[] Map(Point[] points, double distance)
  44.         {
  45.             double dist2 = distance * distance;
  46.             var ids = new int[points.Length];
  47.  
  48.             // init identifiers with an invalid value
  49.             for (int i = 0; i < ids.Length; i++)
  50.                 ids[i] = -1;
  51.  
  52.             // iterate over all points
  53.             for (int i = 0; i < points.Length; i++)
  54.             {
  55.                 int id = 0;
  56.  
  57.                 // get neighbors identifier
  58.                 var colors = GetNeighborsID(points, ids, i, dist2);
  59.  
  60.                 // compute the first available id
  61.                 while (colors.Contains(id))
  62.                     id++;
  63.  
  64.                 // assign the id
  65.                 ids[i] = id;
  66.             }
  67.  
  68.             return ids;
  69.         }
  70.  
  71.  
  72.         /// <summary>
  73.         /// Gets the neighbors color.
  74.         /// </summary>
  75.         /// <param name="points">Array of points.</param>
  76.         /// <param name="ids">Array of identifiers.</param>
  77.         /// <param name="idx">Index of the current point.</param>
  78.         /// <param name="dist2">Neighbor distance (its square).</param>
  79.         /// <returns>Returns a list of already assigned identifiers.</returns>
  80.         private static List<int> GetNeighborsID(Point[] points, int[] ids, int idx, double dist2)
  81.         {
  82.             var colors = new List<int>();
  83.  
  84.             for (int i = 0; i < idx; i++)
  85.             {
  86.                 Debug.Assert(ids[i] >= 0);
  87.  
  88.                 // check if the i-point is a neighbor for idx-point
  89.                 if (EDist3DSquare(points[idx], points[i]) <= dist2)
  90.                     colors.Add(ids[i]);
  91.             }
  92.  
  93.             return colors;
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement