Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
Signed-off-by: Dharini Dutia <[email protected]>
  • Loading branch information
quarkytale committed Dec 27, 2023
1 parent 6941c1c commit f151561
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 162 deletions.
12 changes: 8 additions & 4 deletions src/SdfEntityCreator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -807,15 +807,19 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual)
{
std::string scriptName = visualMaterial.ScriptName();

if ((scriptName.find("Gazebo/") == 0u)) {
if ((scriptName.find("Gazebo/") == 0u))
{
// \todo Add documentation page with warning
gzwarn << "Using an internal gazebo.material to parse " << scriptName
<< std::endl;
MaterialParser::MaterialValues parsed = this->dataPtr->materialParser.GetMaterialValues(scriptName);

visualMaterial.SetAmbient(parsed.ambient.value_or(visualMaterial.Ambient()));
visualMaterial.SetDiffuse(parsed.diffuse.value_or(visualMaterial.Diffuse()));
visualMaterial.SetSpecular(parsed.specular.value_or(visualMaterial.Specular()));
visualMaterial.SetAmbient
(parsed.ambient.value_or(visualMaterial.Ambient()));
visualMaterial.SetDiffuse
(parsed.diffuse.value_or(visualMaterial.Diffuse()));
visualMaterial.SetSpecular
(parsed.specular.value_or(visualMaterial.Specular()));
}
}
this->dataPtr->ecm->CreateComponent(visualEntity,
Expand Down
23 changes: 13 additions & 10 deletions src/rendering/MaterialParser/ConfigLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ void ConfigLoader::loadMaterialFiles(ConfigLoader * c)
"gazebo.material");
std::ifstream in(installedConfig, std::ios::binary);
c->parseScript(in);

}
catch(std::filesystem::filesystem_error & e)
{
Expand All @@ -60,7 +59,7 @@ ConfigLoader::~ConfigLoader()

void ConfigLoader::clearScriptList()
{
for (auto i : m_scriptList) {
for (auto & i : m_scriptList) {
delete i.second;
}
m_scriptList.clear();
Expand All @@ -77,7 +76,7 @@ ConfigNode * ConfigLoader::getConfigScript(const std::string & name)
}
}

std::map < std::string, ConfigNode * > ConfigLoader::getAllConfigScripts()
std::map<std::string, ConfigNode *> ConfigLoader::getAllConfigScripts()
{
return m_scriptList;
}
Expand Down Expand Up @@ -114,7 +113,8 @@ void ConfigLoader::_nextToken(std::ifstream & stream)
tok = TOKEN_EOF;
return;
}
while ((ch == ' ' || ch == 9) && !stream.eof()) { // Skip leading spaces / tabs
// Skip leading spaces / tabs
while ((ch == ' ' || ch == 9) && !stream.eof()) {
ch = stream.get();
}

Expand Down Expand Up @@ -145,7 +145,8 @@ void ConfigLoader::_nextToken(std::ifstream & stream)

// Text token
if (ch < 32 || ch > 122) { // Verify valid char
throw std::runtime_error("Parse Error: Invalid character, ConfigLoader::load()");
throw std::runtime_error(
"Parse Error: Invalid character, ConfigLoader::load()");
}

tokVal = "";
Expand Down Expand Up @@ -277,7 +278,8 @@ ConfigNode::ConfigNode(ConfigNode * parent, const std::string & name)
_removeSelf = true; // For proper destruction
m_lastChildFound = -1;

// Add self to parent's child list (unless this is the root node being created)
// Add self to parent's child list
// (unless this is the root node being created)
if (parent != NULL) {
m_parent->m_children.push_back(this);
_iter = --(m_parent->m_children.end());
Expand All @@ -301,7 +303,8 @@ ConfigNode::~ConfigNode()
}
}

ConfigNode * ConfigNode::addChild(const std::string & name, bool replaceExisting)
ConfigNode * ConfigNode::addChild(
const std::string & name, bool replaceExisting)
{
if (replaceExisting) {
ConfigNode * node = findChild(name, false);
Expand All @@ -318,7 +321,7 @@ ConfigNode * ConfigNode::findChild(const std::string & name, bool recursive)
int childCount = static_cast<int>(m_children.size());

if (m_lastChildFound != -1) {
// If possible, try checking the nodes neighboring the last successful search
// If possible, try checking nodes neighboring the last successful search
// (often nodes searched for in sequence, so this will std search speeds).
prevC = m_lastChildFound - 1;
if (prevC < 0) {
Expand All @@ -340,8 +343,8 @@ ConfigNode * ConfigNode::findChild(const std::string & name, bool recursive)
}
}

// If not found that way, search for the node from start to finish, avoiding the
// already searched area above.
// If not found that way, search for the node from start to finish,
// avoiding the already searched area above.
for (indx = nextC + 1; indx < childCount; ++indx) {
ConfigNode * node = m_children[indx];
if (node->m_name == name) {
Expand Down
231 changes: 120 additions & 111 deletions src/rendering/MaterialParser/ConfigLoader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,144 +19,153 @@
#ifndef RENDERING__MATERIALPARSER__CONFIGLOADER_HH_
#define RENDERING__MATERIALPARSER__CONFIGLOADER_HH_

#include <map>
#include <cassert>
#include <string>
#include <iostream>
#include <map>
#include <string>
#include <vector>

namespace gz
{
namespace sim
{
class ConfigNode;
namespace sim
{
class ConfigNode;

class ConfigLoader
{
class ConfigLoader
{
public:
static void loadMaterialFiles(ConfigLoader * c);
static void loadMaterialFiles(ConfigLoader * c);

ConfigLoader();
ConfigLoader();

~ConfigLoader();
~ConfigLoader();

std::string m_fileEnding;
std::string m_fileEnding;

// For a line like
// entity animals/dog
// {
// ...
// }
// The type is "entity" and the name is "animals/dog"
// Or if animal/dog was not there then name is ""
virtual ConfigNode * getConfigScript(const std::string & name);
// For a line like
// entity animals/dog
// {
// ...
// }
// The type is "entity" and the name is "animals/dog"
// Or if animal/dog was not there then name is ""
virtual ConfigNode * getConfigScript(const std::string & name);

virtual std::map < std::string, ConfigNode * > getAllConfigScripts();
virtual std::map<std::string, ConfigNode *> getAllConfigScripts();

virtual void parseScript(std::ifstream & stream);
virtual void parseScript(std::ifstream & stream);

protected:
float m_LoadOrder;
// like "*.object"
float m_LoadOrder;
// like "*.object"

std::map < std::string, ConfigNode * > m_scriptList;
std::map<std::string, ConfigNode *> m_scriptList;

enum Token
{
TOKEN_Text,
TOKEN_NewLine,
TOKEN_OpenBrace,
TOKEN_CloseBrace,
TOKEN_EOF,
};
enum Token
{
TOKEN_Text,
TOKEN_NewLine,
TOKEN_OpenBrace,
TOKEN_CloseBrace,
TOKEN_EOF,
};

Token tok, lastTok;
std::string tokVal, lastTokVal;
Token tok, lastTok;
std::string tokVal, lastTokVal;

void _parseNodes(std::ifstream & stream, ConfigNode * parent);
void _nextToken(std::ifstream & stream);
void _skipNewLines(std::ifstream & stream);
void _parseNodes(std::ifstream & stream, ConfigNode * parent);
void _nextToken(std::ifstream & stream);
void _skipNewLines(std::ifstream & stream);

virtual void clearScriptList();
};
virtual void clearScriptList();
};

class ConfigNode
{
class ConfigNode
{
public:
ConfigNode(ConfigNode * parent, const std::string & name = "untitled");
~ConfigNode();

inline void setName(const std::string & name)
{
this->m_name = name;
}

inline std::string & getName()
{
return m_name;
}

inline void addValue(const std::string & value)
{
m_values.push_back(value);
}

inline void clearValues()
{
m_values.clear();
}

inline std::vector < std::string > & getValues()
{
return m_values;
}

inline void getValuesInFloat(std::vector < float > & floatValues)
{
for (const auto & str : m_values) {
floatValues.push_back(std::stof(str));
}
}

inline const std::string & getValue(unsigned int index = 0)
{
assert(index < m_values.size());
return m_values[index];
}

ConfigNode * addChild(const std::string & name = "untitled", bool replaceExisting = false);
ConfigNode * findChild(const std::string & name, bool recursive = false);

inline std::vector < ConfigNode * > & getChildren()
{
return m_children;
}

inline ConfigNode * getChild(unsigned int index = 0)
{
assert(index < m_children.size());
return m_children[index];
}

void setParent(ConfigNode * newParent);

inline ConfigNode * getParent()
{
return m_parent;
}
explicit ConfigNode(ConfigNode * parent,
const std::string & name = "untitled");

~ConfigNode();

inline void setName(const std::string & name)
{
this->m_name = name;
}

inline std::string & getName()
{
return m_name;
}

inline void addValue(const std::string & value)
{
m_values.push_back(value);
}

inline void clearValues()
{
m_values.clear();
}

inline std::vector<std::string> & getValues()
{
return m_values;
}

inline void getValuesInFloat(std::vector<float> & floatValues)
{
for (const auto & str : m_values) {
floatValues.push_back(std::stof(str));
}
}

inline const std::string & getValue(unsigned int index = 0)
{
assert(index < m_values.size());
return m_values[index];
}

ConfigNode * addChild(
const std::string & name = "untitled", bool replaceExisting = false);

ConfigNode * findChild(const std::string & name, bool recursive = false);

inline std::vector<ConfigNode *> & getChildren()
{
return m_children;
}

inline ConfigNode * getChild(unsigned int index = 0)
{
assert(index < m_children.size());
return m_children[index];
}

void setParent(ConfigNode * newParent);

inline ConfigNode * getParent()
{
return m_parent;
}

private:
std::string m_name;
std::vector < std::string > m_values;
std::vector < ConfigNode * > m_children;
ConfigNode * m_parent;
std::string m_name;

std::vector<std::string> m_values;

std::vector<ConfigNode *> m_children;

ConfigNode * m_parent;

// The last child node's index found with a call to findChild()
int m_lastChildFound;

int m_lastChildFound; // The last child node's index found with a call to findChild()
std::vector<ConfigNode *> ::iterator _iter;

std::vector < ConfigNode * > ::iterator _iter;
bool _removeSelf;
};
} // namespace sim
bool _removeSelf;
};
} // namespace sim
} // namespace gz

#endif // RENDERING__MATERIALPARSER__CONFIGLOADER_HH_
Loading

0 comments on commit f151561

Please sign in to comment.