Debian Codegraph @

src/thirdparty/PoissonRecon/MAT.inl

raw
1 /*
2 Copyright (c) 2007, Michael Kazhdan
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 //////////////////////////////
30 // MinimalAreaTriangulation //
31 //////////////////////////////
32 template< typename Index , class Real , unsigned int Dim >
33 _MinimalAreaTriangulation< Index , Real , Dim >::_MinimalAreaTriangulation( ConstPointer( Point< Real , Dim > ) vertices , size_t vCount ) : _vertices( vertices ) , _vCount( vCount )
34 {
35 _bestTriangulation = NullPointer( Real );
36 _midpoint = NullPointer( Index );
37 }
38 template< typename Index , class Real , unsigned int Dim >
39 _MinimalAreaTriangulation< Index , Real , Dim >::~_MinimalAreaTriangulation( void )
40 {
41 FreePointer( _bestTriangulation );
42 FreePointer( _midpoint );
43 }
44 template< typename Index , class Real , unsigned int Dim >
45 std::vector< TriangleIndex< Index > > _MinimalAreaTriangulation< Index , Real , Dim >::getTriangulation( void )
46 {
47 std::vector< TriangleIndex< Index > > triangles;
48 if( _vCount==3 )
49 {
50 triangles.resize(1);
51 triangles[0].idx[0] = 0;
52 triangles[0].idx[1] = 1;
53 triangles[0].idx[2] = 2;
54 return triangles;
55 }
56 else if( _vCount==4 )
57 {
58 TriangleIndex< Index > tIndex[2][2];
59 Real area[] = { 0 , 0 };
60
61 triangles.resize(2);
62
63 tIndex[0][0].idx[0]=0;
64 tIndex[0][0].idx[1]=1;
65 tIndex[0][0].idx[2]=2;
66 tIndex[0][1].idx[0]=2;
67 tIndex[0][1].idx[1]=3;
68 tIndex[0][1].idx[2]=0;
69
70 tIndex[1][0].idx[0]=0;
71 tIndex[1][0].idx[1]=1;
72 tIndex[1][0].idx[2]=3;
73 tIndex[1][1].idx[0]=3;
74 tIndex[1][1].idx[1]=1;
75 tIndex[1][1].idx[2]=2;
76
77 Point< Real , Dim > p1 , p2;
78 for( int i=0 ; i<2 ; i++ ) for( int j=0 ; j<2 ; j++ ) area[i] = SquareArea( _vertices[ tIndex[i][j].idx[0] ] , _vertices[ tIndex[i][j].idx[1] ] , _vertices[ tIndex[i][j].idx[2] ] );
79 if( area[0]>area[1] ) triangles[0] = tIndex[1][0] , triangles[1] = tIndex[1][1];
80 else triangles[0] = tIndex[0][0] , triangles[1] = tIndex[0][1];
81 return triangles;
82 }
83 _set();
84 _addTriangles( 1 , 0 , triangles );
85 return triangles;
86 }
87 template< typename Index , class Real , unsigned int Dim >
88 void _MinimalAreaTriangulation< Index , Real , Dim >::_set( void )
89 {
90 FreePointer( _bestTriangulation );
91 FreePointer( _midpoint );
92 _bestTriangulation = AllocPointer< Real >( _vCount * _vCount );
93 _midpoint = AllocPointer< Index >( _vCount * _vCount );
94 for( int i=0 ; i<_vCount*_vCount ; i++ ) _bestTriangulation[i] = -1 , _midpoint[i] = -1;
95 _subPolygonArea( 1 , 0 );
96 }
97
98 template< typename Index , class Real , unsigned int Dim >
99 Index _MinimalAreaTriangulation< Index , Real , Dim >::_subPolygonIndex( Index i , Index j ) const { return (Index)( i*_vCount+j ); }
100
101 template< typename Index , class Real , unsigned int Dim >
102 void _MinimalAreaTriangulation< Index , Real , Dim >::_addTriangles( Index i , Index j , std::vector< TriangleIndex< Index > >& triangles ) const
103 {
104 TriangleIndex< Index > tIndex;
105 if( j<i ) j += (Index)_vCount;
106 if( i==j || i+1==j ) return;
107 Index mid = _midpoint[ _subPolygonIndex( i , j%_vCount ) ];
108 if( mid!=-1 )
109 {
110 tIndex.idx[0] = i;
111 tIndex.idx[1] = mid;
112 tIndex.idx[2] = j%_vCount;
113 triangles.push_back( tIndex );
114 _addTriangles( i , mid , triangles );
115 _addTriangles( mid , j , triangles );
116 }
117 }
118
119 // Get the minimial area of the sub-polygon [ v_i , ... , v_j ]
120 template< typename Index , class Real , unsigned int Dim >
121 Real _MinimalAreaTriangulation< Index , Real , Dim >::_subPolygonArea( Index i , Index j )
122 {
123 Index idx = _subPolygonIndex( i , j );
124 if( _midpoint[idx]!=-1 ) return _bestTriangulation[idx];
125 Real a = FLT_MAX , temp;
126 if( j<i ) j += (Index)_vCount;
127 // If either i==j or i+1=j, the polygon has trivial area
128 if( i==j || i+1==j )
129 {
130 _bestTriangulation[idx] = 0;
131 return 0;
132 }
133 // If we have already computed the minimal area for this edge
134 if( _midpoint[idx]!=-1 ) return _bestTriangulation[idx];
135 Index mid = -1;
136
137 // For each vertex r \in( i , j ):
138 // -- Construct the triangle ( j , r , i )
139 // -- Compute the Area(j,r,i) + Area( j , ... , r ) + Area( r , ... , i )
140 for( Index r=i+1 ; r<j ; r++ )
141 {
142 Index idx1 = _subPolygonIndex( i , r%_vCount ); // SubPolygon( r , ... , i )
143 Index idx2 = _subPolygonIndex( r%_vCount , j%_vCount ); // SubPolygon( j , ... , r );
144
145 temp = SquareArea( _vertices[i] , _vertices[r%_vCount] , _vertices[j%_vCount] );
146 temp = temp<0 ? 0 : (Real)sqrt(temp);
147 // If we have already computed Area( r , ... , i ), use that.
148 if( _bestTriangulation[idx1]>=0 )
149 {
150 temp += _bestTriangulation[idx1];
151 // If the partial area is already too large, terminate
152 if( temp>a ) continue; // Terminate early
153 // Otherwise, compute the total area
154 temp += _subPolygonArea( r%_vCount , j%_vCount );
155 }
156 else
157 {
158 // Otherwise, compute it now
159 temp += _subPolygonArea( r%_vCount , j%_vCount );
160 // If the partial area is already too large, terminate
161 if( temp>a ) continue;
162 // Otherwise, compute the total area
163 temp += _subPolygonArea( i , r%_vCount );
164 }
165
166 if( temp<a ) a=temp , mid=r%_vCount;
167 }
168 _bestTriangulation[idx] = a;
169 _midpoint[idx] = mid;
170 return a;
171 }
172