Panda3D
 All Classes Functions Variables Enumerations
trackerNode.cxx
1 // Filename: trackerNode.cxx
2 // Created by: drose (12Mar02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "trackerNode.h"
16 #include "config_device.h"
17 #include "dataNodeTransmit.h"
18 
19 TypeHandle TrackerNode::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: TrackerNode::Constructor
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 TrackerNode::
27 TrackerNode(ClientBase *client, const string &device_name) :
28  DataNode(device_name)
29 {
30  _transform_output = define_output("transform", TransformState::get_class_type());
31 
32  _transform = TransformState::make_identity();
33 
34  nassertv(client != (ClientBase *)NULL);
35  set_tracker_coordinate_system(client->get_coordinate_system());
36  set_graph_coordinate_system(CS_default);
37 
38  PT(ClientDevice) device =
39  client->get_device(ClientTrackerDevice::get_class_type(), device_name);
40 
41  if (device == (ClientDevice *)NULL) {
42  device_cat.warning()
43  << "Unable to open tracker device " << device_name << "\n";
44  return;
45  }
46 
47  if (!device->is_of_type(ClientTrackerDevice::get_class_type())) {
48  device_cat.error()
49  << "Inappropriate device type " << device->get_type()
50  << " created; expected a ClientTrackerDevice.\n";
51  return;
52  }
53 
54  _tracker = DCAST(ClientTrackerDevice, device);
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: TrackerNode::Constructor
59 // Access: Public
60 // Description:
61 ////////////////////////////////////////////////////////////////////
62 TrackerNode::
63 TrackerNode(ClientTrackerDevice *device) :
64  DataNode(device->get_device_name()),
65  _tracker(device)
66 {
67  _transform_output = define_output("transform", TransformState::get_class_type());
68 
69  _transform = TransformState::make_identity();
70 
71  nassertv(device != (ClientTrackerDevice *)NULL);
72  ClientBase *client = device->get_client();
73  nassertv(client != (ClientBase *)NULL);
74  set_tracker_coordinate_system(client->get_coordinate_system());
75  set_graph_coordinate_system(CS_default);
76 }
77 
78 ////////////////////////////////////////////////////////////////////
79 // Function: TrackerNode::Destructor
80 // Access: Public, Virtual
81 // Description:
82 ////////////////////////////////////////////////////////////////////
83 TrackerNode::
84 ~TrackerNode() {
85  // When the _tracker pointer destructs, the ClientTrackerDevice
86  // disconnects itself from the ClientBase, and everything that needs
87  // to get turned off does. Magic.
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function: TrackerNode::do_transmit_data
92 // Access: Protected, Virtual
93 // Description: The virtual implementation of transmit_data(). This
94 // function receives an array of input parameters and
95 // should generate an array of output parameters. The
96 // input parameters may be accessed with the index
97 // numbers returned by the define_input() calls that
98 // were made earlier (presumably in the constructor);
99 // likewise, the output parameters should be set with
100 // the index numbers returned by the define_output()
101 // calls.
102 ////////////////////////////////////////////////////////////////////
103 void TrackerNode::
104 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &,
105  DataNodeTransmit &output) {
106  if (is_valid()) {
107  _tracker->poll();
108  _tracker->acquire();
109  _data = _tracker->get_data();
110  _tracker->unlock();
111 
112  _data.get_orient().extract_to_matrix(_mat);
113  if (_tracker_cs != _graph_cs) {
114  // Convert the rotation for passing down the data graph.
115  _mat = _mat * LMatrix4::convert_mat(_tracker_cs, _graph_cs);
116  }
117  _mat.set_row(3, _data.get_pos());
118 
119  // Now send our matrix down the pipe. TODO: store this
120  // componentwise instead of just as a matrix-based transform.
121  _transform = TransformState::make_mat(_mat);
122  output.set_data(_transform_output, EventParameter(_transform));
123  }
124 }
The fundamental type of node for the data graph.
Definition: dataNode.h:64
An optional parameter associated with an event.
ClientBase * get_client() const
Returns the ClientBase this device is associated with.
Definition: clientDevice.I:23
void extract_to_matrix(LMatrix3f &m) const
Based on the quat lib from VRPN.
static const LMatrix4f & convert_mat(CoordinateSystem from, CoordinateSystem to)
Returns a matrix that transforms from the indicated coordinate system to the indicated coordinate sys...
Definition: lmatrix.cxx:656
const LPoint3 & get_pos() const
Returns the current position of the tracker.
Definition: trackerData.I:118
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
bool is_valid() const
Returns true if the TrackerNode is valid and connected to a server, false otherwise.
Definition: trackerNode.I:22
A device, attached to the ClientBase by a TrackerNode, that records the data from a single tracker de...
An abstract base class for a family of client device interfaces–including trackers, buttons, dials, and other analog inputs.
Definition: clientBase.h:47
const LOrientation & get_orient() const
Returns the current orientation of the tracker.
Definition: trackerData.I:162
void set_row(int row, const LVecBase4f &v)
Replaces the indicated row of the matrix.
Definition: lmatrix.h:1187
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
Encapsulates the data generated from (or sent into) any particular DataNode.
CoordinateSystem get_coordinate_system() const
Returns the coordinate system that all devices associated with this client will operate in...
Definition: clientBase.I:80
Any of a number of different devices that might be attached to a ClientBase, including trackers...
Definition: clientDevice.h:35
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...