Select Git revision
FactComparer.cs
LotTool.cs 5.28 KiB
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static CommunicationEvents;
public class LotTool : Gadget
//constructs a Perpendicular between a Line and a Point
{
/// \copydoc Gadget.s_type
[JsonProperty]
protected static new string s_type = "LotTool";
//Variables for LineMode distinction
private bool LotModeIsPointSelected = false;
private bool LotModeIsLineSelected = false;
private AbstractLineFact LotModeLineSelected = null;
private PointFact LotModeIntersectionPoint = null;
private PointFact LotModeLinePointA = null;
private RaycastHit LotModeLineHit;
public override void _Hit(RaycastHit hit)
{
void CreateRayAndAngles(string pidIntersectionPoint, string pidLotPoint, bool samestep)
{
FactManager.AddRayFact(pidIntersectionPoint, pidLotPoint, samestep);
//TODO: create at all? / for all points on basline?
FactManager.AddAngleFact(
LotModeLineSelected.Pid1 == pidIntersectionPoint ? LotModeLineSelected.Pid2 : LotModeLineSelected.Pid1,
pidIntersectionPoint, pidLotPoint, true);
}
//If LotPoint is on baseLine
if (LotModeIsPointSelected && (hit.transform.gameObject.layer == LayerMask.NameToLayer("Default") || hit.transform.gameObject.layer == LayerMask.NameToLayer("Tree")))
{
Vector3 LotPoint = Math3d.ProjectPointOnLine(hit.point, LotModeLineSelected.Dir, LotModeIntersectionPoint.Point);
//TODO: which normal?
CreateRayAndAngles(LotModeIntersectionPoint.Id, FactManager.AddPointFact(LotPoint, hit.normal).Id, true);
ResetGadget();
}
//If baseline already selected and point selected
else if (LotModeIsLineSelected && !LotModeIsPointSelected && hit.transform.gameObject.layer == LayerMask.NameToLayer("Point"))
{
PointFact tempFact = StageStatic.stage.factState[hit.transform.GetComponent<FactObject>().URI] as PointFact;
Vector3 intersectionPoint = Math3d.ProjectPointOnLine(LotModeLinePointA.Point, LotModeLineSelected.Dir, tempFact.Point);
if (intersectionPoint == tempFact.Point) // Vector3.operator== tests for almost Equal()
{ //TempFact is on baseLine
LotModeIsPointSelected = true;
LotModeIntersectionPoint = tempFact;
return;
}
//TODO: test Facts existance
//add Facts
var intersectionId = FactManager.AddPointFact(intersectionPoint, LotModeLineHit.normal).Id;
if(LotModeLineSelected is RayFact) //Add OnLineFact only on Ray not Line
FactManager.AddOnLineFact(intersectionId, LotModeLineSelected.Id, true);
CreateRayAndAngles(intersectionId, tempFact.Id, true);
ResetGadget();
}
//If nothing yet selected
else if (!LotModeIsLineSelected
&& (hit.transform.gameObject.layer == LayerMask.NameToLayer("Ray")
|| hit.transform.gameObject.layer == LayerMask.NameToLayer("Line")))
{
Fact tempFact = StageStatic.stage.factState[hit.transform.GetComponent<FactObject>().URI];
//Activate LineDrawing for preview
LotModeIsLineSelected = true;
LotModeLineSelected = tempFact as AbstractLineFact;
LotModeLinePointA = (PointFact) StageStatic.stage.factState[LotModeLineSelected.Pid1];
LotModeLineHit = hit;
ActivateLineDrawing();
}
//unexpected usage
else if (LotModeIsLineSelected)
//Deactivate LineDrawing and first point selection
ResetGadget();
}
protected override void _ResetGadget()
{
LotModeIsLineSelected = false;
LotModeLineSelected = null;
LotModeLinePointA = null;
LotModeIsPointSelected = false;
LotModeIntersectionPoint = null;
LotModeLineHit = default(RaycastHit);
}
protected override void _ActivateLineDrawing()
{
GadgetBehaviour.LineRenderer.positionCount = 3;
GadgetBehaviour.LineRenderer.startWidth = 0.095f;
GadgetBehaviour.LineRenderer.endWidth = 0.095f;
//start at curser
SetPosition(0, GadgetBehaviour.Cursor.transform.position);
//Project curser perpendicular on Line for intersection-point
SetPosition(1, Math3d.ProjectPointOnLine(LotModeLinePointA.Point, LotModeLineSelected.Dir, GetPosition(0)));
//end at Point on the line (i.c.o. projection beeing outside a finite line)
SetPosition(2, Math3d.ProjectPointOnLine(LotModeLinePointA.Point, LotModeLineSelected.Dir, LotModeLineHit.point));
}
//Updates the points of the Lines when baseLine was selected in LineMode
protected override void _UpdateLineDrawing()
{
if (LotModeIsPointSelected)
{
SetPosition(1, LotModeIntersectionPoint.Point);
SetPosition(0, Math3d.ProjectPointOnLine(GadgetBehaviour.Cursor.transform.position, LotModeLineSelected.Dir, GetPosition(1)));
}
else
{
SetPosition(0, GadgetBehaviour.Cursor.transform.position);
SetPosition(1, Math3d.ProjectPointOnLine(LotModeLinePointA.Point, LotModeLineSelected.Dir, GetPosition(0)));
}
}
}