PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
PointObservation.cxx
Go to the documentation of this file.
1 #include "PlusConfigure.h"
2 
3 #include "PointObservation.h"
4 
7 {
8 }
9 
10 
12 ::PointObservation( std::vector<double> newObervation )
13 {
14  this->Observation = newObervation;
15 }
16 
17 
20 {
21  this->Observation.clear();
22 }
23 
24 
26 ::Translate( std::vector<double> translation )
27 {
28  this->Observation = LinearObject::Add( this->Observation, translation );
29 }
30 
31 
33 ::Rotate( vnl_matrix<double>* rotation )
34 {
35  vnl_matrix<double>* currPoint = new vnl_matrix<double>( PointObservation::SIZE, 1, 0.0 );
36  currPoint->put( 0, 0, this->Observation.at(0) );
37  currPoint->put( 1, 0, this->Observation.at(1) );
38  currPoint->put( 2, 0, this->Observation.at(2) );
39 
40  vnl_matrix<double>* rotPoint = new vnl_matrix<double>( ( *rotation ) * ( *currPoint ) );
41  this->Observation.at(0) = rotPoint->get( 0, 0 );
42  this->Observation.at(1) = rotPoint->get( 1, 0 );
43  this->Observation.at(2) = rotPoint->get( 2, 0 );
44 }
45 
46 
47 std::string PointObservation
49 {
50  std::ostringstream xmlstring;
51  std::ostringstream matrixstring;
52  matrixstring << "0 0 0 " << this->Observation.at(0) << " ";
53  matrixstring << "0 0 0 " << this->Observation.at(1) << " ";
54  matrixstring << "0 0 0 " << this->Observation.at(2) << " ";
55  matrixstring << "0 0 0 1";
56 
57  xmlstring << " <log";
58  xmlstring << " TimeStampSec=\"" << 0 << "\"";
59  xmlstring << " TimeStampNSec=\"" << 0 << "\"";
60  xmlstring << " type=\"transform\"";
61  xmlstring << " DeviceName=\"" << "Pointer" << "\"";
62  xmlstring << " transform=\"" << matrixstring.str() << "\"";
63  xmlstring << " />" << std::endl;
64 
65  return xmlstring.str();
66 }
67 
68 
70 ::FromXMLElement( vtkXMLDataElement* element )
71 {
72 
73  if ( strcmp( element->GetName(), "log" ) != 0 || strcmp( element->GetAttribute( "type" ), "transform" ) != 0 )
74  {
75  return; // If it's not a "log" or is the wrong tool jump to the next.
76  }
77 
78  this->Observation = std::vector<double>( SIZE, 0.0 );
79 
80  std::stringstream matrixstring( std::string( element->GetAttribute( "transform" ) ) );
81  double value;
82 
83  for ( int i = 0; i < 16; i++ )
84  {
85  matrixstring >> value;
86  if ( i == 3 )
87  {
88  this->Observation.at(0) = value;
89  }
90  if ( i == 7 )
91  {
92  this->Observation.at(1) = value;
93  }
94  if ( i == 11 )
95  {
96  this->Observation.at(2) = value;
97  }
98  }
99 
100 }
101 
102 
103 bool PointObservation
104 ::FromXMLElement( vtkXMLDataElement* currElement, vtkXMLDataElement* prevElement )
105 {
106  const double ROTATION_THRESHOLD = 0.005;
107  const double TRANSLATION_THRESHOLD = 0.5;
108 
109  // Check to ensure that the transformation has changed since the previously collected element
110  if ( strcmp( currElement->GetName(), "log" ) != 0 || strcmp( currElement->GetAttribute( "type" ), "transform" ) != 0 )
111  {
112  return false; // If it's not a "log" or is the wrong tool jump to the next.
113  }
114  if ( strcmp( prevElement->GetName(), "log" ) != 0 || strcmp( prevElement->GetAttribute( "type" ), "transform" ) != 0 )
115  {
116  return false; // If it's not a "log" or is the wrong tool jump to the next.
117  }
118 
119  std::stringstream currmatrixstring( std::string( currElement->GetAttribute( "transform" ) ) );
120  std::stringstream prevmatrixstring( std::string( prevElement->GetAttribute( "transform" ) ) );
121  double currValue, prevValue;
122 
123  std::vector<double> currRotation, prevRotation;
124  std::vector<double> currTranslation, prevTranslation;
125 
126  for ( int i = 0; i < 16; i++ )
127  {
128  currmatrixstring >> currValue;
129  prevmatrixstring >> prevValue;
130  if ( i == 0 || i == 1 || i == 2 || i == 4 || i == 5 || i == 6 || i == 8 || i == 9 || i == 10 )
131  {
132  currRotation.push_back( currValue );
133  prevRotation.push_back( prevValue );
134  }
135  if ( i == 3 || i == 7 || i == 11 )
136  {
137  currTranslation.push_back( currValue );
138  prevTranslation.push_back( prevValue );
139  }
140  }
141 
142  if ( LinearObject::Distance( currRotation, prevRotation ) > ROTATION_THRESHOLD || LinearObject::Distance( currTranslation, prevTranslation ) > TRANSLATION_THRESHOLD )
143  {
144  this->FromXMLElement( currElement );
145  return true;
146  }
147 
148  return false;
149 }
static double Distance(std::vector< double > v1, std::vector< double > v2)
void Rotate(vnl_matrix< double > *rotation)
void Translate(std::vector< double > translation)
static std::vector< double > Add(std::vector< double > v1, std::vector< double > v2)
for i
static const int SIZE
void FromXMLElement(vtkXMLDataElement *element)
std::string ToXMLString()
const char const char * value
Definition: phidget22.h:5111