예제 #1
0
    def generateRandomParametricString(self, minLength = MIN_GENERATED_STRING_LENGTH, maxLength = MAX_GENERATED_STRING_LENGTH, predecessor_module = None):
        """
        Generates a random string composed of ParametricModules.
        May also randomly open and close branches.
        May also randomly add new defines.
        Forces the string's validity.

        If a predecessor_module is passed, its parameter variables (x,y) need to appear in the generated string
        We add them to the current generated string at the end.
        """
        if self.branchProbability > 0.0: openBranches = 0
        n_modules = self.rnd.randint(minLength,maxLength)
        pstring = ParametricString()
        pstring.setGlobals(self.lsystem.globalDefines)

        for i in range(n_modules):  # We add N modules

            # Generate the module
            new_module = self.generateWeightedRandomModule()#parametric=True)

            # Randomly open branches (NOT: only if the module is a new rotation, otherwise it doesn't make sense)
            if self.branchProbability > 0.0:
                if openBranches > 0 and self.rnd.random() < self.branchCloseProbability:
                    pstring.appendCloseBranch()
                    openBranches -= 1
                if self.rnd.random() < self.branchProbability:   #new_module.isOrientation() and
                    pstring.appendOpenBranch()
                    openBranches += 1

            # Append the module
            pstring.appendModule(new_module)

        # Close the remaining branches
        if self.branchProbability > 0.0:
            for i in range(openBranches): pstring.appendCloseBranch()

        # Change some of the parameters to the predecessor module's variable parameters
        if self.parameterized:
            print("PRED: " + str(predecessor_module))
            if predecessor_module is not None:
                potential_changed_modules = pstring.modulesList
                potential_changed_modules = list(filter(lambda p: not p.isBracket(), pstring.modulesList))

                #print "Potential changed modules: "
                #for m in potential_changed_modules: print m

                # Each predecessor parameter is copied once, if possible
                for param in predecessor_module.params:
                    chosen_module = self.getRandomItemFromList(potential_changed_modules)
                    #if len(chosen_module.params) > 0:
                    index = self.rnd.randint(0,len(chosen_module.params)-1)
                    chosen_module.params[index] = param

        return pstring
 def parseSuccessorString(self,string):
     successor = ParametricString()
     successor.setGlobals(self.globalDefines)
     successor.parseString(string)
     return successor
 def parsePredecessorString(self,string):
     predecessorPString = ParametricString()
     predecessorPString.setGlobals(self.globalDefines)
     predecessorPString.parseString(string)    # TODO: Maybe this predecessor should be treated as a 1-module string for consistency, instead of a module?
     assert len(predecessorPString) == 1, 'The predecessor must contain only one module!'
     return predecessorPString[0]   # Only one
예제 #4
0
 def generateEmptyParametricString(self):
     pstring = ParametricString()
     pstring.setGlobals(self.lsystem.globalDefines)
     return pstring