|
import javax.media.j3d.*;
|
import com.sun.j3d.loaders.Scene;
|
//import com.jme.math.*;
|
//import com.jme.scene.state.MaterialState;
|
//import com.jme.scene.state.TextureState;
|
//import com.jme.renderer.ColorRGBA;
|
|
import javax.vecmath.*;
|
//import java.awt.Color;
|
//import java.awt.color.ColorSpace;
|
|
class cJ3D
|
{
|
static boolean gennormals = false; // can be done through UI
|
static boolean trim = true;
|
static boolean stripify = true;
|
static boolean genUV = false;
|
|
Matrix4d tempmat = new Matrix4d();
|
Matrix4d tempmatR = new Matrix4d();
|
Matrix4d tempmatS = new Matrix4d();
|
Matrix4d tempmatT = new Matrix4d();
|
Quat4d quat = new Quat4d();
|
javax.vecmath.Vector3d trans = new javax.vecmath.Vector3d();
|
|
void ResetTransform(Object3D obj, Node s)
|
{
|
if (s instanceof TransformGroup)
|
{
|
ResetTransform(obj, (TransformGroup)s);
|
return;
|
}
|
|
if (s instanceof Group)
|
{
|
Group g = (Group) s;
|
assert(obj.size() == g.numChildren());
|
for (int i=0; i<g.numChildren(); i++)
|
{
|
ResetTransform(obj.get(i), g.getChild(i));
|
}
|
}
|
}
|
|
Object3D Read(Node s)
|
{
|
// System.out.println("Parsing: " + s);
|
|
if (s == null)
|
return new Object3D("NULL");
|
|
if (s instanceof Shape3D)
|
return Read((Shape3D)s);
|
if (s instanceof TransformGroup)
|
return Read((TransformGroup)s);
|
if (s instanceof Group)
|
return Read((Group)s);
|
// if (s instanceof com.jme.scene.TriMesh)
|
// return Read((com.jme.scene.TriMesh)s);
|
//if (s instanceof SharedNode)
|
//return Read(((SharedNode)s).getChild());
|
|
System.out.println("Unknown node class : " + s.getClass().getSimpleName());
|
|
return new Object3D(s.getClass().getName());
|
}
|
|
public int count = 0;
|
|
/*
|
void ReadTransform(Object3D c, Spatial g)
|
{
|
tempmat.setIdentity();
|
tempmatR.setIdentity();
|
tempmatS.setIdentity();
|
tempmatT.setIdentity();
|
|
Quaternion q = g.getLocalRotation();
|
quat.set(q.x, q.y, q.z, q.w);
|
com.jme.math.Vector3f s = g.getLocalScale();
|
com.jme.math.Vector3f t = g.getLocalTranslation();
|
trans.set(t.x, t.y, t.z);
|
|
tempmatR.setRotation(quat);
|
//tempmat.setScale(s.x);
|
tempmatS.setElement(0,0, s.x);
|
tempmatS.setElement(1,1, s.y);
|
tempmatS.setElement(2,2, s.z);
|
tempmatT.setTranslation(trans);
|
|
// System.out.println("R = " + quat);
|
// System.out.println("S = " + s);
|
// System.out.println("T = " + trans);
|
|
switch(count%6)
|
{
|
case 0:
|
tempmat.mul(tempmatT);
|
tempmat.mul(tempmatR);
|
tempmat.mul(tempmatS);
|
// System.out.println("TRS");
|
break;
|
|
case 1:
|
tempmat.mul(tempmatT);
|
tempmat.mul(tempmatS);
|
tempmat.mul(tempmatR);
|
// System.out.println("TSR");
|
break;
|
|
case 2:
|
tempmat.mul(tempmatR);
|
tempmat.mul(tempmatS);
|
tempmat.mul(tempmatT);
|
// System.out.println("RST");
|
break;
|
|
case 3:
|
tempmat.mul(tempmatR);
|
tempmat.mul(tempmatT);
|
tempmat.mul(tempmatS);
|
// System.out.println("RTS");
|
break;
|
|
case 4:
|
tempmat.mul(tempmatS);
|
tempmat.mul(tempmatT);
|
tempmat.mul(tempmatR);
|
// System.out.println("STR");
|
break;
|
|
case 5:
|
tempmat.mul(tempmatS);
|
tempmat.mul(tempmatR);
|
tempmat.mul(tempmatT);
|
// System.out.println("SRT");
|
break;
|
}
|
|
if (count < 6)
|
{
|
FillMatrix(tempmat, c.toParent);
|
LA.matInvert(c.toParent, c.fromParent);
|
}
|
else
|
{
|
FillMatrix(tempmat, c.fromParent);
|
LA.matInvert(c.toParent, c.toParent);
|
}
|
}
|
*/
|
|
void ReadMaterial(Object3D o, Shape3D s)
|
{
|
cMaterial cmat = o.material; // Read((MaterialState)s.getRenderState(com.jme.scene.state.RenderState.RS_MATERIAL));
|
Material mat = s.getAppearance().getMaterial();
|
javax.vecmath.Color3f col = new javax.vecmath.Color3f();
|
mat.getDiffuseColor(col);
|
buffer[0] = col.x; buffer[1] = col.y; buffer[2] = col.z; buffer[3] = 1;
|
//Color col = new Color(buffer[0], buffer[1], buffer[2]);
|
//col.getColorComponents(ColorSpace.getInstance(ColorSpace.TYPE_HSV), buffer);
|
cColor.RGBtoHSB(buffer[0], buffer[1], buffer[2], buffer);
|
|
cmat.color = buffer[0]+0.001f;
|
cmat.modulation = buffer[1]+0.001f;
|
cmat.diffuse = buffer[2]+0.001f;
|
|
mat.getSpecularColor(col);
|
buffer[0] = col.x; buffer[1] = col.y; buffer[2] = col.z; buffer[3] = 1;
|
|
cmat.specular = buffer[0]+0.001f;
|
// No way to get the metalness
|
|
//col = mat.getAmbient();
|
//buffer[0] = col.r; buffer[1] = col.g; buffer[2] = col.b; buffer[3] = ol.a;
|
|
//cmat.ambient = buffer[0]+0.001f;
|
|
cmat.shininess = 1 / (Math.max(mat.getShininess(),0)+0.001f); // 1/x
|
}
|
|
void ReadTexture(Object3D o, Shape3D s)
|
{
|
o.GetTextures().name = ":"; // Read((TextureState)s.getRenderState(com.jme.scene.state.RenderState.RS_TEXTURE));
|
}
|
|
void ReadNode(Object3D c, Shape3D g)
|
{
|
// ReadTransform(c, g);
|
ReadMaterial(c, g);
|
ReadTexture(c, g);
|
}
|
|
String GetName(SceneGraphObject sgo)
|
{
|
String thename = "no name";
|
|
if (names == null)
|
{
|
if (sgo.getUserData() == null)
|
return null; // sgo.toString();
|
else
|
return sgo.getUserData().toString();
|
}
|
|
/*
|
if (names.get(s) != null) // ?????
|
name = (String)names.get(s);
|
*/
|
for (java.util.Enumeration<String> e = names.keys(); e.hasMoreElements();)
|
{
|
String name = e.nextElement();
|
|
if (names.get(name) == sgo)
|
{
|
thename = name;
|
break;
|
}
|
}
|
|
return thename;
|
}
|
|
Object3D Read(Shape3D s)
|
{
|
Object3D obj = new Object3D(GetName(s));
|
|
obj.bRep = Read(s.getGeometry());
|
|
if (s.getAppearance() != null)
|
{
|
if (s.getAppearance().getUserData() != null)
|
obj.SetBumpTexture(/*"/" +*/ filename + "/" + s.getAppearance().getUserData().toString());
|
if (s.getAppearance().getMaterial() != null)
|
{
|
obj.CreateMaterial();
|
if (s.getAppearance().getMaterial().getUserData() != null)
|
obj.SetPigmentTexture(/*"/" +*/ filename + "/" + s.getAppearance().getMaterial().getUserData().toString());
|
ReadMaterial(obj, s);
|
}
|
}
|
|
return obj;
|
}
|
|
Object3D Read(Group g)
|
{
|
Composite c = new Composite(GetName(g)); // g.getClass().getName());
|
|
// ReadNode(c, g);
|
|
for (int i=0; i<g.numChildren(); i++)
|
{
|
c.addChild(Read(g.getChild(i)));
|
}
|
|
return c;
|
}
|
|
void ResetTransform(Object3D c, TransformGroup g)
|
{
|
Transform3D t = new Transform3D();
|
Matrix4d m = new Matrix4d();
|
|
ResetTransform(c, t, false);
|
|
// g.getTransform(t);
|
// t.get(m);
|
//
|
// c.toParent[0][0] = m.m00;
|
// c.toParent[0][1] = m.m10;
|
// c.toParent[0][2] = m.m20;
|
// c.toParent[0][3] = m.m30;
|
// c.toParent[1][0] = m.m01;
|
// c.toParent[1][1] = m.m11;
|
// c.toParent[1][2] = m.m21;
|
// c.toParent[1][3] = m.m31;
|
// c.toParent[2][0] = m.m02;
|
// c.toParent[2][1] = m.m12;
|
// c.toParent[2][2] = m.m22;
|
// c.toParent[2][3] = m.m32;
|
// c.toParent[3][0] = m.m03;
|
// c.toParent[3][1] = m.m13;
|
// c.toParent[3][2] = m.m23;
|
// c.toParent[3][3] = m.m33;
|
//
|
// // ReadNode(c, g);
|
// LA.matInvert(c.toParent, c.fromParent);
|
|
// c.Touch();
|
|
assert(c.size() == g.numChildren());
|
for (int i=0; i<g.numChildren(); i++)
|
{
|
ResetTransform(c.get(i), g.getChild(i));
|
}
|
}
|
|
static void GetTranslation(Object3D c, Vector3d t)
|
{
|
t.x = c.toParent[3][0];
|
t.y = c.toParent[3][1];
|
t.z = c.toParent[3][2];
|
}
|
|
static void ResetTransform(Object3D c, Transform3D t, boolean translate)
|
{
|
Matrix4d m = new Matrix4d();
|
t.get(m);
|
|
if (c.toParent == null)
|
{
|
c.toParent = LA.newMatrix();
|
c.fromParent = LA.newMatrix();
|
}
|
|
if (!translate)
|
{
|
c.toParent[0][0] = m.m00;
|
c.toParent[0][1] = m.m10;
|
c.toParent[0][2] = m.m20;
|
c.toParent[0][3] = m.m30;
|
c.toParent[1][0] = m.m01;
|
c.toParent[1][1] = m.m11;
|
c.toParent[1][2] = m.m21;
|
c.toParent[1][3] = m.m31;
|
c.toParent[2][0] = m.m02;
|
c.toParent[2][1] = m.m12;
|
c.toParent[2][2] = m.m22;
|
c.toParent[2][3] = m.m32;
|
}
|
|
c.toParent[3][0] = m.m03;
|
c.toParent[3][1] = m.m13;
|
c.toParent[3][2] = m.m23;
|
c.toParent[3][3] = m.m33;
|
|
// ReadNode(c, g);
|
LA.matInvert(c.toParent, c.fromParent);
|
}
|
|
static void SetTransform(double[][] toParent, Matrix4d m)
|
{
|
toParent[0][0] = m.m00;
|
toParent[0][1] = m.m10;
|
toParent[0][2] = m.m20;
|
toParent[0][3] = m.m30;
|
toParent[1][0] = m.m01;
|
toParent[1][1] = m.m11;
|
toParent[1][2] = m.m21;
|
toParent[1][3] = m.m31;
|
toParent[2][0] = m.m02;
|
toParent[2][1] = m.m12;
|
toParent[2][2] = m.m22;
|
toParent[2][3] = m.m32;
|
toParent[3][0] = m.m03;
|
toParent[3][1] = m.m13;
|
toParent[3][2] = m.m23;
|
toParent[3][3] = m.m33;
|
}
|
|
Matrix4d m = new Matrix4d();
|
|
Object3D Read(TransformGroup g)
|
{
|
Object3D c = new Object3D(GetName(g)); // g.getClass().getName());
|
|
Transform3D t = new Transform3D();
|
|
g.getTransform(t);
|
|
t.get(m);
|
|
c.toParent = LA.newMatrix();
|
c.fromParent = LA.newMatrix();
|
|
if (m.m00 != 0 || m.m10 != 0 || m.m20 != 0 || m.m30 != 0 ||
|
m.m01 != 0 || m.m11 != 0 || m.m21 != 0 || m.m31 != 0 ||
|
m.m02 != 0 || m.m12 != 0 || m.m22 != 0 || m.m32 != 0 ||
|
m.m03 != 0 || m.m13 != 0 || m.m23 != 0 /*|| m.m33 != 0 */)
|
{
|
SetMatrix(c, m);
|
}
|
else
|
{
|
System.err.println("Found zero scale matrix. Hide the object instead.");
|
c.count = 1;
|
}
|
|
// ReadNode(c, g);
|
LA.matInvert(c.toParent, c.fromParent);
|
|
for (int i=0; i<g.numChildren(); i++)
|
{
|
c.addChild(Read(g.getChild(i)));
|
}
|
|
return c;
|
}
|
|
private void SetMatrix(Object3D c, Matrix4d m)
|
{
|
c.toParent[0][0] = m.m00;
|
c.toParent[0][1] = m.m10;
|
c.toParent[0][2] = m.m20;
|
c.toParent[0][3] = m.m30;
|
c.toParent[1][0] = m.m01;
|
c.toParent[1][1] = m.m11;
|
c.toParent[1][2] = m.m21;
|
c.toParent[1][3] = m.m31;
|
c.toParent[2][0] = m.m02;
|
c.toParent[2][1] = m.m12;
|
c.toParent[2][2] = m.m22;
|
c.toParent[2][3] = m.m32;
|
c.toParent[3][0] = m.m03;
|
c.toParent[3][1] = m.m13;
|
c.toParent[3][2] = m.m23;
|
c.toParent[3][3] = m.m33;
|
}
|
|
Object3D Read(Scene s, String fn)
|
{
|
javax.media.j3d.Group g = s.getSceneGroup();
|
names = s.getNamedObjects();
|
filename = fn;
|
// enumnames = names.keys();
|
Object3D obj = Read(g);
|
obj.name = fn;
|
return obj;
|
}
|
|
java.util.Hashtable names;
|
String filename;
|
//java.util.Enumeration<String> enumnames;
|
|
float[] buffer = new float[4];
|
|
/*
|
cMaterial Read(MaterialState mat)
|
{
|
if (mat == null)
|
return null;
|
|
cMaterial cmat = new cMaterial();
|
|
ColorRGBA col = mat.getDiffuse();
|
buffer[0] = col.r; buffer[1] = col.g; buffer[2] = col.b; buffer[3] = col.a;
|
//Color col = new Color(buffer[0], buffer[1], buffer[2]);
|
//col.getColorComponents(ColorSpace.getInstance(ColorSpace.TYPE_HSV), buffer);
|
cColor.RGBtoHSB(buffer[0], buffer[1], buffer[2], buffer);
|
|
cmat.color = buffer[0]+0.001f;
|
cmat.modulation = buffer[1]+0.001f;
|
cmat.diffuse = buffer[2]+0.001f;
|
|
col = mat.getSpecular();
|
buffer[0] = col.r; buffer[1] = col.g; buffer[2] = col.b; buffer[3] = col.a;
|
|
cmat.specular = buffer[0]+0.001f;
|
// No way to get the metalness
|
|
col = mat.getAmbient();
|
buffer[0] = col.r; buffer[1] = col.g; buffer[2] = col.b; buffer[3] = col.a;
|
|
cmat.ambient = buffer[0]+0.001f;
|
|
cmat.shininess = (Math.max(mat.getShininess(),0)+0.001f); // 1/x
|
|
return cmat;
|
}
|
|
cTexture Read(TextureState tex)
|
{
|
if (tex == null)
|
return null;
|
|
if( tex.getTexture() != null)
|
{
|
System.out.println(tex.getTexture().getImageLocation());
|
System.out.println(tex.getTexture().getImage());
|
System.out.println(tex.getTexture().getTextureKey());
|
}
|
else
|
System.out.println("NO TEXTURE");
|
|
cTexture ctex = null; // new cTexture();
|
|
return ctex;
|
}
|
*/
|
//Vertex v = new Vertex(true);
|
|
static Vector3f v3f = new Vector3f();
|
static Point3f p3f = new Point3f();
|
static TexCoord2f t2f = new TexCoord2f();
|
|
BoundaryRep Read(javax.media.j3d.Geometry g)
|
{
|
BoundaryRep brep = null;
|
|
if (g instanceof javax.media.j3d.TriangleArray)
|
{
|
javax.media.j3d.TriangleArray ta = (javax.media.j3d.TriangleArray) g;
|
|
int nbvertices = ta.getVertexCount();
|
|
assert (nbvertices % 3 == 0);
|
|
brep = new BoundaryRep();
|
brep.redimension(nbvertices, nbvertices/3);
|
|
for (int i=0; i<nbvertices; i++)
|
{
|
Vertex v = new Vertex(true);
|
|
ta.getCoordinate(i, p3f);
|
|
v.x = p3f.x;
|
v.y = p3f.y;
|
v.z = p3f.z;
|
|
try
|
{
|
ta.getTextureCoordinate(0, i, t2f);
|
|
v.s = t2f.x;
|
v.t = t2f.y;
|
}
|
catch (Exception e)
|
{
|
v.s = 0;
|
v.t = 0;
|
}
|
|
ta.getNormal(i, v3f);
|
|
v.norm.x = v3f.x;
|
v.norm.y = v3f.y;
|
v.norm.z = v3f.z;
|
|
brep.SetVertex(v, i);
|
}
|
|
for (int i=0; i<nbvertices; i+=3)
|
{
|
brep.setFace(i/3,i,i+1,i+2);
|
}
|
|
//brep.Trim(true, false, false, true, false);
|
// NOT REALLY IMPORTANT
|
brep.Trim();
|
brep.Untrim();
|
if (cJME.stripify)
|
brep.Stripify();
|
if (cJME.reduction34)
|
brep.SplitInTwo(true, false);
|
}
|
if (g instanceof javax.media.j3d.TriangleStripArray)
|
{
|
javax.media.j3d.TriangleStripArray tsa = (javax.media.j3d.TriangleStripArray) g;
|
brep = new BoundaryRep();
|
BoundaryRep bRep = brep;
|
|
bRep.redimension(tsa.getVertexCount(), 0); // tsa.getVertexCount() - tsa.getNumStrips()*2);
|
|
boolean cache = true;
|
|
if (!cache)
|
bRep.Trim();
|
|
float[] vertices = bRep.getRawVertices(); // new float[tsa.getValidVertexCount()*3];
|
tsa.getCoordinates(0, vertices);
|
float[] normals = bRep.getRawNormals(); // new float[tsa.getValidVertexCount()*3];
|
tsa.getNormals(0, normals);
|
float[] uvmap = bRep.getRawUVMap(); // new float[tsa.getValidVertexCount()*2];
|
|
try
|
{
|
tsa.getTextureCoordinate(0, 0, uvmap);
|
}
|
catch (Exception e)
|
{
|
// System.err.println("MULTI-TEXTURE ???");
|
//e.printStackTrace();
|
}
|
|
if (cache)
|
{
|
//Vertex v = new Vertex(true);
|
|
// Vertices
|
int i3 = 0, i2 = 0;
|
for (int i=0; i<tsa.getVertexCount(); i++, i3+=3, i2+=2)
|
{
|
//Vertex v = new Vertex(true);
|
Vertex v = bRep.GetVertex(i);
|
v./*pos.*/set(vertices[i3], vertices[i3+1], vertices[i3+2]);
|
v.norm.set(normals[i3], normals[i3+1], normals[i3+2]);
|
//v.s = textures[0][i2];
|
//v.t = textures[0][i2+1];
|
v.s = uvmap[i2];
|
v.t = uvmap[i2+1];
|
// Vertex in = bRep.GetCache(v);
|
// if (true) // in == null)
|
// {
|
// v.index = -1;
|
// Vertex out = new Vertex(v);
|
// //bRep.AddVertex(out);
|
// bRep.Remember(out);
|
// }
|
// else
|
// {
|
// bRep.AddVertex(in);
|
// }
|
}
|
}
|
|
// Faces
|
int[] counts = new int[tsa.getNumStrips()];
|
tsa.getStripVertexCounts(counts);
|
|
int indexF = 0;
|
int indexV = 0;
|
|
for (int i=0; i<tsa.getNumStrips(); i++)
|
{
|
int count = counts[i];
|
|
assert count >= 3;
|
|
bRep.AddFace(indexV, indexV+1, indexV+2);
|
indexF++;
|
//bRep.faces.addElement(null);
|
//bRep.setFace(indexF++, indexV, indexV+1, indexV+2);
|
indexV += 3;
|
|
for (int j=3; j<count; j++, indexV++)
|
{
|
//bRep.faces.addElement(null);
|
if ((indexF % 2) == 0)
|
bRep.AddFace(indexV-2, indexV-1, indexV);
|
else
|
bRep.AddFace(indexV-2, indexV, indexV-1);
|
|
indexF++;
|
}
|
}
|
|
assert indexF == tsa.getVertexCount() - tsa.getNumStrips()*2;
|
assert indexV == tsa.getVertexCount();
|
}
|
/*
|
Object3D result = new Object3D(g.getClass().getSimpleName());
|
|
ReadNode(result, g);
|
|
Vertex.normalmode = true;
|
//Vertex.normalmode = cJME.gennormals;
|
|
int tris = g.getTriangleCount();
|
|
System.out.println("#triangles = " + tris);
|
//int faces = g.getVertexCount()/3;
|
//int verts = g.getVertexCount();
|
int faces = tris;
|
int verts = tris*3;
|
|
BoundaryRep bRep = new BoundaryRep(verts, faces);
|
|
boolean cache = true;
|
|
if (!cache)
|
bRep.Trim();
|
|
//float[] coords = bRep.getRawVertices();
|
//float[] normals = bRep.getRawNormals();
|
//float[] uvmap = bRep.getRawUVMap();
|
|
//assert (coords != null);
|
//assert (normals != null);
|
|
com.jme.math.Triangle[] triangles = new com.jme.math.Triangle[tris];
|
|
for (int i=0; i<tris; i++)
|
{
|
triangles[i] = new com.jme.math.Triangle(
|
new com.jme.math.Vector3f(),
|
new com.jme.math.Vector3f(),
|
new com.jme.math.Vector3f());
|
}
|
|
g.getMeshAsTriangles(0, triangles); // jME1
|
|
//tsa.getNormals(normals);
|
|
//float[][] textures = new float[1][];
|
//textures[0] = uvmap;
|
|
//tsa.getTextureCoordinates(textures);
|
|
// jME1
|
java.nio.FloatBuffer texcoords = g.getTextureBuffer(0,0);
|
//TexCoords texcoords = g.getTextureBuffer(0,0); //g.getTextureCoords(0); //.coords;
|
java.nio.FloatBuffer normals = g.getNormalBuffer(0);
|
|
//System.out.println("#texcoords = " + texcoords.capacity());
|
//System.out.println("#normals = " + normals.capacity());
|
|
if(texcoords != null && texcoords.capacity() != tris*6)
|
texcoords = null;
|
if(normals != null && normals.capacity() != verts*3)
|
normals = null;
|
|
if (cache)
|
{
|
// bbox
|
float minx=Float.MAX_VALUE,miny=Float.MAX_VALUE,minz=Float.MAX_VALUE;
|
float maxx=Float.MIN_VALUE,maxy=Float.MIN_VALUE,maxz=Float.MIN_VALUE;
|
for (int i=0; i<verts; i++)
|
{
|
com.jme.math.Vector3f jv = triangles[i/3].get(i%3);
|
if (minx > jv.x)
|
minx = jv.x;
|
if (miny > jv.y)
|
miny = jv.y;
|
if (minz > jv.z)
|
minz = jv.z;
|
if (maxx < jv.x)
|
maxx = jv.x;
|
if (maxy < jv.y)
|
maxy = jv.y;
|
if (maxz < jv.z)
|
maxz = jv.z;
|
}
|
|
float orgx = (minx+maxx)/2;
|
float orgy = miny;
|
float orgz = (minz+maxz)/2;
|
|
// Vertices
|
int i3 = 0;
|
int i2 = 0;
|
for (int i=0; i<verts; i++, i3+=3, i2+=2)
|
{
|
com.jme.math.Vector3f jv = triangles[i/3].get(i%3);
|
v.set(jv.x-orgx, jv.y-orgy, jv.z-orgz);
|
//v.set(jv.x - 94, jv.y - 90, jv.z + 540);
|
if (normals != null)
|
v.norm.set(normals.get(i3), normals.get(i3+1), normals.get(i3+2));
|
if (texcoords != null)
|
{
|
v.s = texcoords.get(i2);
|
v.t = texcoords.get(i2+1);
|
}
|
Vertex in = bRep.GetCache(v);
|
if (in == null)
|
{
|
v.index = -1;
|
bRep.Remember(new Vertex(v));
|
}
|
else
|
{
|
bRep.AddVertex(in);
|
}
|
}
|
|
LA.matTranslate(result.fromParent, -orgx,-orgy,-orgz);
|
LA.matTranslate(result.toParent, orgx,orgy,orgz);
|
}
|
|
// Faces
|
int indexF = 0;
|
int indexV = 0;
|
|
for (int i=0; i<faces; i++)
|
{
|
//bRep.faces.addElement(null);
|
bRep.AddFace(indexV, indexV+1, indexV+2);
|
indexF++;
|
|
indexV += 3;
|
}
|
|
assert indexF == faces;
|
assert indexV == verts;
|
|
if (cache)
|
bRep.Trim(trim, //true, true, true); //
|
normals==null || gennormals,false,stripify,genUV);
|
|
result.bRep = bRep;
|
|
Vertex.normalmode = false;
|
|
return result;
|
*/
|
return brep;
|
}
|
|
/*
|
static BoundaryRep Read(TriangleStripArray tsa)
|
{
|
//System.out.println("TriangleStripArray");
|
BoundaryRep bRep = new BoundaryRep(tsa.getValidVertexCount(),
|
tsa.getValidVertexCount() -
|
tsa.getValidStripCount()*2);
|
|
boolean cache = true;
|
|
if (!cache)
|
bRep.Trim();
|
|
float[] vertices = bRep.getRawVertices(); // new float[tsa.getValidVertexCount()*3];
|
tsa.getVertices(vertices);
|
float[] normals = bRep.getRawNormals(); // new float[tsa.getValidVertexCount()*3];
|
tsa.getNormals(normals);
|
float[] uvmap = bRep.getRawUVMap(); // new float[tsa.getValidVertexCount()*2];
|
|
float[][] textures = new float[1][];
|
textures[0] = uvmap;
|
|
try
|
{
|
tsa.getTextureCoordinates(textures);
|
}
|
catch (Exception e)
|
{
|
System.out.println("MULTI-TEXTURE ???");
|
}
|
|
if (cache)
|
{
|
// Vertices
|
int i3 = 0, i2 = 0;
|
for (int i=0; i<tsa.getValidVertexCount(); i++, i3+=3, i2+=2)
|
{
|
//Vertex v = new Vertex(true);
|
v.set(vertices[i3], vertices[i3+1], vertices[i3+2]);
|
v.norm.set(normals[i3], normals[i3+1], normals[i3+2]);
|
//v.s = textures[0][i2];
|
//v.t = textures[0][i2+1];
|
v.s = uvmap[i2];
|
v.t = uvmap[i2+1];
|
Vertex in = bRep.GetCache(v);
|
if (true) // in == null)
|
{
|
v.index = -1;
|
Vertex out = new Vertex(v);
|
//bRep.AddVertex(out);
|
bRep.Remember(out);
|
}
|
else
|
{
|
bRep.AddVertex(in);
|
}
|
}
|
}
|
|
// Faces
|
int[] counts = new int[tsa.getValidStripCount()];
|
tsa.getStripCount(counts);
|
|
int indexF = 0;
|
int indexV = 0;
|
|
for (int i=0; i<tsa.getValidStripCount(); i++)
|
{
|
int count = counts[i];
|
|
assert count >= 3;
|
|
bRep.AddFace(indexV, indexV+1, indexV+2);
|
indexF++;
|
//bRep.faces.addElement(null);
|
//bRep.setFace(indexF++, indexV, indexV+1, indexV+2);
|
indexV += 3;
|
|
for (int j=3; j<count; j++, indexV++)
|
{
|
//bRep.faces.addElement(null);
|
if ((indexF % 2) == 0)
|
bRep.AddFace(indexV-2, indexV-1, indexV);
|
else
|
bRep.AddFace(indexV-2, indexV, indexV-1);
|
|
indexF++;
|
}
|
}
|
|
assert indexF == tsa.getValidVertexCount() - tsa.getValidStripCount()*2;
|
assert indexV == tsa.getValidVertexCount();
|
|
if (cache)
|
bRep.Trim();
|
|
return bRep;
|
}
|
|
static BoundaryRep Read(TriangleFanArray tsa)
|
{
|
BoundaryRep bRep = new BoundaryRep();
|
|
return bRep;
|
}
|
|
static BoundaryRep Read(TriangleArray tsa)
|
{
|
//System.out.println("TriangleArray");
|
int faces = tsa.getValidVertexCount()/3;
|
|
BoundaryRep bRep = new BoundaryRep(tsa.getValidVertexCount(),
|
faces);
|
|
boolean cache = true;
|
|
if (!cache)
|
bRep.Trim();
|
|
float[] vertices = bRep.getRawVertices(); // new float[tsa.getValidVertexCount()*3];
|
float[] normals = bRep.getRawNormals(); // new float[tsa.getValidVertexCount()*3];
|
float[] uvmap = bRep.getRawUVMap(); // new float[tsa.getValidVertexCount()*2];
|
|
assert (vertices != null);
|
assert (normals != null);
|
try
|
{
|
tsa.getVertices(vertices);
|
tsa.getNormals(normals);
|
|
float[][] textures = new float[1][];
|
textures[0] = uvmap;
|
|
tsa.getTextureCoordinates(textures);
|
}
|
catch (Exception e)
|
{
|
System.out.println("NO VERTEX!");
|
}
|
|
if (cache)
|
{
|
// Vertices
|
int i3 = 0;
|
int i2 = 0;
|
for (int i=0; i<tsa.getValidVertexCount(); i++, i3+=3, i2+=2)
|
{
|
//Vertex v = new Vertex();
|
v.set(vertices[i3], vertices[i3+1], vertices[i3+2]);
|
v.norm.set(normals[i3], normals[i3+1], normals[i3+2]);
|
v.s = uvmap[i2];
|
v.t = uvmap[i2+1];
|
Vertex in = bRep.GetCache(v);
|
if (in == null)
|
{
|
v.index = -1;
|
bRep.Remember(new Vertex(v));
|
}
|
else
|
{
|
bRep.AddVertex(in);
|
}
|
}
|
}
|
|
// Faces
|
int indexF = 0;
|
int indexV = 0;
|
|
for (int i=0; i<faces; i++)
|
{
|
//bRep.faces.addElement(null);
|
bRep.AddFace(indexV, indexV+1, indexV+2);
|
indexF++;
|
|
indexV += 3;
|
}
|
|
assert indexF == faces;
|
assert indexV == tsa.getValidVertexCount();
|
|
if (cache)
|
bRep.Trim();
|
|
return bRep;
|
}
|
|
static Object3D Read(Shape3D s)
|
{
|
Object3D o = new Object3D(s.getClass().getName());
|
|
Material mat = s.getAppearance().getMaterial();
|
|
if (mat != null)
|
o.material = Read(mat);
|
|
//System.out.println("Geometry = " + s.getGeometry());
|
|
o.bRep = Read(s.getGeometry());
|
|
return o;
|
}
|
*/
|
|
void FillMatrix(Matrix4d in, double[][] out)
|
{
|
for (int j=0; j<4; j++)
|
for (int i=0; i<4; i++)
|
out[i][j] = in.getElement(j,i);
|
}
|
}
|