package jpbrt.shapes;
import java.util.ArrayList;
import java.util.List;
import jpbrt.core.BBox;
import jpbrt.core.Normal;
import jpbrt.core.Point;
import jpbrt.core.Shape;
import jpbrt.core.Texture;
import jpbrt.core.Transform;
import jpbrt.core.Vector;
public class TriangleMesh extends Shape
{
protected int[] vertexIndices;
protected Point[] p;
protected Normal[] n;
protected Vector[] s;
protected double[] uvs;
protected Texture<Float> alphaTex;
public TriangleMesh(Transform o2w, Transform w2o, boolean ro,
int[] vi, Point[] p)
{
this(o2w, w2o, ro, vi, p, null, null, null, null);
}
public TriangleMesh(Transform o2w, Transform w2o, boolean ro,
int[] vi, Point[] p, Normal[] n, Vector[] s, double[] uvs, Texture<Float> atex)
{
super(o2w, w2o, ro);
assert(vi.length % 3 == 0);
this.vertexIndices = new int[vi.length];
System.arraycopy(vi, 0, vertexIndices, 0, vi.length);
// transform mesh vertices to world space
assert(p.length > 0);
this.p = new Point[p.length];
for (int i = 0; i < p.length; i++)
this.p[i] = objectToWorld.transform(p[i]);
// copy uv, n, s vertex data, if present
if (n != null)
{
assert(n.length == p.length);
this.n = new Normal[n.length];
for (int i = 0; i < n.length; i++)
this.n[i] = n[i].clone();
}
if (s != null)
{
assert(s.length == p.length);
this.s = new Vector[s.length];
for (int i = 0; i < s.length; i++)
this.s[i] = s[i].clone();
}
if (uvs != null)
{
assert(uvs.length == p.length);
this.uvs = new double[uvs.length];
System.arraycopy(uvs, 0, this.uvs, 0, uvs.length);
}
this.alphaTex = atex;
}
@Override
public BBox objectBound()
{
BBox bbox = new BBox();
for (Point pt : p)
bbox.unionLocal( worldToObject.transform(pt) );
return bbox;
}
@Override
public BBox worldBound()
{
BBox bbox = new BBox();
for (Point pt : p)
bbox.unionLocal(pt);
return bbox;
}
@Override
public boolean canIntersect()
{
return false;
}
@Override
public List<Shape> refine()
{
List<Shape> refined = new ArrayList<Shape>();
for (int i = 0; i < vertexIndices.length; i += 3)
{
Triangle tri = new Triangle(objectToWorld, worldToObject, reverseOrientation, this, i);
refined.add(tri);
}
return refined;
}
}