Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. import vector
  2.  
  3. def cross(a, b):
  4.     c = vector.Vector3(a.y*b.z - a.z*b.y,
  5.                        a.z*b.x - a.x*b.z,
  6.                        a.x*b.y - a.y*b.x)
  7.     return c
  8.  
  9.  
  10. def dot(a, b):
  11.     return a.x*b.x + a.y*b.y + a.z*b.z
  12.  
  13.  
  14. class Plane:
  15.     # creates empty values
  16.     def __init__(self):
  17.         self.equation = [0,0,0,0]
  18.         self.origin = vector.Vector3()
  19.         self.normal = vector.Vector3()
  20.  
  21.     """
  22.    # initialize values to create a plane
  23.    def plane_plane(self, origin, normal):
  24.        self.origin = self.origin
  25.        self.normal = self.normal
  26.        self.equation[0] = self.normal.x
  27.        self.equation[1] = self.normal.y
  28.        self.equation[2] = self.normal.z
  29.        self.equation[3] = -(self.normal.x * self.origin.x +
  30.                             self.normal.y * self.origin.y +
  31.                             self.normal.z * self.origin.z)
  32.    """
  33.    
  34.     # initialize values to create a triangular plane
  35.     def plane_triangle(self, p1, p2, p3):
  36.         self.normal = cross(p2-p1, p3-p1)
  37.         #self.normal = np.cross(p2-p1, p3-p1)
  38.         self.normal = self.normal.normalized()
  39.         self.origin = p1
  40.         self.equation[0] = self.normal.x
  41.         self.equation[1] = self.normal.y
  42.         self.equation[2] = self.normal.z
  43.         self.equation[3] = -(self.normal.x * self.origin.x +
  44.                              self.normal.y * self.origin.y +
  45.                              self.normal.z * self.origin.z)
  46.  
  47.     # bool of whether direction intersects tri
  48.     def isFrontFacingTo(self, direction):
  49.         dot_product = dot(self.normal, direction)
  50.         #print(dot_product)
  51.         return dot_product <= 0
  52.  
  53.     def signedDistanceTo(self, point):
  54.         print(self.equation[3])
  55.         return dot(point, self.normal) + self.equation[3]
  56.  
  57.  
  58. tri = Plane()
  59.  
  60. tri.plane_triangle(vector.Vector3(1,1,0), # 0,0,0
  61.                    vector.Vector3(2,1,0), # 1,0,0
  62.                    vector.Vector3(2,2,0)) # 1,0,1
  63.  
  64.  
  65. #print(tri.isFrontFacingTo(vector.Vector3(-1,0,0)))
  66. print(tri.signedDistanceTo(vector.Vector3(743,5000,6)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement