Lesson 4: Write embedding expressions

Lesson 3

 

In this lesson you will learn how to manipulate the embeddings of objects with embedding expressions just like the ones you modified in Lesson 3. You will be using the same modeler for witch you will create new rules for the modeler with the editor, export them to the project and use the corresponding operations through the viewer.

Every embedding that you wish to include must be declared in the editor by clicking on “New Embedding”, giving it a name, an associated orbit and a type (which is either an existing java class or one that you can write yourself in the embeddings package of the project).

OBSERVING EMBEDDINGS

  • open the editor and right click on the points embedding on the left of the screen under the embeddings label then click on modify (see Screen 4.1)

You can find out with which orbit it is associated and what it’s type is (defined by a class). Once you know what the type is you can find out which methods are contained in the class and thus how you can manipulate that particular embedding.

Sorry, you'll ahve to use your imagination
Screen 4.1: point embedding information

 

  • now open your IDE and open the Points.java embedding to find out what methods are at your disposal, observe the copy constructors (see Screen 4.2)
Screen 4.2: « Points » embedding class*

 

ACCESSING AN EMBEDDING

You will write a rule which translates an object and changes only the point embedding of each vertex so as to place the object twenty points higher than it’s original position.

  • open the editor and create a new rule called Translate
  • place a node in the left pattern with the orbit to match the whole connected component (i.e. <α0,α1,α2,α3>) and make it a hook

To write an orbit to a node simply select the node and press the number key corresponding to the link you wish to enter or enter the numbers into the « Orbit: » text field at the top and hit Enter.

  • place a node in the right pattern identical to the one in the left pattern (with the same orbit) (see Screen 4.3)
Screen 4.3: Translate

You will notice that the node in the left pattern is called n0 and the one  in the right pattern is called n0 as well because it corresponds to n0 in the left pattern and it hasn’t been changed which means that the sub-graph matched by the left pattern will be preserved by the rule.

n0.point will return the point embedding of the current dart matched by n0.
  • write the point expression for n0 (see Screen 4.4)
    • use the getX(), getY() and getZ() methods from the Points class to get the values of attributes x, y and z from the point embedding of n0
    • return and instance of Points created using the public Points(float x, float y, float z) constructor and adding 20 to y to translate the point along the y axis
    • click on « Apply »
Screen 4.4: point embedding expression
  • Save and export the modeler and try out your new operation on any object after having closed and launched the viewer

If you would like your rule to change the color of the moved object you can observe the color embedding expression of MakeRed to give every face a new color. You just need to copy the expression into the color expression of the  node in the right pattern of your « Translate » rule to obtain a translated red connected component.

 

ACCESSING NEIGHBORING EMBEDDINGS

You will now write a rule which gives a color to a face which is the median color calculated from the color of the face and that of the neighboring face. You will need to access the neighboring color embedding to do so.

  • open the editor and create a new rule called MedianColor1
  • place a node in the left pattern with the dart orbit (i.e. <>) and make it a hook
  • place the same node in the right pattern with the same orbit (see Screen 4.6)
Screen 4.5: MedianColor1
  • edit the color embedding of n0 in the right pattern (see Screen 4.6)
    • create three instances of int (called red, green and blue) which will be given the median of the R value of the color embedding of selected dart and the R value of the color embedding of the selected dart’s neighbor through alpha2
    • return a new Color with these new values for the R, G and B attributes
    • Apply, save and export

To do this you will need to access of the neighboring dart, this is done with n@i which indicates that you want the neighbor of n through the i-link.

To retrieve the red, green and blue attributes you can use the getRed(), getGreen() and getBlue() methods from java.awtColor.

Screen 4.6: color embedding expression
  • try your new rule on the IcositeHexagon by selecting a dart belonging to the face which you want to change color

This rule will be applied to one single dart meaning that the color will be retrieved from the neighboring face which is connected to that particular dart through a 2-link. Depending on the selected dart, the result can vary.

 

COLLECTING DARTS

You will now write a new rule which calculates the median color from all adjacent faces to the one from which a dart is selected. You will learn how to collect darts and how to use a « foreach » loop.

  • open the editor and create a new rule called MedianColor2 (see Screen 4.7)
  • create a node in the left pattern
  • give the (<>) orbit to the node and make it a hook
  • create an identical node in the right pattern

 

Screen 4.7: MedianColor2
  • edit the color embedding expression for that node (see Screen 4.8)
    • create four new instances of integer called red, green, blue and count (all are equal to 0)
    • use a « foreach » loop which browses every JerboaDart as a dart on the <0,1>_<0> orbit starting from n0
    • for each dart add the value of each component of the color from the color embedding of the neighboring dart through a 2-link to red, green and blue and increment count
    • finally divide red, green and blue by count and return a new instance of Color(red, green, blue)
    • Apply, save and export
Screen 4.8: color embedding expression

You can now try the operation on the IcositeHexagon and compare it’s result to that of MedianColor1

 

MORE POSSIBILITIES WITH THE LANGUAGE

You can use these operations in the embedding expressions to manipulate the embeddings.

Operation Jerboa code
Access an « embedding » embedding

Access an i-linked neighboring dart

Collect all embeddings of the <0,1,3> orbit

Collect the <0> sub-orbit from the <0,1,3>(n) orbit

Access the G-map

Get the type of the « embedding » embedding

n.embedding

n@i

<0,1,3>_embedding(n)

<0,1,3>_<0>(n)

@gmap

@ebd<embedding;

You can also use these classic instructions.

if ([condition]) { 
    [instruction] 
} else { 
    [instruction] 
};

while ([condition]) { 
    [instruction] 
};

for ( [type] [variable] : [condition] ) { //foreach
    [instruction] 
};

for i in [beginning] . . [end] { 
    [instruction] 
};

 


What have you learned ?

  • What methods could you create in the « Points.java » embedding class to translate the instance of point by a given vector?
  • How can you collect all the incident faces to a dart n?
  • How can you test whether the incident face to n is a triangle?

Lesson 5