-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshape.cxx
1160 lines (914 loc) · 36.6 KB
/
shape.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// yass: Yet Another Soma Solver
// Copyright (C) 2021 Mark R. Rubin aka "thanks4opensource"
//
// This file is part of yass.
//
// The yass program is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// The yass program is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// (LICENSE.txt) along with the yass program. If not, see
// <https://www.gnu.org/licenses/gpl.html>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits>
#include "rotators.hxx"
#include "signature.hxx"
#include "shape.hxx"
namespace soma {
// Public ======================================================================
Shape::Shape(
unsigned number_of_cubicles)
:
#ifdef SOMA_STD_SET_UNORDERED
// pre-allocate hash table for efficiency
_solutions_sets{SignatureSet(1<<14),
SignatureSet(1<<14),
SignatureSet(1<<14),
SignatureSet(1<<14),
SignatureSet(1<<14),
SignatureSet(1<<14),
SignatureSet(1<<14)},
#endif
_num_cubicles(number_of_cubicles)
#ifdef SOMA_STATISTICS
,
_statuses_uniques {0},
_statuses_duplicates{0}
#endif
{}
// See shape.hxx
void Shape::reset()
{
_symmetries .clear();
_rotators_mirrorers.clear();
_solutions .clear();
_solution_ps .clear();
_solution_ns .clear();
for (SignatureSet &solutions : _solutions_sets)
solutions.clear();
for (Shape *child : _children)
delete child;
_children.clear();
for (Cubicle &cubicle : _cubicles) {
cubicle.occupant = 0 ;
cubicle.parent = &cubicle ;
cubicle.status = Cubicle::Status::UNSET;
}
} // reset()
// See shape.hxx
bool Shape::read(
std::istream &input ,
std::ostream *errors)
{
unsigned x ,
y = 0 ,
z = 0 ,
max_x = 0 ,
max_y = 0 ,
cubicle_ndx = 0 ;
bool z_pending = false, // to handle multiple blank lines ...
started = false , // ... between Z layers
blank ; // to separate Z layers
std::string line ; // input text
while (std::getline(input, line)) {
// truncate line if/at comment character
std::string::size_type special;
if ((special = line.find("#")) != std::string::npos)
line = line.substr(0, special);
// Comment and/or pure whitespace lines also count as blank
if (line.empty())
blank = true;
else if ((special = line.find_first_not_of("\t ")) == std::string::npos)
blank = true;
else
blank = false;
// New Z layer, handling of multiple blank lines
if (blank) {
if (!started) continue;
y = 0 ;
z_pending = true;
continue ;
}
started = true;
// Beginning of new Z layer
if (z_pending) {
z += 1 ;
z_pending = false;
}
// Z layer consisting of multiple Y slices
x = 0;
for (char const &letter : line) {
if (letter == '\t') {
*errors << "Illegal tab character in file"
<< std::endl;
return false;
}
// shape cubicle or empty space
if (letter != '.' && letter != ' ') {
if (cubicle_ndx < NUMBER_OF_CUBICLES) {
_cubicles[cubicle_ndx](x, y, z);
_cubicles[cubicle_ndx].parent = &_cubicles[cubicle_ndx];
_cubicles[cubicle_ndx].in_child = false ;
set_cubicle_piece(_cubicles[cubicle_ndx], letter);
}
++cubicle_ndx;
}
if (x > max_x) max_x = x;
++x;
}
if (y > max_y) max_y = y;
++y;
}
if (cubicle_ndx != NUMBER_OF_CUBICLES) {
if (errors)
*errors << "Bad number of cubicles: "
<< cubicle_ndx
<< " instead of "
<< NUMBER_OF_CUBICLES
<< std::endl;
return false;
}
// save values
_max_pos(max_x, max_y, z);
_num_cubicles = NUMBER_OF_CUBICLES;
// reverse Y and Z coords (were read in high-to-low so indexed 0-to-N)
// but want +Z and +Y up
for (Cubicle &cubicle : _cubicles) {
cubicle.y() = _max_pos.y() - cubicle.y();
cubicle.z() = _max_pos.z() - cubicle.z();
}
return prepare_solve(errors);
} // read(std::istream&, std::ostream&)
// See shape.hxx
bool Shape::specify(
const std::array<int, NUMBER_OF_CUBICLES * 3> coords,
const std::string pieces,
std::ostream *errors)
{
unsigned cubicle_ndx = 0;
for (Cubicle &cubicle : _cubicles) {
cubicle(coords[cubicle_ndx * 3 ],
coords[cubicle_ndx * 3 + 1],
coords[cubicle_ndx * 3 + 2]);
cubicle.parent = &cubicle;
cubicle.in_child = false ;
if (cubicle_ndx < pieces.size())
set_cubicle_piece(cubicle, pieces[cubicle_ndx]);
else
cubicle.occupant = 0;
++cubicle_ndx;
}
return prepare_solve(errors);
} // specify(const std::array, const std::string, std::ostream*)
// See shape.hxx
unsigned Shape::num_piece_cubicles(
const Piece *piece)
{
unsigned count = 0;
for (const Cubicle &cubicle : _cubicles)
if (cubicle.occupant == piece->code())
++count;
return count;
} // num_piece_cubicles(const Piece*)
// See shape.hxx
void Shape::write(
std::ostream &output)
const
{
// space instead of '.' for all-empty Y columns (for separated shapes)
//
IntSet full_xs;
for (auto cubicle : _cubicles)
full_xs.insert(cubicle.x());
auto cubicle = _cubicles.cbegin();
// z and y high-to-low, x low-to-high
for (int z_pos = _max_pos.z(); z_pos >= -_max_pos.z(); z_pos -= 2) {
for (int y_pos = _max_pos.y(); y_pos >= -_max_pos.y(); y_pos -= 2) {
for (int x_pos = -_max_pos.x(); x_pos <= _max_pos.x(); x_pos += 2) {
if (*cubicle == Position(x_pos, y_pos, z_pos)) {
if (cubicle->occupant)
output << Piece::code2name(cubicle->occupant);
else
output << '#';
++cubicle;
}
else if (full_xs.find(x_pos) != full_xs.end())
output << '.';
else
output << ' ';
}
output << std::endl;
}
if (z_pos != -_max_pos.z())
output << std::endl;
}
} // write(ostream&) const
// See shape.hxx
std::array<char, Shape::NUMBER_OF_CUBICLES> Shape::solution(
std::array<int, Shape::NUMBER_OF_CUBICLES * 3> &coords)
const
{
std::array<char, Shape::NUMBER_OF_CUBICLES> pieces;
for (unsigned ndx = 0; ndx < NUMBER_OF_CUBICLES; ++ndx) {
pieces[ndx ] = Piece::code2name(_cubicles[ndx].occupant);
coords[ndx * 3 ] = _cubicles[ndx].x();
coords[ndx * 3 + 1] = _cubicles[ndx].y();
coords[ndx * 3 + 2] = _cubicles[ndx].z();
}
return pieces;
} // solution(...)
// See shape.hxx
bool Shape::generate_rotator_reflectors(
std::ostream *errors)
{
if (_children.size() == 1)
return generate_rotator_reflectors(this, errors);
else
for (Shape *child : _children)
if (!generate_rotator_reflectors(child, errors))
return false;
return true;
} // generate_rotator_reflectors(std::ostream*)
// See shape.hxx
bool Shape::is_duplicate_solution(
const unsigned piece_number)
{
Signature signature;
if (_children.size() == 1)
generate_signature(signature);
else {
unsigned offset = 0;
for (Shape *child : _children) {
child->generate_signature_child(signature, offset);
offset += child->_num_cubicles;
}
}
// Check if in already seen solutions (or their rotations/reflections)
return _solutions_sets[piece_number].find(signature)
!= _solutions_sets[piece_number].end ( );
} // is_duplicate_solution(const unsigned)
// See shape.hxx
void Shape::add_solution(
const unsigned piece_number) // can be called after any piece is placed
{
if (_children.size() == 1) { // No need to combine/permute child solutions
add_solution_no_children(piece_number);
return;
}
// Generate all combinations of rotated/reflected child shapes
// E.g. if child/num_rot_refects are: a/2 b/3 c/1
// generate: a0/b0/c0 a0/b1/c0 a0/b2/c0
// a1/b0/c0 a1/b1/c0 a1/b2/c0
// But also make sure that no combination has multiple "p" or "n"
// shapes (which were generated when reflecting/mirror the
// child shapes>
//
for (Shape *child : _children) {
child->_solutions .clear();
child->_solution_ps.clear();
child->_solution_ns.clear();
add_solution(child) ;
}
// Indices of each child's rotated/mirrored solutions
std::vector<unsigned> combinations(_children.size(), 0);
// Go through all solutions for each child
while ( combinations[_children.size() - 1]
< _children[_children.size() - 1]->_solutions.size()) {
// must be before goto
Signature solution ; // concatenated child signatures
unsigned solution_ndx = 0;
// Only check for multiple "p" and "n" pieces if all pieces placed
if (piece_number == Piece::NUMBER_OF_PIECES - 1) {
unsigned num_ps = 0,
num_ns = 0;
for (unsigned ndx = 0 ; ndx < _children.size() ; ++ndx) {
num_ps += _children[ndx]->_solution_ps[combinations[ndx]];
num_ns += _children[ndx]->_solution_ns[combinations[ndx]];
}
// Must have exactly one each of "p" and "n" pieces
if (num_ps != 1 || num_ns != 1)
goto next_combination;
}
for (unsigned child_ndx = 0 ;
child_ndx < _children.size() ;
++child_ndx ) {
for (unsigned piece_ndx = 0 ;
piece_ndx < _children[child_ndx]->_num_cubicles ;
++piece_ndx ){
solution[solution_ndx++]
= _children[child_ndx]
->_solutions[combinations[child_ndx]][piece_ndx];
}
}
_solutions_sets[piece_number].insert(solution);
next_combination:
// increment to next permutation
for (unsigned permute_ndx = 0 ;
permute_ndx < _children.size() ;
++permute_ndx ) {
if ( ++combinations[permute_ndx]
< _children[permute_ndx]->_solutions.size())
break;
// rollover except last ("most significant digit") one
if (permute_ndx < _children.size() - 1)
combinations[permute_ndx] = 0;
}
}
} // add_solution(const unsigned)
// See shape.hxx
bool Shape::place_piece(
const unsigned cubicle_ndx ,
Piece* const piece ,
const unsigned piece_number ,
const unsigned number_of_cubes,
const Position cubes[] ,
const bool just_test )
{
Cubicle *center = &_cubicles[cubicle_ndx];
Cubicle *peripherals[Piece::MAX_NUMBER_OF_CUBES];
for (unsigned ndx = 0; ndx < number_of_cubes; ++ndx) {
Cubicle *peripheral = find_cubicle(center, cubes[ndx]);
if (!peripheral || peripheral->occupant)
return false;
peripherals[ndx] = peripheral;
}
if (just_test) return true;
center->occupant = piece->code();
// number_of_cubes doesn't include central one
_piece_cubicles[piece_number][number_of_cubes] = center;
for (unsigned ndx = 0; ndx < number_of_cubes; ++ndx) {
peripherals[ndx]->occupant = piece->code();
_piece_cubicles[piece_number][ndx] = peripherals[ndx];
}
return true;
} // place_piece(const unsigned ,
// Piece* const ,
// const unsigned ,
// const unsigned ,
// const Position[],
// const bool )
// See shape.hxx
bool Shape::has_orphan()
const
{
uint32_t handled = 0;
for (unsigned cubicle_ndx = 0 ;
cubicle_ndx < NUMBER_OF_CUBICLES ;
++cubicle_ndx ) {
const Cubicle *cubicle = &_cubicles[cubicle_ndx];
if (cubicle->occupant || (handled & (1 << cubicle_ndx)))
continue;
unsigned num_empties = 0;
const Cubicle *twin ;
for (const Cubicle* adjacent : cubicle->ortho_adjacents)
if (adjacent && !adjacent->occupant) {
++num_empties;
twin = adjacent;
}
if (num_empties == 0)
return true;
else if (num_empties == 1) {
num_empties = 0;
const Cubicle *only = 0; // initialize to silence g++ warning
for (const Cubicle* sibling : twin->ortho_adjacents)
if (sibling && !sibling->occupant) {
++num_empties;
only = sibling;
}
// num_empties can't be 0
if (num_empties == 1 && only == cubicle)
return true;
handled |= 1 << (twin - &_cubicles[0]);
}
}
return false;
} // has_orphan() const
// Protected ===================================================================
// Decode letter into Piece
// Used by read() and specify()
//
void Shape::set_cubicle_piece(
Cubicle &cubicle,
const char letter )
{
switch (letter) {
case 'c':
cubicle.occupant = Piece::corner.code();
Piece::corner.pre_place();
break;
case 'p':
cubicle.occupant = Piece::pos.code();
Piece::pos.pre_place();
break;
case 'n':
cubicle.occupant = Piece::neg.code();
Piece::neg.pre_place();
break;
case 'z':
cubicle.occupant = Piece::zee.code();
Piece::zee.pre_place();
break;
case 't':
cubicle.occupant = Piece::tee.code();
Piece::tee.pre_place();
break;
case 'l':
cubicle.occupant = Piece::ell.code();
Piece::ell.pre_place();
break;
case '3':
cubicle.occupant = Piece::three.code();
Piece::three.pre_place();
break;
default:
cubicle.occupant = 0;
break;
}
} // set_cubicle_piece();
bool Shape::prepare_solve(
std::ostream *errors)
{
normalize ();
center ();
generate_symmetries ();
find_adjacent_cubicles();
if (!create_children()) {
if (errors)
*errors << "Has child shape with unsolvable number of cubicles"
<< std::endl;
return false;
}
return true;
} // prepare_solve(std::ostream*)
// Initialize Cubicle::.adjacents[][][] for each _cubicle
// Fills in only needed 6 orthogonal elements of .adjacents
// ((plus superfluous 0,0,0)
// Use of inefficient find_cubicle(Position&) because only called
// once per solve from:
// read()/specify() -> prepare_solve() ->
// create_children() -> find_adjacent_cubicles()
// Then fills Cubicle::ortho_adjacents for subsequent use
// Must do after center() because find_cubicle(Position&) works
// with 2*(x,y,z) indices. See Position::center(...)
//
void Shape::find_adjacent_cubicles()
{
// find adjacent _cubicles
// must do after centering because find_cubicle() works
// using 2*N indexes
for (Cubicle &cubicle : _cubicles) {
for (int z = 0; z < 3 ; ++z)
for (int y = 0 ; y < 3 ; ++y)
for (int x = 0 ; x < 3 ; ++x)
// Only orthogonals (and unneeded 0,0,0)
if ((x - 1) * (y - 1) * (z - 1) == 0) {
// See Position::center() for why 2*(x,y,z)-2
cubicle.adjacents[x][y][z]
= find_cubicle( cubicle
+ Position(2 * x - 2,
2 * y - 2,
2 * z - 2));
}
cubicle.ortho_adjacents[OrthAdj::UP ] = cubicle.adjacents[1][1][2];
cubicle.ortho_adjacents[OrthAdj::DOWN ] = cubicle.adjacents[1][1][0];
cubicle.ortho_adjacents[OrthAdj::FRONT] = cubicle.adjacents[1][2][1];
cubicle.ortho_adjacents[OrthAdj::BACK ] = cubicle.adjacents[1][0][1];
cubicle.ortho_adjacents[OrthAdj::LEFT ] = cubicle.adjacents[0][1][1];
cubicle.ortho_adjacents[OrthAdj::RIGHT] = cubicle.adjacents[2][1][1];
}
} // find_adjacent_cubicles()
// Create _children shapes, one for each separated part of shape
//
bool Shape::create_children()
{
for (Cubicle &cubicle : _cubicles)
if (!cubicle.in_child) {
// For each unprocessed cubicle, populate_child() will
// traverse all orthogonally-connected cubicles
// and mark as processed.
Shape *child = new Shape(0);
_children.push_back(child) ;
populate_child(child, &cubicle);
}
// init each child
for (Shape *child : _children) {
child->normalize ();
child->undo_odd_even ();
child->center ();
child->generate_symmetries();
// sort child cubicles
std::sort(&child->_cubicles[0] ,
&child->_cubicles[0] + child->_num_cubicles);
}
// Check sanity of each child -- no need to attempt solve if
// any is unsolvable.
// Must have 4*n or 4*n+3 cubicles, n>=0 && n<=6 (combination of
// zero or more 4-cubicle pieces with or without 3-cubicle piece)
for (Shape *child : _children)
if ( child->_num_cubicles < 3
|| ( child->_num_cubicles % 4 != 0
&& child->_num_cubicles % 4 != 3))
return false;
return true;
} // create_children()
// Find raw symmetries of shape, based only on rectangular parallelpiped
// bounding box of shape.
// Later, generate_rotator_reflectors() and check_add_symmetric() will
// prune these taking into account actual shape.
//
void Shape::generate_symmetries()
{
// not necessary because only called at initialization, but
// amount of memory is trivial
_symmetries.reserve(Rotators::MAX_NUMBER_OF_ORIENTATIONS);
// always include identity so add_solution() doesn't have to
// special case newly found non-rotated solution
_symmetries.push_back(Rotators::POSX_POSY_POSZ);
// any centered parallelpiped is unchanged when rotated 180 around any axis
_symmetries.push_back(Rotators::NEGX_NEGY_POSZ);
_symmetries.push_back(Rotators::NEGX_POSY_NEGZ);
_symmetries.push_back(Rotators::POSX_NEGY_NEGZ);
// Check for valid 90 degree rotations
//
if (_max_pos.x() == _max_pos.y()) {
_symmetries.push_back(Rotators::NEGY_POSX_POSZ);
// _symmetries.push_back(Rotators::NEGX_NEGY_POSZ);
_symmetries.push_back(Rotators::POSY_NEGX_POSZ);
// _symmetries.push_back(Rotators::NEGX_POSY_NEGZ);
_symmetries.push_back(Rotators::NEGY_NEGX_NEGZ);
// _symmetries.push_back(Rotators::POSX_NEGY_NEGZ);
_symmetries.push_back(Rotators::POSY_POSX_NEGZ);
}
if (_max_pos.x() == _max_pos.z()) {
// _symmetries.push_back(Rotators::POSX_POSY_POSZ);
// _symmetries.push_back(Rotators::NEGX_POSY_NEGZ);
// _symmetries.push_back(Rotators::POSX_NEGY_NEGZ);
_symmetries.push_back(Rotators::NEGZ_POSY_POSX);
_symmetries.push_back(Rotators::POSZ_NEGY_POSX);
_symmetries.push_back(Rotators::POSZ_POSY_NEGX);
_symmetries.push_back(Rotators::NEGZ_NEGY_NEGX);
}
if (_max_pos.y() == _max_pos.z()) {
// _symmetries.push_back(Rotators::POSX_POSY_POSZ);
// _symmetries.push_back(Rotators::NEGX_POSY_NEGZ);
// _symmetries.push_back(Rotators::POSX_NEGY_NEGZ);
_symmetries.push_back(Rotators::POSX_NEGZ_POSY);
_symmetries.push_back(Rotators::NEGX_POSZ_POSY);
_symmetries.push_back(Rotators::POSX_POSZ_NEGY);
_symmetries.push_back(Rotators::NEGX_NEGZ_NEGY);
}
if (_max_pos.x() == _max_pos.y() && _max_pos.y() == _max_pos.z()) {
_symmetries.push_back(Rotators::POSZ_POSX_POSY);
_symmetries.push_back(Rotators::NEGZ_NEGX_POSY);
_symmetries.push_back(Rotators::NEGZ_POSX_NEGY);
_symmetries.push_back(Rotators::POSZ_NEGX_NEGY);
_symmetries.push_back(Rotators::NEGY_NEGZ_POSX);
_symmetries.push_back(Rotators::POSY_POSZ_POSX);
_symmetries.push_back(Rotators::NEGY_POSZ_NEGX);
_symmetries.push_back(Rotators::POSY_NEGZ_NEGX);
}
} // generate_symmetries()
// Undo odd/even dimension indices 1,3,... vs 0,2,4... (see Position::center())
// created by normalize() because center() needs 0,1,2,...
// Used for child shapes because full shape already had normalize()
//
void Shape::undo_odd_even()
{
_max_pos >>= 1;
for (unsigned ndx = 0 ; ndx < _num_cubicles ; ++ndx)
_cubicles[ndx] >>= 1;
} // undo_odd_even()
// Recursively find all orthogonally-connected cubicles from
// starting cubicle (called from create_children()), and
// add (marked as in_child in main shape) to child
//
void Shape::populate_child(
Shape *child ,
Cubicle *cubicle)
{
child->_cubicles[child->_num_cubicles] = *cubicle;
cubicle->in_child = true;
child->_cubicles[child->_num_cubicles++].parent = cubicle;
for (Cubicle *adjacent : cubicle->ortho_adjacents)
if (adjacent && !adjacent->in_child)
populate_child(child, adjacent);
} // populate_child()
// Prune basic list of valid shape rotations in _symmetries created
// by generate_symmetries() based only on rectangular parallelpiped
// bounding box leaving only truly valid ones based on actual shape.
// Also check for unsolvable one-dimensional shapes (two-dimensional is
// OK because valid for child shapes)
// Calls check_add_symmetric() for each rotation in _symmetries.
//
bool Shape::generate_rotator_reflectors(
Shape* const shape ,
std::ostream *errors)
{
// not necessary because only called at initialization, but
// amount of memory is trivial
_rotators_mirrorers.reserve(MAX_ROTATOR_REFLECTORS);
// Check solvable
unsigned num_2_or_3d = 0;
if (_max_pos.z() > 0) ++num_2_or_3d;
if (_max_pos.y() > 0) ++num_2_or_3d;
if (_max_pos.x() > 0) ++num_2_or_3d;
if (num_2_or_3d < 2) {
*errors << "Unsolvable one- or zero-dimensional shape or part of shape"
<< std::endl;
return false;
}
for (unsigned symmetry : shape->_symmetries)
for (unsigned mirror = 0 ; mirror < 2 ; ++mirror)
// no error if not symmetric, just doesn't get added
check_add_symmetric(shape, symmetry, mirror);
return true;
} // generate_rotator_reflectors(Shape*, std::ostream*)
// Called by generate_rotator_reflectors(), above, for
// each rototator/reflector, both plain and mirrored
// Returns true if shape is symmetric after such transformation
//
void Shape::check_add_symmetric(
Shape* const shape ,
const unsigned symmetry,
const bool mirror )
{
std::array<Cubicle, NUMBER_OF_CUBICLES> rotated ;
// Choose which set of mirrored Rotators to use
unsigned mirror_offset = 0;
if (mirror) {
if (_max_pos.z() > 0) mirror_offset = Rotators::Z_MIRRORED_OFFSET;
else mirror_offset = Rotators::X_MIRRORED_OFFSET;
// already checked 1D or 0D in generate_rotator_reflectors
}
// Rotate/mirror shape
for (unsigned ndx = 0 ; ndx < shape->_num_cubicles ; ++ndx) {
Position position = shape->_cubicles[ndx];
rotated[ndx](position.rotate( Rotators::rotator(symmetry
+ mirror_offset )));
}
// Rotation has changed canonical linear ordering
std::sort(rotated.begin(), rotated.begin() + shape->_num_cubicles);
// Check each cubicle, and abort if any is not symmetric
for (unsigned ndx = 0 ; ndx < shape->_num_cubicles ; ++ndx)
if (rotated[ndx] != shape->_cubicles[ndx])
return;
// All were symmetric
shape->_rotators_mirrorers.push_back(symmetry + mirror_offset);
} // check_add_symmetric(Shape* const, const nusigned, const bool)
// Set Cubicle::status for each _cubicle
// For doing symmetry check.
//
void Shape::set_statuses(
Shape* const child ,
const unsigned piece_number,
const char piece_name )
{
if (child->reset_statuses() == child->_num_cubicles)
return; // all cubicles occupied by pieces, no need to continue
// Unrotated signature
Signature signature;
generate_signature_child(signature, 0);
// Fill in for each rotation/reflection
//
child->_piece_rotators_mirrorers[piece_number].clear();
for (unsigned rot_mir_ndx = 1 ;
rot_mir_ndx < child->_rotators_mirrorers.size() ;
++rot_mir_ndx ) {
const unsigned rotator_mirrorer
= child->_rotators_mirrorers[rot_mir_ndx];
// Check and don't mirror "p" and "n" pieces
if ( ( piece_name == Piece::pos.name()
|| piece_name == Piece::neg.name())
&& rotator_mirrorer >= Rotators::Z_MIRRORED_OFFSET)
break;
Signature rotated_signature;
child->generate_rotated_signature(rotated_signature, rotator_mirrorer);
// Add if this rotation/reflection is symmetric
if (rotated_signature == signature)
child->_piece_rotators_mirrorers[piece_number]
.push_back(rotator_mirrorer);
}
// Go through all _cubicles
//
unsigned
num_unset = std::count_if(child->_cubicles.cbegin() ,
child->_cubicles.cbegin() + child->_num_cubicles,
[](Cubicle cubicle) {
return cubicle.parent->status
== Cubicle::Status::UNSET;} );
unsigned primary_ndx = 0;
while (num_unset) {
// Find a yet-unprocessed cubicle
while ( child->_cubicles[primary_ndx].parent->status
!= Cubicle ::Status::UNSET
&& primary_ndx < child->_num_cubicles )
++primary_ndx;
if (primary_ndx == child->_num_cubicles) { // at end
num_unset = 0;
break;
}
// Mark as primary
Cubicle &primary = child->_cubicles[primary_ndx];
primary.parent->status = Cubicle::Status::PRIMARY;
--num_unset;
// Find all symmetric rotations/reflections of primary cubicle
for (const unsigned rotator_mirrorer
: child->_piece_rotators_mirrorers[piece_number]) {
for (unsigned duplicate_ndx = primary_ndx ;
duplicate_ndx < child->_num_cubicles ;
++duplicate_ndx ) {
Cubicle &duplicate = child->_cubicles[duplicate_ndx];
if (duplicate.parent->status != Cubicle::Status::UNSET)
continue; // already processed
Position rotated
= duplicate.rotate( Rotators
::rotator(rotator_mirrorer));
if (rotated == primary) {
// Is rotation/reflection of primary
duplicate.parent->status = Cubicle::Status::DUPLICATE;
--num_unset;
break;
}
}
}
}
} // set_statuses(Shape* const, const unsigned, const char)
// Slightly more efficient version of set_statuses(), above,
//
void Shape::set_statuses_no_children(
const unsigned piece_number,
const char piece_name )
{
if (reset_statuses() == NUMBER_OF_CUBICLES)
return;
Signature signature;
generate_signature(signature);
_piece_rotators_mirrorers[piece_number].clear();
for (unsigned rot_mir_ndx = 1 ;
rot_mir_ndx < _rotators_mirrorers.size() ;
++rot_mir_ndx ) {
const unsigned rotator_mirrorer
= _rotators_mirrorers[rot_mir_ndx];
if ( ( piece_name == Piece::pos.name()
|| piece_name == Piece::neg.name())
&& rotator_mirrorer >= Rotators::Z_MIRRORED_OFFSET)
break;
Signature rotated_signature;
generate_rotated_signature(rotated_signature, rotator_mirrorer);
if (rotated_signature == signature)
_piece_rotators_mirrorers[piece_number].push_back(rotator_mirrorer);
}
unsigned num_unset
= std::count_if(_cubicles.cbegin() ,
_cubicles.cbegin() + _num_cubicles,
[](Cubicle cubicle) {
return cubicle.parent->status
== Cubicle::Status::UNSET;});
unsigned primary_ndx = 0;
while (num_unset) {
while ( _cubicles[primary_ndx].parent->status
!= Cubicle ::Status::UNSET
&& primary_ndx < _num_cubicles )
++primary_ndx;
if (primary_ndx == _num_cubicles) {
num_unset = 0;
break;
}
Cubicle &primary = _cubicles[primary_ndx];
primary.parent->status = Cubicle::Status::PRIMARY;
--num_unset;
for (const unsigned rotator_mirrorer
: _piece_rotators_mirrorers[piece_number]) {