MINT2
HyperPlane.cpp
Go to the documentation of this file.
1 #include "Mint/HyperPlane.h"
2 #include <math.h>
3 
8  NPlane(a)
9 {
10  WELCOME_LOG << "Hello from the HyperPlane() Constructor";
11 
12  if ( a.getDimension() < (int)a.size() ) ERROR_LOG << "You only need #dim points to define a hyperplane - I'll just use the first #dim";
13  if ( a.getDimension() > (int)a.size() ) ERROR_LOG << "You have not provided enough points to define a hyperplane - I'll probably crash soon";
14 
15 }
16 
20 void HyperPlane::print(std::ostream& os, int endline) const{
21 
22  os << "HyperPlane: v = ";
23  _origin.print(os, 0);
24 
25  for (unsigned int i = 0; i < _v.size(); i++){
26  os << " + ";
27  _v.at(i).print(os, 0);
28  os << "t_" << i;
29  }
30  if (endline) os << std::endl;
31 
32 }
33 
37 bool HyperPlane::pointInPlane(const HyperPoint& hyperPoint) const{
38 
39 
40  HyperPoint interceptionParameters = findPlaneIntersectionParameters(hyperPoint);
41 
42  if (interceptionParameters.size() == 0) return false;
43  HyperPoint pointInPlane = getParametricPoint(interceptionParameters);
44 
45  HyperPoint vectorDifference = hyperPoint - pointInPlane;
46 
47  double difference = 0.0;
48  for (int i = 0; i < getDimension(); i++) difference += fabs(vectorDifference.at(i));
49 
50  if( fabs(difference) < 1e-2 ) return true;
51 
52  return false;
53 
54 }
55 
56 
57 HyperPoint HyperPlane::findPlaneIntersectionParameters(const HyperPoint& hyperPoint, int dimToOmmit) const{
58 
59  if (hyperPoint.getDimension() != getDimension()){
60  ERROR_LOG << "Your HyperPlane and HyperPoint have different dimensionality. Returning 0";
61  HyperPoint(0);
62  }
63 
64  int oldErrorLevel = gErrorIgnoreLevel;
65  gErrorIgnoreLevel = 5000;
66 
67  int dim = getDimension();
68  int reducedDim = dim -1;
69 
70  std::vector<int> linIndep;
71  for (int i = 0; i < dim; i++){
72  if (dimToOmmit != i) linIndep.push_back(i);
73  }
74 
75  HyperPoint lhs = hyperPoint - getOrigin();
76 
77  TMatrixD vectorD(reducedDim, 1);
78  for (int i = 0; i < reducedDim ; i++){
79  vectorD(i,0) = lhs.at(linIndep.at(i));
80  }
81 
82  TMatrixD matrix(reducedDim, reducedDim);
83  for (int j = 0; j < reducedDim; j++){
84  for (int i = 0; i < reducedDim; i++){
85  matrix(i,j) = getDirection(j).at(linIndep.at(i));
86  }
87  }
88 
89  TDecompLU lu(matrix);
90 
91  //std::cout << "getting eigenVectors" << std::endl;
92  //TVectorT<double> eigenValues;
93  //matrix.EigenVectors(eigenValues);
94  //std::cout << "done" << std::endl;
95 
96 
97 
98  if( !lu.Decompose() ) {
99 
100  if (dimToOmmit != (dim -1)) return findPlaneIntersectionParameters(hyperPoint, dimToOmmit + 1);
101  VERBOSE_LOG << "Matrix is singular. Cannot find solution.";
102  matrix.Print();
103 
104  gErrorIgnoreLevel = oldErrorLevel;
105 
106  return HyperPoint(0);
107  }
108 
109  VERBOSE_LOG << "Matrix before inversion";
110 
111  double determinant = 0.0;
112  matrix.Invert(&determinant);
113  VERBOSE_LOG << "Determinant = " << determinant;
114 
115  VERBOSE_LOG << "Matrix after inversion";
116 
117  TMatrixD result = matrix*vectorD;
118 
119  HyperPoint interceptionParameter(reducedDim);
120  for (int i = 0; i < reducedDim; i++){
121  interceptionParameter.at(i) = result(i,0);
122  }
123 
124  VERBOSE_LOG << "All done - returning parameters";
125 
126  gErrorIgnoreLevel = oldErrorLevel;
127 
128  return interceptionParameter;
129 
130 }
131 
133 
134  HyperPoint lineIntersectionParameter = findLineIntersectionParameter(hyperLine);
135 
136  if (lineIntersectionParameter.getDimension()==0) return lineIntersectionParameter;
137 
138  return hyperLine.getParametricPoint(lineIntersectionParameter.at(0));
139 
140 }
141 
143 
144  TMatrixD solution = findIntersectionSolution(hyperLine);
145  if (solution.GetNcols() == 0) return HyperPoint(0);
146  return HyperPoint(solution(0,0));
147 
148 }
149 
151 
152  TMatrixD solution = findIntersectionSolution(hyperLine);
153  if (solution.GetNcols() == 0) return HyperPoint(0);
154 
155  HyperPoint point(getDimension() - 1);
156 
157  for (int i = 1; i < getDimension(); i++){
158  point.at(i-1) = solution(i,0);
159  }
160 
161  return point;
162 
163 }
164 
165 TMatrixD HyperPlane::findIntersectionSolution(const HyperLine& hyperLine) const{
166 
167  if (hyperLine.getDimension() != getDimension()){
168  ERROR_LOG << "Your HyperPlane and HyperLine have different dimensionality. Returning 0";
169  return TMatrixD(0,0);
170  }
171 
172  int oldErrorLevel = gErrorIgnoreLevel;
173  gErrorIgnoreLevel = 5000;
174 
175  HyperPoint lhs = hyperLine.getOrigin() - getOrigin();
176 
177  VERBOSE_LOG << "Making Column Vector";
178  TMatrixD vectorD(getDimension(), 1);
179 
180  for (int i = 0; i < getDimension(); i++){
181  vectorD(i,0) = lhs.at(i);
182  }
183 
184  VERBOSE_LOG << "Making Matrix";
185  TMatrixD matrix(getDimension(), getDimension());
186 
187  VERBOSE_LOG << "Filling Column 0";
188  for (int i = 0; i < getDimension(); i++){
189  matrix(i,0) = -hyperLine.getDirection().at(i);
190  }
191 
192  for (int j = 1; j < getDimension(); j++){
193  VERBOSE_LOG << "Filling Column " << j;
194  for (int i = 0; i < getDimension(); i++){
195  matrix(i,j) = getDirection(j - 1).at(i);
196  }
197  }
198 
199  TDecompLU lu(matrix);
200  if( !lu.Decompose() ) {
201  VERBOSE_LOG << "Matrix is singular. Cannot find solution.";
202  gErrorIgnoreLevel = oldErrorLevel;
203  return TMatrixD(0,0);
204  }
205 
206  VERBOSE_LOG << "Matrix before inversion";
207  //if (_verbose) matrix.Print();
208 
209  double determinant = 0.0;
210  matrix.Invert(&determinant);
211  VERBOSE_LOG << "Determinant = " << determinant;
212 
213  VERBOSE_LOG << "Matrix after inversion";
214  //if (_verbose) matrix.Print();
215 
216  TMatrixD result = matrix*vectorD;
217 
218  gErrorIgnoreLevel = oldErrorLevel;
219 
220  return result;
221 
222 }
223 
227 bool HyperPlane::operator ==(const HyperPlane& other) const{
228  if (other.getDimension() != this->getDimension()) {
229  ERROR_LOG << "Trying to compare HyperPlanes of different dimensions, returning false";
230  return false;
231  }
232  if ( this->pointInPlane(other.getOrigin()) == 0) return false;
233 
234  for (int i = 0; i < getDimension() - 1; i++){
235  HyperPoint parameters(getDimension() - 1);
236  parameters.at(i) = 1;
237  HyperPoint pointInOtherPlane = other.getParametricPoint(parameters);
238  if ( this->pointInPlane(pointInOtherPlane) == 0) return false;
239  }
240  return true;
241 }
242 
243 
245  GOODBYE_LOG << "Goodbye from the HyperPlane() Constructor";
246 }
247 
248 
249 
virtual void print(std::ostream &os=std::cout, int endline=1) const
Definition: HyperPlane.cpp:20
bool pointInPlane(const HyperPoint &hyperPoint) const
Definition: HyperPlane.cpp:37
int size() const
Definition: HyperPoint.h:96
HyperPointSet _v
Definition: NPlane.h:36
virtual void print(std::ostream &os=std::cout, int endline=1) const
Definition: HyperPoint.cpp:453
virtual const HyperPoint & getDirection(int i=0) const
Definition: NPlane.h:54
Definition: NPlane.h:30
const HyperPoint & getOrigin() const
Definition: NPlane.h:51
#define ERROR_LOG
int getDimension() const
Definition: NPlane.h:45
bool operator==(const HyperPlane &other) const
Definition: HyperPlane.cpp:227
HyperPoint findLineIntersectionParameter(const HyperLine &hyperLine) const
Definition: HyperPlane.cpp:142
#define VERBOSE_LOG
HyperPlane(const HyperPointSet &a)
Definition: HyperPlane.cpp:7
const HyperPoint & at(int i) const
HyperPoint _origin
Definition: NPlane.h:34
HyperPoint findPlaneIntersectionParameters(const HyperPoint &hyperPoint, int dimToOmmit=0) const
Definition: HyperPlane.cpp:57
#define GOODBYE_LOG
const double & at(int i) const
Definition: HyperPoint.cpp:433
const int & getDimension() const
Definition: HyperPointSet.h:40
unsigned int size() const
#define WELCOME_LOG
TMatrixD findIntersectionSolution(const HyperLine &hyperLine) const
Definition: HyperPlane.cpp:165
int getDimension() const
Definition: HyperPoint.h:99
HyperPoint getParametricPoint(const HyperPoint &t) const
Definition: NPlane.cpp:40
HyperPoint findIntersectionPoint(const HyperLine &hyperLine) const
Definition: HyperPlane.cpp:132