Orfeo Toolbox  3.16
itkPoint.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkPoint.txx,v $
5  Language: C++
6  Date: $Date: 2007-01-23 13:15:24 $
7  Version: $Revision: 1.50 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #ifndef __itkPoint_txx
19 #define __itkPoint_txx
20 #include "itkPoint.h"
21 #include <vnl/vnl_math.h>
22 #include "itkObject.h"
23 
24 
25 namespace itk
26 {
27 
28 
32 template<class T, unsigned int TPointDimension>
33 Point<T, TPointDimension>&
35 ::operator= (const Self& r)
36 {
37  BaseArray::operator=(r);
38  return *this;
39 }
40 
41 
45 template<class T, unsigned int TPointDimension>
48 ::operator= (const ValueType r[TPointDimension])
49 {
50  BaseArray::operator=(r);
51  return *this;
52 }
53 
54 
58 template<class T, unsigned int TPointDimension>
61 ::operator+=( const VectorType & vec )
62 {
63  for( unsigned int i=0; i<TPointDimension; i++)
64  {
65  (*this)[i] += vec[i];
66  }
67  return *this;
68 }
69 
70 
74 template<class T, unsigned int TPointDimension>
77 ::operator-=( const VectorType & vec )
78 {
79  for( unsigned int i=0; i<TPointDimension; i++)
80  {
81  (*this)[i] -= vec[i];
82  }
83  return *this;
84 }
85 
86 
87 
88 /*
89  * Add operator with a vector
90  */
91 template<class T, unsigned int TPointDimension>
94 ::operator+( const VectorType & vec ) const
95 {
96  Self result;
97  for( unsigned int i=0; i<TPointDimension; i++)
98  {
99  result[i] = (*this)[i] + vec[i];
100  }
101  return result;
102 }
103 
104 /*
105  * Subtract a vector, return a point
106  */
107 template<class T, unsigned int TPointDimension>
110 ::operator-( const VectorType & vec ) const
111 {
112  Self result;
113  for( unsigned int i=0; i<TPointDimension; i++)
114  {
115  result[i] = (*this)[i] - vec[i];
116  }
117  return result;
118 }
119 
120 /*
121  * Difference between two points
122  */
123 template<class T, unsigned int TPointDimension>
126 ::operator-( const Self & pnt ) const
127 {
128  VectorType result;
129  for( unsigned int i=0; i<TPointDimension; i++)
130  {
131  result[i] = (*this)[i] - pnt[i];
132  }
133  return result;
134 }
135 
136 /*
137  * Return a vnl_vector_ref
138  */
139 template<class T, unsigned int TPointDimension >
140 vnl_vector_ref< T >
143 {
144  return vnl_vector_ref< T >( TPointDimension, this->GetDataPointer());
145 }
146 
150 template<class T, unsigned int TPointDimension>
153 ::GetVnlVector( void ) const
154 {
155  // Return a vector_ref<>. This will be automatically converted to a
156  // vnl_vector<>. We have to use a const_cast<> which would normally
157  // be prohibited in a const method, but it is safe to do here
158  // because the cast to vnl_vector<> will ultimately copy the data.
159  return vnl_vector_ref<T>( TPointDimension,
160  const_cast<T*>(this->GetDataPointer()));
161 }
162 
166 template<class T, unsigned int TPointDimension >
167 vnl_vector_ref< T >
170 {
171  return vnl_vector_ref< T >( TPointDimension, this->GetDataPointer());
172 }
173 
177 template<class T, unsigned int TPointDimension>
180 ::Get_vnl_vector( void ) const
181 {
182  // Return a vector_ref<>. This will be automatically converted to a
183  // vnl_vector<>. We have to use a const_cast<> which would normally
184  // be prohibited in a const method, but it is safe to do here
185  // because the cast to vnl_vector<> will ultimately copy the data.
186  return vnl_vector_ref<T>( TPointDimension,
187  const_cast<T*>(this->GetDataPointer()));
188 }
189 
190 template<class T, unsigned int TPointDimension >
194 {
195  // VectorType knows how to construct from ValueType*.
196  return &(*this)[0];
197 }
198 
202 template<class T, unsigned int TPointDimension>
203 void
205 ::SetToMidPoint( const Self & A, const Self & B )
206 {
207  for( unsigned int i=0; i<TPointDimension; i++)
208  {
209  (*this)[i] = ( A[i] + B[i] ) /2.0;
210  }
211 }
212 
216 template<class T, unsigned int TPointDimension>
217 void
220  const Self & B,
221  double alpha )
222 {
223  const double wa = alpha;
224  const double wb = 1.0 - alpha;
225  for( unsigned int i=0; i<TPointDimension; i++)
226  {
227  (*this)[i] = wa * A[i] + wb * B[i];
228  }
229 }
230 
234 template<class T, unsigned int TPointDimension>
235 void
238  const Self & B,
239  const Self & C,
240  double weightForA,
241  double weightForB )
242 {
243  const double weightForC = 1.0 - weightForA - weightForB;
244  for( unsigned int i=0; i<TPointDimension; i++)
245  {
246  (*this)[i] = weightForA * A[i] + weightForB * B[i] + weightForC * C[i];
247  }
248 }
249 
253 template<class T, unsigned int TPointDimension>
254 void
257  const double * weights, unsigned int N )
258 {
259  this->Fill( NumericTraits<T>::Zero ); // put this point to null
260  double weightSum = 0.0;
261  for( unsigned int j=0; j<N-1; j++)
262  {
263  const double weight = weights[j];
264  weightSum += weight;
265  for( unsigned int i=0; i<TPointDimension; i++)
266  {
267  (*this)[i] += weight * P[j][i];
268  }
269  }
270  const double weight = ( 1.0 - weightSum );
271  for( unsigned int i=0; i<TPointDimension; i++)
272  {
273  (*this)[i] += weight * P[N-1][i];
274  }
275 
276 }
277 
281 template< class TPointContainer, class TWeightContainer >
283 ::PointType
286  const PointContainerPointer & points,
287  const WeightContainerType & weights )
288 {
289 
290  typedef typename TPointContainer::Element PointType;
291  typedef typename PointType::ValueType ValueType;
292  PointType barycentre;
293  barycentre.Fill( NumericTraits< ValueType >::Zero ); // set to null
294 
295  typename TPointContainer::Iterator point = points->Begin();
296  typename TPointContainer::Iterator final = points->End();
297  typename TPointContainer::Iterator last = final;
298  last--; // move to the (N)th point
299 
300  double weightSum = 0.0;
301 
302  const unsigned int PointDimension = PointType::PointDimension;
303  unsigned int j = 0;
304 
305  while( point != last )
306  {
307  const double weight = weights[j++];
308  weightSum += weight;
309  for( unsigned int i=0; i<PointDimension; i++)
310  {
311  barycentre[i] += weight * (point->Value())[i];
312  }
313  point++;
314  }
315 
316  // Compute directly the last one
317  // to make sure that the weights sum 1
318  const double weight = ( 1.0 - weightSum );
319  for( unsigned int i=0; i<PointDimension; i++)
320  {
321  barycentre[i] += weight * (last->Value())[i];
322  }
323 
324  return barycentre;
325 
326 }
327 
331 template<class T, unsigned int TPointDimension>
332 std::ostream &
333 operator<<(std::ostream& os,const Point<T,TPointDimension> & vct )
334 {
335  os << "[";
336  if ( TPointDimension == 1)
337  {
338  os << vct[0];
339  }
340  else
341  {
342  for( unsigned int i=0; i+1<TPointDimension; i++)
343  {
344  os << vct[i] << ", ";
345  }
346  os << vct[TPointDimension-1];
347  }
348  os << "]";
349  return os;
350 }
351 
355 template<class T, unsigned int TPointDimension>
356 std::istream &
357 operator>>(std::istream& is, Point<T,TPointDimension> & vct )
358 {
359  for( unsigned int i=0; i<TPointDimension; i++)
360  {
361  is >> vct[i];
362  }
363  return is;
364 }
365 
366 } // end namespace itk
367 
368 #endif

Generated at Sat Feb 2 2013 23:59:39 for Orfeo Toolbox with doxygen 1.8.1.1