Skip to content

Commit

Permalink
Merge pull request #285 from jzy3d/feature/alternative2DViews
Browse files Browse the repository at this point in the history
Feature/alternative2dViews
  • Loading branch information
jzy3d authored Jul 4, 2022
2 parents 632af16 + 8f46638 commit ffc84e1
Show file tree
Hide file tree
Showing 283 changed files with 1,659 additions and 882 deletions.
62 changes: 46 additions & 16 deletions jzy3d-core/src/main/java/org/jzy3d/chart/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import org.jzy3d.plot3d.rendering.lights.Light;
import org.jzy3d.plot3d.rendering.scene.Scene;
import org.jzy3d.plot3d.rendering.view.View;
import org.jzy3d.plot3d.rendering.view.View2D;
import org.jzy3d.plot3d.rendering.view.ViewportMode;
import org.jzy3d.plot3d.rendering.view.layout.IViewportLayout;
import org.jzy3d.plot3d.rendering.view.layout.ViewAndColorbarsLayout;
import org.jzy3d.plot3d.rendering.view.lod.LODCandidates;
import org.jzy3d.plot3d.rendering.view.lod.LODPerf;
Expand Down Expand Up @@ -139,14 +139,19 @@ public Chart color(Color background, Color axis) {
* tick and axis labels.
*/
public Chart view2d() {
return view2d(View2D.XY);
}

public Chart view2d(View2D view2D) {
AxisLayout axisLayout = getAxisLayout();
View view = getView();

// Remember 3D layout
if(view.is3D()) {
axisZTickLabelDisplayed = axisLayout.isZTickLabelDisplayed();
axisXLabelOrientation = axisLayout.getXAxisLabelOrientation();
axisYLabelOrientation = axisLayout.getYAxisLabelOrientation();
axisZLabelDisplayed = axisLayout.isZAxisLabelDisplayed();
axisZLabelOrientation = axisLayout.getZAxisLabelOrientation();

isTickLineDisplayed = axisLayout.isTickLineDisplayed();

isSquaredViewActive = view.getSquared();
Expand All @@ -155,14 +160,36 @@ public Chart view2d() {
viewpoint = view.getViewPoint().clone();
}

// Apply 2D layout to axis
axisLayout.setTickLineDisplayed(false);
axisLayout.setZAxisLabelDisplayed(false);
axisLayout.setZTickLabelDisplayed(false);
axisLayout.setYAxisLabelOrientation(LabelOrientation.VERTICAL);
if(View2D.XY.equals(view2D)) {
// Apply 2D layout to axis
axisLayout.setXAxisLabelOrientation(LabelOrientation.HORIZONTAL);
axisLayout.setYAxisLabelOrientation(LabelOrientation.VERTICAL);

// Apply 2D layout to view
view.setViewPositionMode(ViewPositionMode.TOP);
}
else if(View2D.XZ.equals(view2D)) {
// Apply 2D layout to axis
axisLayout.setXAxisLabelOrientation(LabelOrientation.HORIZONTAL);
axisLayout.setZAxisLabelOrientation(LabelOrientation.VERTICAL);

// Apply 2D layout to view
view.setViewPositionMode(ViewPositionMode.XZ);
}

else if(View2D.YZ.equals(view2D)) {
// Apply 2D layout to axis
axisLayout.setYAxisLabelOrientation(LabelOrientation.HORIZONTAL);
axisLayout.setZAxisLabelOrientation(LabelOrientation.VERTICAL);

// Apply 2D layout to view
view.setViewPositionMode(ViewPositionMode.TOP);
// Apply 2D layout to view
view.setViewPositionMode(ViewPositionMode.YZ);
}

// General 2D axis layout
axisLayout.setTickLineDisplayed(false);

// General 2D view settings
view.setSquared(false);
view.getCamera().setViewportMode(ViewportMode.STRETCH_TO_FILL);

Expand All @@ -174,9 +201,9 @@ public Chart view2d() {
}

// memory of 3D settings before switching to 2D
protected LabelOrientation axisXLabelOrientation = null;
protected LabelOrientation axisYLabelOrientation = null;
protected boolean axisZLabelDisplayed = true;
protected boolean axisZTickLabelDisplayed = true;
protected LabelOrientation axisZLabelOrientation = null;
protected boolean isTickLineDisplayed = true;
protected boolean isSquaredViewActive = true;
protected ViewPositionMode viewPositionMode = ViewPositionMode.FREE;
Expand All @@ -187,13 +214,16 @@ public Chart view3d() {
AxisLayout axisLayout = getAxisLayout();

// Restore 3D layout to axis
axisLayout.setTickLineDisplayed(isTickLineDisplayed);
if(axisXLabelOrientation!=null){
axisLayout.setXAxisLabelOrientation(axisXLabelOrientation);
}
if(axisYLabelOrientation!=null){
axisLayout.setYAxisLabelOrientation(axisYLabelOrientation);
}

axisLayout.setZAxisLabelDisplayed(axisZLabelDisplayed);
axisLayout.setZTickLabelDisplayed(axisZTickLabelDisplayed);
axisLayout.setTickLineDisplayed(isTickLineDisplayed);
if(axisZLabelOrientation!=null){
axisLayout.setZAxisLabelOrientation(axisZLabelOrientation);
}

// Restore 3D layout to view
View view = getView();
Expand Down
4 changes: 4 additions & 0 deletions jzy3d-core/src/main/java/org/jzy3d/maths/Coord3d.java
Original file line number Diff line number Diff line change
Expand Up @@ -684,5 +684,9 @@ public static Coord3d getCoordAt(float[] array, int i) {
return new Coord3d(array[i], array[i+1], array[i+2]);
}

public boolean isValid() {
return Float.isFinite(x) && Float.isFinite(y) && Float.isFinite(z);
}


}
48 changes: 43 additions & 5 deletions jzy3d-core/src/main/java/org/jzy3d/plot3d/primitives/Arrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,62 @@ public class Arrow extends Composite {

protected Cylinder cylinder;
protected Cone cone;

protected Vector3d vector;

protected static Vector3d createVector3d(Coord3d pos, Coord3d dir, float length) {
Coord3d dirN = dir.getNormalizedTo(length / 2f);
Coord3d end = pos.add(dirN);
dirN = dirN.negative();
Coord3d start = pos.add(dirN);
return new Vector3d(start, end);
}

public Arrow() {}

public Arrow(Coord3d pos, Coord3d dir, float length, float radius, int slices, int rings, Color color) {
this();
setData(createVector3d(pos, dir, length), radius, slices, rings, color);
}

public void setData(Vector3d vec, float radius, int slices, int rings, Color color) {
Coord3d position = vec.getCenter();
float length = vec.norm();
public void setData(Vector3d vector, float radius, int slices, int rings, Color color) {
this.vector = vector;

Coord3d position = vector.getCenter();
float length = vector.norm();
float coneHeight = radius * 2.5f;
float cylinderHeight = length - coneHeight;

// Arrow body
cylinder = new Cylinder();
cylinder.setData(new Coord3d(0, 0, -length / 2f), cylinderHeight, radius, slices, rings, color);

// Arrow extremity
cone = new Cone();
cone.setData(new Coord3d(0, 0, length / 2d - coneHeight), coneHeight, radius * 1.6f, slices,
rings, color);

add(cylinder);
add(cone);

// Apply the same wireframe settings to each element
// of this composite
setWireframeDisplayed(isWireframeDisplayed());


Transform trans = new Transform();
Rotate rot = createRotateTo(new Coord3d(0d, 0d, 1d), vec.vector());
trans.add(rot);

// Rotate shape
Rotate rot = createRotateTo(new Coord3d(0d, 0d, 1d), vector.vector());

if(Float.isFinite(rot.getAngle())){
trans.add(rot);
}

// Shift shape
Translate translate = new Translate(position);
trans.add(translate);

applyGeometryTransform(trans);
}

Expand All @@ -47,4 +79,10 @@ private static Rotate createRotateTo(Coord3d from, Coord3d to) {
double angle = Math.acos(from.dot(to) / fromMag / toMag) * 180f / Math.PI;
return new Rotate(angle, from.cross(to));
}

public Vector3d getVector() {
return vector;
}


}
Loading

0 comments on commit ffc84e1

Please sign in to comment.