cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nmrIsOrthonormal.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Anton Deguet
7  Created on: 2005-07-27
8 
9  (C) Copyright 2005-2007 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 
28 #ifndef _nmrIsOrthonormal_h
29 #define _nmrIsOrthonormal_h
30 
31 
34 
35 
36 
63 template <class _elementType>
65 
66 public:
69 
70 protected:
73 
77  inline void SetDimension(size_type m)
78  {
79  Size = m * m;
80  }
81 
102  inline void AllocateWorkspace(bool allocateWorkspace)
103  {
104  if (allocateWorkspace) {
105  this->WorkspaceMemory.SetSize(Size);
106  this->WorkspaceReference.SetRef(this->WorkspaceMemory);
107  } else {
108  this->WorkspaceMemory.SetSize(0);
109  }
110  }
111 
119  template <typename __vectorOwnerTypeWorkspace>
120  inline void
122  throw(std::runtime_error)
123  {
124  if (Size > inWorkspace.size()) {
125  cmnThrow(std::runtime_error("nmrIsOrthonormalDynamicData: Workspace is too small."));
126  }
127  if (!inWorkspace.IsCompact()) {
128  cmnThrow(std::runtime_error("nmrIsOrthonormalDynamicData: Workspace must be compact."));
129  }
130  }
131 
136 
139 
140 public:
141 
149  {
150  return m * m;
151  }
152 
158  template <class __matrixOwnerTypeA>
160  {
162  }
163 
164 
165 #ifndef SWIG
166 #ifndef DOXYGEN
167 
173  class Friend {
174  private:
176  public:
177  Friend(nmrIsOrthonormalDynamicData &data): Data(data) {
178  }
180  return Data.WorkspaceReference;
181  }
182  inline size_type Size(void) {
183  return Data.Size;
184  }
185  };
186  friend class Friend;
187 #endif // DOXYGEN
188 #endif // SWIG
189 
198  Size(static_cast<size_type>(0))
199  {
200  AllocateWorkspace(false);
201  }
202 
214  {
215  this->Allocate(m);
216  }
217 
227  template <class __matrixOwnerTypeA>
229  {
230  this->Allocate(A);
231  }
232 
246  template <class __matrixOwnerTypeA, class __vectorOwnerTypeWorkspace>
249  {
250  this->SetRef(A, inWorkspace);
251  }
252 
253 
262  template <class __matrixOwnerTypeA>
264  {
265  this->Allocate(A.rows());
266  }
267 
277  template <class __matrixOwnerTypeA, class __vectorOwnerTypeWorkspace>
280  {
281  this->SetDimension(A.rows());
282 
283  // allocate output and set references
284  this->AllocateWorkspace(false);
285 
286  // set reference on user provided workspace
287  this->ThrowUnlessWorkspaceSizeIsCorrect(inWorkspace);
288  this->WorkspaceReference.SetRef(inWorkspace);
289  }
290 
298  {
299  this->SetDimension(m);
300  this->AllocateWorkspace(true);
301  }
302 };
303 
304 
305 
332 #ifndef SWIG
333 template <class _elementType, vct::size_type _rows>
335 {
336 public:
337 #ifndef DOXYGEN
338  enum {SIZE = _rows * _rows};
339 #endif // DOXYGEN
340 
343 
344 protected:
347 public:
348 #ifndef DOXYGEN
349 
355  class Friend {
356  private:
358  public:
360  }
362  return Data.WorkspaceMember;
363  }
364  };
365  friend class Friend;
366 #endif // DOXYGEN
367 
371 };
372 #endif // SWIG
373 
374 
375 
436 
445 template <vct::size_type _size, vct::stride_type _rowStride, vct::stride_type _colStride, typename _elementType, class _dataPtrType>
448  _elementType tolerance = cmnTypeTraits<_elementType>::Tolerance()) {
451  product.ProductOf(A, A.TransposeRef());
452  product.Diagonal().Subtract(_elementType(1));
453  return (product.LinfNorm() <= tolerance);
454 }
455 
463 template <vct::size_type _size, vct::stride_type _rowStride, vct::stride_type _colStride, typename _elementType, class _dataPtrType>
465  _elementType tolerance = cmnTypeTraits<_elementType>::Tolerance()) {
467  typedef typename InputType::MatrixValueType MatrixValueType;
468  MatrixValueType product;
469  product.ProductOf(A, A.TransposeRef());
470  product.Diagonal().Subtract(_elementType(1));
471  return (product.LinfNorm() <= tolerance);
472 }
473 
482 template <class _matrixOwnerTypeA, class _elementType>
485  _elementType tolerance = cmnTypeTraits<_elementType>::Tolerance())
486  throw (std::runtime_error)
487 {
488  typename nmrIsOrthonormalDynamicData<_elementType>::Friend dataFriend(data);
489 
490  /* check that this is a square matrix */
491  if (A.cols() != A.rows()) {
492  cmnThrow(std::runtime_error("nmrIsOrthonormal: Requires a square matrix."));
493  }
494  /* check that the workspace is large enough */
495  if ((A.cols() * A.rows()) > dataFriend.Size()) {
496  cmnThrow(std::runtime_error("nmrIsOrthonormal: The data provided is not large enough."));
497  }
499  product.SetRef(A.rows(), A.cols(), A.cols(), 1, dataFriend.Workspace().Pointer());
500  product.ProductOf(A, A.TransposeRef());
501  product.Diagonal().Subtract(_elementType(1));
502  return (product.LinfNorm() <= tolerance);
503 }
504 
512 template <class _matrixOwnerTypeA, typename _elementType>
514  _elementType tolerance = cmnTypeTraits<_elementType>::Tolerance())
515  throw (std::runtime_error)
516 {
518  data.Allocate(A);
519  return nmrIsOrthonormal(A, data, tolerance);
520 }
521 
530 template <class _matrixOwnerTypeA, class _vectorOwnerTypeWorkspace, typename _elementType>
533  _elementType tolerance = cmnTypeTraits<_elementType>::Tolerance())
534  throw (std::runtime_error)
535 {
537  data.SetRef(A, workspace);
538  return nmrIsOrthonormal(A, data, tolerance);
539 }
541 
542 
543 #endif // _nmrIsOrthonormal_h
544 
A template for a fixed size matrix with fixed spacing in memory.
Definition: vctFixedSizeConstMatrixBase.h:103
A vector object of dynamic size.
Definition: vctDynamicVector.h:127
Definition: nmrIsOrthonormal.h:173
Declaration of vctDynamicMatrix.
nmrIsOrthonormalFixedSizeData()
Definition: nmrIsOrthonormal.h:370
void Allocate(size_type m)
Definition: nmrIsOrthonormal.h:297
ThisType & Subtract(const vctDynamicConstVectorBase< __vectorOwnerType, _elementType > &otherVector)
Definition: vctDynamicVectorBase.h:825
Declaration of vctFixedSizeMatrix.
ThisType & ProductOf(const vctFixedSizeConstMatrixBase< _rows, _cols, __rowStride, __colStride, value_type, __dataPtrType > &matrix, const value_type scalar)
Definition: vctFixedSizeMatrixBase.h:909
size_type Size
Definition: nmrIsOrthonormal.h:138
Dynamic vector referencing existing memory.
Definition: vctDynamicVectorRef.h:77
Definition: nmrIsOrthonormal.h:355
size_t size_type
Definition: vctContainerTraits.h:35
VectorTypeWorkspace WorkspaceMember
Definition: nmrIsOrthonormal.h:345
Friend(nmrIsOrthonormalDynamicData &data)
Definition: nmrIsOrthonormal.h:177
Dynamic matrix referencing existing memory.
Definition: vctDynamicMatrixRef.h:74
Friend(nmrIsOrthonormalFixedSizeData< _elementType, _rows > &data)
Definition: nmrIsOrthonormal.h:359
ThisType & ProductOf(const vctDynamicConstMatrixBase< __matrixOwnerType, _elementType > &matrix, const value_type scalar)
Definition: vctDynamicMatrixBase.h:971
Definition: vctDynamicConstMatrixBase.h:77
void AllocateWorkspace(bool allocateWorkspace)
Definition: nmrIsOrthonormal.h:102
void SetRef(size_type rows, size_type cols, stride_type rowStride, stride_type colStride, pointer dataPointer)
Definition: vctDynamicMatrixRef.h:217
Definition: nmrIsOrthonormal.h:338
vctDynamicVectorRef< _elementType > & Workspace(void)
Definition: nmrIsOrthonormal.h:179
bool nmrIsOrthonormal(const vctFixedSizeConstMatrixBase< _size, _size, _rowStride, _colStride, _elementType, _dataPtrType > &A, nmrIsOrthonormalFixedSizeData< _elementType, _size > &data, _elementType tolerance=cmnTypeTraits< _elementType >::Tolerance())
Definition: nmrIsOrthonormal.h:446
size_type rows() const
Definition: vctDynamicConstMatrixBase.h:238
An implementation of the ``abstract'' vctFixedSizeMatrixBase.
Definition: vctFixedSizeMatrixRef.h:46
size_type cols() const
Definition: vctDynamicConstMatrixBase.h:243
void Allocate(const vctDynamicConstMatrixBase< __matrixOwnerTypeA, _elementType > &A)
Definition: nmrIsOrthonormal.h:263
size_type Size(void)
Definition: nmrIsOrthonormal.h:182
vctDynamicVector< _elementType > WorkspaceMemory
Definition: nmrIsOrthonormal.h:72
nmrIsOrthonormalDynamicData(size_type m)
Definition: nmrIsOrthonormal.h:213
ConstRefTransposeType TransposeRef(void) const
Definition: vctFixedSizeConstMatrixRef.h:172
#define cmnThrow(a)
Definition: MinimalCmn.h:4
Data (workspace) for nmrIsOrthonormal (Dynamic).
Definition: nmrIsOrthonormal.h:64
nmrIsOrthonormalDynamicData()
Definition: nmrIsOrthonormal.h:197
Data for nmrIsOrthonormal (Fixed size).
Definition: nmrIsOrthonormal.h:334
VectorTypeWorkspace & Workspace(void)
Definition: nmrIsOrthonormal.h:361
nmrIsOrthonormalDynamicData(const vctDynamicConstMatrixBase< __matrixOwnerTypeA, _elementType > &A)
Definition: nmrIsOrthonormal.h:228
ConstRefTransposeType TransposeRef(void) const
Definition: vctDynamicConstMatrixBase.h:980
vct::size_type size_type
Definition: nmrIsOrthonormal.h:68
vctFixedSizeVector< _elementType, SIZE > VectorTypeWorkspace
Definition: nmrIsOrthonormal.h:342
pointer Pointer(size_type index=0)
Definition: vctFixedSizeVectorBase.h:226
void ThrowUnlessWorkspaceSizeIsCorrect(vctDynamicVectorBase< __vectorOwnerTypeWorkspace, _elementType > &inWorkspace) const
Definition: nmrIsOrthonormal.h:121
A collection of useful information about the C++ basic types, represented in a generic programming wa...
Definition: cmnTypeTraits.h:155
static size_type WorkspaceSize(size_type m)
Definition: nmrIsOrthonormal.h:148
DiagonalRefType Diagonal(void)
Definition: vctDynamicMatrixBase.h:239
static size_type WorkspaceSize(const vctDynamicConstMatrixBase< __matrixOwnerTypeA, _elementType > &A)
Definition: nmrIsOrthonormal.h:159
value_type LinfNorm(void) const
Definition: vctDynamicConstMatrixBase.h:488
void SetRef(const vctDynamicConstMatrixBase< __matrixOwnerTypeA, _elementType > &A, vctDynamicVectorBase< __vectorOwnerTypeWorkspace, _elementType > &inWorkspace)
Definition: nmrIsOrthonormal.h:278
nmrIsOrthonormalDynamicData(const vctDynamicConstMatrixBase< __matrixOwnerTypeA, _elementType > &A, vctDynamicVectorBase< __vectorOwnerTypeWorkspace, _elementType > &inWorkspace)
Definition: nmrIsOrthonormal.h:247
Definition: vctDynamicVectorBase.h:61
vctDynamicVectorRef< _elementType > WorkspaceReference
Definition: nmrIsOrthonormal.h:135
void SetDimension(size_type m)
Definition: nmrIsOrthonormal.h:77