aboutsummaryrefslogtreecommitdiff
path: root/NvBlast/sdk/common/NvBlastGeometry.h
blob: e83ff95e24eb881d0f6ed03ab84dd283ce076000 (plain) (blame)
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
/*
* Copyright (c) 2016-2017, NVIDIA CORPORATION.  All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto.  Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/


#ifndef NVBLASTGEOMETRY_H
#define NVBLASTGEOMETRY_H

#include "NvBlastTypes.h"
#include "NvBlastMath.h"

#include<limits>


namespace Nv {
namespace Blast{

NV_FORCE_INLINE uint32_t findNodeByPositionLinked(const float point[4],
	const uint32_t firstGraphNodeIndex, const uint32_t* familyGraphNodeIndexLinks,
	const uint32_t* adjacencyPartition, const uint32_t* adjacentNodeIndices, const uint32_t* adjacentBondIndices,
	const NvBlastBond* bonds, const float* bondHealths)
{
	uint32_t nodeIndex = firstGraphNodeIndex;
	uint32_t closestNode = nodeIndex;
	float minDist = std::numeric_limits<float>().max();

	while (!Nv::Blast::isInvalidIndex(nodeIndex))
	{
		const uint32_t startIndex = adjacencyPartition[nodeIndex];
		const uint32_t stopIndex = adjacencyPartition[nodeIndex + 1];

		for (uint32_t adjacentIndex = startIndex; adjacentIndex < stopIndex; adjacentIndex++)
		{
			const uint32_t neighbourIndex = adjacentNodeIndices[adjacentIndex];
			if (nodeIndex < neighbourIndex)
			{
				const uint32_t bondIndex = adjacentBondIndices[adjacentIndex];
				if (bondHealths[bondIndex] > 0.0f)
				{
					const NvBlastBond& bond = bonds[bondIndex];

					const float* centroid = bond.centroid;
					float d[3]; VecMath::sub(point, centroid, d);
					float dist = VecMath::dot(d, d);

					if (dist < minDist)
					{
						minDist = dist;
						float s = VecMath::dot(d, bond.normal);
						closestNode = s < 0 ? nodeIndex : neighbourIndex;
					}
				}
			}
		}
		nodeIndex = familyGraphNodeIndexLinks[nodeIndex];
	}

	return closestNode;
}


NV_FORCE_INLINE uint32_t findNodeByPosition(const float point[4],
	const uint32_t graphNodesCount, const uint32_t* graphNodeIndices,
	const uint32_t* adjacencyPartition, const uint32_t* adjacentNodeIndices, const uint32_t* adjacentBondIndices,
	const NvBlastBond* bonds, const float* bondHealths)
{
	uint32_t closestNode = graphNodesCount > 2 ? invalidIndex<uint32_t>() : graphNodeIndices[0];
	float minDist = std::numeric_limits<float>().max();

	for (uint32_t i = 0; i < graphNodesCount; i++)
	{
		const uint32_t nodeIndex = graphNodeIndices[i];
		const uint32_t startIndex = adjacencyPartition[nodeIndex];
		const uint32_t stopIndex = adjacencyPartition[nodeIndex + 1];

		for (uint32_t adjacentIndex = startIndex; adjacentIndex < stopIndex; adjacentIndex++)
		{
			const uint32_t bondIndex = adjacentBondIndices[adjacentIndex];
			if (bondHealths[bondIndex] > 0.0f)
			{
				const uint32_t neighbourIndex = adjacentNodeIndices[adjacentIndex];
				if (nodeIndex < neighbourIndex)
				{
					const NvBlastBond& bond = bonds[bondIndex];

					const float* centroid = bond.centroid;
					float d[3]; VecMath::sub(point, centroid, d);
					float dist = VecMath::dot(d, d);

					if (dist < minDist)
					{
						minDist = dist;
						float s = VecMath::dot(d, bond.normal);
						closestNode = s < 0 ? nodeIndex : neighbourIndex;
					}
				}
			}
		}
	}
	return closestNode;
}


NV_FORCE_INLINE uint32_t findNodeByPosition(const float point[4],
	const uint32_t graphNodesCount, const uint32_t* graphNodeIndices,
	const NvBlastSupportGraph& graph,
	const NvBlastBond* bonds, const float* bondHealths)
{
	return findNodeByPosition(point, graphNodesCount, graphNodeIndices, graph.adjacencyPartition, graph.adjacentNodeIndices, graph.adjacentBondIndices, bonds, bondHealths);
}

} // namespace Blast
} // namespace Nv


#endif // NVBLASTGEOMETRY_H