def pointInTri2D(v, v1, v2, v3):
    global dict_matrix

    key = v1.x, v1.y, v2.x, v2.y, v3.x, v3.y

    # Commented because its slower to do teh bounds check, we should realy cache the bounds info for each face.
    '''
	# BOUNDS CHECK
	xmin= 1000000
	ymin= 1000000
	
	xmax= -1000000
	ymax= -1000000
	
	for i in (0,2,4):
		x= key[i]
		y= key[i+1]
		
		if xmax<x:	xmax= x
		if ymax<y:	ymax= y
		if xmin>x:	xmin= x
		if ymin>y:	ymin= y	
	
	x= v.x
	y= v.y
	
	if x<xmin or x>xmax or y < ymin or y > ymax:
		return False
	# Done with bounds check
	'''
    try:
        mtx = dict_matrix[key]
        if not mtx:
            return False
    except:
        side1 = v2 - v1
        side2 = v3 - v1

        nor = side1.cross(side2)

        l1 = [side1[0], side1[1], side1[2]]
        l2 = [side2[0], side2[1], side2[2]]
        l3 = [nor[0], nor[1], nor[2]]

        mtx = Matrix(l1, l2, l3)

        # Zero area 2d tri, even tho we throw away zerop area faces
        # the projection UV can result in a zero area UV.
        if not mtx.determinant():
            dict_matrix[key] = None
            return False

        mtx.invert()

        dict_matrix[key] = mtx

    uvw = (v - v1) * mtx
    return 0 <= uvw[0] and 0 <= uvw[1] and uvw[0] + uvw[1] <= 1
def pointInTri2D(v, v1, v2, v3):
    global dict_matrix

    key = v1.x, v1.y, v2.x, v2.y, v3.x, v3.y

    # Commented because its slower to do teh bounds check, we should realy cache the bounds info for each face.
    """
	# BOUNDS CHECK
	xmin= 1000000
	ymin= 1000000
	
	xmax= -1000000
	ymax= -1000000
	
	for i in (0,2,4):
		x= key[i]
		y= key[i+1]
		
		if xmax<x:	xmax= x
		if ymax<y:	ymax= y
		if xmin>x:	xmin= x
		if ymin>y:	ymin= y	
	
	x= v.x
	y= v.y
	
	if x<xmin or x>xmax or y < ymin or y > ymax:
		return False
	# Done with bounds check
	"""
    try:
        mtx = dict_matrix[key]
        if not mtx:
            return False
    except:
        side1 = v2 - v1
        side2 = v3 - v1

        nor = side1.cross(side2)

        l1 = [side1[0], side1[1], side1[2]]
        l2 = [side2[0], side2[1], side2[2]]
        l3 = [nor[0], nor[1], nor[2]]

        mtx = Matrix(l1, l2, l3)

        # Zero area 2d tri, even tho we throw away zerop area faces
        # the projection UV can result in a zero area UV.
        if not mtx.determinant():
            dict_matrix[key] = None
            return False

        mtx.invert()

        dict_matrix[key] = mtx

    uvw = (v - v1) * mtx
    return 0 <= uvw[0] and 0 <= uvw[1] and uvw[0] + uvw[1] <= 1