/* CTMEAS : Creation of Continuous Measure Systems
 
						Updated: 1st April 1981
*/

			% continuous measure system already known

cont_meas(Obj,X0,Axis,Fibre,A,B) :- 
	cont_meas1(Obj,X0,Axis,Fibre,A,B).


			% make continuous measure system

cont_meas(Obj,X0,axis(Y),Fibre,A,B) :-
	get_defn(Obj,origin,Defn1), 
	sel(Defn1, A =< X =< B, VAR=X0, Defn2),
	uniform(X,Obj),
	gensym(X,X0),
	subst(X=X0,Defn2,Defn3),	% replace any Xs on right of =
	VAR = X,			% put in the X on the left of =
	move_origin(Y,X,X0,Defn3,origin,Defn4,NewOrigin),
	rec_defn(Fibre,NewOrigin,Defn4),
	assert( const(X0) ),		% hack to prevent X0 being solved for!
	assert(cont_meas1(Obj,X0,axis(Y),Fibre,A,B)).



			% Find definition of object

get_defn(Obj,Origin,Defn) :-
	type(Shape,Obj),
	is_defn(Shape,Obj,Origin,Defn,Relns),
	checklist(ncc,Relns).


 			% Recognise definition of object

rec_defn(Obj,Origin,Defn) :-
	is_defn(Shape,Obj,Origin,Defn,Relns),
	gensym(Shape,Obj),
	checklist(fn_dbentry,Relns),
	trace('\n %t is new fibre defined by %l\n', [Obj,Relns], 2).


			% Test for uniform fibre

uniform(X,_) :- distance_coord(X), !.	% either parameter is not angle

uniform(_,Obj) :-  ncc body1d(Obj), !.	% or fibre is 0 dimensional


			% Types of coordinate

distance_coord(x).
distance_coord(y).
distance_coord(z).
distance_coord(r).

angle_coord(theta).
angle_coord(phi).


/* Move coordinate system if axis of rotation not
perpendicular to continuous measure coordinate */

move_origin(Y,X,X0,Defn,Orgn,Defn,Orgn).	% they are perpendicular

move_origin(X,X,X0,ODefn,OOrgn,NDefn,NOrgn) :- 
	sel(ODefn,X=X0,X=0,NDefn),		% axis and fibre coord
	ncc tangent(axis(X),Dir),		% are the same
	define_orgn(X0,Dir,OOrgn,NOrgn).


move_origin(rr,C,C0,ODefn,OOrgn,NDefn,NOrgn) :-
	ncc tangent(axis(rr), [Alpha,0]),
	get_incr(C,C0,Alpha,X0,Y0,R0),
	update_defns(X0,Y0,ODefn,NDefn),
	define_orgn(R0,[Alpha,0],OOrgn,NOrgn).

/* Get x, y and r increments of coordinate change */
get_incr(x,X0,Alpha,X0,X0/tan(Alpha),X0/cos(Alpha)).
get_incr(y,Y0,Alpha,Y0*tan(Alpha),Y0,Y0/sin(Alpha)).


/* Update definitions by replacing old coordinates by new */

update_defns(X0,Y0,OXineq & OYineq & Zineq, NXineq & NYineq & Zineq) :-
	update_ineq(X0,OXineq,NXineq),
	update_ineq(Y0,OYineq,NYineq).

update_ineq(C0, C=V, C=V1) :-
	poly_form(V+(-1)*C0,V1).

update_ineq(C0, A=<C=<B, A1=<C=<B1) :-
	poly_form(A+(-1)*C0,A1), poly_form(B+(-1)*C0,B1).


/* assert definitions of new origin */

define_orgn(R0,Dir,OOrgn,NOrgn) :-
	gensym(origin,NOrgn),
	fn_dbentry( on(NOrgn,axis(rr)) ),
	fn_dbentry( separation(OOrgn,NOrgn,R0,Dir) ).



