Skip to content

Commit

Permalink
Land picking
Browse files Browse the repository at this point in the history
  • Loading branch information
YvesBoyadjian committed Jan 1, 2024
1 parent 25f0d59 commit 9fc4c22
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 22 deletions.
91 changes: 87 additions & 4 deletions MRIEditor/src/mrieditor/parts/View3DPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.widgets.TextFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
Expand All @@ -49,11 +51,15 @@
import jscenegraph.database.inventor.SbVec3f;
import jscenegraph.database.inventor.SbViewportRegion;
import jscenegraph.database.inventor.SoDB;
import jscenegraph.database.inventor.SoPickedPoint;
import jscenegraph.database.inventor.actions.SoRayPickAction;
import jscenegraph.database.inventor.actions.SoGLRenderAction.TransparencyType;
import jscenegraph.database.inventor.events.SoKeyboardEvent;
import jscenegraph.database.inventor.events.SoMouseButtonEvent;
import jscenegraph.database.inventor.misc.SoNotRec;
import jscenegraph.database.inventor.nodes.SoCamera;
import jscenegraph.database.inventor.nodes.SoLineSet;
import jscenegraph.database.inventor.nodes.SoNode;
import jscenegraph.database.inventor.nodes.SoOrthographicCamera;
import jscenegraph.database.inventor.nodes.SoPerspectiveCamera;
import jscenegraph.database.inventor.sensors.SoDataSensor;
Expand Down Expand Up @@ -83,6 +89,10 @@ public class View3DPart {
private boolean ruler;

private SbVec3f previousRulerPosition = new SbVec3f();

private boolean polylineDraw;

private MouseListener polylineDrawMouseListener;

@Inject
private MPart part;
Expand Down Expand Up @@ -128,6 +138,17 @@ public void widgetSelected(SelectionEvent e) {

});

Button button4 = new Button(upperToolBar, SWT.TOGGLE);
button4.setText("Polyline Draw");

button4.addSelectionListener(new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {
togglePolylineDraw();
}
});

Composite intermediate = new Composite(parent,SWT.NONE);
intermediate.setLayout(new FillLayout());
intermediate.setLayoutData(new GridData(GridData.FILL_BOTH));
Expand Down Expand Up @@ -177,6 +198,31 @@ protected void processMouseScrollEvent(int count) {
}
}

protected void onFire(SoMouseButtonEvent event) {
if (polylineDraw) {

SbViewportRegion vr = this.getSceneHandler().getViewportRegion();
SoNode sg_ = this.getSceneHandler().getSceneGraph();

SoRayPickAction fireAction = new SoRayPickAction(vr);

fireAction.setPoint(event.getPosition(vr));

fireAction.apply(sg_);

SoPickedPoint pp = fireAction.getPickedPoint();
if( pp == null) {
fireAction.destructor();
return;
}
else {
SbVec3f i = pp.getPoint();
System.out.println("x = "+i.getX()+", y = "+i.getY()+", z = "+i.getZ());
}
fireAction.destructor();
}
}

public void idle() {
SoCamera camera = getCameraController().getCamera();

Expand Down Expand Up @@ -253,6 +299,18 @@ else if (camera != null) {
}
};
walkViewer.buildWidget(style);

polylineDrawMouseListener = new MouseAdapter() {

@Override
public void mouseDown(MouseEvent e) {
int mouseX = e.x;
int mouseY = e.y;

System.out.println("x = "+ mouseX +", y = " + mouseY);
}

};
}

@Focus
Expand Down Expand Up @@ -288,7 +346,7 @@ private void load3DModel() {
MainGLFW.Z_TRANSLATION,
max_i,
trails,
null);
null, false);

walkViewer.setHeadlight(false);

Expand Down Expand Up @@ -381,6 +439,15 @@ public void run(Object data, SoSensor sensor) {

walkViewer.start();

setEventCallback(true);

System.out.println("Load 3D Model");

}

private void setEventCallback(boolean set) {

if (set) {
walkViewer.setEventCallback(new SoQtGLWidget.eventCBType() {

@Override
Expand All @@ -404,9 +471,10 @@ else if (type == EventType.MOUSE_EVENT_MOUSE_MOVE) {
}

}, null);

System.out.println("Load 3D Model");

}
else {
walkViewer.setEventCallback(null, null);
}
}

private void upperView() {
Expand Down Expand Up @@ -453,4 +521,19 @@ private void toggleRuler() {
previousRulerPosition.copyFrom(walkViewer.getCameraController().getCamera().position.getValue());
}
}

private void togglePolylineDraw() {
polylineDraw = !polylineDraw;

if (polylineDraw) {
walkViewer.getGLWidget().addMouseListener(polylineDrawMouseListener);
walkViewer.setViewing(false);
setEventCallback(false);
}
else {
walkViewer.getGLWidget().removeMouseListener(polylineDrawMouseListener);
walkViewer.setViewing(true);
setEventCallback(true);
}
}
}
40 changes: 39 additions & 1 deletion application-swt/src/application/swt/SoQtWalkViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import jscenegraph.database.inventor.events.SoEvent;
import jscenegraph.database.inventor.events.SoKeyboardEvent;
import jscenegraph.database.inventor.events.SoLocation2Event;
import jscenegraph.database.inventor.events.SoMouseButtonEvent;
import jscenegraph.database.inventor.nodes.SoCamera;
import jscenegraph.database.inventor.sensors.SoIdleSensor;
import jscenegraph.database.inventor.sensors.SoSensor;
Expand Down Expand Up @@ -258,7 +259,39 @@ else if(
}
return false;
}



protected void onFire(SoMouseButtonEvent event) {
// do nothing by default
}

protected boolean processSoMouseButtonEvent(SoMouseButtonEvent event) {
if (SoMouseButtonEvent.SO_MOUSE_PRESS_EVENT(event, SoMouseButtonEvent.Button.BUTTON1)) {
onFire(event);
return true;
}
if (SoMouseButtonEvent.SO_MOUSE_RELEASE_EVENT(event, SoMouseButtonEvent.Button.BUTTON1)) {
return true;
}
if (SoMouseButtonEvent.SO_MOUSE_PRESS_EVENT(event, SoMouseButtonEvent.Button.BUTTON2)) {
// onAim(event,true);
return true;
}
if (SoMouseButtonEvent.SO_MOUSE_RELEASE_EVENT(event, SoMouseButtonEvent.Button.BUTTON2)) {
// onAim(event,false);
return true;
}
if (SoMouseButtonEvent.SO_MOUSE_PRESS_EVENT(event, SoMouseButtonEvent.Button.BUTTON3)) {
// onAim(event,true);
return true;
}
if (SoMouseButtonEvent.SO_MOUSE_RELEASE_EVENT(event, SoMouseButtonEvent.Button.BUTTON3)) {
// onAim(event,false);
return true;
}
return false;
}

protected void updateLocation(SbVec3f diff_position) {

double currentTimeSec = System.nanoTime()/1.0e9;
Expand Down Expand Up @@ -315,6 +348,11 @@ else if (event.isOfType(SoKeyboardEvent.getClassTypeId()))
result = processSoKeyboardEvent((SoKeyboardEvent)
(event));
}
else if (event.isOfType(SoMouseButtonEvent.getClassTypeId()))
{
result = processSoMouseButtonEvent((SoMouseButtonEvent)
(event));
}

if (!result)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,13 @@ public SceneGraphIndexedFaceSetShader(

chunks = new ChunkArray(w,h,full_island_w);

float West_Bounding_Coordinate = -122.00018518518522f;
float East_Bounding_Coordinate= -121.74981481481484f;
float North_Bounding_Coordinate= 47.000185185185195f;
float South_Bounding_Coordinate= 46.749814814814826f;
double West_Bounding_Coordinate = -122.00018518518522;
double East_Bounding_Coordinate= -121.74981481481484;
double North_Bounding_Coordinate= 47.000185185185195;
double South_Bounding_Coordinate= 46.749814814814826;

float delta_y_deg = (North_Bounding_Coordinate - South_Bounding_Coordinate)/wImageW;
float delta_x_deg = (East_Bounding_Coordinate - West_Bounding_Coordinate)/hImageW;
float delta_y_deg = (float)(North_Bounding_Coordinate - South_Bounding_Coordinate)/wImageW;
float delta_x_deg = (float)(East_Bounding_Coordinate - West_Bounding_Coordinate)/hImageW;

float earth_radius = 6371e3f;
float earth_circumference = earth_radius * 2 * (float)Math.PI;
Expand Down
22 changes: 11 additions & 11 deletions jscenegraph/src/jscenegraph/database/inventor/SbLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ public void copyFrom(Object other) {
// two dimensions are the axes of the aligned plane we will
// use to project the triangle.
//
float xAbs = norm.getValueRead()[0] < 0.0 ? -norm.getValueRead()[0] : norm.getValueRead()[0];
float yAbs = norm.getValueRead()[1] < 0.0 ? -norm.getValueRead()[1] : norm.getValueRead()[1];
float zAbs = norm.getValueRead()[2] < 0.0 ? -norm.getValueRead()[2] : norm.getValueRead()[2];
float xAbs = norm.getCoord(0) < 0.0 ? -norm.getCoord(0) : norm.getCoord(0);
float yAbs = norm.getCoord(1) < 0.0 ? -norm.getCoord(1) : norm.getCoord(1);
float zAbs = norm.getCoord(2) < 0.0 ? -norm.getCoord(2) : norm.getCoord(2);
int axis0, axis1;

if (xAbs > yAbs && xAbs > zAbs) {
Expand All @@ -312,19 +312,19 @@ else if (yAbs > zAbs) {
// Since we deal with only 2 components, we can avoid the
// third computation.
//
float intersection0 = getPosition().getValueRead()[axis0] + t * getDirection().getValueRead()[axis0];
float intersection1 = getPosition().getValueRead()[axis1] + t * getDirection().getValueRead()[axis1];
float intersection0 = getPosition().getCoord(axis0) + t * getDirection().getCoord(axis0);
float intersection1 = getPosition().getCoord(axis1) + t * getDirection().getCoord(axis1);

final SbVec2fSingle diff0 = new SbVec2fSingle(), diff1 = new SbVec2fSingle(), diff2 = new SbVec2fSingle();
boolean isInter = false;
float alpha = Float.NaN, beta;

diff0.getValue()[0] = intersection0 - v0.getValueRead()[axis0];
diff0.getValue()[1] = intersection1 - v0.getValueRead()[axis1];
diff1.getValue()[0] = v1.getValueRead()[axis0] - v0.getValueRead()[axis0];
diff1.getValue()[1] = v1.getValueRead()[axis1] - v0.getValueRead()[axis1];
diff2.getValue()[0] = v2.getValueRead()[axis0] - v0.getValueRead()[axis0];
diff2.getValue()[1] = v2.getValueRead()[axis1] - v0.getValueRead()[axis1];
diff0.getValue()[0] = intersection0 - v0.getCoord(axis0);
diff0.getValue()[1] = intersection1 - v0.getCoord(axis1);
diff1.getValue()[0] = v1.getCoord(axis0) - v0.getCoord(axis0);
diff1.getValue()[1] = v1.getCoord(axis1) - v0.getCoord(axis1);
diff2.getValue()[0] = v2.getCoord(axis0) - v0.getCoord(axis0);
diff2.getValue()[1] = v2.getCoord(axis1) - v0.getCoord(axis1);

// Note: This code was rearranged somewhat from the code in
// Graphics Gems to provide a little more numeric
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ usually fails without the SoShapeHints node (try commenting it out,
import jscenegraph.database.inventor.SbMatrix;
import jscenegraph.database.inventor.SbVec3f;
import jscenegraph.database.inventor.SbVec3fSingle;
import jscenegraph.database.inventor.SbVec3s;
import jscenegraph.database.inventor.SbVec4f;
import jscenegraph.database.inventor.SoDebug;
import jscenegraph.database.inventor.SoPickedPoint;
Expand Down Expand Up @@ -1392,6 +1393,17 @@ else if (tbind != Binding.OVERALL) {
getVertexData(state, coords, normals, normalsShort, cindices,
nindices, tindices, mindices, numindices,
sendNormals, normalCacheUsed);

if (normals[0] == null && normalsShort[0] != null) {
SbVec3sArray source = normalsShort[0];
int length = source.length();
SbVec3fArray destination = SbVec3fArray.allocate(length);
for (int i=0; i<length; i++) {
SbVec3s dummy = source.getOFast(i);
destination.setValueXYZ(i, (float)dummy.getX()/Short.MAX_VALUE, (float)dummy.getY()/Short.MAX_VALUE, (float)dummy.getZ()/Short.MAX_VALUE);
}
normals[0] = destination;
}

SoTextureCoordinateBundle tb = new SoTextureCoordinateBundle(action, false, false);
doTextures = tb.needCoordinates();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jscenegraph.database.inventor.SbVec4fSingle;
import jscenegraph.database.inventor.SoType;
import jscenegraph.database.inventor.actions.SoGLRenderAction;
import jscenegraph.database.inventor.actions.SoRayPickAction;
import jscenegraph.database.inventor.fields.SoField;
import jscenegraph.database.inventor.fields.SoFieldData;
import jscenegraph.database.inventor.fields.SoSFBool;
Expand Down Expand Up @@ -86,5 +87,6 @@ public void destructor() {
initClass() {
SoSubNode.SO__NODE_INIT_CLASS(SoOptimizationFromXYUV.class, "OptimizationFromXYUV", SoNode.class);
SO_ENABLE(SoGLRenderAction.class, SoGLFromXYUVElement.class);
SO_ENABLE(SoRayPickAction.class, SoGLFromXYUVElement.class);
}
}

0 comments on commit 9fc4c22

Please sign in to comment.