JavaFX TriangleMesh causes heavy overhead/slowness compared to normal Boxes - javafx

My goal is to create Boxes with individual images on each side, which I have been completing by using a TriangleMesh to create the boxes, and using a single texture to map the points over the Mesh.
The issue is that I have been experiencing heavy overhead (high ram, long load times and slowness when moving objects in the world) from creating these TriangleMesh, and I'm not sure why fully, but here are a few of my guesses.
I initially tried to use a total of 8 points and reuse the points when creating the individual faces, and wrap the textures, but for some reason the texcoords did not like using only 8 points. As I look at Box's class I see they use 24 points as well as I do, so it seems we cannot reuse points.
2.a I am loading the 3 images into an hbox and then taking a snapshot to then map the coordinates. I am wondering if somehow snapshot is causing issues, or if there is some issue with the amount of pixels.
2.b This further comes into question when I tried turning multiple boxes
that were stacked together into one large box and it still having a
ton of slowness, if not more. I would take a snapshot of x number
of images and then map them onto this one big Box. i.e., if I had
5x5x5 it would be one box with 5 images mapped on each side. Even
with 1 single mesh it still causes slowness. It speeds up if I edit
the "fitWidth/fitHeight" of the imageView which causes a quality
change. To me, it seems like the pixel amounts are the cause. The thing is, if I'm taking the same images and just multiplying them, why is it causing slowness? Especially since when doing a normal box, the single image I was using had more pixels than the other images, so I would think it would be faster by doing the trianglemesh myself.
I don't really have a working example since this was added to an already working program that has data, but I will try to create something if need be.
I'm just trying to figure out why making a TriangleMesh on my own, which has similar code to the Box's code except.
without the face smoothing group, but not sure that matters.
My face array is different, and a bit confused why they have 24 points but the face array only goes from 1-8
int[] faces = {
2, 2, 1, 1, 0, 0, //front
2, 2, 3, 3, 1, 1,
6, 6, 5, 5, 4, 4, //right
6, 6, 7, 7, 5, 5,
10, 10, 9, 9, 8, 8, //back
10, 10, 11, 11, 9, 9,
14, 14, 13, 13, 12, 12,//left
14 ,14, 15, 15, 13, 13,
18, 18, 17, 17, 16, 16,//top
18, 18, 19, 19, 17, 17,
22, 22, 21, 21, 20, 20, //bottom
22, 22, 23, 23, 21, 21`
My texCoords are 6 sets of pairs instead of the 1 set here.
static TriangleMesh createMesh(float w, float h, float d) {
// NOTE: still create mesh for degenerated box
float hw = w / 2f;
float hh = h / 2f;
float hd = d / 2f;
float points[] = {
-hw, -hh, -hd,
hw, -hh, -hd,
hw, hh, -hd,
-hw, hh, -hd,
-hw, -hh, hd,
hw, -hh, hd,
hw, hh, hd,
-hw, hh, hd};
float texCoords[] = {0, 0, 1, 0, 1, 1, 0, 1};
// Specifies hard edges.
int faceSmoothingGroups[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int faces[] = {
0, 0, 2, 2, 1, 1,
2, 2, 0, 0, 3, 3,
1, 0, 6, 2, 5, 1,
6, 2, 1, 0, 2, 3,
5, 0, 7, 2, 4, 1,
7, 2, 5, 0, 6, 3,
4, 0, 3, 2, 0, 1,
3, 2, 4, 0, 7, 3,
3, 0, 6, 2, 2, 1,
6, 2, 3, 0, 7, 3,
4, 0, 1, 2, 5, 1,
1, 2, 4, 0, 0, 3,
};
TriangleMesh mesh = new TriangleMesh(true);
mesh.getPoints().setAll(points);
mesh.getTexCoords().setAll(texCoords);
mesh.getFaces().setAll(faces);
mesh.getFaceSmoothingGroups().setAll(faceSmoothingGroups);
return mesh;
}
I also had heard that there were improvements to 3D with Version 9. I have been trying to update the past few days to 11 with issues (I had tried 10 before but a lot of my code needed fixings), and 9 isn't downloading so I wanted to ask before trying to put in more effort to get 11 working if it will in fact improve the situation, but I am still wondering why trianglmesh is so slowed comparatively. I'm maybe making 1000 boxes max.
Thank you
EDIT:
Example of how I create the images.
public BoxMesh(width, height,depth, int stack)
{
float width = width;
float height = height;
float depth = depth
List<ImageView> imageList = new ArrayList();
imageList.add(new ImageView(new Image("file:"C:\\file1.jpg"));
HBox h = new HBox();
Image image = null;
for(int i = 0; i < stack; i++)
{
image = new Image("file:"C:\\file2.jpg");
ImageView iv = new ImageView(image);
iv.setFitWidth(image.getWidth()/2); //decreases quality to
iv.setFitHeight(image.getHeight()/2); //speed up program
h.getChildren().add(iv);
}
imageList.add(new ImageView(h.snapshot(null,null)));
VBox v = new VBox();
Image image = null;
for(int i = 0; i < stack; i++)
{
image = new Image("file:"C:\\file3.jpg");
ImageView iv = new ImageView(image);
iv.setFitWidth(image.getWidth()/2);
iv.setFitHeight(image.getHeight()/2);
v.getChildren().add(iv);
}
imageList.add(new ImageView(v.snapshot(null, null)));
PhongMaterial p = new PhongMaterial();
HBox hb = new HBox();
hb.getChildren().addAll(imageList);
WritableImage snapshot = hb.snapshot(null, null);
if(snapshot.isError())
{
System.out.println(snapshot.exceptionProperty().getValue());
}
p.setDiffuseMap(snapshot);

Related

how to get fundamental cycles and cutsets for the MST problem- igraph

I was wondering if someone is familiar with a method/function provided by igraph to obtain either the fundamental cycle for an edge not included in the minimum spanning tree (MST) or the fundamental cutset for an edge contained by the MST.
For example, suppose I have a network as shown below, where I color the edges used in the MST.
A = matrix(c(
0, 8, 0, 5, 0, 10,
8, 0, 3, 6, 9, 10,
0, 3, 0, 3, 9, 0,
5, 6, 3, 0, 0, 0,
0, 9, 9, 0, 0, 0,
10, 10, 0, 0, 0, 0), nrow= 6 , ncol= 6 ,byrow = TRUE)
g <- graph.adjacency(A,weighted=TRUE, mode = c("undirected"))
For this graph, the fundamental cycle of edge 1-6 is represented by (1-4), (4-3), (3-2), (2-6), (6-1).
The fundamental cutset of edge 1-4 is (1-4), (1-2), (1-6).

Updating a data.frame based on a lookup value

So I've got a very strict system that allows for adding in R scripting to handle some data. It's a front end system and I've got about 1000 characters to throw in as much as I can. What I'm working on doing is replacing the values on a data.frame (filedata_model) with a value from a translation list.
here's what I have so far:
vGrades <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0, 4, 4, 3.7, 3.3, 3, 2.7, 2.3, 2, 1.7, 1.3, 1, 0, 0);
vGradeMx <- matrix(vGrades, nrow = 14, ncol = 2);
colnames(vGradeMx) <- c("CB_GRADE", "RNL_GPA");
vGradeTb <- as.data.frame(vGradeMx);
I get this is probably wildly inefficient. I'm used to working with VBA and C based programming languages and a LOT of SQL. If I could write an update statement this would take me 2 seconds. But I don't have any kind of back end access, or write capabilities on the actual data itself outside of this small box I can throw R scripting into.
So here's why I've written what I have:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 - These are the values we're getting back from a research vendor in a file
0, 4, 4, 3.7, 3.3, 3, 2.7, 2.3, 2, 1.7, 1.3, 1, 0, 0 - These are the values that we'd like to change them to.
I set up an additional data frame to hold the original and translation values, but now what? Everything I have tried generally fails. All the R I've learned has come from a weekend of trying to cram as many books in my brain as I can.
I appreciate the help!
filedata_model$column_name_here <- vGradeTb$RNL_GPA[match(filedata_model$column_name_here,
vGradeTb$CB_GRADE)]
where column_name_here is the column containing the values you want to change.

How to code adjacency lists in a dodecahedron graph?

It has:
20 vertices
30 edges
G{20,30}
How can you generate the adjacency lists for each vertex 1,2,...20?
V1 is linked with 2 and 3
v2 is linked with 1 and 4
v3 is linked with 1 and 5 ??
Is there like a formula? This is for a game project, don't know if I missed a math class or something? What should I study to understand the solution?
Manually constructed:
int adjs[] = { 1, 4, 7, 0, 2, 9, 1, 3, 11, 2, 4, 13, 0, 3, 5, 4, 6, 14, 5, 7, 16, 0, 6, 8, 7, 9, 17, 1, 8, 10, 9, 11, 18, 2, 10, 12, 11, 13, 19, 3, 12, 14, 5, 13, 15, 14, 16, 19, 6, 15, 17, 8, 16, 18, 10, 17, 19, 12, 15, 18 };
for (int i = 0, int j = 0; i < 20; i++, j = i * 3) {
caves[i] = Cave(i);
for (int c = j; c < j + 3; c++) {
caves[i].adjsListy.addAdj(adjs[c]);
}
I found help in the solution here: https://rosettacode.org/wiki/Hunt_The_Wumpus/C%2B%2B
I think the link list is usually manually constructed.
However you can take a look at the special case of a 2:1 pyritohedron in https://en.wikipedia.org/wiki/Dodecahedron to get an understanding of how to go from a cube to a dodecahedron. (Also take a look at the animated gif in the section "Cartesian Coordinates")
What this tells us is that a dodecahedron can be constructed by inserting new vertices bisecting each of the cubes 12 edges. Then insert new edges connecting these new vertices such that they bisect each face of the 6 faces of the cube in alternating directions.
I think this understanding can be helpful, either for constructing an algorithm, or just to help you construct a manual link list.
This of-course only takes care of the vertex-links, to find the coordinates of each vertex, see e.g. How to generate/calculate vertices of dodecahedron?

Using texture for triangle mesh without having to read/write an image file

This is a followup on a previous question (see Coloring individual triangles in a triangle mesh on javafx) which I believe is another topic on its own.
Is there a way (with javafx) that I can get away from having to actually write to disk (or external device) an image file to use a texture?
In other words: can I use a specific texture without having to use Image?
Since my color map will change on runtime I don't want to have to write to disk every time I run it. Also, this might be a security issue (writing to disk) for someone using my app.(with javafx)
As #Jens-Peter-Haack suggest, with Snapshot you can create any image you want, and then apply this image as the diffusion map. For that, you need to create some nodes, fill them with the colors you require, group them in some container and then take the snapshot.
There's a straight-in approach, where you can build an image with a pattern of colors using PixelWriter.
Let's say you want 256 colors, this method will return an image of 256 pixels, where each pixel has one of these colors. For simplicity I've added two simple ways of building a palette.
public static Image colorPallete(int numColors){
int width=(int)Math.sqrt(numColors);
int height=numColors/width;
WritableImage img = new WritableImage(width, height);
PixelWriter pw = img.getPixelWriter();
AtomicInteger count = new AtomicInteger();
IntStream.range(0, height).boxed()
.forEach(y->IntStream.range(0, width).boxed()
.forEach(x->pw.setColor(x, y, getColor(count.getAndIncrement(),numColors))));
// save for testing purposes
try {
ImageIO.write(SwingFXUtils.fromFXImage(img, null), "jpg", new File("palette.jpg"));
} catch (IOException ex) { }
return img;
}
private Color getColor(int iColor, int numColors){
// nice palette of colors
java.awt.Color c = java.awt.Color.getHSBColor((float) iColor / (float) numColors, 1.0f, 1.0f);
return Color.rgb(c.getRed(), c.getGreen(), c.getBlue());
// raw palette
//return Color.rgb((iColor >> 16) & 0xFF, (iColor >> 8) & 0xFF, iColor & 0xFF);
}
Once you have the image object, you can set the diffuse map:
IcosahedronMesh mesh = new IcosahedronMesh();
PhongMaterial mat = new PhongMaterial();
mat.setDiffuseMap(colorPallete(256));
mesh.setMaterial(mat);
But you still have to provide the proper mapping to the new texture.
For this you need to map the vertices of the mesh to a pixel in the image.
First, we need a way to map colors with texture coordinates on the mesh. This method will return a pair of coordinates for a given color index:
public static float[] getTextureLocation(int iPoint, int numColors){
int width=(int)Math.sqrt(numColors);
int height=numColors/width;
int y = iPoint/width;
int x = iPoint-width*y;
return new float[]{(((float)x)/((float)width)),(((float)y)/((float)height))};
}
Finally, we add these textures to m.getTextCoords() and to the faces m.getFaces(), as shown here.
If we assign a color to every vertex in our icosahedron we pick a color from all the palette (scaling up or down according the number of colors and vertices), and then set every face with t0=p0, t1=p1, t2=p2:
IntStream.range(0,numVertices).boxed()
.forEach(i->m.getTexCoords()
.addAll(getTextureLocation(i*numColors/numVertices,numColors)));
m.getFaces().addAll(
1, 1, 11, 11, 7, 7,
1, 1, 7, 7, 6, 6,
1, 1, 6, 6, 10, 10,
1, 1, 10, 10, 3, 3,
1, 1, 3, 3, 11, 11,
4, 4, 8, 8, 0, 0,
5, 5, 4, 4, 0, 0,
9, 9, 5, 5, 0, 0,
2, 2, 9, 9, 0, 0,
8, 8, 2, 2, 0, 0,
11, 11, 9, 9, 7, 7,
7, 7, 2, 2, 6, 6,
6, 6, 8, 8, 10, 10,
10, 10, 4, 4, 3, 3,
3, 3, 5, 5, 11, 11,
4, 4, 10, 10, 8, 8,
5, 5, 3, 3, 4, 4,
9, 9, 11, 11, 5, 5,
2, 2, 7, 7, 9, 9,
8, 8, 6, 6, 2, 2
);
This will give us something like this:
EDIT
Playing around with the textures coordinates, instead of mapping node with color, you can add some function and easily create a contour plot, like this:
You might create the texture to be used for fill etc. in your code by using any graphic object and convert that into an image in memory, not touching disk.
The example below will create a texture using a green spline.
Pane testImage2(Pane pane) {
Pane inner = new Pane();
inner.prefWidthProperty().bind(pane.widthProperty());
inner.prefHeightProperty().bind(pane.heightProperty());
pane.getChildren().add(inner);
SVGPath texture = new SVGPath();
texture.setStroke(Color.GREEN);
texture.setStrokeWidth(2.5);
texture.setFill(Color.TRANSPARENT);
texture.setContent("M 10 10 C 40 10 10 70 70 20");
SnapshotParameters params = new SnapshotParameters();
params.setViewport(new Rectangle2D(-5, -5, 70, 50));
Image image = texture.snapshot(params, null);
Paint paint = new ImagePattern(image, 5,5, 20, 20, false);
inner.setBackground(new Background(new BackgroundFill(paint, new CornerRadii(0), new Insets(inset))));
return pane;
}

Combination of Random Number Generators

Given two random integer generators one that generates between 1 and 7 and another that generates between 1 and 5, how do you make a random integer generator that generates between 1 and 13? I have tried solving this question in various ways but I have not been able to come up with a solution that generates numbers from 1 to 13 with equal or near equal probability.
Using the top two answers for Expand a random range from 1–5 to 1–7, I've come up with the following. There's probably a more efficient way to do this (maybe using the 1-5 generator?) but this seems to work.
Optimized for Compactness
var j;
do {
j = 7 * (rand7() - 1) + rand7(); // uniformly random between 1 and 49
} while (j > 39);
// j is now uniformly random between 1 and 39 (an even multiple of 13)
j = j % 13 + 1;
Optimized for understandability
var v = [
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 12, 13, 1],
[2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 1, 2],
[3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]
];
var j = 0;
while (j == 0) {
j = v[rand7() - 1][rand7() - 1];
}

Resources