Skip to content

Commit 49cd6a2

Browse files
committed
Piece generators are introduced.
1 parent 3469bf8 commit 49cd6a2

9 files changed

+135
-28
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ The library supports the following features:
1919
- [Movements reflection](doc#movements-reflection) - layers rotation
2020
in the order opposite to a provided one.
2121
- A set of predefined [patterns](doc/patterns-3x3x3) for 3x3x3 cubes.
22+
- [Pieces form customization](doc#piece-generator).

doc/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,56 @@ rubik_cube_reflect_movements(fwd_movements)
187187
```
188188

189189
![](movements-reflection/movements-reflection.gif)
190+
191+
192+
Advanced Technics
193+
-----------------
194+
195+
### [Piece Generators](piece-generators)
196+
197+
Pieces form can be customized by means of piece generators —
198+
special macros which can be separatly implemented. Each generator must
199+
be implemented in a separate file. A cube created by a customized
200+
generator is created by the `rubik_cube_generate_cube(...)` macro
201+
instead of `rubik_cube_create_cube(...)`.
202+
203+
A generator implementation must contain only the body of the macro
204+
without `#macro ...` part (because of scene description language
205+
limitations). The macro is taking the following parameters (and so
206+
they are available in the file as predefined variables):
207+
- `dim` — the cube dimensions (3D vector).
208+
- `pos` — position of the piece to be generated (3D vector). The
209+
position coordinates corresponds to the piece number in order from
210+
left to right, from bottom to top and from front to back.
211+
- `gen_param` — a parameter which is passed to the generator. If
212+
several parameters must be passed, they can be combined in an array.
213+
214+
Normally, a generator returns a piece which is embedded into a cube
215+
with coordinates `<0, 0, 0>`, `<1, 1, 1>`.
216+
217+
For example, a cube made of spheres of two alternating colors:
218+
219+
![](piece-generators/sphere.png)
220+
221+
can be created by the following
222+
[piece generator](piece-generators/sphere.inc):
223+
```
224+
sphere {
225+
0, 0.5
226+
pigment {
227+
gen_param[mod(pos.x + pos.y + pos.z, 2)]
228+
}
229+
translate 0.5
230+
}
231+
```
232+
233+
The file with such implemetation is then
234+
[passed](piece-generators/sphere.pov) to the
235+
`rubik_cube_generate_cube(...)` macro to create a cube:
236+
```
237+
rubik_cube_generate_cube(
238+
<3, 3, 3>, // Cube dimensions.
239+
"sphere.inc", // Piece generator.
240+
array[2] {Red, White} // Piece generator parameter.
241+
)
242+
```

doc/piece-generators/sphere.inc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sphere {
2+
0, 0.5
3+
pigment {
4+
gen_param[mod(pos.x + pos.y + pos.z, 2)]
5+
}
6+
translate 0.5
7+
}

doc/piece-generators/sphere.ini

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Input_File_Name=sphere.pov
2+
Library_Path=../..
3+
Width=320
4+
Height=240
5+
Antialias=true

doc/piece-generators/sphere.png

18.3 KB
Loading

doc/piece-generators/sphere.pov

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "../doc-scene.inc"
2+
#include "rubik-cube.inc"
3+
4+
rubik_cube_to_object(
5+
rubik_cube_generate_cube(
6+
<3, 3, 3>,
7+
"sphere.inc",
8+
array[2] {Red, White}
9+
)
10+
)

rubik-cube-generator-cube.inc

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Generates a piece of cube with defined colors. Each piece is a cube
2+
// of 1 unit size.
3+
//
4+
// gen_param - array of six colors to be applied for the cube. Colors
5+
// are ordered in the following way: right face (0); top face (1);
6+
// back face (2); left face (3); bottom face (4); front face (5).
7+
// Note. Interior faces are colored in black.
8+
9+
object {
10+
#local piece_colors = gen_param;
11+
12+
#if (pos.x != 0) #local piece_colors[3] = Black; #end
13+
#if (pos.x != dim.x - 1) #local piece_colors[0] = Black; #end
14+
#if (pos.y != 0) #local piece_colors[4] = Black; #end
15+
#if (pos.y != dim.y - 1) #local piece_colors[1] = Black; #end
16+
#if (pos.z != 0) #local piece_colors[5] = Black; #end
17+
#if (pos.z != dim.z - 1) #local piece_colors[2] = Black; #end
18+
19+
Round_Box(<-0.5, -0.5, -0.5>, <0.5, 0.5, 0.5>, 0.1, 1)
20+
pigment {
21+
cubic piece_colors[0], piece_colors[1], piece_colors[2], piece_colors[3], piece_colors[4], piece_colors[5]
22+
}
23+
translate 0.5
24+
}

rubik-cube.inc

+34-28
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,41 @@
88
#declare rubik_cube_colors_classic = array[6] {Green, Red, Yellow, Blue, Orange, White};
99

1010

11-
// Generates a piece of cube with defined colors.
11+
// Generates a piece of cube.
1212
//
13-
// colors - array of six colors to be applied for the piece. Colors
14-
// are ordered in the same manner as for rubik_cube_create_cube()
15-
// macro.
13+
// dim - dimensions of the cube (3D vector).
14+
// pos - piece position (3D vector), where the coordinates are numbers
15+
// from 0 to corresponding dimension minus one.
16+
// gen_name - name of the file that contains the generator
17+
// implementation.
18+
// gen_param - a parameter which is passed to the generator. If
19+
// several parameters must be passed, they can be combined in an
20+
// array.
21+
//
22+
// Normally, a generator returns a piece which is embedded into a cube
23+
// with coordinates <0, 0, 0>, <1, 1, 1>.
1624
//
1725
// Returns the generated piece.
18-
#macro rubik_cube_create_piece(colors)
19-
object {
20-
Round_Box(<-0.5, -0.5, -0.5>, <0.5, 0.5, 0.5>, 0.1, 1)
21-
pigment {
22-
cubic colors[0], colors[1], colors[2], colors[3], colors[4], colors[5]
23-
}
24-
translate <0.5, 0.5, 0.5>
25-
}
26+
#macro rubik_cube_generate_piece(dim, pos, gen_name, gen_param)
27+
#include gen_name
2628
#end
2729

2830

2931
// Returns a new cube (3D array of the generated cube pieces).
30-
// Pieces of the cube are smaller cubes of 1 unit size, so a cube
31-
// 3x3x3 will have size of 3 units.
32+
// Pieces of the cube are created by the provided generator.
3233
//
3334
// dim - dimensions of the cube (3D vector).
34-
// colors - array of six colors to be applied for the cube. Colors are
35-
// ordered in the following way: right face (0); top face (1); back
36-
// face (2); left face (3); bottom face (4); front face (5).
37-
// Note. Interior faces are colored in black.
38-
#macro rubik_cube_create_cube(dim, colors)
35+
// gen_name - name of the file that contains the generator
36+
// implementation.
37+
// gen_param - parameter which is passed to the generator.
38+
#macro rubik_cube_generate_cube(dim, gen_name, gen_param)
3939
#local rcube = array[dim.x][dim.y][dim.z];
4040
#for (pos_x, 0, dim.x - 1)
4141
#for (pos_y, 0, dim.y - 1)
4242
#for (pos_z, 0, dim.z - 1)
43-
#local piece_colors = colors;
44-
#if (pos_x != 0) #local piece_colors[3] = Black; #end
45-
#if (pos_x != dim.x - 1) #local piece_colors[0] = Black; #end
46-
#if (pos_y != 0) #local piece_colors[4] = Black; #end
47-
#if (pos_y != dim.y - 1) #local piece_colors[1] = Black; #end
48-
#if (pos_z != 0) #local piece_colors[5] = Black; #end
49-
#if (pos_z != dim.z - 1) #local piece_colors[2] = Black; #end
50-
5143
#local rcube[pos_x][pos_y][pos_z] =
5244
object {
53-
rubik_cube_create_piece(piece_colors)
45+
rubik_cube_generate_piece(dim, <pos_x, pos_y, pos_z>, gen_name, gen_param)
5446
transform {
5547
translate <pos_x, pos_y, pos_z> - <dim.x / 2, dim.y / 2, dim.z / 2>
5648
}
@@ -63,6 +55,20 @@
6355
#end
6456

6557

58+
// Returns a new cube (3D array of the generated cube pieces).
59+
// Pieces of the cube are smaller cubes of 1 unit size, so a cube
60+
// 3x3x3 will have size of 3 units.
61+
//
62+
// dim - dimensions of the cube (3D vector).
63+
// colors - array of six colors to be applied for the cube. Colors are
64+
// ordered in the following way: right face (0); top face (1); back
65+
// face (2); left face (3); bottom face (4); front face (5).
66+
// Note. Interior faces are colored in black.
67+
#macro rubik_cube_create_cube(dim, colors)
68+
rubik_cube_generate_cube(dim, "rubik-cube-generator-cube.inc", colors)
69+
#end
70+
71+
6672
// Returns dimensions of the layers perpendicular to the provided
6773
// axis.
6874
//

test/tests

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
../doc/layers-rotation/layers-rotation-2x4x2.ini
44
../doc/layers-rotation/layers-rotation-3x3x3.ini
55
../doc/notation/notation.ini
6+
../doc/piece-generators/sphere.ini
67
../doc/simple-cube/simple-cube.ini

0 commit comments

Comments
 (0)