using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace PsTech.TrackingTarget
{
public class TrackingTarget
{
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(ConsoleEventHandler handler, bool add);
private delegate bool ConsoleEventHandler(CtrlType sig);
static ConsoleEventHandler ConsoleCtrlEventHandler;
public enum CtrlType
{
CtrlCEvent = 0,
CtrlBreakEvent = 1,
CtrlCloseEvent = 2,
CtrlLogOffEvent = 5,
CtrlShutdownEvent = 6,
}
private static bool ConsoleHandler(CtrlType sig)
{
Exithandler((int)sig);
return true;
}
static bool Running = true;
static int NumberOfSamplesToGrab = 100;
static int SamplesGrabbed = 0;
class MyTrackerListener : TrackerListener
{
public override void OnTrackerData(TrackerData data,
ErrorStatus status)
{
if (SamplesGrabbed++ >= NumberOfSamplesToGrab)
{
Running = false;
}
foreach (TargetPose pose in data.TargetPoseList)
{
Console.Write("Detected " + pose.Target.Name + "\n");
}
}
}
static void Exithandler(int sig)
{
Tracker.Shutdown();
Running = false;
}
static int Main(string[] args)
{
ConsoleCtrlEventHandler += new ConsoleEventHandler(ConsoleHandler);
SetConsoleCtrlHandler(ConsoleCtrlEventHandler, true);
try
{
Tracker tracker = new Tracker();
Console.Write("Running PST Server version " + tracker.GetVersionInfo() + "\n");
TrackerListener listener = new MyTrackerListener();
tracker.AddTrackerListener(ref listener);
Console.Write("Put the Reference card in front of the PST in order to see tracking results.\n\n");
tracker.StartTracker();
Console.Write("System check: " + tracker.Systemcheck() + "\n\n");
TargetStatus[] targets = tracker.GetTargetList();
Console.Write("Found " + targets.Length + " tracking targets:\n");
foreach (TargetStatus t in targets)
{
Console.Write(t.Target.Name + "\t" + t.Status + "\n");
}
Console.Write("\n");
tracker.SetTargetStatus("Reference", true);
TargetMarker[] markers = tracker.GetTargetMarkers("Reference");
Console.Write("3D marker positions making up the Reference target:\n");
foreach (TargetMarker marker in markers)
{
Console.Write("x: " + marker.Coordinates[0] + "\t" + "y: " + marker.Coordinates[1] + "\t" + "z: " + marker.Coordinates[2] + "\n");
}
Console.Write("\n");
while (Running)
{
Thread.Sleep(100);
}
}
{
Console.Write(e + "\n");
}
finally
{
Tracker.Shutdown();
}
Console.Write("Press enter to continue...\n");
Console.Read();
return 0;
}
}
}