j3d.org Code

org.j3d.geom
Class SphereGenerator

java.lang.Object
  extended by org.j3d.geom.GeometryGenerator
      extended by org.j3d.geom.SphereGenerator

public class SphereGenerator
extends GeometryGenerator

A customisable sphere where you can specify the radius, center and the number of segments to use around the radius.

The sphere is located around the origin. Each face of the sphere is generated as a flat four-sided polygon suitable for use in an unindexed quad array.

Due to a focus on the generation speed, we require that the facet count is always a multiple of 2. The code generates half a sphere and then mirrors it to generate the other half. Having an odd number of facets makes this impossbile to do and we must do a lot more generation. Although it is simple enough to change the algorithm to allow odd numbers of facets, the trade-off is that we loose the ability to nicely generate hemispheres. The result is the same number of facets around the base as over the top half part and that looks like crap as it does not look exactly like half a full generated sphere of the same setup.

The sphere surface is always using smooth normals and every point is such that the normal points directly away from the origin.

Version:
$Revision: 1.12 $
Author:
Justin Couch

Constructor Summary
SphereGenerator()
          Constructs a default sphere of radius 1 and uses 16 segments on the radius.
SphereGenerator(float r)
          Constructs a sphere of the given radius and uses 16 segments on the radius.
SphereGenerator(float r, boolean useHalf)
          Constructs a sphere or hemi-sphere of the given radius and uses 16 segments on the radius.
SphereGenerator(float r, int facets)
          Constructs a sphere of the given radius and controllable number of segments on the radius.
SphereGenerator(float r, int facets, boolean half)
          Constructs a sphere or hemi-sphere of the given radius and controllable number of segments on the radius.
 
Method Summary
 void generate(GeometryData data)
          Generate a new set of geometry items based on the passed data.
 float getDimension()
          Get the dimensions of the sphere.
 int getVertexCount(GeometryData data)
          Get the number of vertices that this generator will create for the shape given in the definition.
 boolean isHalf()
          Check to see that this sphere is a half sphere or not
 void setDimensions(float r, boolean half)
          Change the dimensions of the sphere to be generated.
 void setFacetCount(int facets)
          Change the number of facets used to create this cone.
 
Methods inherited from class org.j3d.geom.GeometryGenerator
createFaceNormal, createFaceNormal, createRadialNormal, createRadialNormal
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SphereGenerator

public SphereGenerator()
Constructs a default sphere of radius 1 and uses 16 segments on the radius.


SphereGenerator

public SphereGenerator(float r)
Constructs a sphere of the given radius and uses 16 segments on the radius.

Parameters:
r - The radius of the sphere

SphereGenerator

public SphereGenerator(float r,
                       boolean useHalf)
Constructs a sphere or hemi-sphere of the given radius and uses 16 segments on the radius.

Parameters:
r - The radius of the sphere
useHalf - True to generate a hemi-sphere, false for full spher

SphereGenerator

public SphereGenerator(float r,
                       int facets)
Constructs a sphere of the given radius and controllable number of segments on the radius. The minimum number of facets is 3.

Parameters:
r - The radius of the sphere
facets - The number of facets to use
Throws:
java.lang.IllegalArgumentException - The number of facets is less than 3

SphereGenerator

public SphereGenerator(float r,
                       int facets,
                       boolean half)
Constructs a sphere or hemi-sphere of the given radius and controllable number of segments on the radius. The minimum number of facets is 3.

Parameters:
r - The radius of the sphere
facets - The number of facets to use
half - True to generate a hemi-sphere, false for full sphere
Throws:
java.lang.IllegalArgumentException - The number of facets is less than 4 or not divisible by 2.
Method Detail

isHalf

public boolean isHalf()
Check to see that this sphere is a half sphere or not

Returns:
true if there is only a has sphere

getDimension

public float getDimension()
Get the dimensions of the sphere. These are returned as 2 values of height and radius respectively for the array. A new array is created each time so you can do what you like with it.

Returns:
The current size of the sphere

setDimensions

public void setDimensions(float r,
                          boolean half)
Change the dimensions of the sphere to be generated. Calling this will make the points be re-calculated next time you ask for geometry or normals.

Parameters:
r - The radius of the sphere
half - True to generate a hemi-sphere, false for full spher

setFacetCount

public void setFacetCount(int facets)
                   throws java.lang.IllegalArgumentException
Change the number of facets used to create this cone. This will cause the geometry to be regenerated next time they are asked for. The minimum number of facets is 3.

Parameters:
facets - The number of facets on the side of the cone
Throws:
java.lang.IllegalArgumentException - The number of facets is less than 4 or not divisible by 2.

getVertexCount

public int getVertexCount(GeometryData data)
                   throws UnsupportedTypeException
Get the number of vertices that this generator will create for the shape given in the definition.

Specified by:
getVertexCount in class GeometryGenerator
Parameters:
data - The data to base the calculations on
Returns:
The vertex count for the object
Throws:
UnsupportedTypeException - The generator cannot handle the type of geometry you have requested.

generate

public void generate(GeometryData data)
              throws UnsupportedTypeException,
                     InvalidArraySizeException
Generate a new set of geometry items based on the passed data. If the data does not contain the right minimum array lengths an exception will be generated. If the array reference is null, this will create arrays of the correct length and assign them to the return value.

Specified by:
generate in class GeometryGenerator
Parameters:
data - The data to base the calculations on
Throws:
InvalidArraySizeException - The array is not big enough to contain the requested geometry
UnsupportedTypeException - The generator cannot handle the type of geometry you have requested

j3d.org Code

Latest Info from http://code.j3d.org/
Copyright © 2001 - j3d.org