acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
box.h
1#pragma once
2
3
4#include "location.h"
5#include "dimension.h"
6
7inline ::f64 sqr(::f64 x)
8{
9 return x * x;
10}
11
12template < prototype_number NUMBER >
14{
15public:
16
17
18 location_type < NUMBER > m_origin;
19 location_type < NUMBER > m_direction;
20 dimension_type < NUMBER > m_dimension;
21
22
23 location_type < NUMBER > center()
24 {
25
26
27 location_type < NUMBER > point;
28
29
30 point = m_origin + locationd(m_dimension.cx / 2.0, m_dimension.cy / 2.0, m_dimension.cz / 2.0).rotate(m_direction);
31
32 //point.x = m_dimension.cx *cos(m_direction.z) / 2.0 - m_dimension.cy * sin(m_direction.z) / 2.0;
33 //point.y = m_dimension.cx *sin(m_direction.z) / 2.0 + m_dimension.cy * cos(m_direction.z) / 2.0;
34 //point.y = m_dimension.cy *cos(m_direction.x) / 2.0 - m_dimension.cz * sin(m_direction.x) / 2.0;
35 //point.z = m_dimension.cy *sin(m_direction.x) / 2.0 + m_dimension.cz * cos(m_direction.x) / 2.0;
36 //point.z = m_dimension.cz *cos(m_direction.y) / 2.0 - m_dimension.cx * sin(m_direction.y) / 2.0;
37 //point.x = m_dimension.cz *sin(m_direction.y) / 2.0 + m_dimension.cx * cos(m_direction.y) / 2.0;
38
39 //point += m_origin;
40
41 return point;
42
43
44 }
45
46 void rotate(location_type < NUMBER > point, location_type < NUMBER > pointRotation)
47 {
48
49 operator -=(point);
50
51
52 //locationd OriginDirection(0, acos(m_origin.z / (sqrt(m_origin.x * m_origin.x + m_origin.y * m_origin.y + m_origin.z * m_origin.z))), atan2(m_origin.y,m_origin.x));
53
54 m_direction += pointRotation;
55 m_origin = m_origin.rotate(pointRotation);
56
57 operator +=(point);
58
59
60 }
61
62
63
64
65
66 static array < ::i32_array_base > faces()
67 {
68
69 array < ::i32_array_base > a2;
70
71 // front
72 {
73
74 ::i32_array_base a;
75
76 a.add(0);
77 a.add(1);
78 a.add(3);
79
80 a2.add(a);
81
82 }
83
84 // right
85 {
86
87 ::i32_array_base a;
88
89 a.add(1);
90 a.add(5);
91 a.add(2);
92
93 a2.add(a);
94
95 }
96
97 // back
98 {
99
100 ::i32_array_base a;
101
102 a.add(5);
103 a.add(4);
104 a.add(6);
105
106 a2.add(a);
107
108 }
109
110 // left
111 {
112
113 ::i32_array_base a;
114
115 a.add(4);
116 a.add(0);
117 a.add(7);
118
119 a2.add(a);
120
121 }
122
123 // down
124 {
125
126 ::i32_array_base a;
127
128 a.add(2);
129 a.add(3);
130 a.add(6);
131
132 a2.add(a);
133
134 }
135
136 // up
137 {
138
139 ::i32_array_base a;
140
141 a.add(4);
142 a.add(5);
143 a.add(0);
144
145 a2.add(a);
146
147 }
148
149
150 return a2;
151 }
152
153
154
155 array < array < location_type < NUMBER > > > faces_vertexes()
156 {
157
158 array < location_type < NUMBER > > v = vertexes();
159 array < ::i32_array_base > f = faces();
160
161 array < array < location_type < NUMBER > > > fv;
162 array < location_type < NUMBER > > vertexes;
163
164 for (::collection::index i = 0; i < f.get_count(); i++)
165 {
166 ::i32_array_base& ia = f[i];
167 vertexes.erase_all();
168 for (::collection::index j = 0; j < ia.get_count(); j++)
169 {
170 vertexes.add(v[ia[j]]);
171 }
172 fv.add(vertexes);
173 }
174 return fv;
175
176
177 }
178
179 array < location_type < NUMBER > > vertexes()
180 {
181
182 array < location_type < NUMBER > > point;
183
184 point.set_size(8);
185
186
187 point[0] = m_origin;
188 point[1] = point[0] + location_type < NUMBER >(m_dimension.x, 0, 0).rotate(m_direction);
189 point[2] = point[0] + location_type < NUMBER >(m_dimension.x, m_dimension.y, 0).rotate(m_direction);
190 point[3] = point[0] + location_type < NUMBER >(0, m_dimension.y, 0).rotate(m_direction);
191
192 point[4] = m_origin + location_type < NUMBER >(0, 0, m_dimension.z).rotate(m_direction);
193 point[5] = point[4] + location_type < NUMBER >(m_dimension.x, 0, 0).rotate(m_direction);
194 point[6] = point[4] + location_type < NUMBER >(m_dimension.x, m_dimension.y, 0).rotate(m_direction);
195 point[7] = point[4] + location_type < NUMBER >(0, m_dimension.y, 0).rotate(m_direction);
196
197 return point;
198 }
199
200
201
202
203 location_type < NUMBER > get_nearest_vertice(location_type < NUMBER > point)
204 {
205
206 array < location_type < NUMBER > > verts = vertexes();
207
208 ::f64 dMin = sqrt(::sqr(point.x - verts[0].x) + sqr(point.y - verts[0].y) + sqr(point.z - verts[0].z));
209
210 ::f64 d;
211
212 ::collection::index iFound = 0;
213
214 for (::collection::index i = 1; i < verts.get_count(); i++)
215 {
216 d = sqrt(::sqr(point.x - verts[i].x) + sqr(point.y - verts[i].y) + sqr(point.z - verts[i].z));
217
218 if (d < dMin)
219 {
220 dMin = d;
221 iFound = i;
222 }
223 }
224
225
226 return verts[iFound];
227
228 }
229
230 ::collection::index find_nearest_vertice(location_type < NUMBER > point)
231 {
232
233 array < location_type < NUMBER > > verts = vertexes();
234
235 ::f64 dMin = sqrt(::sqr(point.x - verts[0].x) + sqr(point.y - verts[0].y) + sqr(point.z - verts[0].z));
236
237 ::f64 d;
238
239 ::collection::index iFound = 0;
240
241 for (::collection::index i = 1; i < verts.get_count(); i++)
242 {
243 d = sqrt(::sqr(point.x - verts[i].x) + sqr(point.y - verts[i].y) + sqr(point.z - verts[i].z));
244
245 if (d < dMin)
246 {
247 dMin = d;
248 iFound = i;
249 }
250 }
251
252
253 return iFound;
254
255 }
256
257
258 ::collection::index find_nearest_vertice(::i32 x, ::i32 y)
259 {
260
261 array < location_type < NUMBER > > verts = vertexes();
262
263 ::f64 dMin = sqrt(::sqr(x - verts[0].x) + sqr(y - verts[0].y));
264
265 ::f64 d;
266
267 ::collection::index iFound = 0;
268
269 for (::collection::index i = 1; i < verts.get_count(); i++)
270 {
271 d = sqrt(::sqr(x - verts[i].x) + sqr(y - verts[i].y));
272
273 if (d < dMin)
274 {
275 dMin = d;
276 iFound = i;
277 }
278 }
279
280
281 return iFound;
282
283 }
284
285
286
287 //void rotate(location_type < NUMBER > point, location_type < NUMBER > r);
288
289 box_type & operator -= (const location_type < NUMBER >& point)
290 {
291
292 m_origin -= point;
293
294 return *this;
295
296 }
297
298
299 box_type& operator += (const location_type < NUMBER >& point)
300 {
301
302 m_origin += point;
303
304 return *this;
305
306
307 }
308
309 //array < location_type < NUMBER > > vertexes();
310 //static array < ::i32_array_base > faces();
311 //array < array < location_type < NUMBER > > > faces_vertexes();
312 //location_type < NUMBER > get_nearest_vertice(location_type < NUMBER > point);
313 //index find_nearest_vertice(location_type < NUMBER > point);
314 //index find_nearest_vertice(::i32 x,::i32 y);
315
316
317
318};
319
320
Definition box.h:14