Fix navmesh countour

Fix navmesh countour

(Jin Qing’s Column, Jan., 2023)

After changing some parameters of watershed region partition algorithm,
my test mesh generated an odd region shape, which caused a wrong contour.

Wrong contour:
在这里插入图片描述

Wrong navmesh:
在这里插入图片描述

There are 2 regions, the big region with red contour forms a C shape,
which is closed at one point.
The error is caused by the closing point, which should be a vertex in the simplified contour
of the small blue region but not.

In simplifyContour(), the closing point is incorrectly regarded as the edge point between 2 regions,
and is skipped, but it is accurately a vertex among 3 regions and must be a contour vertex.

To fix the problem, each vertex of the raw contour should count the regions around the vertex.
If the regions count is 3 or 4, then it must be a contour vertex of the simplified contour.

Fixed contour:
在这里插入图片描述

Code:

// Returns the regions count of the contour vertex's 4 cells.
// The contour vertex is the front-right of the current span.
// This vertex must be the simplified contour's vertex.
// The non-region(0) is count as a region.
//
// reg is 4 region IDs of 4 cells in clockwise order: 0-current, 1-dir, 2-diagonal, 3-dirp
//
// Diagonal cells are not connected, so these 4 cells have regions count 4,
//   because region a and b are separated from each other:
//   ```
//   a|b
//   -+-
//   b|a
// ```
size_t getRegionsCount(const rcRegionId reg[4])
{
	size_t diffCount = 0;
	for (size_t i = 0, j = 3; i < 4; j = i++)
	{
		if (reg[i] != reg[j])
			++diffCount;
	}
	assert(diffCount <= 4);
	assert(diffCount != 1);
	if (0 == diffCount)
		return 1;
	return diffCount;
}

void testGetRegionsCount()
{
	assert(1 == getRegionsCount4(1, 1, 1, 1));
	assert(2 == getRegionsCount4(1, 0, 0, 0));
	assert(2 == getRegionsCount4(1, 1, 0, 0));
	assert(3 == getRegionsCount4(0, 1, 2, 0));

	assert(4 == getRegionsCount4(0, 1, 2, 3));
	assert(4 == getRegionsCount4(1, 0, 1, 0));
	assert(4 == getRegionsCount4(1, 0, 2, 0));
}

test.obj: https://download.csdn.net/download/jq0123/87356403

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值