def externalEnergy(self, controlpoints): """ Returns the external energy of the snake. The external energy is computed via the provided ExternalEnergy subclass object. The external energy is the sum of the external energies at each control point which get multiplied by the inverse of the number of control points. """ # compute the factor the energy of each control points get's weighed with external = 0.0 if len(self.controlpoints) > 0: factor = float(1)/len(self.controlpoints) else: factor = 1 # check if the given controlpoints are equal to the current ones if np.equal(controlpoints, self.controlpoints).all(): # take the current normals normals = self.normals else: # otherwise calculate the according normals spline = Spline() spline.addControlPoints(*controlpoints) normals = spline.normals # ACHTUNG! hier müssen die Normalen zur Berechnung gedreht werden, # falls flip es vorgibt if self.flip: normals = map(lambda n: rotateVector(n, angle=pi), normals) # only remember each external control point energy if the given control points are # the snakes current control points memorize_energies = np.equal(controlpoints, self.controlpoints).all() # reset the controlpointenergy list if necessary if memorize_energies: self.ext_energies = [] # sum up the energies at the single control points multiplied by the inverse # of the number of control points for i in range(len(controlpoints)): point = controlpoints[i] # if len(normals) > 0: # normal = normals[i] # else: # normal = None normal = normals[i] pointenergy = self.ExternalEnergy.getEnergy(point, iteration=self.iteration, normal=normal) # check wether to save the point energy if memorize_energies: #self.ext_energies.append(self.ExternalEnergy.getEnergy(point)) self.ext_energies.append(pointenergy) external += pointenergy * factor return external
def update(self): """ Recomputes energies and the contour, e.g. after a control point has been added. """ # compute the contour and normals spline = Spline() spline.addControlPoints(*self.controlpoints) lencontrolpoints = len(self.controlpoints) self.contour = spline.interpolation self.normals = spline.normals # the following is not needed anymore #if self.flip: # self.normals = map(lambda n: rotateVector(n, angle=pi), self.normals) # compute the energies self.spacing = self.spacingEnergy(self.controlpoints) self.curvature = self.curvatureEnergy(self.controlpoints) self.external = self.externalEnergy(self.controlpoints) self.energy = self.totalEnergy(self.controlpoints) # compute the contour gradient for displaying the curvature energies self.contour_gradient = contourCurvatureGradient(self.contour, self.controlpoints, self.crv_energies) # check for saneness if lencontrolpoints == 1 or lencontrolpoints == 0: assert self.contour == [] assert self.spacing == 0.0 assert self.curvature == 0.0 elif lencontrolpoints == 0: assert self.external == 0.0 assert len(self.normals) == [] elif lencontrolpoints > 1: assert len(self.contour) > 0 assert lencontrolpoints == len(self.normals) assert lencontrolpoints == len(self.ext_energies), '%s != %s' % (len(self.controlpoints), len(self.ext_energies))