Beispiel #1
0
  def openBox(self,cmd):
    '''
    Open a bounding box

    '''
    from PyRatBox import PyRatBox
    box = PyRatBox(np.zeros(3),None)
    box.invisible = True
    box.empty = True
    self.top.contents.append(box)
    self.stack.append(self.top)
    self.top = self.top.contents[-1]

    self.verbose == 2 and sys.stderr.write('[%d}'%len(self.stack))
Beispiel #2
0
    def openBox(self, cmd):
        '''
    Open a bounding box

    '''
        from PyRatBox import PyRatBox
        box = PyRatBox(np.zeros(3), None)
        box.invisible = True
        box.empty = True
        self.top.contents.append(box)
        self.stack.append(self.top)
        self.top = self.top.contents[-1]

        self.verbose == 2 and sys.stderr.write('[%d}' % len(self.stack))
Beispiel #3
0
  def box(self,cmd):
    '''
    Open a solid box

    Start a new container box and push on stack
   
    box minx miny minz maxx maxy maxz 
    '''
    from PyRatBox import PyRatBox
    box = PyRatBox(np.array(cmd[1:1+3]).astype(float),np.array(cmd[4:4+3]).astype(float))
    box.invisible = False
    box.empty = False
    self.top.contents.append(box)
    
    self.verbose == 2 and sys.stderr.write('B'%len(self.stack))
Beispiel #4
0
    def box(self, cmd):
        '''
    Open a solid box

    Start a new container box and push on stack
   
    box minx miny minz maxx maxy maxz 
    '''
        from PyRatBox import PyRatBox
        box = PyRatBox(
            np.array(cmd[1:1 + 3]).astype(float),
            np.array(cmd[4:4 + 3]).astype(float))
        box.invisible = False
        box.empty = False
        self.top.contents.append(box)

        self.verbose == 2 and sys.stderr.write('B' % len(self.stack))
Beispiel #5
0
    def __init__(self,
                 filename,
                 GL=False,
                 verbose=True,
                 reportingFrequency=10):
        self.setupDictionary()
        self.GL = hasGL and GL
        self.verbose = verbose
        self.reportingFrequency = reportingFrequency

        none = np.zeros(3)
        self.top = PyRatBox(none, none, material=None)
        self.top.invisible = True
        self.root = self.top
        self.root.defined = False
        self.root.invisible = True
        self.root.empty = False

        self.infinitePlane = []

        self.error = self.top.error

        # this is where we keep groups
        # we have to reconcile all of these as well as
        # the main object in case some of them
        # are #define'd
        self.group = {}

        self.stack = []
        self.point = []
        self.nPoints = 0
        if self.verbose:
            sys.stderr.write('Reading data from %s\n' % filename)
        self.read(filename)
        if self.verbose:
            sys.stderr.write('\n ... sorting bbox contents\n')
        if not self.root.isDefined():
            self.root = self.reconcile(self.root, 1)
        for i in self.group.keys():
            self.group[i] = self.reconcile(self.group[i], 1, defined=True)
        # reset the stack
        self.point = np.array(self.point)
        self.verbose = verbose
        self.reportingFrequency = 10
        if self.GL:
            self.triangleN = []
            self.triangleData = []
            if self.verbose:
                sys.stderr.write('\n ... sorting GL representation\n')
            self.GLobjects = []
            self.loadGL(self.root, np.eye(3), np.matrix(np.zeros(3)))
            for pl in self.infinitePlane:
                # how big to do an infinite plane then?
                pa = np.array([pl.base-self.root.min.copy(),pl.base-self.root.min.copy(),\
                               pl.base-self.root.max.copy(),pl.base-self.root.max.copy()])
                pa[:, 2] = 0.
                pa[1, 1] = pa[3, 1]
                pa[2, 1] = pa[0, 1]
                NN = len(self.triangleData)
                for i in xrange(4):
                    pa[i, 2] = -1. * dot(pa[i], pl.normal) / pl.normal[2]
                    self.triangleData.append(pl.base - pa[i])
                self.triangleN.append([NN + 0, NN + 1, NN + 3])
                self.triangleN.append([NN + 3, NN + 2, NN + 0])
            if len(self.triangleData):
                self.GLobjects.append(
                    Facet(coords=self.triangleData, indices=self.triangleN))
Beispiel #6
0
  def intersects(self,ray,closest=True):
    '''
    Call intersect but return ray as well

    and do hierarchical intersections
    '''
    from PyRatBox import PyRatBox
    import numpy as np
    # transform ray
    transformed_ray = ray.copy()

    try:
      transformed_ray.sourceOfRay = ray.sourceOfRay
    except:
      pass

    try:
      transformed_ray.origin -= self.offset
    except:
      pass

    try:
      transformed_ray.direction = np.array(np.matrix(ray.direction) * self.matrix.T).flatten()
      mod_ray_direction=sqrt(dot(transformed_ray.direction,transformed_ray.direction))
      transformed_ray.direction /= mod_ray_direction
      transformed_ray.origin = np.array(np.matrix(transformed_ray.origin) * self.matrix.T).flatten()
      # to account for scaling effects
      transformed_ray.origin /= (mod_ray_direction*mod_ray_direction)
      transformed_ray.length /= mod_ray_direction
      transformed_ray.tnear /= mod_ray_direction
      transformed_ray.tfar /= mod_ray_direction
      transformed_ray.big /= mod_ray_direction
    except:
      pass
    # with a clone, we are not interested in the intersection other
    # than the transformation we apply
    # contents[0] will always be the contents of a clone as it
    # points to a group which must be a bounding box
    #import pdb;pdb.set_trace()
    hit,thisRay  = PyRatBox.intersects(self.contents[0],transformed_ray,closest=closest)
    if hit:
      try:
        #import pdb;pdb.set_trace()
        thisRay.localNormal = np.array(np.matrix(thisRay.localNormal) * self.matrix).flatten()
        mod = sqrt(dot(thisRay.localNormal,thisRay.localNormal))
        thisRay.localNormal /= mod
      except:
        pass

      try:
        thisRay.length *= self.scale
        thisRay.tnear *= self.scale
        thisRay.tfar *= self.scale
        thisRay.big *= self.scale
      except:
        pass

      try:
        #import pdb;pdb.set_trace()
        #thisRay.length *= mod_ray_direction
        thisRay.origin = ray.origin
        thisRay.direction = ray.direction
      except:
        pass
      ray.ccopy(thisRay)
    return hit,ray
Beispiel #7
0
    def intersects(self, ray, closest=True):
        '''
    Call intersect but return ray as well

    and do hierarchical intersections
    '''
        from PyRatBox import PyRatBox
        import numpy as np
        # transform ray
        transformed_ray = ray.copy()

        try:
            transformed_ray.sourceOfRay = ray.sourceOfRay
        except:
            pass

        try:
            transformed_ray.origin -= self.offset
        except:
            pass

        try:
            transformed_ray.direction = np.array(
                np.matrix(ray.direction) * self.matrix.T).flatten()
            mod_ray_direction = sqrt(
                dot(transformed_ray.direction, transformed_ray.direction))
            transformed_ray.direction /= mod_ray_direction
            transformed_ray.origin = np.array(
                np.matrix(transformed_ray.origin) * self.matrix.T).flatten()
            # to account for scaling effects
            transformed_ray.origin /= (mod_ray_direction * mod_ray_direction)
            transformed_ray.length /= mod_ray_direction
            transformed_ray.tnear /= mod_ray_direction
            transformed_ray.tfar /= mod_ray_direction
            transformed_ray.big /= mod_ray_direction
        except:
            pass
        # with a clone, we are not interested in the intersection other
        # than the transformation we apply
        # contents[0] will always be the contents of a clone as it
        # points to a group which must be a bounding box
        #import pdb;pdb.set_trace()
        hit, thisRay = PyRatBox.intersects(self.contents[0],
                                           transformed_ray,
                                           closest=closest)
        if hit:
            try:
                #import pdb;pdb.set_trace()
                thisRay.localNormal = np.array(
                    np.matrix(thisRay.localNormal) * self.matrix).flatten()
                mod = sqrt(dot(thisRay.localNormal, thisRay.localNormal))
                thisRay.localNormal /= mod
            except:
                pass

            try:
                thisRay.length *= self.scale
                thisRay.tnear *= self.scale
                thisRay.tfar *= self.scale
                thisRay.big *= self.scale
            except:
                pass

            try:
                #import pdb;pdb.set_trace()
                #thisRay.length *= mod_ray_direction
                thisRay.origin = ray.origin
                thisRay.direction = ray.direction
            except:
                pass
            ray.ccopy(thisRay)
        return hit, ray