Skip to content

Commit

Permalink
Added Yaml representation for structures
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan1248 committed Mar 24, 2016
1 parent bb272ea commit f7d4d0a
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/dev/radams/tgcreator/AppNestedStructureDepthTest.cpp
Expand Up @@ -44,17 +44,18 @@ int main(int argc, char** argv)
{
std::cout << "AppNestedStructureTest" << std::endl;

tgStructure* sub0 = new tgStructure();
tgStructure* sub0 = new tgStructure("sub0");
// sub0->setName("sub0");

tgStructure* sub1_1 = new tgStructure();
tgStructure* sub1_1 = new tgStructure("sub1_1");
//sub1_1->setName("s1_1");

sub1_1->addNode(1,2,3,"one two three");
sub1_1->addNode(4,5,6, "four five six");
sub1_1->addPair(0,1,"one two");

tgStructure* sub1_2 = new tgStructure(*sub1_1);
sub1_2->setTags(tgTags("sub1_2"));
sub1_2->move(btVector3(4,4,4));
//sub1_2->setName("s1_2");

Expand All @@ -64,6 +65,7 @@ int main(int argc, char** argv)
tgStructure* root = new tgStructure();
//root->setName("root");
root->addChild(sub0);
root->setTags(tgTags("root"));

tgStructure* sub1 = new tgStructure(*sub0);
//sub1->setName("sub1");
Expand Down
7 changes: 6 additions & 1 deletion src/tgcreator/tgStructure.cpp
Expand Up @@ -37,9 +37,14 @@ tgStructure::tgStructure() : tgTaggable()
{
}

tgStructure::tgStructure(const tgStructure& orig) : tgTaggable(),
/**
* Copy constructor
*/
tgStructure::tgStructure(const tgStructure& orig) : tgTaggable(orig.getTags()),
m_children(orig.m_children.size()), m_nodes(orig.m_nodes), m_pairs(orig.m_pairs)
{

// Copy children
for (std::size_t i = 0; i < orig.m_children.size(); ++i) {
m_children[i] = new tgStructure(*orig.m_children[i]);
}
Expand Down
19 changes: 19 additions & 0 deletions src/tgcreator/tgStructure.h
Expand Up @@ -156,6 +156,7 @@ class tgStructure : public tgTaggable
* @return os
* @todo Inlining this does no good; stream operations are slow.
*/
/*
inline std::ostream&
operator<<(std::ostream& os, const tgStructure& structure)
{
Expand All @@ -181,6 +182,24 @@ operator<<(std::ostream& os, const tgStructure& structure)
return os;
}
*/
/**
* Overload operator<<() to handle a tgStructure
* @param[in,out] os an ostream
* @param[in] tgStructure, its nodes, pairs, and children.
* @return os
* @todo Inlining this does no good; stream operations are slow.
*/
#include "tgYamlStringHelper.h"

inline std::ostream&
operator<<(std::ostream& os, const tgStructure& structure)
{
//os << asYamlElement(os, structure) << std::endl;
os << asYamlElement(structure) << std::endl;
return os;
}



#endif
162 changes: 162 additions & 0 deletions src/tgcreator/tgYamlStringHelper.h
@@ -0,0 +1,162 @@
/*
* Copyright © 2012, United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
* under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/**
* @file tgStructureInfo.h
* @brief Definition of class tgStructureInfo
* @author Ryan Adams
* $Id$
*/

#ifndef TG_YAML_STRING_HELPER_H
#define TG_YAML_STRING_HELPER_H

#include <string>
#include <ostream>
#include <sstream>

// Forward Declarations (needed due to recursion)
std::string asYamlItems(const std::vector<tgStructure*> structures, int indentLevel=0);

/**
* Represent tags as a yaml list
*/
inline std::string asYamlList(const tgTags& tags)
{
std::stringstream os;
os << "[";
for(size_t i = 0; i < tags.size(); i++) {
os << '"' << tags[i] << '"';
if(i != tags.size() - 1)
os << ", ";
}
os << "]";
return os.str();
};

inline std::string asYamlItem(const tgNode& node, int indentLevel=0)
{
std::stringstream os;
std::string indent = std::string(2 * (indentLevel), ' ');
os << indent << "- tags: " << asYamlList(node.getTags()) << std::endl;
os << indent << " xyz: [" << node.x() << ", " << node.y() << ", " << node.z() << "]" << std::endl;
return os.str();
}

inline std::string asYamlItems(const tgNodes& nodes, int indentLevel=0)
{
std::stringstream os;
std::string indent = std::string(2 * (indentLevel), ' ');

if (nodes.size() == 0) {
os << indent << "nodes: []" << std::endl;
return os.str();
}

os << indent << "nodes:" << std::endl;
for(size_t i = 0; i < nodes.size(); i++)
{
os << asYamlItem(nodes[i], indentLevel+1);
}
return os.str();
}

inline std::string asYamlItem(const tgPair& pair, int indentLevel=0)
{
std::stringstream os;
std::string indent = std::string(2 * (indentLevel), ' ');
os << indent << "- tags: " << asYamlList(pair.getTags()) << std::endl;
os << indent << " pair: ["
<< "[" << pair.getFrom().x() << ", " << pair.getFrom().y() << ", " << pair.getFrom().z() << "]" << ", "
<< "[" << pair.getTo().x() << ", " << pair.getTo().y() << ", " << pair.getTo().z() << "]"
<< "]" << std::endl;
return os.str();
}

inline std::string asYamlItems(const tgPairs& pairs, int indentLevel=0)
{
std::stringstream os;
std::string indent = std::string(2 * (indentLevel), ' ');

if (pairs.size() == 0) {
os << indent << "pairs: []" << std::endl;
return os.str();
}

os << indent << "pairs:" << std::endl;
for(size_t i = 0; i < pairs.size(); i++)
{
os << asYamlItem(pairs[i], indentLevel+1);
}
return os.str();
}

/**
* Represent a structure as an element
*/
inline std::string asYamlElement(const tgStructure& structure, int indentLevel=0)
{
std::stringstream os;
std::string indent = std::string(2 * (indentLevel), ' ');
os << indent << "structure:" << std::endl;
os << indent << " tags: " << asYamlList(structure.getTags()) << std::endl;
os << asYamlItems(structure.getNodes(), indentLevel + 1);
os << asYamlItems(structure.getPairs(), indentLevel + 1);
os << asYamlItems(structure.getChildren(), indentLevel + 1);
return os.str();
};


/**
* Represent a structure as a list item (prepended by a dash)
*/
inline std::string asYamlItem(const tgStructure& structure, int indentLevel=0)
{
std::stringstream os;
std::string indent = std::string(2 * (indentLevel), ' ');
os << indent << "- tags: " << asYamlList(structure.getTags()) << std::endl;
os << asYamlItems(structure.getNodes(), indentLevel + 1);
os << asYamlItems(structure.getPairs(), indentLevel + 1);
os << asYamlItems(structure.getChildren(), indentLevel + 1);
return os.str();
};

/**
* Represent a vector of tgStructures as items
*/
inline std::string asYamlItems(const std::vector<tgStructure*> structures, int indentLevel)
{
std::stringstream os;
std::string indent = std::string(2 * (indentLevel), ' ');
if (structures.size() == 0) {
os << indent << "structures: []" << std::endl;
return os.str();
}

os << indent << "structures:" << std::endl;
for(size_t i = 0; i < structures.size(); i++)
{
os << asYamlItem(*structures[i], indentLevel+1);
}
return os.str();
};

// ...


#endif

0 comments on commit f7d4d0a

Please sign in to comment.