Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added classes Line3D and Ray3D to javafx package #32

Merged
merged 5 commits into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package org.openbase.jul.visual.javafx.geometry;

/*-
* #%L
* JUL Visual JavaFX
* %%
* Copyright (C) 2015 - 2017 openbase.org
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.paint.Material;
import javafx.scene.shape.Box;
import javafx.scene.shape.Cylinder;

/**
* This class represents a line in a 3d space. Width, line type, material and
* start and end points can be specified.
*
* @author <a href="mailto:[email protected]">Thoren Huppke</a>
*/
public class Line3D extends Group {

/**
* Upwards vector used to calculate the rotation axis.
*/
private final static Point3D UP = new Point3D(0, 1, 0);

/**
* The LineType used for this Line object.
*/
private final LineType type;

/**
* JavaFX Box used in case of LineType BOX.
*/
private Box box;

/**
* JavaFX Cylinder used in case of LineType CYLINDER.
*/
private Cylinder cylinder;

/**
* Can be used to specify the looks of the Line object.
*/
public enum LineType {
/**
* A box type line is used.
*/
BOX,
/**
* A cylinder type line is used.
*/
CYLINDER
}

/**
* Base constructor.
*
* @param type The LineType that is going to be used.
* @param width The line width.
* @param material The line material.
*/
public Line3D(final LineType type, double width, final Material material) {
this.type = type;
super.setVisible(false);
switch (type) {
case BOX:
box = new Box(width, 0, width);
box.setMaterial(material);
super.getChildren().add(box);
break;
case CYLINDER:
cylinder = new Cylinder(width * 0.5, 0);
cylinder.setMaterial(material);
super.getChildren().add(cylinder);
break;
default:
break;
}
}

/**
* Constructor with start and end intialization.
*
* @param type The LineType that is going to be used.
* @param width The line width.
* @param material The line material.
* @param start Start point of the line.
* @param end End point of the line.
*/
public Line3D(final LineType type, final double width, final Material material, final Point3D start, final Point3D end) {
this(type, width, material);
setStartEndPoints(start, end);
}

/**
* Sets the start and end point of the line.
*
* @param start Start point of the line.
* @param end End point of the line.
*/
public final void setStartEndPoints(final Point3D start, final Point3D end) {
final Point3D direction = start.subtract(end);
final Point3D position = start.midpoint(end);
setLength(direction.magnitude());
final Point3D axis = UP.crossProduct(direction.normalize());
super.setVisible(true);
super.setTranslateX(position.getX());
super.setTranslateY(position.getY());
super.setTranslateZ(position.getZ());
super.setRotationAxis(axis);
super.setRotate(UP.angle(direction.normalize()));
}

/**
* Sets the Material of the line, which can be used to display different
* colors.
*
* @param material The line material.
*/
public void setMaterial(final Material material) {
switch (type) {
case BOX:
box.setMaterial(material);
break;
case CYLINDER:
cylinder.setMaterial(material);
break;
default:
break;
}
}

/**
* Sets the width of the line.
*
* @param width The line width.
*/
public void setWidth(double width) {
switch (type) {
case BOX:
box.setWidth(width);
box.setDepth(width);
break;
case CYLINDER:
cylinder.setRadius(width * 0.5);
break;
default:
break;
}
}

/**
* Sets the length of the line. This is only used internally to shape the
* line correctly.
*
* @param length Length of the line.
*/
private void setLength(double length) {
switch (type) {
case BOX:
box.setHeight(length);
break;
case CYLINDER:
cylinder.setHeight(length);
break;
default:
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.openbase.jul.visual.javafx.geometry;

/*-
* #%L
* JUL Visual JavaFX
* %%
* Copyright (C) 2015 - 2017 openbase.org
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/
import javafx.geometry.Point3D;
import javafx.scene.paint.Material;
import rst.geometry.Ray3DFloatType.Ray3DFloat;
import rst.math.Vec3DFloatType.Vec3DFloat;

/**
* This class represents a ray in a 3d space represented by a cylinder. Default width is 2cm.
*
* @author <a href="mailto:[email protected]">Thoren Huppke</a>
*/
public class Ray3D extends Line3D {

/**
* The default length of a ray.
*/
private final static double DEFAULT_LENGTH = 10;

/**
* The default width of a ray.
*/
private final static double DEFAULT_WIDTH = 0.02;

/**
* The length of this ray.
*/
private final double rayLength;

/**
* Transforms a Vec3DFloat object to a Point3D object.
*
* @param vector The Vec3DFloat object.
* @return The transformed Point3D.
*/
private Point3D VecToPoint(final Vec3DFloat vector) {
return new Point3D(vector.getX(), vector.getY(), vector.getZ());
}

/**
* Constructor using default ray length (10 meters).
*
* @param material The ray material.
*/
public Ray3D(final Material material) {
super(Line3D.LineType.CYLINDER, DEFAULT_WIDTH, material);
this.rayLength = DEFAULT_LENGTH;
}

/**
* Constructor using the given ray length.
*
* @param material The ray material.
* @param rayLength The ray length.
*/
public Ray3D(final Material material, double rayLength) {
super(Line3D.LineType.CYLINDER, DEFAULT_WIDTH, material);
this.rayLength = rayLength;
}

/**
* Updates the ray orientation and position to the specified data.
*
* @param ray Ray3DFloat object defining the placement data of the ray.
*/
public void update(final Ray3DFloat ray) {
final Point3D origin = VecToPoint(ray.getOrigin());
final Point3D direction = VecToPoint(ray.getDirection());
final Point3D end = origin.add(direction.normalize().multiply(rayLength));
super.setStartEndPoints(origin, end);
}
}