// This file is a part of Framsticks SDK. http://www.framsticks.com/ // Copyright (C) 1999-2018 Maciej Komosinski and Szymon Ulatowski. // See LICENSE.txt for details. #ifndef _FL_GENERAL_ #define _FL_GENERAL_ #include #include #include #include #include #include #include #include "fL_matheval.h" /// determines to which class fL element belongs enum fLElementType { TERM, ///< word of L-System INFO, ///< fL_Builder object RULE, ///< rule of L-System BRANCH ///< special type of fL word, which represents branch }; /** @name Constants used in fL methods */ //@{ #define FL_PART_PROPS_COUNT 4 /// parevals; ///begin = begin; this->end = end; bodyelementpointer = NULL; } /** * Destroys data ParamObject and MathEvaluation objects. ParamTab for Word * should be deleted separately, because all words with same name share same * pointer to ParamTab. */ ~fL_Word() { if (data) ParamObject::freeObject(data); for (MathEvaluation *ev : parevals) { if (ev) delete ev; } parevals.clear(); } /** * Checks if there is no other word stored in the builder with the same name, * prepares ParamTab for defined word and adds created word definition to the * builder. * @param builder pointer to the builder that process this word definition * @return 0 if word is added, 1 if word is redefined */ int processDefinition(fL_Builder *builder); /** * Operator copies name string, npar value, pointer to ParamTab and pointers * to MathEvaluation objects for parameters. It sets data of this word to * NULL. * @param src source word */ void operator=(const fL_Word& src); /** * Returns 'w:' line defining word in genotype. * @return string representation of word definition */ virtual SString toString(); /** * Returns token version of this word. It is used during converting successor's * list or axiom list into strings. Flag 'keepformulas' should be set to true * if word parameters should be represented as formulas, and set to false if * word parameters should contain the result of evaluation. * @param keepformulas true if parameters should have mathematical formula, false if it should have result of evaluation */ virtual SString stringify(bool keepformulas = true); /** * Saves formulas or evaluations of parameters in ParamObject. * @param keepformulas true if parameters should have mathematical formula, false if it should have result of evaluation * @return 0 if method successfully managed to save data, 1 if an error occured */ int saveEvals(bool keepformulas); /** * Computes distance between two L-System words in genotype. Distance has sense * only when two words have the same name. Otherwise, returned value equals -1. * The distance is computed as Euclidean distance between words arguments. * @param right the second word * @return Euclidean distance between words in genotype or -1 if words have different name */ double distance(fL_Word *right); }; /** * Represents special branching word in L-System genotype. */ class fL_Branch : public fL_Word { public: /// Determines if branching parenthesis is opened or closed enum BranchType { OPEN, CLOSE }; BranchType btype; ///< branch parenthesis type fL_Branch(BranchType branchtype, int begin, int end) : fL_Word(true, begin, end) { type = fLElementType::BRANCH; btype = branchtype; name = stringify(); } SString toString() { return ""; }; SString stringify(bool keepformulas = false) { return btype == BranchType::OPEN ? "[" : "]"; } }; /** * Represents rules in L-System genotype. Every rule has predecessor and successors. * The optional argument is a condition of deploying rule. Every rule is ran * synchronously for every letter of the current genotype. If condition and * predecessor are satisfied, then predecessor is replaced by successors. */ class fL_Rule : public fL_Element { public: SString predecessor; /// objsucc; ///begin = begin; this->end = end; } ~fL_Rule() { if (objpred) delete objpred; if (condeval) delete condeval; for (fL_Word *word : objsucc) { delete word; } objsucc.clear(); } /** * Loads rule definition from the genotype, checks if word defined in * predecessor does exist, tokenizes successor and condition. * @param builder pointer to the builder that process this word definition * @return 0 if word is added, 1 if an error occured during parsing */ int processDefinition(fL_Builder *builder); SString toString(); /** * Runs rule for a given word, if all conditions and starting rules are satisfied. * Method alters given list of tokens by removing matching word pointed by * pointer and iterator of list and inserting successor sequence into list. * Final iterator value points to the first word after processed word. * @param builder builder containing current genotype * @param in word that is currently processed * @param it iterator of genotype list which determines position of current analysis * @param currtime value representing continuous time value for rule * @return 0 if rule processed current word, 1 if rule is no applicable */ int deploy(fL_Builder *builder, fL_Word *in, std::list::iterator &it, double currtime); }; /** * Structure for holding current Turtle state. */ struct fL_State { Pt3D direction; /// genotype; /// words; /// wordnames; std::vector rules; /// &result, int numparams, int begin, int end); /** * Uses rules to process current genotype and make next iteration step for * L-System development. * @param currtime current time of genotype processing * @return always 0 */ int iterate(double currtime); /** * Alters only parameters that depend on time. Should be used if only fraction * of iteration has changed, not integer part. * @param currtime current time of processing * @return always 0 */ int alterTimedProperties(double currtime); /** * Developes L-System from given genotype and builds Framsticks Model from it. * When using_checkpoints is enabled, method generates checkpoint for each * step defined in timestamp. * @param neededtime reference to a time value after stopping development (usually it will be equal to time specified in the time field, unless the number of allowed words will be exceeded earlier) * @return final model from a fL genotype */ Model* developModel(double &neededtime); /** * Creates new checkpoint for a given model based on current state of genotype. * @param model reference to model * @return 0 if developing went successfully, 1 otherwise */ int buildModelFromSequence(Model *model); /** * Returns stringified product of L-System development. * @return stringified words of developed L-System */ SString getStringifiedProducts(); /** * Returns genotype stored by objects in fL_Builder. * @return genotype converted from L-System objects */ SString toString(); /** * Alters part properties according to informations stored in stickword. * Method takes current values of part's properties computed from previous * calls of the alterPartProperties and computes mean according to upcoming * values of properties. * @param part analysed part * @param stickword the L-System word that affects current part * @param alterationcount the number of times of part modifications - used to compute mean of every word properties * @return always 0 */ int alterPartProperties(Part *part, fL_Word *stickword, double &alterationcount); /** * Finds input neuron that is nearest to attractor or connection definition. * When attractor object is given, then the word with name matching attractor and * with nearest values of parameters is chosen as the point, from which input * neuron is looked for. The searching goes both sides. * @param currneu object storing informations about connection word iterator and current neuron * @param attractor pointer to an attractor definition presented in connection word * @return pointer to the input neuron, or NULL if no neuron could be found */ Neuro *findInputNeuron(std::pair::iterator, Neuro *> currneu, fL_Word *attractor); /** * Removes joints, parts and neurons with its connections from current model, without * removing checkpoint data. * @param m model to clear */ void clearModelElements(Model *m); /** * Converts parameters defined in built-in words into desired * range with use of sigmoid function, which ensures staying * within min and max value. * @param input the value from evaluation of parameter * @param min minimal value of property * @param max maximal value of property * @return value of body element property */ double sigmoidTransform(double input, double min, double max); /** * Counts words defined in the genotype. * @return number of defined words */ int countDefinedWords(); /** * Counts number of sticks in a given sequence * @return number of sticks in sequence */ int countSticksInSequence(std::list sequence); /** * Counts all definitions of words, all words in axiom and rule successors in a genotype. * Used for computing change between original genotype and mutation. * @return number of words in definitions, axiom and rule successors */ int countWordsInLSystem(); /** * Sorts rules and removes rules that will not be used. The "sorting" is * done in such way that all rules with conditions are first, and rules * without conditions go to the end. If there are more than one rule * for the same word with no condition or same condition, than the * second one is removed. */ void removeRedundantRules(); }; #endif // _FL_GENERAL_