E57 Foundation API v1.1.312  Aug. 10, 2011
Functions
examples/FloatCreate.cpp File Reference

example: creating FloatNodes More...

Include dependency graph for FloatCreate.cpp:

Functions

void printFloatInfo (ImageFile imf, ustring pathName)
 Get and print some info about a FloatNode.
int main (int, char **)
 Example use of FloatNode functions.

Detailed Description

example: creating FloatNodes

Also see listing at end of this page for source without line numbers (to cut&paste from).

00001 /*** FloatCreate.cpp example: creating FloatNodes */
00004 #include <iostream>
00005 #include "E57Foundation.h"
00006 using namespace e57;
00007 using namespace std;
00008 
00010 void printFloatInfo(ImageFile imf, ustring pathName)
00011 {
00012     cout << pathName << ":" << endl;
00013 
00014     StructureNode root = imf.root();
00015 
00016     if (root.isDefined(pathName)) {
00017         Node n = root.get(pathName);
00018         if (n.type() == E57_FLOAT) {
00019             FloatNode f = static_cast<FloatNode>(n);
00020             cout << "  value     = " << f.value()     << endl;
00021             cout << "  precision = " << f.precision() << endl;
00022             cout << "  minimum   = " << f.minimum()   << endl;
00023             cout << "  maximum   = " << f.maximum()   << endl;
00024         } else
00025             cout << "oops " << n.pathName() << " isn't an Float" << endl;
00026     }
00027 }
00028 
00030 int main(int /*argc*/, char** /*argv*/) {
00031     try {
00032         ImageFile imf("temp._e57", "w");
00033         StructureNode root = imf.root();
00034 
00035         // Create 7 example Floats 
00036         root.set("f1", FloatNode(imf));
00037         root.set("f2", FloatNode(imf, 123.45F));
00038         root.set("f3", FloatNode(imf, 123.45));
00039         root.set("f4", FloatNode(imf, 123.45F, E57_SINGLE));
00040         root.set("f5", FloatNode(imf, 123.45,  E57_SINGLE));
00041         root.set("f6", FloatNode(imf, 123.45,  E57_DOUBLE));
00042         root.set("f7", FloatNode(imf, 123.45,  E57_DOUBLE, 0.0, 1023.0));
00043 
00044         printFloatInfo(imf, "/f1");
00045         printFloatInfo(imf, "/f2");
00046         printFloatInfo(imf, "/f3");
00047         printFloatInfo(imf, "/f4");
00048         printFloatInfo(imf, "/f5");
00049         printFloatInfo(imf, "/f6");
00050         printFloatInfo(imf, "/f7");
00051 
00052         imf.close(); // don't forget to explicitly close the ImageFile
00053     } catch(E57Exception& ex) {
00054         ex.report(__FILE__, __LINE__, __FUNCTION__);
00055         return(-1);
00056     }
00057     return(0);
00058 }

This example program writes an ImageFile containing 7 FloatNodes. It then prints out some basic information about the state of each FloatNode. See the HelloWorld.cpp example for discussion of the use of include files, constructing an ImageFile, and the try/catch block to handle exceptions. Also see discussion in the IntegerCreate.cpp example concerning downcasting.

FloatNodes come in two different precisions: E57_SINGLE and E57_DOUBLE, that are 32 bit or 64 bit IEEE encoding respectively. Source lines 36-42 illustrate the use of the default arguments in the FloatNode constructor. Source line 36 specifies a 0.0 E57_DOUBLE, with largest possible double precision bounds. Because there is only one FloatNode constructor, source lines 37-38 both produce an E57_DOUBLE (the type of the second argument in source line 37 is promoted to double precision by the compiler). In source lines 39-40, the explicit use of the E57_SINGLE argument causes both FloatNodes to have minimum/maximum bounds set to the min/max representable by a single precision IEEE floating point number (see output listing lines 19-20 and 24-25). Source line 41 has same effect as source lines 37-38. Source line 42 produces a FloatNode with all three value/minimum/maximum numbers specified explicitly. Because of the potential confusion, it is recommended that form in source line 37 be avoided.

In source line 42, if the specified value had been chosen outside the given minimum/maximum bounds, an exception would have been thrown (since the ASTM spec requires the value be within the bounds).

On the XML line 5, the number is different than XML line 6, because of the conversion performed by the compiler on source line 37. Also note that XML lines 4-9 don't have to explicitly specify the minimum/maximum attributes since the API default values match those given in the ASTM spec.

The following console output is produced:

The XML section of the temp._e57 E57 file produced by this example program is as follows:

Here is the source code without line numbers to cut&paste from:

/*** FloatCreate.cpp example: creating FloatNodes */
#include <iostream>
#include "E57Foundation.h"
using namespace e57;
using namespace std;

void printFloatInfo(ImageFile imf, ustring pathName)
{
    cout << pathName << ":" << endl;

    StructureNode root = imf.root();

    if (root.isDefined(pathName)) {
        Node n = root.get(pathName);
        if (n.type() == E57_FLOAT) {
            FloatNode f = static_cast<FloatNode>(n);
            cout << "  value     = " << f.value()     << endl;
            cout << "  precision = " << f.precision() << endl;
            cout << "  minimum   = " << f.minimum()   << endl;
            cout << "  maximum   = " << f.maximum()   << endl;
        } else
            cout << "oops " << n.pathName() << " isn't an Float" << endl;
    }
}

int main(int /*argc*/, char** /*argv*/) {
    try {
        ImageFile imf("temp._e57", "w");
        StructureNode root = imf.root();

        // Create 7 example Floats 
        root.set("f1", FloatNode(imf));
        root.set("f2", FloatNode(imf, 123.45F));
        root.set("f3", FloatNode(imf, 123.45));
        root.set("f4", FloatNode(imf, 123.45F, E57_SINGLE));
        root.set("f5", FloatNode(imf, 123.45,  E57_SINGLE));
        root.set("f6", FloatNode(imf, 123.45,  E57_DOUBLE));
        root.set("f7", FloatNode(imf, 123.45,  E57_DOUBLE, 0.0, 1023.0));

        printFloatInfo(imf, "/f1");
        printFloatInfo(imf, "/f2");
        printFloatInfo(imf, "/f3");
        printFloatInfo(imf, "/f4");
        printFloatInfo(imf, "/f5");
        printFloatInfo(imf, "/f6");
        printFloatInfo(imf, "/f7");

        imf.close(); // don't forget to explicitly close the ImageFile
    } catch(E57Exception& ex) {
        ex.report(__FILE__, __LINE__, __FUNCTION__);
        return(-1);
    }
    return(0);
}
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines