normalise

EXPRESS specification:

FUNCTION normalise
(* Modified for LPM/6 *)(arg : vector_or_direction) : vector_or_direction; LOCAL ndim : INTEGER; v : direction; result : vector_or_direction; vec : vector; mag : REAL; END_LOCAL; IF NOT EXISTS (arg) THEN result := ?; (* When function is called with invalid data a NULL result is returned *) ELSE ndim := arg.dim; IF 'STRUCTURAL_FRAME_SCHEMA.VECTOR' IN TYPEOF(arg) THEN BEGIN v := dummy_gri || direction(arg.orientation.direction_ratios); IF arg.magnitude = 0.0 THEN RETURN(?); ELSE vec := dummy_gri || vector (v, 1.0); END_IF; END; ELSE v := dummy_gri || direction (arg.direction_ratios); END_IF; mag := 0.0; REPEAT i := 1 TO ndim; mag := mag + v.direction_ratios[i]*v.direction_ratios[i]; END_REPEAT; IF mag > 0.0 THEN mag := SQRT(mag); REPEAT i := 1 TO ndim; v.direction_ratios[i] := v.direction_ratios[i]/mag; END_REPEAT; IF 'STRUCTURAL_FRAME_SCHEMA.VECTOR' IN TYPEOF(arg) THEN vec.orientation := v; result := vec; ELSE result := v; END_IF; ELSE RETURN(?); END_IF; END_IF; RETURN (result);
END_FUNCTION;