Skip to content
Snippets Groups Projects
TestPointFact.cs 3.25 KiB
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 TestPointFact : FactWrappedCRTP<TestPointFact>
{
    /// <summary> Position </summary>
    public Vector3 Point;
    /// <summary> Orientation for <see cref="Fact.WorldRepresentation"/> </summary>
    [JsonProperty]
    private Vector3 Normal;

    /// <summary>
    /// Standard Constructor:
    /// Initiates <see cref="Point"/>, <see cref="Normal"/>, <see cref="Fact._URI"/> and creates MMT %Fact Server-Side
    /// </summary>
    /// <param name="Point">sets <see cref="Point"/></param>
    /// <param name="Normal">sets <see cref="Normal"/></param>
    [JsonConstructor]
    public TestPointFact(Vector3 Point, Vector3 Normal) : base()
    {
        this.Point = Point;
        this.Normal = Normal;
    }

    /// <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 TestPointFact(Vector3 Point, SOMDoc ServerDefinition) : base()
    {
        this.Point = Point;
        this.Normal = Vector3.up;
        this.ServerDefinition = ServerDefinition;
    }

    /// \copydoc Fact.parseFact(ScrollFact)
    public new static IEnumerator parseFact(List<Fact> ret, MMTFact fact)
    {
        if (((MMTGeneralFact)fact).defines is not OMA defines)
            yield break;
        ParsingDictionary.parseTermsToId.TryAdd(defines.ToString(), fact.@ref.uri);

        ret.Add(new TestPointFact(SOMDoc.MakeVector3(defines), fact.@ref));
    }

    protected override void RecalculateTransform()
    {
        Position = Point;
        { // Rotation
            Vector3 notNormal = Vector3.forward != Normal ? Vector3.forward : Vector3.up;
            Rotation = Quaternion.LookRotation(
                Vector3.Cross(Normal, notNormal),
                Normal
            );
        }
    }

    /// \copydoc Fact.hasDependentFacts
    public override bool HasDependentFacts => false;
    /// \copydoc Fact.getDependentFactIds
    protected override string[] GetDependentFactIds()
        => new string[] { };

    /// \copydoc Fact.GetHashCode
    public override int GetHashCode()
        => this.Point.GetHashCode();

    /// \copydoc Fact.Equivalent(Fact, Fact)
    protected override bool EquivalentWrapped(TestPointFact f1, TestPointFact f2)
        => Math3d.IsApproximatelyEqual(f1.Point, f2.Point);

    protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new)
        => new TestPointFact(this.Point, this.Normal);

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

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

    public override SOMDoc Defines()
        => new OMA(
                new OMS(MMTConstants.Tuple),
                new[] {
                        new OMLIT<float>(Point.x),
                        new OMLIT<float>(Point.y),
                        new OMLIT<float>(Point.z),
                }
            );
}