Skip to content
Snippets Groups Projects
SphereFact.cs 3.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • Paul-Walcher's avatar
    Paul-Walcher committed
    using Newtonsoft.Json;
    using REST_JSON_API;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using UnityEngine;
    
    /// <summary>
    /// Point in 3D Space
    /// </summary>
    public class SphereFact : FactWrappedCRTP<SphereFact>
    {
    
    
        //used points
        public string PidM, PidT;
    
        public Vector3 M, T;
    
        public float Volume = 0.0F;
        public float Radius = 0.0F;
    
    
        public PointFact GetM {get =>  (PointFact)FactRecorder.AllFacts[PidM];}
        public PointFact GetT {get =>  (PointFact)FactRecorder.AllFacts[PidT];}
        protected void calculate_vectors(){
    
            M = ((PointFact)FactRecorder.AllFacts[PidM]).Point + Vector3.zero;
            T = ((PointFact)FactRecorder.AllFacts[PidT]).Point + Vector3.zero;
    
            Debug.Log(M);
            Debug.Log(T);
    
            Radius = Vector3.Distance(M, T);
            Volume = 4/3 * (Mathf.PI) * (Radius * Radius * Radius);
    
            float dist = 2*Radius;
            Vector3 scale = new Vector3(dist, dist, dist);
            
            LocalScale = scale;
    
            Position =  M;
            
        }
    
        public SphereFact() : base(){
            this.PidM = null;
            this.PidT = null;
        }
        [JsonConstructor]
        public SphereFact( string PidM, string PidT) : base()
        {
            this.PidM = PidM;
            this.PidT = PidT;
    
            calculate_vectors();
        }
    
        /// <summary>
        /// Bypasses initialization of new MMT %Fact by using existend URI, _which is not checked for existence_.
        /// <see cref="Normal"/> set to <c>Vector3.up</c>
        /// </summary>
        /// <param name="Point">sets <see cref="Point"/></param>
        /// <param name="ServerDefinition">MMT URI as OMS</param>
        public SphereFact(string PidM, string PidT, SOMDoc ServerDefinition) : base()
        {
            this.PidM = PidM;
            this.PidT = PidT;
    
            this.ServerDefinition = ServerDefinition;
    
            calculate_vectors();
        }
    
        /// \copydoc Fact.parseFact(ScrollFact)
        public new static IEnumerator parseFact(List<Fact> ret, MMTFact fact)
        {
            if (((MMTGeneralFact)fact).defines is not OMA df)
                yield break;
    
            OMS pointM, pointT;
    
            pointM = (OMS)df.arguments[0];
            pointT = (OMS)df.arguments[1];
    
            string PidM = pointM.uri;
            string PidT = pointT.uri;
    
            
    
            ret.Add(new SphereFact(PidM, PidT));
    
            //ParsingDictionary.parseTermsToId.TryAdd(defines.ToString(), fact.@ref.uri);
            //ret.Add(new PointFact(SOMDoc.MakeVector3(defines), fact.@ref));
        }
    
    
        /// \copydoc Fact.hasDependentFacts
        public override bool HasDependentFacts => true;
    
        /// \copydoc Fact.getDependentFactIds
        protected override string[] GetDependentFactIds()
            => new string[] { PidM, PidT};
    
        /// \copydoc Fact.GetHashCode
        /* public override int GetHashCode()
            => this.Point.GetHashCode();
        */
        protected override void RecalculateTransform()
        {
            calculate_vectors();
        }
        /// \copydoc Fact.Equivalent(Fact, Fact)
        protected override bool EquivalentWrapped(SphereFact f1, SphereFact f2){
            
            return (
    
                        Math3d.IsApproximatelyEqual(f1.M, f2.M)
                    && Math3d.IsApproximatelyEqual(f1.T, f2.T)
    
            );
    
        }
    
        public bool isEqual(SphereFact f2){
            return EquivalentWrapped(this, f2);
        }
    
        protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new){
            
            return new SphereFact(this.PidM, this.PidT);
    
        }
    
        public override MMTFact MakeMMTDeclaration()
        {
            SOMDoc tp = new OMS(MMTConstants.SphereType);
    
            return new MMTGeneralFact(_LastLabel, tp, Defines());
        }
    
        public override SOMDoc Defines()
            => new OMA(
                    new OMS(MMTConstants.SphereCons),
                    new[] {
                            new OMS(PidM),
                            new OMS(PidT),
                    }
                );
    }