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

deg_builder_pchanmap.cc « builder « intern « depsgraph « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f870a33fb681a1a0ad0db9ea97ce98e9aab8fcf4 (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
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2015 Blender Foundation.
 * All rights reserved.
 *
 * Original Author: Sergey Sharybin
 * Contributor(s): Joshua Leung
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/depsgraph/intern/builder/deg_builder_pchanmap.cc
 *  \ingroup depsgraph
 */

#include "intern/builder/deg_builder_pchanmap.h"

#include <stdio.h>
#include <string.h>

extern "C" {
#include "BLI_utildefines.h"
#include "BLI_ghash.h"
}

namespace DEG {

static void free_rootpchanmap_valueset(void *val)
{
	/* Just need to free the set itself - the names stored are all references. */
	GSet *values = (GSet *)val;
	BLI_gset_free(values, NULL);
}

RootPChanMap::RootPChanMap()
{
	/* Just create empty map. */
	map_ = BLI_ghash_str_new("RootPChanMap");
}

RootPChanMap::~RootPChanMap()
{
	/* Free the map, and all the value sets. */
	BLI_ghash_free(map_, NULL, free_rootpchanmap_valueset);
}

/* Debug contents of map */
void RootPChanMap::print_debug()
{
	GHashIterator it1;
	GSetIterator it2;

	printf("Root PChan Map:\n");
	GHASH_ITER(it1, map_) {
		const char *item = (const char *)BLI_ghashIterator_getKey(&it1);
		GSet *values = (GSet *)BLI_ghashIterator_getValue(&it1);

		printf("  %s : { ", item);
		GSET_ITER(it2, values) {
			const char *val = (const char *)BLI_gsetIterator_getKey(&it2);
			printf("%s, ", val);
		}
		printf("}\n");
	}
}

/* Add a mapping. */
void RootPChanMap::add_bone(const char *bone, const char *root)
{
	if (BLI_ghash_haskey(map_, bone)) {
		/* Add new entry, but only add the root if it doesn't already
		 * exist in there.
		 */
		GSet *values = (GSet *)BLI_ghash_lookup(map_, bone);
		BLI_gset_add(values, (void *)root);
	}
	else {
		/* Create new set and mapping. */
		GSet *values = BLI_gset_new(BLI_ghashutil_strhash_p,
		                            BLI_ghashutil_strcmp,
		                            "RootPChanMap Value Set");
		BLI_ghash_insert(map_, (void *)bone, (void *)values);

		/* Add new entry now. */
		BLI_gset_insert(values, (void *)root);
	}
}

/* Check if there's a common root bone between two bones. */
bool RootPChanMap::has_common_root(const char *bone1, const char *bone2)
{
	/* Ensure that both are in the map... */
	if (BLI_ghash_haskey(map_, bone1) == false) {
		//fprintf("RootPChanMap: bone1 '%s' not found (%s => %s)\n", bone1, bone1, bone2);
		//print_debug();
		return false;
	}

	if (BLI_ghash_haskey(map_, bone2) == false) {
		//fprintf("RootPChanMap: bone2 '%s' not found (%s => %s)\n", bone2, bone1, bone2);
		//print_debug();
		return false;
	}

	GSet *bone1_roots = (GSet *)BLI_ghash_lookup(map_, (void *)bone1);
	GSet *bone2_roots = (GSet *)BLI_ghash_lookup(map_, (void *)bone2);

	GSetIterator it1, it2;
	GSET_ITER(it1, bone1_roots) {
		GSET_ITER(it2, bone2_roots) {
			const char *v1 = (const char *)BLI_gsetIterator_getKey(&it1);
			const char *v2 = (const char *)BLI_gsetIterator_getKey(&it2);

			if (strcmp(v1, v2) == 0) {
				//fprintf("RootPchanMap: %s in common for %s => %s\n", v1, bone1, bone2);
				return true;
			}
		}
	}

	//fprintf("RootPChanMap: No common root found (%s => %s)\n", bone1, bone2);
	return false;
}

}  // namespace DEG