Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ForestGeneratorOutlinePositionChecker.c
Go to the documentation of this file.
2{
3 protected ref array<ref array<vector>> m_aOutlineSegments = {}; //<! points are 2D, format { X, 0, Z }
4// protected ref array<vector> m_aOutlineCentres = {};
5// protected ref array<float> m_aOutlineRadii = {}; // are you radii?
6 protected float m_fSetDistanceSq;
7
8 //------------------------------------------------------------------------------------------------
13 {
14 pos[1] = 0; // 2D conversion
15
17 foreach (int i, array<vector> segment : m_aOutlineSegments)
18 {
19 if (Math3D.PointLineSegmentDistanceSqr(pos, segment[0], segment[1]) <= distance)
20 return true;
21 }
22
23 return false;
24 }
25
26 //------------------------------------------------------------------------------------------------
30 {
31 pos[1] = 0; // 2D conversion
32
33 foreach (int i, array<vector> segment : m_aOutlineSegments)
34 {
35 if (Math3D.PointLineSegmentDistanceSqr(pos, segment[0], segment[1]) <= m_fSetDistanceSq)
36 return true;
37 }
38
39 return false;
40 }
41
42 //------------------------------------------------------------------------------------------------
44 protected static array<float> GetOutlineDiffLines(notnull array<float> oldPoints, notnull array<float> newPoints)
45 {
46 // points can only be added one by one
47 // points can be deleted in group and non-sequentially (e.g delete point 1-5-6 out of 10)
48 // points can be moved in group and non-sequentially
49
50 int oldCount = oldPoints.Count();
51 int newCount = newPoints.Count();
52
53 if (newCount > oldCount)
54 return GetOutlineDiffLines_PointAdded(oldPoints, newPoints);
55
56 if (newCount < oldCount)
57 return GetOutlineDiffLines_PointsRemoved(oldPoints, newPoints);
58
59 return GetOutlineDiffLines_PointsMoved(oldPoints, newPoints);
60 }
61
62 //------------------------------------------------------------------------------------------------
63 // wanted result:
64 // e.g points @ index 0, 2 and 3 are removed
65 // old: { x0, y0, x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6 }
66 // new: { x1, y1, x4, y4, x5, y5, x6, y6 }
67 // returns:
68 // result[0] { m1-p0-p1, m1-p2-p1, m1-p3-p1 }
69 // result[1] { m1-p1-p1, m1-p4-p1, m1-p6-p1 (because p0 removed) }
70 protected static array<float> GetOutlineDiffLines_PointsRemoved(notnull array<float> oldPoints, notnull array<float> newPoints)
71 {
72 int oldCount = oldPoints.Count();
73 int newCount = newPoints.Count();
74
75 if (oldCount < 3)
76 return {};
77
78 array<int> removedIndices = {};
79 array<int> relatedIndices = {};
80
81 // first, get removed indices and related new indices
82 int indexOld;
83 for (int i; i < newCount; i += 2) // step 2
84 {
85 bool isRelated = false;
86
87 for (; indexOld < oldCount; indexOld += 2)
88 {
89 if (newPoints[i] != oldPoints[indexOld] || newPoints[i + 1] != oldPoints[indexOld + 1])
90 {
91 removedIndices.Insert(indexOld * 0.5);
92 isRelated = true;
93 }
94 else
95 {
96 indexOld += 2;
97 break;
98 }
99 }
100
101 if (isRelated)
102 relatedIndices.Insert(i * 0.5);
103 }
104
105 if (indexOld < oldCount) // if there was more, add last and first point (if not already added)
106 {
107 if (!relatedIndices.Contains(0))
108 relatedIndices.Insert(0);
109
110 if (!relatedIndices.Contains(newCount * 0.5 - 1))
111 relatedIndices.Insert(newCount * 0.5 - 1);
112 }
113
114 array<float> tmp; // for ease of writing, otherwise linecount++++++
115
116 array<float> result = {};
117 foreach (int removedIndex : removedIndices)
118 {
119 int floatIndex = removedIndex * 2;
120 if (floatIndex == 0)
121 {
122 tmp = {
123 oldPoints[oldCount - 2], oldPoints[oldCount - 1], oldPoints[0], oldPoints[1], // old-1 to oldPoint
124 oldPoints[0], oldPoints[1], oldPoints[2], oldPoints[3], // oldPoint to old+1
125 };
126 }
127 else if (floatIndex == oldCount - 2) // -2
128 {
129 tmp = {
130 oldPoints[oldCount - 4], oldPoints[oldCount - 3], oldPoints[oldCount - 2], oldPoints[oldCount - 1], // old-1 to oldPoint
131 oldPoints[oldCount - 2], oldPoints[oldCount - 1], oldPoints[0], oldPoints[1], // oldPoint to old+1
132 };
133 }
134 else
135 {
136 tmp = {
137 oldPoints[floatIndex - 2], oldPoints[floatIndex - 1], oldPoints[floatIndex], oldPoints[floatIndex + 1], // old-1 to oldPoint
138 oldPoints[floatIndex], oldPoints[floatIndex + 1], oldPoints[floatIndex + 2], oldPoints[floatIndex + 3], // oldPoint to old+1
139 };
140 }
141
142 foreach (float value : tmp)
143 {
144 result.Insert(value);
145 }
146 }
147
148 if (newCount > 2)
149 {
150 foreach (int relatedIndex : relatedIndices)
151 {
152 int floatIndex = relatedIndex * 2;
153 if (floatIndex == 0)
154 {
155 tmp = {
156 newPoints[newCount - 2], newPoints[newCount - 1], newPoints[0], newPoints[1], // old-1 to oldPoint
157 newPoints[0], newPoints[1], newPoints[2], newPoints[3], // oldPoint to old+1
158 };
159 }
160 else if (floatIndex == newCount - 2) // -2
161 {
162 tmp = {
163 newPoints[newCount - 4], newPoints[newCount - 3], newPoints[newCount - 2], newPoints[newCount - 1], // old-1 to oldPoint
164 newPoints[newCount - 2], newPoints[newCount - 1], newPoints[0], newPoints[1], // oldPoint to old+1
165 };
166 }
167 else
168 {
169 tmp = {
170 newPoints[floatIndex - 2], newPoints[floatIndex - 1], newPoints[floatIndex], newPoints[floatIndex + 1], // old-1 to oldPoint
171 newPoints[floatIndex], newPoints[floatIndex + 1], newPoints[floatIndex + 2], newPoints[floatIndex + 3], // oldPoint to old+1
172 };
173 }
174
175 foreach (float value : tmp)
176 {
177 result.Insert(value);
178 }
179 }
180 }
181
182 return result;
183 }
184
185 //------------------------------------------------------------------------------------------------
187 protected static array<float> GetOutlineDiffLines_PointsMoved(notnull array<float> oldPoints, notnull array<float> newPoints)
188 {
189 // count is identical here
190 int count = oldPoints.Count();
191 if (count < 3)
192 return {};
193
194 int lastIndex = count - 2;
195
196 float prevOldPoint[2];
197 float currOldPoint[2];
198 float nextOldPoint[2];
199
200 float prevNewPoint[2];
201 float currNewPoint[2];
202 float nextNewPoint[2];
203
204 array<float> tmp; // for ease of writing, otherwise linecount++++++
205 array<float> result = {};
206 for (int i; i < count; i += 2) // step 2
207 {
208 if (oldPoints[i] != newPoints[i] || newPoints[i + 1] != newPoints[i + 1])
209 {
210 currOldPoint = { oldPoints[i], oldPoints[i + 1] };
211 currNewPoint = { newPoints[i], newPoints[i + 1] };
212
213 if (i == 0) // first point
214 {
215 prevOldPoint = { oldPoints[lastIndex], oldPoints[lastIndex + 1] };
216 nextOldPoint = { oldPoints[i + 2], oldPoints[i + 3] };
217
218 prevNewPoint = { newPoints[lastIndex], newPoints[lastIndex + 1] };
219 nextNewPoint = { newPoints[i + 2], newPoints[i + 3] };
220 }
221 else if (i == lastIndex) // last point
222 {
223 prevOldPoint = { oldPoints[i - 2], oldPoints[i - 1] };
224 nextOldPoint = { oldPoints[0], oldPoints[1] };
225
226 prevNewPoint = { newPoints[i - 2], oldPoints[i - 1] };
227 nextNewPoint = { newPoints[0], oldPoints[1] };
228 }
229 else // normal point
230 {
231 prevOldPoint = { oldPoints[i - 2], oldPoints[i - 1] };
232 nextOldPoint = { oldPoints[i + 2], oldPoints[i + 3] };
233
234 prevNewPoint = { newPoints[i - 2], oldPoints[i - 1] };
235 nextNewPoint = { newPoints[i + 2], newPoints[i + 3] };
236 }
237
238 tmp = {
239 prevOldPoint[0], prevOldPoint[1], currOldPoint[0], currOldPoint[1], // old-1 to oldPoint
240 nextOldPoint[0], nextOldPoint[1], currOldPoint[0], currOldPoint[1], // old+1 to oldPoint
241
242 prevNewPoint[0], prevNewPoint[1], currNewPoint[0], currNewPoint[1], // new-1 to newPoint
243 nextNewPoint[0], nextNewPoint[1], currNewPoint[0], currNewPoint[1], // new+1 to newPoint
244 };
245
246 foreach (float value : tmp)
247 {
248 result.Insert(value);
249 }
250/*
251 Print("moved {" +
252 currOldPoint[0] + ", " + currOldPoint[1] +
253 "} to {" +
254 currNewPoint[0] + ", " + currNewPoint[1] +
255 "}");
256*/
257 }
258 }
259
260 return result;
261 }
262
263 //------------------------------------------------------------------------------------------------
265 protected static array<float> GetOutlineDiffLines_PointAdded(notnull array<float> oldPoints, notnull array<float> newPoints)
266 {
267 int newCount = newPoints.Count();
268 if (newCount < 3)
269 return {};
270
271 int oldCount = oldPoints.Count();
272 int index = -1;
273 for (int i; i < oldCount; i += 2) // step 2
274 {
275 if (oldPoints[i] != newPoints[i] || oldPoints[i + 1] != newPoints[i + 1])
276 {
277 index = i;
278 break;
279 }
280 }
281
282 bool isLastPoint = false;
283 if (index == -1) // no difference found, it is then the last point
284 {
285 isLastPoint = true;
286 index = oldCount + 2;
287 }
288
289 float newPoint[2] = { newPoints[index], newPoints[index + 1] };
290
291 float prevPoint[2];
292 float nextPoint[2];
293
294 if (index == 0) // first point - cannot happen in World Editor AFAIK
295 {
296 prevPoint = { oldPoints[oldCount - 2], oldPoints[oldCount - 1] };
297 nextPoint = { oldPoints[index + 2], oldPoints[index + 3] };
298 }
299 else if (isLastPoint) // last point
300 {
301 prevPoint = { newPoints[index - 2], oldPoints[index - 1] };
302 nextPoint = { oldPoints[0], oldPoints[1] };
303 }
304 else // normal point
305 {
306 prevPoint = { oldPoints[index - 2], oldPoints[index - 1] };
307 nextPoint = { oldPoints[index + 2], oldPoints[index + 3] };
308 }
309
310 float oldPoint[2] = { // virtual middle of old segment
311 nextPoint[0] + 0.5 * (nextPoint[0] - prevPoint[0]),
312 nextPoint[1] + 0.5 * (nextPoint[1] - prevPoint[1]),
313 };
314
315 return {
316 prevPoint[0], prevPoint[1], oldPoint[0], oldPoint[1], // p-1 to oldPoint
317 nextPoint[0], nextPoint[1], oldPoint[0], oldPoint[1], // p+1 to oldPoint
318
319 prevPoint[0], prevPoint[1], newPoint[0], newPoint[1], // p-1 to newPoint
320 nextPoint[0], nextPoint[1], newPoint[0], newPoint[1], // p+1 to newPoint
321 };
322 }
323
324 //------------------------------------------------------------------------------------------------
325 // constructor
329 void SCR_ForestGeneratorOutlinePositionChecker(notnull array<float> oldPoints, notnull array<float> newPoints, float setDistance)
330 {
331 array<float> allOutlinePoints = GetOutlineDiffLines(oldPoints, newPoints);
332
333 array<vector> segment; // cannot use vector segment[2] due to { xxx[i] } below
334 for (int i, count = allOutlinePoints.Count(); i < count; i += 4) // step 4
335 {
336 segment = {
337 { allOutlinePoints[i], 0, allOutlinePoints[i + 1] },
338 { allOutlinePoints[i + 2], 0, allOutlinePoints[i + 3] },
339 };
340
341 m_aOutlineSegments.Insert(segment);
342// m_aOutlineCentres.Insert(vector.Lerp(segment[0], segment[1], 0.5));
343// m_aOutlineRadii.Insert(vector.DistanceXZ(segment[0], segment[1]) * 0.5);
344 }
345
346 m_fSetDistanceSq = setDistance * setDistance;
347 }
348}
float distance
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
void SCR_ForestGeneratorOutlinePositionChecker(notnull array< float > oldPoints, notnull array< float > newPoints, float setDistance)
static array< float > GetOutlineDiffLines_PointAdded(notnull array< float > oldPoints, notnull array< float > newPoints)
static array< float > GetOutlineDiffLines(notnull array< float > oldPoints, notnull array< float > newPoints)
static array< float > GetOutlineDiffLines_PointsRemoved(notnull array< float > oldPoints, notnull array< float > newPoints)
static array< float > GetOutlineDiffLines_PointsMoved(notnull array< float > oldPoints, notnull array< float > newPoints)