Wednesday, 1 January 2014

Create Tube(Pipe) in Java3D and also apply curve




/*
This method create ellipse at given x,y,z  point
*/

LineArray buildEllipse1(double x, double y, double z, double semiMajorAxis,
double semiMinorAxis) {
// build geometry
int totalLines = 50;
int totalPoints = totalLines * 2;
double tInc = 1.0 / (double) totalLines;

Point3d p0 = new Point3d();
Point3d p1 = new Point3d();

LineArray lineArray = new LineArray(totalPoints, LineArray.COORDINATES);

double t = 0.0;
int lineIndex = 0;
for (int i = 0; i < totalLines; i++) {
p0.x = (semiMinorAxis * Math.cos(2.0 * Math.PI * t)) + x;
p0.y = y;
p0.z = (semiMajorAxis * Math.sin(2.0 * Math.PI * t)) + z;
lineArray.setCoordinate(lineIndex++, p0);

p1.x = (semiMinorAxis * Math.cos(2.0 * Math.PI * (t + tInc))) + x;
p1.y = y;
p1.z = (semiMajorAxis * Math.sin(2.0 * Math.PI * (t + tInc))) + z;
// p1.z=10;
lineArray.setCoordinate(lineIndex++, p1);
t += tInc;
}



return lineArray;

}
/ *
Add tube function add tube by merging of circle and make 3d tube(Pipe)
 */
private void addTube_onAction() {

Shape3D circleShape3d = new Shape3D();
// build appearance
Appearance appearance = new Appearance();

LineAttributes lineAttrib = new LineAttributes(2.0f,
LineAttributes.PATTERN_SOLID, true);
appearance.setLineAttributes(lineAttrib);

ColoringAttributes colorAttributes = new ColoringAttributes( new Color3f(Color.YELLOW),
ColoringAttributes.SHADE_GOURAUD);
appearance.setColoringAttributes(colorAttributes);

RenderingAttributes renderAttrib = new RenderingAttributes();
renderAttrib.setDepthBufferEnable(false);
renderAttrib.setDepthBufferWriteEnable(false);
appearance.setRenderingAttributes(renderAttrib);
BranchGroup branchGroup1 = new BranchGroup();



BranchGroup branchGroup = new BranchGroup();
branchGroup.setCapability(Content.ALLOW_DETACH);

Shape3D shape3d = new Shape3D();
Color3f color3f = new Color3f(Color.red);

Point3f pt1 = new Point3f(strToFloat(pt1_x_textField.getText()),
strToFloat(pt1_y_textField.getText()),
strToFloat(pt1_z_textField.getText()));
Point3f pt2 = new Point3f(strToFloat(pt2_x_textField.getText()),
strToFloat(pt2_y_textField.getText()),
strToFloat(pt2_z_textField.getText()));

ArrayList<Point2D> point2ds = generateCurve(
new Point2D.Double(pt1.getX(), pt1.getY()), new Point2D.Double(
pt2.getX(), pt2.getY()), 2000, 1);



Point3d[] coords = new Point3d[point2ds.size()];
int cnt = 0;
for (Point2D point2d : point2ds) {
coords[cnt] = new Point3d(point2d.getX(), point2d.getY(), 250);
circleShape3d.addGeometry(buildEllipse1(point2d.getX(),
point2d.getY(), 250,3,3));
cnt++;
}
//circleShape3d.setAppearance(appearance);
branchGroup1.addChild(circleShape3d);
image3DUniverse.getScene().addChild(branchGroup1);
// // Point3f[] coords = { onX, onY };
//
int N = coords.length;

Color3f colors[] = new Color3f[N];
for (int i = 0; i < N; i++) {
colors[i] = color3f;
}

int[] strip = { N };

LineStripArray ta = new LineStripArray(N, LineStripArray.COORDINATES
| LineStripArray.COLOR_3, strip);
ta.setCoordinates(0, coords);
ta.setColors(0, colors);

shape3d.addGeometry(ta);
branchGroup.addChild(shape3d);
image3DUniverse.getScene().addChild(branchGroup);

}

private static double GetAngle(Point2D x, Point2D o, double R) {
double cosa = (x.getX() - o.getX()) / R;
double sina = (x.getY() - o.getY()) / R;

double angle = Math.acos(cosa);

return Math.sin(angle) * sina >= 0 ? angle : 2 * Math.PI - angle;
}


     /*
      This method apply curve on tube depend on radius value 
      */
private static ArrayList<Point2D> generateCurve(Point2D pFrom, Point2D pTo,
float pRadius, float pMinDistance) {

ArrayList<Point2D> pOutPut = new ArrayList<Point2D>();

double dist = pFrom.distance(pTo);
double h = Math.sqrt(pRadius * pRadius - (dist * dist / 4.0));
double angleStep = pMinDistance / pRadius;

System.out.println(" #### dist ::" + dist);
System.out.println(" #### pRadius ::" + pRadius);

// pRadius = (float) ((dist/ 2 ) + 100) ;

if (2 * pRadius <= dist)
throw new Error("Radius is too small");

// find center
double x1 = pFrom.getX(), x2 = pFrom.getY();
double y1 = pTo.getX(), y2 = pTo.getY();
double m1 = (x1 + y1) / 2, m2 = (x2 + y2) / 2;
double u1 = -(y2 - x2) / dist, u2 = (y1 - x1) / dist;
double o1 = m1 + h * u1, o2 = m2 + h * u2;
Point2D o = new Point2D.Double(o1, o2);

double startAngle = GetAngle(pFrom, o, pRadius);
double endAngle = GetAngle(pTo, o, pRadius);

if (endAngle < startAngle)
endAngle += 2 * Math.PI;

for (double a = startAngle; a < endAngle; a += angleStep) {
pOutPut.add(new Point2D.Double(o1 + pRadius * Math.cos(a), o2
+ pRadius * Math.sin(a)));
}

pOutPut.add(pTo);

return pOutPut;
}

Post a Comment