You are here: Code Examples > Code Examples

# Code Examples

The MrSID Decode SDK includes code samples that demonstrate the use of the SDK's different interfaces.

The following C++ (.cpp) files are located in your examples/src directory.

Code example files and what they demonstrate
UserTutorial.cpp | Opening MG4 files Using the PointIterator to access the point cloud Using PointSource::read() to access a fixed number of points
DumpMG4Info.cpp | Accessing the point cloud properties Displaying metadata
DecodeMG4ToTXT.cpp | Using a PointWriter class
IterateOverPoints.cpp | Using a PointIterator Accessing channel values from a PointData object
support.cpp | Using the FileIO class
UserTest.cpp | Enables you to add your own test code to explore the SDK

Below, we walk through the UserTutorial.cpp example.

The following code opens an MG4 file:

    FileIO *file = FileIO::create();
    file->init("data/Tetons_200k.sid", "r");
    MG4PointReader *pointSource = MG4PointReader::create();
    pointSource->init(file);
    file->release();

Now that the file is initialized, you can access the properties of the point cloud using the following code:

    PointSource::count_type numPoints = pointSource->getNumPoints();
    size_t numChannels = pointSource->getNumChannels();
    const PointInfo &pointInfo = pointSource->getPointInfo();

    printf("Number of points: %lld\n", numPoints);
    printf("Number of channels: %lu\n", numChannels);
    for(size_t i = 0; i < numChannels; i += 1)
       printf("Channel %lu: %s\n",  i,  pointInfo.getChannel(i).getName());

You can use either of the following two methods to access the point cloud. In the first, we use the PointIterator mechanism.

    PointData buffer;
    // create buffers for all the channels 1000 samples long
    buffer.init(pointInfo, 1000);
    // create an iterator of the whole point cloud with all the channels
    PointIterator *iter = pointSource->createIterator(pointSource->getBounds(),
                                                      1.0,
                                                      pointInfo,
                                                      NULL);

    size_t count;
    // walk the iterator
    while((count = iter->getNextPoints(buffer)) != 0)
    {
       // do some thing with this chunk of the point cloud.
    }
    iter->release();

The second method extracts a fixed number of points (10,000 in this case):

    PointData buffer;
    {
       // only decode X, Y, Z
       PointInfo pointInfo;
       pointInfo.init(3);
       pointInfo.getChannel(0).init(*pointSource->getChannel(CHANNEL_NAME_X));
       pointInfo.getChannel(1).init(*pointSource->getChannel(CHANNEL_NAME_Y));
       pointInfo.getChannel(2).init(*pointSource->getChannel(CHANNEL_NAME_Z));
       buffer.init(pointInfo, 10000);
    }

    pointSource->read(Bounds::Huge(), buffer, NULL);
    // do some thing with the points

Now we'll do a little housecleaning. When you're done with your point source, you should release it:

    pointSource->release();
    pointSource = NULL;