File: GhostTrail.cpp

package info (click to toggle)
xmoto 0.5.11+dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 80,908 kB
  • sloc: cpp: 96,757; ansic: 22,196; sh: 4,940; makefile: 1,073; yacc: 289; sed: 16
file content (141 lines) | stat: -rw-r--r-- 4,546 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*=============================================================================
XMOTO

This file is part of XMOTO.

XMOTO is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

XMOTO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with XMOTO; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
=============================================================================*/

#include "GhostTrail.h"
#include "../Replay.h"

#define TRAIL_INTERPOLATED_TRAIL_INTERNODE_LENGTH 0.3
#define TRAIL_INTERPOLATION_STEP 0.1

GhostTrail::GhostTrail(FileGhost* i_ghost) {
    m_ghost = i_ghost;
    m_initialized = false;
}

GhostTrail::~GhostTrail() {
  m_trailData.clear();
  m_simplifiedTrailData.clear();
  m_interpolatedTrailData.clear();
}

void GhostTrail::initialize() {

  if(m_initialized) {
    return;
  }
  
  if(m_ghost == NULL) {
    return;
  }

  m_initialized = true;

  try {
    m_trailData.clear();
    m_interpolatedTrailData.clear();
    m_simplifiedTrailData.clear();
    
    // all states for Ghost Trail
    BikeState v_bs(m_ghost->getPhysicsSettings());
    ReplayPosition v_rp = m_ghost->getReplay()->getReplayPosition();
    m_ghost->getReplay()->rewindAtBeginning();
    while( !m_ghost->getReplay()->endOfFile()) {
      m_ghost->getReplay()->loadState(&v_bs, m_ghost->getPhysicsSettings());
      m_trailData.push_back(v_bs.CenterP);
    }
    m_ghost->getReplay()->rewindAtPosition(v_rp);
    
    // now lets try real linear interpolation   
    Vector2f v_P_old = m_trailData[0], 
      v_P_new, 
      v_vecTmp;
    float v_time = TRAIL_INTERPOLATION_STEP;
    
    for(unsigned int i=1; i<m_trailData.size(); i++) {
      
      //calculate the rise of the function (m) from our current two trail points: p1 to p0
      float dx = m_trailData[i].x - m_trailData[i-1].x;
      float dy = m_trailData[i].y - m_trailData[i-1].y;
      v_P_old = m_trailData[i-1];
      v_vecTmp = Vector2f(dx,dy);
      
      // this is very ugly code, to clean, to clean
      //check if teleportation occurred, lets assume that 7 is a size big enough for beeing usable as marker
      Vector2f v_checkTeleport = Vector2f(  m_trailData[i].x -  v_P_old.x, m_trailData[i].y - v_P_old.y);
      if( v_checkTeleport.length() > 5 ) {
	continue;  
      }
      
      // this is very ugly code, to clean, to clean
      // check if new position vector is very near the next simplifiedtrailData, else pushback vectors in its direction, assume 0.2
      int j=0;
      do {
	// so lets push back new vector2fsm untilk we're near the next point on the simplified trail
	v_P_new.x = (v_vecTmp.x / v_vecTmp.length()) * v_time + v_P_old.x;
	v_P_new.y = (v_vecTmp.y / v_vecTmp.length()) * v_time + v_P_old.y;
	
	m_interpolatedTrailData.push_back(v_P_new);
	v_P_old = v_P_new;
	
	j++;
	v_checkTeleport = Vector2f( m_trailData[j].x -  v_P_old.x, m_trailData[j].y - v_P_old.y);
	if( v_checkTeleport.length() > 5 ) {
	  continue;  
	}
	
      } while( j<int(v_vecTmp.length()/v_time) );
    }
    
    // now smoothen path in time: cumulate Vector.length, every n length pushBack median
    // interpolated in time, not position
    float v_cumulum = 0; 
    for(unsigned int i=1; i<m_interpolatedTrailData.size(); i++) {
      Vector2f v_vec = Vector2f(fabs(m_interpolatedTrailData[i].x - m_interpolatedTrailData[i-1].x), fabs(m_interpolatedTrailData[i].y - m_interpolatedTrailData[i-1].y));
      v_cumulum+=v_vec.length();
      if(v_cumulum > TRAIL_INTERPOLATED_TRAIL_INTERNODE_LENGTH) {
	m_simplifiedTrailData.push_back(m_interpolatedTrailData[i-1]);
	v_cumulum = 0;
      }
    }
    
    
    m_trailAvailable = true; 
  } catch(Exception &e) {
  } 
}

std::vector<Vector2f>* GhostTrail::getGhostTrailData() { 
  initialize();
  return &m_trailData; 
}

std::vector<Vector2f>* GhostTrail::getSimplifiedGhostTrailData() {
  initialize();
  return &m_simplifiedTrailData; 
}
 
std::vector<Vector2f>* GhostTrail::getInterpolatedGhostTrailData() {
  initialize();
  return &m_interpolatedTrailData;
}

bool GhostTrail::getGhostTrailAvailable() {
  return m_ghost != NULL;
}