Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

Commit

Permalink
fix segfault on groovy, not tested on fuerte see #4
Browse files Browse the repository at this point in the history
  • Loading branch information
k-okada committed Feb 15, 2013
1 parent 3ee9e5a commit ff7c4f4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 86 deletions.
32 changes: 2 additions & 30 deletions collada_urdf/include/collada_urdf/collada_urdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,12 @@ class ColladaUrdfException : public std::runtime_error
ColladaUrdfException(std::string const& what);
};

/** Construct a COLLADA DOM from an URDF file
* \param file The filename from where to read the URDF
* \param dom The resulting COLLADA DOM
* \return true on success, false on failure
*/
bool colladaFromUrdfFile(std::string const& file, boost::shared_ptr<DAE>& dom);

/** Construct a COLLADA DOM from a string containing URDF
* \param xml A string containing the XML description of the robot
* \param dom The resulting COLLADA DOM
* \return true on success, false on failure
*/
bool colladaFromUrdfString(std::string const& xml, boost::shared_ptr<DAE>& dom);

/** Construct a COLLADA DOM from a TiXmlDocument containing URDF
* \param xml_doc The TiXmlDocument containing URDF
* \param dom The resulting COLLADA DOM
* \return true on success, false on failure
*/
bool colladaFromUrdfXml(TiXmlDocument* xml_doc, boost::shared_ptr<DAE>& dom);

/** Construct a COLLADA DOM from a URDF robot model
* \param robot_model The URDF robot model
* \param dom The resulting COLLADA DOM
* \return true on success, false on failure
*/
bool colladaFromUrdfModel(urdf::Model const& robot_model, boost::shared_ptr<DAE>& dom);

/** Write a COLLADA DOM to a file
* \param dom COLLADA DOM to write
* \param robot_model The URDF robot model
* \param file The filename to write the document to
* \return true on success, false on failure
*/
bool colladaToFile(boost::shared_ptr<DAE> dom, std::string const& file);
bool WriteUrdfModelToColladaFile(urdf::Model const& robot_model, std::string const& file);

}

Expand Down
77 changes: 28 additions & 49 deletions collada_urdf/src/collada_urdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,24 +595,24 @@ class ColladaWriter : public daeErrorHandler
public:
ColladaWriter(const urdf::Model& robot, int writeoptions) : _writeoptions(writeoptions), _robot(robot), _dom(NULL) {
daeErrorHandler::setErrorHandler(this);
_collada.reset(new DAE);
_collada->setIOPlugin(NULL);
_collada->setDatabase(NULL);
_importer.SetIOHandler(new ResourceIOSystem());
}
virtual ~ColladaWriter() {
}

boost::shared_ptr<DAE> convert()
daeDocument* doc() {
return _doc;
}

bool convert()
{
try {
const char* documentName = "urdf_snapshot";
daeDocument *doc = NULL;
daeInt error = _collada->getDatabase()->insertDocument(documentName, &doc ); // also creates a collada root
if (error != DAE_OK || doc == NULL) {
daeInt error = _collada.getDatabase()->insertDocument(documentName, &_doc ); // also creates a collada root
if (error != DAE_OK || _doc == NULL) {
throw ColladaUrdfException("Failed to create document");
}
_dom = daeSafeCast<domCOLLADA>(doc->getDomRoot());
_dom = daeSafeCast<domCOLLADA>(_doc->getDomRoot());
_dom->setAttribute("xmlns:math","http://www.w3.org/1998/Math/MathML");

//create the required asset tag
Expand Down Expand Up @@ -674,12 +674,22 @@ class ColladaWriter : public daeErrorHandler
_WritePhysics();
_WriteRobot();
_WriteBindingsInstance_kinematics_scene();
return _collada;
return true;
}
catch (ColladaUrdfException ex) {
ROS_ERROR("Error converting: %s", ex.what());
return boost::shared_ptr<DAE>();
return false;
}
}

bool writeTo(string const& file) {
try {
daeString uri = _doc->getDocumentURI()->getURI();
_collada.writeTo(uri, file);
} catch (ColladaUrdfException ex) {
return false;
}
return true;
}

protected:
Expand Down Expand Up @@ -1764,8 +1774,9 @@ class ColladaWriter : public daeErrorHandler
int _writeoptions;

const urdf::Model& _robot;
boost::shared_ptr<DAE> _collada;
DAE _collada;
domCOLLADA* _dom;
daeDocument *_doc;
domCOLLADA::domSceneRef _globalscene;

domLibrary_visual_scenesRef _visualScenesLib;
Expand Down Expand Up @@ -1794,45 +1805,13 @@ ColladaUrdfException::ColladaUrdfException(std::string const& what)
{
}

bool colladaFromUrdfFile(string const& file, boost::shared_ptr<DAE>& dom) {
TiXmlDocument urdf_xml;
if (!urdf_xml.LoadFile(file)) {
ROS_ERROR("Could not load XML file");
return false;
}

return colladaFromUrdfXml(&urdf_xml, dom);
}

bool colladaFromUrdfString(string const& xml, boost::shared_ptr<DAE>& dom) {
TiXmlDocument urdf_xml;
if (urdf_xml.Parse(xml.c_str()) == 0) {
ROS_ERROR("Could not parse XML document");
return false;
}

return colladaFromUrdfXml(&urdf_xml, dom);
}

bool colladaFromUrdfXml(TiXmlDocument* xml_doc, boost::shared_ptr<DAE>& dom) {
urdf::Model robot_model;
if (!robot_model.initXml(xml_doc)) {
ROS_ERROR("Could not generate robot model");
return false;
}

return colladaFromUrdfModel(robot_model, dom);
}

bool colladaFromUrdfModel(urdf::Model const& robot_model, boost::shared_ptr<DAE>& dom) {
bool WriteUrdfModelToColladaFile(urdf::Model const& robot_model, string const& file) {
ColladaWriter writer(robot_model,0);
dom = writer.convert();
return dom != boost::shared_ptr<DAE>();
}

bool colladaToFile(boost::shared_ptr<DAE> dom, string const& file) {
daeString uri = dom->getDoc(0)->getDocumentURI()->getURI();
return dom->writeTo(uri, file);
if ( ! writer.convert() ) {
std::cerr << std::endl << "Error converting document" << std::endl;
return -1;
}
return writer.writeTo(file);
}

}
8 changes: 1 addition & 7 deletions collada_urdf/src/urdf_to_collada.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ int main(int argc, char** argv)
ROS_ERROR("failed to open urdf file %s",input_filename.c_str());
}

boost::shared_ptr<DAE> dom;
if (!collada_urdf::colladaFromUrdfModel(robot_model, dom)) {
std::cerr << std::endl << "Error converting document" << std::endl;
return -1;
}

collada_urdf::colladaToFile(dom, output_filename);
collada_urdf::WriteUrdfModelToColladaFile(robot_model, output_filename);
std::cout << std::endl << "Document successfully written to " << output_filename << std::endl;

return 0;
Expand Down

3 comments on commit ff7c4f4

@jacquelinekay
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @k-okada:

I was wondering why collada_urdf.h has so many missing functions. This PR got merged before I took over as a maintainer; do you think you could tell me what the justification was?

I think that it's better API design to break up the functionality of ColladaWriter into separate functions. Additionally, the tests for this package are currently commented out, and the body of those tests includes functionality that was deleted in this commit. If you think it's a good idea I will attempt to cherry-pick some old commits (e.g. e92227f) to restore them.

@k-okada
Copy link
Contributor Author

@k-okada k-okada commented on ff7c4f4 May 16, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacquelinekay
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see why the shared_ptr is a bad idea; it seems that some of the data pointed to by the DAE is deleted when the ColladaWriter object goes out of scope.

Please sign in to comment.