Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

How to get id of closest vertex?

edited February 2016

I am trying to get a ball moving on a mesh and have it react according to the slope of the mesh. I have calculated all the slopes of the vertexes and saved the values in an arraylist called slope. I can find the closest vertex to my moving ball, but I am not able to figure out how to access the id number of the vertex to get the slope out of my arraylist. Any help will be appreciated.

Thanks

the code i used

``````import processing.opengl.*;
import toxi.geom.*;
import java.util.Iterator;
import java.util.*;
import peasy.*;
import wblut.math.*;
import wblut.processing.*;
import wblut.core.*;
import wblut.hemesh.*;
import wblut.geom.*;

ArrayList Slope = new ArrayList();
HE_Mesh mesh;
WB_KDTree vertexTree;
WB_Render render;
WB_Coord mnorm;
PeasyCam cam;

int speedx =1;
int speedy =1;

void setup() {

size(700, 700, P3D);

frameRate(30);
smooth();

cam = new PeasyCam(this, 1200);

mesh = new HEC_FromOBJFile(sketchPath("meshtoimport2.obj")).create();
vertexTree = mesh.getVertexTree();

int novert = mesh.getNumberOfVertices();

for (int i=0; i< novert; i++) {
mnorm = mesh.getVertexNormal(i);

float xnPos = (Float)  mnorm.xf();
float ynPos = (Float)  mnorm.yf();
float znPos = (Float)  mnorm.zf();

Vec3D mnormv = new Vec3D(xnPos, ynPos, znPos);
Vec3D mvert = new Vec3D(0, 0, 1);

float slope = mnormv.angleBetween(mvert);
slope = degrees(slope);

}

render = new WB_Render( this );
}

void draw() {
background(0);
lights();
noStroke();
render.drawFaces(mesh);

stroke(255, 0, 255);
strokeWeight(.5);
render.drawFaceNormals(2, mesh);

speedx = speedx+1;
speedy = speedy+1;
WB_Point tpos = new WB_Point(speedx, speedy, 0);
WB_Coord ptonmesh = mesh.getClosestPoint(tpos, vertexTree);
HE_Vertex meshPt = mesh.getClosestVertex(tpos, vertexTree);

float xPos = (Float) ptonmesh.xf();
float yPos = (Float)  ptonmesh.yf();
float zPos = (Float)  ptonmesh.zf();

fill(60, 60, 200, 200);
translate(xPos, yPos, zPos);
sphere(15);
}
``````
Tagged:

• highlight code, press ctrl-o to format.

• edited February 2016

thanks, do you know how to get the vertex id?

• Before line 50 just store the id in the slope

• Thanks Chrisir, the thing is I have the id from line 50. Its when I run line 70 I get the the closest vertex, but not the id. Is it possible to get the id in HE MEsh or by some other method?

• I mean is there any way in HEMesh to get the id instead of closest vertex on line 70?

you could use a Hashmap<HE_Vertex, Integer>, store them during your setup loop, use it to lookup the index in draw...

(if you're only going to use the index to look up the slope then you can put the slope into the hashmap instead of the index)

• Thanks thats solved it :)

Here is the updated code if anyone else is looking for it

``````import processing.opengl.*;
import toxi.geom.*;
import java.util.Iterator;
import java.util.*;
import peasy.*;
import wblut.math.*;
import wblut.processing.*;
import wblut.core.*;
import wblut.hemesh.*;
import wblut.geom.*;
import java.util.Map;

HashMap<WB_Coord, Integer> Slope = new HashMap<WB_Coord, Integer>();

ArrayList xMax = new ArrayList();
ArrayList yMax = new ArrayList();

HE_Mesh mesh;
WB_KDTree vertexTree;
WB_Render render;
WB_Coord mnorm;
WB_Coord mface;
PeasyCam cam;

int speedx =1;
int speedy =1;

void setup() {

size(700, 700, P3D);

frameRate(30);
smooth();

cam = new PeasyCam(this, 1200);

mesh = new HEC_FromOBJFile(sketchPath("meshtoimport2.obj")).create();
vertexTree = mesh.getVertexTree();

int novert = mesh.getNumberOfVertices();

for (int i=0; i< novert; i++) {
mnorm = mesh.getVertexNormal(i);
WB_Coord vertex1 = mesh.getVertex(i);

float xnPos = (Float)  mnorm.xf();
float ynPos = (Float)  mnorm.yf();
float znPos = (Float)  mnorm.zf();

float xfPos = (Float)  vertex1.xf();
float yfPos = (Float)  vertex1.yf();

Vec3D mnormv = new Vec3D(xnPos, ynPos, znPos);
Vec3D mvert = new Vec3D(0, 0, 1);

float slope = mnormv.angleBetween(mvert);

slope = degrees(slope);

int slopeint = (int)slope;
Slope.put(vertex1, slopeint);
}

render = new WB_Render( this );
float xmaxint = Collections.max(xMax);
float ymaxint = Collections.max(yMax);
println(xmaxint);
println(ymaxint);
}

void draw() {
background(0);

lights();
noStroke();

render.drawFaces(mesh);

stroke(255, 0, 255);
strokeWeight(.5);
render.drawFaceNormals(2, mesh);

speedx = speedx+1;
speedy = speedy+1;
WB_Point tpos = new WB_Point(speedx, speedy, 0);
WB_Coord meshPt = mesh.getClosestVertex(tpos, vertexTree);

float xPos = (Float)  meshPt.xf();
float yPos = (Float)  meshPt.yf();
float zPos = (Float)  meshPt.zf();

int Sval = Slope.get(meshPt);
println(Sval);

fill(60, 60, 200, 200);
translate(xPos, yPos, zPos);
sphere(15);
}
``````