//--------------------------------------------------------------------------------------------------
/// Create a new PropertySet from the specified XML element
///
/// The XML element passed as a parameter is assumed to be a PropertySet element
//--------------------------------------------------------------------------------------------------
ref<PropertySet> PropertyXmlSerializer::createPropertySetFromXmlElement(const XmlElement& xmlPropertySetElement)
{
    CVF_ASSERT(xmlPropertySetElement.name() == "PropertySet");

    String propSetClassType = xmlPropertySetElement.getAttributeString("classType");
    ref<PropertySet> ps = new PropertySet(propSetClassType);

    const XmlElement* xmlElem = xmlPropertySetElement.firstChildElement();
    while (xmlElem)
    {
        Variant variant = variantFromXmlElement(*xmlElem);
        if (variant.isValid())
        {
            String key = xmlElem->getAttributeString("key");
            if (!key.isEmpty())
            {
                ps->setValue(key, variant);
            }
        }

        xmlElem = xmlElem->nextSiblingElement();
    }

    return ps;
}
//--------------------------------------------------------------------------------------------------
/// Populate the PropertySet from the specified XML element
///
/// The XML element passed as a parameter is assumed to be a PropertySet element
//--------------------------------------------------------------------------------------------------
void PropertyXmlSerializer::toPropertySetCollection(const XmlElement& xmlPropertySetCollectionElement, PropertySetCollection* propertySetCollection)
{
    CVF_ASSERT(xmlPropertySetCollectionElement.name() == "PropertySetCollection");
    CVF_ASSERT(propertySetCollection);

    const XmlElement* xmlElem = xmlPropertySetCollectionElement.firstChildElement("PropertySet");
    while (xmlElem)
    {
        ref<PropertySet> ps = createPropertySetFromXmlElement(*xmlElem);
        propertySetCollection->addPropertySet(ps.p());

        xmlElem = xmlElem->nextSiblingElement("PropertySet");
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
cvf::Variant PropertyXmlSerializer::arrayVariantFromXmlElement(const XmlElement& xmlArrayVariantElement)
{
    CVF_ASSERT(xmlArrayVariantElement.name().toLower() == "array");

    std::vector<Variant> arr;

    const XmlElement* xmlElem = xmlArrayVariantElement.firstChildElement();
    while (xmlElem)
    {
        Variant variant = variantFromXmlElement(*xmlElem);
        if (variant.isValid())
        {
            arr.push_back(variant);
        }

        xmlElem = xmlElem->nextSiblingElement();
    }

    return Variant(arr);
}
//--------------------------------------------------------------------------------------------------
/// Create variant from the specified XML element
///
/// If the passed XML element is not recognized as a variant, an invalid variant will be returned
//--------------------------------------------------------------------------------------------------
cvf::Variant PropertyXmlSerializer::variantFromXmlElement(const XmlElement& xmlVariantElement)
{
    const String elementName = xmlVariantElement.name().toLower();
    const String valueText = xmlVariantElement.valueText();

    if (elementName == "int")
    {
        bool ok = false;
        int val = valueText.toInt(&ok);
        if (ok)
        {
            return Variant(val);
        }
    }
    else if (elementName == "uint")
    {
        bool ok = false;
        uint val = valueText.toUInt(&ok);
        if (ok)
        {
            return Variant(val);
        }
    }
    else if (elementName == "double")
    {
        bool ok = false;
        double val = valueText.toDouble(&ok);
        if (ok)
        {
            return Variant(val);
        }
    }
    else if (elementName == "float")
    {
        bool ok = false;
        float val = valueText.toFloat(&ok);
        if (ok)
        {
            return Variant(val);
        }
    }
    else if (elementName == "bool")
    {
        String valStr = valueText.toLower();
        bool val = (valStr == "true") ? true : false;
        return Variant(val);
    }
    else if (elementName == "vec3d")
    {
        return variantFromVec3dValueText(valueText);
    }
    else if (elementName == "color3f")
    {
        return variantFromColor3fValueText(valueText);
    }
    else if (elementName == "string")
    {
        return Variant(valueText);
    }
    else if (elementName == "array")
    {
        return arrayVariantFromXmlElement(xmlVariantElement);
    }

    return Variant();
}
Exemplo n.º 5
0
void FlowData::load()
{
	if(!mReady)
	{
		Timer timer(true);
		
		XmlDocument * pointsDoc = new XmlDocument(dataSource);
		XmlElement rootNode = pointsDoc->rootNode();
		
		if(rootNode.hasChildren())
		{
			XmlElement tagNode = rootNode.findChild("tag");
			XmlElement drawingNode = tagNode.findChild("drawing");
			
			// Now get all the drawingNode's children.. and only worry about the stroke nodes.
			if(drawingNode.hasChildren())
			{
				std::vector<XmlElement> strokeNodes = drawingNode.children();
				
				// std::vector<XmlElement> strokeNodes = rootNode.xpath("//tag/drawing//stroke");
				
				if(strokeNodes.size() > 0)
				{
					int totalPoints = 0;
					
					// Now declare some variables to reuse in our loop.
					FlowPoint lastPt(Vec2f::zero(), Vec2f::zero(), -1.f);
					
					for(std::vector<XmlElement>::iterator it = strokeNodes.begin(); it < strokeNodes.end(); it++)
					{
						XmlElement strokeNode = *it;
						
						if(strokeNode.name() == "stroke")
						{
							// Get all the point nodes.
							std::vector<XmlElement> pointNodes = strokeNode.children();
							
							// Create a new stroke 
							std::vector<FlowPoint> * stroke = new std::vector<FlowPoint>;
							
							for(std::vector<XmlElement>::iterator it2 = pointNodes.begin(); it2 < pointNodes.end(); it2++)
							{
								XmlElement ptNode = *it2;
								if(ptNode.name() == "pt")
								{
									std::string xVal = ptNode.findChild("x").value();
									// float x = fromString( xVal );
									float x = boost::lexical_cast<float>( xVal );
									
									std::string yVal = ptNode.findChild("y").value();
									// float y = fromString( yVal );
									float y = boost::lexical_cast<float>( yVal );
									
									std::string timeVal = ptNode.findChild("time").value();
									// float time = fromString( timeVal );
									float time = boost::lexical_cast<float>( timeVal );
									
									x = mRemapMinX + (mRemapMaxX - mRemapMinX) * x;
									y = mRemapMinY + (mRemapMaxY - mRemapMinY) * y;
									Vec2f pos(x, y);
									Vec2f vel;
									if(lastPt.time > -1)
									{
										vel.x = pos.x - lastPt.getX();
										vel.y = pos.y - lastPt.getY();
									}
									FlowPoint pt(pos, vel, time);
									
									bool shouldAddPoint = false;
									if(ignoreRedundantPositions == true)
									{
										if(lastPt.time > -1)
										{
											if(pt.getX() != lastPt.getX() && pt.getY() != lastPt.getY())
											{
												shouldAddPoint = true;
											}  
										}
										else
										{
											shouldAddPoint = true;
										}
									}
									else
									{
										shouldAddPoint = true;
									}
									
									if(shouldAddPoint)
									{
										totalPoints++;
										stroke->push_back(pt);
										lastPt = FlowPoint(pt.pos, pt.vel, pt.time);
									}
								}
							}
							
							// Now see if our stroke is long enough.
							if(stroke->size() > minNumberOfPointsInStroke)
							{
								mStrokes.push_back(stroke);
							}
						}
					}
					
					// We're done.
					timer.stop();
					
					
					std::cout << "FlowData :: GML file parsing complete. Elapsed seconds: " << toString( timer.getSeconds() ) << std::endl;
					std::cout << "FlowData :: Total number of points: " << totalPoints << std::endl;
					std::cout << "FlowData :: Number of strokes: " << mStrokes.size() << std::endl;
					
					if(mStrokes.size() > 0)
						mReady = true;
				}
			}
		}
	}
}