-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.hxx
310 lines (254 loc) · 8.05 KB
/
position.hxx
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// yass: Yet Another Soma Solver
// Copyright (C) 2021 Mark R. Rubin aka "thanks4opensource"
//
// This file is part of yass.
//
// The yass program 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 3 of
// the License, or (at your option) any later version.
//
// The yass program 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
// (LICENSE.txt) along with the yass program. If not, see
// <https://www.gnu.org/licenses/gpl.html>
#ifndef POSITION_H
#define POSITION_H
#include <algorithm> // DEBUG (if sorting in center())
#include <cstdint>
#if SOMA_MATRIX_ROTATION + SOMA_LAMBDA_ROTATION != 1
#error #define one and only one of SOMA_MATRIX_ROTATION or SOMA_LAMBDA_ROTATION
#endif
namespace soma {
// Position (aka point, vector, coordinate) class
// Stores 3 int8_t coordinate values accessable as x(), y(), z()
// or via operator[], returning values or references depending on
// RHS/LHS, const/non-const)
//
class Position {
public:
// constructors and destructors
constexpr Position() : _word(0) {}
constexpr
Position(
int8_t in_x,
int8_t in_y,
int8_t in_z)
: _coords{in_x, in_y, in_z, 0}
{}
// See inline, below
template<typename Type, typename Container>
static Position normalize(
Container &positions,
unsigned size );
// See inline, below
template<typename Type, typename Container>
static void center(
Container &positions,
unsigned size ,
const Position maxes ,
bool sort );
Position& operator()(
int x,
int y,
int z)
{
_coords[0] = x;
_coords[1] = y;
_coords[2] = z;
return *this;
}
Position operator=(
const Position &other)
{
_coords[0] = other.x();
_coords[1] = other.y();
_coords[2] = other.z();
return *this;
}
Position operator+=(
const Position &add)
{
_coords[0] += add.x();
_coords[1] += add.y();
_coords[2] += add.z();
return *this;
}
Position operator-=(
const Position &sub)
{
_coords[0] -= sub.x();
_coords[1] -= sub.y();
_coords[2] -= sub.z();
return *this;
}
Position operator>>=(
const unsigned right_shift)
{
_coords[0] >>= right_shift;
_coords[1] >>= right_shift;
_coords[2] >>= right_shift;
return *this;
}
#ifdef SOMA_MATRIX_ROTATION
Position operator*(
const int rotation[3][3])
const
{
Position result;
result._coords[0] = rotation[0][0] * _coords[0]
+ rotation[0][1] * _coords[1]
+ rotation[0][2] * _coords[2];
result._coords[1] = rotation[1][0] * _coords[0]
+ rotation[1][1] * _coords[1]
+ rotation[1][2] * _coords[2];
result._coords[2] = rotation[2][0] * _coords[0]
+ rotation[2][1] * _coords[1]
+ rotation[2][2] * _coords[2];
return result;
}
#endif
bool operator==(
const Position &check)
const
{
return _word == check._word;
}
bool operator!=(const Position check) const { return !(*this == check); }
bool operator<(
const Position &other)
const
{
if (z() > other.z()) return true ;
if (z() < other.z()) return false;
if (y() > other.y()) return true ;
if (y() < other.y()) return false;
if (x() < other.x()) return true ;
return false;
}
Position operator+(
const Position &add)
const
{
return Position(_coords[0] + add._coords[0],
_coords[1] + add._coords[1],
_coords[2] + add._coords[2]);
}
Position operator*(
const unsigned multiplier)
{
Position result;
result._coords[0] = _coords[0] * multiplier;
result._coords[1] = _coords[1] * multiplier;
result._coords[2] = _coords[2] * multiplier;
return result;
}
Position operator-(
const Position &other)
{
Position result;
result._coords[0] = _coords[0] - other.x();
result._coords[1] = _coords[1] - other.y();
result._coords[2] = _coords[2] - other.z();
return result;
}
int8_t operator[](int ndx)const {return _coords[ndx]; }
int8_t &operator[](int ndx) {return _coords[ndx]; }
int8_t x() const { return _coords[0]; }
int8_t y() const { return _coords[1]; }
int8_t z() const { return _coords[2]; }
int8_t &x() { return _coords[0]; }
int8_t &y() { return _coords[1]; }
int8_t &z() { return _coords[2]; }
#ifdef SOMA_MATRIX_ROTATION
Position rotate(
const int matrix[3][3])
{
return *this * matrix;
}
#endif
#ifdef SOMA_LAMBDA_ROTATION
Position rotate(
Position(*rotator)(const Position))
{
return rotator(*this);
}
#endif
private:
// instance data
union {
int8_t _coords[4];
uint32_t _word;
};
}; // class Position
// Translate a collection of Positions to (0,0,0)
// so that all x,y,z values range from 0 to some
// positive value
// Returns maximum x,y,z cordinate.
//
template<typename Type, typename Container>
Position Position::normalize(
Container &positions,
unsigned size )
{
int min_x = std::numeric_limits<int>::max(),
min_y = std::numeric_limits<int>::max(),
min_z = std::numeric_limits<int>::max(),
max_x = std::numeric_limits<int>::min(),
max_y = std::numeric_limits<int>::min(),
max_z = std::numeric_limits<int>::min();
for (unsigned ndx = 0 ; ndx < size ; ++ndx) {
Type &position = positions[ndx];
min_x = std::min(min_x, static_cast<int>(position.x()));
min_y = std::min(min_y, static_cast<int>(position.y()));
min_z = std::min(min_z, static_cast<int>(position.z()));
max_x = std::max(max_x, static_cast<int>(position.x()));
max_y = std::max(max_y, static_cast<int>(position.y()));
max_z = std::max(max_z, static_cast<int>(position.z()));
}
Position zeroer (min_x, min_y, min_z),
max_pos(max_x, max_y, max_z);
max_pos -= zeroer;
for (unsigned ndx = 0 ; ndx < size ; ++ndx)
positions[ndx] -= zeroer;
return max_pos;
} // normalize()
// Center a collection of Positions around origin.
// Collection must already be in range Position(0,0,0) to Position maxes
// (passed-in argument, typically generated by normalize(), above)
//
// if max N in x,y,z is even coords are: -N,-N+2,...-2,0,2,...,N-2,N
// " " " " ","," " odd " " : -N,-N+2,...-1, 1,...,N-2,N
// examples:
// 0,1 -> -1,1
// 0,1,2 -> -2,0,2
// 0,1,2,3 -> -3,-1,1,3
// 0,1,2,3,4 -> -4,-2,0,2,4
// 0,1,2,3,4,5 -> -5,-3,-1,1,3,5
// Therefor orthogonal distance between any two positions is 2,
// regardless of odd or even
// Can optionally sort Positions after centering
//
template<typename Type, typename Container>
void Position::center(
Container &positions,
unsigned size ,
const Position maxes ,
bool sort )
{
// formula: centered = 2 * original - N
for (unsigned ndx = 0; ndx < size; ++ndx) {
Type &position = positions[ndx];
position.x() = 2 * position.x() - maxes.x();
position.y() = 2 * position.y() - maxes.y();
position.z() = 2 * position.z() - maxes.z();
}
if (sort)
std::sort(&positions[0], &positions[size]);
} // center()
} // namespace soma
#endif // ifndef POSITION_H