| 1 |
/* |
| 2 |
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
#ifndef SPARSE_MATRIX_INCLUDED |
| 29 |
#define SPARSE_MATRIX_INCLUDED |
| 30 |
|
| 31 |
#include <float.h> |
| 32 |
#include <complex> |
| 33 |
#include <unordered_map> |
| 34 |
#include "SparseMatrixInterface.h" |
| 35 |
#include "Array.h" |
| 36 |
|
| 37 |
namespace PoissonRecon |
| 38 |
{ |
| 39 |
|
| 40 |
template< class T , class IndexType , size_t MaxRowSize=0 > class SparseMatrix; |
| 41 |
|
| 42 |
template< class T , class IndexType > class SparseMatrix< T , IndexType , 0 > : public SparseMatrixInterface< T , ConstPointer( MatrixEntry< T , IndexType > ) > |
| 43 |
{ |
| 44 |
template< class T2 , class IndexType2 , size_t MaxRowSize2 > friend class SparseMatrix; |
| 45 |
Pointer( Pointer( MatrixEntry< T , IndexType > ) ) _entries; |
| 46 |
public: |
| 47 |
static void Swap( SparseMatrix& M1 , SparseMatrix& M2 ) |
| 48 |
{ |
| 49 |
std::swap( M1.rowNum , M2.rowNum ); |
| 50 |
std::swap( M1.rowSizes , M2.rowSizes ); |
| 51 |
std::swap( M1._entries , M2._entries ); |
| 52 |
} |
| 53 |
typedef SparseMatrixInterface< T , ConstPointer( MatrixEntry< T , IndexType > ) > Interface; |
| 54 |
typedef ConstPointer( MatrixEntry< T , IndexType > ) RowIterator; |
| 55 |
|
| 56 |
size_t rowNum; |
| 57 |
Pointer( size_t ) rowSizes; |
| 58 |
|
| 59 |
SparseMatrix( void ); |
| 60 |
SparseMatrix( const SparseMatrix& M ); |
| 61 |
SparseMatrix( SparseMatrix&& M ); |
| 62 |
template< class T2 , class IndexType2 > |
| 63 |
SparseMatrix( const SparseMatrix< T2 , IndexType2 , 0 >& M ); |
| 64 |
~SparseMatrix(); |
| 65 |
SparseMatrix& operator = ( SparseMatrix&& M ); |
| 66 |
SparseMatrix< T , IndexType >& operator = ( const SparseMatrix< T , IndexType >& M ); |
| 67 |
template< class T2 , class IndexType2 > |
| 68 |
SparseMatrix< T , IndexType , 0 >& operator = ( const SparseMatrix< T2 , IndexType2 , 0 >& M ); |
| 69 |
|
| 70 |
template< class T2 > void operator()( const T2* in , T2* out ) const; |
| 71 |
|
| 72 |
template< class T2 , class IndexType2 > |
| 73 |
SparseMatrix< T , IndexType , 0 >& copy( const SparseMatrix< T2 , IndexType2 , 0 >& M ); |
| 74 |
|
| 75 |
inline ConstPointer( MatrixEntry< T , IndexType > ) begin( size_t row ) const { return _entries[row]; } |
| 76 |
inline ConstPointer( MatrixEntry< T , IndexType > ) end ( size_t row ) const { return _entries[row] + (unsigned long long)rowSizes[row]; } |
| 77 |
inline size_t rows ( void ) const { return rowNum; } |
| 78 |
inline size_t rowSize ( size_t idx ) const { return rowSizes[idx]; } |
| 79 |
|
| 80 |
SparseMatrix( size_t rowNum ); |
| 81 |
void resize ( size_t rowNum ); |
| 82 |
void setRowSize( size_t row , size_t count ); |
| 83 |
void resetRowSize( size_t row , size_t count ); |
| 84 |
inline Pointer( MatrixEntry< T , IndexType > ) operator[] ( size_t idx ) { return _entries[idx]; } |
| 85 |
inline ConstPointer( MatrixEntry< T , IndexType > ) operator[] ( size_t idx ) const { return _entries[idx]; } |
| 86 |
|
| 87 |
// With copy move, these should be well-behaved from a memory perspective |
| 88 |
static SparseMatrix Identity( size_t dim ); |
| 89 |
SparseMatrix transpose( T (*TransposeFunction)( const T& )=NULL ) const; |
| 90 |
SparseMatrix transpose( size_t outRows , T (*TransposeFunction)( const T& )=NULL ) const; |
| 91 |
SparseMatrix operator * ( T s ) const; |
| 92 |
SparseMatrix operator / ( T s ) const; |
| 93 |
SparseMatrix operator * ( const SparseMatrix& M ) const; |
| 94 |
SparseMatrix operator + ( const SparseMatrix& M ) const; |
| 95 |
SparseMatrix operator - ( const SparseMatrix& M ) const; |
| 96 |
SparseMatrix& operator *= ( T s ); |
| 97 |
SparseMatrix& operator /= ( T s ); |
| 98 |
SparseMatrix& operator *= ( const SparseMatrix& M ); |
| 99 |
SparseMatrix& operator += ( const SparseMatrix& M ); |
| 100 |
SparseMatrix& operator -= ( const SparseMatrix& M ); |
| 101 |
|
| 102 |
template< class A_const_iterator , class B_const_iterator > |
| 103 |
static SparseMatrix Multiply( const SparseMatrixInterface< T , A_const_iterator >& A , const SparseMatrixInterface< T , B_const_iterator >& B ); |
| 104 |
template< class const_iterator > |
| 105 |
static SparseMatrix Transpose( const SparseMatrixInterface< T , const_iterator >& At , T (*TransposeFunction)( const T& )=NULL ); |
| 106 |
template< class const_iterator > |
| 107 |
static SparseMatrix Transpose( const SparseMatrixInterface< T , const_iterator >& At , size_t outRows , T (*TransposeFunction)( const T& )=NULL ); |
| 108 |
}; |
| 109 |
|
| 110 |
template< class T , class IndexType , size_t MaxRowSize > class SparseMatrix : public SparseMatrixInterface< T , ConstPointer( MatrixEntry< T , IndexType > ) > |
| 111 |
{ |
| 112 |
template< class T2 , class IndexType2 > friend class _SparseMatrix; |
| 113 |
Pointer( MatrixEntry< T , IndexType > ) _entries; |
| 114 |
size_t _rowNum; |
| 115 |
Pointer( size_t ) _rowSizes; |
| 116 |
size_t _maxRows; |
| 117 |
public: |
| 118 |
static void Swap( SparseMatrix& M1 , SparseMatrix& M2 ) |
| 119 |
{ |
| 120 |
std::swap( M1._rowNum , M2._rowNum ); |
| 121 |
std::swap( M1._rowSizes , M2._rowSizes ); |
| 122 |
std::swap( M1._entries , M2._entries ); |
| 123 |
} |
| 124 |
typedef SparseMatrixInterface< T , ConstPointer( MatrixEntry< T , IndexType > ) > Interface; |
| 125 |
typedef ConstPointer( MatrixEntry< T , IndexType > ) RowIterator; |
| 126 |
|
| 127 |
SparseMatrix( void ); |
| 128 |
SparseMatrix( const SparseMatrix& M ); |
| 129 |
SparseMatrix( SparseMatrix&& M ); |
| 130 |
template< class T2 , class IndexType2 > |
| 131 |
SparseMatrix( const SparseMatrix< T2 , IndexType2 , MaxRowSize >& M ); |
| 132 |
SparseMatrix& operator = ( SparseMatrix&& M ); |
| 133 |
SparseMatrix< T , IndexType , MaxRowSize >& operator = ( const SparseMatrix< T , IndexType , MaxRowSize >& M ); |
| 134 |
template< class T2 , class IndexType2 > |
| 135 |
SparseMatrix< T , IndexType , MaxRowSize >& operator = ( const SparseMatrix< T2 , IndexType2 , MaxRowSize >& M ); |
| 136 |
~SparseMatrix( void ); |
| 137 |
|
| 138 |
template< class T2 > void operator()( const T2* in , T2* out ) const; |
| 139 |
|
| 140 |
inline ConstPointer( MatrixEntry< T , IndexType > ) begin( size_t row ) const { return _entries + MaxRowSize * row; } |
| 141 |
inline ConstPointer( MatrixEntry< T , IndexType > ) end ( size_t row ) const { return _entries + MaxRowSize * row + (unsigned long long)_rowSizes[row]; } |
| 142 |
inline size_t rows ( void ) const { return _rowNum; } |
| 143 |
inline size_t rowSize ( size_t idx ) const { return _rowSizes[idx]; } |
| 144 |
|
| 145 |
SparseMatrix( size_t rowNum ); |
| 146 |
void resize ( size_t rowNum ); |
| 147 |
void setRowSize( size_t row , size_t rowSize ); |
| 148 |
void resetRowSize( size_t row , size_t rowSize ); |
| 149 |
inline Pointer( MatrixEntry< T , IndexType > ) operator[] ( size_t idx ) { return _entries + MaxRowSize * idx; } |
| 150 |
inline ConstPointer( MatrixEntry< T , IndexType > ) operator[] ( size_t idx ) const { return _entries + MaxRowSize * idx; } |
| 151 |
|
| 152 |
// With copy move, these should be well-behaved from a memory perspective |
| 153 |
SparseMatrix operator * ( T s ) const; |
| 154 |
SparseMatrix operator / ( T s ) const; |
| 155 |
SparseMatrix& operator *= ( T s ); |
| 156 |
SparseMatrix& operator /= ( T s ); |
| 157 |
}; |
| 158 |
|
| 159 |
#include "SparseMatrix.inl" |
| 160 |
} |
| 161 |
|
| 162 |
#endif // SPARSE_MATRIX_INCLUDED |