Use singleton-like constructs instead of static

pointer to quad set etc.


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/reverse_mode@10809 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2012-02-05 22:24:20 +00:00
parent 648eb17e0a
commit 80ec893758
6 changed files with 75 additions and 56 deletions

View File

@ -25,28 +25,19 @@
#include "tracks/quad_graph.hpp"
#include "tracks/quad_set.hpp"
// A static variable that gives a single graph node easy access to
// all quads and avoids unnecessary parameters in many calls.
QuadSet *GraphNode::m_all_quads=NULL;
// This static variable gives a node access to the graph, and therefore
// to the quad to which a graph node index belongs.
QuadGraph *GraphNode::m_all_nodes=NULL;
// ----------------------------------------------------------------------------
/** Constructor. Saves the quad index which belongs to this graph node.
* \param index Index of the quad to use for this node (in m_all_quads).
* \param index Index of the quad to use for this node (in QuadSet).
*/
GraphNode::GraphNode(unsigned int quad_index, unsigned int node_index)
{
assert(quad_index<m_all_quads->getNumberOfQuads());
assert(quad_index<QuadSet::get()->getNumberOfQuads());
m_quad_index = quad_index;
m_node_index = node_index;
m_predecessor = -1;
m_distance_from_start = 0;
const Quad &quad = m_all_quads->getQuad(m_quad_index);
const Quad &quad = QuadSet::get()->getQuad(m_quad_index);
// FIXME: the following values should depend on the actual orientation
// of the quad. ATM we always assume that indices 0,1 are the lower end,
// and 2,3 are the upper end.
@ -75,17 +66,17 @@ GraphNode::GraphNode(unsigned int quad_index, unsigned int node_index)
void GraphNode::addSuccessor(unsigned int to)
{
m_successor_node.push_back(to);
// m_quad_index is the quad index, so we use m_all_quads
const Quad &this_quad = m_all_quads->getQuad(m_quad_index);
// to is the graph node, so we have to use m_all_nodes to get the right quad
GraphNode &gn = m_all_nodes->getNode(to);
const Quad &next_quad = m_all_nodes->getQuadOfNode(to);
// m_quad_index is the quad index
const Quad &this_quad = QuadSet::get()->getQuad(m_quad_index);
// to is the graph node
GraphNode &gn = QuadGraph::get()->getNode(to);
const Quad &next_quad = QuadGraph::get()->getQuadOfNode(to);
// Keep the first predecessor, which is usually the most 'natural' one.
if(gn.m_predecessor==-1)
gn.m_predecessor = m_node_index;
core::vector2df d2 = m_lower_center_2d
- m_all_nodes->getNode(to).m_lower_center_2d;
- QuadGraph::get()->getNode(to).m_lower_center_2d;
Vec3 diff = next_quad.getCenter() - this_quad.getCenter();
m_distance_to_next.push_back(d2.getLength());
@ -107,16 +98,17 @@ void GraphNode::addSuccessor(unsigned int to)
// from start of the last node) could be smaller than some of the
// paths. This can result in incorrect results for the arrival time
// estimation of the AI karts. See trac #354 for details.
if(m_all_nodes->getNode(to).m_distance_from_start==0)
if(QuadGraph::get()->getNode(to).m_distance_from_start==0)
{
m_all_nodes->getNode(to).m_distance_from_start = distance_for_next;
QuadGraph::get()->getNode(to).m_distance_from_start
= distance_for_next;
}
else if(m_all_nodes->getNode(to).m_distance_from_start
else if(QuadGraph::get()->getNode(to).m_distance_from_start
< distance_for_next)
{
float delta = distance_for_next
- m_all_nodes->getNode(to).getDistanceFromStart();
m_all_nodes->updateDistancesForAllSuccessors(to, delta);
- QuadGraph::get()->getNode(to).getDistanceFromStart();
QuadGraph::get()->updateDistancesForAllSuccessors(to, delta);
}
}
} // addSuccessor

View File

@ -109,13 +109,6 @@ class GraphNode
std::vector< int > m_checkline_requirements;
public:
/** Keep a shared pointer so that some asserts and tests can be
* done without adding additional parameters. */
static QuadSet *m_all_quads;
/** Keep a shared pointer to the graph structure so that each node
* has access to the actual quad to which a node points. */
static QuadGraph *m_all_nodes;
GraphNode(unsigned int quad_index, unsigned int node_index);
void addSuccessor (unsigned int to);
void getDistances(const Vec3 &xyz, Vec3 *result);
@ -136,14 +129,14 @@ public:
// ------------------------------------------------------------------------
/** Returns the quad of this graph node. */
const Quad& getQuad() const {return m_all_quads->getQuad(m_quad_index);}
const Quad& getQuad() const {return QuadSet::get()->getQuad(m_quad_index);}
// ------------------------------------------------------------------------
/** Returns the i-th. point of a quad. ATM this just returns the vertices
* from the quads, but if necessary this method will also consider
* rotated quads. So index 0 will always be lower left point, then
* counterclockwise. */
const Vec3& operator[](int i) const
{return m_all_quads->getQuad(m_quad_index)[i];}
{return QuadSet::get()->getQuad(m_quad_index)[i];}
// ------------------------------------------------------------------------
/** Returns the distance to the j-th. successor. */
float getDistanceToSuccessor(unsigned int j) const
@ -183,7 +176,7 @@ public:
* \param index Index of the successor. */
bool ignoreSuccessorForAI(unsigned int i) const
{
return m_all_quads->getQuad(m_successor_node[i]).letAIIgnore();
return QuadSet::get()->getQuad(m_successor_node[i]).letAIIgnore();
};
// ------------------------------------------------------------------------
/** Returns a predecessor for this node. */

View File

@ -50,13 +50,13 @@ QuadGraph::QuadGraph(const std::string &quad_file_name,
m_mesh = NULL;
m_mesh_buffer = NULL;
m_lap_length = 0;
m_all_quads = new QuadSet(quad_file_name);
QuadSet::create();
QuadSet::get()->init(quad_file_name);
if(m_reverse) {
m_all_quads->reverse_all_quads();
QuadSet::get()->reverse_all_quads();
}
m_quad_filename = quad_file_name;
GraphNode::m_all_quads = m_all_quads;
GraphNode::m_all_nodes = this;
m_quad_graph = this;
load(graph_file_name);
} // QuadGraph
@ -64,7 +64,7 @@ QuadGraph::QuadGraph(const std::string &quad_file_name,
/** Destructor, removes all nodes of the graph. */
QuadGraph::~QuadGraph()
{
delete m_all_quads;
QuadSet::destroy();
for(unsigned int i=0; i<m_all_nodes.size(); i++) {
delete m_all_nodes[i];
}
@ -79,8 +79,9 @@ void QuadGraph::addSuccessor(unsigned int from, unsigned int to) {
m_all_nodes[to]->addSuccessor(from);
else
m_all_nodes[from]->addSuccessor(to);
}
} // addSuccessor
// -----------------------------------------------------------------------------
/** Loads a quad graph from a file.
* \param filename Name of the file to load.
*/
@ -93,7 +94,7 @@ void QuadGraph::load(const std::string &filename)
// No graph file exist, assume a default loop X -> X+1
// i.e. each quad is part of the graph exactly once.
// First create an empty graph node for each quad:
for(unsigned int i=0; i<m_all_quads->getNumberOfQuads(); i++)
for(unsigned int i=0; i<QuadSet::get()->getNumberOfQuads(); i++)
m_all_nodes.push_back(new GraphNode(i, m_all_nodes.size()));
// Then set the default loop:
setDefaultSuccessors();
@ -448,7 +449,7 @@ void QuadGraph::createMesh(bool show_invisible,
ind[6*i+4] = 4*i+2;
ind[6*i+5] = 4*i+3;
i++;
} // for i=1; i<m_all_quads
} // for i=1; i<QuadSet::get()
m_mesh_buffer->append(new_v, n*4, ind, n*6);
@ -462,7 +463,7 @@ void QuadGraph::createMesh(bool show_invisible,
// Now scale the length (distance between vertix 0 and 3
// and between 1 and 2) to be 'length':
Vec3 bb_min, bb_max;
m_all_quads->getBoundingBox(&bb_min, &bb_max);
QuadSet::get()->getBoundingBox(&bb_min, &bb_max);
// Length of the lap line about 3% of the 'height'
// of the track.
const float length=(bb_max.getZ()-bb_min.getZ())*0.03f;
@ -794,7 +795,7 @@ video::ITexture *QuadGraph::makeMiniMap(const core::dimension2du &dimension,
// ---------------
scene::ICameraSceneNode *camera = irr_driver->addCameraSceneNode();
Vec3 bb_min, bb_max;
m_all_quads->getBoundingBox(&bb_min, &bb_max);
QuadSet::get()->getBoundingBox(&bb_min, &bb_max);
Vec3 center = (bb_max+bb_min)*0.5f;
float dx = bb_max.getX()-bb_min.getX();

View File

@ -56,8 +56,6 @@ private:
/** The actual graph data structure. */
std::vector<GraphNode*> m_all_nodes;
/** The set of all quads. */
QuadSet *m_all_quads;
/** For debug mode only: the node of the debug mesh. */
scene::ISceneNode *m_node;
/** For debug only: the mesh of the debug mesh. */
@ -134,7 +132,9 @@ public:
const bool reverse)
{
assert(m_quad_graph==NULL);
m_quad_graph = new QuadGraph(quad_file_name, graph_file_name, reverse);
// assignment to m_quad_graph is done in the constructor, since
// functions called from the constructor need it to be defined.
new QuadGraph(quad_file_name, graph_file_name, reverse);
} // create
// ----------------------------------------------------------------------
/** Cleans up the quad graph. It is possible that this function is called
@ -167,7 +167,7 @@ public:
// ----------------------------------------------------------------------
/** Returns the quad that belongs to a graph node. */
const Quad& getQuadOfNode(unsigned int j) const
{ return m_all_quads->getQuad(m_all_nodes[j]->getIndex()); }
{ return QuadSet::get()->getQuad(m_all_nodes[j]->getIndex()); }
// ----------------------------------------------------------------------
/** Returns the quad that belongs to a graph node. */
GraphNode& getNode(unsigned int j) const{ return *m_all_nodes[j]; }

View File

@ -25,11 +25,14 @@
#include "io/xml_node.hpp"
#include "utils/string_utils.hpp"
/** Constructor, loads the quad set from a file.
* \param filename filename to load.
QuadSet *QuadSet::m_quad_set = NULL;
/** Constructor, loads the quad set from a file. Assigns a pointer
* to this instance to m_quad_set, so that it can be accessed using get().
*/
QuadSet::QuadSet(const std::string& filename) {
load(filename);
QuadSet::QuadSet()
{
m_quad_set = this;
} // QuadSet
// -----------------------------------------------------------------------------
@ -42,6 +45,7 @@ QuadSet::~QuadSet()
delete m_all_quads[i];
}
m_all_quads.clear();
m_quad_set = NULL;
} // ~QuadSet
// -----------------------------------------------------------------------------
@ -76,7 +80,10 @@ void QuadSet::getPoint(const XMLNode *xml, const std::string &attribute_name,
} // getPoint
// -----------------------------------------------------------------------------
void QuadSet::load(const std::string &filename)
/** Loads the set of all quads from the speciified filename.
* \param filename The absolute filename to load the quad file from.
*/
void QuadSet::init(const std::string &filename)
{
m_min = Vec3( 99999, 99999, 99999);
m_max = Vec3(-99999, -99999, -99999);

View File

@ -36,30 +36,56 @@ private:
/** The 2d bounding box, used for hashing. */
Vec3 m_min;
Vec3 m_max;
/** The list of all quads. */
std::vector<Quad*> m_all_quads;
void load (const std::string &filename);
/** Pointer to the one instance of a quad set. */
static QuadSet *m_quad_set;
void getPoint(const XMLNode *xml, const std::string &attribute_name,
Vec3 *result) const;
QuadSet();
~QuadSet();
public:
static const int QUAD_NONE=-1;
QuadSet (const std::string& filename);
~QuadSet ();
void init (const std::string &filename);
void reverse_all_quads();
int getCurrentQuad(const Vec3& p, int oldQuad) const;
// ------------------------------------------------------------------------
/** Creates one instance of the quad set. */
static void create()
{
assert(m_quad_set==NULL);
m_quad_set = new QuadSet();
}
// ------------------------------------------------------------------------
/** Destroys the one instance of a quad set. */
static void destroy()
{
delete m_quad_set; m_quad_set = NULL;
}
// ------------------------------------------------------------------------
/** Static member function to access the QuadSet instance. */
static QuadSet *get() { return m_quad_set; }
// ------------------------------------------------------------------------
/** Returns the quad with a given index number. */
const Quad& getQuad(int n) const {return *(m_all_quads[n]); }
// ------------------------------------------------------------------------
/** Return the minimum and maximum coordinates of this quad set. */
void getBoundingBox(Vec3 *min, Vec3 *max)
{ *min=m_min; *max=m_max; }
// ------------------------------------------------------------------------
/** Returns the number of quads. */
unsigned int getNumberOfQuads() const
{return (unsigned int)m_all_quads.size(); }
// ------------------------------------------------------------------------
/** Returns the center of quad n. */
const Vec3& getCenterOfQuad(int n) const
{return m_all_quads[n]->getCenter(); }
// ------------------------------------------------------------------------
/** Returns the n-th. quad. */
const Quad& getQuad(int n) {return *(m_all_quads[n]); }
}; // QuadSet