Use a point set factory

From JReality Wiki
Jump to: navigation, search

Source file: Cube01

JavaDoc: PointSetFactory

Run as Java webstart

The best and recommended way to create a specific geometry is to use an appropriate factory. Each instance of a factory manages one unique instance of geometry. For example, if you need an IndexedFaceSet use the IndexedFaceSetFactory to create your geometry and to set certain properties. After making changes to the factory (using the various set... methods), call update() to bring the geometry instance up-to-date. Use the getGeometry() method (and its variants) to obtain the (immutable) geometry instance.

As an example we will build the unit cube, starting with its vertices and adding edges and faces gradually.

First of all, we'll create a PointSet containing the vertices of the unit square in xy-plane using the PointSetFactory. We must specify the number of vertices and their coordinates.

package de.jreality.tutorial;
public class Cube01 {
  public static void main(String[] args) {
    PointSetFactory psf = new PointSetFactory();
    double [][] vertices = new double[][] {
      {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}
    psf.setVertexCount( vertices.length );
    psf.setVertexCoordinates( vertices );

Data structures used by the factories

It's also possible to specify the vertices as an inlined array of type double[12]. The factory determines automatically the length of each vector by using the vertexCount value. This is true of all arrays which can be passed to the factories: there is a method which accepts two-dimensional arrays, and a corresponding one which accepts one-dimensional (inlined) arrays.

Furthermore, if you are working with homogeneous coordinates for vertices (with four values instead of three per vertex), you can specify the vertices either with an array of type double[4][4] or double[16]. This is also true for specifying other attributes, such as normals or colors (see below).

psf.getPointSet() returns the created geometry which we can now display using the Viewer Application (JRViewer).

Always call the update() method of a used factory before extracting geometry from it.


some points

Previous: Use geometry factories Developer Tutorial: Contents Next: Use an indexed line set factory