1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/*
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer. Redistributions in binary form must reproduce
the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
Neither the name of the Johns Hopkins University nor the names of its contributors
may be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
#include <stdio.h>
#include "MyMiscellany.h"
template< class Real > Real Random( void ){ return Real( rand() )/RAND_MAX; }
template< class Real , int Dim >
Point< Real , Dim > RandomBallPoint( void )
{
Point< Real , Dim > p;
while(1)
{
for( int d=0 ; d<Dim ; d++ ) p[d] = Real( 1.0-2.0*Random< Real >() );
double l=SquareLength(p);
if( SquareLength( p )<=1 ) return p;
}
}
template< class Real , int Dim >
Point< Real , Dim > RandomSpherePoint( void )
{
Point< Real , Dim > p = RandomBallPoint< Real , Dim >();
return p / (Real)Length( p );
}
/////////////////////////
// CoredVectorMeshData //
/////////////////////////
template< class Vertex , typename Index >
CoredVectorMeshData< Vertex , Index >::CoredVectorMeshData( void ) { oocPointIndex = polygonIndex = threadIndex = 0 ; polygons.resize( std::thread::hardware_concurrency() ); }
template< class Vertex , typename Index >
void CoredVectorMeshData< Vertex , Index >::resetIterator ( void ) { oocPointIndex = polygonIndex = threadIndex = 0; }
template< class Vertex , typename Index >
Index CoredVectorMeshData< Vertex , Index >::addOutOfCorePoint( const Vertex& p )
{
oocPoints.push_back(p);
return ( Index )oocPoints.size()-1;
}
template< class Vertex , typename Index >
Index CoredVectorMeshData< Vertex , Index >::addOutOfCorePoint_s( unsigned int thread , const Vertex& p )
{
size_t sz;
{
static std::mutex m;
std::lock_guard< std::mutex > lock(m);
sz = oocPoints.size();
oocPoints.push_back(p);
}
return (Index)sz;
}
template< class Vertex , typename Index >
void CoredVectorMeshData< Vertex , Index >::addPolygon_s( unsigned int thread , const std::vector< Index >& polygon )
{
polygons[ thread ].push_back( polygon );
}
template< class Vertex , typename Index >
void CoredVectorMeshData< Vertex , Index >::addPolygon_s( unsigned int thread , const std::vector< CoredVertexIndex< Index > >& vertices )
{
std::vector< Index > polygon( vertices.size() );
for( int i=0 ; i<(int)vertices.size() ; i++ )
if( vertices[i].inCore ) polygon[i] = vertices[i].idx;
else polygon[i] = -vertices[i].idx-1;
return addPolygon_s( thread , polygon );
}
template< class Vertex , typename Index >
Index CoredVectorMeshData< Vertex , Index >::nextOutOfCorePoint( Vertex& p )
{
if( oocPointIndex<(Index)oocPoints.size() )
{
p=oocPoints[oocPointIndex++];
return 1;
}
else return 0;
}
template< class Vertex , typename Index >
Index CoredVectorMeshData< Vertex , Index >::nextPolygon( std::vector< CoredVertexIndex< Index > >& vertices )
{
while( true )
{
if( threadIndex<(int)polygons.size() )
{
if( polygonIndex<(Index)( polygons[threadIndex].size() ) )
{
std::vector< Index >& polygon = polygons[threadIndex][ polygonIndex++ ];
vertices.resize( polygon.size() );
for( int i=0 ; i<int(polygon.size()) ; i++ )
if( polygon[i]<0 ) vertices[i].idx = -polygon[i]-1 , vertices[i].inCore = false;
else vertices[i].idx = polygon[i] , vertices[i].inCore = true;
return 1;
}
else threadIndex++ , polygonIndex = 0;
}
else return 0;
}
}
template< class Vertex , typename Index >
size_t CoredVectorMeshData< Vertex , Index >::outOfCorePointCount( void ){ return oocPoints.size(); }
template< class Vertex , typename Index >
size_t CoredVectorMeshData< Vertex , Index >::polygonCount( void )
{
size_t count = 0;
for( size_t i=0 ; i<polygons.size() ; i++ ) count += polygons[i].size();
return count;
}
///////////////////////
// CoredFileMeshData //
///////////////////////
template< class Vertex , typename Index >
CoredFileMeshData< Vertex , Index >::CoredFileMeshData( const char* fileHeader )
{
threadIndex = 0;
oocPoints = 0;
polygons.resize( std::thread::hardware_concurrency() );
for( unsigned int i=0 ; i<polygons.size() ; i++ ) polygons[i] = 0;
char _fileHeader[1024];
sprintf( _fileHeader , "%s_points_" , fileHeader );
oocPointFile = new BufferedReadWriteFile( NULL , _fileHeader );
polygonFiles.resize( std::thread::hardware_concurrency() );
for( unsigned int i=0 ; i<polygonFiles.size() ; i++ )
{
sprintf( _fileHeader , "%s_polygons_t%d_" , fileHeader , i );
polygonFiles[i] = new BufferedReadWriteFile( NULL , _fileHeader );
}
}
template< class Vertex , typename Index >
CoredFileMeshData< Vertex , Index >::~CoredFileMeshData( void )
{
delete oocPointFile;
for( unsigned int i=0 ; i<polygonFiles.size() ; i++ ) delete polygonFiles[i];
}
template< class Vertex , typename Index >
void CoredFileMeshData< Vertex , Index >::resetIterator ( void )
{
oocPointFile->reset();
threadIndex = 0;
for( unsigned int i=0 ; i<polygonFiles.size() ; i++ ) polygonFiles[i]->reset();
}
template< class Vertex , typename Index >
Index CoredFileMeshData< Vertex , Index >::addOutOfCorePoint( const Vertex& p )
{
oocPointFile->write( &p , sizeof( Vertex ) );
oocPoints++;
return oocPoints-1;
}
template< class Vertex , typename Index >
Index CoredFileMeshData< Vertex , Index >::addOutOfCorePoint_s( unsigned int thread , const Vertex& p )
{
Index sz;
{
static std::mutex m;
std::lock_guard< std::mutex > lock(m);
sz = oocPoints;
oocPointFile->write( &p , sizeof( Vertex ) );
oocPoints++;
}
return sz;
}
template< class Vertex , typename Index >
void CoredFileMeshData< Vertex , Index >::addPolygon_s( unsigned int thread , const std::vector< Index >& vertices )
{
unsigned int vSize = (unsigned int)vertices.size();
polygonFiles[thread]->write( &vSize , sizeof(unsigned int) );
polygonFiles[thread]->write( &vertices[0] , sizeof(Index) * vSize );
polygons[thread]++;
}
template< class Vertex , typename Index >
void CoredFileMeshData< Vertex , Index >::addPolygon_s( unsigned int thread , const std::vector< CoredVertexIndex< Index > >& vertices )
{
std::vector< Index > polygon( vertices.size() );
for( unsigned int i=0 ; i<(unsigned int)vertices.size() ; i++ )
if( vertices[i].inCore ) polygon[i] = vertices[i].idx;
else polygon[i] = -vertices[i].idx-1;
return addPolygon_s( thread , polygon );
}
template< class Vertex , typename Index >
Index CoredFileMeshData< Vertex , Index >::nextOutOfCorePoint( Vertex& p )
{
if( oocPointFile->read( &p , sizeof( Vertex ) ) ) return 1;
else return 0;
}
template< class Vertex , typename Index >
Index CoredFileMeshData< Vertex , Index >::nextPolygon( std::vector< CoredVertexIndex< Index > >& vertices )
{
while( true )
{
if( threadIndex<(unsigned int)polygonFiles.size() )
{
unsigned int pSize;
if( polygonFiles[threadIndex]->read( &pSize , sizeof(unsigned int) ) )
{
std::vector< Index > polygon( pSize );
if( polygonFiles[threadIndex]->read( &polygon[0] , sizeof(Index)*pSize ) )
{
vertices.resize( pSize );
for( unsigned int i=0 ; i<(unsigned int)polygon.size() ; i++ )
if( polygon[i]<0 ) vertices[i].idx = -polygon[i]-1 , vertices[i].inCore = false;
else vertices[i].idx = polygon[i] , vertices[i].inCore = true;
return 1;
}
ERROR_OUT( "Failed to read polygon from file" );
}
else threadIndex++;
}
else return 0;
}
}
template< class Vertex , typename Index >
size_t CoredFileMeshData< Vertex , Index >::outOfCorePointCount( void ){ return oocPoints; }
template< class Vertex , typename Index >
size_t CoredFileMeshData< Vertex , Index >::polygonCount( void )
{
size_t count = 0;
for( size_t i=0 ; i<polygons.size() ; i++ ) count += polygons[i];
return count;
}
/////////////
// Simplex //
/////////////
template< class Real , unsigned int Dim , unsigned int K >
void Simplex< Real , Dim , K >::split( Point< Real , Dim > pNormal , Real pOffset , std::vector< Simplex >& back , std::vector< Simplex >& front ) const
{
Real values[K+1];
bool frontSet = false , backSet = false;
// Evaluate the hyper-plane's function at the vertices and mark if strictly front/back vertices have been found
for( unsigned int k=0 ; k<=K ; k++ )
{
values[k] = Point< Real , Dim >::Dot( p[k] , pNormal ) - pOffset;
backSet |= ( values[k]<0 ) , frontSet |= ( values[k]>0 );
}
// If all the vertices are behind or on, or all the vertices are in front or on, we are done.
if( !frontSet ){ back.push_back( *this ) ; return; }
if( !backSet ){ front.push_back( *this ) ; return; }
// Pick some intersection of the hyper-plane with a simplex edge
unsigned int v1 , v2;
Point< Real , Dim > midPoint;
{
for( unsigned int i=0 ; i<K ; i++ ) for( unsigned int j=i+1 ; j<=K ; j++ ) if( values[i]*values[j]<0 )
{
v1 = i , v2 = j;
Real t1 = values[i] / ( values[i] - values[j] ) , t2 = (Real)( 1. - t1 );
midPoint = p[j]*t1 + p[i]*t2;
}
}
// Iterate over each face of the simplex, split it with the hyper-plane and connect the sub-simplices to the mid-point
for( unsigned int i=0 ; i<=K ; i++ )
{
if( i!=v1 && i!=v2 ) continue;
Simplex< Real , Dim , K-1 > f; // The face
Simplex< Real , Dim , K > s; // The sub-simplex
for( unsigned int j=0 , idx=0 ; j<=K ; j++ ) if( j!=i ) f[idx++] = p[j];
std::vector< Simplex< Real , Dim , K-1 > > _back , _front;
f.split( pNormal , pOffset , _back , _front );
s[i] = midPoint;
for( unsigned int j=0 ; j<_back.size() ; j++ )
{
for( unsigned int k=0 ; k<K ; k++ ) s[ k<i ? k : k+1 ] = _back[j][k];
back.push_back( s );
}
for( unsigned int j=0 ; j<_front.size() ; j++ )
{
for( unsigned int k=0 ; k<K ; k++ ) s[ k<i ? k : k+1 ] = _front[j][k];
front.push_back( s );
}
}
}
|