-
Notifications
You must be signed in to change notification settings - Fork 450
/
DHFactor.java
1356 lines (1187 loc) · 33.6 KB
/
DHFactor.java
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
import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.lang.*;
/**
* A class to simplify symbolic transform expressions.
* @author Peter I. Corke peter.i.corke@gmail.com
*
* Major update 2/2014
*/
/**
* Element is a class that represents one element in a transform expression.
* The transform types represented include:
* TX, TY, TZ pure translation along X, Y and Z axes respectively
* RX, RY, RZ pure rotations about the X, Y and Z axes respectively
* DH Denavit-Hartenberg joint transformation
*
* public boolean istrans()
* public boolean isrot()
* public int axis()
* public boolean isjoint() {
* public boolean factorMatch(int dhWhich, int i, int verbose) {
* public void add(Element e) {
* public Element(int type, int constant) {
* public Element(int type) // new of specified type
*
* public static String toString(Element [] e) {
* public String argString() {
* public String toString() {
*
* Constructors:
* Element(Element e) // clone of argument
* Element(Element e, int type, int sign) // clone of argument with new type
* Element(Element e, int type) // clone of argument with new type
* Element(String s)
*/
class Element {
static final int TX = 0,
TY = 1,
TZ = 2,
RX = 3,
RY = 4,
RZ = 5,
DH_STANDARD = 6,
DH_MODIFIED = 7;
// one of TX, TY ... RZ, DH_STANDARD/MODIFIED
int type;
// transform parameters, only one of these is set
String var; // eg. q1, for joint var types
String symconst; // eg. L1, for lengths
int constant; // eg. 90, for angles
// DH parameters, only set if type is DH_STANDARD/MODIFIED
int theta,
alpha;
String A,
D;
int prismatic;
int offset;
// an array of counters for the application of each rule
// just for debugging.
static int[] rules = new int[20]; // class variable
// mapping from type to string
static final String[] typeName = {
"Tx", "Ty", "Tz", "Rx", "Ry", "Rz", "DH", "DHm"};
// order of elementary transform for each DH convention
// in each tuple, the first element is the transform type,
// the second is true if it can be a joint variable.
static final int dhStandard[][] = {
{RZ, 1}, {TX, 0}, {TZ, 1}, {RX, 0} };
static final int dhModified[][] = {
{RX, 0}, {TX, 0}, {RZ, 1}, {TZ, 1} };
/*
* Display the number of times each rule was used in the
* conversion.
*/
public static void showRuleUsage() {
for (int i=0; i<20; i++)
if (rules[i] > 0)
System.out.println("Rule " + i + ": " +
rules[i]);
}
// test if the Element is a translation, eg. TX, TY or TZ
public boolean istrans() {
return (type == TX) || (type == TY) || (type == TZ);
}
// test if the Element is a rotation, eg. RX, RY or RZ
public boolean isrot() {
return (type == RX) || (type == RY) || (type == RZ);
}
// true if this transform represents a joint coordinate, ie. not
// a constant
public boolean isjoint() {
return this.var != null;
}
// return the axis, 0 for X, 1 for Y, 2 for Z
public int axis() {
switch (type) {
case TX:
case RX:
return 0;
case TY:
case RY:
return 1;
case TZ:
case RZ:
return 2;
default:
throw new IllegalArgumentException("bad transform type");
}
}
// return the summation of two symbolic parameters as a string
private String symAdd(String s1, String s2)
{
if ( (s1 == null) && (s2 == null) )
return null;
else if ( (s1 != null) && (s2 == null) )
return new String(s1);
else if ( (s1 == null) && (s2 != null) )
return new String(s2);
else {
if (s2.charAt(0) == '-')
// handle the case where S2 begins with a negative sign
return s1 + s2;
else
return s1 + "+" + s2;
}
}
/**
* Add the argument of another Element to this element.
* assumes that variable has not already been set
* used by factor() to build a DH element
*/
public void add(Element e) {
if ((this.type != DH_STANDARD) && (this.type != DH_MODIFIED))
throw new IllegalArgumentException("wrong element type " + this);
System.out.println(" adding: " + this + " += " + e);
switch (e.type) {
case RZ:
if (e.isjoint()) {
this.prismatic = 0;
this.var = e.var;
this.offset = e.constant;
this.theta = 0;
} else
this.theta = e.constant;
break;
case TX:
this.A = e.symconst; break;
case TZ:
if (e.isjoint()) {
this.prismatic = 1;
this.var = e.var;
this.D = null;
} else
this.D = e.symconst;
break;
case RX:
this.alpha = e.constant; break;
default:
throw new IllegalArgumentException("cant factorize " + e);
}
}
// test if this particular element could be part of a DH term
// eg. Rz(q1) can be, Rx(q1) cannot.
public boolean factorMatch(int dhWhich, int i, int verbose) {
int dhFactors[][];
boolean match;
switch (dhWhich) {
case DH_STANDARD:
dhFactors = dhStandard;
break;
case DH_MODIFIED:
dhFactors = dhModified;
break;
default:
throw new IllegalArgumentException("bad DH type");
}
match = (this.type == dhFactors[i][0]) &&
!((dhFactors[i][1] == 0) && this.isjoint());
if (verbose > 0)
System.out.println(" matching " + this + " (i=" + i + ") " +
" to " + typeName[dhFactors[i][0]] + "<" +
dhFactors[i][1] + ">" + " -> " + match);
return match;
}
/**
* test if two transforms can be merged
* @param e the element to compare with this
* @return - this if no merge to be done
* - null if the result is a null transform
* - new transform resulting from a merge.
*/
Element merge(Element e) {
/*
* don't merge if dissimilar transform or
* both are joint variables
*/
if (
(e.type != this.type) ||
(e.isjoint() && this.isjoint())
)
return this;
Element sum = new Element(this);
sum.var = symAdd(this.var, e.var);
sum.symconst = symAdd(this.symconst, e.symconst);
sum.constant = this.constant + e.constant;
/* hack
if (Math.abs(sum.constant) > 90)
throw new IllegalArgumentException("rotation angle > 90");
*/
/*
* remove a null transform which can result from
* a merge operation
*/
if ( !sum.isjoint() && (sum.symconst == null) && (sum.constant == 0)) {
System.out.println("Eliminate: " + this + " " + e);
return null;
} else {
System.out.println("Merge: " + this + " " + e + " := " + sum);
return sum;
}
}
/**
* test if two transforms need to be swapped
* @param e the element to compare with this
* @return - true if swap is required
*/
boolean swap(Element next, int dhWhich) {
/*
* don't swap if both are joint variables
*/
if ( this.isjoint() && next.isjoint() )
return false;
switch (dhWhich) {
case Element.DH_STANDARD:
/*
* we want to sort terms into the order:
* RZ
* TX
* TZ
* RX
*/
/* TX TY TZ RX RY RZ */
int order[] = { 2, 0, 3, 4, 0, 1 };
if (
((this.type == TZ) && (next.type == TX)) ||
/*
* push constant translations through rotational joints
* of the same type
*/
((this.type == TX) && (next.type == RX) && next.isjoint()) ||
((this.type == TY) && (next.type == RY)) && next.isjoint() ||
((this.type == TZ) && (next.type == RZ)) && next.isjoint() ||
(!this.isjoint() && (this.type == RX) && (next.type == TX)) ||
(!this.isjoint() && (this.type == RY) && (next.type == TY)) ||
//(!this.isjoint() && (this.type == RZ) && (next.type == TZ)) ||
(!this.isjoint() && !next.isjoint() && (this.type == TZ) && (next.type == RZ)) ||
/*
* move Ty terms to the right
*/
((this.type == TY) && (next.type == TZ)) ||
((this.type == TY) && (next.type == TX))
) {
System.out.println("Swap: " + this + " <-> " + next);
return true;
}
break;
case Element.DH_MODIFIED:
if (
((this.type == RX) && (next.type == TX)) ||
((this.type == RY) && (next.type == TY)) ||
((this.type == RZ) && (next.type == TZ)) ||
((this.type == TZ) && (next.type == TX))
) {
System.out.println("Swap: " + this + " <-> " + next);
return true;
}
break;
default:
throw new IllegalArgumentException("bad DH type");
}
return false;
}
/**
* Substitute this transform for a triple of transforms
* that includes an RZ or TZ.
*
* @return - null if no substituion required
* - array of Elements to substitute
*/
Element[] substituteToZ() {
Element[] s = new Element[3];
switch (this.type) {
case RX:
s[0] = new Element(RY, 90);
s[1] = new Element(this, RZ);
s[2] = new Element(RY, -90);
return s;
case RY:
s[0] = new Element(RX, -90);
s[1] = new Element(this, RZ);
s[2] = new Element(RX, 90);
return s;
case TX:
s[0] = new Element(RY, 90);
s[1] = new Element(this, TZ);
s[2] = new Element(RY, -90);
return s;
case TY:
s[0] = new Element(RX, -90);
s[1] = new Element(this, TZ);
s[2] = new Element(RX, 90);
return s;
default:
return null;
}
}
Element[] substituteToZ(Element prev) {
Element[] s = new Element[3];
switch (this.type) {
case RY:
s[0] = new Element(RZ, 90);
s[1] = new Element(this, RX);
s[2] = new Element(RZ, -90);
rules[8]++;
return s;
case TY:
if (prev.type == RZ) {
s[0] = new Element(RZ, 90);
s[1] = new Element(this, TX);
s[2] = new Element(RZ, -90);
rules[6]++;
return s;
} else {
s[0] = new Element(RX, -90);
s[1] = new Element(this, TZ);
s[2] = new Element(RX, 90);
rules[7]++;
return s;
}
default:
return null;
}
}
/**
* Simple rewriting rule for adjacent transform pairs. Attempt to
* eliminate TY and RY.
* @param previous element in list
* @return - null if no substituion required
* - array of Elements to subsitute
*/
Element[] substituteY(Element prev, Element next) {
Element[] s = new Element[2];
if (prev.isjoint() || this.isjoint())
return null;
/* note that if rotation is -90 we must make the displacement -ve */
if ((prev.type == RX) && (this.type == TY)) {
// RX.TY -> TZ.RX
s[0] = new Element(this, TZ, prev.constant);
s[1] = new Element(prev);
rules[0]++;
return s;
} else if ((prev.type == RX) && (this.type == TZ)) {
// RX.TZ -> TY.RX
s[0] = new Element(this, TY, -prev.constant);
s[1] = new Element(prev);
rules[2]++;
return s;
} else if ((prev.type == RY) && (this.type == TX)) {
// RY.TX-> TZ.RY
s[0] = new Element(this, TZ, -prev.constant);
s[1] = new Element(prev);
rules[1]++;
return s;
} else if ((prev.type == RY) && (this.type == TZ)) {
// RY.TZ-> TX.RY
s[0] = new Element(this, TX, prev.constant);
s[1] = new Element(prev);
rules[11]++;
return s;
} else if ((prev.type == TY) && (this.type == RX)) {
// TY.RX -> RX.TZ
s[0] = new Element(this);
s[1] = new Element(prev, TZ, -this.constant);
rules[5]++;
//return s;
return null;
} else if ((prev.type == RY) && (this.type == RX)) {
// RY(Q).RX -> RX.RZ(-Q)
s[0] = new Element(this);
s[1] = new Element(prev, RZ, -1);
rules[3]++;
return s;
} else if ((prev.type == RX) && (this.type == RY)) {
// RX.RY -> RZ.RX
s[0] = new Element(this, RZ);
s[1] = new Element(prev);
rules[4]++;
return s;
} else if ((prev.type == RZ) && (this.type == RX)) {
// RZ.RX -> RX.RY
s[0] = new Element(this);
s[1] = new Element(prev, RY);
//rules[10]++;
//return s;
return null;
}
return null;
}
/*
* Element constructors. String is of the form:
*/
public Element(int type, int constant) {
this.type = type;
this.var = null;
this.symconst = null;
this.constant = constant;
}
public Element(int type) { // new of specified type
this.type = type;
}
public Element(Element e) { // clone of argument
this.type = e.type;
if (e.var != null)
this.var = new String(e.var);
if (e.symconst != null)
this.symconst = new String(e.symconst);
this.constant = e.constant;
}
/**
* Constructor for Element.
* @param e Template for new Element.
* @param type Replacement type for new Element.
* @param sign Sign of argument, either -1 or +1.
* @return a new Element with specified type and argument.
*/
public Element(Element e, int type, int sign) { // clone of argument with new type
this.type = type;
if (e.var != null)
this.var = new String(e.var);
this.constant = e.constant;
if (e.symconst != null)
this.symconst = new String(e.symconst);
if (sign < 0)
this.negate();
}
public Element(Element e, int type) { // clone of argument with new type
this(e, type, 1);
}
// negate the arguments of the element
public void negate() {
//System.out.println("negate: " + this.constant + " " + this.symconst);
// flip the numeric part, easy
this.constant = -this.constant;
if (this.symconst != null) {
StringBuffer s = new StringBuffer(this.symconst);
// if no leading sign character insert one (so we can flip it)
if ((s.charAt(0) != '+') &&
(s.charAt(0) != '-')
)
s.insert(0, '+');
// go through the string and flip all sign chars
for (int i=0; i<s.length(); i++)
switch (s.charAt(i)) {
case '+':
s.setCharAt(i, '-');
break;
case '-':
s.setCharAt(i, '+');
break;
default:
break;
}
if (s.charAt(0) == '+')
s.delete(0, 1);
this.symconst = new String(s);
}
//System.out.println("negate: " + this.constant + " " + this.symconst);
}
/**
* Parsing constructor.
* @param transform string expression, eg. Tx(q1) Rx(90) Ty(L2)
*
* where q1 represents a joint variable, L2 is a dimension.
*/
public Element(String s)
throws IllegalArgumentException { // constructor
int i;
String sType = s.substring(0,2); // Tx, Rx etc
String sRest = s.substring(2); // the argument including brackets
if (!(sRest.endsWith(")") && sRest.startsWith("(")))
throw(new IllegalArgumentException("brackets"));
for (i=0; i<6; i++)
if (sType.equalsIgnoreCase(typeName[i]))
break;
if (i >= 6)
throw(new IllegalArgumentException("bad transform name" + sType));
type = i;
sRest = sRest.substring(1, sRest.length()-1); // get the argument from between brackets
// handle an optional minus sign
String negative = "";
if (sRest.charAt(0) == '-') {
negative = "-";
sRest = sRest.substring(1);
}
switch (sRest.charAt(0)) {
case 'q':
var = negative + sRest;
break;
case 'L':
symconst = negative + sRest;
break;
default:
try {
constant = Integer.parseInt(sRest);
}
catch(NumberFormatException e) {
System.err.println(e.getMessage());
throw(new IllegalArgumentException("bad argument in term " + s));
}
}
}
/*
* Return a string representation of the parameters (argument)
* of the element, which can be a number, symbolic constant,
* or a joint variable.
*/
public String argString() {
String s = "";
switch (type) {
case RX:
case RY:
case RZ:
case TX:
case TY:
case TZ:
// Tx(L1-L3), symconst
// Rz(q1+180), var+constant
// Tx(L2), symconst
// Ry(90), constant
if (var != null)
s = var;
if (symconst != null) {
if (var != null)
if (symconst.charAt(0) != '-')
s += "+";
s += symconst;
}
// constants always displayed with a sign character
if (constant != 0)
s += (constant < 0 ? "" : "+") + constant;
break;
case DH_STANDARD:
case DH_MODIFIED:
// theta, d, a, alpha
// theta
if (prismatic == 0) {
s += var;
if (offset > 0)
s += "+" + offset;
else if (offset < 0)
s += offset;
} else
s += theta;
s += ", ";
// d
if (prismatic > 0)
s += var;
else
s += (D == null) ? "0" : D;
s += ", ";
// a
s += (A == null) ? "0" : A;
s += ", ";
// alpha
s += alpha;
break;
default:
throw new IllegalArgumentException("bad Element type");
}
return s;
}
/*
* Return a string representation of the element.
* eg. Rz(q1), Tx(L1), Rx(90), DH(....)
*/
public String toString() {
String s = typeName[type] + "(";
s += argString();
s += ")";
return s;
}
}
/**********************************************************************
/* A list of Elements. Subclass of Java's arrayList
*
* public int factorize(int dhWhich, int verbose)
*
* public int floatRight() {
* public int swap(int dhWhich) {
* public int substituteToZ() {
* public int substituteToZ2() {
* public int substituteY() {
* public int merge() {
* public void simplify() {
* public ElementList() { // constructor, use superclass
* public String toString() {
*/
@SuppressWarnings("serial")
class ElementList extends ArrayList<Element> {
/**
* Attempt to group this and subsequent elements into a DH term
* @return: the number of factors matched, zero means no DH term found
*
* Modifies the ElementList and compresses the terms.
*/
public int factorize(int dhWhich, int verbose) {
int match, jvars;
int i, j, f;
int nfactors = 0;
for (i=0; i<this.size(); i++) {
j = i;
jvars = match = 0;
for (f=0; f<4; f++) {
if (j >= this.size())
break;
Element e = this.get(j);
if ((f == 0) && (verbose > 0))
System.out.println("Starting at " + e);
Element ee = e;
ee.factorMatch(dhWhich, f, verbose);
e.factorMatch(dhWhich, f, verbose);
if (e.factorMatch(dhWhich, f, verbose)
) {
j++; // move on to next element
match++;
if (e.isjoint())
jvars++;
if (jvars > 1) // can only have 1 joint var per DH
break;
}
}
if ((match == 0) || (jvars == 0))
continue; // no DH subexpression found, keep looking
int start, end;
if (verbose > 0)
System.out.println(" found subexpression " + match + " " + jvars);
start = i;
end = j;
if (jvars > 1)
end--;
Element dh = new Element(dhWhich);
for (j=start; j<end; j++) {
dh.add( this.get(i) );
this.remove(i);
}
this.add(i, dh);
nfactors++;
if (verbose > 0)
System.out.println(" result: " + dh);
}
return nfactors;
}
/**
* Attempt to 'float' translational terms as far to the right as
* possible and across joint boundaries.
*/
public int floatRight() {
Element e, f = null;
int nchanges = 0;
int i, j;
boolean crossed;
for (i=0; i<(this.size()-1); i++) {
e = this.get(i);
if (e.isjoint())
continue;
if (!e.istrans())
continue;
f = null;
crossed = false;
for (j=i+1; j<(this.size()-1); j++) {
f = this.get(j);
if (f.istrans())
continue;
if (f.isrot() && (f.axis() == e.axis())) {
crossed = true;
continue;
}
break;
}
if (crossed && (f != null)) {
System.out.println("Float: " + e + " to " + f);
this.remove(i);
this.add(j-1, e);
nchanges++;
i--;
}
}
return nchanges;
}
/**
* Swap adjacent terms according to inbuilt rules so as to achieve
* desired term ordering.
*/
public int swap(int dhWhich) {
int total_changes = 0;
int nchanges = 0;
do {
nchanges = 0;
for (int i=0; i<(this.size()-1); i++) {
Element e = this.get(i);
if (e.swap( this.get(i+1), dhWhich)) {
this.remove(i);
this.add(i+1, e);
nchanges++;
}
}
total_changes += nchanges;
} while (nchanges > 0);
return total_changes;
}
/**
* substitute all non Z joint transforms according to rules.
*/
public int substituteToZ() {
Element[] replacement;
int nchanges = 0;
for (int i=0; i<this.size(); i++) {
Element e = this.get(i);
if (!e.isjoint())
continue;
replacement = e.substituteToZ();
if (replacement != null) {
// diagnostic string
System.out.print("ReplaceToZ: " + e + " := ");
for (int j=0; j<replacement.length; j++)
System.out.print(replacement[j]);
System.out.println();
this.remove(i);
for (int j=replacement.length-1; j>=0; j--)
this.add(i, replacement[j]);
i += replacement.length-1;
nchanges++;
}
}
return nchanges;
}
/**
* substitute all non Z joint transforms according to rules.
*/
public int substituteToZ2() {
Element e, prev;
Element[] replacement;
int nchanges = 0;
boolean jointYet = false;
for (int i=0; i<this.size(); i++) {
e = this.get(i);
if (e.isjoint())
jointYet = true;
if (e.isjoint())
continue;
if ((i == 0) || !jointYet) // leave initial const xform
continue;
prev = this.get(i-1);
//System.out.println("in ToZ2: " + e + " " + prev);
replacement = e.substituteToZ(prev);
if (replacement != null) {
// diagnostic string
System.out.print("ReplaceToZ2: " + e + " := ");
for (int j=0; j<replacement.length; j++)
System.out.print(replacement[j]);
System.out.println();
this.remove(i);
for (int j=replacement.length-1; j>=0; j--)
this.add(i, replacement[j]);
i += replacement.length-1;
nchanges++;
}
}
return nchanges;
}
/**
* substitute transforms according to rules.
*/
public int substituteY() {
Element[] replacement;
int nchanges = 0;
boolean jointYet = false;
Element next;
for (int i=1; i<this.size(); i++) {
Element e = this.get(i);
if (e.isjoint())
jointYet = true;
if ((i == 0) || !jointYet) // leave initial const xform
continue;
Element prev = this.get(i-1);
if ((i+1) < this.size())
next = this.get(i+1);
else
next = null;
replacement = e.substituteY(prev, next);
if (replacement != null) {
// diagnostic string
System.out.print("ReplaceY: " + prev + e + " := ");
for (int j=0; j<replacement.length; j++)
System.out.print(replacement[j]);
System.out.println();
this.remove(i);
this.remove(i-1);
for (int j=replacement.length-1; j>=0; j--)
this.add(i-1, replacement[j]);
i += replacement.length-2;
nchanges++;
}
}
return nchanges;
}
/**
* merge adjacent transforms according to rules.
*/
public int merge() {
int nchanges = 0;
for (int i=0; i<(this.size()-1); i++) {
Element e = this.get(i);
e = e.merge( this.get(i+1));
if (e == this.get(i))
continue;
this.remove(i);
this.remove(i);
if (e != null)
this.add(i, e);
nchanges++;
}
return nchanges;
}
/**
* simplify expression. Cycle continually around merging, substituting,
* and swapping until no more changes occur.
*/
public void simplify() {
int nchanges;
int nloops = 0;
/*
* simplify as much as possible, then subsitute for all
* joint variables not in Z.
*/
this.merge();
this.swap(Element.DH_STANDARD);
this.merge();
System.out.println(this);
this.floatRight();
this.merge();
System.out.println("initial merge + swap");
System.out.println(this);
this.substituteToZ();
this.merge();
System.out.println("joint vars to Z");
System.out.println(this);
System.out.println("0---------------------------------------");
do {
nchanges = 0;
nchanges += this.merge();
nchanges += this.swap(Element.DH_STANDARD);
nchanges += this.merge();
nchanges += this.substituteY();
nchanges += this.merge();
System.out.println(this);
System.out.println("1---------------------------------------");
if (nchanges == 0) {
System.out.println("** deal with Ry/Ty");
nchanges += this.substituteToZ2();
nchanges += this.merge();
}
} while ((nchanges > 0) && (nloops++ < 10));
}
/*
public ElementList() { // constructor, use superclass
super();
}
*/
public String toString() {
String s = "";
for (int i=0; i<this.size(); i++)
s += this.get(i) + (i < (this.size()-1) ? "." : "");
return s;
}
/*
static String jstr2offset(String s) {
int i;
if (s != null) {