| 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 MARCHING_CUBES_INCLUDED |
| 30 |
#define MARCHING_CUBES_INCLUDED |
| 31 |
|
| 32 |
#include <stdio.h> |
| 33 |
#include <type_traits> |
| 34 |
#include "Geometry.h" |
| 35 |
|
| 36 |
#include "Window.h" |
| 37 |
|
| 38 |
namespace PoissonRecon |
| 39 |
{ |
| 40 |
namespace HyperCube |
| 41 |
{ |
| 42 |
enum Direction{ BACK , CROSS , FRONT }; |
| 43 |
inline Direction Opposite( Direction dir ){ return dir==BACK ? FRONT : ( dir==FRONT ? BACK : CROSS ); } |
| 44 |
inline std::string DirectionName( Direction dir ) |
| 45 |
{ |
| 46 |
if( dir==BACK ) return std::string( "back" ); |
| 47 |
else if( dir==CROSS ) return std::string( "cross" ); |
| 48 |
else if( dir==FRONT ) return std::string( "front" ); |
| 49 |
else{ MK_THROW( "Unrecognized direction" ) ; return std::string( "" ); } |
| 50 |
} |
| 51 |
|
| 52 |
// The number of k-dimensional elements in a d-dimensional cube is equal to |
| 53 |
// the number of (k-1)-dimensional elements in a (d-1)-dimensional hypercube plus twice the number of k-dimensional elements in a (d-1)-dimensional hypercube |
| 54 |
|
| 55 |
// Number of elements of dimension K in a cube of dimension D |
| 56 |
template< unsigned int D , unsigned int K > struct ElementNum { static const unsigned int Value = 2 * ElementNum< D-1 , K >::Value + ElementNum< D-1 , K-1 >::Value; }; |
| 57 |
template< unsigned int D > struct ElementNum< D , 0 >{ static const unsigned int Value = 2 * ElementNum< D-1 , 0 >::Value; }; |
| 58 |
template< unsigned int D > struct ElementNum< D , D >{ static const unsigned int Value = 1; }; |
| 59 |
template< > struct ElementNum< 0 , 0 >{ static const unsigned int Value = 1; }; |
| 60 |
// [WARNING] This shouldn't really happen, but we need to support the definition of OverlapElementNum |
| 61 |
template< unsigned int K > struct ElementNum< 0 , K >{ static const unsigned int Value = K==0 ? 1 : 0; }; |
| 62 |
|
| 63 |
template< unsigned int D , unsigned int K1 , unsigned int K2 > struct OverlapElementNum { static const unsigned int Value = K1>=K2 ? ElementNum< K1 , K2 >::Value : OverlapElementNum< D-1 , K1 , K2 >::Value + OverlapElementNum< D-1 , K1 , K2-1 >::Value; }; |
| 64 |
template< unsigned int D , unsigned int K > struct OverlapElementNum< D , D , K >{ static const unsigned int Value = ElementNum< D , K >::Value; }; |
| 65 |
template< unsigned int D > struct OverlapElementNum< D , D , 0 >{ static const unsigned int Value = ElementNum< D , 0 >::Value; }; |
| 66 |
template< unsigned int D , unsigned int K > struct OverlapElementNum< D , K , 0 >{ static const unsigned int Value = ElementNum< K , 0 >::Value; }; |
| 67 |
template< unsigned int D , unsigned int K > struct OverlapElementNum< D , K , K >{ static const unsigned int Value = 1; }; |
| 68 |
template< unsigned int D , unsigned int K > struct OverlapElementNum< D , K , D >{ static const unsigned int Value = 1; }; |
| 69 |
template< unsigned int D > struct OverlapElementNum< D , D , D >{ static const unsigned int Value = 1; }; |
| 70 |
template< unsigned int D > struct OverlapElementNum< D , 0 , 0 >{ static const unsigned int Value = 1; }; |
| 71 |
|
| 72 |
template< unsigned int D > |
| 73 |
struct Cube |
| 74 |
{ |
| 75 |
// Corner index (x,y,z,...) -> x + 2*y + 4*z + ... |
| 76 |
// CROSS -> the D-th axis |
| 77 |
|
| 78 |
// Representation of a K-dimensional element of the cube |
| 79 |
template< unsigned int K > |
| 80 |
struct Element |
| 81 |
{ |
| 82 |
static_assert( D>=K , "[ERROR] Element dimension exceeds cube dimension" ); |
| 83 |
|
| 84 |
// The index of the element, sorted as: |
| 85 |
// 1. All K-dimensional elements contained in the back face |
| 86 |
// 2. All K-dimensional elements spanning the D-th axis |
| 87 |
// 3. All K-dimensional elements contained in the front face |
| 88 |
unsigned int index; |
| 89 |
|
| 90 |
// Initialize by index |
| 91 |
Element( unsigned int idx=0 ); |
| 92 |
|
| 93 |
// Initialize by co-index: |
| 94 |
// 1. A K-dimensional element in either BACK or FRONT |
| 95 |
// 2. A (K-1)-dimensional element extruded across the D-th axis |
| 96 |
Element( Direction dir , unsigned int coIndex ); |
| 97 |
|
| 98 |
// Given a K-Dimensional sub-element living inside a DK-dimensional sub-cube, get the element relative to the D-dimensional cube |
| 99 |
template< unsigned int DK > |
| 100 |
Element( Element< DK > subCube , typename Cube< DK >::template Element< K > subElement ); |
| 101 |
|
| 102 |
// Initialize by setting the directions |
| 103 |
Element( const Direction dirs[D] ); |
| 104 |
|
| 105 |
// Print the element to the specified stream |
| 106 |
void print( FILE* fp=stdout ) const; |
| 107 |
|
| 108 |
// Sets the direction and co-index of the element |
| 109 |
void factor( Direction& dir , unsigned int& coIndex ) const; |
| 110 |
|
| 111 |
// Returns the direction along which the element lives |
| 112 |
Direction direction( void ) const; |
| 113 |
|
| 114 |
// Returns the co-index of the element |
| 115 |
unsigned int coIndex( void ) const; |
| 116 |
|
| 117 |
// Compute the directions of the element |
| 118 |
void directions( Direction* dirs ) const; |
| 119 |
|
| 120 |
// Returns the antipodal element |
| 121 |
typename Cube< D >::template Element< K > antipodal( void ) const; |
| 122 |
|
| 123 |
// Comparison operators |
| 124 |
bool operator < ( Element e ) const { return index< e.index; } |
| 125 |
bool operator <= ( Element e ) const { return index<=e.index; } |
| 126 |
bool operator > ( Element e ) const { return index> e.index; } |
| 127 |
bool operator >= ( Element e ) const { return index>=e.index; } |
| 128 |
bool operator == ( Element e ) const { return index==e.index; } |
| 129 |
bool operator != ( Element e ) const { return index!=e.index; } |
| 130 |
bool operator < ( unsigned int i ) const { return index< i; } |
| 131 |
bool operator <= ( unsigned int i ) const { return index<=i; } |
| 132 |
bool operator > ( unsigned int i ) const { return index> i; } |
| 133 |
bool operator >= ( unsigned int i ) const { return index>=i; } |
| 134 |
bool operator == ( unsigned int i ) const { return index==i; } |
| 135 |
bool operator != ( unsigned int i ) const { return index!=i; } |
| 136 |
|
| 137 |
// Increment operators |
| 138 |
Element& operator ++ ( void ) { index++ ; return *this; } |
| 139 |
Element operator ++ ( int ) { index++ ; return Element(index-1); } |
| 140 |
protected: |
| 141 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< _D!=0 && _K!=0 >::type _setElement( Direction dir , unsigned int coIndex ); |
| 142 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< _D!=0 && _K==0 >::type _setElement( Direction dir , unsigned int coIndex ); |
| 143 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< _D==0 && _K==0 >::type _setElement( Direction dir , unsigned int coIndex ); |
| 144 |
|
| 145 |
template< unsigned int KD > typename std::enable_if< (D> KD) && (KD>K) && K!=0 >::type _setElement( typename Cube< D >::template Element< KD > subCube , typename Cube< KD >::template Element< K > subElement ); |
| 146 |
template< unsigned int KD > typename std::enable_if< (D> KD) && (KD>K) && K==0 >::type _setElement( typename Cube< D >::template Element< KD > subCube , typename Cube< KD >::template Element< K > subElement ); |
| 147 |
template< unsigned int KD > typename std::enable_if< (D==KD) && (KD>K) >::type _setElement( typename Cube< D >::template Element< KD > subCube , typename Cube< KD >::template Element< K > subElement ); |
| 148 |
template< unsigned int KD > typename std::enable_if< (KD==K) >::type _setElement( typename Cube< D >::template Element< KD > subCube , typename Cube< KD >::template Element< K > subElement ); |
| 149 |
|
| 150 |
template< unsigned int _D=D > typename std::enable_if< _D!=0 >::type _setElement( const Direction* dirs ); |
| 151 |
template< unsigned int _D=D > typename std::enable_if< _D==0 >::type _setElement( const Direction* dirs ); |
| 152 |
|
| 153 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< _D==_K >::type _factor( Direction& dir , unsigned int& coIndex ) const; |
| 154 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< _D!=_K && _K!=0 >::type _factor( Direction& dir , unsigned int& coIndex ) const; |
| 155 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< _D!=_K && _K==0 >::type _factor( Direction& dir , unsigned int& coIndex ) const; |
| 156 |
|
| 157 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< (_D>_K) && _K!=0 >::type _directions( Direction* dirs ) const; |
| 158 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< (_D>_K) && _K==0 >::type _directions( Direction* dirs ) const; |
| 159 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< _D==_K >::type _directions( Direction* dirs ) const; |
| 160 |
|
| 161 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< (_D>_K) && _K!=0 , Element >::type _antipodal( void ) const; |
| 162 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< (_D>_K) && _K==0 , Element >::type _antipodal( void ) const; |
| 163 |
template< unsigned int _D=D , unsigned int _K=K > typename std::enable_if< _D==_K , Element >::type _antipodal( void ) const; |
| 164 |
}; |
| 165 |
// A way of indexing the cubes incident on an element |
| 166 |
template< unsigned int K > using IncidentCubeIndex = typename Cube< D-K >::template Element< 0 >; |
| 167 |
|
| 168 |
// Number of elements of dimension K |
| 169 |
template< unsigned int K > static constexpr unsigned int ElementNum( void ){ return HyperCube::ElementNum< D , K >::Value; } |
| 170 |
// Number of cubes incident to an element of dimension K |
| 171 |
template< unsigned int K > static constexpr unsigned int IncidentCubeNum( void ){ return HyperCube::ElementNum< D-K , 0 >::Value; } |
| 172 |
// Number of overlapping elements of dimension K1 / K2 |
| 173 |
template< unsigned int K1 , unsigned int K2 > static constexpr unsigned int OverlapElementNum( void ){ return HyperCube::OverlapElementNum< D , K1 , K2 >::Value; } |
| 174 |
|
| 175 |
// Is the face outward-facing |
| 176 |
static bool IsOriented( Element< D-1 > e ); |
| 177 |
|
| 178 |
// Is one element contained in the other? |
| 179 |
template< unsigned int K1 , unsigned int K2 > |
| 180 |
static bool Overlap( Element< K1 > e1 , Element< K2 > e2 ); |
| 181 |
|
| 182 |
// If K1>K2: returns all elements contained in e |
| 183 |
// Else: returns all elements containing e |
| 184 |
template< unsigned int K1 , unsigned int K2 > |
| 185 |
static void OverlapElements( Element< K1 > e , Element< K2 >* es ); |
| 186 |
|
| 187 |
// Returns the marching-cubes index for the set of values |
| 188 |
template< typename Real > |
| 189 |
static unsigned int MCIndex( const Real values[ Cube::ElementNum< 0 >() ] , Real iso ); |
| 190 |
|
| 191 |
// Extracts the marching-cubes sub-index for the associated element |
| 192 |
template< unsigned int K > |
| 193 |
static unsigned int ElementMCIndex( Element< K > element , unsigned int mcIndex ); |
| 194 |
|
| 195 |
// Does the marching cubes index have a zero-crossing |
| 196 |
static bool HasMCRoots( unsigned int mcIndex ); |
| 197 |
|
| 198 |
// Sets the offset of the incident cube relative to the center cube, x[i] \in {-1,0,1} |
| 199 |
template< unsigned int K > |
| 200 |
static void CellOffset( Element< K > e , IncidentCubeIndex< K > d , int x[D] ); |
| 201 |
|
| 202 |
// Returns the linearized offset of the incident cube relative to the center cube, \in [0,3^D) |
| 203 |
template< unsigned int K > |
| 204 |
static unsigned int CellOffset( Element< K > e , IncidentCubeIndex< K > d ); |
| 205 |
|
| 206 |
// Returns the index of the incident cube that is the source |
| 207 |
template< unsigned int K > |
| 208 |
static typename Cube< D >::template IncidentCubeIndex< K > IncidentCube( Element< K > e ); |
| 209 |
|
| 210 |
// Returns the corresponding element in the incident cube |
| 211 |
template< unsigned int K > |
| 212 |
static typename Cube< D >::template Element< K > IncidentElement( Element< K > e , IncidentCubeIndex< K > d ); |
| 213 |
|
| 214 |
protected: |
| 215 |
template< unsigned int K1 , unsigned int K2 > static typename std::enable_if< (K1>=K2) , bool >::type _Overlap( Element< K1 > e1 , Element< K2 > e2 ); |
| 216 |
template< unsigned int K1 , unsigned int K2 > static typename std::enable_if< (K1< K2) , bool >::type _Overlap( Element< K1 > e1 , Element< K2 > e2 ); |
| 217 |
|
| 218 |
template< unsigned int K1 , unsigned int K2 > static typename std::enable_if< (K1>=K2) >::type _OverlapElements( Element< K1 > e , Element< K2 >* es ); |
| 219 |
template< unsigned int K1 , unsigned int K2 > static typename std::enable_if< (K1< K2) && D==K2 >::type _OverlapElements( Element< K1 > e , Element< K2 >* es ); |
| 220 |
template< unsigned int K1 , unsigned int K2 > static typename std::enable_if< (K1< K2) && D!=K2 && K1!=0 >::type _OverlapElements( Element< K1 > e , Element< K2 >* es ); |
| 221 |
template< unsigned int K1 , unsigned int K2 > static typename std::enable_if< (K1< K2) && D!=K2 && K1==0 >::type _OverlapElements( Element< K1 > e , Element< K2 >* es ); |
| 222 |
|
| 223 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D==K , IncidentCubeIndex< K > >::type _IncidentCube( Element< K > e ); |
| 224 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D!=K && _D!=0 && K!=0 , IncidentCubeIndex< K > >::type _IncidentCube( Element< K > e ); |
| 225 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D!=K && _D!=0 && K==0 , IncidentCubeIndex< K > >::type _IncidentCube( Element< K > e ); |
| 226 |
|
| 227 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D==K >::type _CellOffset( Element< K > e , IncidentCubeIndex< K > d , int* x ); |
| 228 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D!=K && K!=0 >::type _CellOffset( Element< K > e , IncidentCubeIndex< K > d , int* x ); |
| 229 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D!=K && K==0 >::type _CellOffset( Element< K > e , IncidentCubeIndex< K > d , int* x ); |
| 230 |
|
| 231 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D==K && K==0 , unsigned int >::type _CellOffset( Element< K > e , IncidentCubeIndex< K > d ); |
| 232 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D==K && K!=0 , unsigned int >::type _CellOffset( Element< K > e , IncidentCubeIndex< K > d ); |
| 233 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D!=K && K!=0 , unsigned int >::type _CellOffset( Element< K > e , IncidentCubeIndex< K > d ); |
| 234 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D!=K && K==0 , unsigned int >::type _CellOffset( Element< K > e , IncidentCubeIndex< K > d ); |
| 235 |
|
| 236 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D==K , Element< K > >::type _IncidentElement( Element< K > e , IncidentCubeIndex< K > d ); |
| 237 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D!=K && _D!=0 && K!=0 , Element< K > >::type _IncidentElement( Element< K > e , IncidentCubeIndex< K > d ); |
| 238 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D!=K && _D!=0 && K==0 , Element< K > >::type _IncidentElement( Element< K > e , IncidentCubeIndex< K > d ); |
| 239 |
|
| 240 |
template< unsigned int _D=D > static typename std::enable_if< _D!=1 >::type _FactorOrientation( Element< D-1 > e , unsigned int& dim , Direction& dir ); |
| 241 |
template< unsigned int _D=D > static typename std::enable_if< _D==1 >::type _FactorOrientation( Element< D-1 > e , unsigned int& dim , Direction& dir ); |
| 242 |
|
| 243 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D!=K && K!=0 , unsigned int >::type _ElementMCIndex( Element< K > element , unsigned int mcIndex ); |
| 244 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D!=K && K==0 , unsigned int >::type _ElementMCIndex( Element< K > element , unsigned int mcIndex ); |
| 245 |
template< unsigned int K , unsigned int _D=D > static typename std::enable_if< _D==K , unsigned int >::type _ElementMCIndex( Element< K > element , unsigned int mcIndex ); |
| 246 |
|
| 247 |
template< unsigned int DD > friend struct Cube; |
| 248 |
}; |
| 249 |
|
| 250 |
// Specialized class for extracting iso-curves from a square |
| 251 |
struct MarchingSquares |
| 252 |
{ |
| 253 |
const static unsigned int MAX_EDGES=2; |
| 254 |
static const int edges[1<<HyperCube::Cube< 2 >::ElementNum< 0 >()][2*MAX_EDGES+1]; |
| 255 |
static inline int AddEdgeIndices( unsigned char mcIndex , int* edges); |
| 256 |
}; |
| 257 |
|
| 258 |
/////////////////// |
| 259 |
// Cube::Element // |
| 260 |
/////////////////// |
| 261 |
template< unsigned int D > template< unsigned int K > |
| 262 |
Cube< D >::Element< K >::Element( unsigned int idx ) : index( idx ){} |
| 263 |
template< unsigned int D > template< unsigned int K > |
| 264 |
Cube< D >::Element< K >::Element( Direction dir , unsigned int coIndex ){ _setElement( dir , coIndex ); } |
| 265 |
template< unsigned int D > template< unsigned int K > template< unsigned int DK > |
| 266 |
Cube< D >::Element< K >::Element( Element< DK > subCube , typename Cube< DK >::template Element< K > subElement ) |
| 267 |
{ |
| 268 |
static_assert( DK>=K , "[ERROR] Element::Element: sub-cube dimension cannot be smaller than the sub-element dimension" ); |
| 269 |
static_assert( DK<=D , "[ERROR] Element::Element: sub-cube dimension cannot be larger than the cube dimension" ); |
| 270 |
_setElement( subCube , subElement ); |
| 271 |
} |
| 272 |
template< unsigned int D > template< unsigned int K > |
| 273 |
Cube< D >::Element< K >::Element( const Direction dirs[D] ){ _setElement( dirs ); } |
| 274 |
|
| 275 |
template< unsigned int D > template< unsigned int K > template< unsigned int KD > |
| 276 |
typename std::enable_if< (D>KD) && (KD>K) && K!=0 >::type Cube< D >::Element< K >::_setElement( typename Cube< D >::template Element< KD > subCube , typename Cube< KD >::template Element< K > subElement ) |
| 277 |
{ |
| 278 |
Direction dir ; unsigned int coIndex; |
| 279 |
subCube.factor( dir , coIndex ); |
| 280 |
// If the sub-cube lies entirely in the back/front, we can compute the element in the smaller cube. |
| 281 |
if( dir==BACK || dir==FRONT ) |
| 282 |
{ |
| 283 |
typename Cube< D-1 >::template Element< KD > _subCube( coIndex ); |
| 284 |
typename Cube< D-1 >::template Element< K > _element( _subCube , subElement ); |
| 285 |
*this = Element( dir , _element.index ); |
| 286 |
} |
| 287 |
else |
| 288 |
{ |
| 289 |
typename Cube< D-1 >::template Element< KD-1 > _subCube( coIndex ); |
| 290 |
|
| 291 |
Direction _dir ; unsigned int _coIndex; |
| 292 |
subElement.factor( _dir , _coIndex ); |
| 293 |
// If the sub-element lies entirely in the back/front, we can compute the element in the smaller cube. |
| 294 |
if( _dir==BACK || _dir==FRONT ) |
| 295 |
{ |
| 296 |
typename Cube< KD-1 >::template Element< K > _subElement( _coIndex ); |
| 297 |
typename Cube< D-1 >::template Element< K > _element( _subCube , _subElement ); |
| 298 |
*this = Element( _dir , _element.index ); |
| 299 |
} |
| 300 |
// Otherwise |
| 301 |
else |
| 302 |
{ |
| 303 |
typename Cube< KD-1 >::template Element< K-1 > _subElement( _coIndex ); |
| 304 |
typename Cube< D-1 >::template Element< K-1 > _element( _subCube , _subElement ); |
| 305 |
*this = Element( _dir , _element.index ); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
template< unsigned int D > template< unsigned int K > template< unsigned int KD > |
| 310 |
typename std::enable_if< (D>KD) && (KD>K) && K==0 >::type Cube< D >::Element< K >::_setElement( typename Cube< D >::template Element< KD > subCube , typename Cube< KD >::template Element< K > subElement ) |
| 311 |
{ |
| 312 |
Direction dir ; unsigned int coIndex; |
| 313 |
subCube.factor( dir , coIndex ); |
| 314 |
// If the sub-cube lies entirely in the back/front, we can compute the element in the smaller cube. |
| 315 |
if( dir==BACK || dir==FRONT ) |
| 316 |
{ |
| 317 |
typename Cube< D-1 >::template Element< KD > _subCube( coIndex ); |
| 318 |
typename Cube< D-1 >::template Element< K > _element( _subCube , subElement ); |
| 319 |
*this = Element( dir , _element.index ); |
| 320 |
} |
| 321 |
else |
| 322 |
{ |
| 323 |
typename Cube< D-1 >::template Element< KD-1 > _subCube( coIndex ); |
| 324 |
|
| 325 |
Direction _dir ; unsigned int _coIndex; |
| 326 |
subElement.factor( _dir , _coIndex ); |
| 327 |
// If the sub-element lies entirely in the back/front, we can compute the element in the smaller cube. |
| 328 |
if( _dir==BACK || _dir==FRONT ) |
| 329 |
{ |
| 330 |
typename Cube< KD-1 >::template Element< K > _subElement( _coIndex ); |
| 331 |
typename Cube< D-1 >::template Element< K > _element( _subCube , _subElement ); |
| 332 |
*this = Element( _dir , _element.index ); |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
template< unsigned int D > template< unsigned int K > template< unsigned int KD > |
| 337 |
typename std::enable_if< (D==KD) && (KD>K) >::type Cube< D >::Element< K >::_setElement( typename Cube< D >::template Element< KD > subCube , typename Cube< KD >::template Element< K > subElement ){ *this = subElement; } |
| 338 |
template< unsigned int D > template< unsigned int K > template< unsigned int KD > |
| 339 |
typename std::enable_if< (KD==K) >::type Cube< D >::Element< K >::_setElement( typename Cube< D >::template Element< KD > subCube , typename Cube< KD >::template Element< K > subElement ){ *this = subCube; } |
| 340 |
|
| 341 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 342 |
typename std::enable_if< _D!=0 && _K!=0 >::type Cube< D >::Element< K >::_setElement( Direction dir , unsigned int coIndex ) |
| 343 |
{ |
| 344 |
switch( dir ) |
| 345 |
{ |
| 346 |
case BACK: index = coIndex ; break; |
| 347 |
case CROSS: index = coIndex + HyperCube::ElementNum< D-1 , K >::Value ; break; |
| 348 |
case FRONT: index = coIndex + HyperCube::ElementNum< D-1 , K >::Value + HyperCube::ElementNum< D-1 , K-1 >::Value ; break; |
| 349 |
default: MK_THROW( "Bad direction: " , dir ); |
| 350 |
} |
| 351 |
} |
| 352 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 353 |
typename std::enable_if< _D!=0 && _K==0 >::type Cube< D >::Element< K >::_setElement( Direction dir , unsigned int coIndex ) |
| 354 |
{ |
| 355 |
switch( dir ) |
| 356 |
{ |
| 357 |
case BACK: index = coIndex ; break; |
| 358 |
case FRONT: index = coIndex + HyperCube::ElementNum< D-1 , K >::Value ; break; |
| 359 |
default: MK_THROW( "Bad direction: " , dir ); |
| 360 |
} |
| 361 |
} |
| 362 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 363 |
typename std::enable_if< _D==0 && _K==0 >::type Cube< D >::Element< K >::_setElement( Direction dir , unsigned int coIndex ){ index = coIndex; } |
| 364 |
|
| 365 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D > |
| 366 |
typename std::enable_if< _D!=0 >::type Cube< D >::Element< K>::_setElement( const Direction* dirs ) |
| 367 |
{ |
| 368 |
if( dirs[D-1]==CROSS ) *this = Element( dirs[D-1] , typename Cube< D-1 >::template Element< K-1 >( dirs ).index ); |
| 369 |
else *this = Element( dirs[D-1] , typename Cube< D-1 >::template Element< K >( dirs ).index ); |
| 370 |
} |
| 371 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D > |
| 372 |
typename std::enable_if< _D==0 >::type Cube< D >::Element< K>::_setElement( const Direction* dirs ){} |
| 373 |
|
| 374 |
template< unsigned int D > template< unsigned int K > |
| 375 |
void Cube< D >::Element< K >::print( FILE* fp ) const |
| 376 |
{ |
| 377 |
Direction dirs[D==0?1:D]; |
| 378 |
directions( dirs ); |
| 379 |
for( int d=0 ; d<D ; d++ ) fprintf( fp , "%c" , dirs[d]==BACK ? 'B' : ( dirs[d]==CROSS ? 'C' : 'F' ) ); |
| 380 |
} |
| 381 |
|
| 382 |
template< unsigned int D > template< unsigned int K > |
| 383 |
void Cube< D >::Element< K >::factor( Direction& dir , unsigned int& coIndex ) const { _factor( dir , coIndex ); } |
| 384 |
|
| 385 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 386 |
typename std::enable_if< _D!=_K && _K!=0 >::type Cube< D >::Element< K >::_factor( Direction& dir , unsigned int& coIndex ) const |
| 387 |
{ |
| 388 |
if ( index<HyperCube::ElementNum< D-1 , K >::Value ) dir = BACK , coIndex = index; |
| 389 |
else if( index<HyperCube::ElementNum< D-1 , K >::Value + HyperCube::ElementNum< D-1 , K-1 >::Value ) dir = CROSS , coIndex = index - HyperCube::ElementNum< D-1 , K >::Value; |
| 390 |
else dir = FRONT , coIndex = index - HyperCube::ElementNum< D-1 , K >::Value - HyperCube::ElementNum< D-1 , K-1 >::Value; |
| 391 |
} |
| 392 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 393 |
typename std::enable_if< _D!=_K && _K==0 >::type Cube< D >::Element< K >::_factor( Direction& dir , unsigned int& coIndex ) const |
| 394 |
{ |
| 395 |
if ( index<HyperCube::ElementNum< D-1 , K >::Value ) dir = BACK , coIndex = index; |
| 396 |
else dir = FRONT , coIndex = index - HyperCube::ElementNum< D-1 , K >::Value; |
| 397 |
} |
| 398 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 399 |
typename std::enable_if< _D==_K >::type Cube< D >::Element< K >::_factor( Direction& dir , unsigned int& coIndex ) const { dir=CROSS , coIndex=0; } |
| 400 |
|
| 401 |
template< unsigned int D > template< unsigned int K > |
| 402 |
Direction Cube< D >::Element< K >::direction( void ) const |
| 403 |
{ |
| 404 |
Direction dir ; unsigned int coIndex; |
| 405 |
factor( dir , coIndex ); |
| 406 |
return dir; |
| 407 |
} |
| 408 |
template< unsigned int D > template< unsigned int K > |
| 409 |
unsigned int Cube< D >::Element< K >::coIndex( void ) const |
| 410 |
{ |
| 411 |
Direction dir ; unsigned int coIndex; |
| 412 |
factor( dir , coIndex ); |
| 413 |
return coIndex; |
| 414 |
} |
| 415 |
|
| 416 |
template< unsigned int D > template< unsigned int K > |
| 417 |
void Cube< D >::Element< K >::directions( Direction* dirs ) const { _directions( dirs ); } |
| 418 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 419 |
typename std::enable_if< (_D>_K) && _K!=0 >::type Cube< D >::Element< K >::_directions( Direction* dirs ) const |
| 420 |
{ |
| 421 |
unsigned int coIndex; |
| 422 |
factor( dirs[D-1] , coIndex ); |
| 423 |
if( dirs[D-1]==CROSS ) typename Cube< D-1 >::template Element< K-1 >( coIndex ).directions( dirs ); |
| 424 |
else typename Cube< D-1 >::template Element< K >( coIndex ).directions( dirs ); |
| 425 |
} |
| 426 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 427 |
typename std::enable_if< (_D>_K) && _K==0 >::type Cube< D >::Element< K >::_directions( Direction* dirs ) const |
| 428 |
{ |
| 429 |
unsigned int coIndex; |
| 430 |
factor( dirs[D-1] , coIndex ); |
| 431 |
if( dirs[D-1]==FRONT || dirs[D-1]==BACK ) typename Cube< D-1 >::template Element< K >( coIndex ).directions( dirs ); |
| 432 |
} |
| 433 |
|
| 434 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 435 |
typename std::enable_if< _D==_K >::type Cube< D >::Element< K >::_directions( Direction* dirs ) const { for( int d=0 ; d<D ; d++ ) dirs[d] = CROSS; } |
| 436 |
|
| 437 |
template< unsigned int D > template< unsigned int K > |
| 438 |
typename Cube< D >::template Element< K > Cube< D >::Element< K >::antipodal( void ) const { return _antipodal(); } |
| 439 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 440 |
typename std::enable_if< (_D>_K) && _K!=0 , typename Cube< D >::template Element< K > >::type Cube< D >::Element< K >::_antipodal( void ) const |
| 441 |
{ |
| 442 |
Direction dir ; unsigned int coIndex; |
| 443 |
factor( dir , coIndex ); |
| 444 |
if ( dir==CROSS ) return Element< K >( CROSS , typename Cube< D-1 >::template Element< K-1 >( coIndex ).antipodal().index ); |
| 445 |
else if( dir==FRONT ) return Element< K >( BACK , typename Cube< D-1 >::template Element< K >( coIndex ).antipodal().index ); |
| 446 |
else if( dir==BACK ) return Element< K >( FRONT , typename Cube< D-1 >::template Element< K >( coIndex ).antipodal().index ); |
| 447 |
return Element< K >(); |
| 448 |
} |
| 449 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 450 |
#ifdef _MSC_VER |
| 451 |
typename std::enable_if< (_D>_K) && _K==0 , typename Cube< D >::Element< K > >::type Cube< D >::Element< K >::_antipodal( void ) const |
| 452 |
#else // !_MSC_VER |
| 453 |
typename std::enable_if< (_D>_K) && _K==0 , typename Cube< D >::template Element< K > >::type Cube< D >::Element< K >::_antipodal( void ) const |
| 454 |
#endif // _MSC_VER |
| 455 |
{ |
| 456 |
Direction dir ; unsigned int coIndex; |
| 457 |
factor( dir , coIndex ); |
| 458 |
if ( dir==FRONT ) return Element< K >( BACK , typename Cube< D-1 >::template Element< K >( coIndex ).antipodal().index ); |
| 459 |
else if( dir==BACK ) return Element< K >( FRONT , typename Cube< D-1 >::template Element< K >( coIndex ).antipodal().index ); |
| 460 |
return Element< K >(); |
| 461 |
} |
| 462 |
template< unsigned int D > template< unsigned int K > template< unsigned int _D , unsigned int _K > |
| 463 |
#ifdef _MSC_VER |
| 464 |
typename std::enable_if< _D==_K , typename Cube< D >::Element< K > >::type Cube< D >::Element< K >::_antipodal( void ) const { return *this; } |
| 465 |
#else // !_MSC_VER |
| 466 |
typename std::enable_if< _D==_K , typename Cube< D >::template Element< K > >::type Cube< D >::Element< K >::_antipodal( void ) const { return *this; } |
| 467 |
#endif // _MSC_VER |
| 468 |
|
| 469 |
////////// |
| 470 |
// Cube // |
| 471 |
////////// |
| 472 |
template< unsigned int D > template< unsigned int K1 , unsigned int K2 > |
| 473 |
bool Cube< D >::Overlap( Element< K1 > e1 , Element< K2 > e2 ){ return _Overlap( e1 , e2 ); } |
| 474 |
template< unsigned int D > template< unsigned int K1 , unsigned int K2 > |
| 475 |
typename std::enable_if< (K1>=K2) , bool >::type Cube< D >::_Overlap( Element< K1 > e1 , Element< K2 > e2 ) |
| 476 |
{ |
| 477 |
Direction dir1[ D ] , dir2[ D ]; |
| 478 |
e1.directions( dir1 ) , e2.directions( dir2 ); |
| 479 |
for( int d=0 ; d<D ; d++ ) if( dir1[d]!=CROSS && dir1[d]!=dir2[d] ) return false; |
| 480 |
return true; |
| 481 |
} |
| 482 |
template< unsigned int D > template< unsigned int K1 , unsigned int K2 > |
| 483 |
typename std::enable_if< (K1< K2) , bool >::type Cube< D >::_Overlap( Element< K1 > e1 , Element< K2 > e2 ){ return _Overlap( e2 , e1 ); } |
| 484 |
|
| 485 |
template< unsigned int D > template< unsigned int K1 , unsigned int K2 > |
| 486 |
void Cube< D >::OverlapElements( Element< K1 > e , Element< K2 >* es ){ _OverlapElements( e , es ); } |
| 487 |
template< unsigned int D > template< unsigned int K1 , unsigned int K2 > |
| 488 |
typename std::enable_if< (K1>=K2) >::type Cube< D >::_OverlapElements( Element< K1 > e , Element< K2 >* es ) |
| 489 |
{ |
| 490 |
for( typename Cube< K1 >::template Element< K2 > _e ; _e<Cube< K1 >::template ElementNum< K2 >() ; _e++ ) es[_e.index] = Element< K2 >( e , _e ); |
| 491 |
} |
| 492 |
template< unsigned int D > template< unsigned int K1 , unsigned int K2 > |
| 493 |
typename std::enable_if< (K1< K2) && D==K2 >::type Cube< D >::_OverlapElements( Element< K1 > e , Element< K2 >* es ) |
| 494 |
{ |
| 495 |
es[0] = Element< D >(); |
| 496 |
} |
| 497 |
template< unsigned int D > template< unsigned int K1 , unsigned int K2 > |
| 498 |
typename std::enable_if< (K1< K2) && D!=K2 && K1!=0 >::type Cube< D >::_OverlapElements( Element< K1 > e , Element< K2 >* es ) |
| 499 |
{ |
| 500 |
Direction dir = e.direction() ; unsigned int coIndex; |
| 501 |
e.factor( dir , coIndex ); |
| 502 |
if( dir==FRONT || dir==BACK ) |
| 503 |
{ |
| 504 |
typename Cube< D-1 >::template Element< K2 > _es1[ HyperCube::OverlapElementNum< D-1 , K1 , K2 >::Value ]; |
| 505 |
typename Cube< D-1 >::template Element< K2-1 > _es2[ HyperCube::OverlapElementNum< D-1 , K1 , K2-1 >::Value ]; |
| 506 |
Cube< D-1 >::OverlapElements( typename Cube< D-1 >::template Element< K1 >( coIndex ) , _es1 ); |
| 507 |
Cube< D-1 >::OverlapElements( typename Cube< D-1 >::template Element< K1 >( coIndex ) , _es2 ); |
| 508 |
for( unsigned int i=0 ; i<HyperCube::OverlapElementNum< D-1 , K1 , K2 >::Value ; i++ ) es[i] = typename Cube< D >::template Element< K2 >( dir , _es1[i].index ); |
| 509 |
es += HyperCube::OverlapElementNum< D-1 , K1 , K2 >::Value; |
| 510 |
for( unsigned int i=0 ; i<HyperCube::OverlapElementNum< D-1 , K1 , K2-1 >::Value ; i++ ) es[i] = typename Cube< D >::template Element< K2 >( CROSS , _es2[i].index ); |
| 511 |
} |
| 512 |
else if( dir==CROSS ) |
| 513 |
{ |
| 514 |
typename Cube< D-1 >::template Element< K2-1 > _es1[ HyperCube::OverlapElementNum< D-1 , K1-1 , K2-1 >::Value ]; |
| 515 |
Cube< D-1 >::OverlapElements( typename Cube< D-1 >::template Element< K1-1 >( coIndex ) , _es1 ); |
| 516 |
for( unsigned int i=0 ; i<HyperCube::OverlapElementNum< D-1 , K1-1 , K2-1 >::Value ; i++ ) es[i] = typename Cube< D >::template Element< K2 >( CROSS , _es1[i].index ); |
| 517 |
} |
| 518 |
} |
| 519 |
template< unsigned int D > template< unsigned int K1 , unsigned int K2 > |
| 520 |
typename std::enable_if< (K1< K2) && D!=K2 && K1==0 >::type Cube< D >::_OverlapElements( Element< K1 > e , Element< K2 >* es ) |
| 521 |
{ |
| 522 |
Direction dir = e.direction() ; unsigned int coIndex; |
| 523 |
e.factor( dir , coIndex ); |
| 524 |
if( dir==FRONT || dir==BACK ) |
| 525 |
{ |
| 526 |
typename Cube< D-1 >::template Element< K2 > _es1[ HyperCube::OverlapElementNum< D-1 , K1 , K2 >::Value ]; |
| 527 |
typename Cube< D-1 >::template Element< K2-1 > _es2[ HyperCube::OverlapElementNum< D-1 , K1 , K2-1 >::Value ]; |
| 528 |
Cube< D-1 >::OverlapElements( typename Cube< D-1 >::template Element< K1 >( coIndex ) , _es1 ); |
| 529 |
Cube< D-1 >::OverlapElements( typename Cube< D-1 >::template Element< K1 >( coIndex ) , _es2 ); |
| 530 |
for( unsigned int i=0 ; i<HyperCube::OverlapElementNum< D-1 , K1 , K2 >::Value ; i++ ) es[i] = typename Cube< D >::template Element< K2 >( dir , _es1[i].index ); |
| 531 |
es += HyperCube::OverlapElementNum< D-1 , K1 , K2 >::Value; |
| 532 |
for( unsigned int i=0 ; i<HyperCube::OverlapElementNum< D-1 , K1 , K2-1 >::Value ; i++ ) es[i] = typename Cube< D >::template Element< K2 >( CROSS , _es2[i].index ); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
template< unsigned int D > template< unsigned int K > |
| 537 |
typename Cube< D >::template IncidentCubeIndex< K > Cube< D >::IncidentCube( Element< K > e ){ return _IncidentCube( e ); } |
| 538 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 539 |
#ifdef _MSC_VER |
| 540 |
typename std::enable_if< _D==K , typename Cube< D >::IncidentCubeIndex< K > >::type Cube< D >::_IncidentCube( Element< K > e ){ return IncidentCubeIndex< D >(); } |
| 541 |
#else // !_MSC_VER |
| 542 |
typename std::enable_if< _D==K , typename Cube< D >::template IncidentCubeIndex< K > >::type Cube< D >::_IncidentCube( Element< K > e ){ return IncidentCubeIndex< D >(); } |
| 543 |
#endif // _MSC_VER |
| 544 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 545 |
#ifdef _MSC_VER |
| 546 |
typename std::enable_if< _D!=K && _D!=0 && K!=0 , typename Cube< D >::IncidentCubeIndex< K > >::type Cube< D >::_IncidentCube( Element< K > e ) |
| 547 |
#else // !_MSC_VER |
| 548 |
typename std::enable_if< _D!=K && _D!=0 && K!=0 , typename Cube< D >::template IncidentCubeIndex< K > >::type Cube< D >::_IncidentCube( Element< K > e ) |
| 549 |
#endif // _MSC_VER |
| 550 |
{ |
| 551 |
Direction dir ; unsigned int coIndex; |
| 552 |
e.factor( dir , coIndex ); |
| 553 |
if ( dir==CROSS ) return Cube< D-1 >::IncidentCube( typename Cube< D-1 >::template Element< K-1 >( coIndex ) ); |
| 554 |
else if( dir==FRONT ) return IncidentCubeIndex< K >( BACK , Cube< D-1 >::IncidentCube( typename Cube< D-1 >::template Element< K >( coIndex ) ).index ); |
| 555 |
else return IncidentCubeIndex< K >( FRONT , Cube< D-1 >::IncidentCube( typename Cube< D-1 >::template Element< K >( coIndex ) ).index ); |
| 556 |
} |
| 557 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 558 |
#ifdef _MSC_VER |
| 559 |
typename std::enable_if< _D!=K && _D!=0 && K==0 , typename Cube< D >::IncidentCubeIndex< K > >::type Cube< D >::_IncidentCube( Element< K > e ) |
| 560 |
#else // !_MSC_VER |
| 561 |
typename std::enable_if< _D!=K && _D!=0 && K==0 , typename Cube< D >::template IncidentCubeIndex< K > >::type Cube< D >::_IncidentCube( Element< K > e ) |
| 562 |
#endif // _MSC_VER |
| 563 |
{ |
| 564 |
Direction dir ; unsigned int coIndex; |
| 565 |
e.factor( dir , coIndex ); |
| 566 |
if( dir==FRONT ) return IncidentCubeIndex< K >( BACK , Cube< D-1 >::IncidentCube( typename Cube< D-1 >::template Element< K >( coIndex ) ).index ); |
| 567 |
else return IncidentCubeIndex< K >( FRONT , Cube< D-1 >::IncidentCube( typename Cube< D-1 >::template Element< K >( coIndex ) ).index ); |
| 568 |
} |
| 569 |
|
| 570 |
template< unsigned int D > |
| 571 |
bool Cube< D >::IsOriented( Element< D-1 > e ) |
| 572 |
{ |
| 573 |
unsigned int dim ; Direction dir; |
| 574 |
_FactorOrientation( e , dim , dir ); |
| 575 |
return (dir==FRONT) ^ ((D-dim-1)&1); |
| 576 |
} |
| 577 |
template< unsigned int D > template< unsigned int _D > |
| 578 |
typename std::enable_if< _D!=1 >::type Cube< D >::_FactorOrientation( Element< D-1 > e , unsigned int& dim , Direction& dir ) |
| 579 |
{ |
| 580 |
unsigned int coIndex; |
| 581 |
e.factor( dir , coIndex ); |
| 582 |
if( dir==CROSS ) Cube< D-1 >::_FactorOrientation( typename Cube< D-1 >::template Element< D-2 >( coIndex ) , dim , dir ); |
| 583 |
else dim = D-1; |
| 584 |
} |
| 585 |
template< unsigned int D > template< unsigned int _D > |
| 586 |
typename std::enable_if< _D==1 >::type Cube< D >::_FactorOrientation( Element< D-1 > e , unsigned int& dim , Direction& dir ) |
| 587 |
{ |
| 588 |
unsigned int coIndex; |
| 589 |
e.factor( dir , coIndex ); |
| 590 |
dim = 0; |
| 591 |
} |
| 592 |
|
| 593 |
template< unsigned int D > template< typename Real > |
| 594 |
unsigned int Cube< D >::MCIndex( const Real values[ Cube< D >::ElementNum< 0 >() ] , Real iso ) |
| 595 |
{ |
| 596 |
unsigned int mcIdx = 0; |
| 597 |
for( unsigned int c=0 ; c<ElementNum< 0 >() ; c++ ) if( values[c]<iso ) mcIdx |= (1<<c); |
| 598 |
return mcIdx; |
| 599 |
} |
| 600 |
|
| 601 |
template< unsigned int D > template< unsigned int K > |
| 602 |
unsigned int Cube< D >::ElementMCIndex( Element< K > element , unsigned int mcIndex ){ return _ElementMCIndex( element , mcIndex ); } |
| 603 |
|
| 604 |
template< unsigned int D > |
| 605 |
bool Cube< D >::HasMCRoots( unsigned int mcIndex ) |
| 606 |
{ |
| 607 |
static const unsigned int Mask = (1<<(1<<D)) - 1; |
| 608 |
return mcIndex!=0 && ( mcIndex & Mask )!=Mask; |
| 609 |
} |
| 610 |
|
| 611 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 612 |
typename std::enable_if< _D!=K && K!=0 , unsigned int >::type Cube< D >::_ElementMCIndex( Element< K > element , unsigned int mcIndex ) |
| 613 |
{ |
| 614 |
static const unsigned int Mask = ( 1<<( ElementNum< 0 >() / 2 ) ) - 1; |
| 615 |
static const unsigned int Shift = ElementNum< 0 >() / 2 , _Shift = Cube< K >::template ElementNum< 0 >() / 2; |
| 616 |
unsigned int mcIndex0 = mcIndex & Mask , mcIndex1 = ( mcIndex>>Shift ) & Mask; |
| 617 |
Direction dir ; unsigned int coIndex; |
| 618 |
element.factor( dir , coIndex ); |
| 619 |
if( dir==CROSS ) return Cube< D-1 >::template ElementMCIndex< K-1 >( coIndex , mcIndex0 ) | ( Cube< D-1 >::template ElementMCIndex< K-1 >( coIndex , mcIndex1 )<<_Shift ); |
| 620 |
else return Cube< D-1 >::template ElementMCIndex< K >( coIndex , dir==BACK ? mcIndex0 : mcIndex1 ); |
| 621 |
} |
| 622 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 623 |
typename std::enable_if< _D!=K && K==0 , unsigned int >::type Cube< D >::_ElementMCIndex( Element< K > element , unsigned int mcIndex ) |
| 624 |
{ |
| 625 |
static const unsigned int Mask = ( 1<<( ElementNum< 0 >() / 2 ) ) - 1; |
| 626 |
static const unsigned int Shift = ElementNum< 0 >() / 2 , _Shift = Cube< K >::template ElementNum< 0 >() / 2; |
| 627 |
unsigned int mcIndex0 = mcIndex & Mask , mcIndex1 = ( mcIndex>>Shift ) & Mask; |
| 628 |
Direction dir ; unsigned int coIndex; |
| 629 |
element.factor( dir , coIndex ); |
| 630 |
return Cube< D-1 >::template ElementMCIndex< K >( typename Cube< D-1 >::template Element< K >( coIndex ) , dir==BACK ? mcIndex0 : mcIndex1 ); |
| 631 |
} |
| 632 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 633 |
typename std::enable_if< _D==K , unsigned int >::type Cube< D >::_ElementMCIndex( Element< K > element , unsigned int mcIndex ){ return mcIndex; } |
| 634 |
|
| 635 |
template< unsigned int D > template< unsigned int K > |
| 636 |
void Cube< D >::CellOffset( Element< K > e , IncidentCubeIndex< K > d , int x[D] ){ _CellOffset( e , d , x ); } |
| 637 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 638 |
typename std::enable_if< _D==K >::type Cube< D >::_CellOffset( Element< K > e , IncidentCubeIndex< K > d , int *x ){ for( int d=0 ; d<D ; d++ ) x[d] = 0; } |
| 639 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 640 |
typename std::enable_if< _D!=K && K!=0 >::type Cube< D >::_CellOffset( Element< K > e , IncidentCubeIndex< K > d , int *x ) |
| 641 |
{ |
| 642 |
Direction eDir , dDir ; unsigned int eCoIndex , dCoIndex; |
| 643 |
e.factor( eDir , eCoIndex ) , d.factor( dDir , dCoIndex ); |
| 644 |
if ( eDir==CROSS ){ x[D-1] = 0 ; Cube< D-1 >::CellOffset( typename Cube< D-1 >::template Element< K-1 >( eCoIndex ) , d , x ); } |
| 645 |
else if( eDir==BACK ){ x[D-1] = -1 + ( dDir==BACK ? 0 : 1 ) ; Cube< D-1 >::CellOffset( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) , x ); } |
| 646 |
else if( eDir==FRONT ){ x[D-1] = 0 + ( dDir==BACK ? 0 : 1 ) ; Cube< D-1 >::CellOffset( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) , x ); } |
| 647 |
} |
| 648 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 649 |
typename std::enable_if< _D!=K && K==0 >::type Cube< D >::_CellOffset( Element< K > e , IncidentCubeIndex< K > d , int *x ) |
| 650 |
{ |
| 651 |
Direction eDir , dDir ; unsigned int eCoIndex , dCoIndex; |
| 652 |
e.factor( eDir , eCoIndex ) , d.factor( dDir , dCoIndex ); |
| 653 |
if ( eDir==BACK ){ x[D-1] = -1 + ( dDir==BACK ? 0 : 1 ) ; Cube< D-1 >::CellOffset( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) , x ); } |
| 654 |
else if( eDir==FRONT ){ x[D-1] = 0 + ( dDir==BACK ? 0 : 1 ) ; Cube< D-1 >::CellOffset( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) , x ); } |
| 655 |
} |
| 656 |
|
| 657 |
template< unsigned int D > template< unsigned int K > |
| 658 |
unsigned int Cube< D >::CellOffset( Element< K > e , IncidentCubeIndex< K > d ){ return _CellOffset( e , d ); } |
| 659 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 660 |
typename std::enable_if< _D==K && K==0 , unsigned int >::type Cube< D >::_CellOffset( Element< K > e , IncidentCubeIndex< K > d ){ return 0; } |
| 661 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 662 |
typename std::enable_if< _D==K && K!=0 , unsigned int >::type Cube< D >::_CellOffset( Element< K > e , IncidentCubeIndex< K > d ){ return WindowIndex< IsotropicUIntPack< D , 3 > , IsotropicUIntPack< D , 1 > >::Index; } |
| 663 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 664 |
typename std::enable_if< _D!=K && K!=0 , unsigned int >::type Cube< D >::_CellOffset( Element< K > e , IncidentCubeIndex< K > d ) |
| 665 |
{ |
| 666 |
Direction eDir , dDir ; unsigned int eCoIndex , dCoIndex; |
| 667 |
e.factor( eDir , eCoIndex ) , d.factor( dDir , dCoIndex ); |
| 668 |
if ( eDir==CROSS ){ return 1 + Cube< D-1 >::CellOffset( typename Cube< D-1 >::template Element< K-1 >( eCoIndex ) , d ) * 3; } |
| 669 |
else if( eDir==BACK ){ return 0 + ( dDir==BACK ? 0 : 1 ) + Cube< D-1 >::CellOffset( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) ) * 3; } |
| 670 |
else if( eDir==FRONT ){ return 1 + ( dDir==BACK ? 0 : 1 ) + Cube< D-1 >::CellOffset( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) ) * 3; } |
| 671 |
return 0; |
| 672 |
} |
| 673 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 674 |
typename std::enable_if< _D!=K && K==0 , unsigned int >::type Cube< D >::_CellOffset( Element< K > e , IncidentCubeIndex< K > d ) |
| 675 |
{ |
| 676 |
Direction eDir , dDir ; unsigned int eCoIndex , dCoIndex; |
| 677 |
e.factor( eDir , eCoIndex ) , d.factor( dDir , dCoIndex ); |
| 678 |
if ( eDir==BACK ){ return 0 + ( dDir==BACK ? 0 : 1 ) + Cube< D-1 >::CellOffset( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) ) * 3; } |
| 679 |
else if( eDir==FRONT ){ return 1 + ( dDir==BACK ? 0 : 1 ) + Cube< D-1 >::CellOffset( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) ) * 3; } |
| 680 |
return 0; |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
template< unsigned int D > template< unsigned int K > |
| 685 |
typename Cube< D >::template Element< K > Cube< D >::IncidentElement( Element< K > e , IncidentCubeIndex< K > d ){ return _IncidentElement( e , d ); } |
| 686 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 687 |
#ifdef _MSC_VER |
| 688 |
typename std::enable_if< _D==K , typename Cube< D >::Element< K > >::type Cube< D >::_IncidentElement( Element< K > e , IncidentCubeIndex< K > d ){ return e; } |
| 689 |
#else // !_MSC_VER |
| 690 |
typename std::enable_if< _D==K , typename Cube< D >::template Element< K > >::type Cube< D >::_IncidentElement( Element< K > e , IncidentCubeIndex< K > d ){ return e; } |
| 691 |
#endif // _MSC_VER |
| 692 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 693 |
#ifdef _MSC_VER |
| 694 |
typename std::enable_if< _D!=K && _D!=0 && K!=0 , typename Cube< D >::Element< K > >::type Cube< D >::_IncidentElement( Element< K > e , IncidentCubeIndex< K > d ) |
| 695 |
#else // !_MSC_VER |
| 696 |
typename std::enable_if< _D!=K && _D!=0 && K!=0 , typename Cube< D >::template Element< K > >::type Cube< D >::_IncidentElement( Element< K > e , IncidentCubeIndex< K > d ) |
| 697 |
#endif // _MSC_VER |
| 698 |
{ |
| 699 |
Direction eDir , dDir ; unsigned int eCoIndex , dCoIndex; |
| 700 |
e.factor( eDir , eCoIndex ) , d.factor( dDir , dCoIndex ); |
| 701 |
if ( eDir==CROSS ) return Element< K >( eDir , Cube< D-1 >::IncidentElement( typename Cube< D-1 >::template Element< K-1 >( eCoIndex ) , d ).index ); |
| 702 |
else if( eDir==dDir ) return Element< K >( Opposite( eDir ) , Cube< D-1 >::IncidentElement( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) ).index ); |
| 703 |
else return Element< K >( eDir , Cube< D-1 >::IncidentElement( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) ).index ); |
| 704 |
} |
| 705 |
template< unsigned int D > template< unsigned int K , unsigned int _D > |
| 706 |
#ifdef _MSC_VER |
| 707 |
typename std::enable_if< _D!=K && _D!=0 && K==0 , typename Cube< D >::Element< K > >::type Cube< D >::_IncidentElement( Element< K > e , IncidentCubeIndex< K > d ) |
| 708 |
#else // !_MSC_VER |
| 709 |
typename std::enable_if< _D!=K && _D!=0 && K==0 , typename Cube< D >::template Element< K > >::type Cube< D >::_IncidentElement( Element< K > e , IncidentCubeIndex< K > d ) |
| 710 |
#endif // _MSC_VER |
| 711 |
{ |
| 712 |
Direction eDir , dDir ; unsigned int eCoIndex , dCoIndex; |
| 713 |
e.factor( eDir , eCoIndex ) , d.factor( dDir , dCoIndex ); |
| 714 |
if( eDir==dDir ) return Element< K >( Opposite( eDir ) , Cube< D-1 >::IncidentElement( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) ).index ); |
| 715 |
else return Element< K >( eDir , Cube< D-1 >::IncidentElement( typename Cube< D-1 >::template Element< K >( eCoIndex ) , typename Cube< D-1 >::template IncidentCubeIndex< K >( dCoIndex ) ).index ); |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
///////////////////// |
| 720 |
// MarchingSquares // |
| 721 |
///////////////////// |
| 722 |
const inline int MarchingSquares::edges[][MAX_EDGES*2+1] = |
| 723 |
{ |
| 724 |
// Positive to the right |
| 725 |
// Positive in center |
| 726 |
/////////////////////////////////// (0,0) (1,0) (0,1) (1,1) |
| 727 |
{ -1 , -1 , -1 , -1 , -1 } , // - - - - // |
| 728 |
{ 1 , 0 , -1 , -1 , -1 } , // + - - - // (0,0) - (0,1) | (0,0) - (1,0) |
| 729 |
{ 0 , 2 , -1 , -1 , -1 } , // - + - - // (0,0) - (1,0) | (1,0) - (1,1) |
| 730 |
{ 1 , 2 , -1 , -1 , -1 } , // + + - - // (0,0) - (0,1) | (1,0) - (1,1) |
| 731 |
{ 3 , 1 , -1 , -1 , -1 } , // - - + - // (0,1) - (1,1) | (0,0) - (0,1) |
| 732 |
{ 3 , 0 , -1 , -1 , -1 } , // + - + - // (0,1) - (1,1) | (0,0) - (1,0) |
| 733 |
{ 0 , 1 , 3 , 2 , -1 } , // - + + - // (0,0) - (1,0) | (0,0) - (0,1) & (0,1) - (1,1) | (1,0) - (1,1) |
| 734 |
{ 3 , 2 , -1 , -1 , -1 } , // + + + - // (0,1) - (1,1) | (1,0) - (1,1) |
| 735 |
{ 2 , 3 , -1 , -1 , -1 } , // - - - + // (1,0) - (1,1) | (0,1) - (1,1) |
| 736 |
{ 1 , 3 , 2 , 0 , -1 } , // + - - + // (0,0) - (0,1) | (0,1) - (1,1) & (1,0) - (1,1) | (0,0) - (1,0) |
| 737 |
{ 0 , 3 , -1 , -1 , -1 } , // - + - + // (0,0) - (1,0) | (0,1) - (1,1) |
| 738 |
{ 1 , 3 , -1 , -1 , -1 } , // + + - + // (0,0) - (0,1) | (0,1) - (1,1) |
| 739 |
{ 2 , 1 , -1 , -1 , -1 } , // - - + + // (1,0) - (1,1) | (0,0) - (0,1) |
| 740 |
{ 2 , 0 , -1 , -1 , -1 } , // + - + + // (1,0) - (1,1) | (0,0) - (1,0) |
| 741 |
{ 0 , 1 , -1 , -1 , -1 } , // - + + + // (0,0) - (1,0) | (0,0) - (0,1) |
| 742 |
{ -1 , -1 , -1 , -1 , -1 } , // + + + + // |
| 743 |
}; |
| 744 |
inline int MarchingSquares::AddEdgeIndices( unsigned char mcIndex , int* isoIndices ) |
| 745 |
{ |
| 746 |
int nEdges = 0; |
| 747 |
/* Square is entirely in/out of the surface */ |
| 748 |
if( mcIndex==0 || mcIndex==15 ) return 0; |
| 749 |
|
| 750 |
/* Create the edges */ |
| 751 |
for( int i=0 ; edges[mcIndex][i]!=-1 ; i+=2 ) |
| 752 |
{ |
| 753 |
for( int j=0 ; j<2 ; j++ ) isoIndices[i+j] = edges[mcIndex][i+j]; |
| 754 |
nEdges++; |
| 755 |
} |
| 756 |
return nEdges; |
| 757 |
} |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
#endif //MARCHING_CUBES_INCLUDED |