Skip to content
Snippets Groups Projects
Fact.cs 45.5 KiB
Newer Older
  • Learn to ignore specific revisions
  •     /// \copydoc Fact.s_type
        public new string s_type = "AngleFact";
    
    
        /// @{ <summary>
        /// One <see cref="Fact.Id">Id</see> of three <see cref="PointFact">PointFacts</see> defining Angle [<see cref="Pid1"/>, <see cref="Pid2"/>, <see cref="Pid3"/>].
        /// </summary>
    
        public string Pid1, Pid2, Pid3;
    
        /// @}
    
        /// <summary> <see langword="true"/>, if AngleFact is approximately 90° or 270°</summary>
    
        public bool is_right_angle;
    
    
        /// <summary> \copydoc Fact.Fact </summary>
    
        public AngleFact() : base()
        {
            this.Pid1 = null;
            this.Pid2 = null;
            this.Pid3 = null;
            this.is_right_angle = false;
        }
    
    
        /// <summary>
        /// Copies <paramref name="fact"/> by initiating new MMT %Fact.
        /// </summary>
        /// <param name="fact">Fact to be copied</param>
        /// <param name="old_to_new"><c>Dictionary</c> mapping <paramref name="fact"/>.<see cref="getDependentFactIds"/> in <paramref name="fact"/>.<see cref="Fact._Facts"/> to corresponding <see cref="Fact.Id"/> in <paramref name="organizer"/> </param>
        /// <param name="organizer">sets <see cref="_Facts"/></param>
    
        public AngleFact(AngleFact fact, Dictionary<string, string> old_to_new, FactOrganizer organizer) : base(fact, organizer)
        {
            init(old_to_new[fact.Pid1], old_to_new[fact.Pid2], old_to_new[fact.Pid3]);
        }
    
    
        /// <summary>
        /// Standard Constructor
        /// </summary>
        /// <param name="pid1">sets <see cref="Pid1"/></param>
        /// <param name="pid2">sets <see cref="Pid2"/></param>
        /// <param name="pid3">sets <see cref="Pid3"/></param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
    
        public AngleFact(string pid1, string pid2, string pid3, FactOrganizer organizer) : base(organizer)
    
        /// <summary>
        /// Initiates <see cref="Pid1"/>, <see cref="Pid2"/>, <see cref="Pid3"/>, <see cref="is_right_angle"/>, <see cref="Fact._URI"/> and creates MMT %Fact Server-Side
        /// </summary>
        /// <param name="pid1">sets <see cref="Pid1"/></param>
        /// <param name="pid2">sets <see cref="Pid2"/></param>
        /// <param name="pid3">sets <see cref="Pid3"/></param>
    
        private void init(string pid1, string pid2, string pid3)
    
            this.Pid1 = pid1;
            this.Pid2 = pid2;
            this.Pid3 = pid3;
    
            PointFact pf1 = _Facts[pid1] as PointFact;
            PointFact pf2 = _Facts[pid2] as PointFact;
            PointFact pf3 = _Facts[pid3] as PointFact;
    
            float v = GetAngle(); // sets is_right_angle
    
            string p1URI = pf1.Id;
            string p2URI = pf2.Id;
            string p3URI = pf3.Id;
    
            if (is_right_angle)
    
                mmtDecl = generate90DegreeAngleDeclaration(v, p1URI, p2URI, p3URI);
            else
                mmtDecl = generateNot90DegreeAngleDeclaration(v, p1URI, p2URI, p3URI);
    
            AddFactResponse.sendAdd(mmtDecl, out this._URI);
    
        /// <summary>
        /// Bypasses initialization of new MMT %Fact by using existend URI, _which is not checked for existence_.
        /// </summary>
        /// <param name="Pid1">sets <see cref="Pid1"/></param>
        /// <param name="Pid2">sets <see cref="Pid2"/></param>
        /// <param name="Pid3">sets <see cref="Pid3"/></param>
        /// <param name="backendURI">MMT URI</param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
    
        public AngleFact(string Pid1, string Pid2, string Pid3, string backendURI, FactOrganizer organizer) : base(organizer)
    
        {
            this.Pid1 = Pid1;
            this.Pid2 = Pid2;
            this.Pid3 = Pid3;
    
    
            GetAngle();
    
            this._URI = backendURI;
    
        /// \copydoc Fact.parseFact(Scroll.ScrollFact)
        public new static AngleFact parseFact(Scroll.ScrollFact fact)
    
            string uri = fact.@ref.uri;
            string
                pointAUri,
                pointBUri,
                pointCUri;
    
    
            //If angle is not a 90Degree-Angle
            if (fact.GetType().Equals(typeof(Scroll.ScrollValueFact)))
            {
                pointAUri = ((OMS)((OMA)((Scroll.ScrollValueFact)fact).lhs).arguments[0]).uri;
                pointBUri = ((OMS)((OMA)((Scroll.ScrollValueFact)fact).lhs).arguments[1]).uri;
                pointCUri = ((OMS)((OMA)((Scroll.ScrollValueFact)fact).lhs).arguments[2]).uri;
            }
            //If angle is a 90Degree-Angle
            else {
                pointAUri = ((OMS)((OMA)((OMA)((OMA)((Scroll.ScrollSymbolFact)fact).tp).arguments[0]).arguments[1]).arguments[0]).uri;
                pointBUri = ((OMS)((OMA)((OMA)((OMA)((Scroll.ScrollSymbolFact)fact).tp).arguments[0]).arguments[1]).arguments[1]).uri;
                pointCUri = ((OMS)((OMA)((OMA)((OMA)((Scroll.ScrollSymbolFact)fact).tp).arguments[0]).arguments[1]).arguments[2]).uri;
            }
    
    
            if (StageStatic.stage.factState.ContainsKey(pointAUri)
             && StageStatic.stage.factState.ContainsKey(pointBUri)
             && StageStatic.stage.factState.ContainsKey(pointCUri))
    
                return new AngleFact(pointAUri, pointBUri, pointCUri, uri, StageStatic.stage.factState);
    
    
            else    //If dependent facts do not exist return null
                return null;
    
        protected override string generateLabel()
        {
    
            return (is_right_angle ? "⊾" : "∠") + _Facts[Pid1].Label + _Facts[Pid2].Label + _Facts[Pid3].Label;
    
        /// <summary>
        /// Computes smallest angle and sets <see cref="is_right_angle"/>
        /// </summary>
        /// <returns>Smallets angle between [<see cref="Pid1"/>, <see cref="Pid2"/>] and [<see cref="Pid2"/>, <see cref="Pid3"/>]</returns>
    
        private float GetAngle()
        {
    
            PointFact pf1 = _Facts[Pid1] as PointFact;
            PointFact pf2 = _Facts[Pid2] as PointFact;
            PointFact pf3 = _Facts[Pid3] as PointFact;
    
    
            float v = Vector3.Angle((pf1.Point - pf2.Point), (pf3.Point - pf2.Point));
            this.is_right_angle = Mathf.Abs(v - 90.0f) < 0.01;
    
            return is_right_angle ? 90f : v;
        }
    
    
        /// <summary>
        /// Constructs struct for right-angled MMT %Fact <see cref="AddFactResponse"/>
        /// </summary>
        /// <param name="val">Angle == 90f, _not checked_</param>
        /// <param name="p1URI"><see cref="Pid1"/></param>
        /// <param name="p2URI"><see cref="Pid2"/></param>
        /// <param name="p3URI"><see cref="Pid3"/></param>
        /// <returns>struct for <see cref="AddFactResponse"/></returns>
    
        private MMTDeclaration generate90DegreeAngleDeclaration(float val, string p1URI, string p2URI, string p3URI) {
    
            MMTTerm argument = new OMA(
                new OMS(MMTURIs.Eq),
                new List<MMTTerm> {
                    new OMS(MMTURIs.RealLit),
                    new OMA(
                        new OMS(MMTURIs.Angle),
                        new List<MMTTerm> {
                            new OMS(p1URI),
                            new OMS(p2URI),
                            new OMS(p3URI)
                        }
                    ),
    
                    new OMF(val) // 90f
    
                }
            );
            
            MMTTerm tp = new OMA(new OMS(MMTURIs.Ded), new List<MMTTerm> {argument});
            MMTTerm df = null;
    
            return new MMTSymbolDeclaration(this.Label, tp, df);
        }
    
    
        /// <summary>
        /// Constructs struct for not-right-angled MMT %Fact <see cref="AddFactResponse"/>
        /// </summary>
        /// <param name="val">Angle != 90f, _not checked_</param>
        /// <param name="p1URI"><see cref="Pid1"/></param>
        /// <param name="p2URI"><see cref="Pid2"/></param>
        /// <param name="p3URI"><see cref="Pid3"/></param>
        /// <returns>struct for <see cref="AddFactResponse"/></returns>
    
        private MMTDeclaration generateNot90DegreeAngleDeclaration(float val, string p1URI, string p2URI, string p3URI)
        {
            MMTTerm lhs =
                new OMA(
                    new OMS(MMTURIs.Angle),
                    new List<MMTTerm> {
                        new OMS(p1URI),
                        new OMS(p2URI),
                        new OMS(p3URI)
                    }
                );
    
            MMTTerm valueTp = new OMS(MMTURIs.RealLit);
            MMTTerm value = new OMF(val);
            
            return new MMTValueDeclaration(this.Label, lhs, valueTp, value);
        }
    
        public override Boolean hasDependentFacts()
        {
            return true;
        }
    
    
        /// \copydoc Fact.getDependentFactIds
    
        public override string[] getDependentFactIds()
    
            return new string[] { Pid1, Pid2, Pid3 };
    
        /// \copydoc Fact.instantiateDisplay(GameObject, Transform)
    
    John Schihada's avatar
    John Schihada committed
        public override GameObject instantiateDisplay(GameObject prefab, Transform transform) {
    
    John Schihada's avatar
    John Schihada committed
            var obj = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity, transform);
    
            obj.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>().text = _Facts[this.Pid1].Label;
            obj.transform.GetChild(1).gameObject.GetComponent<TextMeshProUGUI>().text = _Facts[this.Pid2].Label;
            obj.transform.GetChild(2).gameObject.GetComponent<TextMeshProUGUI>().text = _Facts[this.Pid3].Label;
    
    John Schihada's avatar
    John Schihada committed
            obj.GetComponent<FactWrapper>().fact = this;
            return obj;
        }
    
    
            return this.Pid1.GetHashCode() ^ this.Pid2.GetHashCode() ^ this.Pid3.GetHashCode();
    
        /// \copydoc Fact.Equivalent(Fact, Fact)
    
        protected override bool EquivalentWrapped(AngleFact f1, AngleFact f2)
    
            if ((f1.Pid1 == f2.Pid1 && f1.Pid2 == f2.Pid2 && f1.Pid3 == f2.Pid3))// || 
                //(f1.Pid1 == f2.Pid3 && f1.Pid2 == f2.Pid2 && f1.Pid3 == f2.Pid1))
                return true;
    
    
            PointFact p1f1 = (PointFact)_Facts[f1.Pid1];
            PointFact p2f1 = (PointFact)_Facts[f1.Pid2];
            PointFact p3f1 = (PointFact)_Facts[f1.Pid3];
            PointFact p1f2 = (PointFact)_Facts[f2.Pid1];
            PointFact p2f2 = (PointFact)_Facts[f2.Pid2];
            PointFact p3f2 = (PointFact)_Facts[f2.Pid3];
    
    
            return (p1f1.Equivalent(p1f2) && p2f1.Equivalent(p2f2) && p3f1.Equivalent(p3f2))
                ;//|| (p1f1.Equivalent(p3f2) && p2f1.Equivalent(p2f2) && p1f1.Equivalent(p3f2));