123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671 |
- using System;
- using System.Collections.Generic;
- using System.Windows;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using OptiTrack;
- using System.Runtime.InteropServices;
- using System.IO;
- namespace SketchAssistantWPF
- {
- public class MVP_Model
- {
-
-
-
- MVP_Presenter programPresenter;
-
-
-
- ActionHistory historyOfActions;
-
-
-
- OptiTrackConnector connector;
-
-
-
-
-
-
- readonly double WARNING_ZONE_BOUNDARY = 0.10;
-
-
-
- public bool inDrawingMode { get; private set; }
-
-
-
- public bool optiTrackInUse { get; private set; }
-
-
-
- int deletionRadius = 5;
-
-
-
- Point currentCursorPosition;
-
-
-
- Point previousCursorPosition;
-
-
-
- Queue<Point> cursorPositions = new Queue<Point>();
-
-
-
- Point currentOptiCursorPosition;
-
-
-
- Point previousOptiCursorPosition;
-
-
-
- Queue<Point> optiCursorPositions = new Queue<Point>();
-
-
-
- bool[,] isFilledMatrix;
-
-
-
- HashSet<int>[,] linesMatrix;
-
-
-
- public int leftImageBoxWidth;
-
-
-
- public int leftImageBoxHeight;
-
-
-
- public int rightImageBoxWidth;
-
-
-
- public int rightImageBoxHeight;
-
-
-
- public ImageDimension rightImageSize { get; private set; }
-
-
-
- public bool canvasActive { get; set; }
-
-
-
- public bool graphicLoaded { get; set; }
-
-
-
- public bool optitrackAvailable { get; private set; }
-
-
-
- public float optiTrackX;
-
-
-
- public float optiTrackY;
-
-
-
- public float optiTrackZ;
-
-
-
- private bool optiTrackInsideDrawingZone = false;
-
-
-
- private Wristband wristband;
-
-
-
-
- bool OptiMovingBack = false;
-
-
-
- int OptiLayer = 0;
-
-
-
- double PathTraveled = 0;
-
-
-
- private bool mouseDown;
-
-
-
- List<InternalLine> leftLineList;
-
-
-
- List<Tuple<bool, InternalLine>> rightLineList;
-
-
-
- List<Point> currentLine = new List<Point>();
- public MVP_Model(MVP_Presenter presenter)
- {
- programPresenter = presenter;
- historyOfActions = new ActionHistory();
- rightLineList = new List<Tuple<bool, InternalLine>>();
- canvasActive = false;
- UpdateUI();
- rightImageSize = new ImageDimension(0, 0);
- connector = new OptiTrackConnector();
- wristband = new Wristband();
-
- optitrackAvailable = false;
- if (File.Exists(@"C:\Users\videowall-pc-user\Documents\BP-SketchAssistant\SketchAssistant\optitrack_setup.ttp"))
- {
- if (connector.Init(@"C:\Users\videowall-pc-user\Documents\BP-SketchAssistant\SketchAssistant\optitrack_setup.ttp"))
- {
- optitrackAvailable = true;
- connector.StartTracking(GetOptiTrackPosition);
- }
- }
- }
-
-
-
-
-
-
-
-
- private bool CheckInsideDrawingZone(float optiTrackZ)
- {
- if (Math.Abs(optiTrackZ) > WARNING_ZONE_BOUNDARY * 2) return false;
- return true;
- }
-
-
-
-
- void GetOptiTrackPosition(OptiTrack.Frame frame)
- {
- if (frame.Trackables.Length >= 1)
- {
- optiTrackX = frame.Trackables[0].X;
- optiTrackY = frame.Trackables[0].Y;
- optiTrackZ = frame.Trackables[0].Z;
- }
- }
-
-
-
-
-
- private void ChangeLines(HashSet<int> lines, bool shown)
- {
- foreach (int lineId in lines)
- {
- if (lineId <= rightLineList.Count - 1 && lineId >= 0)
- {
- rightLineList[lineId] = new Tuple<bool, InternalLine>(shown, rightLineList[lineId].Item2);
- }
- }
- }
-
-
-
- private void CheckPathTraveled()
- {
- var a = Math.Abs(previousOptiCursorPosition.X - currentOptiCursorPosition.X);
- var b = Math.Abs(previousOptiCursorPosition.Y - currentOptiCursorPosition.Y);
- PathTraveled += Math.Sqrt(Math.Pow(a,2) + Math.Pow(b,2));
-
- if(PathTraveled > 2)
- {
- PathTraveled = 0;
-
- }
- }
-
-
-
- private void RepopulateDeletionMatrixes()
- {
- if (canvasActive)
- {
- isFilledMatrix = new bool[rightImageSize.Width, rightImageSize.Height];
- linesMatrix = new HashSet<int>[rightImageSize.Width, rightImageSize.Height];
- foreach (Tuple<bool, InternalLine> lineTuple in rightLineList)
- {
- if (lineTuple.Item1)
- {
- lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
- }
- }
- }
- }
-
-
-
- private void UpdateUI()
- {
- programPresenter.UpdateUIState(inDrawingMode, historyOfActions.CanUndo(), historyOfActions.CanRedo(), canvasActive, graphicLoaded, optitrackAvailable, optiTrackInUse);
- }
-
-
-
-
-
-
-
- private HashSet<int> CheckDeletionMatrixesAroundPoint(Point p, int range)
- {
- HashSet<int> returnSet = new HashSet<int>();
- foreach (Point pnt in GeometryCalculator.FilledCircleAlgorithm(p, (int)range))
- {
- if (pnt.X >= 0 && pnt.Y >= 0 && pnt.X < rightImageSize.Width && pnt.Y < rightImageSize.Height)
- {
- if (isFilledMatrix[(int)pnt.X, (int)pnt.Y])
- {
- returnSet.UnionWith(linesMatrix[(int)pnt.X, (int)pnt.Y]);
- }
- }
- }
- return returnSet;
- }
-
-
-
-
-
- private Point ConvertTo96thsOfInch(Point p)
- {
-
-
- double OPTITRACK_X_OFFSET = -0.4854;
- double OPTITRACK_Y_OFFSET = 0.9;
- double OPTITRACK_CANVAS_HEIGHT = 1.2;
- double OPTITRACK_X_SCALE = 0.21 * (((1.8316 / 0.0254) * 96) / (1.8316));
- double OPTITRACK_Y_SCALE = 0.205 * (((1.2 / 0.0254) * 96) / (1.2));
-
- double xCoordinate = (p.X - OPTITRACK_X_OFFSET) * OPTITRACK_X_SCALE;
- double yCoordinate = (OPTITRACK_CANVAS_HEIGHT - (p.Y - OPTITRACK_Y_OFFSET)) * OPTITRACK_Y_SCALE;
- return new Point(xCoordinate, yCoordinate);
- }
-
-
-
-
- private void SetCurrentFingerPosition(Point p)
- {
- Point correctedPoint = ConvertTo96thsOfInch(p);
- if (optiTrackZ < -2.2 * WARNING_ZONE_BOUNDARY && OptiLayer > -1)
- {
- OptiLayer = -1; OptiMovingBack = false;
- programPresenter.SetOverlayColor("optipoint", Brushes.Yellow);
- }
- else if (optiTrackZ > 2 * WARNING_ZONE_BOUNDARY && OptiLayer < 1)
- {
- programPresenter.SetOverlayColor("optipoint", Brushes.Red);
- OptiLayer = 1;
- }
- else if(optiTrackZ <= 2 * WARNING_ZONE_BOUNDARY && optiTrackZ >= -2.2 * WARNING_ZONE_BOUNDARY){
- if(OptiLayer > 0)
- OptiMovingBack = true;
- programPresenter.SetOverlayColor("optipoint", Brushes.Green);
- OptiLayer = 0;
- }
- currentOptiCursorPosition = correctedPoint;
- programPresenter.MoveOptiPoint(currentOptiCursorPosition);
- if (optiCursorPositions.Count > 0) { previousOptiCursorPosition = optiCursorPositions.Dequeue(); }
- else { previousOptiCursorPosition = currentOptiCursorPosition; }
- optiCursorPositions.Enqueue(currentOptiCursorPosition);
- }
-
-
-
-
-
-
-
- public void ResizeEvent(ImageDimension RightCanvas)
- {
- if (RightCanvas.Height >= 0 && RightCanvas.Width >= 0) { rightImageSize = RightCanvas; }
- RepopulateDeletionMatrixes();
- }
-
-
-
- public void ResetRightImage()
- {
- if(currentLine.Count > 0)
- FinishCurrentLine(true);
- rightLineList.Clear();
- programPresenter.PassLastActionTaken(historyOfActions.Reset());
- programPresenter.ClearRightLines();
- }
-
-
-
-
-
-
- public void SetLeftLineList(int width, int height, List<InternalLine> listOfLines)
- {
- rightImageSize = new ImageDimension(width, height);
- leftLineList = listOfLines;
- graphicLoaded = true;
- programPresenter.UpdateLeftLines(leftLineList);
- CanvasActivated();
- }
-
-
-
- public void CanvasActivated()
- {
- canvasActive = true;
- RepopulateDeletionMatrixes();
- UpdateUI();
- }
-
-
-
- public void Undo()
- {
- if (historyOfActions.CanUndo())
- {
- HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
- SketchAction.ActionType undoAction = historyOfActions.GetCurrentAction().GetActionType();
- switch (undoAction)
- {
- case SketchAction.ActionType.Delete:
-
- ChangeLines(affectedLines, true);
- break;
- case SketchAction.ActionType.Draw:
-
- ChangeLines(affectedLines, false);
- break;
- default:
- break;
- }
- programPresenter.UpdateRightLines(rightLineList);
- }
- RepopulateDeletionMatrixes();
- programPresenter.PassLastActionTaken(historyOfActions.MoveAction(true));
- UpdateUI();
- }
-
-
-
- public void Redo()
- {
- if (historyOfActions.CanRedo())
- {
- programPresenter.PassLastActionTaken(historyOfActions.MoveAction(false));
- HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
- SketchAction.ActionType redoAction = historyOfActions.GetCurrentAction().GetActionType();
- switch (redoAction)
- {
- case SketchAction.ActionType.Delete:
-
- ChangeLines(affectedLines, false);
- break;
- case SketchAction.ActionType.Draw:
-
- ChangeLines(affectedLines, true);
- break;
- default:
- break;
- }
- programPresenter.UpdateRightLines(rightLineList);
- RepopulateDeletionMatrixes();
- }
- UpdateUI();
- }
-
-
-
-
- public void ChangeState(bool nowDrawing)
- {
- if(inDrawingMode && !nowDrawing && currentLine.Count > 0 && optiTrackInUse)
- FinishCurrentLine(true);
- inDrawingMode = nowDrawing;
- UpdateUI();
- }
-
-
-
-
- public void SetOptiTrack(bool usingOptiTrack)
- {
- optiTrackInUse = usingOptiTrack;
- if (usingOptiTrack && optiTrackX == 0 && optiTrackY == 0 && optiTrackZ == 0)
- {
- programPresenter.PassWarning("Trackable not detected, please check if OptiTrack is activated and Trackable is recognized");
- optiTrackInUse = false;
-
- programPresenter.SetOverlayStatus("optipoint", false, currentCursorPosition);
- }
- else
- {
-
- programPresenter.SetOverlayStatus("optipoint", true, currentCursorPosition);
- }
- }
-
-
-
-
- public void SetCurrentCursorPosition(Point p)
- {
- currentCursorPosition = p;
- mouseDown = programPresenter.IsMousePressed();
- }
-
-
-
- public void StartNewLine()
- {
- mouseDown = true;
- if (inDrawingMode)
- {
- if(optiTrackInUse)
- {
- currentLine.Clear();
- currentLine.Add(currentOptiCursorPosition);
- }
- else if (programPresenter.IsMousePressed())
- {
- currentLine.Clear();
- currentLine.Add(currentCursorPosition);
- }
- }
- }
-
-
-
-
- public void FinishCurrentLine(bool valid)
- {
- mouseDown = false;
- if (valid)
- {
- if (inDrawingMode && currentLine.Count > 0)
- {
- InternalLine newLine = new InternalLine(currentLine, rightLineList.Count);
- rightLineList.Add(new Tuple<bool, InternalLine>(true, newLine));
- newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
- programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID())));
-
- programPresenter.UpdateRightLines(rightLineList);
- currentLine.Clear();
- programPresenter.UpdateCurrentLine(currentLine);
- }
- }
- else
- {
- currentLine.Clear();
- }
- UpdateUI();
- }
-
-
-
-
-
- public void FinishCurrentLine(List<Point> p)
- {
- mouseDown = false;
- if (inDrawingMode && currentLine.Count > 0)
- {
- InternalLine newLine = new InternalLine(p, rightLineList.Count);
- rightLineList.Add(new Tuple<bool, InternalLine>(true, newLine));
- newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
- programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID())));
- programPresenter.UpdateRightLines(rightLineList);
- currentLine.Clear();
- }
- UpdateUI();
- }
-
-
-
- public void Tick()
- {
- if (cursorPositions.Count > 0) { previousCursorPosition = cursorPositions.Dequeue(); }
- else { previousCursorPosition = currentCursorPosition; }
- if(optitrackAvailable)
- SetCurrentFingerPosition(new Point(optiTrackX, optiTrackY));
-
- if (optiTrackInUse && inDrawingMode && !OptiMovingBack)
- {
-
- if (!CheckInsideDrawingZone(optiTrackZ))
- {
-
- if (optiTrackInsideDrawingZone && inDrawingMode)
- {
- optiTrackInsideDrawingZone = false;
- FinishCurrentLine(true);
- }
- }
- else
- {
-
- if (!optiTrackInsideDrawingZone)
- {
- optiTrackInsideDrawingZone = true;
- StartNewLine();
- }
- else currentLine.Add(currentOptiCursorPosition);
- programPresenter.UpdateCurrentLine(currentLine);
- if (optiTrackZ > WARNING_ZONE_BOUNDARY)
- {
- wristband.PushForward();
- }
- else if (optiTrackZ < -1 * WARNING_ZONE_BOUNDARY)
- {
- wristband.PushBackward();
- }
- }
- }
- else if( !optiTrackInUse && inDrawingMode)
- {
-
- cursorPositions.Enqueue(currentCursorPosition);
- if (inDrawingMode && programPresenter.IsMousePressed())
- {
- currentLine.Add(currentCursorPosition);
-
- }
- }
-
- if (!inDrawingMode)
- {
- List<Point> uncheckedPoints = new List<Point>();
- if (programPresenter.IsMousePressed() && !optiTrackInUse)
- uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
- if(optiTrackInUse && CheckInsideDrawingZone(optiTrackZ) && !OptiMovingBack)
- uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousOptiCursorPosition, currentOptiCursorPosition);
- foreach (Point currPoint in uncheckedPoints)
- {
- HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionRadius);
- if (linesToDelete.Count > 0)
- {
- programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Delete, linesToDelete)));
- foreach (int lineID in linesToDelete)
- {
- rightLineList[lineID] = new Tuple<bool, InternalLine>(false, rightLineList[lineID].Item2);
- }
- RepopulateDeletionMatrixes();
- programPresenter.UpdateRightLines(rightLineList);
- }
- }
- }
- }
-
-
-
-
- public bool HasUnsavedProgress()
- {
- return !historyOfActions.IsEmpty();
- }
- }
- }
|