RecastLayers.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <float.h>
  19. #define _USE_MATH_DEFINES
  20. #include <math.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include "Recast.h"
  25. #include "RecastAlloc.h"
  26. #include "RecastAssert.h"
  27. // Must be 255 or smaller (not 256) because layer IDs are stored as
  28. // a byte where 255 is a special value.
  29. static const int RC_MAX_LAYERS = 63;
  30. static const int RC_MAX_NEIS = 16;
  31. struct rcLayerRegion
  32. {
  33. unsigned char layers[RC_MAX_LAYERS];
  34. unsigned char neis[RC_MAX_NEIS];
  35. unsigned short ymin, ymax;
  36. unsigned char layerId; // Layer ID
  37. unsigned char nlayers; // Layer count
  38. unsigned char nneis; // Neighbour count
  39. unsigned char base; // Flag indicating if the region is the base of merged regions.
  40. };
  41. static bool contains(const unsigned char* a, const unsigned char an, const unsigned char v)
  42. {
  43. const int n = (int)an;
  44. for (int i = 0; i < n; ++i)
  45. {
  46. if (a[i] == v)
  47. return true;
  48. }
  49. return false;
  50. }
  51. static bool addUnique(unsigned char* a, unsigned char& an, int anMax, unsigned char v)
  52. {
  53. if (contains(a, an, v))
  54. return true;
  55. if ((int)an >= anMax)
  56. return false;
  57. a[an] = v;
  58. an++;
  59. return true;
  60. }
  61. inline bool overlapRange(const unsigned short amin, const unsigned short amax,
  62. const unsigned short bmin, const unsigned short bmax)
  63. {
  64. return (amin > bmax || amax < bmin) ? false : true;
  65. }
  66. struct rcLayerSweepSpan
  67. {
  68. unsigned short ns; // number samples
  69. unsigned char id; // region id
  70. unsigned char nei; // neighbour id
  71. };
  72. /// @par
  73. ///
  74. /// See the #rcConfig documentation for more information on the configuration parameters.
  75. ///
  76. /// @see rcAllocHeightfieldLayerSet, rcCompactHeightfield, rcHeightfieldLayerSet, rcConfig
  77. bool rcBuildHeightfieldLayers(rcContext* ctx, rcCompactHeightfield& chf,
  78. const int borderSize, const int walkableHeight,
  79. rcHeightfieldLayerSet& lset)
  80. {
  81. rcAssert(ctx);
  82. rcScopedTimer timer(ctx, RC_TIMER_BUILD_LAYERS);
  83. const int w = chf.width;
  84. const int h = chf.height;
  85. rcScopedDelete<unsigned char> srcReg((unsigned char*)rcAlloc(sizeof(unsigned char)*chf.spanCount, RC_ALLOC_TEMP));
  86. if (!srcReg)
  87. {
  88. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'srcReg' (%d).", chf.spanCount);
  89. return false;
  90. }
  91. memset(srcReg,0xff,sizeof(unsigned char)*chf.spanCount);
  92. const int nsweeps = chf.width;
  93. rcScopedDelete<rcLayerSweepSpan> sweeps((rcLayerSweepSpan*)rcAlloc(sizeof(rcLayerSweepSpan)*nsweeps, RC_ALLOC_TEMP));
  94. if (!sweeps)
  95. {
  96. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'sweeps' (%d).", nsweeps);
  97. return false;
  98. }
  99. // Partition walkable area into monotone regions.
  100. int prevCount[256];
  101. unsigned char regId = 0;
  102. for (int y = borderSize; y < h-borderSize; ++y)
  103. {
  104. memset(prevCount,0,sizeof(int)*regId);
  105. unsigned char sweepId = 0;
  106. for (int x = borderSize; x < w-borderSize; ++x)
  107. {
  108. const rcCompactCell& c = chf.cells[x+y*w];
  109. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  110. {
  111. const rcCompactSpan& s = chf.spans[i];
  112. if (chf.areas[i] == RC_NULL_AREA) continue;
  113. unsigned char sid = 0xff;
  114. // -x
  115. if (rcGetCon(s, 0) != RC_NOT_CONNECTED)
  116. {
  117. const int ax = x + rcGetDirOffsetX(0);
  118. const int ay = y + rcGetDirOffsetY(0);
  119. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 0);
  120. if (chf.areas[ai] != RC_NULL_AREA && srcReg[ai] != 0xff)
  121. sid = srcReg[ai];
  122. }
  123. if (sid == 0xff)
  124. {
  125. sid = sweepId++;
  126. sweeps[sid].nei = 0xff;
  127. sweeps[sid].ns = 0;
  128. }
  129. // -y
  130. if (rcGetCon(s,3) != RC_NOT_CONNECTED)
  131. {
  132. const int ax = x + rcGetDirOffsetX(3);
  133. const int ay = y + rcGetDirOffsetY(3);
  134. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 3);
  135. const unsigned char nr = srcReg[ai];
  136. if (nr != 0xff)
  137. {
  138. // Set neighbour when first valid neighbour is encoutered.
  139. if (sweeps[sid].ns == 0)
  140. sweeps[sid].nei = nr;
  141. if (sweeps[sid].nei == nr)
  142. {
  143. // Update existing neighbour
  144. sweeps[sid].ns++;
  145. prevCount[nr]++;
  146. }
  147. else
  148. {
  149. // This is hit if there is nore than one neighbour.
  150. // Invalidate the neighbour.
  151. sweeps[sid].nei = 0xff;
  152. }
  153. }
  154. }
  155. srcReg[i] = sid;
  156. }
  157. }
  158. // Create unique ID.
  159. for (int i = 0; i < sweepId; ++i)
  160. {
  161. // If the neighbour is set and there is only one continuous connection to it,
  162. // the sweep will be merged with the previous one, else new region is created.
  163. if (sweeps[i].nei != 0xff && prevCount[sweeps[i].nei] == (int)sweeps[i].ns)
  164. {
  165. sweeps[i].id = sweeps[i].nei;
  166. }
  167. else
  168. {
  169. if (regId == 255)
  170. {
  171. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Region ID overflow.");
  172. return false;
  173. }
  174. sweeps[i].id = regId++;
  175. }
  176. }
  177. // Remap local sweep ids to region ids.
  178. for (int x = borderSize; x < w-borderSize; ++x)
  179. {
  180. const rcCompactCell& c = chf.cells[x+y*w];
  181. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  182. {
  183. if (srcReg[i] != 0xff)
  184. srcReg[i] = sweeps[srcReg[i]].id;
  185. }
  186. }
  187. }
  188. // Allocate and init layer regions.
  189. const int nregs = (int)regId;
  190. rcScopedDelete<rcLayerRegion> regs((rcLayerRegion*)rcAlloc(sizeof(rcLayerRegion)*nregs, RC_ALLOC_TEMP));
  191. if (!regs)
  192. {
  193. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'regs' (%d).", nregs);
  194. return false;
  195. }
  196. memset(regs, 0, sizeof(rcLayerRegion)*nregs);
  197. for (int i = 0; i < nregs; ++i)
  198. {
  199. regs[i].layerId = 0xff;
  200. regs[i].ymin = 0xffff;
  201. regs[i].ymax = 0;
  202. }
  203. // Find region neighbours and overlapping regions.
  204. for (int y = 0; y < h; ++y)
  205. {
  206. for (int x = 0; x < w; ++x)
  207. {
  208. const rcCompactCell& c = chf.cells[x+y*w];
  209. unsigned char lregs[RC_MAX_LAYERS];
  210. int nlregs = 0;
  211. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  212. {
  213. const rcCompactSpan& s = chf.spans[i];
  214. const unsigned char ri = srcReg[i];
  215. if (ri == 0xff) continue;
  216. regs[ri].ymin = rcMin(regs[ri].ymin, s.y);
  217. regs[ri].ymax = rcMax(regs[ri].ymax, s.y);
  218. // Collect all region layers.
  219. if (nlregs < RC_MAX_LAYERS)
  220. lregs[nlregs++] = ri;
  221. // Update neighbours
  222. for (int dir = 0; dir < 4; ++dir)
  223. {
  224. if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
  225. {
  226. const int ax = x + rcGetDirOffsetX(dir);
  227. const int ay = y + rcGetDirOffsetY(dir);
  228. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
  229. const unsigned char rai = srcReg[ai];
  230. if (rai != 0xff && rai != ri)
  231. {
  232. // Don't check return value -- if we cannot add the neighbor
  233. // it will just cause a few more regions to be created, which
  234. // is fine.
  235. addUnique(regs[ri].neis, regs[ri].nneis, RC_MAX_NEIS, rai);
  236. }
  237. }
  238. }
  239. }
  240. // Update overlapping regions.
  241. for (int i = 0; i < nlregs-1; ++i)
  242. {
  243. for (int j = i+1; j < nlregs; ++j)
  244. {
  245. if (lregs[i] != lregs[j])
  246. {
  247. rcLayerRegion& ri = regs[lregs[i]];
  248. rcLayerRegion& rj = regs[lregs[j]];
  249. if (!addUnique(ri.layers, ri.nlayers, RC_MAX_LAYERS, lregs[j]) ||
  250. !addUnique(rj.layers, rj.nlayers, RC_MAX_LAYERS, lregs[i]))
  251. {
  252. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: layer overflow (too many overlapping walkable platforms). Try increasing RC_MAX_LAYERS.");
  253. return false;
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. // Create 2D layers from regions.
  261. unsigned char layerId = 0;
  262. static const int MAX_STACK = 64;
  263. unsigned char stack[MAX_STACK];
  264. int nstack = 0;
  265. for (int i = 0; i < nregs; ++i)
  266. {
  267. rcLayerRegion& root = regs[i];
  268. // Skip already visited.
  269. if (root.layerId != 0xff)
  270. continue;
  271. // Start search.
  272. root.layerId = layerId;
  273. root.base = 1;
  274. nstack = 0;
  275. stack[nstack++] = (unsigned char)i;
  276. while (nstack)
  277. {
  278. // Pop front
  279. rcLayerRegion& reg = regs[stack[0]];
  280. nstack--;
  281. for (int j = 0; j < nstack; ++j)
  282. stack[j] = stack[j+1];
  283. const int nneis = (int)reg.nneis;
  284. for (int j = 0; j < nneis; ++j)
  285. {
  286. const unsigned char nei = reg.neis[j];
  287. rcLayerRegion& regn = regs[nei];
  288. // Skip already visited.
  289. if (regn.layerId != 0xff)
  290. continue;
  291. // Skip if the neighbour is overlapping root region.
  292. if (contains(root.layers, root.nlayers, nei))
  293. continue;
  294. // Skip if the height range would become too large.
  295. const int ymin = rcMin(root.ymin, regn.ymin);
  296. const int ymax = rcMax(root.ymax, regn.ymax);
  297. if ((ymax - ymin) >= 255)
  298. continue;
  299. if (nstack < MAX_STACK)
  300. {
  301. // Deepen
  302. stack[nstack++] = (unsigned char)nei;
  303. // Mark layer id
  304. regn.layerId = layerId;
  305. // Merge current layers to root.
  306. for (int k = 0; k < regn.nlayers; ++k)
  307. {
  308. if (!addUnique(root.layers, root.nlayers, RC_MAX_LAYERS, regn.layers[k]))
  309. {
  310. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: layer overflow (too many overlapping walkable platforms). Try increasing RC_MAX_LAYERS.");
  311. return false;
  312. }
  313. }
  314. root.ymin = rcMin(root.ymin, regn.ymin);
  315. root.ymax = rcMax(root.ymax, regn.ymax);
  316. }
  317. }
  318. }
  319. layerId++;
  320. }
  321. // Merge non-overlapping regions that are close in height.
  322. const unsigned short mergeHeight = (unsigned short)walkableHeight * 4;
  323. for (int i = 0; i < nregs; ++i)
  324. {
  325. rcLayerRegion& ri = regs[i];
  326. if (!ri.base) continue;
  327. unsigned char newId = ri.layerId;
  328. for (;;)
  329. {
  330. unsigned char oldId = 0xff;
  331. for (int j = 0; j < nregs; ++j)
  332. {
  333. if (i == j) continue;
  334. rcLayerRegion& rj = regs[j];
  335. if (!rj.base) continue;
  336. // Skip if the regions are not close to each other.
  337. if (!overlapRange(ri.ymin,ri.ymax+mergeHeight, rj.ymin,rj.ymax+mergeHeight))
  338. continue;
  339. // Skip if the height range would become too large.
  340. const int ymin = rcMin(ri.ymin, rj.ymin);
  341. const int ymax = rcMax(ri.ymax, rj.ymax);
  342. if ((ymax - ymin) >= 255)
  343. continue;
  344. // Make sure that there is no overlap when merging 'ri' and 'rj'.
  345. bool overlap = false;
  346. // Iterate over all regions which have the same layerId as 'rj'
  347. for (int k = 0; k < nregs; ++k)
  348. {
  349. if (regs[k].layerId != rj.layerId)
  350. continue;
  351. // Check if region 'k' is overlapping region 'ri'
  352. // Index to 'regs' is the same as region id.
  353. if (contains(ri.layers,ri.nlayers, (unsigned char)k))
  354. {
  355. overlap = true;
  356. break;
  357. }
  358. }
  359. // Cannot merge of regions overlap.
  360. if (overlap)
  361. continue;
  362. // Can merge i and j.
  363. oldId = rj.layerId;
  364. break;
  365. }
  366. // Could not find anything to merge with, stop.
  367. if (oldId == 0xff)
  368. break;
  369. // Merge
  370. for (int j = 0; j < nregs; ++j)
  371. {
  372. rcLayerRegion& rj = regs[j];
  373. if (rj.layerId == oldId)
  374. {
  375. rj.base = 0;
  376. // Remap layerIds.
  377. rj.layerId = newId;
  378. // Add overlaid layers from 'rj' to 'ri'.
  379. for (int k = 0; k < rj.nlayers; ++k)
  380. {
  381. if (!addUnique(ri.layers, ri.nlayers, RC_MAX_LAYERS, rj.layers[k]))
  382. {
  383. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: layer overflow (too many overlapping walkable platforms). Try increasing RC_MAX_LAYERS.");
  384. return false;
  385. }
  386. }
  387. // Update height bounds.
  388. ri.ymin = rcMin(ri.ymin, rj.ymin);
  389. ri.ymax = rcMax(ri.ymax, rj.ymax);
  390. }
  391. }
  392. }
  393. }
  394. // Compact layerIds
  395. unsigned char remap[256];
  396. memset(remap, 0, 256);
  397. // Find number of unique layers.
  398. layerId = 0;
  399. for (int i = 0; i < nregs; ++i)
  400. remap[regs[i].layerId] = 1;
  401. for (int i = 0; i < 256; ++i)
  402. {
  403. if (remap[i])
  404. remap[i] = layerId++;
  405. else
  406. remap[i] = 0xff;
  407. }
  408. // Remap ids.
  409. for (int i = 0; i < nregs; ++i)
  410. regs[i].layerId = remap[regs[i].layerId];
  411. // No layers, return empty.
  412. if (layerId == 0)
  413. return true;
  414. // Create layers.
  415. rcAssert(lset.layers == 0);
  416. const int lw = w - borderSize*2;
  417. const int lh = h - borderSize*2;
  418. // Build contracted bbox for layers.
  419. float bmin[3], bmax[3];
  420. rcVcopy(bmin, chf.bmin);
  421. rcVcopy(bmax, chf.bmax);
  422. bmin[0] += borderSize*chf.cs;
  423. bmin[2] += borderSize*chf.cs;
  424. bmax[0] -= borderSize*chf.cs;
  425. bmax[2] -= borderSize*chf.cs;
  426. lset.nlayers = (int)layerId;
  427. lset.layers = (rcHeightfieldLayer*)rcAlloc(sizeof(rcHeightfieldLayer)*lset.nlayers, RC_ALLOC_PERM);
  428. if (!lset.layers)
  429. {
  430. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'layers' (%d).", lset.nlayers);
  431. return false;
  432. }
  433. memset(lset.layers, 0, sizeof(rcHeightfieldLayer)*lset.nlayers);
  434. // Store layers.
  435. for (int i = 0; i < lset.nlayers; ++i)
  436. {
  437. unsigned char curId = (unsigned char)i;
  438. rcHeightfieldLayer* layer = &lset.layers[i];
  439. const int gridSize = sizeof(unsigned char)*lw*lh;
  440. layer->heights = (unsigned char*)rcAlloc(gridSize, RC_ALLOC_PERM);
  441. if (!layer->heights)
  442. {
  443. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'heights' (%d).", gridSize);
  444. return false;
  445. }
  446. memset(layer->heights, 0xff, gridSize);
  447. layer->areas = (unsigned char*)rcAlloc(gridSize, RC_ALLOC_PERM);
  448. if (!layer->areas)
  449. {
  450. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'areas' (%d).", gridSize);
  451. return false;
  452. }
  453. memset(layer->areas, 0, gridSize);
  454. layer->cons = (unsigned char*)rcAlloc(gridSize, RC_ALLOC_PERM);
  455. if (!layer->cons)
  456. {
  457. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'cons' (%d).", gridSize);
  458. return false;
  459. }
  460. memset(layer->cons, 0, gridSize);
  461. // Find layer height bounds.
  462. int hmin = 0, hmax = 0;
  463. for (int j = 0; j < nregs; ++j)
  464. {
  465. if (regs[j].base && regs[j].layerId == curId)
  466. {
  467. hmin = (int)regs[j].ymin;
  468. hmax = (int)regs[j].ymax;
  469. }
  470. }
  471. layer->width = lw;
  472. layer->height = lh;
  473. layer->cs = chf.cs;
  474. layer->ch = chf.ch;
  475. // Adjust the bbox to fit the heightfield.
  476. rcVcopy(layer->bmin, bmin);
  477. rcVcopy(layer->bmax, bmax);
  478. layer->bmin[1] = bmin[1] + hmin*chf.ch;
  479. layer->bmax[1] = bmin[1] + hmax*chf.ch;
  480. layer->hmin = hmin;
  481. layer->hmax = hmax;
  482. // Update usable data region.
  483. layer->minx = layer->width;
  484. layer->maxx = 0;
  485. layer->miny = layer->height;
  486. layer->maxy = 0;
  487. // Copy height and area from compact heightfield.
  488. for (int y = 0; y < lh; ++y)
  489. {
  490. for (int x = 0; x < lw; ++x)
  491. {
  492. const int cx = borderSize+x;
  493. const int cy = borderSize+y;
  494. const rcCompactCell& c = chf.cells[cx+cy*w];
  495. for (int j = (int)c.index, nj = (int)(c.index+c.count); j < nj; ++j)
  496. {
  497. const rcCompactSpan& s = chf.spans[j];
  498. // Skip unassigned regions.
  499. if (srcReg[j] == 0xff)
  500. continue;
  501. // Skip of does nto belong to current layer.
  502. unsigned char lid = regs[srcReg[j]].layerId;
  503. if (lid != curId)
  504. continue;
  505. // Update data bounds.
  506. layer->minx = rcMin(layer->minx, x);
  507. layer->maxx = rcMax(layer->maxx, x);
  508. layer->miny = rcMin(layer->miny, y);
  509. layer->maxy = rcMax(layer->maxy, y);
  510. // Store height and area type.
  511. const int idx = x+y*lw;
  512. layer->heights[idx] = (unsigned char)(s.y - hmin);
  513. layer->areas[idx] = chf.areas[j];
  514. // Check connection.
  515. unsigned char portal = 0;
  516. unsigned char con = 0;
  517. for (int dir = 0; dir < 4; ++dir)
  518. {
  519. if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
  520. {
  521. const int ax = cx + rcGetDirOffsetX(dir);
  522. const int ay = cy + rcGetDirOffsetY(dir);
  523. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
  524. unsigned char alid = srcReg[ai] != 0xff ? regs[srcReg[ai]].layerId : 0xff;
  525. // Portal mask
  526. if (chf.areas[ai] != RC_NULL_AREA && lid != alid)
  527. {
  528. portal |= (unsigned char)(1<<dir);
  529. // Update height so that it matches on both sides of the portal.
  530. const rcCompactSpan& as = chf.spans[ai];
  531. if (as.y > hmin)
  532. layer->heights[idx] = rcMax(layer->heights[idx], (unsigned char)(as.y - hmin));
  533. }
  534. // Valid connection mask
  535. if (chf.areas[ai] != RC_NULL_AREA && lid == alid)
  536. {
  537. const int nx = ax - borderSize;
  538. const int ny = ay - borderSize;
  539. if (nx >= 0 && ny >= 0 && nx < lw && ny < lh)
  540. con |= (unsigned char)(1<<dir);
  541. }
  542. }
  543. }
  544. layer->cons[idx] = (portal << 4) | con;
  545. }
  546. }
  547. }
  548. if (layer->minx > layer->maxx)
  549. layer->minx = layer->maxx = 0;
  550. if (layer->miny > layer->maxy)
  551. layer->miny = layer->maxy = 0;
  552. }
  553. return true;
  554. }