/* 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 template Real Random(void){return Real(rand())/RAND_MAX;} template Point3D RandomBallPoint(void){ Point3D p; while(1){ p.coords[0]=Real(1.0-2.0*Random()); p.coords[1]=Real(1.0-2.0*Random()); p.coords[2]=Real(1.0-2.0*Random()); double l=SquareLength(p); if(l<=1){return p;} } } template Point3D RandomSpherePoint(void){ Point3D p=RandomBallPoint(); Real l=Real(Length(p)); p.coords[0]/=l; p.coords[1]/=l; p.coords[2]/=l; return p; } template double SquareLength(const Point3D& p){return p.coords[0]*p.coords[0]+p.coords[1]*p.coords[1]+p.coords[2]*p.coords[2];} template double Length(const Point3D& p){return sqrt(SquareLength(p));} template double SquareDistance(const Point3D& p1,const Point3D& p2){ return (p1.coords[0]-p2.coords[0])*(p1.coords[0]-p2.coords[0])+(p1.coords[1]-p2.coords[1])*(p1.coords[1]-p2.coords[1])+(p1.coords[2]-p2.coords[2])*(p1.coords[2]-p2.coords[2]); } template double Distance(const Point3D& p1,const Point3D& p2){return sqrt(SquareDistance(p1,p2));} template void CrossProduct(const Point3D& p1,const Point3D& p2,Point3D& p){ p.coords[0]= p1.coords[1]*p2.coords[2]-p1.coords[2]*p2.coords[1]; p.coords[1]=-p1.coords[0]*p2.coords[2]+p1.coords[2]*p2.coords[0]; p.coords[2]= p1.coords[0]*p2.coords[1]-p1.coords[1]*p2.coords[0]; } template void EdgeCollapse(const Real& edgeRatio,std::vector& triangles,std::vector< Point3D >& positions,std::vector< Point3D >* normals){ int i,j,*remapTable,*pointCount,idx[3]; Point3D p[3],q[2],c; double d[3],a; double Ratio=12.0/sqrt(3.0); // (Sum of Squares Length / Area) for and equilateral triangle remapTable=new int[positions.size()]; pointCount=new int[positions.size()]; for(i=0;i=0;i--){ for(j=0;j<3;j++){ idx[j]=triangles[i].idx[j]; while(remapTable[idx[j]] a*Ratio){ // Find the smallest edge j=0; if(d[1]=0;i--){ for(j=0;j<3;j++){ idx[j]=triangles[i].idx[j]; while(remapTable[idx[j]] void TriangleCollapse(const Real& edgeRatio,std::vector& triangles,std::vector< Point3D >& positions,std::vector< Point3D >* normals){ int i,j,*remapTable,*pointCount,idx[3]; Point3D p[3],q[2],c; double d[3],a; double Ratio=12.0/sqrt(3.0); // (Sum of Squares Length / Area) for and equilateral triangle remapTable=new int[positions.size()]; pointCount=new int[positions.size()]; for(i=0;i=0;i--){ for(j=0;j<3;j++){ idx[j]=triangles[i].idx[j]; while(remapTable[idx[j]] a*Ratio){ // Find the smallest edge j=0; if(d[1]=0;i--){ for(j=0;j<3;j++){ idx[j]=triangles[i].idx[j]; while(remapTable[idx[j]] long long Triangulation::EdgeIndex( int p1 , int p2 ) { if(p1>p2) {return ((long long)(p1)<<32) | ((long long)(p2));} else {return ((long long)(p2)<<32) | ((long long)(p1));} } template int Triangulation::factor(int tIndex,int& p1,int& p2,int & p3){ if(triangles[tIndex].eIndex[0]<0 || triangles[tIndex].eIndex[1]<0 || triangles[tIndex].eIndex[2]<0){return 0;} if(edges[triangles[tIndex].eIndex[0]].tIndex[0]==tIndex){p1=edges[triangles[tIndex].eIndex[0]].pIndex[0];} else {p1=edges[triangles[tIndex].eIndex[0]].pIndex[1];} if(edges[triangles[tIndex].eIndex[1]].tIndex[0]==tIndex){p2=edges[triangles[tIndex].eIndex[1]].pIndex[0];} else {p2=edges[triangles[tIndex].eIndex[1]].pIndex[1];} if(edges[triangles[tIndex].eIndex[2]].tIndex[0]==tIndex){p3=edges[triangles[tIndex].eIndex[2]].pIndex[0];} else {p3=edges[triangles[tIndex].eIndex[2]].pIndex[1];} return 1; } template double Triangulation::area(int p1,int p2,int p3){ Point3D q1,q2,q; for(int i=0;i<3;i++){ q1.coords[i]=points[p2].coords[i]-points[p1].coords[i]; q2.coords[i]=points[p3].coords[i]-points[p1].coords[i]; } CrossProduct(q1,q2,q); return Length(q); } template double Triangulation::area(int tIndex){ int p1,p2,p3; factor(tIndex,p1,p2,p3); return area(p1,p2,p3); } template double Triangulation::area(void){ double a=0; for(int i=0;i int Triangulation::addTriangle(int p1,int p2,int p3){ hash_map::iterator iter; int tIdx,eIdx,p[3]; p[0]=p1; p[1]=p2; p[2]=p3; triangles.push_back(TriangulationTriangle()); tIdx=int(triangles.size())-1; for(int i=0;i<3;i++) { long long e = EdgeIndex(p[i],p[(i+1)%3]); iter=edgeMap.find(e); if(iter==edgeMap.end()) { TriangulationEdge edge; edge.pIndex[0]=p[i]; edge.pIndex[1]=p[(i+1)%3]; edges.push_back(edge); eIdx=int(edges.size())-1; edgeMap[e]=eIdx; edges[eIdx].tIndex[0]=tIdx; } else{ eIdx=edgeMap[e]; if(edges[eIdx].pIndex[0]==p[i]){ if(edges[eIdx].tIndex[0]<0){edges[eIdx].tIndex[0]=tIdx;} else{printf("Edge Triangle in use 1\n");return 0;} } else{ if(edges[eIdx].tIndex[1]<0){edges[eIdx].tIndex[1]=tIdx;} else{printf("Edge Triangle in use 2\n");return 0;} } } triangles[tIdx].eIndex[i]=eIdx; } return tIdx; } template int Triangulation::flipMinimize(int eIndex){ double oldArea,newArea; int oldP[3],oldQ[3],newP[3],newQ[3]; TriangulationEdge newEdge; if(edges[eIndex].tIndex[0]<0 || edges[eIndex].tIndex[1]<0){return 0;} if(!factor(edges[eIndex].tIndex[0],oldP[0],oldP[1],oldP[2])){return 0;} if(!factor(edges[eIndex].tIndex[1],oldQ[0],oldQ[1],oldQ[2])){return 0;} oldArea=area(oldP[0],oldP[1],oldP[2])+area(oldQ[0],oldQ[1],oldQ[2]); int idxP,idxQ; for(idxP=0;idxP<3;idxP++){ int i; for(i=0;i<3;i++){if(oldP[idxP]==oldQ[i]){break;}} if(i==3){break;} } for(idxQ=0;idxQ<3;idxQ++){ int i; for(i=0;i<3;i++){if(oldP[i]==oldQ[idxQ]){break;}} if(i==3){break;} } if(idxP==3 || idxQ==3){return 0;} newP[0]=oldP[idxP]; newP[1]=oldP[(idxP+1)%3]; newP[2]=oldQ[idxQ]; newQ[0]=oldQ[idxQ]; newQ[1]=oldP[(idxP+2)%3]; newQ[2]=oldP[idxP]; newArea=area(newP[0],newP[1],newP[2])+area(newQ[0],newQ[1],newQ[2]); if(oldArea<=newArea){return 0;} // Remove the entry in the hash_table for the old edge edgeMap.erase(EdgeIndex(edges[eIndex].pIndex[0],edges[eIndex].pIndex[1])); // Set the new edge so that the zero-side is newQ edges[eIndex].pIndex[0]=newP[0]; edges[eIndex].pIndex[1]=newQ[0]; // Insert the entry into the hash_table for the new edge edgeMap[EdgeIndex(newP[0],newQ[0])]=eIndex; // Update the triangle information for(int i=0;i<3;i++){ int idx; idx=edgeMap[EdgeIndex(newQ[i],newQ[(i+1)%3])]; triangles[edges[eIndex].tIndex[0]].eIndex[i]=idx; if(idx!=eIndex){ if(edges[idx].tIndex[0]==edges[eIndex].tIndex[1]){edges[idx].tIndex[0]=edges[eIndex].tIndex[0];} if(edges[idx].tIndex[1]==edges[eIndex].tIndex[1]){edges[idx].tIndex[1]=edges[eIndex].tIndex[0];} } idx=edgeMap[EdgeIndex(newP[i],newP[(i+1)%3])]; triangles[edges[eIndex].tIndex[1]].eIndex[i]=idx; if(idx!=eIndex){ if(edges[idx].tIndex[0]==edges[eIndex].tIndex[0]){edges[idx].tIndex[0]=edges[eIndex].tIndex[1];} if(edges[idx].tIndex[1]==edges[eIndex].tIndex[0]){edges[idx].tIndex[1]=edges[eIndex].tIndex[1];} } } return 1; } ///////////////////////// // CoredVectorMeshData // ///////////////////////// template< class Vertex > CoredVectorMeshData< Vertex >::CoredVectorMeshData( void ) { oocPointIndex = polygonIndex = 0; } template< class Vertex > void CoredVectorMeshData< Vertex >::resetIterator ( void ) { oocPointIndex = polygonIndex = 0; } template< class Vertex > int CoredVectorMeshData< Vertex >::addOutOfCorePoint( const Vertex& p ) { oocPoints.push_back(p); return int(oocPoints.size())-1; } template< class Vertex > int CoredVectorMeshData< Vertex >::addOutOfCorePoint_s( const Vertex& p ) { size_t sz; #pragma omp critical (CoredVectorMeshData_addOutOfCorePoint_s ) { sz = oocPoints.size(); oocPoints.push_back(p); } return (int)sz; } template< class Vertex > int CoredVectorMeshData< Vertex >::addPolygon_s( const std::vector< int >& polygon ) { size_t sz; #pragma omp critical (CoredVectorMeshData_addPolygon_s) { sz = polygon.size(); polygons.push_back( polygon ); } return (int)sz; } template< class Vertex > int CoredVectorMeshData< Vertex >::addPolygon_s( const std::vector< CoredVertexIndex >& vertices ) { std::vector< int > 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( polygon ); } template< class Vertex > int CoredVectorMeshData< Vertex >::nextOutOfCorePoint( Vertex& p ) { if( oocPointIndex int CoredVectorMeshData< Vertex >::nextPolygon( std::vector< CoredVertexIndex >& vertices ) { if( polygonIndex& polygon = polygons[ polygonIndex++ ]; vertices.resize( polygon.size() ); for( int i=0 ; i int CoredVectorMeshData< Vertex >::outOfCorePointCount(void){return int(oocPoints.size());} template< class Vertex > int CoredVectorMeshData< Vertex >::polygonCount( void ) { return int( polygons.size() ); } /////////////////////// // CoredFileMeshData // /////////////////////// template< class Vertex > CoredFileMeshData< Vertex >::CoredFileMeshData( void ) { oocPoints = polygons = 0; oocPointFile = new BufferedReadWriteFile(); polygonFile = new BufferedReadWriteFile(); } template< class Vertex > CoredFileMeshData< Vertex >::~CoredFileMeshData( void ) { delete oocPointFile; delete polygonFile; } template< class Vertex > void CoredFileMeshData< Vertex >::resetIterator ( void ) { oocPointFile->reset(); polygonFile->reset(); } template< class Vertex > int CoredFileMeshData< Vertex >::addOutOfCorePoint( const Vertex& p ) { oocPointFile->write( &p , sizeof( Vertex ) ); oocPoints++; return oocPoints-1; } template< class Vertex > int CoredFileMeshData< Vertex >::addOutOfCorePoint_s( const Vertex& p ) { int sz; #pragma omp critical (CoredFileMeshData_addOutOfCorePoint_s) { sz = oocPoints; oocPointFile->write( &p , sizeof( Vertex ) ); oocPoints++; } return sz; } template< class Vertex > int CoredFileMeshData< Vertex >::addPolygon_s( const std::vector< int >& vertices ) { int sz , vSize = (int)vertices.size(); #pragma omp critical (CoredFileMeshData_addPolygon_s ) { sz = polygons; polygonFile->write( &vSize , sizeof(int) ); polygonFile->write( &vertices[0] , sizeof(int) * vSize ); polygons++; } return sz; } template< class Vertex > int CoredFileMeshData< Vertex >::addPolygon_s( const std::vector< CoredVertexIndex >& vertices ) { std::vector< int > 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( polygon ); } template< class Vertex > int CoredFileMeshData< Vertex >::nextOutOfCorePoint( Vertex& p ) { if( oocPointFile->read( &p , sizeof( Vertex ) ) ) return 1; else return 0; } template< class Vertex > int CoredFileMeshData< Vertex >::nextPolygon( std::vector< CoredVertexIndex >& vertices ) { int pSize; if( polygonFile->read( &pSize , sizeof(int) ) ) { std::vector< int > polygon( pSize ); if( polygonFile->read( &polygon[0] , sizeof(int)*pSize ) ) { vertices.resize( pSize ); for( int i=0 ; i int CoredFileMeshData< Vertex >::outOfCorePointCount( void ){ return oocPoints; } template< class Vertex > int CoredFileMeshData< Vertex >::polygonCount( void ) { return polygons; }