Skip to content
Snippets Groups Projects
UnsortedFact.cs 18.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • MaZiFAU's avatar
    MaZiFAU committed
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    using Newtonsoft.Json;
    using REST_JSON_API;
    using System.Collections.Generic;
    using System.Linq;
    using System;
    using UnityEngine;
    using System.Collections;
    
    /// <summary>
    /// Point in 3D Space
    /// </summary>
    public class PointFact : FactWrappedCRTP<PointFact>
    {
        /// <summary> Position </summary>
        public Vector3 Point;
        /// <summary> Orientation for <see cref="Fact.WorldRepresentation"/> </summary>
        [JsonProperty]
        private Vector3 Normal;
    
        /// <summary> \copydoc Fact.Fact </summary>
        public PointFact() : base()
        {
            this.Point = Vector3.zero;
            this.Normal = Vector3.up;
        }
    
        /// <summary>
        /// Standard Constructor:
        /// Initiates <see cref="Point"/>, <see cref="Normal"/>, <see cref="Fact._URI"/> and creates MMT %Fact Server-Side
        /// </summary>
        /// <param name="P">sets <see cref="Point"/></param>
        /// <param name="N">sets <see cref="Normal"/></param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
        public PointFact(Vector3 P, Vector3 N, FactRecorder organizer) : base(organizer)
        {
            this.Point = P;
            this.Normal = N;
        }
    
        protected override void RecalculateTransform()
        {
            Position = Point;
            { // Rotation
                Vector3 notNormal = Vector3.forward != Normal ? Vector3.forward : Vector3.up;
                Rotation = Quaternion.LookRotation(
                    Vector3.Cross(Normal, notNormal),
                    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="uri">MMT URI</param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
        public PointFact(Vector3 point, SOMDoc _ServerDefinition, FactRecorder organizer) : base(organizer)
        {
            this.Point = point;
            this.Normal = Vector3.up;
            this.ServerDefinition = _ServerDefinition;
            _ = this.Label;
        }
    
        /// \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);
    
            Vector3 point = SOMDoc.MakeVector3(defines);
            ret.Add(new PointFact(point, fact.@ref, StageStatic.stage.factState));
        }
    
        /// \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(PointFact f1, PointFact f2)
            => Math3d.IsApproximatelyEqual(f1.Point, f2.Point);
    
        protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactRecorder organizer)
            => new PointFact(this.Point, this.Normal, organizer);
    
        public override MMTFact MakeMMTDeclaration()
        {
            SOMDoc tp = new OMS(MMTConstants.Point);
    
            return new MMTGeneralFact(Label, 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),
                    }
                );
    }
    
    /// <summary>
    /// A <see cref="PointFact"/> on a <see cref="AbstractLineFact"/>
    /// </summary>
    public class OnLineFact : FactWrappedCRTP<OnLineFact>
    {
        /// <summary> <see cref="PointFact"/>.<see cref="Fact.Id">Id</see> </summary>
        public string Pid;
        [JsonIgnore]
        public PointFact Point { get => (PointFact)FactRecorder.AllFacts[Pid]; }
    
        /// <summary> <see cref="AbstractLineFact"/>.<see cref="Fact.Id">Id</see> </summary>
        public string Rid;
        [JsonIgnore]
        public AbstractLineFact Ray { get => (AbstractLineFact)FactRecorder.AllFacts[Rid]; }
    
        /// <summary> \copydoc Fact.Fact </summary>
        public OnLineFact() : base()
        {
            this.Pid = null;
            this.Rid = null;
        }
    
        /// <summary>
        /// Standard Constructor:
        /// Initiates <see cref="Pid"/>, <see cref="Rid"/>, <see cref="Fact._URI"/> and creates MMT %Fact Server-Side
        /// </summary>
        /// <param name="pid">sets <see cref="Pid"/></param>
        /// <param name="rid">sets <see cref="Rid"/></param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
        public OnLineFact(string pid, string rid, FactRecorder organizer) : base(organizer)
        {
            this.Pid = pid;
            this.Rid = rid;
        }
    
        protected override void RecalculateTransform()
        {
            Position = Point.Position;
            { //Rotation
                Vector3 up = Point.Rotation * Vector3.up;
                Vector3 forward = Ray.Dir;
    
                if (Math3d.IsApproximatelyEqual(up, forward))
                {
                    Vector3 arbitary = Math3d.IsApproximatelyEqual(forward, Vector3.forward)
                        ? Vector3.right
                        : Vector3.forward;
    
                    up = Vector3.Cross(arbitary, forward);
                }
    
                Rotation = Quaternion.LookRotation(forward, up);
            }
        }
    
        /// <summary>
        /// Bypasses initialization of new MMT %Fact by using existend URI, _which is not checked for existence_.
        /// </summary>
        /// <param name="pid">sets <see cref="Pid"/></param>
        /// <param name="rid">sets <see cref="Rid"/></param>
        /// <param name="uri">MMT URI</param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
        public OnLineFact(string pid, string rid, SOMDoc _ServerDefinition, FactRecorder organizer) : base(organizer)
        {
            this.Pid = pid;
            this.Rid = rid;
            this.ServerDefinition = _ServerDefinition;
            _ = this.Label;
        }
    
        /// \copydoc Fact.parseFact(ScrollFact)
        public new static IEnumerator parseFact(List<Fact> ret, MMTFact fact)
        {
            string pointUri = ((OMS)((OMA)((OMA)((MMTGeneralFact)fact).type).arguments[0]).arguments[1]).uri;
            string lineUri = ((OMA)((OMA)((MMTGeneralFact)fact).type).arguments[0]).arguments[0] is OMS
                // standard case
                ? ((OMS)((OMA)((OMA)((MMTGeneralFact)fact).type).arguments[0]).arguments[0]).uri
                // case when line Uri has a projl on the line Argument 
                : ((OMS)((OMA)((OMA)((OMA)((MMTGeneralFact)fact).type).arguments[0]).arguments[0]).arguments[0]).uri;
    
            if (!FactRecorder.AllFacts.ContainsKey(pointUri)
             || !FactRecorder.AllFacts.ContainsKey(lineUri))
                yield break;
    
            ret.Add(new OnLineFact(pointUri, lineUri, fact.@ref, StageStatic.stage.factState));
        }
    
        /// \copydoc Fact.generateLabel
        protected override string generateLabel()
            => Point.Label + "∈" + Ray.Label;
    
        /// \copydoc Fact.hasDependentFacts
        public override bool HasDependentFacts => true;
    
        /// \copydoc Fact.getDependentFactIds
        protected override string[] GetDependentFactIds()
            => new string[] { Pid, Rid };
    
        protected override bool EquivalentWrapped(OnLineFact f1, OnLineFact f2)
            => DependentFactsEquivalent(f1, f2);
    
        protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactRecorder organizer)
            => new OnLineFact(old_to_new[this.Pid], old_to_new[this.Rid], organizer);
    
        public override MMTFact MakeMMTDeclaration()
        {
            SOMDoc tp = new OMA(
                new OMS(MMTConstants.Ded),
                new[] {
                    new OMA(
                        new OMS(MMTConstants.OnLine),
                        new[]  {
                            new OMS(Rid),
                            new OMS(Pid)
            }),});
    
            return new MMTGeneralFact(this.Label, tp, Defines());
        }
    
        public override SOMDoc Defines() => null;
    }
    
    /// <summary>
    /// Two parallel Lines comprised of two <see cref="LineFact">LineFacts</see> 
    /// </summary>
    public class ParallelLineFact : FactWrappedCRTP<ParallelLineFact>
    {
        /// @{ <summary>
        /// One <see cref="Fact.Id">Id</see> of two <see cref="LineFact"/> that are parallel [<see cref="Lid1"/>, <see cref="Lid2"/>].
        /// </summary>
        public string Lid1, Lid2;
        /// @}
    
        [JsonIgnore]
        public AbstractLineFact Ray1 { get => (AbstractLineFact)FactRecorder.AllFacts[Lid1]; }
        [JsonIgnore]
        public AbstractLineFact Ray2 { get => (AbstractLineFact)FactRecorder.AllFacts[Lid2]; }
    
        /// <summary> \copydoc Fact.Fact </summary>
        public ParallelLineFact() : base()
        {
            this.Lid1 = null;
            this.Lid2 = null;
        }
    
        /// <summary>
        /// Standard Constructor
        /// </summary>
        /// <param name="lid1">sets <see cref="Lid1"/></param>
        /// <param name="lid2">sets <see cref="Lid2"/></param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
        public ParallelLineFact(string lid1, string lid2, FactRecorder organizer) : base(organizer)
        {
            this.Lid1 = lid1;
            this.Lid2 = lid2;
        }
    
        protected override void RecalculateTransform() { }
    
        /// <summary>
        /// Bypasses initialization of new MMT %Fact by using existend URI, _which is not checked for existence_.
        /// </summary>
        /// <param name="Lid1">sets <see cref="Lid1"/></param>
        /// <param name="Lid2">sets <see cref="Lid2"/></param>
        /// <param name="backendURI">MMT URI</param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
        public ParallelLineFact(string Lid1, string Lid2, SOMDoc _ServerDefinition, FactRecorder organizer) : base(organizer)
        {
            this.Lid1 = Lid1;
            this.Lid2 = Lid2;
    
            this.ServerDefinition = _ServerDefinition;
            _ = this.Label;
        }
    
        /// \copydoc Fact.parseFact(ScrollFact)
        public new static IEnumerator parseFact(List<Fact> ret, MMTFact fact)
        {
            if (((MMTGeneralFact)fact).type is not OMA type) // proof DED
                yield break;
    
            OMA parallel_lines_OMA = (OMA)type.arguments[0]; // parallel
    
            string lineAUri = ((OMS)parallel_lines_OMA.arguments[0]).uri;
            string lineBUri = ((OMS)parallel_lines_OMA.arguments[1]).uri;
    
            if (!FactRecorder.AllFacts.ContainsKey(lineAUri)
             || !FactRecorder.AllFacts.ContainsKey(lineBUri))
                yield break;
    
            ret.Add(new ParallelLineFact(lineAUri, lineBUri, fact.@ref, StageStatic.stage.factState));
        }
    
        /// \copydoc Fact.generateLabel
        protected override string generateLabel()
            => Ray1.Label + "||" + Ray2.Label;
    
        public override MMTFact MakeMMTDeclaration()
        {
            SOMDoc tp = new OMA(
                new OMS(MMTConstants.Ded),
                new[] {
                    new OMA(
                        new OMS(MMTConstants.ParallelLine),
                        new[]  {
                            new OMS(Lid1),
                            new OMS(Lid2),
                        }
                    ),
                }
            );
    
            return new MMTGeneralFact(this.Label, tp, Defines());
        }
    
        public override SOMDoc Defines() => null;
    
        /// \copydoc Fact.hasDependentFacts
        public override bool HasDependentFacts => true;
    
        /// \copydoc Fact.getDependentFactIds
        protected override string[] GetDependentFactIds()
            => new string[] { Lid1, Lid2 };
    
        /// \copydoc Fact.Equivalent(Fact, Fact)
        protected override bool EquivalentWrapped(ParallelLineFact f1, ParallelLineFact f2)
            => DependentFactsEquivalent(f1, f2);
    
        protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactRecorder organizer)
            => new ParallelLineFact(old_to_new[this.Lid1], old_to_new[this.Lid2], organizer);
    }
    
    /// <summary>
    /// Used for BouncingScroll
    /// </summary>
    public class QuadFact : FactWrappedCRTP<QuadFact>
    {
        /// <summary> Defining Corners; Order is Cyclic;
        /// <see cref="PointFact"/>.<see cref="Fact.Id">Id</see> </summary>
        public string[] Pids;
        [JsonIgnore]
        public PointFact[] Points
        {
            get => _Points ??= Pids.Select(pid => (PointFact)FactRecorder.AllFacts[pid]).ToArray();
        }
        private PointFact[] _Points;
    
        [JsonIgnore]
        public Vector3 Normal, Tangents, AltTangents;
    
        /// <summary> \copydoc Fact.Fact </summary>
        public QuadFact() : base() { }
    
        /// <summary>
        /// Standard Constructor:
        /// Initiates <see cref="Pids"/> and creates MMT %Fact Server-Side
        /// </summary>
        /// <param name="pid_corners">sets <see cref="Pids"/></param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
        public QuadFact(string[] pid_corners, FactRecorder organizer) : base(organizer)
        {
            Init(pid_corners);
        }
    
        /// <summary>
        /// Bypasses initialization of new MMT %Fact by using existend URI, _which is not checked for existence_.
        /// </summary>
        /// <param name="pid_corners">sets <see cref="Pids"/></param>
        /// <param name="uri">MMT URI</param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
        public QuadFact(string[] pid_corners, SOMDoc _ServerDefinition, FactRecorder organizer) : base(organizer)
        {
            Init(pid_corners);
    
            this.ServerDefinition = _ServerDefinition;
            _ = this.Label;
        }
    
        private void Init(string[] pid_corners)
        {
            Pids = pid_corners;
    
            Tangents = (Points[0].Point - Points[1].Point).normalized;
            AltTangents = (Points[0].Point - Points[2].Point).normalized;
            Normal = Vector3.Cross(Tangents, AltTangents).normalized;
    
            if (Math3d.vectorPrecission < Math.Abs(Vector3.Dot(Normal, Points[0].Point - Points[3].Point)))
                throw new ArgumentException("All Points must lie on the same Plane!");
        }
    
        /// \copydoc Fact.parseFact(ScrollFact)
        public new static IEnumerator parseFact(List<Fact> ret, MMTFact fact)
        {
            throw new NotImplementedException();
        }
    
        public override MMTFact MakeMMTDeclaration()
        {
            SOMDoc tp = new OMS(MMTConstants.Wall);
    
            return new MMTGeneralFact(Label, tp, Defines());
        }
    
        public override SOMDoc Defines()
            => new OMA(
                    new OMS(MMTConstants.CreateWall),
                    Pids.Select(pid => new OMS(pid)).ToArray()
                );
    
        protected override bool EquivalentWrapped(QuadFact f1, QuadFact f2)
        {
            if (f1.Points.Length != f2.Points.Length)
                return false;
    
            int i = 0;
            for (; i < f2.Points.Length; i++) // 1st incedence
                if (f2.Points[i].Equivalent(f1.Points[0]))
                    break;
    
            if (i == f2.Points.Length) // no match
                return false;
    
            for (int j = 1; j < f2.Points.Length; j++) // cyclic match
                if (!f2.Points[(i + j) % f2.Points.Length].Equivalent(f1.Points[j]))
                    return false;
    
            return true;
        }
    
        public override bool HasDependentFacts => true;
    
        protected override string[] GetDependentFactIds()
            => Pids;
    
        protected override void RecalculateTransform() { }
    
        protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactRecorder organizer)
            => new QuadFact(Pids.Select(pid => old_to_new[pid]).ToArray(), organizer);
    }
    
    /// <summary>
    /// Used for BouncingScroll
    /// </summary>
    public class TriangleFact : FactWrappedCRTP<TriangleFact>
    {
        /// <summary> Defining Verticies; Order gives NormalDirection;
        public Vector3[] Verticies;
    
        [JsonIgnore]
        public Vector3 Normal, Tangents, AltTangents;
    
        /// <summary> \copydoc Fact.Fact </summary>
        public TriangleFact() : base() { }
    
        /// <summary>
        /// Standard Constructor:
        /// Initiates <see cref="Pids"/> and creates MMT %Fact Server-Side
        /// </summary>
        /// <param name="pid_corners">sets <see cref="Pids"/></param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
        public TriangleFact(Vector3[] Verticies, FactRecorder organizer) : base(organizer)
        {
            Init(Verticies);
        }
    
        /// <summary>
        /// Bypasses initialization of new MMT %Fact by using existend URI, _which is not checked for existence_.
        /// </summary>
        /// <param name="pid_corners">sets <see cref="Pids"/></param>
        /// <param name="uri">MMT URI</param>
        /// <param name="organizer">sets <see cref="Fact._Facts"/></param>
        public TriangleFact(Vector3[] Verticies, SOMDoc _ServerDefinition, FactRecorder organizer) : base(organizer)
        {
            Init(Verticies);
    
            this.ServerDefinition = _ServerDefinition;
            _ = this.Label;
        }
    
        private void Init(Vector3[] Verticies)
        {
            this.Verticies = Verticies;
    
            Tangents = (Verticies[0] - Verticies[1]).normalized;
            AltTangents = (Verticies[0] - Verticies[2]).normalized;
            Normal = Vector3.Cross(Tangents, AltTangents).normalized;
        }
    
        /// \copydoc Fact.parseFact(ScrollFact)
        public new static IEnumerator parseFact(List<Fact> ret, MMTFact fact)
        {
            throw new NotImplementedException();
        }
    
        public override MMTFact MakeMMTDeclaration()
        {
            SOMDoc tp = new OMS(MMTConstants.Triangle);
    
            return new MMTGeneralFact(Label, tp, Defines());
        }
    
        public override SOMDoc Defines()
            => new OMA(
                    new OMS(MMTConstants.CreateTriangle),
                    Verticies.Select(vert => SOMDoc.MakeVector3(vert)).ToArray()
                );
    
        protected override bool EquivalentWrapped(TriangleFact f1, TriangleFact f2)
        {
            int i = 0;
            for (; i < 3; i++)
                if (Math3d.IsApproximatelyEqual(f2.Verticies[i], f1.Verticies[0]))
                    break; // 1st incedence
    
            return i < 3 // match ? then do: cyclic match
                && Math3d.IsApproximatelyEqual(f2.Verticies[(i + 1) % 3], f1.Verticies[1])
                && Math3d.IsApproximatelyEqual(f2.Verticies[(i + 2) % 3], f1.Verticies[2]);
        }
    
        public override bool HasDependentFacts => false;
    
        protected override string[] GetDependentFactIds()
            => new string[0];
    
        protected override void RecalculateTransform()
        {
            Position = Verticies[0];
            Rotation = Quaternion.LookRotation(Tangents, Vector3.up);
        }
    
        protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactRecorder organizer)
            => new TriangleFact(Verticies, organizer);
    }