ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Geometry.cpp
Go to the documentation of this file.
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 #include "Geometry.h"
29 #include <stdio.h>
30 #include <string.h>
31 #ifdef _WIN32
32 #include <io.h>
33 #endif // _WIN32
34 
36 // CoredMeshData //
38 
41 
43 // BufferedReadWriteFile //
45 BufferedReadWriteFile::BufferedReadWriteFile( char* fileName , int bufferSize )
46 {
47  _bufferIndex = 0;
48  _bufferSize = bufferSize;
49  if( fileName ) strcpy( _fileName , fileName ) , tempFile = false , _fp = fopen( _fileName , "w+b" );
50  else
51  {
52  strcpy( _fileName , "PR_XXXXXX" );
53 #ifdef _WIN32
54  _mktemp( _fileName );
55  _fp = fopen( _fileName , "w+b" );
56 #else // !_WIN32
57  _fp = fdopen( mkstemp( _fileName ) , "w+b" );
58 #endif // _WIN32
59  tempFile = true;
60  }
61  if( !_fp ) fprintf( stderr , "[ERROR] Failed to open file: %s\n" , _fileName ) , exit( 0 );
62  _buffer = (char*) malloc( _bufferSize );
63 }
65 {
66  free( _buffer );
67  fclose( _fp );
68  if( tempFile ) remove( _fileName );
69 }
71 {
72  if( _bufferIndex ) fwrite( _buffer , 1 , _bufferIndex , _fp );
73  _bufferIndex = 0;
74  fseek( _fp , 0 , SEEK_SET );
75  _bufferIndex = 0;
76  _bufferSize = fread( _buffer , 1 , _bufferSize , _fp );
77 }
78 bool BufferedReadWriteFile::write( const void* data , size_t size )
79 {
80  if( !size ) return true;
81  char* _data = (char*) data;
82  size_t sz = _bufferSize - _bufferIndex;
83  while( sz<=size )
84  {
85  memcpy( _buffer+_bufferIndex , _data , sz );
86  fwrite( _buffer , 1 , _bufferSize , _fp );
87  _data += sz;
88  size -= sz;
89  _bufferIndex = 0;
90  sz = _bufferSize;
91  }
92  if( size )
93  {
94  memcpy( _buffer+_bufferIndex , _data , size );
95  _bufferIndex += size;
96  }
97  return true;
98 }
99 bool BufferedReadWriteFile::read( void* data , size_t size )
100 {
101  if( !size ) return true;
102  char *_data = (char*) data;
103  size_t sz = _bufferSize - _bufferIndex;
104  while( sz<=size )
105  {
106  if( size && !_bufferSize ) return false;
107  memcpy( _data , _buffer+_bufferIndex , sz );
108  _bufferSize = fread( _buffer , 1 , _bufferSize , _fp );
109  _data += sz;
110  size -= sz;
111  _bufferIndex = 0;
112  if( !size ) return true;
113  sz = _bufferSize;
114  }
115  if( size )
116  {
117  if( !_bufferSize ) return false;
118  memcpy( _data , _buffer+_bufferIndex , size );
119  _bufferIndex += size;
120  }
121  return true;
122 }
int size
BufferedReadWriteFile(char *fileName=NULL, int bufferSize=(1<< 20))
Definition: Geometry.cpp:45
bool read(void *data, size_t size)
Definition: Geometry.cpp:99
~BufferedReadWriteFile(void)
Definition: Geometry.cpp:64
bool write(const void *data, size_t size)
Definition: Geometry.cpp:78
TriangulationEdge(void)
Definition: Geometry.cpp:39
#define SEEK_SET
Definition: qioapi.cpp:38