//----------------------------------------------------------------------------- bool datareader_c::read_joint( model_ptr owner, xml_element_ptr top, std::string key ) { xml_attribute_ptr attribute = top->attribute( "id" ); if( !attribute ) return false; std::string name = attribute->get_value(); xml_element_ptr element; std::string tag; joint_ptr joint = joint_ptr( new joint_c() ); component_ptr component = boost::dynamic_pointer_cast<component_c>( joint ); if( key != "" ) key += "::"; key += name; joint->id = name; for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); tag = element->get_name(); if( tag == "Field" ) { read_field( component, element, key ); } } owner->insert( joint ); return true; }
//----------------------------------------------------------------------------- bool datareader_c::read_model( component_ptr owner, xml_element_ptr top, std::string key ) { xml_attribute_ptr attribute = top->attribute( "id" ); if( !attribute ) return false; std::string name = attribute->get_value(); xml_element_ptr element; std::string tag; model_ptr model = model_ptr( new model_c() ); if( owner->component_type() == component_c::TRIAL ) { trial_ptr trial = boost::dynamic_pointer_cast<trial_c>( owner ); trial->models.push_back( model ); } else if( owner->component_type() == component_c::SOLUTION ) { solution_ptr solution = boost::dynamic_pointer_cast<solution_c>( owner ); solution->models.push_back( model ); } else { return false; } key = name; model->id = name; for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); tag = element->get_name(); if( tag == "Link" ) { read_link( model, element, key ); } else if( tag == "Joint" ) { read_joint( model, element, key ); } } return true; }
//----------------------------------------------------------------------------- bool datamap_c::map_field_element( xml_element_ptr element, unsigned& column, std::string parent_key ) { xml_attribute_ptr attribute; std::string key = ""; unsigned size = 1; // This method needs more flexibility. Parsing the field here makes it easy // to know size, but the hardcoding of field names limits the capability for( unsigned j = 0; j < element->attributes(); j++ ) { attribute = element->attribute( j ); if( attribute->get_name() == "name" ) { key += attribute->get_value(); if( key == "position" ) { size = 3; } else if( key == "rotation" ) { size = 4; } else if( key == "linear-velocity" ) { size = 3; } else if( key == "angular-velocity" ) { size = 3; } else if( key == "control" ) { size = 6; } if( parent_key != "" ) { key = parent_key + "::" + key; } _map.insert( std::pair<std::string,unsigned>( key, ++column ) ); column += (size - 1); break; } } return true; }
//----------------------------------------------------------------------------- bool datamap_c::map_model_element( xml_element_ptr top, unsigned& column ) { xml_element_ptr element; xml_attribute_ptr attribute; std::string key = ""; for( unsigned i = 0; i < top->attributes(); i++ ) { attribute = top->attribute( i ); if( attribute->get_name() == "id" ) { key = attribute->get_value(); break; } } for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); if( element->get_name() == "Link" ) map_link_element( element, column, key ); else if( element->get_name() == "Joint" ) map_joint_element( element, column, key ); } return true; }
//----------------------------------------------------------------------------- bool xml_c::build_tixml_element( void* tixml_element, xml_element_ptr element ) { // TODO: robust error checking TiXmlElement* tixml_e = (TiXmlElement*) tixml_element; std::string value = element->get_value(); if( value != "" ) tixml_e->LinkEndChild( new TiXmlText( value ) ); for( unsigned i = 0; i < element->attributes(); i++ ) { xml_attribute_ptr attrib = element->attribute( i ); tixml_e->SetAttribute( attrib->get_name(), attrib->get_value() ); } for( unsigned i = 0; i < element->elements(); i++ ) { xml_element_ptr child = element->element( i ); TiXmlElement* tixml_child = new TiXmlElement( child->get_name() ); tixml_e->LinkEndChild( tixml_child ); build_tixml_element( tixml_child, child ); } return true; }
//----------------------------------------------------------------------------- bool datamap_c::map_link_element( xml_element_ptr top, unsigned& column, std::string parent_key ) { xml_element_ptr element; xml_attribute_ptr attribute; std::string key = ""; for( unsigned i = 0; i < top->attributes(); i++ ) { attribute = top->attribute( i ); if( attribute->get_name() == "id" ) { key = attribute->get_value(); break; } } if( parent_key != "" ) { key = parent_key + "::" + key; } for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); if( element->get_name() == "Field" ) map_field_element( element, column, key ); } return true; }
//----------------------------------------------------------------------------- bool datareader_c::read_field( component_ptr owner, xml_element_ptr top, std::string key ) { xml_attribute_ptr attribute = top->attribute( "name" ); if( !attribute ) return false; std::string name = attribute->get_value(); // printf( "name: %s\n", name.c_str() ); unsigned size = 1; if( key != "" ) key += "::"; key += name; unsigned column = _column_map->column( key ); //printf( "key[%s], column[%u]\n", key.c_str(), column ); if( owner->component_type() == component_c::TRIAL ) { trial_ptr trial = boost::dynamic_pointer_cast<trial_c>( owner ); if( name == "time" ) { trial->t = _cells[column-1]; return true; } } else if( owner->component_type() == component_c::SOLUTION ) { solution_ptr solution = boost::dynamic_pointer_cast<solution_c>( owner ); if( name == "time" ) { solution->t = _cells[column-1]; return true; } else if( name == "real-time" ) { solution->real_time = _cells[column-1]; return true; } } else if( owner->component_type() == component_c::LINK ) { link_ptr link = boost::dynamic_pointer_cast<link_c>( owner ); if( name == "position" ) { size = 3; link->state.linear_x( _cells[column-1] ); link->state.linear_y( _cells[column] ); link->state.linear_z( _cells[column+1] ); return true; } else if( name == "rotation" ) { size = 4; link->state.angular_x( _cells[column-1] ); link->state.angular_y( _cells[column] ); link->state.angular_z( _cells[column+1] ); link->state.angular_w( _cells[column+2] ); return true; } else if( name == "linear-velocity" ) { size = 3; link->state.linear_dx( _cells[column-1] ); link->state.linear_dy( _cells[column] ); link->state.linear_dz( _cells[column+1] ); return true; } else if( name == "angular-velocity" ) { size = 3; link->state.angular_dx( _cells[column-1] ); link->state.angular_dy( _cells[column] ); link->state.angular_dz( _cells[column+1] ); return true; } } else if( owner->component_type() == component_c::JOINT ) { joint_ptr joint = boost::dynamic_pointer_cast<joint_c>( owner ); if( name == "control" ) { size = 6; for( unsigned i = 0; i < size; i++ ) { unsigned cell = column - 1 + i; joint->control[i] = _cells[cell]; } return true; } } return false; }