/* parity.
Parity switching for Roller Coaster Problems
Alan Bundy 7.4.81 */


/*PARITY DEALING*/
/*--------------*/


/*PARTICLE IS INSIDE OR OUTSIDE OF CONCAVITY IN PATH*/ % Are these used?
inside(Part,Path,Time) :- wh_side(Part,Path,Par,Par,Time).

outside(Part,Path,Time) :- wh_side(Part,Path,Par1,Par2,Time),
  opposite(Par1,Par2).

/* Which side of path is particle on? */
wh_side(Part,Path,Ans,Nc,Time) :-
  dc cued(motion(Part,Path,Start,Side,Time)),
  dc end(Path,Start,Par),
  condval(Par,Side,Ans),
  dc concavity(Path,Conv), norm(Conv,Nc).

/* The inclinations of two paths at a shared point are either
identical or at 180 */

/* Ph1 is a subpath of Ph2 */
condturn2(Pt,Ph1,Ph2,Ang,Ang) :-
	dc partition(Ph2,PhList),
	member(Ph1,PhList), !.

/* Ph2 is a subpath of Ph1 */
condturn2(Pt,Ph1,Ph2,Ang,Ang) :-
	dc partition(Ph1,PhList),
	member(Ph2,PhList), !.

/* Ph1 and Ph2 join (smoothly?) at Pt */
condturn2(Pt,Ph1,Ph2,Ang1,Ang2) :- !,
  dc end(Ph1,Pt,Par1),
  dc end(Ph2,Pt,Par2),
  condval(Par,Par1,Par2),		% Par is right iff Par1 & 2 differ
  dc concavity(Ph1,Conv1), norm(Conv1,Nc1),
  dc concavity(Ph2,Conv2), norm(Conv2,Nc2),
  condval(Nc,Nc1,Nc2),			% Nc is right iff Nc1 & 2 differ
  condturn3(Par,Nc,Ang1,Ang2).		% not clear this is right

/* Angles are turned iff parities are right right */
condturn3(right,right,Ang1,Ang2) :- !,
	aboutturn(Ang1,Ang2).
condturn3(Par1,Par2,Ang,Ang) :- !.


/* Angles are turned iff parities are different */
condturn1(Par,Par,Ang,Ang) :- !.
condturn1(Par1,Par2,Ang1,Ang2) :-
  opposite(Par1,Par2), aboutturn(Ang1,Ang2).

/*NORMALIZE PARITIES*/
norm(stline,left) :- !.
norm(Par,Par).

/*CONDITIONAL REVERSE*/
condrev(left,List,List).

condrev(right,List,Rlist) :- rev(List,Rlist).

/*CONDITIONAL VALUE*/
condval(Par,threaded,threaded) :- !.
condval(left,Side,Side) :- !.
condval(right,Side1,Side2) :- !, opposite(Side1,Side2).

/*PARITY CHANGER*/
opposite(left,right).
opposite(right,left).


