PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
Line.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2 Program: Plus
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 #include "PlusConfigure.h"
8 
9 #include "Line.h"
10 
12 {
13  this->Type = "Line";
14 }
15 
16 //-----------------------------------------------------------------------------
17 
18 Line::Line( std::vector<double> newBasePoint, std::vector<double> newEndPoint )
19 {
20  this->Type = "Line";
21  this->BasePoint = newBasePoint;
22  this->EndPoint = newEndPoint;
23 }
24 
25 //-----------------------------------------------------------------------------
26 
28 {
29  this->EndPoint.clear();
30 }
31 
32 //-----------------------------------------------------------------------------
33 
34 std::vector<double> Line::GetDirection()
35 {
36  std::vector<double> vector = Subtract( this->EndPoint, this->BasePoint );
37  return Multiply( 1 / Norm( vector ), vector );
38 }
39 
40 //-----------------------------------------------------------------------------
41 
42 std::vector<double> Line::ProjectVector( std::vector<double> vector )
43 {
44  std::vector<double> outVec = Subtract( vector, this->BasePoint );
45  return Add( Multiply( Dot( this->GetDirection(), outVec ), this->GetDirection() ), this->BasePoint );
46 }
47 
48 //-----------------------------------------------------------------------------
49 
50 void Line::Translate( std::vector<double> vector )
51 {
52  for ( unsigned int i = 0; i < vector.size(); i++ )
53  {
54  this->BasePoint.at(i) = this->BasePoint.at(i) + vector.at(i);
55  this->EndPoint.at(i) = this->EndPoint.at(i) + vector.at(i);
56  }
57 }
58 
59 //-----------------------------------------------------------------------------
60 
61 std::vector<double> Line::GetOrthogonalNormal1()
62 {
63  // Find the two axis unit vectors least parallel with the direction vector
64  std::vector<double> e1( 3, 0.0 );
65  std::vector<double> e2( 3, 0.0 );
66  if ( fabs( this->GetDirection().at(1) ) <= fabs( this->GetDirection().at(0) ) && fabs( this->GetDirection().at(2) ) <= fabs( this->GetDirection().at(0) ) )
67  {
68  e1.at(0) = 0; e1.at(1) = 1; e1.at(2) = 0;
69  e2.at(0) = 0; e2.at(1) = 0; e2.at(2) = 1;
70  }
71  if ( fabs( this->GetDirection().at(0) ) <= fabs( this->GetDirection().at(1) ) && fabs( this->GetDirection().at(2) ) <= fabs( this->GetDirection().at(1) ) )
72  {
73  e1.at(0) = 1; e1.at(1) = 0; e1.at(2) = 0;
74  e2.at(0) = 0; e2.at(1) = 0; e2.at(2) = 1;
75  }
76  if ( fabs( this->GetDirection().at(0) ) <= fabs( this->GetDirection().at(2) ) && fabs( this->GetDirection().at(1) ) <= fabs( this->GetDirection().at(2) ) )
77  {
78  e1.at(0) = 1; e1.at(1) = 0; e1.at(2) = 0;
79  e2.at(0) = 0; e2.at(1) = 1; e2.at(2) = 0;
80  }
81 
82  std::vector<double> Normal1 = Subtract( e1, Multiply( Dot( e1, this->GetDirection() ), this->GetDirection() ) );
83  Normal1 = Multiply( 1 / Norm( Normal1 ), Normal1 );
84 
85  std::vector<double> Normal2 = Subtract( e2, Add( Multiply( Dot( e2, this->GetDirection() ), this->GetDirection() ), Multiply( Dot( e2, Normal1 ), Normal1 ) ) );
86  Normal2 = Multiply( 1 / Norm( Normal2 ), Normal2 );
87 
88  return Normal1;
89 }
90 
91 //-----------------------------------------------------------------------------
92 
93 std::vector<double> Line::GetOrthogonalNormal2()
94 {
95  // Find the two axis unit vectors least parallel with the direction vector
96  std::vector<double> e1( 3, 0.0 );
97  std::vector<double> e2( 3, 0.0 );
98  if ( fabs( this->GetDirection().at(1) ) <= fabs( this->GetDirection().at(0) ) && fabs( this->GetDirection().at(2) ) <= fabs( this->GetDirection().at(0) ) )
99  {
100  e1.at(0) = 0; e1.at(1) = 1; e1.at(2) = 0;
101  e2.at(0) = 0; e2.at(1) = 0; e2.at(2) = 1;
102  }
103  if ( fabs( this->GetDirection().at(0) ) <= fabs( this->GetDirection().at(1) ) && fabs( this->GetDirection().at(2) ) <= fabs( this->GetDirection().at(1) ) )
104  {
105  e1.at(0) = 1; e1.at(1) = 0; e1.at(2) = 0;
106  e2.at(0) = 0; e2.at(1) = 0; e2.at(2) = 1;
107  }
108  if ( fabs( this->GetDirection().at(0) ) <= fabs( this->GetDirection().at(2) ) && fabs( this->GetDirection().at(1) ) <= fabs( this->GetDirection().at(2) ) )
109  {
110  e1.at(0) = 1; e1.at(1) = 0; e1.at(2) = 0;
111  e2.at(0) = 0; e2.at(1) = 1; e2.at(2) = 0;
112  }
113 
114  std::vector<double> Normal1 = Subtract( e1, Multiply( Dot( e1, this->GetDirection() ), this->GetDirection() ) );
115  Normal1 = Multiply( 1 / Norm( Normal1 ), Normal1 );
116 
117  std::vector<double> Normal2 = Subtract( e2, Add( Multiply( Dot( e2, this->GetDirection() ), this->GetDirection() ), Multiply( Dot( e2, Normal1 ), Normal1 ) ) );
118  Normal2 = Multiply( 1 / Norm( Normal2 ), Normal2 );
119 
120  return Normal2;
121 }
122 
123 //-----------------------------------------------------------------------------
124 
125 std::string Line::ToXMLString() const
126 {
127  std::ostringstream xmlstring;
128 
129  xmlstring << " <Line";
130  xmlstring << " Name=\"" << this->Name << "\"";
131  xmlstring << " BasePoint=\"" << VectorToString( this->BasePoint ) << "\"";
132  xmlstring << " EndPoint=\"" << VectorToString( this->EndPoint ) << "\"";
133  xmlstring << " />" << std::endl;
134 
135  return xmlstring.str();
136 }
137 
138 //-----------------------------------------------------------------------------
139 
140 void Line::FromXMLElement( vtkXMLDataElement* element )
141 {
142 
143  if ( strcmp( element->GetName(), "Line" ) != 0 )
144  {
145  return; // If it's not a "log" or is the wrong tool jump to the next.
146  }
147 
148  this->Name = std::string( element->GetAttribute( "Name" ) );
149  this->BasePoint = StringToVector( std::string( element->GetAttribute( "BasePoint" ) ), 3 );
150  this->EndPoint = StringToVector( std::string( element->GetAttribute( "EndPoint" ) ), 3 );
151 
152 }
~Line()
Definition: Line.cxx:27
static double Norm(std::vector< double > vector)
Line()
Definition: Line.cxx:11
virtual void FromXMLElement(vtkXMLDataElement *element)
Definition: Line.cxx:140
static std::vector< double > Add(std::vector< double > v1, std::vector< double > v2)
std::vector< double > BasePoint
Definition: LinearObject.h:23
std::vector< double > GetDirection()
Definition: Line.cxx:34
std::vector< double > ProjectVector(std::vector< double > vector)
Definition: Line.cxx:42
for i
std::vector< double > GetOrthogonalNormal2()
Definition: Line.cxx:93
static double Dot(std::vector< double > v1, std::vector< double > v2)
std::vector< double > EndPoint
Definition: Line.h:20
virtual std::string ToXMLString() const
Definition: Line.cxx:125
static std::vector< double > StringToVector(std::string s, int size)
static std::string VectorToString(std::vector< double > vector)
std::vector< double > GetOrthogonalNormal1()
Definition: Line.cxx:61
std::string Name
Definition: LinearObject.h:20
void Translate(std::vector< double > vector)
Definition: Line.cxx:50
static std::vector< double > Multiply(double c, std::vector< double > vector)
static std::vector< double > Subtract(std::vector< double > v1, std::vector< double > v2)
std::string Type
Definition: LinearObject.h:21