Fixed various clang-tidy messages.

RefactorUnifiedBlendTreeStateMachineHandling
Martin Felis 2023-04-15 22:07:22 +02:00
parent c659909020
commit c809fe0c8c
4 changed files with 69 additions and 97 deletions

View File

@ -44,7 +44,7 @@ AnimSamplerNode::~AnimSamplerNode() noexcept {
bool AnimSamplerNode::Init(AnimGraphContext& context) {
assert (m_animation == nullptr);
assert(m_filename.size() != 0);
assert(!m_filename.empty());
AnimGraphContext::AnimationFileMap::const_iterator animation_map_iter;
animation_map_iter = context.m_animation_map.find(m_filename);

View File

@ -13,13 +13,6 @@
struct AnimNode;
struct NodeInput {
AnimNode* m_node;
SocketType m_type = SocketType::SocketTypeUndefined;
Socket* m_node_output_socket;
std::string m_input_name;
};
enum class AnimNodeEvalState {
Undefined,
Deactivated,
@ -45,13 +38,13 @@ struct AnimNode {
AnimNodeEvalState m_state = AnimNodeEvalState::Undefined;
SyncTrack m_sync_track;
virtual ~AnimNode(){};
virtual ~AnimNode() = default;
virtual bool Init(AnimGraphContext& context) { return true; };
virtual void MarkActiveInputs(const std::vector<AnimGraphConnection>& inputs) {
for (size_t i = 0, n = inputs.size(); i < n; i++) {
AnimNode* input_node = inputs[i].m_source_node;
for (const auto & input : inputs) {
AnimNode* input_node = input.m_source_node;
if (input_node != nullptr) {
input_node->m_state = AnimNodeEvalState::Activated;
}
@ -59,10 +52,10 @@ struct AnimNode {
}
virtual void CalcSyncTrack(const std::vector<AnimGraphConnection>& inputs) {
for (size_t i = 0, n = inputs.size(); i < n; i++) {
AnimNode* input_node = inputs[i].m_source_node;
for (const auto & input : inputs) {
AnimNode* input_node = input.m_source_node;
if (input_node != nullptr
&& inputs[i].m_source_socket.m_type == SocketType::SocketTypeAnimation
&& input.m_source_socket.m_type == SocketType::SocketTypeAnimation
&& input_node->m_state != AnimNodeEvalState::Deactivated) {
m_sync_track = input_node->m_sync_track;
return;
@ -101,18 +94,18 @@ struct Blend2Node : public AnimNode {
bool m_sync_blend = false;
virtual void MarkActiveInputs(const std::vector<AnimGraphConnection>& inputs) override {
for (size_t i = 0, n = inputs.size(); i < n; i++) {
AnimNode* input_node = inputs[i].m_source_node;
for (const auto & input : inputs) {
AnimNode* input_node = input.m_source_node;
if (input_node == nullptr) {
continue;
}
if (inputs[i].m_target_socket.m_name == "Input0" && *i_blend_weight < 0.999) {
if (input.m_target_socket.m_name == "Input0" && *i_blend_weight < 0.999) {
input_node->m_state = AnimNodeEvalState::Activated;
continue;
}
if (inputs[i].m_target_socket.m_name == "Input1" && *i_blend_weight > 0.001) {
if (input.m_target_socket.m_name == "Input1" && *i_blend_weight > 0.001) {
input_node->m_state = AnimNodeEvalState::Activated;
continue;
}
@ -138,7 +131,7 @@ struct NodeDescriptor<Blend2Node> : public NodeDescriptorBase {
Socket* weight_input_socket = FindSocket("Weight", m_inputs);
assert(weight_input_socket != nullptr);
if (GetProperty<bool>("Sync") == true) {
if (GetProperty<bool>("Sync")) {
weight_input_socket->m_flags = SocketFlags::SocketFlagAffectsTime;
} else {
weight_input_socket->m_flags = SocketFlags::SocketFlagNone;
@ -217,10 +210,10 @@ struct NodeDescriptor<AnimSamplerNode> : public NodeDescriptorBase {
struct LockTranslationNode : public AnimNode {
AnimData* i_input = nullptr;
AnimData* o_output = nullptr;
int m_locked_bone_index;
bool m_lock_x;
bool m_lock_y;
bool m_lock_z;
int m_locked_bone_index = 0;
bool m_lock_x = false;
bool m_lock_y = false;
bool m_lock_z = false;
virtual void Evaluate(AnimGraphContext& context) override;
};

View File

@ -29,7 +29,7 @@ json sSocketToJson(const Socket& socket) {
result["type"] = sSocketTypeToStr(socket.m_type);
if (socket.m_type == SocketType::SocketTypeString
&& socket.m_value_string.size() > 0) {
&& !socket.m_value_string.empty()) {
result["value"] = socket.m_value_string;
} else if (socket.m_value.flag) {
if (socket.m_type == SocketType::SocketTypeBool) {
@ -123,7 +123,7 @@ Socket sJsonToSocket(const json& json_data) {
//
json sAnimGraphNodeToJson(
const AnimNodeResource& node,
int node_index,
size_t node_index,
const std::vector<AnimGraphConnectionResource>& connections) {
json result;
@ -135,17 +135,15 @@ json sAnimGraphNodeToJson(
result["position"][j] = node.m_position[j];
}
for (size_t j = 0, n = node.m_socket_accessor->m_inputs.size(); j < n; j++) {
const Socket& socket = node.m_socket_accessor->m_inputs[j];
for (const auto & socket : node.m_socket_accessor->m_inputs) {
if (socket.m_type == SocketType::SocketTypeAnimation) {
continue;
}
bool socket_connected = false;
for (size_t k = 0, m = connections.size(); k < m; k++) {
if (connections[k].source_node_index == node_index
&& connections[k].source_socket_name == socket.m_name) {
for (const auto & connection : connections) {
if (connection.source_node_index == node_index
&& connection.source_socket_name == socket.m_name) {
socket_connected = true;
break;
}
@ -156,16 +154,14 @@ json sAnimGraphNodeToJson(
}
}
for (size_t j = 0, n = node.m_socket_accessor->m_properties.size(); j < n;
j++) {
Socket& property = node.m_socket_accessor->m_properties[j];
for (auto & property : node.m_socket_accessor->m_properties) {
result["properties"][property.m_name] = sSocketToJson(property);
}
return result;
}
AnimNodeResource sAnimGraphNodeFromJson(const json& json_node, int node_index) {
AnimNodeResource sAnimGraphNodeFromJson(const json& json_node, size_t node_index) {
AnimNodeResource result;
result.m_name = json_node["name"];
@ -177,9 +173,7 @@ AnimNodeResource sAnimGraphNodeFromJson(const json& json_node, int node_index) {
result.m_socket_accessor =
AnimNodeDescriptorFactory(result.m_type_name, result.m_anim_node);
for (size_t j = 0, n = result.m_socket_accessor->m_properties.size(); j < n;
j++) {
Socket& property = result.m_socket_accessor->m_properties[j];
for (auto & property : result.m_socket_accessor->m_properties) {
property = sJsonToSocket(json_node["properties"][property.m_name]);
}
@ -205,7 +199,6 @@ AnimNodeResource sAnimGraphNodeFromJson(const json& json_node, int node_index) {
// AnimGraphConnectionResource <-> Json
//
json sAnimGraphConnectionToJson(
const AnimGraphResource& graph_resource,
const AnimGraphConnectionResource& connection) {
json result;
@ -221,7 +214,6 @@ json sAnimGraphConnectionToJson(
}
AnimGraphConnectionResource sAnimGraphConnectionFromJson(
const AnimGraphResource& graph_resource,
const json& json_node) {
AnimGraphConnectionResource connection;
@ -244,11 +236,11 @@ void AnimGraphResource::clear() {
}
void AnimGraphResource::clearNodes() {
for (size_t i = 0; i < m_nodes.size(); i++) {
delete m_nodes[i].m_socket_accessor;
m_nodes[i].m_socket_accessor = nullptr;
delete m_nodes[i].m_anim_node;
m_nodes[i].m_anim_node = nullptr;
for (auto & m_node : m_nodes) {
delete m_node.m_socket_accessor;
m_node.m_socket_accessor = nullptr;
delete m_node.m_anim_node;
m_node.m_anim_node = nullptr;
}
m_nodes.clear();
}
@ -273,7 +265,7 @@ bool AnimGraphResource::saveToFile(const char* filename) const {
for (size_t i = 0; i < m_connections.size(); i++) {
const AnimGraphConnectionResource& connection = m_connections[i];
result["connections"][i] = sAnimGraphConnectionToJson(*this, connection);
result["connections"][i] = sAnimGraphConnectionToJson(connection);
}
// Graph inputs and outputs
@ -340,22 +332,21 @@ bool AnimGraphResource::loadFromFile(const char* filename) {
// Setup graph inputs and outputs
const json& graph_outputs = json_data["nodes"][0]["inputs"];
for (size_t i = 0, n = graph_outputs.size(); i < n; i++) {
for (const auto & graph_output : graph_outputs) {
AnimNodeResource& graph_node = m_nodes[0];
graph_node.m_socket_accessor->m_inputs.push_back(
sJsonToSocket(graph_outputs[i]));
sJsonToSocket(graph_output));
}
const json& graph_inputs = json_data["nodes"][1]["outputs"];
for (size_t i = 0, n = graph_inputs.size(); i < n; i++) {
for (const auto & graph_input : graph_inputs) {
AnimNodeResource& graph_node = m_nodes[1];
graph_node.m_socket_accessor->m_outputs.push_back(
sJsonToSocket(graph_inputs[i]));
sJsonToSocket(graph_input));
}
// Load connections
for (size_t i = 0; i < json_data["connections"].size(); i++) {
const json& json_connection = json_data["connections"][i];
for (const auto & json_connection : json_data["connections"]) {
if (json_connection["type"] != "AnimGraphConnectionResource") {
std::cerr
<< "Invalid json object. Expected type 'AnimGraphConnectionResource' "
@ -365,7 +356,7 @@ bool AnimGraphResource::loadFromFile(const char* filename) {
}
AnimGraphConnectionResource connection =
sAnimGraphConnectionFromJson(*this, json_connection);
sAnimGraphConnectionFromJson(json_connection);
m_connections.push_back(connection);
}
@ -384,17 +375,16 @@ void AnimGraphResource::createInstance(AnimGraph& result) const {
void AnimGraphResource::createRuntimeNodeInstances(AnimGraph& instance) const {
for (int i = 0; i < m_nodes.size(); i++) {
const AnimNodeResource& node_resource = m_nodes[i];
AnimNode* node = AnimNodeFactory(node_resource.m_type_name.c_str());
AnimNode* node = AnimNodeFactory(node_resource.m_type_name);
node->m_name = node_resource.m_name;
node->m_node_type_name = node_resource.m_type_name;
node->m_index = i;
instance.m_nodes.push_back(node);
// runtime node connections
instance.m_node_input_connections.push_back(
std::vector<AnimGraphConnection>());
instance.m_node_output_connections.push_back(
std::vector<AnimGraphConnection>());
instance.m_node_input_connections.emplace_back();
instance.m_node_output_connections.emplace_back();
}
}
@ -451,9 +441,8 @@ void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
// connections: make source and target sockets point to the same address in the connection data storage.
// TODO: instead of every connection, only create data blocks for the source sockets and make sure every source socket gets allocated once.
int connection_data_storage_size = 0;
for (int i = 0; i < m_connections.size(); i++) {
const AnimGraphConnectionResource& connection = m_connections[i];
size_t connection_data_storage_size = 0;
for (const auto & connection : m_connections) {
const AnimNodeResource& source_node = m_nodes[connection.source_node_index];
Socket* source_socket = source_node.m_socket_accessor->GetOutputSocket(
connection.source_socket_name.c_str());
@ -470,7 +459,7 @@ void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
nullptr);
for (int i = 0; i < m_nodes.size(); i++) {
instance_node_descriptors[i] = AnimNodeDescriptorFactory(
m_nodes[i].m_type_name.c_str(),
m_nodes[i].m_type_name,
instance.m_nodes[i]);
}
@ -478,10 +467,8 @@ void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
instance_node_descriptors[1]->m_outputs =
instance.m_node_descriptor->m_outputs;
int connection_data_offset = 0;
for (int i = 0; i < m_connections.size(); i++) {
const AnimGraphConnectionResource& connection = m_connections[i];
size_t connection_data_offset = 0;
for (const auto & connection : m_connections) {
NodeDescriptorBase* source_node_descriptor =
instance_node_descriptors[connection.source_node_index];
NodeDescriptorBase* target_node_descriptor =
@ -526,16 +513,16 @@ void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
// const node inputs
//
std::vector<Socket*> const_inputs =
getConstNodeInputs(instance, instance_node_descriptors);
int const_node_inputs_buffer_size = 0;
for (int i = 0, n = const_inputs.size(); i < n; i++) {
if (const_inputs[i]->m_type == SocketType::SocketTypeString) {
getConstNodeInputs( instance_node_descriptors);
size_t const_node_inputs_buffer_size = 0;
for (auto & const_input : const_inputs) {
if (const_input->m_type == SocketType::SocketTypeString) {
// TODO: implement string const node input support
std::cerr << "Error: const inputs for strings not yet implemented!"
<< std::endl;
abort();
}
const_node_inputs_buffer_size += const_inputs[i]->m_type_size;
const_node_inputs_buffer_size += const_input->m_type_size;
}
if (const_node_inputs_buffer_size > 0) {
@ -543,18 +530,18 @@ void AnimGraphResource::prepareGraphIOData(AnimGraph& instance) const {
memset(instance.m_const_node_inputs, '\0', const_node_inputs_buffer_size);
}
int const_input_buffer_offset = 0;
for (int i = 0, n = const_inputs.size(); i < n; i++) {
Socket* const_input = const_inputs[i];
size_t const_input_buffer_offset = 0;
for (auto & i : const_inputs) {
Socket* const_input = i;
// TODO: implement string const node input support
assert(const_input->m_type != SocketType::SocketTypeString);
*const_input->m_reference.ptr_ptr =
&instance.m_const_node_inputs[const_input_buffer_offset];
memcpy (*const_input->m_reference.ptr_ptr, &const_input->m_value, const_inputs[i]->m_type_size);
memcpy (*const_input->m_reference.ptr_ptr, &const_input->m_value, i->m_type_size);
const_input_buffer_offset += const_inputs[i]->m_type_size;
const_input_buffer_offset += i->m_type_size;
}
for (int i = 0; i < m_nodes.size(); i++) {
@ -572,8 +559,7 @@ void AnimGraphResource::setRuntimeNodeProperties(AnimGraph& instance) const {
std::vector<Socket>& resource_properties =
node_resource.m_socket_accessor->m_properties;
for (size_t j = 0, n = resource_properties.size(); j < n; j++) {
const Socket& property = resource_properties[j];
for (const auto & property : resource_properties) {
const std::string& name = property.m_name;
switch (property.m_type) {
@ -618,12 +604,11 @@ void AnimGraphResource::setRuntimeNodeProperties(AnimGraph& instance) const {
}
std::vector<Socket*> AnimGraphResource::getConstNodeInputs(
AnimGraph& instance,
std::vector<NodeDescriptorBase*>& instance_node_descriptors) const {
std::vector<Socket*> result;
for (int i = 0; i < m_nodes.size(); i++) {
for (int j = 0, num_inputs = instance_node_descriptors[i]->m_inputs.size();
for (size_t i = 0; i < m_nodes.size(); i++) {
for (size_t j = 0, num_inputs = instance_node_descriptors[i]->m_inputs.size();
j < num_inputs;
j++) {
Socket& input = instance_node_descriptors[i]->m_inputs[j];

View File

@ -18,7 +18,6 @@
#include "SyncTrack.h"
struct AnimNode;
struct NodeSocketAccessorBase;
struct AnimNodeResource {
std::string m_name;
@ -34,7 +33,7 @@ static inline AnimNodeResource AnimNodeResourceFactory(
result.m_type_name = node_type_name;
result.m_anim_node = AnimNodeFactory(node_type_name);
result.m_socket_accessor =
AnimNodeDescriptorFactory(node_type_name.c_str(), result.m_anim_node);
AnimNodeDescriptorFactory(node_type_name, result.m_anim_node);
return result;
}
@ -43,9 +42,9 @@ static inline AnimNodeResource AnimNodeResourceFactory(
//
struct AnimGraphConnectionResource {
size_t source_node_index = -1;
std::string source_socket_name = "";
std::string source_socket_name;
size_t target_node_index = -1;
std::string target_socket_name = "";
std::string target_socket_name;
};
struct AnimGraphResource {
@ -54,9 +53,9 @@ struct AnimGraphResource {
std::vector<AnimGraphConnectionResource> m_connections;
~AnimGraphResource() {
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
delete m_nodes[i].m_anim_node;
delete m_nodes[i].m_socket_accessor;
for (auto & m_node : m_nodes) {
delete m_node.m_anim_node;
delete m_node.m_socket_accessor;
}
}
@ -71,9 +70,6 @@ struct AnimGraphResource {
AnimNodeResource& getGraphOutputNode() { return m_nodes[0]; }
AnimNodeResource& getGraphInputNode() { return m_nodes[1]; }
const AnimNodeResource& getGraphOutputNode() const { return m_nodes[0]; }
const AnimNodeResource& getGraphInputNode() const { return m_nodes[1]; }
size_t getNodeIndex(const AnimNodeResource& node_resource) const {
for (size_t i = 0, n = m_nodes.size(); i < n; i++) {
if (&m_nodes[i] == &node_resource) {
@ -84,7 +80,7 @@ struct AnimGraphResource {
return -1;
}
size_t addNode(AnimNodeResource node_resource) {
size_t addNode(const AnimNodeResource &node_resource) {
m_nodes.push_back(node_resource);
return m_nodes.size() - 1;
}
@ -126,9 +122,8 @@ struct AnimGraphResource {
bool isSocketConnected(
const AnimNodeResource& node,
const std::string& socket_name) {
int node_index = getNodeIndex(node);
for (size_t i = 0, n = m_connections.size(); i < n; i++) {
const AnimGraphConnectionResource& connection = m_connections[i];
size_t node_index = getNodeIndex(node);
for (const auto & connection : m_connections) {
if ((connection.source_node_index == node_index
&& connection.source_socket_name == socket_name)
|| ((connection.target_node_index == node_index)
@ -144,9 +139,8 @@ struct AnimGraphResource {
void createRuntimeNodeInstances(AnimGraph& instance) const;
void prepareGraphIOData(AnimGraph& instance) const;
void connectRuntimeNodes(AnimGraph& instance) const;
void setRuntimeNodeProperties(AnimGraph& instance) const;
std::vector<Socket*> getConstNodeInputs(AnimGraph& instance, std::vector<NodeDescriptorBase*>& instance_node_descriptors) const;
std::vector<Socket*> getConstNodeInputs(std::vector<NodeDescriptorBase*>& instance_node_descriptors) const;
};
#endif //ANIMTESTBED_ANIMGRAPHRESOURCE_H