Changeset 11274

Show
Ignore:
Timestamp:
03/22/10 15:55:52 (3 years ago)
Author:
paulmartz
Message:

2.8 branch: Hm, mysteriously, the ply plugin seems to be missing changes that were previously merged. This commit brings them up to date with svn trunk as of r11237.

Location:
OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/ply.h

    r10012 r11274  
    3434#define __PLY_H__ 
    3535 
    36 #ifdef __cplusplus 
    37 extern "C" { 
    38 #endif 
     36// include to quieten down silly VS warnings 
     37#include <osg/Export> 
    3938 
    4039#include <stdio.h> 
     
    7069typedef struct PlyProperty {    /* description of a property */ 
    7170 
    72   char *name;                           /* property name */ 
     71  const char *name;                           /* property name */ 
    7372  int external_type;                    /* file's data type */ 
    7473  int internal_type;                    /* program's data type */ 
     
    151150extern PlyProperty **ply_get_element_description(PlyFile *, char *, int*, int*); 
    152151extern void ply_get_element_setup( PlyFile *, char *, int, PlyProperty *); 
    153 extern void ply_get_property(PlyFile *, char *, PlyProperty *); 
     152extern void ply_get_property(PlyFile *, const char *, PlyProperty *); 
    154153extern PlyOtherProp *ply_get_other_properties(PlyFile *, char *, int); 
    155154extern void ply_get_element(PlyFile *, void *); 
     
    165164extern int equal_strings(const char *, const char *); 
    166165 
    167  
    168 #ifdef __cplusplus 
    169 } 
    170 #endif 
    171166#endif /* !__PLY_H__ */ 
    172167 
  • OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/plyfile.cpp

    r10012 r11274  
    4040 
    4141#include "ply.h" 
     42#include"typedefs.h" 
     43 
    4244#include <stdio.h> 
    4345#include <stdlib.h> 
    4446#include <math.h> 
    4547#include <string.h> 
     48 
     49#include <osg/Math> 
     50 
     51#if defined(_MSC_VER) && defined(OSG_DISABLE_MSVC_WARNINGS) 
     52    #pragma warning( disable : 4996 ) 
     53#endif 
    4654 
    4755#ifdef WIN32 
     
    316324 
    317325  plyfile = ply_write (fp, nelems, elem_names, file_type); 
     326 
     327  // If the plyfile could not load return NULL 
    318328  if (plyfile == NULL) 
    319329    return (NULL); 
     
    354364  elem = find_element (plyfile, elem_name); 
    355365  if (elem == NULL) { 
    356     fprintf(stderr,"ply_describe_element: can't find element '%s'\n",elem_name); 
    357     exit (-1); 
     366    char error[100]; 
     367    sprintf (error, "ply_describe_element: can't find element '%s'\n",elem_name); 
     368    throw ply::MeshException( error ); 
    358369  } 
    359370 
     
    501512  elem = find_element (plyfile, elem_name); 
    502513  if (elem == NULL) { 
    503     fprintf(stderr,"ply_element_count: can't find element '%s'\n",elem_name); 
    504     exit (-1); 
     514    char error[100]; 
     515    sprintf (error, "ply_element_count: can't find element '%s'\n",elem_name); 
     516    throw ply::MeshException( error ); 
    505517  } 
    506518 
     
    537549      break; 
    538550    default: 
    539       fprintf (stderr, "ply_header_complete: bad file type = %d\n", 
     551      char error[100]; 
     552      sprintf (error, "ply_header_complete: bad file type = %d\n", 
    540553               plyfile->file_type); 
    541       exit (-1); 
     554      throw ply::MeshException( error ); 
    542555  } 
    543556 
     
    596609  elem = find_element (plyfile, elem_name); 
    597610  if (elem == NULL) { 
    598     fprintf(stderr, "ply_elements_setup: can't find element '%s'\n", elem_name); 
    599     exit (-1); 
     611    char error[100]; 
     612    sprintf (error, "ply_elements_setup: can't find element '%s'\n", elem_name); 
     613    throw ply::MeshException( error ); 
    600614  } 
    601615 
     
    846860        return (NULL); 
    847861        } 
    848       plyfile->version = atof (words[2]); 
     862      plyfile->version = osg::asciiToDouble (words[2]); 
    849863    } 
    850864    else if (equal_strings (words[0], "element")) 
     
    941955  plyfile = ply_read (fp, nelems, elem_names); 
    942956 
     957  if(!plyfile) 
     958  { 
     959    std::cout<<"Ply File Error : Could not read file"<<std::endl; 
     960    return NULL; 
     961  } 
     962 
    943963  /* determine the file type and version */ 
    944964 
     
    10601080void ply_get_property( 
    10611081  PlyFile *plyfile, 
    1062   char *elem_name, 
     1082  const char *elem_name, 
    10631083  PlyProperty *prop 
    10641084) 
     
    13311351  elem = find_element (plyfile, elem_name); 
    13321352  if (elem == NULL) { 
    1333     fprintf (stderr, 
    1334             "ply_get_other_element: can't find element '%s'\n", elem_name); 
    1335     exit (-1); 
     1353    char error[100]; 
     1354    sprintf (error, "ply_get_other_element: can't find element '%s'\n", elem_name); 
     1355    throw ply::MeshException( error ); 
    13361356  } 
    13371357 
     
    16511671  words = get_words (plyfile->fp, &nwords, &orig_line); 
    16521672  if (words == NULL) { 
    1653     fprintf (stderr, "ply_get_element: unexpected end of file\n"); 
    1654     exit (-1); 
     1673    char error[100]; 
     1674    sprintf (error, "ply_get_element: unexpected end of file\n"); 
     1675    throw ply::MeshException( error ); 
    16551676  } 
    16561677 
     
    18481869 
    18491870  if (code <= PLY_START_TYPE || code >= PLY_END_TYPE) { 
    1850     fprintf (stderr, "write_scalar_type: bad data code = %d\n", code); 
    1851     exit (-1); 
     1871    char error[100]; 
     1872    sprintf (error, "write_scalar_type: bad data code = %d\n", code); 
     1873    throw ply::MeshException( error ); 
    18521874  } 
    18531875 
     
    21192141      break; 
    21202142    default: 
    2121       fprintf (stderr, "write_binary_item: bad type = %d\n", type); 
    2122       exit (-1); 
     2143      char error[100]; 
     2144      sprintf (error, "write_binary_item: bad type = %d\n", type); 
     2145      throw ply::MeshException( error ); 
    21232146  } 
    21242147} 
     
    21632186      break; 
    21642187    default: 
    2165       fprintf (stderr, "write_ascii_item: bad type = %d\n", type); 
    2166       exit (-1); 
     2188      char error[100]; 
     2189      sprintf (error, "write_ascii_item: bad type = %d\n", type); 
     2190      throw ply::MeshException( error ); 
    21672191  } 
    21682192} 
     
    22352259      break; 
    22362260    default: 
    2237       fprintf (stderr, "get_stored_item: bad type = %d\n", type); 
    2238       exit (-1); 
     2261      char error[100]; 
     2262      sprintf (error, "get_stored_item: bad type = %d\n", type); 
     2263      throw ply::MeshException( error ); 
    22392264  } 
    22402265} 
     
    22672292 
    22682293  ptr = (void *) c; 
     2294  size_t result = 0; 
    22692295 
    22702296  switch (type) { 
    22712297    case PLY_CHAR: 
    2272       fread (ptr, 1, 1, plyfile->fp); 
     2298      result = fread (ptr, 1, 1, plyfile->fp); 
     2299      if(result < 1) 
     2300      { 
     2301          throw ply::MeshException( "Error in reading PLY file." 
     2302                                 "fread not succeeded." ); 
     2303      } 
    22732304      *int_val = *((char *) ptr); 
    22742305      *uint_val = *int_val; 
     
    22772308      case PLY_UCHAR: 
    22782309      case PLY_UINT8: 
    2279           fread (ptr, 1, 1, plyfile->fp); 
     2310          result = fread (ptr, 1, 1, plyfile->fp); 
     2311          if(result < 1) 
     2312          { 
     2313              throw ply::MeshException( "Error in reading PLY file." 
     2314                                 "fread not succeeded." ); 
     2315          } 
    22802316          *uint_val = *((unsigned char *) ptr); 
    22812317          *int_val = *uint_val; 
     
    22832319          break; 
    22842320      case PLY_SHORT: 
    2285           fread (ptr, 2, 1, plyfile->fp); 
     2321          result = fread (ptr, 2, 1, plyfile->fp); 
     2322          if(result < 1 ) 
     2323          { 
     2324              throw ply::MeshException( "Error in reading PLY file." 
     2325                                 "fread not succeeded." ); 
     2326          } 
    22862327          if( plyfile->file_type == PLY_BINARY_BE ) 
    22872328          { 
     
    22972338          break; 
    22982339      case PLY_USHORT: 
    2299           fread (ptr, 2, 1, plyfile->fp); 
     2340          result = fread (ptr, 2, 1, plyfile->fp); 
     2341          if(result < 1) 
     2342          { 
     2343              throw ply::MeshException( "Error in reading PLY file." 
     2344                                 "fread not succeeded." ); 
     2345          } 
    23002346          if( plyfile->file_type == PLY_BINARY_BE ) 
    23012347          { 
     
    23122358      case PLY_INT: 
    23132359      case PLY_INT32: 
    2314           fread (ptr, 4, 1, plyfile->fp); 
     2360          result = fread (ptr, 4, 1, plyfile->fp); 
     2361          if(result < 1) 
     2362          { 
     2363              throw ply::MeshException( "Error in reading PLY file." 
     2364                                 "fread not succeeded." ); 
     2365          } 
    23152366          if( plyfile->file_type == PLY_BINARY_BE ) 
    23162367          { 
     
    23262377          break; 
    23272378      case PLY_UINT: 
    2328           fread (ptr, 4, 1, plyfile->fp); 
     2379          result = fread (ptr, 4, 1, plyfile->fp); 
     2380          if(result < 1) 
     2381          { 
     2382              throw ply::MeshException( "Error in reading PLY file." 
     2383                                 "fread not succeeded." ); 
     2384          } 
    23292385          if( plyfile->file_type == PLY_BINARY_BE ) 
    23302386          { 
     
    23412397      case PLY_FLOAT: 
    23422398      case PLY_FLOAT32: 
    2343           fread (ptr, 4, 1, plyfile->fp); 
     2399          result = fread (ptr, 4, 1, plyfile->fp); 
     2400          if(result < 1) 
     2401          { 
     2402              throw ply::MeshException( "Error in reading PLY file." 
     2403                                 "fread not succeeded." ); 
     2404          } 
    23442405          if( plyfile->file_type == PLY_BINARY_BE ) 
    23452406          { 
     
    23552416          break; 
    23562417      case PLY_DOUBLE: 
    2357           fread (ptr, 8, 1, plyfile->fp); 
    2358           if( plyfile->file_type == PLY_BINARY_BE ) 
    2359           { 
    2360               swap8BE(ptr); 
    2361           } 
    2362           else 
    2363           { 
    2364               swap8LE(ptr); 
    2365           } 
    2366       *double_val = *((double *) ptr); 
    2367       *int_val = (int) *double_val; 
    2368       *uint_val = (unsigned int) *double_val; 
    2369       break; 
     2418        result = fread (ptr, 8, 1, plyfile->fp); 
     2419        if(result < 1) 
     2420        { 
     2421            throw ply::MeshException( "Error in reading PLY file." 
     2422                                "fread not succeeded." ); 
     2423        } 
     2424        if( plyfile->file_type == PLY_BINARY_BE ) 
     2425        { 
     2426            swap8BE(ptr); 
     2427        } 
     2428        else 
     2429        { 
     2430            swap8LE(ptr); 
     2431        } 
     2432        *double_val = *((double *) ptr); 
     2433        *int_val = (int) *double_val; 
     2434        *uint_val = (unsigned int) *double_val; 
     2435        break; 
    23702436    default: 
    2371       fprintf (stderr, "get_binary_item: bad type = %d\n", type); 
    2372       exit (-1); 
     2437      char error[100]; 
     2438      sprintf (error, "get_binary_item: bad type = %d\n", type); 
     2439      throw ply::MeshException( error ); 
    23732440  } 
    23742441} 
     
    24192486    case PLY_FLOAT32: 
    24202487    case PLY_DOUBLE: 
    2421       *double_val = atof (word); 
     2488      *double_val = osg::asciiToDouble(word); 
    24222489      *int_val = (int) *double_val; 
    24232490      *uint_val = (unsigned int) *double_val; 
     
    24252492 
    24262493    default: 
    2427       fprintf (stderr, "get_ascii_item: bad type = %d\n", type); 
    2428       exit (-1); 
     2494      char error[100]; 
     2495      sprintf (error, "get_ascii_item: bad type = %d\n", type); 
     2496      throw ply::MeshException( error ); 
    24292497  } 
    24302498} 
     
    24972565      break; 
    24982566    default: 
    2499       fprintf (stderr, "store_item: bad type = %d\n", type); 
    2500       exit (-1); 
     2567      char error[100]; 
     2568      sprintf (error, "store_item: bad type = %d\n", type); 
     2569      throw ply::MeshException( error ); 
    25012570  } 
    25022571} 
  • OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/typedefs.h

    r10012 r11274  
    1313#define MESH_TYPEDEFS_H 
    1414 
    15 #   ifdef WIN32 
     15#   if defined(_MSC_VER) 
    1616#      include <Winsock2.h> 
    1717#      include <Windows.h> 
     
    1919 
    2020#   include <osg/Notify> 
    21 #    include <cassert> 
    22 #   define MESHASSERT  assert 
     21 
     22#ifdef NDEBUG 
     23#   define MESHASSERT( x ) 
     24#else 
     25#    define MESHASSERT(x) { if( !(x) )                                      \ 
     26              osg::notify(osg::WARN) << "Ply Loader ##### Assert: " << #x << " #####" << std::endl; } 
     27#endif 
     28 
    2329#   define MESHERROR   osg::notify(osg::WARN) 
    2430#   define MESHWARN    osg::notify(osg::WARN) 
    2531#   define MESHINFO    osg::notify(osg::INFO) 
    2632 
    27 #ifdef WIN32 
     33#if defined(_MSC_VER) 
    2834typedef int        socklen_t; 
    2935 
     
    3844#    endif 
    3945 
    40 #endif // Win32 
     46#endif // defined(_MSC_VER) 
    4147 
    4248#include <exception> 
  • OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/vertexData.cpp

    r10012 r11274  
    1010 
    1111#include "typedefs.h" 
    12  
    1312#include "vertexData.h" 
    1413#include "ply.h" 
     14 
    1515#include <cstdlib> 
    1616#include <algorithm> 
     
    127127     
    128128    // read in the faces, asserting that they are only triangles 
    129     uint8_t ind1 = _invertFaces ? 2 : 0; 
    130     uint8_t ind3 = _invertFaces ? 0 : 2; 
     129    int ind1 = _invertFaces ? 2 : 0; 
     130    int ind3 = _invertFaces ? 0 : 2; 
    131131    for( int i = 0; i < nFaces; ++i ) 
    132132    { 
     
    158158    float   version; 
    159159    bool    result = false; 
    160     int        nComments; 
    161     char**    comments; 
    162      
    163     PlyFile* file = ply_open_for_reading( const_cast< char* >( filename ),  
     160    int     nComments; 
     161    char**  comments; 
     162     
     163    PlyFile* file = NULL; 
     164 
     165    // Try to open ply file as for reading 
     166    try{ 
     167            file  = ply_open_for_reading( const_cast< char* >( filename ),  
    164168                                          &nPlyElems, &elemNames,  
    165169                                          &fileType, &version ); 
     170    } 
     171    // Catch the if any exception thrown 
     172    catch( exception& e ) 
     173    { 
     174        MESHERROR << "Unable to read PLY file, an exception occured:  "  
     175                    << e.what() << endl; 
     176    } 
    166177 
    167178    if( !file ) 
     
    173184 
    174185    MESHASSERT( elemNames != 0 ); 
     186     
    175187 
    176188    nComments = file->num_comments; 
     
    196208        int nProps; 
    197209         
    198         PlyProperty** props = ply_get_element_description( file, elemNames[i],  
    199                                                            &nElems, &nProps ); 
     210        PlyProperty** props = NULL; 
     211        try{ 
     212                props = ply_get_element_description( file, elemNames[i],  
     213                                                     &nElems, &nProps ); 
     214        } 
     215        catch( exception& e ) 
     216        { 
     217            MESHERROR << "Unable to get PLY file description, an exception occured:  "  
     218                        << e.what() << endl; 
     219        } 
    200220        MESHASSERT( props != 0 ); 
    201221         
     
    222242            if( ignoreColors ) 
    223243                MESHINFO << "Colors in PLY file ignored per request." << endl; 
    224              
    225             // Read vertices and store in a std::vector array 
    226             readVertices( file, nElems, hasColors && !ignoreColors ); 
    227             // Check whether all vertices are loaded or not 
    228             MESHASSERT( _vertices->size() == static_cast< size_t >( nElems ) ); 
    229             // Check all color elements read or not 
    230             if( hasColors && !ignoreColors ) 
    231                 MESHASSERT( _colors->size() == static_cast< size_t >( nElems ) ); 
     244          
     245            try {    
     246                // Read vertices and store in a std::vector array 
     247                readVertices( file, nElems, hasColors && !ignoreColors ); 
     248                // Check whether all vertices are loaded or not 
     249                MESHASSERT( _vertices->size() == static_cast< size_t >( nElems ) ); 
     250                // Check all color elements read or not 
     251                if( hasColors && !ignoreColors ) 
     252                { 
     253                    MESHASSERT( _colors->size() == static_cast< size_t >( nElems ) ); 
     254                } 
     255 
     256                result = true; 
     257            } 
     258            catch( exception& e ) 
     259            { 
     260                MESHERROR << "Unable to read vertex in PLY file, an exception occured:  "  
     261                            << e.what() << endl; 
     262                // stop for loop by setting the loop variable to break condition 
     263                // this way resources still get released even on error cases 
     264                i = nPlyElems; 
     265                 
     266            } 
    232267        } 
    233268        // If the string is face means triangle info started 
     
    273308 
    274309        // If the normals are not calculated calculate the normals for faces 
    275         if(!_normals.valid()) 
    276             _calculateNormals(); 
    277  
    278          
    279         // set the normals 
    280         geom->setNormalArray(_normals.get()); 
    281         geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); 
     310        if(_triangles.valid()) 
     311        { 
     312            if(!_normals.valid()) 
     313                _calculateNormals(); 
     314 
     315            // set the normals 
     316            geom->setNormalArray(_normals.get()); 
     317            geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); 
     318        } 
    282319         
    283320        // Add the premetive set 
    284         geom->addPrimitiveSet(_triangles.get()); 
     321        if (_triangles.valid() && _triangles->size() > 0 ) 
     322            geom->addPrimitiveSet(_triangles.get()); 
     323        else 
     324            geom->addPrimitiveSet(new osg::DrawArrays(GL_POINTS, 0, _vertices->size())); 
    285325 
    286326        // if color info is given set the color array 
  • OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/vertexData.h

    r10012 r11274  
    2727 
    2828// defined elsewhere 
    29 class PlyFile; 
     29struct PlyFile; 
    3030 
    3131namespace ply