Debian Codegraph @

src/thirdparty/PoissonRecon/SparseMatrixInterface.h

raw
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
29 #ifndef SPARSE_MATRIX_INTERFACE_INCLUDED
30 #define SPARSE_MATRIX_INTERFACE_INCLUDED
31
32 #define FORCE_TWO_BYTE_ALIGNMENT
33 #include "MyMiscellany.h"
34 #include "Array.h"
35
36 namespace PoissonRecon
37 {
38
39 #ifdef FORCE_TWO_BYTE_ALIGNMENT
40 #pragma pack(push)
41 #pragma pack(2)
42 #endif // FORCE_TWO_BYTE_ALIGNMENT
43 template< class T , class IndexType >
44 struct MatrixEntry
45 {
46 MatrixEntry( void ) { N =-1 , Value = 0; }
47 MatrixEntry( IndexType i ) { N = i , Value = 0; }
48 MatrixEntry( IndexType n , T v ){ N = n , Value = v; }
49 IndexType N;
50 T Value;
51 };
52
53 #ifdef FORCE_TWO_BYTE_ALIGNMENT
54 #pragma pack(pop)
55 #endif // FORCE_TWO_BYTE_ALIGNMENT
56
57 enum
58 {
59 MULTIPLY_ADD = 1 ,
60 MULTIPLY_NEGATE = 2
61 };
62
63 //#pragma message( "[WARNING] make me templated off of IndexType as well" )
64 template< class T , class const_iterator > class SparseMatrixInterface
65 {
66 public:
67 virtual const_iterator begin( size_t row ) const = 0;
68 virtual const_iterator end ( size_t row ) const = 0;
69 virtual size_t rows ( void ) const = 0;
70 virtual size_t rowSize( size_t idx ) const = 0;
71
72 size_t entries( void ) const;
73
74 double squareNorm( void ) const;
75 double squareASymmetricNorm( void ) const;
76 double squareASymmetricNorm( size_t &idx1 , size_t &idx2 ) const;
77
78 template< class T2 > void multiply ( ConstPointer( T2 ) In , Pointer( T2 ) Out , char multiplyFlag=0 ) const;
79 template< class T2 > void multiplyScaled( T scale , ConstPointer( T2 ) In , Pointer( T2 ) Out , char multiplyFlag=0 ) const;
80 template< class T2 > void multiply ( Pointer( T2 ) In , Pointer( T2 ) Out , char multiplyFlag=0 ) const { multiply ( ( ConstPointer(T2) )( In ) , Out , multiplyFlag ); }
81 template< class T2 > void multiplyScaled( T scale , Pointer( T2 ) In , Pointer( T2 ) Out , char multiplyFlag=0 ) const { multiplyScaled( scale , ( ConstPointer(T2) )( In ) , Out , multiplyFlag ); }
82
83 void setDiagonal( Pointer( T ) diagonal ) const;
84 void setDiagonalR( Pointer( T ) diagonal ) const;
85 template< class T2 > void jacobiIteration( ConstPointer( T ) diagonal , ConstPointer( T2 ) b , ConstPointer( T2 ) in , Pointer( T2 ) out , bool dReciprocal ) const;
86 template< class T2 > void gsIteration( const std::vector< size_t > & multiColorIndices , ConstPointer( T ) diagonal , ConstPointer( T2 ) b , Pointer( T2 ) x , bool dReciprocal ) const;
87 template< class T2 > void gsIteration( const std::vector< std::vector< size_t > >& multiColorIndices , ConstPointer( T ) diagonal , ConstPointer( T2 ) b , Pointer( T2 ) x , bool forward , bool dReciprocal ) const;
88 template< class T2 > void gsIteration( ConstPointer( T ) diagonal , ConstPointer( T2 ) b , Pointer( T2 ) x , bool forward , bool dReciprocal ) const;
89 };
90
91 // Assuming that the SPDOperator class defines:
92 // auto SPDOperator::()( ConstPointer( T ) , Pointer( T ) ) const
93 template< class SPDFunctor , class T , typename Real , class TDotTFunctor > size_t SolveCG( const SPDFunctor& M , size_t dim , ConstPointer( T ) b , size_t iters , Pointer( T ) x , double eps , TDotTFunctor Dot );
94 template< class SPDFunctor , class Preconditioner , class T , typename Real , class TDotTFunctor > size_t SolveCG( const SPDFunctor& M , const Preconditioner& P , size_t dim , ConstPointer( T ) b , size_t iters , Pointer( T ) x , double eps , TDotTFunctor Dot );
95
96 template< typename T >
97 struct AbstractArrayWrapper
98 {
99 virtual T operator[]( size_t i ) const = 0;
100 };
101 template< typename T , typename AbstractArrayWrapper1 , typename AbstractArrayWrapper2 > struct _VectorSum;
102 template< typename T , typename AbstractArrayWrapper1 , typename AbstractArrayWrapper2 > struct _VectorDifference;
103 template< typename T , typename Real , typename const_iterator , typename _AbstractArrayWrapper > struct _VectorProduct;
104
105 template< typename T , typename AbstractArrayWrapper1 , typename AbstractArrayWrapper2 >
106 struct _VectorSum : public AbstractArrayWrapper< T >
107 {
108 _VectorSum( const AbstractArrayWrapper1 &v1 , const AbstractArrayWrapper2 &v2 ) : _v1(v1) , _v2(v2)
109 {
110 // static_assert( std::is_convertible< AbstractArrayWrapper1 , AbstractArrayWrapper< T > >::value || std::is_convertible< AbstractArrayWrapper1 , ConstPointer(T) >::value , "[ERROR] Bad AbstractArrayWrapper1" );
111 // static_assert( std::is_convertible< AbstractArrayWrapper2 , AbstractArrayWrapper< T > >::value || std::is_convertible< AbstractArrayWrapper2 , ConstPointer(T) >::value , "[ERROR] Bad AbstractArrayWrapper2" );
112 }
113 T operator[] ( size_t i ) const { return _v1[i] + _v2[i]; }
114 protected:
115 const AbstractArrayWrapper1 &_v1;
116 const AbstractArrayWrapper2 &_v2;
117 };
118
119 template< typename T , typename AbstractArrayWrapper1 , typename AbstractArrayWrapper2 >
120 struct _VectorDifference : public AbstractArrayWrapper< T >
121 {
122 _VectorDifference( const AbstractArrayWrapper1 &v1 , const AbstractArrayWrapper2 &v2 ) : _v1(v1) , _v2(v2)
123 {
124 // static_assert( std::is_convertible< AbstractArrayWrapper1 , AbstractArrayWrapper< T > >::value || std::is_convertible< AbstractArrayWrapper1 , ConstPointer(T) >::value , "[ERROR] Bad AbstractArrayWrapper1" );
125 // static_assert( std::is_convertible< AbstractArrayWrapper2 , AbstractArrayWrapper< T > >::value || std::is_convertible< AbstractArrayWrapper2 , ConstPointer(T) >::value , "[ERROR] Bad AbstractArrayWrapper2" );
126 }
127 T operator[] ( size_t i ) const { return _v1[i] - _v2[i]; }
128 protected:
129 const AbstractArrayWrapper1 &_v1;
130 const AbstractArrayWrapper2 &_v2;
131 };
132
133 template< typename T , typename Real , typename const_iterator , typename _AbstractArrayWrapper >
134 struct _VectorProduct : public AbstractArrayWrapper< T >
135 {
136 _VectorProduct( const SparseMatrixInterface< Real , const_iterator > &M , const _AbstractArrayWrapper &v ) : _M(M) , _v(v)
137 {
138 // static_assert( std::is_convertible< _AbstractArrayWrapper , AbstractArrayWrapper< T > >::value || std::is_convertible< _AbstractArrayWrapper , ConstPointer(T) >::value , "[ERROR] Bad _AbstractArrayWrapper" );
139 }
140 T operator[] ( size_t i ) const
141 {
142 T t = {};
143 const_iterator e = _M.end( i );
144 for( const_iterator iter = _M.begin( i ) ; iter!=e ; iter++ ) t += _v[ iter->N ] * iter->Value;
145 return t;
146 }
147 protected:
148 const SparseMatrixInterface< Real , const_iterator > &_M;
149 const _AbstractArrayWrapper &_v;
150 };
151
152 template< typename T , typename AbstractArrayWrapper1 , typename AbstractArrayWrapper2 >
153 _VectorSum< T , AbstractArrayWrapper1 , AbstractArrayWrapper2 > VectorSum( const AbstractArrayWrapper1 &v1 , const AbstractArrayWrapper2 &v2 ){ return _VectorSum< T , AbstractArrayWrapper1 , AbstractArrayWrapper2 >( v1 , v2 ); }
154 template< typename T , typename AbstractArrayWrapper1 , typename AbstractArrayWrapper2 >
155 _VectorDifference< T , AbstractArrayWrapper1 , AbstractArrayWrapper2 > VectorDifference( const AbstractArrayWrapper1 &v1 , const AbstractArrayWrapper2 &v2 ){ return _VectorDifference< T , AbstractArrayWrapper1 , AbstractArrayWrapper2 >( v1 , v2 ); }
156 template< typename T , typename Real , typename const_iterator , typename _AbstractArrayWrapper >
157 _VectorProduct< T , Real , const_iterator , _AbstractArrayWrapper > VectorProduct( const SparseMatrixInterface< Real , const_iterator > &M , const _AbstractArrayWrapper &v ){ return _VectorProduct< T , Real , const_iterator , _AbstractArrayWrapper >( M , v ); }
158
159 #include "SparseMatrixInterface.inl"
160 }
161
162 #endif // SPARSE_MATRIX_INTERFACE_INCLUDED