Grammar.h
1 /************************************************************
2 * (C) Voxel Farm Inc. 2015
3 */
4 
5 #pragma once
6 
7 #include <map>
8 #include <vector>
9 #include <set>
10 #include <string>
11 #include <iterator>
12 
13 #include "Vector.h"
14 #include "MatrixAlg.h"
15 #include "RTree.h"
16 #include "WhiteNoise.h"
17 
18 namespace VoxelFarm
19 {
20  namespace Architecture
21  {
24  {
25  ACTION_VAR,
26  ACTION_SNAP,
27  ACTION_MOVE,
28  ACTION_ROTATE,
29  ACTION_SCALE,
30  ACTION_CENTER_PIVOT,
31  ACTION_RESET_PIVOT,
32  ACTION_ALIGN,
33  ACTION_ROTATEX,
34  ACTION_ROTATEY,
35  ACTION_ROTATEZ,
36  ACTION_OCCLUDE,
37  ACTION_LOCAL,
38  ACTION_BOX,
39  ACTION_NGON,
40  ACTION_LOFT_BOX,
41  ACTION_LOFT_NGON,
42  ACTION_PRISM,
43  ACTION_ROOF,
44  ACTION_CALL,
45  ACTION_DEFER,
46  ACTION_SELECT,
47  ACTION_INSTANCE,
48  ACTION_DIVIDE,
49  ACTION_REPEAT,
50  ACTION_PUSH,
51  ACTION_POP,
52  ACTION_MATERIAL
53  };
54 
56  enum NodeType
57  {
58  NODE_ROOT,
59  NODE_NULL,
60  NODE_VOLUME,
61  NODE_FACE,
62  NODE_EDGE,
63  NODE_POINT
64  };
65 
67  enum ScopeType
68  {
69  SCOPE_BOX,
70  SCOPE_PRISM,
71  SCOPE_PRISM_LEFT,
72  SCOPE_PRISM_RIGHT
73  };
74 
76  class CDebugInfo
77  {
78  public:
79  String filename;
80  int line;
81  int column;
82  CDebugInfo() : filename(""), line(-1), column(-1) {}
83  };
84 
85  enum DebugAction
86  {
87  DEBUG_RUN,
88  DEBUG_STOP,
89  DEBUG_STEP,
90  DEBUG_INSTANCE
91  };
92 
93  class CEvaluator;
94  class IDebugger
95  {
96  public:
97  virtual DebugAction debug(const char* filename, int line, int column, CEvaluator* evaluator) = 0;
98  };
99 
101  class CAction
102  {
103  public:
107  TMap<String, String> attributes;
110  };
111 
113  class CScope
114  {
115  public:
116  CScope() :
117  type(SCOPE_BOX),
118  x(0), y(0), z(0),
119  sx(0), sy(0), sz(0),
120  ax(0), ay(0), az(0),
121  dx(0), dy(0), dz(0),
122  px(0), py(0), pz(0),
123  xaligned(false),
124  yaligned(false),
125  zaligned(false) {}
126  public:
128  float x;
130  float y;
132  float z;
134  float dx;
136  float dy;
138  float dz;
140  float px;
142  float py;
144  float pz;
146  float sx;
148  float sy;
150  float sz;
152  float ax;
154  float ay;
156  float az;
158  bool xaligned;
160  bool yaligned;
162  bool zaligned;
165  };
166 
169  {
170  public:
171  CBoxVolume() : active(false) {}
173  bool active;
174  };
175 
178  {
179  public:
180  CNGonVolume() : sides(0), radius(0), active(false) {}
182  int sides;
184  float radius;
186  bool active;
187  };
188 
191  {
192  public:
193  CBoxLoftVolume() : active(false) {}
195  bool active;
197  String positions;
199  String widths;
201  String depths;
202  };
203 
206  {
207  public:
208  CNGonLoftVolume() : active(false), sides(0) {}
210  bool active;
212  int sides;
214  String positions;
216  String radii;
217  };
218 
220  class CNode
221  {
222  public:
223  CNode() : parent(NULL), generation(0), occludes(false), matrixCacheValid(false), hasLocalNames(false), type(0), cachedMatrix() {}
224  ~CNode();
226  int type;
230  TVector<CNode*> children;
232  TVector<std::pair<CNode*, String>> recursePending;
234  TVector<std::pair<CNode*, String>> deferred;
236  TMap<String, String> variables;
238  TVector<CAction> actions;
252  String module;
254  String material;
256  bool occludes;
258  TMap<String, String> localNames;
265  public:
267  void newGeneration(CNode* child, String module);
269  void defer(CNode* child, String module);
270  };
271 
272  class CModule;
273 
275  class CRule
276  {
277  public:
279  CRule& var(String identifier, String value, bool constant);
281  CRule& snap(String dir, String id, String radius);
283  CRule& condition(String exp);
285  CRule& scale(String x, String y, String z);
287  CRule& move(String x, String y, String z);
289  CRule& rotate_x(String angle);
291  CRule& rotate_y(String angle);
293  CRule& rotate_z(String angle);
295  CRule& align(String dir);
297  CRule& center();
299  CRule& center_x();
301  CRule& center_y();
303  CRule& center_z();
305  CRule& center_xy();
307  CRule& center_xz();
309  CRule& center_yz();
310 
312  CRule& box();
314  CRule& ngon(String sides);
316  CRule& prism(String type);
318  CRule& loft_box(String positions, String widths, String depths);
320  CRule& loft_ngon(String sides, String positions, String radii);
321 
323  CRule& module(String name, String generations = "-1");
325  CRule& defer(String name);
327  CRule& instance(String geometry);
329  CRule& material(String id);
330 
332  CRule& select(String stype, String module);
333 
335  CRule& if_occluded_rule(String scope, String volumeId, String margin);
336 
338  CRule& occludes(String volumeid);
339 
341  CRule& local(String volumeid);
342 
344  CRule& subdivide_x(String pattern, String modules);
346  CRule& subdivide_y(String pattern, String modules);
348  CRule& subdivide_z(String pattern, String modules);
350  CRule& subdivide_prism(String position, String center, String top, String left, String right);
352  CRule& repeat_x(String length, String module, String flex = "flex", String snap = "");
354  CRule& repeat_y(String length, String module, String flex = "flex", String snap = "");
356  CRule& repeat_z(String length, String module, String flex = "flex", String snap = "");
357 
359  CRule& parameter(String name, String value, String module, String flex = "flex");
360 
362  CRule& push();
364  CRule& pop();
365 
367  CModule& end();
368  public:
374  TVector<CAction> actions;
376  TVector<String> conditionals;
378  TVector<String> occlusionVolumeIds;
380  TVector<String> occlusionMargins;
382  TVector<String> occlusionScopes;
383  };
384 
386  class CModule
387  {
388  public:
390  CModule& axiom(String name);
392  CModule& add(CRule& rule);
394  CRule& begin(String name);
395  public:
397  TMap<String, TVector<CRule>> rules;
399  String baseAxiom;
400  public:
402  void saveToFile(const char* filename);
404  void loadFromFile(const char* filename, char* debugfile = NULL);
405  };
406 
408  class CGrammar
409  {
410  public:
412  CModule& define(String name);
413  public:
415  TMap<String, CModule> modules;
416  };
417 
419  typedef void CreateInstance(
421  String id,
423  Algebra::Matrix& matrix,
425  Algebra::Vector& size,
427  ScopeType scopeType,
429  String material);
430 
433  {
434  public:
436  virtual void createInstance(
438  String id,
440  Algebra::Matrix& matrix,
442  Algebra::Vector& size,
444  ScopeType scopeType,
446  String material) = 0;
447  };
448 
450  class SnapPlane
451  {
452  public:
453  Algebra::Vector s1p1;
454  Algebra::Vector s1p2;
455  Algebra::Vector s2p1;
456  Algebra::Vector s2p2;
457  };
458 
461 
464  {
465  public:
466  CEvaluator();
469  public:
471  void run(
473  CGrammar& grammar,
475  String module,
479  int maxgeneration = -1);
480  public:
490  float eval(
492  String exp,
494  float total = -1.0f,
496  CNode* startNode = NULL);
498  void recurse(String rule);
500  bool evaluatePending(CNode* node, int maxgeneration = -1);
502  bool evaluateDeferred(CNode* node, int maxgeneration = -1);
503 
505  void findSnapPoints(
507  String& id,
509  Algebra::Vector segments[4][2],
511  TVector<float>& points);
513  bool isOccluded(
515  CNode* node,
517  String volumeId,
519  bool partial);
520  public:
522  TMap<String, float> parameters;
524  TMap<String, TVector<SnapPlane>> snapPlanes;
530  TMap<String, OccluderIndex*> occluders;
532  int guid;
534  double rootOffsetX;
535  double rootOffsetY;
536  double rootOffsetZ;
537  public:
538  DebugAction debugAction;
539  };
540 
541  void updateScope(CNode* node, VoxelFarm::Algebra::Matrix* matrix, bool useCache = false);
542  };
543 
544 }
String widths
Contains the list of widths for the loft.
Definition: Grammar.h:199
CRule & center_xy()
Moves the scope's pivot to the scope's geometric center along the XY plane.
float dx
Scope translation along X axis.
Definition: Grammar.h:134
TMap< String, String > variables
Variables defined for the node and children.
Definition: Grammar.h:236
float dy
Scope translation along Y axis.
Definition: Grammar.h:136
CreateInstance * instancer
Pointer to the create instance callback.
Definition: Grammar.h:526
String positions
A list of positions for the loft.
Definition: Grammar.h:214
float y
Scope origin along Y axis.
Definition: Grammar.h:130
CRule & if_occluded_rule(String scope, String volumeId, String margin)
Adds occlusion test.
void findSnapPoints(String &id, Algebra::Vector segments[4][2], TVector< float > &points)
Given four segments in a scope, this function return the intersection points between the segments and...
Algebra::Matrix cachedMatrix
To speed up processing, the transformation matrix can be cached at some point.
Definition: Grammar.h:262
CRule & align(String dir)
Aligns the scope along the specified axis.
CRule & var(String identifier, String value, bool constant)
Defines a variable.
Defines a box loft primitive.
Definition: Grammar.h:190
CRule & loft_ngon(String sides, String positions, String radii)
Defines a ngon loft primitive.
TMap< String, String > attributes
A list of parameters for the action.
Definition: Grammar.h:107
CRule & subdivide_prism(String position, String center, String top, String left, String right)
Divide a prism scope.
CRule & snap(String dir, String id, String radius)
Defines a snap plane.
int sides
Number of sides in the ngon loft.
Definition: Grammar.h:212
CBoxLoftVolume boxLoft
Box loft primitive for the node.
Definition: Grammar.h:246
Contains all classes and functions for the VoxelFarm engine.
String radii
A list of radii for the loft.
Definition: Grammar.h:216
CRule & ngon(String sides)
Defines a ngon primitive.
Defines a box primitive.
Definition: Grammar.h:168
CGrammar * currentGrammar
Current grammar being evaluated.
Definition: Grammar.h:486
Defines a node in the program tree.
Definition: Grammar.h:220
bool isOccluded(CNode *node, String volumeId, bool partial)
Tests whether the provided node is occluded.
bool active
Flags whether the primitive is active or not.
Definition: Grammar.h:210
CModule * currentModule
Current module being evaluated.
Definition: Grammar.h:488
CDebugInfo debugInfo
Debug information for the action.
Definition: Grammar.h:109
A 3D Vector.
Definition: Vector.h:34
This object can be used to evaluate a grammar.
Definition: Grammar.h:463
Defines a program.
Definition: Grammar.h:386
CRule & rotate_x(String angle)
Defines a rotation over the X axis.
virtual void createInstance(String id, Algebra::Matrix &matrix, Algebra::Vector &size, ScopeType scopeType, String material)=0
Requests the creation of an instance. The IInstanceCreator interface provides an alternative to this ...
CNode * parent
Pointer to parent node.
Definition: Grammar.h:228
String depths
Contains the list of depths for the loft.
Definition: Grammar.h:201
float pz
Scope pivot along Z axis.
Definition: Grammar.h:144
void saveToFile(const char *filename)
Saves the program to a file name.
TMap< String, TVector< CRule > > rules
Maps rules to identifiers. A single identifier can be shared by many rules.
Definition: Grammar.h:397
TVector< String > conditionals
List of conditional statements for the rule.
Definition: Grammar.h:376
int generation
Counts the tree generation for the node.
Definition: Grammar.h:250
CNGonVolume ngon
Ngon primitive for the node.
Definition: Grammar.h:244
TVector< CNode * > children
List of children nodes.
Definition: Grammar.h:230
CRule & material(String id)
Set the node's material.
TVector< String > occlusionMargins
List of occluding margins for occlusion tests.
Definition: Grammar.h:380
void recurse(String rule)
Evaluates the specified rule and then any other rules it may spawn.
String baseAxiom
Stores the base axiom.
Definition: Grammar.h:399
CRule & repeat_z(String length, String module, String flex="flex", String snap="")
Repeat module along Z axis.
NodeType
Enumerates node types for a grammar program.
Definition: Grammar.h:56
CRule & begin(String name)
Starts the definition of a new rule object.
TVector< std::pair< CNode *, String > > recursePending
List of child nodes that still require processing.
Definition: Grammar.h:232
CRule & center_z()
Moves the scope's pivot to the scope's geometric center along the Y axis.
bool yaligned
Flags whether the scope is aligned along the Y axis.
Definition: Grammar.h:160
CRule & move(String x, String y, String z)
Defines a translation statement.
CNode root
Root node for the evaluation tree.
Definition: Grammar.h:468
TMap< String, TVector< SnapPlane > > snapPlanes
Stores snap planes according to their IDs.
Definition: Grammar.h:524
void CreateInstance(String id, Algebra::Matrix &matrix, Algebra::Vector &size, ScopeType scopeType, String material)
A callback to request the creation of an instance. The IInstanceCreator interface provides an alterna...
Definition: Grammar.h:419
float z
Scope origin along Z axis.
Definition: Grammar.h:132
bool evaluatePending(CNode *node, int maxgeneration=-1)
Evaluate any pending rules.
int type
Node type (see ActionType)
Definition: Grammar.h:226
float sy
Scope size along Y axis.
Definition: Grammar.h:148
CRule & rotate_y(String angle)
Defines a rotation over the Y axis.
CRule & occludes(String volumeid)
Declares the current scope may occlude other scopes.
float sx
Scope size along X axis.
Definition: Grammar.h:146
CRule & select(String stype, String module)
Select a primitive feature and invoke module within it.
Defines a ngon loft primitive.
Definition: Grammar.h:205
CRule & instance(String geometry)
Instances a geometry mesh.
CNode * context
Pointer to the node where the rule is being exectuted.
Definition: Grammar.h:372
IInstanceCreator * instanceCreator
Pointer to the create instance interface.
Definition: Grammar.h:528
TMap< String, float > parameters
A set of parameters supplied to the evaluator.
Definition: Grammar.h:522
bool xaligned
Flags whether the scope is aligned along the X axis.
Definition: Grammar.h:158
bool active
Flags whether the primitive is active or not.
Definition: Grammar.h:186
bool matrixCacheValid
Indicates whether the cache transformation maxtrix is still valid.
Definition: Grammar.h:264
CModule & axiom(String name)
Declares the starting rule object.
ActionType
Enumerates instructions for the grammar virtual machine.
Definition: Grammar.h:23
IDebugger * debugger
Interface to a debugger.
Definition: Grammar.h:482
CRule & rotate_z(String angle)
Defines a rotation over the Z axis.
TVector< std::pair< CNode *, String > > deferred
List of child nodes that requested deferred processing.
Definition: Grammar.h:234
Contains four co-planar 3D points that define a snap plane.
Definition: Grammar.h:450
String material
Stores the material ID set for the node.
Definition: Grammar.h:254
CModule & define(String name)
Starts the definition of a program.
Defines a scope object.
Definition: Grammar.h:113
CRule & push()
Pushes the current state into the stack.
ActionType type
Action type.
Definition: Grammar.h:105
TMap< String, CModule > modules
A grammar contains several module programs. Each program is identified by a String.
Definition: Grammar.h:415
ScopeType type
Scope type (whether it is a box or a prism, etc)
Definition: Grammar.h:164
CRule & center_yz()
Moves the scope's pivot to the scope's geometric center along the YZ plane.
float ax
Scope rotation angle over X axis.
Definition: Grammar.h:152
RTree< CNode *, float, 3, float, 20 > OccluderIndex
RTree to accelerate occlussion test.
Definition: Grammar.h:460
bool evaluateDeferred(CNode *node, int maxgeneration=-1)
Evaluate any rules that may have been deferred.
void defer(CNode *child, String module)
Defers a new execution subtree generation from this child, applying the specified grammar module...
CModule & end()
Used for fluent inteface decalaration. Ends a series of rule declarations and returns a reference to ...
void newGeneration(CNode *child, String module)
Starts a new execution subtree generation from this child, applying the specified grammar module...
CRule & center()
Moves the scope's pivot to the scope's geometric center.
int sides
Sides in the ngon.
Definition: Grammar.h:182
TVector< String > occlusionScopes
List of occluding modes for occlusion tests (partial, full, front, back)
Definition: Grammar.h:382
String module
Stores the ID of the grammar module that originated the node.
Definition: Grammar.h:252
TVector< String > occlusionVolumeIds
List of occluding IDs for occlusion tests.
Definition: Grammar.h:378
CRule & repeat_x(String length, String module, String flex="flex", String snap="")
Repeat module along X axis.
A Matrix for 3D operations.
Definition: MatrixAlg.h:38
bool active
Flags whether the primitive is active or not.
Definition: Grammar.h:173
Encapsulates a series of actions to be performed over a node. This is equivalent to a "module" statem...
Definition: Grammar.h:275
float px
Scope pivot along X axis.
Definition: Grammar.h:140
CRule & subdivide_z(String pattern, String modules)
Divide the current scope along the Z axis.
CRule & subdivide_x(String pattern, String modules)
Divide the current scope along the X axis.
CRule & parameter(String name, String value, String module, String flex="flex")
Defines a parameter.
This interfaces allows the grammar evaluator to delegate creation of instances.
Definition: Grammar.h:432
CRule & loft_box(String positions, String widths, String depths)
Defines a box loft primitive.
bool occludes
Indicate whether this module may occlude other modules.
Definition: Grammar.h:256
CRule & center_xz()
Moves the scope's pivot to the scope's geometric center along the XZ plane.
TMap< String, String > localNames
Contain a list of aliases for occlusion and snap identifiers so they can be local to a portion of the...
Definition: Grammar.h:258
void run(CGrammar &grammar, String module, CreateInstance *instancer, int maxgeneration=-1)
Runs a grammar program.
bool zaligned
Flags whether the scope is aligned along the Z axis.
Definition: Grammar.h:162
TVector< CAction > actions
List of actions to be performed by the node.
Definition: Grammar.h:238
float az
Scope rotation angle over Z axis.
Definition: Grammar.h:156
CRule & module(String name, String generations="-1")
Invokes a module.
CBoxVolume box
Box primitive for the node.
Definition: Grammar.h:242
CRule & pop()
Pops the current state into the stack.
CRule & local(String volumeid)
Creates an alias for the specified Id so queries can be localized from this point on...
Defines a ngon primitive.
Definition: Grammar.h:177
float eval(String exp, float total=-1.0f, CNode *startNode=NULL)
Evaluates an expresion.
CRule & subdivide_y(String pattern, String modules)
Divide the current scope along the Y axis.
A grammar is a collection of programs.
Definition: Grammar.h:408
CRule & box()
Defines a box primitive.
CRule & center_y()
Moves the scope's pivot to the scope's geometric center along the Y axis.
CRule & prism(String type)
Defines a prism primitive.
float dz
Scope translation along Z axis.
Definition: Grammar.h:138
CNode * currentNode
Current evaluation node.
Definition: Grammar.h:484
double rootOffsetX
Position of the root node in world coordinates. It is used for the rnd command.
Definition: Grammar.h:534
ScopeType
Enumerates possible scope types.
Definition: Grammar.h:67
CModule & add(CRule &rule)
Adds a rule.
float ay
Scope rotation angle over Y axis.
Definition: Grammar.h:154
CRule & condition(String exp)
Defines a conditional.
void loadFromFile(const char *filename, char *debugfile=NULL)
Loads a program from a file name.
int guid
Keeps an ever-incrementing counter used to generate unique aliases for local features.
Definition: Grammar.h:532
TMap< String, OccluderIndex * > occluders
Store occludes according to their IDs.
Definition: Grammar.h:530
float sz
Scope size along Z axis.
Definition: Grammar.h:150
CRule & scale(String x, String y, String z)
Defines a scale statement.
CNGonLoftVolume ngonLoft
Ngon loft primitive for the node.
Definition: Grammar.h:248
CScope scope
Scope for the node.
Definition: Grammar.h:240
CRule & defer(String name)
Deferred invocation for a module.
TVector< CAction > actions
List of actions for the rule.
Definition: Grammar.h:374
float x
Scope origin along X axis.
Definition: Grammar.h:128
String positions
Contains the list of positions for the loft.
Definition: Grammar.h:197
CModule * parent
Pointer to the module that owns the rule.
Definition: Grammar.h:370
CRule & center_x()
Moves the scope's pivot to the scope's geometric center along the X axis.
bool hasLocalNames
Indicates whether the node has local aliases.
Definition: Grammar.h:260
bool active
Flags whether the primitive is active or not.
Definition: Grammar.h:195
float py
Scope pivot along Y axis.
Definition: Grammar.h:142
CRule & repeat_y(String length, String module, String flex="flex", String snap="")
Repeat module along Y axis.
Defines a single action and its parameters.
Definition: Grammar.h:101