示例#1
0
void ScenarioRenderer::loadGroundBasedComplexModel(ComplexModel &cm) {
    SharedPointer<istream> in = m_scnxArchive->getModelData(cm.getModelFile());
    if (in.isValid()) {
        // Load model.
        OBJXArchive *objxArchive = NULL;
        if (cm.getModelFile().find(".objx") != string::npos) {
            objxArchive = OBJXArchiveFactory::getInstance().getOBJXArchive(*in);
        }
        else if (cm.getModelFile().find(".obj") != string::npos) {
            objxArchive = OBJXArchiveFactory::getInstance().getOBJXArchiveFromPlainOBJFile(*in);
        }

        if (objxArchive != NULL) {
            m_listOfLoadedOBJXArchives.push_back(objxArchive);

            vector<TriangleSet> listOfTriangleSets = objxArchive->getListOfTriangleSets();

            if (listOfTriangleSets.size() > 0) {
                clog << "OBJ model successfully opened (containing " << listOfTriangleSets.size() << " sets of triangles)." << endl;
                clog << "  Translation: " << cm.getPosition().toString() << endl;
                clog << "  Rotation: " << cm.getRotation().toString() << endl;

                m_mapOfGroundBasedComplexModels[cm.toString()] = listOfTriangleSets;
            }
            else {
                clog << "OBJ model could not be opened." << endl;
            }
        }
    }
}
        TransformGroup* GroundBasedComplexModelLoader::getGroundBasedComplexModels(const SCNXArchive &scnxArchive) const {
            TransformGroup *complexModels = new TransformGroup();

            // Get list of all ground based complex models.
            vector<ComplexModel*> listOfComplexModels = scnxArchive.getListOfGroundBasedComplexModels();

            // Iterate over all ground based complex models and try to build a transform group.
            vector<ComplexModel*>::iterator jt = listOfComplexModels.begin();
            while (jt != listOfComplexModels.end()) {
                ComplexModel *cm = (*jt++);
                SharedPointer<istream> in = scnxArchive.getModelData(cm->getModelFile());
                if (in.isValid()) {
                    Node *model = NULL;

                    // Check model.
                    OBJXArchive *objxArchive = NULL;
                    if (cm->getModelFile().find(".objx") != string::npos) {
                        objxArchive = OBJXArchiveFactory::getInstance().getOBJXArchive(*in);
                    } else if (cm->getModelFile().find(".obj") != string::npos) {
                        objxArchive = OBJXArchiveFactory::getInstance().getOBJXArchiveFromPlainOBJFile(*in);
                    }

                    if (objxArchive != NULL) {
                        model = objxArchive->createTransformGroup(NodeDescriptor(cm->getName()));

                        if (model != NULL) {
                            clog << "OBJ model successfully opened." << endl;
                            clog << "  Translation: " << cm->getPosition().toString() << endl;
                            clog << "  Rotation: " << cm->getRotation().toString() << endl;

                            TransformGroup *complexModel = new TransformGroup();

                            // Translation.
                            Point3 translation(cm->getPosition());
                            complexModel->setTranslation(translation);

                            // TODO: Achsenprüfung!!
                            Point3 rotation(cm->getRotation().getX(), cm->getRotation().getZ(), cm->getRotation().getY());
                            complexModel->setRotation(rotation);

                            complexModel->addChild(model);

                            complexModels->addChild(complexModel);

                        } else {
                            clog << "OBJ model could not be opened." << endl;
                        }

                        OPENDAVINCI_CORE_DELETE_POINTER(objxArchive);
                    }
                }
            }

            return complexModels;
        }
void ManagedClientModule::setContainerConference(SharedPointer<core::io::conference::ContainerConference> c) {
    if (m_hasExternalContainerConference) {
        m_containerConference.release();
        m_hasExternalContainerConference = false;
    }

    if (c.isValid()) {
        m_containerConference = c;
        m_hasExternalContainerConference = true;
    }
}
        SCNXArchive& SCNXArchiveFactory::getSCNXArchive(const URL &url) throw (InvalidArgumentException) {
            if (!(url.isValid())) {
                OPENDAVINCI_CORE_THROW_EXCEPTION(InvalidArgumentException, "URL is invalid.");
            }

            SCNXArchive *scnxArchive = NULL;

            // Try to find an existing SCNXArchive in the map using the URL as key.
            map<string, SCNXArchive*, core::strings::StringComparator>::iterator it = m_mapOfSCNXArchives.find(url.toString());
            if (it != m_mapOfSCNXArchives.end()) {
            	clog << "Found already constructed data structure." << endl;
                scnxArchive = it->second;
            }

            if (scnxArchive == NULL) {
                clog << "Creating new SCNXArchive from " << url.toString() << endl;

                string fileName = url.getResource();
                fstream fin(fileName.c_str(), ios::binary | ios::in);
                core::SharedPointer<core::wrapper::DecompressedData> data = core::wrapper::CompressionFactory::getContents(fin);
                fin.close();

                if (data.isValid()) {
                    Scenario scenario;
                    SharedPointer<istream> stream = data->getInputStreamFor("scenario.scn");
                    if (stream.isValid()) {
                        stringstream s;
                        char c;
                        while (stream->good()) {
                            stream->get(c);
                            s << c;
                        }

                        // Trying to parse the input.
                        scenario = ScenarioFactory::getInstance().getScenario(s.str());
                    } else {
                        OPENDAVINCI_CORE_THROW_EXCEPTION(InvalidArgumentException, "Archive from the given URL does not contain a valid SCN file.");
                    }

                    // Create SCNXArchive.
                    scnxArchive = new SCNXArchive(scenario, data);

                    // Store SCNXArchive for further usage.
                    // Somehow, there seems to be a bug because the data structure got corrupt...
//                    m_mapOfSCNXArchives[url.toString()] = scnxArchive;
                }
                else {
                    OPENDAVINCI_CORE_THROW_EXCEPTION(InvalidArgumentException, "URL could not be used to read input data.");
                }
            }

            return *scnxArchive;
        }
            AbstractConferenceClientModule::AbstractConferenceClientModule(const int32_t &argc, char **argv, const string &name) throw (InvalidArgumentException) :
                    ManagedClientModule(argc, argv, name) {
                SharedPointer<ContainerConference> containerConference = ContainerConferenceFactory::getInstance().getContainerConference(getMultiCastGroup());
                if (!containerConference.isValid()) {
                    OPENDAVINCI_CORE_THROW_EXCEPTION(InvalidArgumentException, "ContainerConference invalid!");
                }
                setContainerConference(containerConference);

                // Register ourselves as ContainerListener.
                getContainerConference()->setContainerListener(this);
                
                SerializationFactory::getInstance();
            }
            OBJXArchive* OBJXArchiveFactory::getOBJXArchive(istream &in) throw (InvalidArgumentException) {
                if (!(in.good())) {
                    // Try to rewind the stream.
                    clog << "Trying to rewind the stream." << endl;
                    in.clear();
                    in.seekg(ios::beg);

                    if (!(in.good())) {
                        OPENDAVINCI_CORE_THROW_EXCEPTION(InvalidArgumentException, "Given inputstream is invalid.");
                    }
                }

                OBJXArchive *objxArchive = NULL;

                // Use CompressionFactory to read the contents of the OBJXArchive.
                SharedPointer<core::wrapper::DecompressedData> data = core::wrapper::CompressionFactory::getContents(in);

                if (data.isValid()) {
                    // Create OBJXArchive.
                    objxArchive = new OBJXArchive();

                    vector<string> listOfEntries = data->getListOfEntries();
                    vector<string>::iterator it = listOfEntries.begin();

                    while (it != listOfEntries.end()) {
                        string entry = (*it++);

                        if (entry.find(".obj") != string::npos) {
                            // Set object file.
                            SharedPointer<istream> stream = data->getInputStreamFor(entry);
                            if (stream.isValid()) {
                                char c;
                                stringstream s;
                                while (stream->good()) {
                                    stream->get(c);
                                    s << c;
                                }
                                objxArchive->setContentsOfObjFile(s.str());
                            }
                        } else if (entry.find(".mtl") != string::npos) {
                            // Set material file.
                            SharedPointer<istream> stream = data->getInputStreamFor(entry);
                            if (stream.isValid()) {
                                char c;
                                stringstream s;
                                while (stream->good()) {
                                    stream->get(c);
                                    s << c;
                                }
                                objxArchive->setContentsOfMtlFile(s.str());
                            }
                        } else {
                            // Try to load an image.
                            SharedPointer<istream> stream = data->getInputStreamFor(entry);

                            if (stream.isValid()) {
                                core::wrapper::Image *image = core::wrapper::ImageFactory::getInstance().getImage(*stream);

                                if (image != NULL) {
                                    // TODO: Check where origin lies.
                                    image->rotate(static_cast<float>(cartesian::Constants::PI));

                                    // Remove any directory prefixes from the entry.
                                    string name = entry;
                                    if (name.rfind('/') != string::npos) {
                                        name = name.substr(name.rfind('/') + 1);
                                    }

                                    objxArchive->addImage(name, image);
                                }
                            }
                        }

                    }
                }

                return objxArchive;
            }
int32_t main(int32_t argc, char **argv)
{
    uint32_t retVal = 0;
    int recIndex=1;
    bool log=false;
    
    if((argc != 2 && argc != 3 && argc != 4) || (argc==4 && string(argv[1]).compare("-l")!=0))
    {
        errorMessage(string(argv[0]));
        retVal = 1;
    }
    else if(argc==2 && string(argv[1]).compare("-h")==0)
    {
        helpMessage(string(argv[0]));
        retVal = 0;
    }
    else
    {
        // if -l option is set
        if(argc==4 || (argc==3 && string(argv[1]).compare("-l")==0))
        {
            ++recIndex;
            log=true;
        }
        
        // Use command line parameter as file for playback;
        string recordingFile(argv[recIndex]);
        stringstream recordingFileUrl;
        recordingFileUrl << "file://" << recordingFile;

        // Location of the recording file.
        URL url(recordingFileUrl.str());

        // Do we want to rewind the stream on EOF?
        const bool AUTO_REWIND = false;

        // Size of the memory buffer that should fit at least the size of one frame.
        const uint32_t MEMORY_SEGMENT_SIZE = 1024 * 768;

        // Number of memory segments (one is enough as we are running sychronously).
        const uint32_t NUMBER_OF_SEGMENTS = 1;

        // Run player in synchronous mode without data caching in background.
        const bool THREADING = false;

        // Construct the player.
        Player player(url, AUTO_REWIND, MEMORY_SEGMENT_SIZE, NUMBER_OF_SEGMENTS, THREADING);

        // The next container from the recording.
        Container nextContainer;

        // Using OpenCV's IplImage data structure to simply playback the data.
        IplImage *image = NULL;

        // Create the OpenCV playback window.
        cvNamedWindow("CaroloCup-CameraPlayback", CV_WINDOW_AUTOSIZE);

        // This flag indicates whether we have attached already to the shared
        // memory containing the sequence of captured images.
        bool hasAttachedToSharedImageMemory = false;

        // Using this variable, we will access the captured images while
        // also having convenient automated system resource management.
        SharedPointer<SharedMemory> sharedImageMemory;

        ifstream file(argv[recIndex+1]);
        CSVRow row;
        // read out the header row
        row.readNextRow(file);
        uint32_t frameNumber=1, csvFN;
        int32_t VPx,VPy,BLx,BLy,BRx,BRy,TLx,TLy,TRx,TRy;
        stringstream frameMessage;
        stringstream VPMessage;
        frameMessage.str(string());
        VPMessage.str(string());
        bool fbf=false;
        
        // Main data processing loop.
        while (player.hasMoreData()) {
            // Read next entry from recording.
            nextContainer = player.getNextContainerToBeSent();

            // Data type SHARED_IMAGE contains a SharedImage data structure that
            // provides meta-information about the captured image.
            if (nextContainer.getDataType() == Container::SHARED_IMAGE) {
                // Read the data structure to retrieve information about the image.
                SharedImage si = nextContainer.getData<SharedImage>();

                // Check if we have already attached to the shared memory.
                if (!hasAttachedToSharedImageMemory) {
                    sharedImageMemory = SharedMemoryFactory::attachToSharedMemory(si.getName());

                    // Toggle the flag as we have now attached to the shared memory.
                    hasAttachedToSharedImageMemory = true;
                }

                // Check if we could successfully attach to the shared memory.
                if (sharedImageMemory->isValid()) {
                    // Using a scoped lock to get exclusive access.
                    {
                        Lock l(sharedImageMemory);
                        if (image == NULL) {
                            // Create the IplImage header data and access the shared memory for the actual image data. 
                            image = cvCreateImageHeader(cvSize(si.getWidth(), si.getHeight()), IPL_DEPTH_8U, si.getBytesPerPixel());

                            // Let the IplImage point to the shared memory containing the captured image.
                            image->imageData = static_cast<char*>(sharedImageMemory->getSharedMemory());
                        }
                    }

                    // Show the image using OpenCV.
                    
                    // if csv parameter is set
                    if(argc==4 || (argc==3 && string(argv[1]).compare("-l")!=0))
                    {
                        if(! row.readNextRow(file)) break;
                        while(row[0].compare("")==0)
                            if(! row.readNextRow(file)) break;
                        
                        sscanf(row[0].c_str(), "%d", &csvFN);
                        
                        if(frameNumber==csvFN)
                        {
                            Mat img = cvarrToMat(image);
                            
                        
                            frameMessage.str(string());
                            VPMessage.str(string());
                            sscanf(row[9].c_str(), "%d", &VPx);
                            sscanf(row[10].c_str(), "%d", &VPy);
                            
                            frameMessage<<"Frame "<<frameNumber;
                            VPMessage<<"Vanishing Point ("<<VPx<<","<<VPy<<")";
                            
                            setLabel(img, frameMessage.str(), cvPoint(30,45));
                            setLabel(img, VPMessage.str(), cvPoint(30,60));
                            
                            if(log)
                                cout << frameNumber << ", " << VPx << ", " << VPy <<endl;
                            
                            // print support points and lines
                            sscanf(row[1].c_str(), "%d", &BLx);
                            sscanf(row[2].c_str(), "%d", &BLy);BLy+=60;
                            sscanf(row[3].c_str(), "%d", &TLx);
                            sscanf(row[4].c_str(), "%d", &TLy);TLy+=60;
                            sscanf(row[5].c_str(), "%d", &TRx);
                            sscanf(row[6].c_str(), "%d", &TRy);TRy+=60;
                            sscanf(row[7].c_str(), "%d", &BRx);
                            sscanf(row[8].c_str(), "%d", &BRy);BRy+=60;
                            
                            circle(img, Point(BLx,BLy), 5, CV_RGB(255, 255, 255), CV_FILLED);
                            circle(img, Point(TLx,TLy), 5, CV_RGB(255, 255, 255), CV_FILLED);
                            circle(img, Point(TRx,TRy), 5, CV_RGB(255, 255, 255), CV_FILLED);
                            circle(img, Point(BRx,BRy), 5, CV_RGB(255, 255, 255), CV_FILLED);
                            
                            double slope1 = static_cast<double>(TLy-BLy)/static_cast<double>(TLx-BLx);
                            double slope2 = static_cast<double>(TRy-BRy)/static_cast<double>(TRx-BRx);
                            Point p1(0,0), q1(img.cols,img.rows);
                            Point p2(0,0), q2(img.cols,img.rows);
                            p1.y = -(BLx-p1.x) * slope1 + BLy;
                            q1.y = -(TLx-q1.x) * slope1 + TLy;
                            p2.y = -(BRx-p2.x) * slope2 + BRy;
                            q2.y = -(TRx-q2.x) * slope2 + TRy;
                            
                            line(img,p1,q1,CV_RGB(255, 255, 255),1,CV_AA);
                            line(img,p2,q2,CV_RGB(255, 255, 255),1,CV_AA);
                            
                            imshow("CaroloCup-CameraPlayback", img);
                        }
                    }
                    else
                        cvShowImage("CaroloCup-CameraPlayback", image);

                    // Let the image render before proceeding to the next image.
                    char c = cvWaitKey(10);
                    // Check if the user wants to stop the replay by pressing ESC or pause it by pressing SPACE (needed also to go frame-by-frame).
                    if (static_cast<uint8_t>(c) == 27) break;
                    else if (static_cast<uint8_t>(c) == 32 || fbf) {
                        do
                        {
                            c = cvWaitKey();
                        }while(c!='n' && static_cast<uint8_t>(c) != 32 && static_cast<uint8_t>(c) != 27);
                        
                        if (static_cast<uint8_t>(c) == 27) break; // ESC
                        else if (static_cast<uint8_t>(c) == 32) fbf=false; // SPACE -> continue
                        else if (c=='n') fbf=true; // pressed 'n' -> next frame
                    }
                    
                    ++frameNumber;
                }
            }
        }

        // maybe print EOF message && wait for user input?

        // Release IplImage data structure.
        cvReleaseImage(&image);

        // Close playback window.
        cvDestroyWindow("CaroloCup-CameraPlayback");

        // The shared memory will be automatically released.
    }

    // Return error code.
    return retVal;
}