HOG2
MinimalSectorAbstraction.h
Go to the documentation of this file.
1 /*
2  * MinimalSectorAbstraction.h
3  *
4  * Copyright (c) 2007, Nathan Sturtevant
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the University of Alberta nor the
15  * names of its contributors may be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY NATHAN STURTEVANT ``AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifdef _MSC_VER
31 #include "stdafx.h"
32 #else
33 #include <stdint.h>
34 #endif
35 
36 #include <vector>
37 #include "Map.h"
38 #include "Graphics.h"
39 
40 #ifndef MINIMALSECTORABSTRACTION_H
41 #define MINIMALSECTORABSTRACTION_H
42 
43 const int maxNumParents = 14;
44 const int parentBits = 4;
45 //const int sectorSize = 16;
47 const int sectorOffsetBits = 4;
48 const int maxNumEdges = 7;
49 
50 // each abstract node needs 32 bits...2^16 memory fixed = 64k
51 // number of parents/regions (8 bits)
52 // initial address in memory (16 bits)
53 // total number of edges (8 bits)
54 
55 // each parent has (16 bits)
56 // sectorOffsetBits = 8 -- this is 0..16 the square in the sector
57 // edge end = 8
58 
59 // each edge has (8 bits)
60 // direction = 2
61 // parent num = 6
62 
63 // pad everything to the word boundary
64 // abstraction size info = 2*parents + edges + (4-(2*parents + edges)%4)
65 
66 struct sectorInfo {
67  uint8_t numRegions;
68  uint8_t numEdges;
69  uint16_t memoryAddress;
70 };
71 
72 struct tempEdgeData {
73  int from, to, direction;
74 };
75 
76 inline bool operator==(const tempEdgeData& x, const tempEdgeData& y)
77 {
78  return ((x.to == y.to) && (x.from == y.from) && (x.direction == y.direction));
79 }
80 
93 public:
95  void OpenGLDraw();
96  void Draw(Graphics::Display &display) const;
97  int GetSector(int x, int y);
98  int GetRegion(int x, int y);
99  void GetXYLocation(unsigned int sector, unsigned int region,
100  unsigned int &x, unsigned int &y) const;
101  void GetNeighbors(unsigned int sector, unsigned int region,
102  std::vector<tempEdgeData> &edges) const;
103  int GetAdjacentSector(unsigned int sector, int direction);
104 
106  void InitializeOptimization();
108  int GetAbstractionBytesUsed() { return sectors.size()*4+memory.size(); }
109 private:
110  void BuildAbstraction();
111  void GetEdges(std::vector<std::vector<int> > &areas,
112  int xSector, int ySector,
113  std::vector<tempEdgeData> &edges);
114 
115  void GetEdgeHelper(std::vector<int> &startRegion,
116  int startIndex, int startOffset,
117  std::vector<int> &targetRegion,
118  int tarGetIndex, int targetOffset,
119  std::vector<tempEdgeData> &edges,
120  int direction);
121  int GetSectorRegions(std::vector<int> &area,
122  int absXSector,
123  int absYSector);
124  void LabelRegion(std::vector<int> &area,
125  int x, int y, int label);
126 
128  std::vector<int> &area,
129  std::vector<tempEdgeData> &edges);
130  uint8_t GetAbstractEdge(tempEdgeData &data);
131  uint8_t GetAbstractLocation(std::vector<int> area, int value);
132  int FindParentRegion(int startLoc,
133  std::vector<int> &parents,
134  int mapXOffset, int mapYOffset);
135  double GetRegionError(int fromSector, int fromRegion,
136  int sx, int sy, double limit);
137  void MoveRegionCenter(int sector, int region);
139  void ResetAbstractCenter(int sector, int region);
140 
142  std::vector<sectorInfo> sectors;
143  std::vector<uint8_t> memory;
144  std::vector<std::vector<double> > regionError;
146  std::vector<std::vector<int> > areas;
149 };
150 
151 #endif
tempEdgeData::direction
int direction
Definition: MinimalSectorAbstraction.h:73
MinimalSectorAbstraction::GetSector
int GetSector(int x, int y)
MinimalSectorAbstraction::GetSector()
Definition: MinimalSectorAbstraction.cpp:765
tempEdgeData
Definition: MinimalSectorAbstraction.h:72
MinimalSectorAbstraction::regionError
std::vector< std::vector< double > > regionError
Definition: MinimalSectorAbstraction.h:144
MinimalSectorAbstraction::ComputePotentialMemorySavings
void ComputePotentialMemorySavings()
MinimalSectorAbstraction::ComputePotentialMemorySavings()
Definition: MinimalSectorAbstraction.cpp:1146
maxNumEdges
const int maxNumEdges
Definition: MinimalSectorAbstraction.h:48
operator==
bool operator==(const tempEdgeData &x, const tempEdgeData &y)
Definition: MinimalSectorAbstraction.h:76
MinimalSectorAbstraction::OpenGLDraw
void OpenGLDraw()
MinimalSectorAbstraction::OpenGLDraw()
Definition: MinimalSectorAbstraction.cpp:444
MinimalSectorAbstraction::StoreSectorInMemory
void StoreSectorInMemory(sectorInfo &si, std::vector< int > &area, std::vector< tempEdgeData > &edges)
MinimalSectorAbstraction::StoreSectorInMemory()
Definition: MinimalSectorAbstraction.cpp:314
MinimalSectorAbstraction
MinimalSectorAbstraction.
Definition: MinimalSectorAbstraction.h:92
MinimalSectorAbstraction::GetEdges
void GetEdges(std::vector< std::vector< int > > &areas, int xSector, int ySector, std::vector< tempEdgeData > &edges)
MinimalSectorAbstraction::GetEdges()
Definition: MinimalSectorAbstraction.cpp:132
MinimalSectorAbstraction::numXSectors
int numXSectors
Definition: MinimalSectorAbstraction.h:141
MinimalSectorAbstraction::GetAbstractLocation
uint8_t GetAbstractLocation(std::vector< int > area, int value)
MinimalSectorAbstraction::GetAbstractLocation()
Definition: MinimalSectorAbstraction.cpp:386
MinimalSectorAbstraction::numYSectors
int numYSectors
Definition: MinimalSectorAbstraction.h:141
MinimalSectorAbstraction::map
Map * map
Definition: MinimalSectorAbstraction.h:145
MinimalSectorAbstraction::InitializeOptimization
void InitializeOptimization()
MinimalSectorAbstraction::InitializeOptimization()
Definition: MinimalSectorAbstraction.cpp:962
MinimalSectorAbstraction::GetEdgeHelper
void GetEdgeHelper(std::vector< int > &startRegion, int startIndex, int startOffset, std::vector< int > &targetRegion, int tarGetIndex, int targetOffset, std::vector< tempEdgeData > &edges, int direction)
MinimalSectorAbstraction::GetEdgeHelper()
Definition: MinimalSectorAbstraction.cpp:258
MinimalSectorAbstraction::GetAbstractionBytesUsed
int GetAbstractionBytesUsed()
Definition: MinimalSectorAbstraction.h:108
MinimalSectorAbstraction::MinimalSectorAbstraction
MinimalSectorAbstraction(Map *map, int sectorSize)
MinimalSectorAbstraction::MinimalSectorAbstraction()
Definition: MinimalSectorAbstraction.cpp:52
Graphics::Display
Definition: Graphics.h:146
MinimalSectorAbstraction::FindParentRegion
int FindParentRegion(int startLoc, std::vector< int > &parents, int mapXOffset, int mapYOffset)
MinimalSectorAbstraction::FindParentRegion()
Definition: MinimalSectorAbstraction.cpp:821
sectorInfo::memoryAddress
uint16_t memoryAddress
Definition: MinimalSectorAbstraction.h:69
sectorInfo
Definition: MinimalSectorAbstraction.h:66
MinimalSectorAbstraction::GetXYLocation
void GetXYLocation(unsigned int sector, unsigned int region, unsigned int &x, unsigned int &y) const
MinimalSectorAbstraction::GetXYLocation()
Definition: MinimalSectorAbstraction.cpp:667
MinimalSectorAbstraction::PerformOneOptimizationStep
bool PerformOneOptimizationStep()
MinimalSectorAbstraction::PerformOneOptimizationStep()
Definition: MinimalSectorAbstraction.cpp:978
tempEdgeData::to
int to
Definition: MinimalSectorAbstraction.h:73
MinimalSectorAbstraction::GetRegion
int GetRegion(int x, int y)
MinimalSectorAbstraction::GetRegion()
Definition: MinimalSectorAbstraction.cpp:789
sectorInfo::numEdges
uint8_t numEdges
Definition: MinimalSectorAbstraction.h:68
MinimalSectorAbstraction::optimizationIndex
int optimizationIndex
Definition: MinimalSectorAbstraction.h:148
sectorInfo::numRegions
uint8_t numRegions
Definition: MinimalSectorAbstraction.h:67
Graphics.h
tempEdgeData::from
int from
Definition: MinimalSectorAbstraction.h:73
MinimalSectorAbstraction::LabelRegion
void LabelRegion(std::vector< int > &area, int x, int y, int label)
MinimalSectorAbstraction::LabelRegion()
Definition: MinimalSectorAbstraction.cpp:936
MinimalSectorAbstraction::areas
std::vector< std::vector< int > > areas
Definition: MinimalSectorAbstraction.h:146
MinimalSectorAbstraction::MoveRegionCenter
void MoveRegionCenter(int sector, int region)
MinimalSectorAbstraction::MoveRegionCenter()
Definition: MinimalSectorAbstraction.cpp:1048
MinimalSectorAbstraction::GetSectorRegions
int GetSectorRegions(std::vector< int > &area, int absXSector, int absYSector)
MinimalSectorAbstraction::GetSectorRegions()
Definition: MinimalSectorAbstraction.cpp:888
MinimalSectorAbstraction::sectors
std::vector< sectorInfo > sectors
Definition: MinimalSectorAbstraction.h:142
maxNumParents
const int maxNumParents
Definition: MinimalSectorAbstraction.h:43
MinimalSectorAbstraction::OptimizeRegionLocations
void OptimizeRegionLocations()
MinimalSectorAbstraction::OptimizeRegionLocations()
Definition: MinimalSectorAbstraction.cpp:1030
MinimalSectorAbstraction::memory
std::vector< uint8_t > memory
Definition: MinimalSectorAbstraction.h:143
sectorOffsetBits
const int sectorOffsetBits
extern int sectorSize;
Definition: MinimalSectorAbstraction.h:47
MinimalSectorAbstraction::Draw
void Draw(Graphics::Display &display) const
Definition: MinimalSectorAbstraction.cpp:556
MinimalSectorAbstraction::GetAbstractEdge
uint8_t GetAbstractEdge(tempEdgeData &data)
MinimalSectorAbstraction::GetAbstractEdge()
Definition: MinimalSectorAbstraction.cpp:365
MinimalSectorAbstraction::GetAdjacentSector
int GetAdjacentSector(unsigned int sector, int direction)
MinimalSectorAbstraction::GetAdjacentSector()
Definition: MinimalSectorAbstraction.cpp:735
Map.h
parentBits
const int parentBits
Definition: MinimalSectorAbstraction.h:44
MinimalSectorAbstraction::GetRegionError
double GetRegionError(int fromSector, int fromRegion, int sx, int sy, double limit)
MinimalSectorAbstraction::GetRegionError()
Definition: MinimalSectorAbstraction.cpp:1094
MinimalSectorAbstraction::ResetAbstractCenter
void ResetAbstractCenter(int sector, int region)
MinimalSectorAbstraction::ResetAbstractCenter()
Definition: MinimalSectorAbstraction.cpp:1016
MinimalSectorAbstraction::sectorSize
int sectorSize
Definition: MinimalSectorAbstraction.h:147
MinimalSectorAbstraction::GetNeighbors
void GetNeighbors(unsigned int sector, unsigned int region, std::vector< tempEdgeData > &edges) const
MinimalSectorAbstraction::GetNeighbors()
Definition: MinimalSectorAbstraction.cpp:691
MinimalSectorAbstraction::BuildAbstraction
void BuildAbstraction()
MinimalSectorAbstraction::BuildAbstraction()
Definition: MinimalSectorAbstraction.cpp:81
Map
A tile-based representation of the world.
Definition: Map.h:142