Difference between revisions of "XB PointStream/custom parsers"

From CDOT Wiki
Jump to: navigation, search
(Created page with ' ===Parser Interface=== There will be cases in which users have their own file format which they want to render with XB PointStream (for example, .ARA or .XML). We need to give …')
 
(Parser Interface)
Line 2: Line 2:
 
===Parser Interface===
 
===Parser Interface===
  
There will be cases in which users have their own file format
+
Currently XBPS only supports reading .ASC file types. If you require the library to render other files, you will need to write a custom parser as register it with the library. This should not be difficult, there are only a few things your parser must implement.
which they want to render with XB PointStream (for example,
 
.ARA or .XML). We need to give users the ability to write their
 
own parser and hook it into the library.
 
  
Users would write their JavaScript code which would implement
+
We have already created a demonstration of this. You can find it [http://# here].
the methods below. Once they do that, they would register their
 
parser with the library by passing in an extension and their parser.
 
  
The library would then take care of the rest by creating an instance
+
Here is a skeleton of a parser you could take and implement:
of their parser, call its methods and return a point cloud object.
 
  
 
<pre>
 
<pre>
/*
+
var Your_Parser_Name = (function() {
   The constructor of the parser.
+
  /**
 
+
   XBPS will create an instance of your parser and pass in an object with three
   @param {Object} obj - collection of named functions
+
   named properties:
 
+
    
  These functions pass the parser back to the library since the library could
 
   be working with many parsers simultaneously.
 
 
 
 
   start - must occur exactly once. Has one argument, the parser itself
 
   start - must occur exactly once. Has one argument, the parser itself
 
   end  - must occur exactly once. Has one argument, the parser itself
 
   end  - must occur exactly once. Has one argument, the parser itself
  parse - may occur one or many times. Has two arguments, the parser itself and a
 
  named collection of value types.
 
 
  See below for an example
 
 
*/
 
Constructor(obj)
 
 
/*
 
  Begins to load the resource.
 
 
  @param {String} path - path to resource
 
*/
 
load(path)
 
 
  
////// Getters
+
  Everytime your parser calls the parse function, it must send in an object with
 +
  named arrays. In the following example, vertexArray is a Float32Array which contain
 +
  the vertices which have already been read and parsed.
  
/*
+
  var attributes = {};
   Get the version of this parser.
+
  attributes["ps_Vertex"] = vertexArray;
 +
   attributes["ps_Color"] = colorsArray;
 
   
 
   
   @returns {String} parser version
+
   parse - may occur one or many times. Has two arguments, the parser itself and a
*/
+
 
version
+
  Your parser is responsible for calling these functions at the appropriate times.
 
+
  */
/*
+
  function Your_Parser_Name(config) {
  Get the number of points which have been parsed.
+
 
 
+
    /*Returns the version of this parser.*/
  @returns {Number} the number of points parsed so far by the parser.
+
    this.__defineGetter__("version", function(){
*/
+
      return /*!!*/;
numParsedPoints
+
    });
 
+
 
/*
+
    /*Get the number of parsed points so far.*/
  Get the total number of points in the point cloud, including points
+
    this.__defineGetter__("numParsedPoints", function(){
  which have not yet been parsed.
+
      return /*!!*/;
 
+
    });
  @returns {Number} the total number of points in the resource or -1 if unknown.
+
 
*/
+
    /*Get the total number of points in the point cloud.*/
numTotalPoints
+
    this.__defineGetter__("numTotalPoints", function(){
 
+
      return /*!!*/;
/*
+
    });
  Get the progress of the parser, how much it has parsed so far.
+
 
 
+
    /*Returns the progress of downloading the point cloud between zero and one.*/
  @returns {Number} value between 0 to 1 or -1 if unknown.
+
    this.__defineGetter__("progress", function(){
*/
+
      return /*!!*/;
progress
+
    });
 +
 
 +
    /*Returns the file size of the resource in bytes.*/
 +
    this.__defineGetter__("fileSize", function(){
 +
      return /*!!*/;
 +
    });
 +
 
 +
    /**
 +
      @param path Path to the resource
 +
    */
 +
    this.load = function(path){
 +
      /*!!*/
 +
    };
 +
  }
 +
  return Your_Parser_Name;
 +
}());
 +
</pre>
  
/*
 
  The size of the resource in bytes.
 
  
  @returns {Number} the number of bytes in the resource or -1 if unknown.
 
*/
 
fileSize
 
</pre>
 
  
 
<pre>
 
<pre>
 
/*
 
/*
   The following example demonstrates how XB PointStream might use
+
   The following example demonstrates how XBPS might use
 
   a particular parser.
 
   a particular parser.
 
*/
 
*/
Line 105: Line 94:
  
 
// create a hypothetical parser and set the callbacks
 
// create a hypothetical parser and set the callbacks
parser = new XYZParser({ start: startCallback,
+
parser = new Your_Parser_Name({ start: startCallback,
                        parse: parseCallback,
+
                                parse: parseCallback,
                        end: finishCallback});
+
                                end: finishCallback});
 
// load some resource
 
// load some resource
 
parser.load("pointcloud.xyz");
 
parser.load("pointcloud.xyz");
 
</pre>
 
</pre>

Revision as of 18:37, 14 February 2011

Parser Interface

Currently XBPS only supports reading .ASC file types. If you require the library to render other files, you will need to write a custom parser as register it with the library. This should not be difficult, there are only a few things your parser must implement.

We have already created a demonstration of this. You can find it here.

Here is a skeleton of a parser you could take and implement:

var Your_Parser_Name = (function() {
  /**
  XBPS will create an instance of your parser and pass in an object with three
  named properties:
  
  start - must occur exactly once. Has one argument, the parser itself
  end   - must occur exactly once. Has one argument, the parser itself

  Everytime your parser calls the parse function, it must send in an object with
  named arrays. In the following example, vertexArray is a Float32Array which contain
  the vertices which have already been read and parsed.

  var attributes = {};
  attributes["ps_Vertex"] = vertexArray;
  attributes["ps_Color"] = colorsArray;
 
  parse - may occur one or many times. Has two arguments, the parser itself and a
  
   Your parser is responsible for calling these functions at the appropriate times.
  */
  function Your_Parser_Name(config) {
   
    /*Returns the version of this parser.*/
    this.__defineGetter__("version", function(){
      return /*!!*/;
    });
   
    /*Get the number of parsed points so far.*/
    this.__defineGetter__("numParsedPoints", function(){
      return /*!!*/;
    });
   
    /*Get the total number of points in the point cloud.*/
    this.__defineGetter__("numTotalPoints", function(){
      return /*!!*/;
    });
   
    /*Returns the progress of downloading the point cloud between zero and one.*/
    this.__defineGetter__("progress", function(){
      return /*!!*/;
    });
   
    /*Returns the file size of the resource in bytes.*/
    this.__defineGetter__("fileSize", function(){
      return /*!!*/;
    });
   
    /**
      @param path Path to the resource
    */
    this.load = function(path){
      /*!!*/
    };
  }
  return Your_Parser_Name;
}());


/*
  The following example demonstrates how XBPS might use
  a particular parser.
*/

var parser;

function startCallback(parser){
  // started
}

function parseCallback(parser, attributes){
  parser.version;
  parser.numParsedPoints;
  parser.numTotalPoints;
  parser.progress;
  parser.fileSize;
}

function finishCallback(parser){
  // finished
}

// create a hypothetical parser and set the callbacks
parser = new Your_Parser_Name({ start: startCallback,
                                parse: parseCallback,
                                end: finishCallback});
// load some resource
parser.load("pointcloud.xyz");