我正在尝试使用多个凸多边形绘制一个凸面多面体py。我使用java3d的triangleFanArray来表示每个多边形。
package ucl.physiol.neuroconstruct.j3D;
import com.sun.j3d.utils.geometry.*;
import javax.vecmath.*;
import javax.media.j3d.*;
import quickhull3d.Point3d;
import java.io.*;
public class Polygon extends Primitive
{
int divisions;
//vertices are Point3d and the indices are integers
Point3d vertices[];
int faces[][];
int flags;
/**
* Returns the flags of primitive (generate normal, textures, caching, etc).
*/
@Override
public int getPrimitiveFlags()
{
return flags;
}
/*
public Polygon()
{
this(0.5f, 2.0f, GENERATE_NORMALS, DEFAULT_DIVISIONS, null);
}
*/
public Polygon (Point3d vertices[],int faces[][])
{
this(vertices, faces, GENERATE_NORMALS, null);
}
public Polygon (Point3d vertices[],int faces[][], Appearance ap)
{
this(vertices,faces, GENERATE_NORMALS, ap);
}
public Shape3D getShape(int partId)
{
if (partId >= this.faces.length || partId < 0) return null;
return (Shape3D) getChild(partId);
}
public void setAppearance(Appearance ap)
{
for(int i=0;i<faces.length;i++)
{
((Shape3D)getChild(i)).setAppearance(ap);
}
}
public Appearance getAppearance(int partId)
{
if (partId >= faces.length || partId < 0)return null;
return getShape(partId).getAppearance();
}
public Polygon(Point3d vert[], int fac[][], int primflags, Appearance ap)
{
super();
this.flags = primflags;
vertices=vert;
faces=fac;
Shape3D shape[] = new Shape3D[fac.length];
System.out.println("We are here inside Polygon1");
for(int i=0;i<fac.length;i++)
{
int k=faces[i].length;
javax.vecmath.Point3d face_vertices[]=new javax.vecmath.Point3d[k];
System.out.println(" Between 1 and 2");
for(int j=0;j<k;j++)
{
face_vertices[i]=new javax.vecmath.Point3d(vert[faces[i][j]].x,vert[faces[i][j]].y,vert[faces[i][j]].z);
}
System.out.println(face_vertices.length);
System.out.println("We are here inside Polygon2");
TriangleFanArray face = new TriangleFanArray(face_vertices.length,TriangleFanArray.COORDINATES |TriangleFanArray.NORMALS,new int[]{face_vertices.length});
System.out.println("We are between Polygon 2 and 3");
***face.setCoordinates(0, face_vertices);***
System.out.println("We are between Polygon 2 and 3");
shape[i] = new Shape3D(face);
this.addChild(shape[i]);
System.out.println("We are here inside Polygon3");
if ( (flags & ENABLE_APPEARANCE_MODIFY) != 0)
{
(shape[i]).setCapability(Shape3D.ALLOW_APPEARANCE_READ);
(shape[i]).setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
}
if ( (flags & ENABLE_GEOMETRY_PICKING) != 0)
{
(shape[i]).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
}
}
System.out.println("We are here inside Polygon4");
if (ap == null)
{
setAppearance();
}
else setAppearance(ap);
}
public Node cloneNode(boolean forceDuplicate) {
Polygon c = new Polygon(vertices, faces, flags, getAppearance());
c.duplicateNode(this, forceDuplicate);
return c;
}
public void duplicateNode(Node originalNode, boolean forceDuplicate) {
super.duplicateNode(originalNode, forceDuplicate);
}
}
调用TriangleFanArray.setCoordinates(int,Point3d)时出现NullPointerException。我无法找到任何理由。