Exemple #1
0
XERCES_CPP_NAMESPACE_USE

#define ARRAY_SIZE(ARY) ( (int)(sizeof(ARY)/sizeof(ARY[0])) )

#ifdef CONF_DUMP
#define ENABLE_DUMP
#define ENABLE_DUMP1
#endif
#include "ParserDump.h"

void JointParser::parse(DOMNode &target, Eval &eval)
{
	char *name = XMLUtils::getAttribute(target, "name");
	DUMP1(("name %s\n", name));

	m_transform.push();

	DOMNode *p = target.getFirstChild();
	while (p) {
		char *s = XMLString::transcode(p->getNodeName());
		//		printf("%s\n", s);
		if (strcmp(s, "type") == 0) {
			char *type = XMLUtils::parseJointType(*p);
			DUMP1(("type=\"%s\"\n", type));
			Joint *j = 0;
			if (strcmp(type, "fixed") == 0) {
				j = new FixedJoint(name);
			} else if (strcmp(type, "ball") == 0) {
				j = new BallJoint(name);
			} else if (strcmp(type, "hinge") == 0) {
				j = new HingeJoint(name, Vector3d(0, 1, 0));
			}
			
			if (j) {
				m_joint = j;
			}
			XMLString::release(&type);
			/*
		} else if (strcmp(s, "limitOrientation") == 0) {
			double *v = XMLUtils::parseLimitOrientation(*p);
			if (m_joint) {
				delete m_joint;
			}
			m_joint = new HingeJoint(name, Vector3d(v[0], v[1], v[2]));
			*/
				
		} else if (strcmp(s, "translation") == 0) {
			double * values = XMLUtils::parseTranslation(*p, eval);
			DUMP1(("translation (%f, %f, %f)\n", values[0], values[1], values[2]));
			m_transform.curr().push(Vector3d(values[0], values[1], values[2]));

		} else if (strcmp(s, "rotation") == 0) {
			double *values = XMLUtils::parseRotation(*p);
			DUMP1(("rotation (%f, %f, %f, %f)\n", values[0], values[1], values[2], values[3]));
			Rotation rot;
			rot.setAxisAndAngle(values[0], values[1], values[2], values[3]);
			m_transform.curr().push(rot);
			
		} else if (strcmp(s, "axis") == 0) {
			double *values = XMLUtils::parseAxis(*p);
			DUMP1(("axis (%f, %f, %f)\n", values[0], values[1], values[2]));
			
			if (m_joint) {
				if (m_joint->type() == Joint::TYPE_HINGE) {
					HingeJoint *hinge = static_cast<HingeJoint*>(m_joint);
					hinge->setAxis(values[0], values[1], values[2]);
				} else {
					DUMP1(("not hinge type joint. axis ingored\n"));
				}
			} else {
				DUMP1(("no joint type"));
			}
				

		} else if (strcmp(s, "children") == 0) {
			DUMP1(("JOINT TRANSFORM TEST \n"));
			TRANSFORM_TEST(m_transform.curr());
			parseChildren(*p, eval);
		}
		
		p = p->getNextSibling();
		XMLString::release(&s);
	}
	{
		Transform &t = m_transform.curr();
		const Vector3d &v = t.translation();
		if (m_joint) {
			m_f.addJoint(m_joint);
			DUMP(("Joint : (%f, %f, %f)\n", v.x(), v.y(), v.z()));
			m_joint->setAnchor(v.x(), v.y(), v.z());
		}
	}
	m_transform.pop();

	char *strs[] = { name,};
	for (int i=0; i<ARRAY_SIZE(strs); i++) {
		char *p = strs[i];
		XMLString::release(&p);
	}
}
Exemple #2
0
Joint* JointInteraction::xmlParseJoint(IrrXMLReader* xml, std::string& jointType)
{
	bool finished = false;
	Constraint* constraint = NULL;

	Joint* joint = JointFactory::create(xml->getAttributeValue("type"));
	jointType = xml->getAttributeValue("type");
	joint->setWorld(world);
	joint->setId(atoi(xml->getAttributeValue("id")));
	joint->setJointInteractionClass(this);
	if (xml->getAttributeValue("active"))
		joint->setActive(atoi(xml->getAttributeValue("active")));

	while (!finished && xml && xml->read())
	{
		switch (xml->getNodeType())
		{
			case EXN_ELEMENT:
				if (!strcmp("entities", xml->getNodeName()))
				{
					xmlParseEntities(xml, joint);
				} // else if
				else if (!strcmp("anchor", xml->getNodeName()))
				{
					if (!jointType.compare("ball") || !jointType.compare("Ball") || !jointType.compare("BALL"))
					{
						BallJoint* ball = dynamic_cast<BallJoint*>(joint);
						ball->setAnchor(atof(xml->getAttributeValue("xPos")), atof(xml->getAttributeValue("yPos")), atof(xml->getAttributeValue("zPos")));
					} // if
					else if (!jointType.compare("hinge") || !jointType.compare("Hinge") || !jointType.compare("HINGE"))
					{
						HingeJoint* hinge = dynamic_cast<HingeJoint*>(joint);
						hinge->setAnchor(atof(xml->getAttributeValue("xPos")), atof(xml->getAttributeValue("yPos")), atof(xml->getAttributeValue("zPos")));
					} // else if
					else if (!jointType.compare("universal") || !jointType.compare("Universal") || !jointType.compare("UNIVERSAL"))
					{
						UniversalJoint* universal = dynamic_cast<UniversalJoint*>(joint);
						universal->setAnchor(atof(xml->getAttributeValue("xPos")), atof(xml->getAttributeValue("yPos")), atof(xml->getAttributeValue("zPos")));
					} // else if
					else if (!jointType.compare("hinge2") || !jointType.compare("Hinge2") || !jointType.compare("HINGE2"))
					{
						Hinge2Joint* hinge2 = dynamic_cast<Hinge2Joint*>(joint);
						hinge2->setAnchor(atof(xml->getAttributeValue("xPos")), atof(xml->getAttributeValue("yPos")), atof(xml->getAttributeValue("zPos")));
					} // else if
					else
						printd(WARNING, "JointInteraction::loadJoints(): WARNING: JOINT-TYPE  %s NEEDS NO anchor ELEMENT!!!\n", jointType.c_str());
				} // else if
				else if (!strcmp("axis", xml->getNodeName()))
				{
					if (!jointType.compare("hinge") || !jointType.compare("Hinge") || !jointType.compare("HINGE"))
					{
						HingeJoint* hinge = dynamic_cast<HingeJoint*>(joint);
						hinge->setAxis(atof(xml->getAttributeValue("xDir")), atof(xml->getAttributeValue("yDir")), atof(xml->getAttributeValue("zDir")));
					} // if
					else if (!jointType.compare("slider") || !jointType.compare("Slider") || !jointType.compare("SLIDER"))
					{
						SliderJoint* slider = dynamic_cast<SliderJoint*>(joint);
						slider->setAxis(atof(xml->getAttributeValue("xDir")), atof(xml->getAttributeValue("yDir")), atof(xml->getAttributeValue("zDir")));
					} // else if
					else if (!jointType.compare("universal") || !jointType.compare("Universal") || !jointType.compare("UNIVERSAL")) {
						UniversalJoint* universal = dynamic_cast<UniversalJoint*>(joint);
						if (atoi(xml->getAttributeValue("index")) == 1)
							universal->setAxis1(atof(xml->getAttributeValue("xDir")), atof(xml->getAttributeValue("yDir")), atof(xml->getAttributeValue("zDir")));
						else if (atoi(xml->getAttributeValue("index")) == 2)
							universal->setAxis2(atof(xml->getAttributeValue("xDir")), atof(xml->getAttributeValue("yDir")), atof(xml->getAttributeValue("zDir")));
						else
							printd(WARNING, "JointInteraction::loadJoints(): WARNING: WRONG ATTRIBUTE FOUND IN axis ELEMENT of UniversalJoint!!!\n");
					} // else if
					else if (!jointType.compare("hinge2") || !jointType.compare("Hinge2") || !jointType.compare("HINGE2")) {
						Hinge2Joint* hinge2 = dynamic_cast<Hinge2Joint*>(joint);
						if (atoi(xml->getAttributeValue("index")) == 1)
							hinge2->setAxis1(atof(xml->getAttributeValue("xDir")), atof(xml->getAttributeValue("yDir")), atof(xml->getAttributeValue("zDir")));
						else if (atoi(xml->getAttributeValue("index")) == 2)
							hinge2->setAxis2(atof(xml->getAttributeValue("xDir")), atof(xml->getAttributeValue("yDir")), atof(xml->getAttributeValue("zDir")));
						else
							printd(WARNING, "JointInteraction::loadJoints(): WARNING: WRONG ATTRIBUTE FOUND IN axis ELEMENT of Hinge2Joint!!!\n");
					} // else if
					else
						printd(WARNING, "JointInteraction::loadJoints(): WARNING: JOINT-TYPE  %s NEEDS NO axis ELEMENT!!!\n", jointType.c_str());
				} // else if
				else if (!strcmp("angles", xml->getNodeName()))
				{
					if (!jointType.compare("hinge") || !jointType.compare("Hinge") || !jointType.compare("HINGE"))
					{
						HingeJoint* hinge = dynamic_cast<HingeJoint*>(joint);
						hinge->setAngles(atof(xml->getAttributeValue("min")), atof(xml->getAttributeValue("max")));
					} // if
					else
						printd(WARNING, "JointInteraction::loadJoints(): WARNING: JOINT-TYPE  %s HAS NO angles ELEMENT!!!\n", jointType.c_str());
				} // else if
				else if (!strcmp("positions", xml->getNodeName()))
				{
					SliderJoint* slider = dynamic_cast<SliderJoint*>(joint);
					slider->setPositions(atof(xml->getAttributeValue("min")), atof(xml->getAttributeValue("max")));
				} // else if
				else if (!strcmp("activateIF", xml->getNodeName()))
				{
					constraint = new Constraint(this);
					constraint->setActionType(Constraint::ACTIVATEIF);
					constraint->setOwner(joint);
					xmlParseConstraintAttributes(xml, constraint);
					joint->addConstraint(constraint);
					constraint = NULL;
				} // else if
				else if (!strcmp("deactivateIF", xml->getNodeName()))
				{
					constraint = new Constraint(this);
					constraint->setActionType(Constraint::DEACTIVATEIF);
					constraint->setOwner(joint);
					xmlParseConstraintAttributes(xml, constraint);
					joint->addConstraint(constraint);
					constraint = NULL;
				} // else if
				else if (!strcmp("activeIF", xml->getNodeName()))
				{
					constraint = new Constraint(this);
					constraint->setActionType(Constraint::ACTIVEIF);
					constraint->setOwner(joint);
					xmlParseConstraintAttributes(xml, constraint);
					joint->addConstraint(constraint);
					constraint = NULL;
				} // else if
				else if (!strcmp("inactiveIF", xml->getNodeName()))
				{
					constraint = new Constraint(this);
					constraint->setActionType(Constraint::INACTIVEIF);
					constraint->setOwner(joint);
					xmlParseConstraintAttributes(xml, constraint);
					joint->addConstraint(constraint);
					constraint = NULL;
				} // else if
				break;
			case EXN_ELEMENT_END:
				if (!strcmp("joint", xml->getNodeName()))
					finished = true;
				break;
			default:
				break;
		} // switch
	} // while
	assert(xml);
	return joint;
} // xmlParseJoint