edu.mines.jtk.sgl.PointGroup Maven / Gradle / Ivy
Show all versions of edu-mines-jtk Show documentation
/****************************************************************************
Copyright 2009, Colorado School of Mines and others.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/
package edu.mines.jtk.sgl;
import java.nio.FloatBuffer;
import static edu.mines.jtk.ogl.Gl.*;
import edu.mines.jtk.util.Direct;
/**
* A group of unstructured points.
* @author Dave Hale, Colorado School of Mines
* @version 2009.01.13
*/
public class PointGroup extends Group {
/**
* Constructs a points group with specified coordinates.
*
* The (x,y,z) coordinates of points are packed into the specified
* array xyz. The number of points is np = xyz.length/3.
* @param xyz array[3*np] of packed point coordinates.
*/
public PointGroup(float[] xyz) {
this(xyz,null);
}
/**
* Constructs a points group with specified coordinates and colors.
*
* The (x,y,z) coordinates of points are packed into the specified
* array xyz. The number of points is np = xyz.length/3.
*
* The (r,g,b) components of colors are packed into the specified
* array rgb. The number of colors equals the number of points.
* @param xyz array[3*np] of packed point coordinates.
* @param rgb array[3*np] of packed color components.
*/
public PointGroup(float[] xyz, float[] rgb) {
buildTree(xyz,rgb);
}
/**
* Constructs a points group with specified coordinates.
*
* The (x,y,z) coordinates of points are packed into the specified
* array xyz. The number of points is np = xyz.length/3.
* @param size size of cubes used to represent points.
* @param xyz array[3*np] of packed point coordinates.
*/
public PointGroup(float size, float[] xyz) {
this(size,xyz,null);
}
/**
* Constructs a points group with specified coordinates and colors.
*
* The (x,y,z) coordinates of points are packed into the specified
* array xyz. The number of points is np = xyz.length/3.
*
* The (r,g,b) components of colors are packed into the specified
* array rgb. The number of colors equals the number of points.
* @param size size of cubes used to represent points.
* @param xyz array[3*np] of packed point coordinates.
* @param rgb array[3*np] of packed color components.
*/
public PointGroup(float size, float[] xyz, float[] rgb) {
_size = size;
buildTree(xyz,rgb);
}
///////////////////////////////////////////////////////////////////////////
// private
// Constants for indexing packed arrays.
private static final int X = 0, Y = 1, Z = 2;
private static final int R = 0, G = 1, B = 2;
private static final int MIN_POINT_PER_NODE = 2048;
private float _size; // size of cubes used to represent points
/**
* Recursively builds a binary tree with leaf point nodes.
*/
private void buildTree(float[] xyz, float[] rgb) {
BoundingBoxTree bbt = new BoundingBoxTree(MIN_POINT_PER_NODE,xyz);
buildTree(this,bbt.getRoot(),xyz,rgb);
}
private void buildTree(
Group parent, BoundingBoxTree.Node bbtNode,
float[] xyz, float[] rgb)
{
if (bbtNode.isLeaf()) {
PointNode pn;
if (_size>0.0f) {
pn = new PointNode(bbtNode,_size,xyz,rgb);
} else {
pn = new PointNode(bbtNode,xyz,rgb);
}
parent.addChild(pn);
} else {
Group group = new Group();
parent.addChild(group);
buildTree(group,bbtNode.getLeft(),xyz,rgb);
buildTree(group,bbtNode.getRight(),xyz,rgb);
}
}
/**
* A leaf point node in the hierarchy of nodes for this group.
*/
private class PointNode extends Node {
public PointNode(
BoundingBoxTree.Node bbtNode, float[] xyz, float[] rgb)
{
BoundingBox bb = bbtNode.getBoundingBox();
_bs = new BoundingSphere(bb);
_np = bbtNode.getSize();
int np = _np;
int nv = np;
int nc = np;
int[] index = bbtNode.getIndices();
_vb = Direct.newFloatBuffer(3*nv);
_cb = (rgb!=null)?Direct.newFloatBuffer(3*nc):null;
for (int ip=0,iv=0,ic=0; ip0.0f) {
glDrawArrays(GL_QUADS,0,4*6*_np);
} else {
glDrawArrays(GL_POINTS,0,_np);
}
if (_cb!=null)
glDisableClientState(GL_COLOR_ARRAY);
if (_nb!=null)
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
private BoundingSphere _bs; // pre-computed bounding sphere
private int _np; // number of points
private FloatBuffer _vb; // vertex buffer
private FloatBuffer _nb; // normal buffer
private FloatBuffer _cb; // color buffer
}
}