Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_astar.h « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e17cc7133519232fb76bd6921fc622a3b26e3467 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2014 Blender Foundation. All rights reserved. */

#pragma once

/** \file
 * \ingroup bli
 * \brief An implementation of the A* (AStar) algorithm to solve shortest path problem.
 */

#include "BLI_utildefines.h"

#include "BLI_bitmap.h"

#ifdef __cplusplus
extern "C" {
#endif

/* -------------------------------------------------------------------- */

typedef struct BLI_AStarGNLink {
  int nodes[2];
  float cost;

  void *custom_data;
} BLI_AStarGNLink;

typedef struct BLI_AStarGNode {
  struct ListBase neighbor_links;

  void *custom_data;
} BLI_AStarGNode;

typedef struct BLI_AStarSolution {
  /* Final 'most useful' data. */
  /** Number of steps (i.e. walked links) in path
   * (nodes num, including start and end, is steps + 1). */
  int steps;
  /** Store the path, in reversed order (from destination to source node), as indices. */
  int *prev_nodes;
  /** Indices are nodes' ones, as prev_nodes, but they map to relevant link. */
  BLI_AStarGNLink **prev_links;

  void *custom_data;

  /* Mostly runtime data. */
  BLI_bitmap *done_nodes;
  float *g_costs;
  int *g_steps;

  struct MemArena *mem; /* Memory arena. */
} BLI_AStarSolution;

typedef struct BLI_AStarGraph {
  int node_num;
  BLI_AStarGNode *nodes;

  void *custom_data;

  struct MemArena *mem; /* Memory arena. */
} BLI_AStarGraph;

/**
 * Initialize a node in A* graph.
 *
 * \param custom_data: an opaque pointer attached to this link,
 * available e.g. to cost callback function.
 */
void BLI_astar_node_init(BLI_AStarGraph *as_graph, int node_index, void *custom_data);
/**
 * Add a link between two nodes of our A* graph.
 *
 * \param cost: The 'length' of the link
 * (actual distance between two vertices or face centers e.g.).
 * \param custom_data: An opaque pointer attached to this link,
 * available e.g. to cost callback function.
 */
void BLI_astar_node_link_add(
    BLI_AStarGraph *as_graph, int node1_index, int node2_index, float cost, void *custom_data);
/**
 * \return The index of the other node of given link.
 */
int BLI_astar_node_link_other_node(BLI_AStarGNLink *lnk, int idx);

/**
 * Initialize a solution data for given A* graph. Does not compute anything!
 *
 * \param custom_data: an opaque pointer attached to this link, available e.g
 * . to cost callback function.
 *
 * \note BLI_AStarSolution stores nearly all data needed during solution compute.
 */
void BLI_astar_solution_init(BLI_AStarGraph *as_graph,
                             BLI_AStarSolution *as_solution,
                             void *custom_data);
/**
 * Clear given solution's data, but does not release its memory.
 * Avoids having to recreate/allocate a memarena in loops, e.g.
 *
 * \note This *has to be called* between each path solving.
 */
void BLI_astar_solution_clear(BLI_AStarSolution *as_solution);
/**
 * Release the memory allocated for this solution.
 */
void BLI_astar_solution_free(BLI_AStarSolution *as_solution);

/**
 * Callback computing the current cost (distance) to next node,
 * and the estimated overall cost to destination node
 * (A* expects this estimation to always be less or equal than actual shortest path
 * from next node to destination one).
 *
 * \param link: the graph link between current node and next one.
 * \param node_idx_curr: current node index.
 * \param node_idx_next: next node index.
 * \param node_idx_dst: destination node index.
 */
typedef float (*astar_f_cost)(BLI_AStarGraph *as_graph,
                              BLI_AStarSolution *as_solution,
                              BLI_AStarGNLink *link,
                              int node_idx_curr,
                              int node_idx_next,
                              int node_idx_dst);

/**
 * Initialize an A* graph. Total number of nodes must be known.
 *
 * Nodes might be e.g. vertices, faces, ... etc.
 *
 * \param custom_data: an opaque pointer attached to this link,
 * available e.g. to cost callback function.
 */
void BLI_astar_graph_init(BLI_AStarGraph *as_graph, int node_num, void *custom_data);
void BLI_astar_graph_free(BLI_AStarGraph *as_graph);
/**
 * Solve a path in given graph, using given 'cost' callback function.
 *
 * \param max_steps: maximum number of nodes the found path may have.
 * Useful in performance-critical usages.
 * If no path is found within given steps, returns false too.
 * \return true if a path was found, false otherwise.
 */
bool BLI_astar_graph_solve(BLI_AStarGraph *as_graph,
                           int node_index_src,
                           int node_index_dst,
                           astar_f_cost f_cost_cb,
                           BLI_AStarSolution *r_solution,
                           int max_steps);

#ifdef __cplusplus
}
#endif