void ModelLoaderHelper::createColdetModel(Link* link, const LinkInfo& linkInfo)
{
    int totalNumVertices = 0;
    int totalNumTriangles = 0;
    const TransformedShapeIndexSequence& shapeIndices = linkInfo.shapeIndices;
    unsigned int nshape = shapeIndices.length();
    short shapeIndex;
    double R[9], p[3];
    for(unsigned int i=0; i < shapeIndices.length(); i++){
        shapeIndex = shapeIndices[i].shapeIndex;
        const DblArray12 &tform = shapeIndices[i].transformMatrix;
        R[0] = tform[0]; R[1] = tform[1]; R[2] = tform[2]; p[0] = tform[3];
        R[3] = tform[4]; R[4] = tform[5]; R[5] = tform[6]; p[1] = tform[7];
        R[6] = tform[8]; R[7] = tform[9]; R[8] = tform[10]; p[2] = tform[11];
        const ShapeInfo& shapeInfo = shapeInfoSeq[shapeIndex];
        totalNumVertices += shapeInfo.vertices.length() / 3;
        totalNumTriangles += shapeInfo.triangles.length() / 3;
    }

    const SensorInfoSequence& sensors = linkInfo.sensors;
    for (unsigned int i=0; i<sensors.length(); i++){
        const SensorInfo &sinfo = sensors[i];
        const TransformedShapeIndexSequence tsis = sinfo.shapeIndices;
        nshape += tsis.length();
        for (unsigned int j=0; j<tsis.length(); j++){
            short shapeIndex = tsis[j].shapeIndex;
            const ShapeInfo& shapeInfo = shapeInfoSeq[shapeIndex];
            totalNumTriangles += shapeInfo.triangles.length() / 3;
            totalNumVertices += shapeInfo.vertices.length() / 3 ;
        }
    }

    ColdetModelPtr coldetModel(new ColdetModel());
    coldetModel->setName(std::string(linkInfo.name));
    if(totalNumTriangles > 0){
        coldetModel->setNumVertices(totalNumVertices);
        coldetModel->setNumTriangles(totalNumTriangles);
        if (nshape == 1){
            addLinkPrimitiveInfo(coldetModel, R, p, shapeInfoSeq[shapeIndex]);
        }
        addLinkVerticesAndTriangles(coldetModel, linkInfo);
        coldetModel->build();
    }
    link->coldetModel = coldetModel;
}
Esempio n. 2
0
ColdetBody::ColdetBody(BodyInfo_ptr bodyInfo)
{
    LinkInfoSequence_var links = bodyInfo->links();
    ShapeInfoSequence_var shapes = bodyInfo->shapes();

    int numLinks = links->length();

    linkColdetModels.resize(numLinks);
		
    for(int linkIndex = 0; linkIndex < numLinks ; ++linkIndex){

        LinkInfo& linkInfo = links[linkIndex];
			
        int totalNumTriangles = 0;
        int totalNumVertices = 0;
        const TransformedShapeIndexSequence& shapeIndices = linkInfo.shapeIndices;
        short shapeIndex;
        double R[9], p[3];
        unsigned int nshape = shapeIndices.length();
	    for(int i=0; i < shapeIndices.length(); i++){
            shapeIndex = shapeIndices[i].shapeIndex;
            const DblArray12 &tform = shapeIndices[i].transformMatrix;
            R[0] = tform[0]; R[1] = tform[1]; R[2] = tform[2]; p[0] = tform[3];
            R[3] = tform[4]; R[4] = tform[5]; R[5] = tform[6]; p[1] = tform[7];
            R[6] = tform[8]; R[7] = tform[9]; R[8] = tform[10]; p[2] = tform[11];
            const ShapeInfo& shapeInfo = shapes[shapeIndex];
            totalNumTriangles += shapeInfo.triangles.length() / 3;
            totalNumVertices += shapeInfo.vertices.length() / 3;
        }

        const SensorInfoSequence& sensors = linkInfo.sensors;
        for (unsigned int i=0; i<sensors.length(); i++){
            const SensorInfo &sinfo = sensors[i];
            const TransformedShapeIndexSequence tsis = sinfo.shapeIndices;
            nshape += tsis.length();
            for (unsigned int j=0; j<tsis.length(); j++){
                shapeIndex = tsis[j].shapeIndex;
                const DblArray12 &tform = tsis[j].transformMatrix;
                R[0] = tform[0]; R[1] = tform[1]; R[2] = tform[2]; p[0] = tform[3];
                R[3] = tform[4]; R[4] = tform[5]; R[5] = tform[6]; p[1] = tform[7];
                R[6] = tform[8]; R[7] = tform[9]; R[8] = tform[10]; p[2] = tform[11];
                const ShapeInfo& shapeInfo = shapes[shapeIndex];
                totalNumTriangles += shapeInfo.triangles.length() / 3;
                totalNumVertices += shapeInfo.vertices.length() / 3 ;
            }
        }

        //int totalNumVertices = totalNumTriangles * 3;

        ColdetModelPtr coldetModel(new ColdetModel());
        coldetModel->setName(std::string(linkInfo.name));
        
        if(totalNumTriangles > 0){
            coldetModel->setNumVertices(totalNumVertices);
            coldetModel->setNumTriangles(totalNumTriangles);
            if (nshape == 1){
                addLinkPrimitiveInfo(coldetModel, R, p, shapes[shapeIndex]);
            }
            addLinkVerticesAndTriangles(coldetModel, linkInfo, shapes);
            coldetModel->build();
        }

        linkColdetModels[linkIndex] = coldetModel;
        linkNameToColdetModelMap.insert(make_pair(linkInfo.name, coldetModel));

        cout << linkInfo.name << " has "<< totalNumTriangles << " triangles." << endl;
    }
}
Esempio n. 3
0
void ShapeSetInfo_impl::setColdetModel(ColdetModelPtr& coldetModel, TransformedShapeIndexSequence shapeIndices, const Matrix44& Tparent, int& vertexIndex, int& triangleIndex){
    for(unsigned int i=0; i < shapeIndices.length(); i++){
        setColdetModelTriangles(coldetModel, shapeIndices[i], Tparent, vertexIndex, triangleIndex);
    }
}
Esempio n. 4
0
/*!
  @if jp
  Shape ノード探索のための再帰関数

  node以下のシーングラフをたどり、ShapeInfoを生成していく。
  生成したShapeInfoはshapes_に追加する。
  shapes_に追加した位置(index)を io_shapeIndicesに追加する。
  io_shapeIndices は、BodyInfo構築時は LinkInfoのshapeIndices になり、
  SceneInfo 構築時は SceneInfo のshapeIndicesになる。

  @endif
*/
void ShapeSetInfo_impl::traverseShapeNodes
(VrmlNode* node, const Matrix44& T, TransformedShapeIndexSequence& io_shapeIndices, DblArray12Sequence& inlinedShapeM, const SFString* url)
{
    static int inline_count=0;   
    SFString url_ = *url;
    if(node->isCategoryOf(PROTO_INSTANCE_NODE)){
        VrmlProtoInstance* protoInstance = static_cast<VrmlProtoInstance*>(node);
        if(protoInstance->actualNode){
            traverseShapeNodes(protoInstance->actualNode.get(), T, io_shapeIndices, inlinedShapeM, url);
        }

    } else if(node->isCategoryOf(GROUPING_NODE)) {
        VrmlGroup* groupNode = static_cast<VrmlGroup*>(node);
        VrmlTransform* transformNode = dynamic_cast<VrmlTransform*>(groupNode);
        VrmlInline* inlineNode = NULL;
        
        inlineNode = dynamic_cast<VrmlInline*>(groupNode);
        const Matrix44* pT;
        if(inlineNode){
            if(!inline_count){
                int inlinedShapeMIndex = inlinedShapeM.length();
                inlinedShapeM.length(inlinedShapeMIndex+1);
                int p = 0;
                for(int row=0; row < 3; ++row){
                    for(int col=0; col < 4; ++col){
                        inlinedShapeM[inlinedShapeMIndex][p++] = T(row, col);
                    }
                }  
                url_ = inlineNode->urls[0];
            }
            inline_count++;
            for( MFString::iterator ite = inlineNode->urls.begin(); ite != inlineNode->urls.end(); ++ite ){
                string filename(deleteURLScheme(*ite));
                struct stat statbuff;
                time_t mtime = 0;
                if( stat( filename.c_str(), &statbuff ) == 0 ){
                    mtime = statbuff.st_mtime;
                }
                fileTimeMap.insert(make_pair(filename, mtime));
            }
        }
        Matrix44 T2;
        if( transformNode ){
            Matrix44 Tlocal;
            calcTransformMatrix(transformNode, Tlocal);
            T2 = T * Tlocal;
            pT = &T2;
        } else {
            pT = &T;
        }

        VrmlSwitch* switchNode = dynamic_cast<VrmlSwitch*>(node);
        if(switchNode){
            int whichChoice = switchNode->whichChoice;
            if( whichChoice >= 0 && whichChoice < switchNode->countChildren() )
                traverseShapeNodes(switchNode->getChild(whichChoice), *pT, io_shapeIndices, inlinedShapeM, &url_);
        }else{
            for(int i=0; i < groupNode->countChildren(); ++i){
                traverseShapeNodes(groupNode->getChild(i), *pT, io_shapeIndices, inlinedShapeM, &url_);
            }
        }
        if(inlineNode)
            inline_count--;

        
    } else if(node->isCategoryOf(SHAPE_NODE)) {

        VrmlShape* shapeNode = static_cast<VrmlShape*>(node);
        short shapeInfoIndex;
        
        ShapeNodeToShapeInfoIndexMap::iterator p = shapeInfoIndexMap.find(shapeNode);
        if(p != shapeInfoIndexMap.end()){
            shapeInfoIndex = p->second;
        } else {
            shapeInfoIndex = createShapeInfo(shapeNode, url);
        }
        
        if(shapeInfoIndex >= 0){
            long length = io_shapeIndices.length();
            io_shapeIndices.length(length + 1);
            TransformedShapeIndex& tsi = io_shapeIndices[length];
            tsi.shapeIndex = shapeInfoIndex;
            int p = 0;
            for(int row=0; row < 3; ++row){
                for(int col=0; col < 4; ++col){
                    tsi.transformMatrix[p++] = T(row, col);
                }
            }
            if(inline_count)
                tsi.inlinedShapeTransformMatrixIndex=inlinedShapeM.length()-1;
            else
                tsi.inlinedShapeTransformMatrixIndex=-1;
        }
    }
}