From deff3079b87a6ef00a4775dd73b8eb7e45df53e4 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Tue, 22 Jul 2014 21:40:13 -0700 Subject: [PATCH] Update ObjectParser for boxes ObjectParser's constructor takes the tag value, which determines the type of object constructed. ObjectParser adds "near" and "far" sections for boxes. --- src/yaml/objectParser.cc | 99 ++++++++++++++++++++++++++++++++++++---- src/yaml/objectParser.hh | 22 +++++++-- src/yaml/sceneParser.cc | 3 +- 3 files changed, 110 insertions(+), 14 deletions(-) diff --git a/src/yaml/objectParser.cc b/src/yaml/objectParser.cc index b8f7366..6638924 100644 --- a/src/yaml/objectParser.cc +++ b/src/yaml/objectParser.cc @@ -12,6 +12,7 @@ #include "material.h" #include "object.h" +#include "objectBox.hh" #include "object_sphere.h" #include "yaml/objectParser.hh" #include "yaml/vectorParser.hh" @@ -23,33 +24,67 @@ using namespace charles; namespace yaml { ObjectParser::ObjectParser(Scene& scene, - ParserStack& parsers) + ParserStack& parsers, + const std::string& tag) : ScalarMappingParser(scene, parsers), - mObject(new Sphere()), + mObject(nullptr), mSection(NoSection) { + if (tag == "!Object.Box") { + mType = Type::Box; + mObject.reset(new Box()); + } else if (tag == "!Object.Sphere") { + mType = Type::Sphere; + mObject.reset(new Sphere()); + } else { + assert(false); + } GetScene().add_shape(mObject); } ObjectParser::~ObjectParser() -{ } +{ + mObject.reset(); +} void ObjectParser::HandleKeyEvent(const std::string& key) { - static const std::map sSections = { - {"color", ColorSection}, + static const std::map sCommonSections = { + {"color", ColorSection} + }; + + static const std::map sSphereSections = { {"origin", OriginSection}, {"radius", RadiusSection} }; - if (sSections.count(key) > 0) { - mSection = sSections.at(key); + static const std::map sBoxSections = { + {"near", NearSection}, + {"far", FarSection} + }; + + if (sCommonSections.count(key) > 0) { + mSection = sCommonSections.at(key); + return; } - else { - mSection = NoSection; + + if (mType == Type::Box) { + if (sBoxSections.count(key) > 0) { + mSection = sBoxSections.at(key); + } else { + mSection = NoSection; + } + } else if (mType == Type::Sphere) { + if (sSphereSections.count(key) > 0) { + mSection = sSphereSections.at(key); + } else { + mSection = NoSection; + } + } else { + assert(false); } } @@ -67,6 +102,12 @@ ObjectParser::HandleValueEvent(yaml_event_t& event) case RadiusSection: HandleRadiusEvent(event); break; + case NearSection: + HandleNearEvent(event); + break; + case FarSection: + HandleFarEvent(event); + break; default: assert(false); break; @@ -127,9 +168,47 @@ ObjectParser::HandleRadiusEvent(yaml_event_t& event) /* TODO: Clean this up. */ assert(false); } - mObject->set_radius(radius); + std::dynamic_pointer_cast(mObject)->set_radius(radius); mSection = NoSection; SetShouldExpectKey(true); } + +void +ObjectParser::HandleNearEvent(yaml_event_t& event) +{ + if (event.type != YAML_SEQUENCE_START_EVENT) { + /* TODO: Clean this up. */ + assert(event.type != YAML_SEQUENCE_START_EVENT); + return; + } + + auto onDone = [this](Vector3 near) { + std::dynamic_pointer_cast(mObject)->SetNear(near); + mSection = NoSection; + SetShouldExpectKey(true); + }; + + GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone)); +} + + +void +ObjectParser::HandleFarEvent(yaml_event_t& event) +{ + if (event.type != YAML_SEQUENCE_START_EVENT) { + /* TODO: Clean this up. */ + assert(event.type != YAML_SEQUENCE_START_EVENT); + return; + } + + auto onDone = [this](Vector3 far) { + std::dynamic_pointer_cast(mObject)->SetFar(far); + mSection = NoSection; + SetShouldExpectKey(true); + }; + + GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone)); +} + } /* namespace yaml */ diff --git a/src/yaml/objectParser.hh b/src/yaml/objectParser.hh index f83a85f..4cf727e 100644 --- a/src/yaml/objectParser.hh +++ b/src/yaml/objectParser.hh @@ -19,7 +19,7 @@ namespace yaml { struct ObjectParser : public ScalarMappingParser { - ObjectParser(Scene& scene, ParserStack& parsers); + ObjectParser(Scene& scene, ParserStack& parsers, const std::string& tag); ~ObjectParser(); protected: @@ -27,18 +27,34 @@ protected: void HandleValueEvent(yaml_event_t& event); private: + enum class Type { + Box, + Sphere + }; + enum Section { NoSection, ColorSection, + + /* Sphere sections */ OriginSection, - RadiusSection + RadiusSection, + + /* Box sections */ + NearSection, + FarSection }; void HandleColorEvent(yaml_event_t& event); + void HandleOriginEvent(yaml_event_t& event); void HandleRadiusEvent(yaml_event_t& event); - charles::Sphere::Ptr mObject; + void HandleNearEvent(yaml_event_t& event); + void HandleFarEvent(yaml_event_t& event); + + charles::Object::Ptr mObject; + Type mType; Section mSection; }; diff --git a/src/yaml/sceneParser.cc b/src/yaml/sceneParser.cc index e94199b..a435df8 100644 --- a/src/yaml/sceneParser.cc +++ b/src/yaml/sceneParser.cc @@ -132,7 +132,8 @@ SceneParser::HandleObjectsEvent(yaml_event_t& event) return; } - GetParsers().push(new ObjectParser(GetScene(), GetParsers())); + GetParsers().push(new ObjectParser(GetScene(), GetParsers(), + (char*)event.data.mapping_start.tag)); } } /* namespace yaml */