Skip to content

FIX: edit->Add compass #285

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
132 changes: 95 additions & 37 deletions src/main/java/sc/iview/commands/edit/AddOrientationCompass.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@
package sc.iview.commands.edit;

import graphics.scenery.*;
import org.joml.Vector3f;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector2f;
import org.joml.Vector3f;
import org.scijava.command.Command;
import org.scijava.command.CommandService;
import org.scijava.plugin.Menu;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import sc.iview.SciView;
import sc.iview.node.Line3D;

import java.util.HashMap;
import java.lang.Math;

import static sc.iview.commands.MenuWeights.EDIT;
import static sc.iview.commands.MenuWeights.EDIT_ADD_COMPASS;
Expand All @@ -50,6 +47,7 @@
* Command to orientation compass (R,G,B cylinders oriented along X,Y,Z axes, respectively) to the scene
*
* @author Vladimir Ulman
* @author Kyle Harrington
*
*/
@Plugin(type = Command.class, menuRoot = "SciView", //
Expand All @@ -60,30 +58,39 @@ public class AddOrientationCompass implements Command {
@Parameter
private SciView sciView;

@Parameter
@Parameter(label = "Length of each bar:", stepSize = "0.1", min = "0")
private float axisLength = 0.1f;

@Parameter
private float AXESBARRADIUS = 0.001f;
@Parameter(label = "Thickness of each bar:", stepSize = "0.001", min = "0")
private float axisBarRadius = 0.001f;

@Parameter
//@Parameter -- waits until scijava can offer color-picker dialog
private Vector3f xColor = new Vector3f(1f,0f,0f);
//NB: RGB colors ~ XYZ axes

@Parameter
//@Parameter
private Vector3f yColor = new Vector3f(0f,1f,0f);

@Parameter
//@Parameter
private Vector3f zColor = new Vector3f(0f,0f,1f);

@Parameter(label = "Show in overlay in top-left corner:")
boolean attachToCam = true;

@Parameter(label = "Show as controllable node in the scene:")
boolean showInTheScene = false;


private Node makeAxis( float axisLength, float angleX, float angleY, float angleZ, Vector3f color ) {
Cylinder axisNode = new Cylinder(AXESBARRADIUS, axisLength,4);
axisNode.setName("compass axis: X");
Cylinder axisNode = new Cylinder(axisBarRadius, axisLength,4);
axisNode.setName("compass bar");
axisNode.setRotation( new Quaternionf().rotateXYZ( angleX, angleY, angleZ ) );
axisNode.getMaterial().getDiffuse().set(color);
axisNode.getMaterial().setDepthTest(Material.DepthTest.Always);
axisNode.getMaterial().getBlending().setTransparent(true);

Icosphere axisCap = new Icosphere(AXESBARRADIUS, 2);
Icosphere axisCap = new Icosphere(axisBarRadius, 2);
axisCap.setName("cap of the bar");
axisCap.setPosition(new Vector3f(0, axisLength, 0));
axisCap.getMaterial().getDiffuse().set(color);
axisCap.getMaterial().setDepthTest(Material.DepthTest.Always);
Expand All @@ -93,47 +100,98 @@ private Node makeAxis( float axisLength, float angleX, float angleY, float angle
return axisNode;
}

@Override
public void run() {
private Node createCompass() {
final Node root = new Node("Scene orientation compass");

//NB: RGB colors ~ XYZ axes
//x axis:
Node axisNode = makeAxis( axisLength, 0,0,(float)(-0.5*Math.PI), xColor );
axisNode.setName("compass axis: X");
root.addChild( axisNode );

//y axis:
axisNode = makeAxis( axisLength, 0,0, 0, yColor );

axisNode.setName("compass axis: Y");
root.addChild( axisNode );

//z axis:
axisNode = makeAxis( axisLength, (float)(0.5*Math.PI),0,0, zColor );
axisNode.setName("compass axis: Z");
root.addChild( axisNode );

sciView.addNode( root );

sciView.getCamera().addChild(root);

root.getUpdate().add(() -> {
final Camera cam = sciView.getCamera();
root.setPosition(cam.viewportToView(new Vector2f(-0.9f, 0.7f)));
root.setRotation(new Quaternionf(sciView.getCamera().getRotation()).conjugate());
return null;
});

return root;
}

public static void main(String... args) throws Exception {
SciView sv = SciView.create();

CommandService command = sv.getScijavaContext().getService(CommandService.class);

HashMap<String, Object> argmap = new HashMap<>();
private Node createBox() {
final Node root = new Node("Scene representing box");

// X - red edges
final float halfPI = (float)(0.5*Math.PI);
final float edgeLen = 0.05f;
final float edgeRadius = 0.0008f;
for (int i = 0; i < 4; ++i)
{
final Node n = new Cylinder(edgeRadius,edgeLen,4);
n.getPosition().set( -0.5f * edgeLen );
n.getPosition().add( new Vector3f(0, i%2 *edgeLen, i < 2 ? 0 : edgeLen) );
n.setRotation( new Quaternionf().rotateXYZ(0,0,-halfPI).normalize() );
n.setName("box edge: X");
n.getMaterial().getDiffuse().set( new Vector3f(1,0,0) );
n.getMaterial().setDepthTest(Material.DepthTest.Always);
//n.getMaterial().getBlending().setTransparent(true);
root.addChild( n );
}

// Y - green edges
for (int i = 0; i < 4; ++i)
{
final Node n = new Cylinder(edgeRadius,edgeLen,4);
n.getPosition().set( -0.5f * edgeLen );
n.getPosition().add( new Vector3f(i%2 *edgeLen, 0, i < 2 ? 0 : edgeLen) );
n.setName("box edge: Y");
n.getMaterial().getDiffuse().set( new Vector3f(0,1,0) );
n.getMaterial().setDepthTest(Material.DepthTest.Always);
//n.getMaterial().getBlending().setTransparent(true);
root.addChild( n );
}

// Z - blue edges
for (int i = 0; i < 4; ++i)
{
final Node n = new Cylinder(edgeRadius,edgeLen,4);
n.getPosition().set( -0.5f * edgeLen );
n.getPosition().add( new Vector3f(i%2 *edgeLen, i < 2 ? 0 : edgeLen, 0) );
n.setRotation( new Quaternionf().rotateXYZ(halfPI,0,0).normalize() );
n.setName("box edge: Z");
n.getMaterial().getDiffuse().set( new Vector3f(0,0,1) );
n.getMaterial().setDepthTest(Material.DepthTest.Always);
//n.getMaterial().getBlending().setTransparent(true);
root.addChild( n );
}

return root;
}

command.run(AddOrientationCompass.class, true, argmap);
@Override
public void run() {
if (showInTheScene) sciView.addNode( createCompass() );
if (attachToCam) {
final Node compass = createBox();
sciView.getCamera().addChild( compass );
compass.setWantsComposeModel(false);

final float xStretch = sciView.getCamera().getProjection().m00();
final float yStretch = sciView.getCamera().getProjection().m11();
//
final Matrix4f positionDaBox = new Matrix4f();
positionDaBox.set(1,0,0,0, //1st col
0,1,0,0, //2nd col
0,0,1,0,
-0.9f/xStretch,+0.85f/yStretch,-1.0f,1);

compass.getUpdate().add(() -> {
sciView.getCamera().getRotation().get( compass.getModel() );
compass.getModel().mulLocal( positionDaBox );
return null;
});
}
}
}