Have jReality programming problems or questions? Post them here.
-
pederico
- Posts: 5
- Joined: Wed 2. Apr 2014, 22:55
Post
by pederico » Wed 2. Apr 2014, 23:04
Hello,
I have combined the AddPoints and the DragEventTool examples from the tutorials. Please find the resulting source code below. What I am trying to do is after dragging a vertex to directly update the coordinates in the List points and in the IndexedLineSetFactory lsf. However, it seems I am doing something wrong, because during the drag operation the coordinates seems to get completly messed up. Your help would be much appreciated.
Code: Select all
package de.jreality.tutorial.tool;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import de.jreality.geometry.IndexedLineSetFactory;
import de.jreality.math.Matrix;
import de.jreality.math.Rn;
import de.jreality.plugin.JRViewer;
import de.jreality.plugin.content.ContentTools;
import de.jreality.plugin.content.DirectContent;
import de.jreality.scene.Appearance;
import de.jreality.scene.SceneGraphComponent;
import de.jreality.scene.pick.PickResult;
import de.jreality.scene.tool.AbstractTool;
import de.jreality.scene.tool.InputSlot;
import de.jreality.scene.tool.ToolContext;
import de.jreality.shader.DefaultGeometryShader;
import de.jreality.shader.DefaultLineShader;
import de.jreality.shader.DefaultPointShader;
import de.jreality.shader.ShaderUtility;
import de.jreality.tools.ClickWheelCameraZoomTool;
import de.jreality.tools.DragEventTool;
import de.jreality.tools.PointDragEvent;
import de.jreality.tools.PointDragListener;
import de.jreality.toolsystem.ToolUtility;
public class AddPointsExample extends AbstractTool {
static List<double[]> points = new ArrayList<double[]>();
static IndexedLineSetFactory lsf = new IndexedLineSetFactory();
double offset = 5;
public AddPointsExample() {
addCurrentSlot(InputSlot.SHIFT_LEFT_BUTTON, "add a new point");
updateGeometry();
}
public static void pa(double[] ar, String msg)
{
System.out.println(msg + "--------------------");
for(int i=0; i < ar.length; i++)
System.out.print(ar[i]+" ");
System.out.println();
}
@Override
public void perform(ToolContext tc) {
if (!tc.getAxisState(InputSlot.SHIFT_LEFT_BUTTON).isPressed()) return;
int idx = -1;
if(tc.getCurrentPick() != null && tc.getCurrentPick().getPickType() == PickResult.PICK_TYPE_LINE)
{
idx = tc.getCurrentPick().getIndex() + 1;
}
// determine the pointer transformation:
// translation is the mouse pointer on the near clipping plane
// z-axis is the direction of the mouse ray out of the screen
// for a 6DOF input device, it is the position/orientation of the device
Matrix m = new Matrix(tc.getTransformationMatrix(InputSlot.POINTER_TRANSFORMATION));
// we compute the coordinates of the new point in world coordinates
double[] foot = m.getColumn(3);
double[] dir = m.getColumn(2);
double[] offset = Rn.times(null, -5, dir);
double[] newPoint = Rn.add(null, foot, offset);
newPoint = ToolUtility.worldToLocal(tc, newPoint);
if(idx == -1)
points.add(newPoint);
else
points.add(idx, newPoint);
updateGeometry();
}
private void updateGeometry() {
int n = points.size();
// set new vertices
lsf.setVertexCount(n);
if (n>0)
{
lsf.setVertexCoordinates(points.toArray(new double[0][]));
}
if (n>1) {
// compute and set new edge indices:
int[][] idx = new int[n-1][2];
for (int i=1; i<n; i++) {
idx[i-1][0] = i-1;
idx[i-1][1] = i;
}
lsf.setEdgeCount(n-1);
lsf.setEdgeIndices(idx);
}
lsf.update();
}
public static void main(String[] args) {
AddPointsExample example = new AddPointsExample();
SceneGraphComponent cmp = new SceneGraphComponent();
cmp.setGeometry(example.lsf.getGeometry());
cmp.addTool(example);
Appearance ap = new Appearance();
cmp.setAppearance(ap);
setupAppearance(ap);
DragEventTool t = new DragEventTool();
t.addPointDragListener(new PointDragListener() {
public void pointDragStart(PointDragEvent e) {
System.out.println("drag start of vertex no "+e.getIndex());
}
public void pointDragged(PointDragEvent e) {
AddPointsExample.points.set(e.getIndex(), np);
AddPointsExample.lsf.setVertexCoordinates(AddPointsExample.points.toArray(new double[0][]));
AddPointsExample.lsf.update();
public void pointDragEnd(PointDragEvent e) {
}
});
cmp.addTool(t);
JRViewer.display(cmp);
}
private static void setupAppearance(Appearance ap) {
DefaultGeometryShader dgs;
DefaultLineShader dls;
DefaultPointShader dpts;
dgs = ShaderUtility.createDefaultGeometryShader(ap, true);
dgs.setShowFaces(true);
dgs.setShowLines(true);
dgs.setShowPoints(true);
dls = (DefaultLineShader) dgs.createLineShader("default");
dls.setDiffuseColor(Color.yellow);
dls.setTubeRadius(.03);
dpts = (DefaultPointShader) dgs.createPointShader("default");
dpts.setDiffuseColor(Color.red);
dpts.setPointRadius(.05);
}
}
-
Andre
- Posts: 226
- Joined: Fri 18. Sep 2009, 11:30
Post
by Andre » Wed 23. Apr 2014, 12:11
This Code should work. I simply changed your code a bit.
Code: Select all
package de.jreality.tutorial.tool;
import java.util.ArrayList;
import java.util.List;
import de.jreality.geometry.IndexedLineSetFactory;
import de.jreality.math.Matrix;
import de.jreality.math.Rn;
import de.jreality.plugin.JRViewer;
import de.jreality.scene.Appearance;
import de.jreality.scene.PointSet;
import de.jreality.scene.SceneGraphComponent;
import de.jreality.scene.data.Attribute;
import de.jreality.scene.data.StorageModel;
import de.jreality.scene.pick.PickResult;
import de.jreality.scene.tool.AbstractTool;
import de.jreality.scene.tool.InputSlot;
import de.jreality.scene.tool.ToolContext;
import de.jreality.shader.Color;
import de.jreality.shader.DefaultGeometryShader;
import de.jreality.shader.DefaultLineShader;
import de.jreality.shader.DefaultPointShader;
import de.jreality.shader.ShaderUtility;
import de.jreality.tools.DragEventTool;
import de.jreality.tools.PointDragEvent;
import de.jreality.tools.PointDragListener;
import de.jreality.toolsystem.ToolUtility;
public class AddPointsExample2 extends AbstractTool {
static List<double[]> points = new ArrayList<double[]>();
static IndexedLineSetFactory lsf = new IndexedLineSetFactory();
double offset = 5;
public AddPointsExample2() {
addCurrentSlot(InputSlot.SHIFT_LEFT_BUTTON, "add a new point");
updateGeometry();
}
public static void pa(double[] ar, String msg)
{
System.out.println(msg + "--------------------");
for(int i=0; i < ar.length; i++)
System.out.print(ar[i]+" ");
System.out.println();
}
@Override
public void perform(ToolContext tc) {
if (!tc.getAxisState(InputSlot.SHIFT_LEFT_BUTTON).isPressed()) return;
int idx = -1;
if(tc.getCurrentPick() != null && tc.getCurrentPick().getPickType() == PickResult.PICK_TYPE_LINE)
{
idx = tc.getCurrentPick().getIndex() + 1;
}
// determine the pointer transformation:
// translation is the mouse pointer on the near clipping plane
// z-axis is the direction of the mouse ray out of the screen
// for a 6DOF input device, it is the position/orientation of the device
Matrix m = new Matrix(tc.getTransformationMatrix(InputSlot.POINTER_TRANSFORMATION));
// we compute the coordinates of the new point in world coordinates
double[] foot = m.getColumn(3);
double[] dir = m.getColumn(2);
double[] offset = Rn.times(null, -5, dir);
double[] newPoint = Rn.add(null, foot, offset);
newPoint = ToolUtility.worldToLocal(tc, newPoint);
if(idx == -1)
points.add(newPoint);
else
points.add(idx, newPoint);
updateGeometry();
}
private void updateGeometry() {
int n = points.size();
// set new vertices
lsf.setVertexCount(n);
if (n>0)
{
lsf.setVertexCoordinates(points.toArray(new double[0][]));
}
if (n>1) {
// compute and set new edge indices:
int[][] idx = new int[n-1][2];
for (int i=1; i<n; i++) {
idx[i-1][0] = i-1;
idx[i-1][1] = i;
}
lsf.setEdgeCount(n-1);
lsf.setEdgeIndices(idx);
}
lsf.update();
}
public static void main(String[] args) {
AddPointsExample example = new AddPointsExample();
SceneGraphComponent cmp = new SceneGraphComponent();
cmp.setGeometry(example.lsf.getGeometry());
cmp.addTool(example);
Appearance ap = new Appearance();
cmp.setAppearance(ap);
setupAppearance(ap);
DragEventTool t = new DragEventTool();
t.addPointDragListener(new PointDragListener() {
public void pointDragStart(PointDragEvent e) {
System.out.println("drag start of vertex no "+e.getIndex());
}
@Override
public void pointDragEnd(PointDragEvent e) {
}
@Override
public void pointDragged(PointDragEvent e) {
PointSet pointSet = e.getPointSet();
double[][] points=new double[pointSet.getNumPoints()][];
pointSet.getVertexAttributes(Attribute.COORDINATES).toDoubleArrayArray(points);
points[e.getIndex()]=e.getPosition();
pointSet.setVertexAttributes(Attribute.COORDINATES,StorageModel.DOUBLE_ARRAY.array(3).createReadOnly(points));
}
});
cmp.addTool(t);
JRViewer.display(cmp);
}
private static void setupAppearance(Appearance ap) {
DefaultGeometryShader dgs;
DefaultLineShader dls;
DefaultPointShader dpts;
dgs = ShaderUtility.createDefaultGeometryShader(ap, true);
dgs.setShowFaces(true);
dgs.setShowLines(true);
dgs.setShowPoints(true);
dls = (DefaultLineShader) dgs.createLineShader("default");
dls.setDiffuseColor(Color.yellow);
dls.setTubeRadius(.03);
dpts = (DefaultPointShader) dgs.createPointShader("default");
dpts.setDiffuseColor(Color.red);
dpts.setPointRadius(.05);
}
}
-
Andre
- Posts: 226
- Joined: Fri 18. Sep 2009, 11:30
Post
by Andre » Thu 24. Apr 2014, 16:12
Ups, I it seems there is still a bug.
I followed the bug back to the de.jreality.tools.DragEventTool (src-tool). I fixed it for the next release. If you want to change it temporarily you have to edit the DragEventTool line 217ff
Code: Select all
double[] translation = { translation3[0], translation3[1],
translation3[2], 1 };
double[] position = new double[4];
position = Rn.add(null, translation, pickPoint);
position[3] = 1.; //bugfix
if (pickType == PickResult.PICK_TYPE_OBJECT) {
firePrimitiveDragged(position);
} else if (pickType == PickResult.PICK_TYPE_POINT) {
firePointDragged(position);
} else if (pickType == PickResult.PICK_TYPE_LINE) {
fireLineDragged(translation, position);
} else if (pickType == PickResult.PICK_TYPE_FACE) {
fireFaceDragged(translation, position);
}
And here comes the new TutorialCode for you:
Code: Select all
package de.jreality.tutorial.tool;
import java.util.ArrayList;
import java.util.List;
import de.jreality.geometry.IndexedLineSetFactory;
import de.jreality.math.Matrix;
import de.jreality.math.Rn;
import de.jreality.plugin.JRViewer;
import de.jreality.scene.Appearance;
import de.jreality.scene.Geometry;
import de.jreality.scene.SceneGraphComponent;
import de.jreality.scene.pick.PickResult;
import de.jreality.scene.tool.AbstractTool;
import de.jreality.scene.tool.InputSlot;
import de.jreality.scene.tool.ToolContext;
import de.jreality.shader.Color;
import de.jreality.shader.DefaultGeometryShader;
import de.jreality.shader.DefaultLineShader;
import de.jreality.shader.DefaultPointShader;
import de.jreality.shader.ShaderUtility;
import de.jreality.tools.DragEventTool;
import de.jreality.tools.PointDragEvent;
import de.jreality.tools.PointDragListener;
import de.jreality.toolsystem.ToolUtility;
public class AddPointsAndDragPointsExample extends AbstractTool {
static List<double[]> points = new ArrayList<double[]>();
static IndexedLineSetFactory lsf = new IndexedLineSetFactory();
double offset = 5;
public AddPointsAndDragPointsExample() {
addCurrentSlot(InputSlot.SHIFT_LEFT_BUTTON, "add a new point");
updateGeometry();
}
public static void pa(double[] ar, String msg)
{
System.out.println(msg + "--------------------");
for(int i=0; i < ar.length; i++)
System.out.print(ar[i]+" ");
System.out.println();
}
@Override
public void perform(ToolContext tc) {
if (!tc.getAxisState(InputSlot.SHIFT_LEFT_BUTTON).isPressed()) return;
int idx = -1;
if(tc.getCurrentPick() != null && tc.getCurrentPick().getPickType() == PickResult.PICK_TYPE_LINE)
{
idx = tc.getCurrentPick().getIndex() + 1;
}
// determine the pointer transformation:
// translation is the mouse pointer on the near clipping plane
// z-axis is the direction of the mouse ray out of the screen
// for a 6DOF input device, it is the position/orientation of the device
Matrix m = new Matrix(tc.getTransformationMatrix(InputSlot.POINTER_TRANSFORMATION));
// we compute the coordinates of the new point in world coordinates
double[] foot = m.getColumn(3);
double[] dir = m.getColumn(2);
double[] offset = Rn.times(null, -5, dir);
double[] newPoint = Rn.add(null, foot, offset);
newPoint = ToolUtility.worldToLocal(tc, newPoint);
if(idx == -1)
points.add(newPoint);
else
points.add(idx, newPoint);
updateGeometry();
}
private void updateGeometry() {
int n = points.size();
for(int i=0;i<n;i++){
points.get(i)[3]=1.;
}
// set new vertices
lsf.setVertexCount(n);
if (n>0)
{
lsf.setVertexCoordinates(points.toArray(new double[0][]));
}
if (n>1) {
// compute and set new edge indices:
int[][] idx = new int[n-1][2];
for (int i=1; i<n; i++) {
idx[i-1][0] = i-1;
idx[i-1][1] = i;
}
lsf.setEdgeCount(n-1);
lsf.setEdgeIndices(idx);
}
lsf.update();
}
public Geometry getGeometry(){
return lsf.getGeometry();
}
private static void setupAppearance(Appearance ap) {
DefaultGeometryShader dgs;
DefaultLineShader dls;
DefaultPointShader dpts;
dgs = ShaderUtility.createDefaultGeometryShader(ap, true);
dgs.setShowFaces(true);
dgs.setShowLines(true);
dgs.setShowPoints(true);
dls = (DefaultLineShader) dgs.createLineShader("default");
dls.setDiffuseColor(Color.yellow);
dls.setTubeRadius(.03);
dpts = (DefaultPointShader) dgs.createPointShader("default");
dpts.setDiffuseColor(Color.red);
dpts.setPointRadius(.05);
}
public static void main(String[] args) {
final AddPointsAndDragPointsExample example = new AddPointsAndDragPointsExample();
SceneGraphComponent cmp = new SceneGraphComponent();
cmp.setGeometry(example.getGeometry());
cmp.addTool(example);
Appearance ap = new Appearance();
cmp.setAppearance(ap);
setupAppearance(ap);
DragEventTool t = new DragEventTool();
t.addPointDragListener(new PointDragListener() {
public void pointDragStart(PointDragEvent e) {
System.out.println("drag start of vertex no "+e.getIndex());
}
@Override
public void pointDragEnd(PointDragEvent e) {
}
@Override
public void pointDragged(PointDragEvent e) {
points.set(e.getIndex(), e.getPosition());
example.updateGeometry();
}
});
cmp.addTool(t);
JRViewer.display(cmp);
}
}
-
pederico
- Posts: 5
- Joined: Wed 2. Apr 2014, 22:55
Post
by pederico » Thu 24. Apr 2014, 16:23
thank you for your help
-
Andre
- Posts: 226
- Joined: Fri 18. Sep 2009, 11:30
Post
by Andre » Thu 24. Apr 2014, 16:24
thx 4 bugtracking ;) and welcome to jReality