Komak57

Untitled

Mar 5th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1.         bool check_same_clock_dir(Vector pt1, Vector pt2, Vector pt3, Vector norm)
  2.         {
  3.             float testi, testj, testk;
  4.             float dotprod;
  5.             // normal of trinagle
  6.             testi = (((pt2.Y - pt1.Y) * (pt3.Z - pt1.Z)) - ((pt3.Y - pt1.Y) * (pt2.Z - pt1.Z)));
  7.             testj = (((pt2.Z - pt1.Z) * (pt3.X - pt1.X)) - ((pt3.Z - pt1.Z) * (pt2.X - pt1.X)));
  8.             testk = (((pt2.X - pt1.X) * (pt3.Y - pt1.Y)) - ((pt3.X - pt1.X) * (pt2.Y - pt1.Y)));
  9.  
  10.             // Dot product with triangle normal
  11.             dotprod = testi * norm.X + testj * norm.Y + testk*norm.Z;
  12.  
  13.             //answer
  14.             if (dotprod < 0) return false;
  15.             else return true;
  16.         }
  17.         bool LinePlane(Vector start, Vector end, Vector[] face, Vector norm)
  18.         {
  19.             //Vector direction = end.Subtract(start);
  20.             Vector direction = end.Subtract(start);
  21.  
  22.             // Prepare our barycentric variables
  23.             // UV are our pixel coordinates in a 0 to 1 range
  24.             Vector tmp = face[1].Subtract(face[0]);
  25.             Vector3 u = new Vector3(tmp.X, tmp.Y, tmp.Z);
  26.             //u.Normalize();
  27.             tmp = face[2].Subtract(face[0]);
  28.             Vector3 v = new Vector3(tmp.X, tmp.Y, tmp.Z);
  29.             //v.Normalize();
  30.             tmp = start.Subtract(face[0]);
  31.             Vector3 w = new Vector3(tmp.X, tmp.Y, tmp.Z);
  32.             //w.Normalize();
  33.            
  34.             Vector3 vCrossW = Vector3.Cross(v, w);
  35.             Vector3 vCrossU = Vector3.Cross(v, u);
  36.  
  37.             // Test sign of r
  38.             if (Vector3.Dot(vCrossW, vCrossU) < 0) // < 0
  39.                 return false;
  40.  
  41.             Vector3 uCrossW = Vector3.Cross(u, w);
  42.             Vector3 uCrossV = Vector3.Cross(u, v);
  43.  
  44.             // Test sign of t
  45.             if (Vector3.Dot(uCrossW, uCrossV) < 0) // < 0
  46.                 return false;
  47.  
  48.             // At this piont, we know that r and t and both > 0
  49.             float denom = uCrossV.Length();
  50.             float r = vCrossW.Length() / denom;
  51.             float t = uCrossW.Length() / denom;
  52.  
  53.             if (!(r <= 1 && t <= 1 && r + t <= 1))
  54.                 return false;
  55.             //Find point of intersect to triangle plane.
  56.  
  57.             double a = Vector.Dot(direction, norm);
  58.             if (a == 0) // ray is parallel
  59.                 return false;
  60.             //double t = (Vector.Dot(face[0], norm) - Vector.Dot(norm, start)) / a;
  61.             //find t to intersect point
  62.             if (check_same_clock_dir(face[0], face[1], start, norm))
  63.                 if (check_same_clock_dir(face[1], face[2], start, norm))
  64.                     //if (check_same_clock_dir(face[2], face[1], start, norm))
  65.                         return true;
  66.             return false;
  67.         }
Advertisement
Add Comment
Please, Sign In to add comment