Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1.         private static void PrecomputeTraits(TriangleMesh mesh)
  2.         {
  3.  
  4.            
  5.             /// compute all cotans
  6.             /// store each cotan at the foot of each halfedge
  7.             foreach (var ti in mesh.Faces)
  8.             {
  9.                
  10.                 //area calculation
  11.                 var l1 = ti.Halfedge.FromVertex.Traits.Position - ti.Halfedge.ToVertex.Traits.Position;
  12.                 var l2 = ti.Halfedge.Next.FromVertex.Traits.Position - ti.Halfedge.Next.ToVertex.Traits.Position;
  13.                 ti.Traits.Area = Vector3.Cross(l1, l2).Length()/2;
  14.  
  15.                 /// compute cotans
  16.                 foreach (var hi in ti.Halfedges)
  17.                 {
  18.                     /// TODO_A2 Task 3:
  19.                     /// Implement (extended) cotangents Laplacian matrix (easy)
  20.                     /// here it is recommended to precompute cotans and areas for all faces/halfedges
  21.                     /// You should call it at the beginning of CreateCotanLaplacian and CreateExtendedCotanLaplacian.
  22.  
  23.                     Vector3 v1 = hi.ToVertex.Traits.Position - hi.FromVertex.Traits.Position;
  24.                     Vector3 v2 = hi.Previous.FromVertex.Traits.Position - hi.Previous.ToVertex.Traits.Position;
  25.                     //v1.Normalize();
  26.                     //v2.Normalize();
  27.                     double angle = Math.Acos(Vector3.Dot(v1, v2) / v1.Length()*v2.Length());
  28.                     if (angle > Math.PI/2) angle = Math.PI - angle;
  29.                     hi.Traits.Cotan = 1.0 / Math.Tan(angle);
  30.                 }
  31.             }
  32.             //compute
  33.             foreach (var hi in mesh.Halfedges)
  34.             {
  35.                 if(hi.Face == null || hi.Opposite.Face == null) continue;
  36.                 //triangle is non-obtuse
  37.                 Vector3 v1 = hi.ToVertex.Traits.Position - hi.FromVertex.Traits.Position;
  38.                 Vector3 v2 = hi.Previous.FromVertex.Traits.Position - hi.Previous.ToVertex.Traits.Position;
  39.  
  40.                 if (hi.Traits.Cotan >= 0 && hi.Next.Traits.Cotan >= 0 && hi.Previous.Traits.Cotan >= 0)
  41.                 {
  42.                     hi.Traits.VoronoiRegionArea = (v1.Length()*hi.Previous.Traits.Cotan
  43.                                                   + v2.Length()*hi.Next.Traits.Cotan)/8.0;
  44.                 }
  45.                 else if (hi.Traits.Cotan < 0) //triangle is obtuse, angle at halfedge is obtuse
  46.                 {
  47.                     hi.Traits.VoronoiRegionArea = hi.Face.Traits.Area/2.0;
  48.                 }
  49.                 else //Some other angle is obtuse
  50.                 {
  51.                     hi.Traits.VoronoiRegionArea = hi.Face.Traits.Area / 4.0;
  52.                 }
  53.  
  54.                 //calculate the are for the opposite face of the halfedge
  55.                 Vector3 v3 = hi.Opposite.Next.ToVertex.Traits.Position - hi.FromVertex.Traits.Position;
  56.                 //triangle is non-obtuse
  57.                 if (hi.Opposite.Traits.Cotan >= 0 && hi.Opposite.Next.Traits.Cotan >= 0 && hi.Opposite.Previous.Traits.Cotan >= 0)
  58.                 {
  59.                     hi.Traits.VoronoiRegionArea += (v1.Length() * hi.Opposite.Previous.Traits.Cotan
  60.                                                   + v3.Length() * hi.Opposite.Traits.Cotan) / 8.0;
  61.  
  62.                 }
  63.                 else if (hi.Traits.Cotan < 0) //triangle is obtuse, angle at halfedge is obtuse
  64.                 {
  65.                     hi.Traits.VoronoiRegionArea += hi.Opposite.Face.Traits.Area / 2.0;
  66.                 }
  67.                 else //Some other angle is obtuse
  68.                 {
  69.                     hi.Traits.VoronoiRegionArea += hi.Opposite.Face.Traits.Area / 4.0;
  70.                 }
  71.  
  72.  
  73.             }
  74.            
  75.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement