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