40 #include <boost/regex.hpp>
49 static const boost::regex compound_regex(
"compound_\\w{6}");
69 std::map<std::string, int> tgCompoundRigidSensorInfo::getCompoundTags(
tgModel* pModel)
72 if ( pModel == NULL) {
73 throw std::invalid_argument(
"pModel was NULL inside tgCompoundRigidSensorInfo.");
79 std::map<std::string, int> compounds;
83 for (
size_t i=0; i < descendants.size(); i++){
86 std::stringstream tagstream;
87 tagstream << descendants[i]->getTags();
88 std::string tags = tagstream.str();
90 boost::smatch matches;
91 bool anymatches = boost::regex_search(tags, matches, compound_regex);
102 if (matches.size() >= 2 ){
103 throw std::runtime_error(
"A tgModel has more than one compound tag in its tag list, inside tgCompoundRigidSensorInfo. This is impossible, and something is very wrong.");
108 tgCast::cast<tgSenseable, tgBaseRigid>(descendants[i]);
109 if (pBaseRigid == 0 ) {
110 throw std::runtime_error(
"A tgModel that is NOT a rigid body has a compound tag attached to it. Only rigid bodies should have compound tags.");
113 compounds[matches.str(0)] += 1;
123 bool tgCompoundRigidSensorInfo::isBlacklisted(std::string tag)
126 std::vector<std::string>::iterator it;
127 it = std::find( blacklist.begin(), blacklist.end(), tag);
128 if( it == blacklist.end() ) {
148 if ( pSenseable == NULL) {
149 throw std::invalid_argument(
"pSenseable was NULL inside tgCompoundRigidSensorInfo.");
154 tgModel* pModel = tgCast::cast<tgSenseable, tgModel>(pSenseable);
159 std::map<std::string, int> compounds = getCompoundTags(pModel);
168 std::map<std::string, int>::iterator it;
169 for( it = compounds.begin(); it != compounds.end(); ++it){
172 if( (it->second) >= 2 ) {
175 if( !isBlacklisted( it->first ) ){
179 else if( (it->second) == 1) {
182 throw std::runtime_error(
"There is only one object with a compound tag. That's not possible, compound tgModels should be at least two objects. Exiting.");
198 throw std::invalid_argument(
"pSenseable is NOT a valid tgModel or does not have non-blacklisted compound rigid bodies, inside tgCompoundRigidSensorInfo.");
201 std::vector<tgSensor*> newSensors;
212 tgModel* pModel = tgCast::cast<tgSenseable, tgModel>(pSenseable);
214 throw std::invalid_argument(
"pSenseable is not a tgModel inside tgCompoundRigidSensorInfo::createSensorsIfAppropriate.");
218 std::map<std::string, int> compounds = getCompoundTags(pModel);
220 for(
size_t i=0; i < blacklist.size(); i++) {
222 compounds.erase(blacklist[i]);
226 std::map<std::string, int>::iterator it;
227 for( it = compounds.begin(); it != compounds.end(); ++it){
236 blacklist.push_back( it->first );
virtual std::vector< tgSensor * > createSensorsIfAppropriate(tgSenseable *pSenseable)
Create a box shape as an obstacle or add it to your tensegrity.
Constains definition of concrete class tgCompoundRigidSensor.
Utility class for class casting and filtering collections by type.
virtual bool isThisMySenseable(tgSenseable *pSenseable)
Contains the definition of class tgModel.
Definition of concrete class tgCompoundRigidSensorInfo.
~tgCompoundRigidSensorInfo()
Constains the implementation of mixin class tgSenseable.
tgCompoundRigidSensorInfo()
std::vector< tgModel * > getDescendants() const