From abddbea62bb8c587d52e09309bc280eb10e1a9eb Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 25 Mar 2022 12:05:56 +0100 Subject: [PATCH] Refactoring. --- src/AnimGraph/AnimGraph.h | 202 +----------------------- src/AnimGraph/AnimGraphNodes.h | 40 +++++ src/AnimGraph/AnimGraphResource.h | 253 ++++++++++++++++++++++++------ tests/AnimGraphResourceTests.cc | 20 +-- 4 files changed, 260 insertions(+), 255 deletions(-) diff --git a/src/AnimGraph/AnimGraph.h b/src/AnimGraph/AnimGraph.h index 184d615..6a34815 100644 --- a/src/AnimGraph/AnimGraph.h +++ b/src/AnimGraph/AnimGraph.h @@ -5,7 +5,8 @@ #ifndef ANIMTESTBED_ANIMGRAPH_H #define ANIMTESTBED_ANIMGRAPH_H -#include "AnimGraphResource.h" +#include "AnimGraphData.h" +#include "AnimGraphNodes.h" // // AnimGraph (Runtime) @@ -87,205 +88,6 @@ struct AnimGraph { return nullptr; } - - static AnimGraph createFromResource(const AnimGraphResource& resource) { - AnimGraph result; - - // create nodes - for (int i = 0; i < resource.m_nodes.size(); i++) { - const AnimNodeResource& node_resource = resource.m_nodes[i]; - AnimNode* node = AnimNodeFactory(node_resource.m_type_name.c_str()); - node->m_name = node_resource.m_name; - node->m_node_type_name = node_resource.m_type_name; - node->m_index = i; - result.m_nodes.push_back(node); - - assert(node_resource.m_socket_accessor != nullptr); - result.m_node_inputs.push_back(std::vector()); - std::vector& node_inputs = result.m_node_inputs.back(); - - for (int j = 0, n = node_resource.m_socket_accessor->m_inputs.size(); - j < n; - j++) { - const Socket& input_socket = - node_resource.m_socket_accessor->m_inputs[j]; - - NodeInput input; - input.m_node = nullptr; - input.m_type = input_socket.m_type; - input.m_input_name = input_socket.m_name; - - node_inputs.push_back(input); - } - } - - // Prepare graph inputs - result.m_socket_accessor = - AnimNodeAccessorFactory("BlendTree", result.m_nodes[0]); - result.m_socket_accessor->m_outputs = - resource.m_nodes[1].m_socket_accessor->m_outputs; - result.m_socket_accessor->m_inputs = - resource.m_nodes[0].m_socket_accessor->m_inputs; - - // inputs - int input_block_size = 0; - std::vector& graph_inputs = result.getGraphInputs(); - for (int i = 0; i < graph_inputs.size(); i++) { - input_block_size += sizeof(void*); - } - result.m_input_buffer = new char[input_block_size]; - memset(result.m_input_buffer, 0, input_block_size); - - int input_block_offset = 0; - for (int i = 0; i < graph_inputs.size(); i++) { - if (graph_inputs[i].m_type == SocketType::SocketTypeAnimation) { - } - graph_inputs[i].m_value.ptr = - (void*)&result.m_input_buffer[input_block_offset]; - input_block_offset += sizeof(void*); - } - - // outputs - int output_block_size = 0; - std::vector& graph_outputs = result.getGraphOutputs(); - for (int i = 0; i < graph_outputs.size(); i++) { - output_block_size += graph_outputs[i].m_type_size; - } - result.m_output_buffer = new char[output_block_size]; - memset(result.m_output_buffer, 0, output_block_size); - - int output_block_offset = 0; - for (int i = 0; i < graph_outputs.size(); i++) { - if (graph_outputs[i].m_type == SocketType::SocketTypeAnimation) { - } - graph_outputs[i].m_value.ptr = - (void*)&result.m_output_buffer[output_block_offset]; - output_block_offset += graph_outputs[i].m_type_size; - } - - // connect the nodes - for (int i = 0; i < resource.m_connections.size(); i++) { - const AnimGraphConnection& connection = resource.m_connections[i]; - std::string source_node_type = ""; - std::string target_node_type = ""; - std::string source_node_name = ""; - std::string target_node_name = ""; - AnimNode* source_node = nullptr; - AnimNode* target_node = nullptr; - NodeSocketAccessorBase* source_node_accessor = nullptr; - NodeSocketAccessorBase* target_node_accessor = nullptr; - SocketType source_type; - SocketType target_type; - size_t source_socket_index = -1; - size_t target_socket_index = -1; - - if (connection.m_source_node != nullptr) { - size_t node_index = resource.getNodeIndex(*connection.m_source_node); - if (node_index == -1) { - std::cerr << "Could not find source node index." << std::endl; - continue; - } - source_node = result.m_nodes[node_index]; - source_node_name = source_node->m_name; - source_node_type = source_node->m_node_type_name; - if (node_index == 1) { - source_node_accessor = result.m_socket_accessor; - } else { - source_node_accessor = - AnimNodeAccessorFactory(source_node_type, source_node); - } - } - - if (connection.m_target_node != nullptr) { - size_t node_index = resource.getNodeIndex(*connection.m_target_node); - if (node_index == -1) { - std::cerr << "Could not find source node index." << std::endl; - continue; - } - target_node = result.m_nodes[node_index]; - target_node_name = target_node->m_name; - target_node_type = target_node->m_node_type_name; - if (node_index == 0) { - target_node_accessor = result.m_socket_accessor; - } else { - target_node_accessor = - AnimNodeAccessorFactory(target_node_type, target_node); - } - } - - assert(source_node != nullptr); - assert(target_node != nullptr); - - // - // Map resource node sockets to graph instance node sockets - // - if (connection.m_source_socket == nullptr) { - std::cerr << "Invalid source socket for connection " << i << "." - << std::endl; - continue; - } - - if (connection.m_target_socket == nullptr) { - std::cerr << "Invalid source socket for connection " << i << "." - << std::endl; - continue; - } - - source_socket_index = source_node_accessor->GetOutputIndex( - connection.m_source_socket->m_name); - if (source_socket_index == -1) { - std::cerr << "Invalid source socket " - << connection.m_source_socket->m_name << " for node " - << connection.m_source_node->m_name << "." << std::endl; - continue; - } - const Socket* source_socket = - &source_node_accessor->m_outputs[source_socket_index]; - - target_socket_index = target_node_accessor->GetInputIndex( - connection.m_target_socket->m_name); - if (target_socket_index == -1) { - std::cerr << "Invalid target socket " - << connection.m_target_socket->m_name << " for node " - << connection.m_target_node->m_name << "." << std::endl; - continue; - } - const Socket* target_socket = - &target_node_accessor->m_inputs[target_socket_index]; - - if (source_socket->m_type != target_socket->m_type) { - std::cerr << "Cannot connect sockets: invalid types!" << std::endl; - } - - // - // Wire up outputs to inputs. - // - (*source_socket->m_value.ptr_ptr) = target_socket->m_value.ptr; - - size_t target_node_index = target_node->m_index; - - std::vector& node_inputs = - result.m_node_inputs[target_node_index]; - for (int j = 0, n = node_inputs.size(); j < n; j++) { - if (node_inputs[j].m_input_name == target_socket->m_name) { - node_inputs[j].m_node = source_node; - } - } - - if (target_node_accessor != result.m_socket_accessor) { - delete target_node_accessor; - } - - if (source_node_accessor != result.m_socket_accessor) { - delete source_node_accessor; - } - } - - result.updateOrderedNodes(); - result.reset(); - - return result; - } }; #endif //ANIMTESTBED_ANIMGRAPH_H diff --git a/src/AnimGraph/AnimGraphNodes.h b/src/AnimGraph/AnimGraphNodes.h index d21a167..c445297 100644 --- a/src/AnimGraph/AnimGraphNodes.h +++ b/src/AnimGraph/AnimGraphNodes.h @@ -205,4 +205,44 @@ struct NodeSocketAccessor : public NodeSocketAccessorBase { } }; + +static inline AnimNode* AnimNodeFactory(const std::string& name) { + AnimNode* result; + if (name == "Blend2") { + result = new Blend2Node; + } else if (name == "SpeedScale") { + result = new SpeedScaleNode; + } else if (name == "AnimSampler") { + result = new AnimSamplerNode; + } else if (name == "BlendTree") { + result = new BlendTreeNode; + } + + if (result != nullptr) { + result->m_node_type_name = name; + return result; + } + + std::cerr << "Invalid node type: " << name << std::endl; + return nullptr; +} + +static inline NodeSocketAccessorBase* AnimNodeAccessorFactory( + const std::string& node_type_name, + AnimNode* node) { + if (node_type_name == "Blend2") { + return new NodeSocketAccessor(node); + } else if (node_type_name == "SpeedScale") { + return new NodeSocketAccessor(node); + } else if (node_type_name == "AnimSampler") { + return new NodeSocketAccessor(node); + } else if (node_type_name == "BlendTree") { + return new NodeSocketAccessor(node); + } else { + std::cerr << "Invalid node type name " << node_type_name << "." + << std::endl; + } + return nullptr; +} + #endif //ANIMTESTBED_ANIMGRAPHNODES_H diff --git a/src/AnimGraph/AnimGraphResource.h b/src/AnimGraph/AnimGraphResource.h index 025dd44..d35ca15 100644 --- a/src/AnimGraph/AnimGraphResource.h +++ b/src/AnimGraph/AnimGraphResource.h @@ -15,6 +15,7 @@ #include "SyncTrack.h" #include "AnimGraphData.h" #include "AnimGraphNodes.h" +#include "AnimGraph.h" struct AnimNode; struct NodeSocketAccessorBase; @@ -27,7 +28,15 @@ struct AnimNodeResource { float m_position[2] = {0.f, 0.f}; }; - +static inline AnimNodeResource AnimNodeResourceFactory( + const std::string& node_type_name) { + AnimNodeResource result; + result.m_type_name = node_type_name; + result.m_anim_node = AnimNodeFactory(node_type_name); + result.m_socket_accessor = + AnimNodeAccessorFactory(node_type_name, result.m_anim_node); + return result; +} // // AnimGraphResource @@ -125,55 +134,207 @@ struct AnimGraphResource { return true; } -}; -static inline AnimNode* AnimNodeFactory(const std::string& name) { - AnimNode* result; - if (name == "Blend2") { - result = new Blend2Node; - } else if (name == "SpeedScale") { - result = new SpeedScaleNode; - } else if (name == "AnimSampler") { - result = new AnimSamplerNode; - } else if (name == "BlendTree") { - result = new BlendTreeNode; - } - if (result != nullptr) { - result->m_node_type_name = name; + AnimGraph createInstance() { + AnimGraph result; + + // create nodes + 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()); + node->m_name = node_resource.m_name; + node->m_node_type_name = node_resource.m_type_name; + node->m_index = i; + result.m_nodes.push_back(node); + + assert(node_resource.m_socket_accessor != nullptr); + result.m_node_inputs.push_back(std::vector()); + std::vector& node_inputs = result.m_node_inputs.back(); + + for (int j = 0, n = node_resource.m_socket_accessor->m_inputs.size(); + j < n; + j++) { + const Socket& input_socket = + node_resource.m_socket_accessor->m_inputs[j]; + + NodeInput input; + input.m_node = nullptr; + input.m_type = input_socket.m_type; + input.m_input_name = input_socket.m_name; + + node_inputs.push_back(input); + } + } + + // Prepare graph inputs + result.m_socket_accessor = + AnimNodeAccessorFactory("BlendTree", result.m_nodes[0]); + result.m_socket_accessor->m_outputs = + m_nodes[1].m_socket_accessor->m_outputs; + result.m_socket_accessor->m_inputs = + m_nodes[0].m_socket_accessor->m_inputs; + + // inputs + int input_block_size = 0; + std::vector& graph_inputs = result.getGraphInputs(); + for (int i = 0; i < graph_inputs.size(); i++) { + input_block_size += sizeof(void*); + } + result.m_input_buffer = new char[input_block_size]; + memset(result.m_input_buffer, 0, input_block_size); + + int input_block_offset = 0; + for (int i = 0; i < graph_inputs.size(); i++) { + if (graph_inputs[i].m_type == SocketType::SocketTypeAnimation) { + } + graph_inputs[i].m_value.ptr = + (void*)&result.m_input_buffer[input_block_offset]; + input_block_offset += sizeof(void*); + } + + // outputs + int output_block_size = 0; + std::vector& graph_outputs = result.getGraphOutputs(); + for (int i = 0; i < graph_outputs.size(); i++) { + output_block_size += graph_outputs[i].m_type_size; + } + result.m_output_buffer = new char[output_block_size]; + memset(result.m_output_buffer, 0, output_block_size); + + int output_block_offset = 0; + for (int i = 0; i < graph_outputs.size(); i++) { + if (graph_outputs[i].m_type == SocketType::SocketTypeAnimation) { + } + graph_outputs[i].m_value.ptr = + (void*)&result.m_output_buffer[output_block_offset]; + output_block_offset += graph_outputs[i].m_type_size; + } + + // connect the nodes + for (int i = 0; i < m_connections.size(); i++) { + const AnimGraphConnection& connection = m_connections[i]; + std::string source_node_type = ""; + std::string target_node_type = ""; + std::string source_node_name = ""; + std::string target_node_name = ""; + AnimNode* source_node = nullptr; + AnimNode* target_node = nullptr; + NodeSocketAccessorBase* source_node_accessor = nullptr; + NodeSocketAccessorBase* target_node_accessor = nullptr; + SocketType source_type; + SocketType target_type; + size_t source_socket_index = -1; + size_t target_socket_index = -1; + + if (connection.m_source_node != nullptr) { + size_t node_index = getNodeIndex(*connection.m_source_node); + if (node_index == -1) { + std::cerr << "Could not find source node index." << std::endl; + continue; + } + source_node = result.m_nodes[node_index]; + source_node_name = source_node->m_name; + source_node_type = source_node->m_node_type_name; + if (node_index == 1) { + source_node_accessor = result.m_socket_accessor; + } else { + source_node_accessor = + AnimNodeAccessorFactory(source_node_type, source_node); + } + } + + if (connection.m_target_node != nullptr) { + size_t node_index = getNodeIndex(*connection.m_target_node); + if (node_index == -1) { + std::cerr << "Could not find source node index." << std::endl; + continue; + } + target_node = result.m_nodes[node_index]; + target_node_name = target_node->m_name; + target_node_type = target_node->m_node_type_name; + if (node_index == 0) { + target_node_accessor = result.m_socket_accessor; + } else { + target_node_accessor = + AnimNodeAccessorFactory(target_node_type, target_node); + } + } + + assert(source_node != nullptr); + assert(target_node != nullptr); + + // + // Map resource node sockets to graph instance node sockets + // + if (connection.m_source_socket == nullptr) { + std::cerr << "Invalid source socket for connection " << i << "." + << std::endl; + continue; + } + + if (connection.m_target_socket == nullptr) { + std::cerr << "Invalid source socket for connection " << i << "." + << std::endl; + continue; + } + + source_socket_index = source_node_accessor->GetOutputIndex( + connection.m_source_socket->m_name); + if (source_socket_index == -1) { + std::cerr << "Invalid source socket " + << connection.m_source_socket->m_name << " for node " + << connection.m_source_node->m_name << "." << std::endl; + continue; + } + const Socket* source_socket = + &source_node_accessor->m_outputs[source_socket_index]; + + target_socket_index = target_node_accessor->GetInputIndex( + connection.m_target_socket->m_name); + if (target_socket_index == -1) { + std::cerr << "Invalid target socket " + << connection.m_target_socket->m_name << " for node " + << connection.m_target_node->m_name << "." << std::endl; + continue; + } + const Socket* target_socket = + &target_node_accessor->m_inputs[target_socket_index]; + + if (source_socket->m_type != target_socket->m_type) { + std::cerr << "Cannot connect sockets: invalid types!" << std::endl; + } + + // + // Wire up outputs to inputs. + // + (*source_socket->m_value.ptr_ptr) = target_socket->m_value.ptr; + + size_t target_node_index = target_node->m_index; + + std::vector& node_inputs = + result.m_node_inputs[target_node_index]; + for (int j = 0, n = node_inputs.size(); j < n; j++) { + if (node_inputs[j].m_input_name == target_socket->m_name) { + node_inputs[j].m_node = source_node; + } + } + + if (target_node_accessor != result.m_socket_accessor) { + delete target_node_accessor; + } + + if (source_node_accessor != result.m_socket_accessor) { + delete source_node_accessor; + } + } + + result.updateOrderedNodes(); + result.reset(); + return result; } - std::cerr << "Invalid node type: " << name << std::endl; - return nullptr; -} - -static inline NodeSocketAccessorBase* AnimNodeAccessorFactory( - const std::string& node_type_name, - AnimNode* node) { - if (node_type_name == "Blend2") { - return new NodeSocketAccessor(node); - } else if (node_type_name == "SpeedScale") { - return new NodeSocketAccessor(node); - } else if (node_type_name == "AnimSampler") { - return new NodeSocketAccessor(node); - } else if (node_type_name == "BlendTree") { - return new NodeSocketAccessor(node); - } else { - std::cerr << "Invalid node type name " << node_type_name << "." - << std::endl; - } - return nullptr; -} - -static inline AnimNodeResource AnimNodeResourceFactory( - const std::string& node_type_name) { - AnimNodeResource result; - result.m_type_name = node_type_name; - result.m_anim_node = AnimNodeFactory(node_type_name); - result.m_socket_accessor = - AnimNodeAccessorFactory(node_type_name, result.m_anim_node); - return result; -} +}; #endif //ANIMTESTBED_ANIMGRAPHRESOURCE_H diff --git a/tests/AnimGraphResourceTests.cc b/tests/AnimGraphResourceTests.cc index 793c5cf..20abbc8 100644 --- a/tests/AnimGraphResourceTests.cc +++ b/tests/AnimGraphResourceTests.cc @@ -65,7 +65,7 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") { graph_resource.saveToFile("WalkGraph.animgraph.json"); - AnimGraph graph = AnimGraph::createFromResource(graph_resource); + AnimGraph graph = graph_resource.createInstance(); REQUIRE(graph.m_nodes.size() == 5); REQUIRE(graph.m_nodes[0]->m_node_type_name == "BlendTree"); @@ -192,8 +192,8 @@ TEST_CASE("ResourceSaveLoadGraphInputs", "[AnimGraphResource]") { } WHEN("Instantiating an AnimGraph") { - AnimGraph anim_graph = - AnimGraph::createFromResource(graph_resource_loaded); + AnimGraph anim_graph = graph_resource_loaded.createInstance(); + REQUIRE( anim_graph.getOutput("GraphOutput") == anim_graph.m_output_buffer); REQUIRE( @@ -216,7 +216,7 @@ TEST_CASE("ResourceSaveLoadGraphInputs", "[AnimGraphResource]") { graph_output_node, "GraphOutput")); - AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource_origin); + AnimGraph anim_graph = graph_resource_origin.createInstance(); void* graph_anim_input_ptr = anim_graph.getInput("GraphAnimInput"); void* graph_output_ptr = anim_graph.getOutput("GraphOutput"); @@ -265,7 +265,7 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") { graph_resource.getGraphOutputNode(), "GraphFloatOutput")); - AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource); + AnimGraph anim_graph = graph_resource.createInstance(); THEN("Writing to the input pointer changes the value of the output.") { float* float_input_ptr = (float*)anim_graph.getInput("GraphFloatInput"); @@ -292,7 +292,8 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") { "Weight")); THEN("Connected float input points to the blend weight.") { - AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource); + AnimGraph anim_graph = graph_resource.createInstance(); + Blend2Node* blend2_node = dynamic_cast(anim_graph.m_nodes[blend2_node_index]); @@ -327,7 +328,8 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") { THEN( "AnimData from output gets blended and result is written to " "Output.") { - AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource); + AnimGraph anim_graph = graph_resource.createInstance(); + Blend2Node* blend2_node = dynamic_cast(anim_graph.m_nodes[blend2_node_index]); @@ -407,7 +409,7 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") { "GraphAnimOutput")); THEN("Data flow and node ordering must be correct.") { - AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource); + AnimGraph anim_graph = graph_resource.createInstance(); Blend2Node* blend2_node = dynamic_cast(anim_graph.m_nodes[blend2_node_index]); SpeedScaleNode* speed_scale_node = dynamic_cast( @@ -461,7 +463,7 @@ TEST_CASE("GraphInputOutputConnectivity", "[AnimGraphResource]") { } WHEN("Instantiating graph") { - AnimGraph anim_graph = AnimGraph::createFromResource(graph_resource); + AnimGraph anim_graph = graph_resource.createInstance(); float* blend_weight_input = reinterpret_cast(anim_graph.getInput("GraphFloatInput"));