private double PointDist(DrawPoint a, DrawPoint b) { double delX = a.X - b.X; double delY = a.Y - b.Y; return(Math.Sqrt(delX * delX + delY * delY)); }
//Translates the centroid of the shape to the origin private void TranslatePointsToOrigin(List <DrawPoint> givenPoints, int numPoints) { DrawPoint cent = new DrawPoint(0, 0); foreach (DrawPoint p in givenPoints) { cent.X += p.X; cent.Y += p.Y; } //Console.WriteLine("cent before div (" + cent.X + "," + cent.Y + ")"); cent.X /= numPoints; cent.Y /= numPoints; //Console.WriteLine("cent before (" + cent.X + "," + cent.Y + ")"); for (int i = 0; i < givenPoints.Count; i++) { DrawPoint p = givenPoints[i]; p.X -= cent.X; p.Y -= cent.Y; givenPoints[i] = p; } /* * cent.X = 0; * cent.Y = 0; * foreach (DrawPoint p in givenPoints) * { * cent.X += p.X; * cent.Y += p.Y; * } * cent.X /= numPoints; * cent.Y /= numPoints; * Console.WriteLine("cent after (" + cent.X + "," + cent.Y + ")"); */ }
//Scales the given list of points to percentages of the greater of width/height private void Scale(List <DrawPoint> givenPoints) { double xmin, ymin, xmax, ymax, scale; xmin = ymin = Int32.MaxValue; xmax = ymax = 0; foreach (DrawPoint p in givenPoints) { xmin = Math.Min(xmin, p.X); ymin = Math.Min(ymin, p.Y); xmax = Math.Max(xmax, p.X); ymax = Math.Max(ymax, p.Y); } scale = Math.Max(xmax - xmin, ymax - ymin); for (int i = 0; i < givenPoints.Count; i++) { //Console.WriteLine("before: ("+givenPoints[i].X+","+ givenPoints[i].Y+")"); DrawPoint p = givenPoints[i]; p.X = (givenPoints[i].X - xmin) / scale; p.Y = (givenPoints[i].Y - ymin) / scale; givenPoints[i] = p; //Console.WriteLine("after: (" + givenPoints[i].X + "," + givenPoints[i].Y + ")"); } }
//Returns a resampled list of points to fit the template format. private List <DrawPoint> ResamplePoints(List <DrawPoint> givenPoints, int numPoints) { double incrementLength = pathLength(givenPoints) / (numPoints); //Console.WriteLine("pathLen: " + pathLength(givenPoints) + " incrementLen: " + incrementLength); double distSum = 0; List <DrawPoint> newPoints = new List <DrawPoint>(); newPoints.Add(new DrawPoint(givenPoints[0].X, givenPoints[0].Y)); DrawPoint lastPoint = new DrawPoint(0, 0); for (int i = 1; i < givenPoints.Count(); i++) { //Console.WriteLine("i is " + i+", lastPoint.X="+lastPoint.X+" size is "+newPoints.Count); if (lastPoint.X != 0 || lastPoint.Y != 0) { double lastDist = PointDist(lastPoint, givenPoints[i]); if (lastPoint.stroke == givenPoints[i].stroke) { if (distSum + lastDist >= incrementLength) { double perc = (incrementLength - distSum) / lastDist; double newX = lastPoint.X; newX += (perc * (givenPoints[i].X - lastPoint.X)); double newY = lastPoint.Y; newY += (perc * (givenPoints[i].Y - lastPoint.Y)); newPoints.Add(new DrawPoint(newX, newY)); lastPoint.X = newX; lastPoint.Y = newY; lastPoint.stroke = givenPoints[i].stroke; distSum = 0; i--; } else { distSum += lastDist; lastPoint.X = 0; lastPoint.Y = 0; } } else { lastPoint.X = 0; lastPoint.Y = 0; } } else { double dist = PointDist(givenPoints[i - 1], givenPoints[i]); //Console.WriteLine("dist from (" + givenPoints[i - 1].X + "," + givenPoints[i - 1].Y + ") to (" + givenPoints[i].X + "," + givenPoints[i].Y + ") is " + dist + ". distSum is "+distSum); //exclusion is meant to ignore multistroke jumps if (givenPoints[i - 1].stroke == givenPoints[i].stroke) { if (distSum + dist >= incrementLength) { double perc = (incrementLength - distSum) / dist; double newX = givenPoints[i - 1].X; newX += (perc * (givenPoints[i].X - givenPoints[i - 1].X)); double newY = givenPoints[i - 1].Y; newY += (perc * (givenPoints[i].Y - givenPoints[i - 1].Y)); newPoints.Add(new DrawPoint(newX, newY)); //givenPoints.Insert(i,new Point(newX, newY)); //the new point should be the NEXT selected point //Console.WriteLine("dump new point ("+newX+","+newY+")"); lastPoint.X = newX; lastPoint.Y = newY; lastPoint.stroke = givenPoints[i].stroke; distSum = 0; i--; } else { distSum += dist; //Console.WriteLine("new distSum is " + distSum); } } else { //Console.WriteLine("skip\n"); } } } //Console.WriteLine("thing has " + newPoints.Count); return(newPoints); }
//Adds the point to the current list of points public void AddPoint(DrawPoint p) { points.Add(p); }
/////////////////////////////////////////////////////////////////////// /// <summary> /// Called when Wintab WT_PACKET events are received. /// </summary> /// <param name="sender_I">The EventMessage object sending the report.</param> /// <param name="eventArgs_I">eventArgs_I.Message.WParam contains ID of packet containing the data.</param> public void MyWTPacketEventHandler(Object sender_I, MessageReceivedEventArgs eventArgs_I) { //System.Diagnostics.Debug.WriteLine("Received WT_PACKET event"); if (m_wtData == null) { return; } try { if (m_maxPkts == 1) { uint pktID = (uint)eventArgs_I.Message.WParam; WintabPacket pkt = m_wtData.GetDataPacket((uint)eventArgs_I.Message.LParam, pktID); //DEPRECATED WintabPacket pkt = m_wtData.GetDataPacket(pktID); if (pkt.pkContext != 0) { m_pkX = pkt.pkX; m_pkY = pkt.pkY; m_pressure = pkt.pkNormalPressure; //Trace.WriteLine("SCREEN: pkX: " + pkt.pkX + ", pkY:" + pkt.pkY + ", pressure: " + pkt.pkNormalPressure); m_pkTime = pkt.pkTime; //Console.WriteLine("time: " + m_pkTime); if (m_graphics == null) { // display data mode TraceMsg("Received WT_PACKET event[" + pktID + "]: X/Y/P = " + pkt.pkX + " / " + pkt.pkY + " / " + pkt.pkNormalPressure + "\n"); } else { // scribble mode int clientWidth = scribblePanel.Width; int clientHeight = scribblePanel.Height; // m_pkX and m_pkY are in screen (system) coordinates. /* //Converts the program to use relative coordinates. System.Drawing.Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea; double percentX = m_pkX / (double)workingRectangle.Width; double percentY = m_pkY / (double)workingRectangle.Height; int newX = (int)(percentX * clientWidth); int newY = (int)(percentY * clientHeight); Point clientPoint = new Point(newX, newY); */ Point clientPoint = scribblePanel.PointToClient(new Point(m_pkX, m_pkY)); if (clientPoint.X < 0) return; if (clientPoint.Y < 0) return; if (clientPoint.X > scribblePanel.Width) return; if (clientPoint.Y > scribblePanel.Height) return; //Trace.WriteLine("CLIENT: X: " + clientPoint.X + ", Y:" + clientPoint.Y); if (m_lastPoint.Equals(Point.Empty)) { m_lastPoint = clientPoint; m_pkTimeLast = m_pkTime; } //m_pen.Width = (float)(m_pressure / 200); m_pen.Width = (float)3.0; if (m_pressure > 0 && screenEnabled) { if(!timeoutTimer.Enabled && timeoutEnabled && currentSeq != 0) { timeoutTimer.Start(); } if(compareTimer.Enabled) { compareTimer.Stop(); } uint unixTimestamp = (uint)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds; if (m_pkTime - m_pkTimeLast > 50) { Console.WriteLine("new stroke"); m_graphics.DrawRectangle(m_pen, clientPoint.X, clientPoint.Y, 1, 1); strokeNum++; DrawPoint dp = new DrawPoint(clientPoint.X, clientPoint.Y, strokeNum, unixTimestamp, m_pressure); TR.AddStartPoint(dp); m_lastPoint = clientPoint; m_pkTimeLast = m_pkTime; } else { m_graphics.DrawLine(m_pen, clientPoint, m_lastPoint); DrawPoint dp = new DrawPoint(clientPoint.X, clientPoint.Y, strokeNum, unixTimestamp, m_pressure); TR.AddPoint(dp); compareTimer.Start(); m_lastPoint = clientPoint; m_pkTimeLast = m_pkTime; } } } } } } catch (Exception ex) { throw new Exception("FAILED to get packet data: " + ex.ToString()); } }
/////////////////////////////////////////////////////////////////////// /// <summary> /// Called when Wintab WT_PACKET events are received. /// </summary> /// <param name="sender_I">The EventMessage object sending the report.</param> /// <param name="eventArgs_I">eventArgs_I.Message.WParam contains ID of packet containing the data.</param> public void MyWTPacketEventHandler(Object sender_I, MessageReceivedEventArgs eventArgs_I) { //System.Diagnostics.Debug.WriteLine("Received WT_PACKET event"); if (m_wtData == null) { return; } try { if (m_maxPkts == 1) { uint pktID = (uint)eventArgs_I.Message.WParam; WintabPacket pkt = m_wtData.GetDataPacket((uint)eventArgs_I.Message.LParam, pktID); //DEPRECATED WintabPacket pkt = m_wtData.GetDataPacket(pktID); if (pkt.pkContext != 0) { m_pkX = pkt.pkX; m_pkY = pkt.pkY; m_pressure = pkt.pkNormalPressure; //Trace.WriteLine("SCREEN: pkX: " + pkt.pkX + ", pkY:" + pkt.pkY + ", pressure: " + pkt.pkNormalPressure); m_pkTime = pkt.pkTime; //Console.WriteLine("time: " + m_pkTime); if (m_graphics == null) { // display data mode TraceMsg("Received WT_PACKET event[" + pktID + "]: X/Y/P = " + pkt.pkX + " / " + pkt.pkY + " / " + pkt.pkNormalPressure + "\n"); } else { // scribble mode int clientWidth = scribblePanel.Width; int clientHeight = scribblePanel.Height; // m_pkX and m_pkY are in screen (system) coordinates. /* * //Converts the program to use relative coordinates. * System.Drawing.Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea; * * double percentX = m_pkX / (double)workingRectangle.Width; * double percentY = m_pkY / (double)workingRectangle.Height; * * int newX = (int)(percentX * clientWidth); * int newY = (int)(percentY * clientHeight); * * Point clientPoint = new Point(newX, newY); */ Point clientPoint = scribblePanel.PointToClient(new Point(m_pkX, m_pkY)); if (clientPoint.X < 0) { return; } if (clientPoint.Y < 0) { return; } if (clientPoint.X > scribblePanel.Width) { return; } if (clientPoint.Y > scribblePanel.Height) { return; } //Trace.WriteLine("CLIENT: X: " + clientPoint.X + ", Y:" + clientPoint.Y); if (m_lastPoint.Equals(Point.Empty)) { m_lastPoint = clientPoint; m_pkTimeLast = m_pkTime; } //m_pen.Width = (float)(m_pressure / 200); m_pen.Width = (float)3.0; if (m_pressure > 0 && screenEnabled) { if (!timeoutTimer.Enabled && timeoutEnabled && currentSeq != 0) { timeoutTimer.Start(); } if (compareTimer.Enabled) { compareTimer.Stop(); } uint unixTimestamp = (uint)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds; if (m_pkTime - m_pkTimeLast > 50) { Console.WriteLine("new stroke"); m_graphics.DrawRectangle(m_pen, clientPoint.X, clientPoint.Y, 1, 1); strokeNum++; DrawPoint dp = new DrawPoint(clientPoint.X, clientPoint.Y, strokeNum, unixTimestamp, m_pressure); TR.AddStartPoint(dp); m_lastPoint = clientPoint; m_pkTimeLast = m_pkTime; } else { m_graphics.DrawLine(m_pen, clientPoint, m_lastPoint); DrawPoint dp = new DrawPoint(clientPoint.X, clientPoint.Y, strokeNum, unixTimestamp, m_pressure); TR.AddPoint(dp); compareTimer.Start(); m_lastPoint = clientPoint; m_pkTimeLast = m_pkTime; } } } } } } catch (Exception ex) { throw new Exception("FAILED to get packet data: " + ex.ToString()); } }
//Translates the centroid of the shape to the origin private void TranslatePointsToOrigin(List<DrawPoint> givenPoints, int numPoints) { DrawPoint cent = new DrawPoint(0,0); foreach(DrawPoint p in givenPoints) { cent.X += p.X; cent.Y += p.Y; } //Console.WriteLine("cent before div (" + cent.X + "," + cent.Y + ")"); cent.X /= numPoints; cent.Y /= numPoints; //Console.WriteLine("cent before (" + cent.X + "," + cent.Y + ")"); for(int i=0;i<givenPoints.Count;i++) { DrawPoint p = givenPoints[i]; p.X -= cent.X; p.Y -= cent.Y; givenPoints[i] = p; } /* cent.X = 0; cent.Y = 0; foreach (DrawPoint p in givenPoints) { cent.X += p.X; cent.Y += p.Y; } cent.X /= numPoints; cent.Y /= numPoints; Console.WriteLine("cent after (" + cent.X + "," + cent.Y + ")"); */ }
public void AddStartPoint(DrawPoint p) { points.Add(p); }
//Returns a resampled list of points to fit the template format. private List<DrawPoint> ResamplePoints(List<DrawPoint> givenPoints, int numPoints) { double incrementLength = pathLength(givenPoints) / (numPoints); //Console.WriteLine("pathLen: " + pathLength(givenPoints) + " incrementLen: " + incrementLength); double distSum = 0; List<DrawPoint> newPoints = new List<DrawPoint>(); newPoints.Add(new DrawPoint(givenPoints[0].X,givenPoints[0].Y)); DrawPoint lastPoint = new DrawPoint(0, 0); for (int i=1;i< givenPoints.Count();i++) { //Console.WriteLine("i is " + i+", lastPoint.X="+lastPoint.X+" size is "+newPoints.Count); if (lastPoint.X != 0 || lastPoint.Y != 0) { double lastDist = PointDist(lastPoint, givenPoints[i]); if (lastPoint.stroke == givenPoints[i].stroke) { if (distSum + lastDist >= incrementLength) { double perc = (incrementLength - distSum) / lastDist; double newX = lastPoint.X; newX += (perc * (givenPoints[i].X - lastPoint.X)); double newY = lastPoint.Y; newY += (perc * (givenPoints[i].Y - lastPoint.Y)); newPoints.Add(new DrawPoint(newX, newY)); lastPoint.X = newX; lastPoint.Y = newY; lastPoint.stroke = givenPoints[i].stroke; distSum = 0; i--; } else { distSum += lastDist; lastPoint.X = 0; lastPoint.Y = 0; } } else { lastPoint.X = 0; lastPoint.Y = 0; } } else { double dist = PointDist(givenPoints[i - 1], givenPoints[i]); //Console.WriteLine("dist from (" + givenPoints[i - 1].X + "," + givenPoints[i - 1].Y + ") to (" + givenPoints[i].X + "," + givenPoints[i].Y + ") is " + dist + ". distSum is "+distSum); //exclusion is meant to ignore multistroke jumps if (givenPoints[i - 1].stroke == givenPoints[i].stroke) { if (distSum + dist >= incrementLength) { double perc = (incrementLength - distSum) / dist; double newX = givenPoints[i - 1].X; newX += (perc * (givenPoints[i].X - givenPoints[i - 1].X)); double newY = givenPoints[i - 1].Y; newY += (perc * (givenPoints[i].Y - givenPoints[i - 1].Y)); newPoints.Add(new DrawPoint(newX, newY)); //givenPoints.Insert(i,new Point(newX, newY)); //the new point should be the NEXT selected point //Console.WriteLine("dump new point ("+newX+","+newY+")"); lastPoint.X = newX; lastPoint.Y = newY; lastPoint.stroke = givenPoints[i].stroke; distSum = 0; i--; } else { distSum += dist; //Console.WriteLine("new distSum is " + distSum); } } else { //Console.WriteLine("skip\n"); } } } //Console.WriteLine("thing has " + newPoints.Count); return newPoints; }
private double PointDist(DrawPoint a, DrawPoint b) { double delX = a.X - b.X; double delY = a.Y - b.Y; return Math.Sqrt(delX * delX + delY * delY); }