Examlpes

Problem: Reconstruct a surface from unordered wireframe edges

The SurfaceReconstructor class provides methods to reconstruct a surface consisting of triangles and quads from unordered edges forming a wireframe model.

The off file of this example is taken from the Princeton Shape Benchmark.

 

Solution using G#

  1. public static void Reconstruct()
  2. {
  3.     //-------------------------------------------------------------
  4.     //Settings for best results.
  5.     //-------------------------------------------------------------
  6.     ceometric.VectorGeometry.Global.AbsoluteEpsilon = -1;
  7.     ceometric.VectorGeometry.Global.RelativeEpsilon = -1;
  8.  
  9.     //-------------------------------------------------------------
  10.     //Generate input edge data. This is done by reading triangles
  11.     //from an .off file and extracting the edges of the triangles
  12.     //into a list. The edge list will be randomly permuted and
  13.     //contain many duplicate edges, which is OK for the algorithm.
  14.     //-------------------------------------------------------------
  15.     string offFile = @"E:\ceometric\ComputationalCAD\OFF\shark.off";
  16.     OFFReader or = new OFFReader();
  17.     or.Read(offFile);
  18.     List<Edge> edges = GetEdges(or.Triangles);
  19.  
  20.     //-------------------------------------------------------------
  21.     //Reconstruct surface. Measure elapsed time.
  22.     //-------------------------------------------------------------
  23.     KernelTimeGauge tg = new KernelTimeGauge();
  24.     tg.Start();
  25.     SurfaceReconstructor sr = new SurfaceReconstructor(edges);
  26.     sr.ReconstructSurface(6);
  27.     tg.Stop();
  28.  
  29.     //-------------------------------------------------------------
  30.     //Some statistics
  31.     //-------------------------------------------------------------
  32.     Console.WriteLine(
  33.         "Triangles read         : " + or.Triangles.Count);
  34.     Console.WriteLine(
  35.         "Edges extracted        : " + edges.Count);
  36.     Console.WriteLine(
  37.         "Triangles reconstructed: " + or.Triangles.Count +
  38.         " in " + tg.DurationInMilliSeconds.ToString() + "ms");
  39.  
  40.     //-------------------------------------------------------------
  41.     //Visualize
  42.     //-------------------------------------------------------------
  43.     Scene sc = new Scene();
  44.     Scene.Layer lay = new Scene.Layer("reconstructed_surface", 7);
  45.     int failed = 0;
  46.     sc.Add(sr.GetSurfaceAsTriangleList(ref failed), lay);
  47.     sc.WriteDxf(@"e:\sr.dxf");
  48.  
  49.     Console.ReadLine();
  50. }
  51.  
  52. /// <summary>A helper method to extract the edges from a
  53. /// list of triangles. Contains many duplicates.</summary>
  54. /// <param name="trs">A list of triangles.</param>
  55. /// <returns>Returns a list of randomly permuted edges.</returns>
  56. public static List<Edge> GetEdges(List<Triangle> trs)
  57. {
  58.     List<Edge> edges = new List<Edge>();
  59.     for (int i = 0; i < trs.Count; i++)
  60.     {
  61.         edges.Add(trs[i].Edge1);
  62.         edges.Add(trs[i].Edge2);
  63.         edges.Add(trs[i].Edge3);
  64.     }
  65.     Edge.RandomPermute(ref edges);
  66.     return edges;
  67. }

Output

A shark reconstructed from wireframe edges.

<< back