rtcMeshSet3D.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008
00003  * Robert Bosch LLC
00004  * Research and Technology Center North America
00005  * Palo Alto, California
00006  *
00007  * All rights reserved.
00008  *
00009  *------------------------------------------------------------------------------
00010  * project ....: Autonomous Technologies
00011  * file .......: MeshSet3D.cpp
00012  * authors ....: Benjamin Pitzer
00013  * organization: Robert Bosch LLC
00014  * creation ...: 02/29/2008
00015  * modified ...: $Date: 2009-02-16 15:05:05 -0800 (Mon, 16 Feb 2009) $
00016  * changed by .: $Author: wg75pal $
00017  * revision ...: $Revision: 817 $
00018  */
00019 
00020 //== INCLUDES ==================================================================
00021 #include <cstdio>
00022 #include <cstdlib>
00023 #include <fstream>
00024 #include <iostream>
00025 #include <string>
00026 #include "rtc/rtcMeshSet3D.h"
00027 #include "rtc/rtcBinaryInputHandler.h"
00028 #include "rtc/rtcBinaryOutputHandler.h"
00029 
00030 //== NAMESPACES ================================================================
00031 namespace rtc {
00032 
00033 // Constructor
00034 MeshSet3D::MeshSet3D()
00035 :IOObject(), flags(0)
00036 {
00037 }
00038 
00039 MeshSet3D::MeshSet3D(const MeshSet3D& other)
00040 :IOObject(), flags(0)
00041 {
00042   set(other);
00043 }
00044 
00045 const MeshSet3D &MeshSet3D::operator=(const MeshSet3D& other)
00046 {
00047   set(other);
00048   return *this;
00049 }
00050 
00051 // Destructor
00052 MeshSet3D::~MeshSet3D() {
00053   clear();
00054 }
00055 
00056 int MeshSet3D::numMeshes() const
00057 {
00058   return (int)meshes.size();
00059 }
00060 
00061 void MeshSet3D::clear()
00062 {
00063   for (int i=0;i<int(meshes.size());i++) delete (meshes[i]);
00064   meshes.clear();
00065 }
00066 
00067 void MeshSet3D::set(const MeshSet3D& other)
00068 {
00069   clear();
00070   flags = other.flags;
00071   VecMesh3D::const_iterator it,it_end;
00072   for (it=other.meshes.begin(),it_end=other.meshes.end(); it!=it_end; ++it){
00073     Mesh3D* mesh = new Mesh3D(*(*it));
00074     meshes.push_back(mesh);
00075   }
00076 }
00077 
00078 // updates the normals in all meshes
00079 void MeshSet3D::updateNormals()
00080 {
00081   for (unsigned int i=0;i<meshes.size();i++)
00082     meshes[i]->updateNormals();
00083 }
00084 
00085 bool MeshSet3D::getFlag(int flag) const
00086 {
00087   return (flags&flag);
00088 }
00089 
00090 void MeshSet3D::setFlag(int flag, bool value)
00091 {
00092   if(value)
00093     flags|=flag;
00094   else
00095     flags&=flag;
00096 }
00097 
00098 bool MeshSet3D::hasAABB() const
00099 {
00100   return (flags&MESHSET3D_HAS_AABB);
00101 }
00102 
00103 bool MeshSet3D::write(OutputHandler &oh) const
00104 {
00105   // write number of meshes
00106   int num_meshes = meshes.size();
00107   rtc_write(oh,"flags",flags);
00108   rtc_write(oh,"num_meshes",num_meshes);
00109   // write meshes
00110   for(unsigned int i=0;i<meshes.size();++i) {
00111     meshes[i]->write(oh);
00112   }
00113   if (hasAABB()) {
00114     topleft.write(oh);
00115     bottomright.write(oh);
00116   }
00117   return oh.good();
00118 }
00119 
00120 bool MeshSet3D::read(InputHandler &ih)
00121 {
00122   // read number of meshes
00123   clear();
00124   int num_meshes;
00125   rtc_read(ih,"flags",flags);
00126   rtc_read(ih,"num_meshes",num_meshes);
00127   // load meshes
00128   for(int i=0;i<num_meshes;++i) {
00129     Mesh3D* mesh = new Mesh3D();
00130     mesh->read(ih);
00131     meshes.push_back(mesh);
00132     if(!ih.good())
00133       throw Exception("format error: unexpected end of file");
00134   }
00135   if (hasAABB()) {
00136     topleft.read(ih);
00137     bottomright.read(ih);
00138   }
00139   return ih.good();
00140 }
00141 
00142 bool MeshSet3D::writeToFile( const char* filename ) const
00143 {
00144   try
00145   {
00146     rtc::BinaryOutputHandler bsh;
00147     std::ofstream out(filename, std::ios_base::binary);
00148     bsh.use(out);
00149     write(bsh);
00150   }
00151   catch (Exception e)
00152   {
00153     std::cout << e.getErrorMessage() << std::endl;
00154     return false;
00155   }
00156   return true;
00157 }
00158 
00159 bool MeshSet3D::readFromFile( const char* filename )
00160 {
00161   // load file in binary mode
00162   try {
00163     // Open a stream in binary mode
00164     rtc::BinaryInputHandler bsh;
00165     std::ifstream in(filename,std::ios_base::binary);
00166     bsh.use(in);
00167     read(bsh);
00168   }
00169   catch (Exception e) {
00170     std::cout << e.getErrorMessage() << std::endl;
00171     return false;
00172   }
00173   return true;
00174 }
00175 
00176 void MeshSet3D::read( FILE *fp )
00177 {
00178   size_t res = 0;
00179   // read number of meshes
00180   clear();
00181   int num_meshes;
00182   res+=fread(&flags,sizeof(flags),1,fp);
00183   res+=fread(&num_meshes,sizeof(num_meshes),1,fp);
00184   // load meshes
00185   for(int i=0;i<num_meshes;++i) {
00186     Mesh3D* mesh = new Mesh3D();
00187     mesh->read(fp);
00188     meshes.push_back(mesh);
00189     if(feof(fp))
00190       throw Exception("format error: unexpected end of file");
00191   }
00192   if (hasAABB()) {
00193     res+=fread(&topleft,sizeof(topleft),1,fp);
00194     res+=fread(&bottomright,sizeof(bottomright),1,fp);
00195   }
00196 }
00197 
00198 void MeshSet3D::write(FILE *fp) const
00199 {
00200   // write number of meshes
00201   int num_meshes = meshes.size();
00202   fwrite(&flags,sizeof(flags),1,fp);
00203   fwrite(&num_meshes,sizeof(num_meshes),1,fp);
00204   // write meshes
00205   for(unsigned int i=0;i<meshes.size();++i) {
00206     meshes[i]->write(fp);
00207   }
00208   if (hasAABB()) {
00209     fwrite(&topleft,sizeof(topleft),1,fp);
00210     fwrite(&bottomright,sizeof(bottomright),1,fp);
00211   }
00212 }
00213 
00214 void MeshSet3D::loadToGPU()
00215 {
00216   for (size_t i=0;i<meshes.size();i++)
00217     meshes[i]->loadTexture();
00218 }
00219 
00220 void MeshSet3D::unloadGPU()
00221 {
00222   for (size_t i=0;i<meshes.size();i++)
00223     meshes[i]->unloadTexture();
00224 }
00225 
00226 //==============================================================================
00227 } // namespace rtc
00228 //==============================================================================


rtc
Author(s): Benjamin Pitzer
autogenerated on Thu Jan 2 2014 11:04:53