Skip to content
Snippets Groups Projects
CylinderFact.cs 4.51 KiB
Newer Older
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 CylinderFact : FactWrappedCRTP<CylinderFact>
{


    //used points
    public string PidM, PidE, PidT;
    public Vector3 M, E, T;
    public float Volume = 0.0F;
Paul-Walcher's avatar
Paul-Walcher committed
    public float Radius = 0.0F;
    public float Height = 0.0F;
Paul-Walcher's avatar
Paul-Walcher committed


    public PointFact GetM {get =>  (PointFact)FactRecorder.AllFacts[PidM];}
    public PointFact GetE {get =>  (PointFact)FactRecorder.AllFacts[PidE];}
    public PointFact GetT {get =>  (PointFact)FactRecorder.AllFacts[PidT];}

    protected void calculate_vectors(){

        M = ((PointFact)FactRecorder.AllFacts[PidM]).Point + Vector3.zero;
        E = ((PointFact)FactRecorder.AllFacts[PidE]).Point + Vector3.zero;
        T = ((PointFact)FactRecorder.AllFacts[PidT]).Point + Vector3.zero;
        //Rotation = Quaternion.LookRotation(forward, new Vector3(1.0F, 0.0F, 0.0F));

Paul-Walcher's avatar
Paul-Walcher committed
        Position = T;
        Radius = Vector3.Distance(M, E);
Paul-Walcher's avatar
Paul-Walcher committed
        Height = Vector3.Distance(T, M);

        LocalScale = new Vector3(Radius*2 + 0.1F, Height + 0.1F, Radius*2 + 0.1F);
Paul-Walcher's avatar
Paul-Walcher committed

Paul-Walcher's avatar
Paul-Walcher committed
        Vector3 bottomnormal = (T - M).normalized;
        Vector3 rightvector = (E - M).normalized;
Paul-Walcher's avatar
Paul-Walcher committed

Paul-Walcher's avatar
Paul-Walcher committed
        Rotation = Quaternion.LookRotation(rightvector, bottomnormal);
Paul-Walcher's avatar
Paul-Walcher committed

        Volume = Mathf.PI * Radius * Radius * Height;

Paul-Walcher's avatar
Paul-Walcher committed
        
    }

    public CylinderFact() : base(){
        this.PidM = null;
        this.PidE = null;
        this.PidT = null;
    }
    [JsonConstructor]
    public CylinderFact( string PidM, string PidE, string PidT) : base()
    {
        this.PidM = PidM;
        this.PidE = PidE;
        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 CylinderFact(string PidM, string PidE, string PidT, SOMDoc ServerDefinition) : base()
    {
        this.PidM = PidM;
        this.PidE = PidE;
        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, pointE, pointT;

        pointM = (OMS)df.arguments[0];
        pointE = (OMS)df.arguments[1];
        pointT = (OMS)df.arguments[2];

        string PidM = pointM.uri;
        string PidE = pointE.uri;
        string PidT = pointT.uri;

        

        ret.Add(new CylinderFact(PidM, PidE, 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, PidE, 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(CylinderFact f1, CylinderFact f2){
        
        return (

                    Math3d.IsApproximatelyEqual(f1.M, f2.M)
                && Math3d.IsApproximatelyEqual(f1.E, f2.E)
                && Math3d.IsApproximatelyEqual(f1.T, f2.T)

        );

    }

    public bool isEqual(CylinderFact f2){
        return EquivalentWrapped(this, f2);
    }

    protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new){
        
        return new CylinderFact(this.PidM, this.PidE, this.PidT);

    }

    public override MMTFact MakeMMTDeclaration()
    {
        SOMDoc tp = new OMS(MMTConstants.CylinderType);

        return new MMTGeneralFact(_LastLabel, tp, Defines());
    }

    public override SOMDoc Defines()
        => new OMA(
                new OMS(MMTConstants.CylinderCons),
                new[] {
                        new OMS(PidM),
                        new OMS(PidE),
                        new OMS(PidT),
                }
            );
}