1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef _OBJECT_POS_SELECTOR_H |
---|
20 | #define _OBJECT_POS_SELECTOR_H |
---|
21 | |
---|
22 | #include<Common.h> |
---|
23 | |
---|
24 | #include<map> |
---|
25 | |
---|
26 | enum UsedPosType { USED_POS_PLUS, USED_POS_MINUS }; |
---|
27 | |
---|
28 | inline UsedPosType operator ~(UsedPosType uptype) |
---|
29 | { |
---|
30 | return uptype==USED_POS_PLUS ? USED_POS_MINUS : USED_POS_PLUS; |
---|
31 | } |
---|
32 | |
---|
33 | struct ObjectPosSelector |
---|
34 | { |
---|
35 | struct UsedPos |
---|
36 | { |
---|
37 | UsedPos(float sign_, float size_,float dist_) : sign(sign_), size(size_),dist(dist_) {} |
---|
38 | |
---|
39 | float sign; |
---|
40 | |
---|
41 | float size; // size of point |
---|
42 | float dist; // dist to central point (including central point size) |
---|
43 | }; |
---|
44 | |
---|
45 | typedef std::multimap<float,UsedPos> UsedPosList; // abs(angle)->Node |
---|
46 | |
---|
47 | ObjectPosSelector(float x,float y,float size,float dist); |
---|
48 | |
---|
49 | void AddUsedPos(float size,float angle,float dist); |
---|
50 | void InitializeAngle(); |
---|
51 | |
---|
52 | bool FirstAngle(float& angle); |
---|
53 | bool NextAngle(float& angle); |
---|
54 | bool NextUsedAngle(float& angle); |
---|
55 | |
---|
56 | bool NextPosibleAngle( float& angle ); |
---|
57 | |
---|
58 | bool CheckAngle(UsedPosList::value_type const& nextUsedPos, float sign, float angle ) const |
---|
59 | { |
---|
60 | float angle_step2 = GetAngle(nextUsedPos.second); |
---|
61 | |
---|
62 | float next_angle = nextUsedPos.first; |
---|
63 | if(nextUsedPos.second.sign * sign < 0) // last node from diff. list (-pi+alpha) |
---|
64 | next_angle = 2*M_PI-next_angle; // move to positive |
---|
65 | |
---|
66 | return fabs(angle)+angle_step2 <= next_angle; |
---|
67 | } |
---|
68 | |
---|
69 | bool CheckOriginal() const |
---|
70 | { |
---|
71 | return (m_UsedPosLists[USED_POS_PLUS].empty() || CheckAngle( *m_UsedPosLists[USED_POS_PLUS].begin(),1.0,0)) && |
---|
72 | (m_UsedPosLists[USED_POS_MINUS].empty() || CheckAngle( *m_UsedPosLists[USED_POS_MINUS].begin(),-1.0,0)); |
---|
73 | } |
---|
74 | |
---|
75 | bool IsNonBalanced() const { return m_UsedPosLists[USED_POS_PLUS].empty() != m_UsedPosLists[USED_POS_MINUS].empty(); } |
---|
76 | |
---|
77 | bool NextAngleFor( UsedPosList::value_type const& usedPos, float sign, UsedPosType uptype, float &angle ) |
---|
78 | { |
---|
79 | float angle_step = GetAngle(usedPos.second); |
---|
80 | |
---|
81 | // next possible angle |
---|
82 | angle = usedPos.first * usedPos.second.sign + angle_step * sign; |
---|
83 | |
---|
84 | UsedPosList::value_type const* nextNode = nextUsedPos(uptype); |
---|
85 | if(nextNode) |
---|
86 | { |
---|
87 | // if next node permit use selected angle, then do it |
---|
88 | if(!CheckAngle(*nextNode, sign, angle)) |
---|
89 | { |
---|
90 | m_smallStepOk[uptype] = false; |
---|
91 | return false; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | // possible more points |
---|
96 | m_smallStepOk[uptype] = true; |
---|
97 | m_smallStepAngle[uptype] = angle; |
---|
98 | m_smallStepNextUsedPos[uptype] = nextNode; |
---|
99 | |
---|
100 | return true; |
---|
101 | } |
---|
102 | |
---|
103 | bool NextSmallStepAngle( float sign, UsedPosType uptype, float &angle ) |
---|
104 | { |
---|
105 | // next possible angle |
---|
106 | angle = m_smallStepAngle[uptype] + m_anglestep * sign; |
---|
107 | |
---|
108 | if(fabs(angle) > M_PI) |
---|
109 | { |
---|
110 | m_smallStepOk[uptype] = false; |
---|
111 | return false; |
---|
112 | } |
---|
113 | |
---|
114 | if(m_smallStepNextUsedPos[uptype]) |
---|
115 | { |
---|
116 | if(fabs(angle) >= m_smallStepNextUsedPos[uptype]->first) |
---|
117 | { |
---|
118 | m_smallStepOk[uptype] = false; |
---|
119 | return false; |
---|
120 | } |
---|
121 | |
---|
122 | // if next node permit use selected angle, then do it |
---|
123 | if(!CheckAngle(*m_smallStepNextUsedPos[uptype], sign, angle)) |
---|
124 | { |
---|
125 | m_smallStepOk[uptype] = false; |
---|
126 | return false; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | // possible more points |
---|
131 | m_smallStepAngle[uptype] = angle; |
---|
132 | return true; |
---|
133 | } |
---|
134 | |
---|
135 | // next used post for m_nextUsedPos[uptype] |
---|
136 | UsedPosList::value_type const* nextUsedPos(UsedPosType uptype); |
---|
137 | |
---|
138 | // angle from used pos to next possible free pos |
---|
139 | float GetAngle(UsedPos const& usedPos) const { return acos(m_dist/(usedPos.dist+usedPos.size+m_size)); } |
---|
140 | |
---|
141 | float m_center_x; |
---|
142 | float m_center_y; |
---|
143 | float m_size; // size of object in center |
---|
144 | float m_dist; // distance for searching pos (including central object size) |
---|
145 | float m_anglestep; |
---|
146 | |
---|
147 | UsedPosList m_UsedPosLists[2]; |
---|
148 | UsedPosList::const_iterator m_nextUsedPos[2]; |
---|
149 | |
---|
150 | // field for small step from first after next used pos until next pos |
---|
151 | float m_smallStepAngle[2]; |
---|
152 | bool m_smallStepOk[2]; |
---|
153 | UsedPosList::value_type const* m_smallStepNextUsedPos[2]; |
---|
154 | }; |
---|
155 | #endif |
---|