OSDN Git Service

航行距離のデータ形式をdoubleに変更。少しは誤差が少なくなると思う。
[gvonavish/GVONavish.git] / GVONavish / GVONavish / GVOShipRoute.cpp
1 #include "stdafx.h"
2 #include "GVOShipRoute.h"
3 #include "GVOVector.h"
4
5
6 namespace {
7         const float k_worldLoopThreshold = 0.5f;
8
9         inline POINT s_denormalizedPoint( const GVONormalizedPoint & point )
10         {
11                 POINT p = {
12                         static_cast<long>(::round( point.x() * k_worldWidth )),
13                         static_cast<long>(::round( point.y() * k_worldHeight )),
14                 };
15                 return p;
16         }
17 }
18
19
20 void GVOShipRoute::addRoutePoint( const GVONormalizedPoint & point )
21 {
22         _ASSERT( !isFixed() );
23
24         if ( m_lines.empty() ) {
25                 m_lines.push_back( Line() );
26         }
27
28         Line & line = m_lines.back();
29         if ( line.empty() ) {
30                 line.push_back( point );
31                 return;
32         }
33
34         GVOVector vector( s_denormalizedPoint( line.back() ), s_denormalizedPoint( point ) );
35         m_length += vector.length();
36
37         const GVONormalizedPoint & prevPoint = line.back();
38         if ( prevPoint.isEqualValue( point ) ) {
39                 return;
40         }
41
42         // \90¢\8aE\82ð\8c×\82¢\82¾\8fê\8d\87\82Í\90ü\82ð\95ª\8a\84\82·\82é
43         if ( prevPoint.x() < point.x() && (k_worldLoopThreshold <= (point.x() - prevPoint.x())) ) {
44                 // \90¼\82É\8cü\82©\82Á\82Ä\8c×\82¢\82¾\8fê\8d\87
45                 const GVONormalizedPoint leftSideSubPoint( point.x() - 1.0f, point.y() );
46                 const GVONormalizedPoint rightSideSubPoint( prevPoint.x() + 1.0f, prevPoint.y() );
47
48                 line.push_back( leftSideSubPoint );
49                 m_lines.emplace( m_lines.end(), std::move( Line{ rightSideSubPoint, point } ) );
50         }
51         else if ( point.x() < prevPoint.x() && (k_worldLoopThreshold <= (prevPoint.x() - point.x())) ) {
52                 // \93\8c\82É\8cü\82©\82Á\82Ä\8c×\82¢\82¾\8fê\8d\87
53                 const GVONormalizedPoint rightSideSubPoint( point.x() + 1.0f, point.y() );
54                 const GVONormalizedPoint leftSideSubPoint( prevPoint.x() - 1.0f, prevPoint.y() );
55
56                 line.push_back( rightSideSubPoint );
57                 m_lines.emplace( m_lines.end(), std::move( Line{ leftSideSubPoint, point } ) );
58         }
59         else {
60                 line.push_back( point );
61         }
62 }
63
64
65 void GVOShipRoute::jointPreviousLinesWithRoute( const GVOShipRoute & srcRoute )
66 {
67         // \98A\8c\8b\8c³\82Ì\8dq\98H\82ª\90ü\82ð\95Û\8e\9d\82µ\82Ä\82¢\82È\82¯\82ê\82Î\96³\8e\8b\82·\82é\81B
68         if ( srcRoute.isEmptyRoute() ) {
69                 return;
70         }
71         // \98A\8c\8b\90æ\82Ì\8dq\98H\82ª\90ü\82ð\95Û\8e\9d\82µ\82Ä\82¢\82È\82¯\82ê\82Î\96³\8e\8b\82·\82é\81B
72         if ( isEmptyRoute() ) {
73                 m_lines = srcRoute.m_lines;
74                 return;
75         }
76
77
78         // \82Q\82Â\82Ì\8dq\98H\82Ì\92·\82³\82ð\91«\82·
79         m_length += srcRoute.m_length;
80
81
82         Lines tmp = srcRoute.m_lines;
83
84         // \91O\82Ì\8dq\98H\82Ì\8fI\93_\82Æ\8c»\8dÝ\82Ì\8dq\98H\82Ì\8en\93_\82ª\98A\8c\8b\89Â\94\\82È\82ç\83\89\83C\83\93\82ð\8cq\82°\82È\82¯\82ê\82Î\82È\82ç\82È\82¢
85
86         Line & prevLine = tmp.back();
87         Line & nextLine = m_lines.front();
88
89         // \82Q\82Â\82Ì\90ü\82ª\93_\82ð\95Û\8e\9d\82µ\82Ä\82¢\82é\82©\8am\94F\82µ\82Ä\82¨\82­
90         if ( !prevLine.empty() && !nextLine.empty() ) {
91                 GVONormalizedPoint prevPoint = prevLine.back();
92                 GVONormalizedPoint nextPoint = nextLine.front();
93
94                 // \8dq\98H\8aÔ\82Ì\92·\82³\82ð\91«\82·
95                 const double betweenLength = GVOVector( s_denormalizedPoint( prevPoint ), s_denormalizedPoint( nextPoint ) ).length();
96                 m_length += betweenLength;
97
98                 // \90¢\8aE\82ð\8c×\82¢\82Å\82¢\82È\82¢\82æ\82¤\82È\82ç\90ü\82ð\90Ú\91±\82·\82é
99                 if ( (std::max( prevPoint.x(), nextPoint.x() ) - std::min(prevPoint.x(), nextPoint.x())) < k_worldLoopThreshold ) {
100                         prevLine.insert( prevLine.end(), nextLine.begin(), nextLine.end() );
101                         m_lines.erase( m_lines.begin() );
102                 }
103         }
104
105         tmp.insert( tmp.end(), m_lines.begin(), m_lines.end() );
106         m_lines.swap(tmp);
107
108         // \82Ç\82¿\82ç\82©\82ª\82¨\8bC\82É\93ü\82è\8dq\98H\82È\82ç\98A\8c\8b\8cã\82à\82¨\8bC\82É\93ü\82è\8dq\98H\82Æ\8c©\82È\82·
109         setFavorite( isFavorite() | srcRoute.isFavorite() );
110 }