Пример #1
0
def findDimensions(pointList):
  '''finds the dimension in the 3 principal directions, longest first'''
  dimensions = []
  directions = sortDirections(pointList)
  for direction in directions:
    firstPoint = pointList[0]
    try:
      if 1 == len(direction):
        direction = direction[0]  # stupid numpy
    except TypeError:
      pass
    min = geometry.dot(firstPoint, direction)
    max = min  # same for now
    for point in pointList[1:]:  # already done first point
      newScalar = geometry.dot(point, direction)
      try:
        if newScalar < min:
          min = newScalar
        if newScalar > max:
          max = newScalar
      except TypeError:
        newScalar = newScalar.real
        min = min.real
        max = max.real
        if newScalar < min:
          min = newScalar
        if newScalar > max:
          max = newScalar
    dimensions.append(max - min)
  return dimensions
Пример #2
0
def findLongestDimension(pointList):
  '''calls direction, returns length in that direction'''
  direction = findLongestDirection(pointList)
  #direction is a unit vector, so can do scalar projection, i.e.
  #dot product of each point with direction gives the length in that direction
  #and is negative if in the opposite direction, so just find max-min and return
  firstPoint = pointList[0]
  try:
    if 1 == len(direction):
      direction = direction[0]  # numpy/numeric return differently
  except TypeError:
    pass
  min = geometry.dot(firstPoint, direction)
  max = min  # same for now
  for point in pointList[1:]:  # already done first point
    newScalar = geometry.dot(point, direction)
    try:
      if newScalar < min:
        min = newScalar
      if newScalar > max:
        max = newScalar
    except TypeError:
      newScalar = newScalar.real
      min = min.real
      max = max.real
      if newScalar < min:
        min = newScalar
      if newScalar > max:
        max = newScalar
  return max - min
Пример #3
0
    def __init__(self, depth, maxDepth, state, player):
        self.depth = depth
        self.maxDepth = maxDepth
        self.state = state
        self.value = 0
        self.player = player
        self.direction = self.state.players[player].direction

        if (depth == maxDepth):
            self.value = self.state.players[player].pos.x

        else:
            self.propagateDown()
            if (depth % 2 == 1):
                self.value = 0
                for i in range(len(self.newState)):
                    self.value = max(self.value, self.newState[i][0].value)
            else:
                self.value = 1000000
                for i in range(len(self.newState)):
                    self.value = min(self.value, self.newState[i][0].value)

            if (self.depth == 1):
                i = 0
                for d in geometry.directions:
                    if (geometry.dot(self.state.players[player].direction, d)
                            != -1):
                        if (self.newState[i][0].value == self.value):
                            self.direction = d
                        i += 1
Пример #4
0
def processInput(screen, state):
    #Get key input
    for j in range(setup.players):
        if (state.players[j].alive and setup.humanList[j]):
            for i in range(0, len(keyDirections[0])):
                if (inputControl.keyTap[keyDirections[j][i]] and dot(
                        keyVectors[i], state.players[j].direction) == 0):
                    state.players[j].direction = keyVectors[i]

    #If a player presses space, kill off someone
    if (inputControl.keyTap[pygame.K_SPACE]):
        for j in range(setup.players):
            if (state.players[j].alive):
                killPlayer(state, j, j)
                break
Пример #5
0
def field(x, y, z, t):
    strength = 7. + .03 * np.log(1.e-6 + fract(np.sin(t) * 4373.11))
    accum = np.zeros_like(x)
    prev = np.zeros_like(x)
    tw = np.zeros_like(x)
    p = x, y, z
    for i in range(32):
        mag = dot(p, p)
        p = np.abs(p[0]) / mag, np.abs(p[1]) / mag, np.abs(p[2]) / mag
        p = p[0] - .51, p[1] - .4, p[2] - 1.3
        w = np.exp(-i / 7.)
        accum += w * np.exp(-strength * (np.abs(mag - prev)**2.3))
        tw += w
        prev = mag

    return np.maximum(0., 5. * accum / tw - .7)
Пример #6
0
    def propagateDown(self):
        self.newState = []
        currentPlayer = self.state.players[self.player]

        for i in geometry.directions:
            if geometry.dot(currentPlayer.direction, i) != -1:

                tempState = copy.deepcopy(self.state)
                newPlayer = tempState.players[self.player]
                newPlayer.direction = i
                dead = False

                for j in range(1, newPlayer.speed + 1):
                    newPos = newPlayer.pos + newPlayer.direction * j
                    if (newPos.x < 0 or newPos.x > setup.gameWidth
                            or newPos.y < 0 or newPos.y > setup.height):
                        dead = True
                    else:
                        for r in range(setup.players):
                            for line in self.state.players[r].lines:
                                end = line.direction * line.length + line.start
                                if (geometry.colinear(newPos, line.start, end)
                                        and min(line.start.x, end.x) <=
                                        newPos.x <= max(line.start.x, end.x)
                                        and max(line.start.y, end.y) <=
                                        newPos.y <= max(line.start.y, end.y)):
                                    dead = True

                if (len(newPlayer.lines) == 0 or
                        newPlayer.lines[-1].direction != newPlayer.direction):
                    l = geometry.Line(newPlayer.pos, newPlayer.direction,
                                      newPlayer.speed)
                    newPlayer.lines.append(l)
                else:
                    newPlayer.lines[-1].length += newPlayer.speed
                newPlayer.pos += newPlayer.direction * newPlayer.speed
                self.newState.append((Node(self.depth + 1, self.maxDepth,
                                           tempState, self.player), dead))
Пример #7
0
def findProjectAndSplit(pointListList, altSplit=False, overlap=0):
  '''finds the eigs, projects the points, splits into 2 sub lists. returns
  indices from orig pointListList split into 2 groups.
  altsplit true means biggest gap splitting
  altsplit false means bisective splitting (based on average)
  maybe want a 3rd option to split on median (equal subgroups).
  overlap parameter allows some points to be returned in both clusters.
  useful sometimes maybe.'''
  maxVecRet = findLongestProjectedDirection(pointListList)
  flattenedList = flatten(pointListList)
  projectedPts = []
  for pointList in flattenedList:
    projectedPt = geometry.dot(maxVecRet, pointList)
    try:
      projectedPt = projectedPt.real  # in case it is complex
    except AttributeError:
      pass  # this is okay, not a complex number
    projectedPts.append(projectedPt)
  if not altSplit:  # use bisective splitting, i.e. split based on mean
    avgPoint = geometry.getAverage1(projectedPts)
    indicesSplit = findBisectiveSplit(projectedPts, avgPoint, overlap)
  else:
    indicesSplit = findBiggestGapSplit(projectedPts, overlap)
  return indicesSplit
Пример #8
0
    def _update_area(self):
        """Based on three or more markers in total, draw a nice
        polygon and update the total area.
        """

        if len(self._markers) >= 3:
            # start from apex, then all markers to the right of the
            # pogo line, then the lm point, then all markers to the
            # left.

            p1,p2 = [self._markers[i].GetPosition()[0:2] for i in range(2)] 
            z = self._markers[0].GetPosition()[2]

            n,mag,lv = geometry.normalise_line(p1,p2) 

            # get its orthogonal vector
            no = - n[1],n[0]

            pts = [self._markers[i].GetPosition()[0:2] 
                    for i in range(2, len(self._markers))]
            right_pts = []
            left_pts = []

            for p in pts:
                v = geometry.points_to_vector(p1,p) 
                # project v onto n
                v_on_n = geometry.dot(v,n) * n
                # then use that to determine the vector orthogonal on
                # n from p
                v_ortho_n = v - v_on_n
                # rl is positive for right hemisphere, negative for
                # otherwise
                rl = geometry.dot(no, v_ortho_n)
                if rl >= 0:
                    right_pts.append(p)
                elif rl < 0:
                    left_pts.append(p)

            vpts = vtk.vtkPoints()
            vpts.InsertPoint(0,p1[0],p1[1],z)
            for i,j in enumerate(right_pts):
                vpts.InsertPoint(i+1,j[0],j[1],z)

            if len(right_pts) == 0:
                i = -1 

            vpts.InsertPoint(i+2,p2[0],p2[1],z)

            for k,j in enumerate(left_pts):
                vpts.InsertPoint(i+3+k,j[0],j[1],z)

            num_points = 2 + len(left_pts) + len(right_pts)
            assert(vpts.GetNumberOfPoints() == num_points)

            self._area_polydata.SetPoints(vpts)

            cells = vtk.vtkCellArray()
            # we repeat the first point
            cells.InsertNextCell(num_points + 1) 
            for i in range(num_points):
                cells.InsertCellPoint(i)

            cells.InsertCellPoint(0)

            self._area_polydata.SetLines(cells)

            # now calculate the polygon area according to:
            # http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
            all_pts = [p1] + right_pts + [p2] + left_pts + [p1]

            tot = 0
            for i in range(len(all_pts)-1):
                pi = all_pts[i]
                pip = all_pts[i+1]
                tot += pi[0]*pip[1] - pip[0]*pi[1]

            area = - tot / 2.0

            # store area in current measurement
            self._current_measurement.area = area

            self._view_frame.area_txt.SetValue('%.2f' % (area,))