OSDN Git Service

航行距離が復元されないバグ修正。 release/ver1.3.2
authorMandhelingFreak <mandheling30-freak@yahoo.co.jp>
Thu, 13 Mar 2014 18:02:24 +0000 (03:02 +0900)
committerMandhelingFreak <mandheling30-freak@yahoo.co.jp>
Thu, 13 Mar 2014 18:02:24 +0000 (03:02 +0900)
ついでにファイル入出力周りの整理。

GVONavish/GVONavish/GVOShipRoute.cpp
GVONavish/GVONavish/GVOShipRoute.h
GVONavish/GVONavish/GVOShipRouteList.cpp

index 3559d97..3cbd3fc 100644 (file)
@@ -4,6 +4,14 @@
 
 
 namespace {
+       struct ChunkHeader {
+               enum  : uint32_t {
+                       k_Version1 = 1,
+               };
+               const uint32_t version = k_Version1;
+               uint32_t lineCount = 0;
+       };
+
        const float k_worldLoopThreshold = 0.5f;
 
        inline POINT s_denormalizedPoint( const GVONormalizedPoint & point )
@@ -14,6 +22,84 @@ namespace {
                };
                return p;
        }
+
+       inline double s_calcLineLength( const GVOShipRoute::Line & line )
+       {
+               double length = 0.0;
+
+               for ( auto it = line.begin(); it != line.end(); ++it ) {
+                       auto p1 = *it;
+                       if ( std::next(it) == line.end() ) {
+                               break;
+                       }
+                       auto p2 = *std::next( it );
+                       auto vector = GVOVector( s_denormalizedPoint( p1 ), s_denormalizedPoint( p2 ) );
+                       length += vector.length();
+               }
+               return length;
+       }
+}
+
+
+std::ostream & operator << (std::ostream& os, GVOShipRoute& shipRoute)
+{
+       _ASSERT( os.good() );
+       if ( !os.good() ) {
+               throw std::runtime_error( "output stream error." );
+       }
+
+       ChunkHeader header;
+       header.lineCount = shipRoute.getLines().size();
+       os.write( reinterpret_cast<const char *>(&header), sizeof(header) );
+
+       for ( const auto & line : shipRoute.getLines() ) {
+               const size_t count = line.size();
+               os.write( reinterpret_cast<const char *>(&count), sizeof(count) );
+               if ( !line.empty() ) {
+                       os.write( reinterpret_cast<const char *>(&line[0]), sizeof(line[0]) * line.size() );
+               }
+       }
+
+       _ASSERT( os.good() );
+       return os;
+}
+
+
+std::istream & operator >> (std::istream& is, GVOShipRoute& shipRoute)
+{
+       _ASSERT( is.good() );
+       if ( !is.good() ) {
+               throw std::runtime_error( "input stream error." );
+       }
+
+       ChunkHeader header;
+       is.read( reinterpret_cast<char *>(&header), sizeof(header) );
+
+       if ( header.version != ChunkHeader::k_Version1 ) {
+               throw std::runtime_error( "unknown file version." );
+       }
+
+       shipRoute.setFavorite( true );
+       shipRoute.setFix( true );
+
+       for ( size_t k = 0; k < header.lineCount; ++k ) {
+               size_t pointCount = 0;
+               is.read( reinterpret_cast<char *>(&pointCount), sizeof(pointCount) );
+               if ( 0 < pointCount ) {
+                       GVOShipRoute::Line tmp( pointCount );
+                       is.read( reinterpret_cast<char *>(&tmp[0]), sizeof(tmp[0]) * tmp.size() );
+                       if ( !shipRoute.getLines().empty() && !tmp.empty() ) {
+                               auto p1 = shipRoute.getLines().back().back();
+                               auto p2 = tmp.front();
+                               shipRoute.m_length += GVOVector( s_denormalizedPoint( p1 ), s_denormalizedPoint( p2 ) ).length();
+                       }
+                       shipRoute.m_length += s_calcLineLength( tmp );
+                       shipRoute.addLine( std::move( tmp ) );
+               }
+       }
+
+       _ASSERT( is.good() );
+       return is;
 }
 
 
index 1791201..e86f6d5 100644 (file)
@@ -3,8 +3,12 @@
 #include <ctime>
 #include "GVONormalizedPoint.h"
 
+
 //!@brief \8dq\98H
 class GVOShipRoute {
+       friend std::ostream & operator << (std::ostream& os, GVOShipRoute& shipRoute);
+       friend std::istream & operator >> (std::istream& is, GVOShipRoute& shipRoute);
+
 public:
        typedef std::vector<GVONormalizedPoint> Line;
        typedef std::deque<Line> Lines;
index ead5003..62cd528 100644 (file)
@@ -30,17 +30,7 @@ std::ostream & operator <<(std::ostream & os, const GVOShipRouteList & shipRoute
        for ( const auto & shipRoute : shipRouteList.m_shipRouteList ) {
                if ( shipRoute->isFavorite() ) {
                        ++fileHeader.favoritsCount;
-
-                       const size_t lineCount = shipRoute->getLines().size();
-                       os.write( reinterpret_cast<const char *>(&lineCount), sizeof(lineCount) );
-
-                       for ( const auto & line : shipRoute->getLines() ) {
-                               const size_t count = line.size();
-                               os.write( reinterpret_cast<const char *>(&count), sizeof(count) );
-                               if ( !line.empty() ) {
-                                       os.write( reinterpret_cast<const char *>(&line[0]), sizeof(line[0]) * line.size() );
-                               }
-                       }
+                       os << *shipRoute;
                }
        }
 
@@ -73,20 +63,7 @@ std::istream & operator >>(std::istream & is, GVOShipRouteList & shipRouteList)
 
        for ( uint32_t i = 0; i < fileHeader.favoritsCount; ++i ) {
                GVOShipRoutePtr shipRoute( new GVOShipRoute() );
-               shipRoute->setFavorite( true );
-
-               size_t lineCount = 0;
-               is.read( reinterpret_cast<char *>(&lineCount), sizeof(lineCount) );
-
-               for ( size_t k = 0; k < lineCount; ++k ) {
-                       size_t pointCount = 0;
-                       is.read( reinterpret_cast<char *>(&pointCount), sizeof(pointCount) );
-                       if ( 0 < pointCount ) {
-                               GVOShipRoute::Line tmp( pointCount );
-                               is.read( reinterpret_cast<char *>(&tmp[0]), sizeof(tmp[0]) * tmp.size() );
-                               shipRoute->addLine( std::move( tmp ) );
-                       }
-               }
+               is >> *shipRoute;
 
                workRouteList.push_back( std::move( shipRoute ) );
        }