package jhpro.engine3d;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.IOException;
import javax.swing.JPanel;
import jhpro.engine3d.MathParser.MathParser;
import jhpro.engine3d.models.*;
public class ViewPanel3d extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public static Window3d drawPad = null;
static int DrawSize = 500;
// public static boolean wantSolidRendering = true;
// public static boolean wantShading = true;
static boolean take_new_value = false;
static int nv = 0;
private String modelName = "";
// public static Color penColor = Color.black;
// public static Color fillColor = Color.red;
private FacetedObject fo=null;
public void start() {
drawPad.Start();
}
public void stop() {
drawPad.Stop();
}
public ViewPanel3d(String modelName) {
this.modelName = modelName;
setLayout(new BorderLayout());
setBackground(Color.white);
if (modelName.length() > 1) {
try {
fo = FileParse.readOffObject(modelName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
fo = RingBand.create12();
fo.setFillColor(fo.getFillColor());
fo.setShading(true);
// fo.setSolid(true);
fo.setPenColor(fo.getPenColor());
}
// between 0 and 255
fo.setTransperancy(fo.getTransperancy());
drawPad = new Window3d(DrawSize, fo);
this.add(drawPad, BorderLayout.CENTER);
Window3d.spinDelay = 80;
start();
}
/**
* Return 3D object
* @return
*/
public FacetedObject getObject3D(){
return fo;
}
/**
* Return 3D pad
* @return
*/
public Window3d getPad3D(){
return drawPad;
}
/**
* Load a new model
*
* @param model
*/
public void loadFileModel(String model) {
// System.out.println("Loading a new modrl=" + model);
drawPad.rotatePause();
drawPad.loadModel(model);
drawPad.repaint();
drawPad.rotateResume();
}
/**
* Load a new model
*
* @param model
*/
public void loadFileModel(FacetedObject fo) {
drawPad.rotatePause();
drawPad.loadModel(fo);
drawPad.repaint();
drawPad.rotateResume();
}
public void parseFunction(String expression, int gridDivsU, int gridDivsV) {
double area;
boolean isCurve = false;
Vec coords[][];
/** The coordinates describing the grid to be drawn. */
MathParser mathParser = new MathParser();
coords = new Vec[gridDivsU][isCurve ? 1 : gridDivsV];
area = 0;
mathParser.resetVariables();
mathParser.setVariable("uSteps", gridDivsU - 1);
mathParser.setVariable("tSteps", gridDivsU - 1);
mathParser.setVariable("vSteps", isCurve ? 0 : gridDivsV - 1);
for (int i = 0; i < gridDivsU; i++) {
double u = (float) i / (gridDivsU - 1);
for (int j = 0; j < (isCurve ? 1 : gridDivsV); j++) {
double v = (float) j / (gridDivsV - 1);
mathParser.setVariable("u", u);
mathParser.setVariable("t", u);
mathParser.setVariable("v", v);
mathParser.setVariable("x", isCurve ? 0 : 2 * u - 1);
mathParser.setVariable("y", isCurve ? 0 : 2 * v - 1);
mathParser.setVariable("z", 0);
try {
mathParser.parseExpression(expression);
coords[i][j] = new Vec(mathParser.getVariable("x"),
mathParser.getVariable("z"), // change z,y to
// display
// 'mathematically'
mathParser.getVariable("y"));
if (isCurve) {
if (i > 0)
area += coords[i][0].substract(coords[i - 1][0])
.norm();
} else {
if (i > 0 && j > 0)
area += coords[i][j - 1].substract(
coords[i - 1][j - 1]).crossProduct(
coords[i - 1][j]
.substract(coords[i - 1][j - 1]))
.norm()
/ 2
+ coords[i][j - 1]
.substract(coords[i][j])
.crossProduct(
coords[i - 1][j]
.substract(coords[i][j]))
.norm() / 2;
}
} catch (Exception e) {
coords[i][j] = new Vec(Double.NaN, Double.NaN, Double.NaN);
// e.printStackTrace();
}
}
}
}
// FacetedObject fo = new FacetedObject(coords, vi);
// return fo;
};
Ads help maintain this website.