Skip to content
Snippets Groups Projects
Commit 3ade84b5 authored by dawehr's avatar dawehr
Browse files

Added input names to nodes in rendering. Added a "Gain" block

parent 5256acbf
No related branches found
No related tags found
1 merge request!8Controller network
......@@ -121,18 +121,23 @@ void graph_compute_node(struct computation_graph *graph, int node_id) {
int export_dot(const struct computation_graph* graph, FILE* of) {
fprintf(of, "digraph G {\n"); // Header
fprintf(of, "rankdir=\"LR\"\n"); // Horizontal layout
// Draw all the nodes and their inputs
int i;
for (i = 0; i < graph->n_nodes; i++) {
struct graph_node *node = &graph->nodes[i];
// Create node
fprintf(of, "\"%s\"[label=\"", node->name);
fprintf(of, "Name: %s\n", node->name); // Node name first
fprintf(of, "\"%s\"[shape=record\nlabel=\"\n", node->name);
fprintf(of, "<f0> %s\n", node->name); // Node name is port 0
int j;
// Add parameters as labels
// Create ports for inputs
for (j = 0; j < node->type->n_inputs; j++) {
fprintf(of, "|<f%d> --\\>%s\n", j+1, node->type->input_names[j]);
}
// Create ports for parameters
for (j = 0; j < node->type->n_params; j++) {
fprintf(of, "%s=%.3f\n", node->type->param_names[j], node->param_values[j]);
fprintf(of, "|<f%d> [%s=%.3f]\n", j+1+node->type->n_inputs, node->type->param_names[j],node->param_values[j]);
}
fprintf(of, "\"]\n"); // Close label bracket
//
......@@ -140,26 +145,9 @@ int export_dot(const struct computation_graph* graph, FILE* of) {
struct graph_node* src_node = &graph->nodes[node->input_srcs[j].controller_id];
int output_id = node->input_srcs[j].controller_output;
const char* output_name = src_node->type->output_names[output_id];
fprintf(of, "\"%s\" -> \"%s\" [label=\"%s=%.3f\"]\n", src_node->name, node->name, output_name, src_node->output_values[output_id]);
}
}
/*
// Draw the links between nodes
for (i = 0; i < graph->n_nodes; i++) {
struct graph_node *node = &graph->nodes[i];
int j;
for (j = 0; j < node->n_inputs; j++) {
// Input is connected if controller ID is not -1
if (node->input_srcs[j].controller_id != -1) {
struct graph_node* src_node = &graph->nodes[node->input_srcs[j].controller_id];
int output_id = node->input_srcs[j].controller_output;
const char* output_name = src_node->output_names[output_id];
// Draws link from source controller to node input block. Also labels the value of the link
fprintf(of, "\"%s\" -> \"%s[%s]\" [label=\"%s=%.3f\"]\n", src_node->name, node->name, node->input_names[j], output_name, src_node->output_values[output_id]);
}
fprintf(of, "\"%s\" -> \"%s\":f%d [label=\"%s=%.3f\"]\n", src_node->name, node->name, j+1, output_name, src_node->output_values[output_id]);
}
}
*/
fprintf(of, "}"); // Close graph1
fprintf(of, "}"); // Close graph
return 0;
}
\ No newline at end of file
......@@ -3,7 +3,7 @@
#include "node_add.h"
#include "node_mult.h"
#include "node_constant.h"
#include "node_pow.h"
#include "node_gain.h"
int main() {
struct computation_graph *graph = create_graph();
......@@ -17,17 +17,21 @@ int main() {
graph_set_source(graph, add1_id, ADD_SUMMAND1, const1, CONST_VAL);
graph_set_source(graph, add1_id, ADD_SUMMAND2, const2, CONST_VAL);
int pow1_id = graph_add_node_pow(graph, "Pow");
graph_set_param_val(graph, pow1_id, POW_EXP, 3);
graph_set_source(graph, pow1_id, POW_BASE, add1_id, ADD_SUM);
int gain1_id = graph_add_node_gain(graph, "Gain");
graph_set_param_val(graph, gain1_id, GAIN_GAIN, 3);
graph_set_source(graph, gain1_id, GAIN_INPUT, add1_id, ADD_SUM);
graph_compute_node(graph, pow1_id);
int mult1_id = graph_add_node_mult(graph, "Mult");
graph_set_source(graph, mult1_id, MULT_MULTIPLICAND1, gain1_id, GAIN_RESULT);
graph_set_source(graph, mult1_id, MULT_MULTIPLICAND2, const1, CONST_VAL);
graph_compute_node(graph, mult1_id);
FILE* dot_fp;
dot_fp = fopen("..\\comp_graph.dot", "w");
export_dot(graph, dot_fp);
fclose(dot_fp);
printf("Sum is %f\n", graph_get_output(graph, pow1_id, POW_RESULT));
printf("Sum is %f\n", graph_get_output(graph, mult1_id, GAIN_RESULT));
fflush(stdout);
return 0;
}
#include "node_gain.h"
#include <stdlib.h>
static void scale_nodes(void *state, const double* params, const double *inputs, double *outputs) {
outputs[GAIN_RESULT] = inputs[GAIN_INPUT] * params[GAIN_GAIN];
}
static void reset(void *state) {}
static const char* const in_names[1] = {"Input"};
static const char* const out_names[1] = {"Amplified"};
static const char* const param_names[1] = {"Gain"};
const struct graph_node_type node_gain_type = {
.input_names = in_names,
.output_names = out_names,
.param_names = param_names,
.n_inputs = 1,
.n_outputs = 1,
.n_params = 1,
.execute = scale_nodes,
.reset = reset
};
int graph_add_node_gain(struct computation_graph *graph, const char* name) {
return graph_add_node(graph, name, &node_gain_type, NULL);
}
#ifndef __NODE_GAIN_H__
#define __NODE_GAIN_H__
#include "computation_graph.h"
int graph_add_node_gain(struct computation_graph *graph, const char* name);
const extern struct graph_node_type node_gain_type;
enum graph_node_pow_inputs {
GAIN_INPUT
};
enum graph_node_pow_params {
GAIN_GAIN
};
enum graph_node_gain_outputs {
GAIN_RESULT
};
#endif // __NODE_GAIN_H__
\ No newline at end of file
......@@ -14,7 +14,7 @@ enum graph_node_pow_params {
POW_EXP
};
enum graph_node_add2_outputs {
enum graph_node_pow_outputs {
POW_RESULT
};
#endif // __NODE_POW_H__
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment