import pickle

from interp.baker import interpolate
from interp.baker import get_phis
import interp

__version__ = interp.__version__


class grid(object):
    def __init__(self):
        """
            Child classes should populate at a minimum the points and values
            arrays, and a method for getting a simplex and extra points.
        """
        pass

    def get_simplex_extra_points(self, X, extra_points=8):
        pass

    def interpolate(self, X, order=2, extra_points=3):
        r, s = self.get_simplex_extra_points(X, extra_points=extra_points)
        return interpolate(X, self.points[r], self.values[r],
                              self.points[s], self.values[s], order=order)

    def dump_to_blender_files(self,
                    pfile='/tmp/points.p', cfile='/tmp/cells.p'):
        if len(self.verts[0]) == 2:
            pickle.dump([(p[0], p[1], 0.0) for p in self.verts],
                                open(pfile, 'w'))
        else:
            pickle.dump([(p[0], p[1], p[2]) for p in self.verts],
                                open(pfile, 'w'))

        pickle.dump([f.verts for f in self.cells.itervalues()],
                                open(cfile, 'w'))


def contains(X, R):
    """
        tests if X (point) is in R

        R is a simplex, represented by a list of N-degree coordinates
    """
    phis = get_phis(X, R)
    any_negatives = any(map(lambda x: x < 0, phis))
    return not any_negatives
