Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I tried to put java list into a 2D array and Retrieve it from the JSP. But It wasn't success.I really need this done.. Support me if you can..I have put my codes below if you able to solve this I'm really appreciate that.
Update:
1st List is getting inserted to first row(I want it insert to first column). likewise other three lists inserted to other 3 rows instead of three columns.
Screenshot
Requirement :
Ex:-
Servlet Code.
Scanner scanner = new Scanner(Result);
List<String> cdLine = new ArrayList<String>();
List<Integer> wtc = new ArrayList<Integer>();
List<Integer> ncc = new ArrayList<Integer>();
List<Integer> ccpps = new ArrayList<Integer>();
ControlData controlData = new ControlData();
while(scanner.hasNextLine())
{
token1 = scanner.nextLine();
Wtcs = controlData.CtrlWeight(token1);
NC = controlData.NofConditions(token1);
Ccspps = controlData.previousComplex(token1);
cdLine.add(token1);
wtc.add(Ccspps);
ncc.add(NC);
ccpps.add(Wtcs);
#SuppressWarnings("rawtypes")
List arr[][]={{cdLine},{wtc},{ncc},{ccpps}};
}
#SuppressWarnings("rawtypes")
List arr[][]={{cdLine},{wtc},{ncc},{ccpps}};
scanner.close(); //close the scanner
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/views/Control_structures.jsp");
request.setAttribute("Code_string", arr);
dispatcher.forward(request, response);
JSP code(using JSTL)
<c:forEach items="${Code_string}" var="post" varStatus="theCount">
<tbody>
<c:forEach items="${post}" var="value" varStatus="cell">
<tr>
<td scope="row">${theCount.count}</td>
<td>${value[0]}</td>
<td>${value[1]}</td>
<td>${value[2]}</td>
<td>${value[3]}</td>
<td>-</td>
</tr>
</c:forEach>
</tbody>
</c:forEach>
Update 2:
The 2D array that parsing to the JSP is like this. Hope this also need to be changed.
[[[public class Prime {, if, public static void main(String[]
args) {, , int low = 20, high = 50;, , while (low <
high) {, if(checkPrimeNumber(low)),
System.out.print(low + " ");, , ++low;, }, },
, public static boolean checkPrimeNumber(int num) {,
boolean flag = true;, , for(int i = 2; i <= num/2; ++i) {, ,
if(num % i == 0) {, flag = false;,
break;, }, }, , return flag;, }, }]],
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]]
Output that I'm getting:
Thank you for your contribution.
There are two problems in your code:
A. With the following statement, you have created a 3-D structure which is an array of arrays of lists (Note that the List is an implementation of a dynamic 1-D array).
List arr[][]={{cdLine},{wtc},{ncc},{ccpps}};
You need an array of lists as given below:
List arr[]={cdLine,wtc,ncc,ccpps};
B. Do not place the above line inside the while loop. Do it just once after the while loop.
Apart from the points mentioned above, I would recommend you create a custom type instead of using raw type for the List array. Your custom type should be something like:
class MyType {
private List<String> cdLine = new ArrayList<String>();
private List<Integer> wtc = new ArrayList<Integer>();
private List<Integer> ncc = new ArrayList<Integer>();
private List<Integer> ccpps = new ArrayList<Integer>();
// ..constructors and getters and setters
}
Feel free to comment in case of any doubt/issue.
I have some Mesh-Objects like a flat rectangle:
private void generateMesh(float xshift_mesh, float yshift_mesh, float zshift_mesh, float size_mesh) {
float size = size_mesh/2;
mesh = new Mesh(true, 4, 6, VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), VertexAttribute.TexCoords(0), VertexAttribute.Normal());
mesh.setVertices(new float[]
// Position XYZ Color RGBA Texture Coordinates UV Normal XYZ
//|-,-,-,| |-,-,-,-,| |-,-,| |-,-,-|
{
-size+xshift_mesh, -size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 0, 1, 0, 0, -1,
size+xshift_mesh, -size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 1, 1, 0, 0, 1,
size+xshift_mesh, size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 1, 0, 0, 0, 1,
-size+xshift_mesh, size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 0, 0, 0, 0, -1
});
mesh.setIndices(new short[] {0, 1, 2,2, 3, 0});
}
My task is to create a vertex shader which let such an object wave in some way.
My idea:
First I deliver the vertex shader a uniform variable which changes every second from 0 to 1 and vice versa:
Date endDate = new Date();
float numSeconds = (float)((endDate.getTime() - startDate.getTime()) / 1000);
float even = (numSeconds % 2);
...
shader.setUniformMatrix("u_worldView", cam.combined);//WVP-Matrix
shader.setUniformi("u_texture", 0);
shader.setUniformf("u_even", even);
Each vertex of the rectangle object has an Normal vector, like you in see above in the generateMesh method. I can access and use this vector inside the vertex shader:
uniform float u_even;
uniform mat4 u_worldView;
attribute vec4 a_color;
attribute vec4 a_position;
attribute vec2 a_texCoord0;
attribute vec4 a_normal;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
if(u_even > 0.5) gl_Position = u_worldView * a_position + a_normal;
else gl_Position = u_worldView * a_position - a_normal;
}
I expected that the object would change every second, which it does in some way, but I also manipulate the camera position everytime. The view position also goes back and forth if the values of the Normal vector are all 0.
I have also tried:
if(u_even > 0.5) gl_Position = u_worldView * (a_position + a_normal);
else gl_Position = u_worldView * (a_position - a_normal);
But its even worse...
So how can I change the position of a vertex without changing the whole point of view?
I appreciate your help!
It's been a while. Just want to closing the question as resolved.
The problem lied in the generateMesh function, specifically in the NORMAL XYZ values:
private void generateMesh(float xshift_mesh, float yshift_mesh, float zshift_mesh, float size_mesh) {
if(size_mesh < 0) {
return;
}
float size = size_mesh/2;
mesh = new Mesh(true, 4, 6, VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), VertexAttribute.TexCoords(0), VertexAttribute.Normal());
mesh.setVertices(new float[]
//<------------ Position XYZ ------------------------> <-Color RGBA-> <Texture Coordinates UV> <Normal XYZ>
//| -, -, -,| |-, -, -, -, | | -, -, | |-, -, -|
{
-size+xshift_mesh, -size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 0, 1, 0, 0, 1,
size+xshift_mesh, -size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 1, 1, 0, 0, 1,
size+xshift_mesh, size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 1, 0, 0, 0, -1,
-size+xshift_mesh, size+yshift_mesh, zshift_mesh, 1, 1, 1, 1, 0, 0, 0, 0, -1
});
mesh.setIndices(new short[] {0, 1, 2,2, 3, 0});
}
Good Day! I have the following issue. The graphic model is not displayed correctly: some backside faces of the model that should be hidden by the frontside remain visible. Here are some exmples to clarify: (isometry)
(issue)
This issue comes out especially notable when applying light and material. So the the question is how this can be solved for JavaFX?
UPD:
public class VertexTest extends Application {
PerspectiveCamera camera;
Cam cam = new Cam();
double mouseOldX, mouseOldY, mousePosX, mousePosY, mouseDeltaX, mouseDeltaY;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
TriangleMesh mesh = new Shape3DRectangle(100, 100, 100);
MeshView view = new MeshView(mesh);
view.setDrawMode(DrawMode.LINE);
view.setMaterial(new PhongMaterial(Color.RED));
cam.getChildren().add(view);
Scene scene = new Scene(cam, 1000, 1000, true);
addEvents(view, scene);
camera = new PerspectiveCamera();
camera.setTranslateX(-500);
camera.setTranslateY(-500);
camera.setTranslateZ(1000);
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.show();
}
private void addEvents(MeshView view, Scene s) {
s.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
mouseOldX = mousePosX;
mouseOldY = mousePosY;
mousePosX = me.getX();
mousePosY = me.getY();
mouseDeltaX = mousePosX - mouseOldX;
mouseDeltaY = mousePosY - mouseOldY;
cam.ry.setAngle(cam.ry.getAngle() - mouseDeltaX);
cam.rx.setAngle(cam.rx.getAngle() + mouseDeltaY);
}
});
}
class Cam extends Group {
Translate t = new Translate();
Translate p = new Translate();
Translate ip = new Translate();
Rotate rx = new Rotate();
{
rx.setAxis(Rotate.X_AXIS);
}
Rotate ry = new Rotate();
{
ry.setAxis(Rotate.Y_AXIS);
}
Rotate rz = new Rotate();
{
rz.setAxis(Rotate.Z_AXIS);
}
Scale s = new Scale();
public Cam() {
super();
getTransforms().addAll(t, p, rx, rz, ry, s, ip);
}
}
public class Shape3DRectangle extends TriangleMesh {
public Shape3DRectangle(float Width, float Height, float deep) {
this.getPoints().setAll(-Width / 2, Height / 2, deep / 2, // idx p0
Width / 2, Height / 2, deep / 2, // idx p1
-Width / 2, -Height / 2, deep / 2, // idx p2
Width / 2, -Height / 2, deep / 2, // idx p3
-Width / 2, Height / 2, -deep / 2, // idx p4
Width / 2, Height / 2, -deep / 2, // idx p5
-Width / 2, -Height / 2, -deep / 2, // idx p6
Width, -Height / 2, -deep / 2 // idx p7
);
this.getTexCoords().addAll(0.0f, 0.0f);
this.getFaces().addAll(5, 0, 4, 0, 0, 0 // P5,T1 ,P4,T0 ,P0,T3
, 5, 0, 0, 0, 1, 0 // P5,T1 ,P0,T3 ,P1,T4
, 0, 0, 4, 0, 6, 0 // P0,T3 ,P4,T2 ,P6,T7
, 0, 0, 6, 0, 2, 0 // P0,T3 ,P6,T7 ,P2,T8
, 1, 0, 0, 0, 2, 0 // P1,T4 ,P0,T3 ,P2,T8
, 1, 0, 2, 0, 3, 0 // P1,T4 ,P2,T8 ,P3,T9
, 5, 0, 1, 0, 3, 0 // P5,T5 ,P1,T4 ,P3,T9
, 5, 0, 3, 0, 7, 0 // P5,T5 ,P3,T9 ,P7,T10
, 4, 0, 5, 0, 7, 0 // P4,T6 ,P5,T5 ,P7,T10
, 4, 0, 7, 0, 6, 0 // P4,T6 ,P7,T10 ,P6,T11
, 3, 0, 2, 0, 6, 0 // P3,T9 ,P2,T8 ,P6,T12
, 3, 0, 6, 0, 7, 0 // P3,T9 ,P6,T12 ,P7,T13
);
}
}
}
I've been playing around with your sample, and I think I've found out the reason of your issues.
First, I checked the winding of the faces. All of them are counter-clockwise, so all their normals go outwards, as they should be.
Then I modified other vertices instead of the last one. In some cases there were no issues, in others, the issue was still there.
Basically, the issue happens when there are "concave" surfaces, meaning that two faces have normals that will cross. And it doesn't happen when all the surfaces are "convex", meaning that their normals point outwards and won't cross.
This is a clear image of both type of meshes taken from here:
Back to your sample, you are defining a concave mesh:
But if instead of modifying vertex #7, we make the #5 larger, we have a convex mesh, with no rendering issues:
Obviously, while this fix the rendering problem, it changes your initial shape.
If you want to keep your initial geometry, the other possible solution is changing the faces, so you don't have any concave areas.
Let's have a look at the faces 5-1-3 and 5-3-7, and let's say we want to move now the vertex #1.
If we keep your triangles, face 5-1-3 and 5-3-7 will define a concave surface to be render (their normals will cross), while if we change those triangles to 5-1-7 and 1-3-7, then the surface will be convex (their normals won't cross):
Back to your initial shape, this change in those two faces will solve the rendering issues.
While the vertices are the same, the geometry is a little bit difference. So it requires some refinement (more elements). Adding those elements should be done keeping in mind this convex concept. The problem is not trivial, though, as you can see here.
Nice analysis by Jose but it looks to me as if the OP has just forgotten to divide the Width by 2 in this line of his code.
Width, -Height / 2, -deep / 2 // idx p7
should be
Width / 2, -Height / 2, -deep / 2 // idx p7
The class is called Shape3DRectangle but with this mistake
the geometry is not rectangular anymore.
You can set the cullFaceProperty for every Shape3D. I guess that is what you need but I am not sure whether I understood your question precisely.
Shape3D#cullFaceProperty
I have the following code that reduces an image's contrast and by 50% and turns down the saturation to 0. Works flawlessly. However, I need to overlay a give colour over the resulting image, much like the PorterDuff.Mode.OVERLAY filter.
I've been fiddling with this for hours, didn't get me anywhere close to what I wanted.
float contrast = -0.5f;
float scale = contrast + 1.f;
float translate = (-.5f * scale + .5f) * 255.f;
float[] array = new float[]{
scale, 0, 0, 0, translate,
0, scale, 0, 0, translate,
0, 0, scale, 0, translate,
0, 0, 0, 1, 0};
ColorMatrix contrastMatrix = new ColorMatrix(array);
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix.setSaturation(0);
ColorMatrix matrix = new ColorMatrix();
matrix.setConcat(contrastMatrix, saturationMatrix);
// This is where I've been trying to overlay the colour by fiddling the colour matrix array.
// Didn't get the effect I wanted.
int primaryColor = context.getResources().getColor(R.color.primary500);
matrix.postConcat(new ColorMatrix(
new float[]{
Color.red(primaryColor)/255f, 0, 0, 0, 0,
0, Color.green(primaryColor)/255f, 0, 0, 0,
0, 0, Color.blue(primaryColor)/255f, 0, 0,
0, 0, 0, 1, 0}));
imageView.setColorFilter(filter);
The array I've used above give a much dark tone of blue. Can't seem the get the it right for some reason.
Any help would be much appreciated. Many thanks!
Actually, i want to find out the dominant colour in the image, so i want to find the HSV histogram of the image and hence filter out the other colours. However, i dont know how to do this in java platform using opence. I only find the code in C++. Thank you.
Mat image = Highgui.imread("binary07.jpg");
//Mat src = new Mat(image.height(), image.width(), CvType.CV_8UC2);
Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2GRAY);
List<Mat> hsv_planes = new ArrayList<Mat>();
Core.split(image, hsv_planes);
MatOfInt histSize = new MatOfInt(256);
final MatOfFloat histRange = new MatOfFloat(0f, 256f);
boolean accumulate = false;
Mat h_hist = new Mat();
Mat s_hist = new Mat();
Mat v_hist = new Mat();
//error appear in the following sentences
Imgproc.calcHist((List<Mat>) hsv_planes.get(0), new MatOfInt(3), new Mat(), h_hist, histSize, histRange, accumulate);
Imgproc.calcHist((List<Mat>) hsv_planes.get(1), new MatOfInt(3), new Mat(), s_hist, histSize, histRange, accumulate);
Imgproc.calcHist((List<Mat>) hsv_planes.get(2), new MatOfInt(3), new Mat(), v_hist, histSize, histRange, accumulate);
int hist_w = 512;
int hist_h = 600;
long bin_w = Math.round((double) hist_w / 256);
//bin_w = Math.round((double) (hist_w / 256));
Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC1);
Core.normalize(h_hist, h_hist, 3, histImage.rows(), Core.NORM_MINMAX);
Core.normalize(s_hist, s_hist, 3, histImage.rows(), Core.NORM_MINMAX);
Core.normalize(v_hist, v_hist, 3, histImage.rows(), Core.NORM_MINMAX);
for (int i = 1; i < 256; i++) {
Point p1 = new Point(bin_w * (i - 1), hist_h - Math.round(h_hist.get(i - 1, 0)[0]));
Point p2 = new Point(bin_w * (i), hist_h - Math.round(h_hist.get(i, 0)[0]));
Core.line(histImage, p1, p2, new Scalar(255, 0, 0), 2, 8, 0);
Point p3 = new Point(bin_w * (i - 1), hist_h - Math.round(s_hist.get(i - 1, 0)[0]));
Point p4 = new Point(bin_w * (i), hist_h - Math.round(s_hist.get(i, 0)[0]));
Core.line(histImage, p3, p4, new Scalar(0, 255, 0), 2, 8, 0);
Point p5 = new Point(bin_w * (i - 1), hist_h - Math.round(v_hist.get(i - 1, 0)[0]));
Point p6 = new Point(bin_w * (i), hist_h - Math.round(v_hist.get(i, 0)[0]));
Core.line(histImage, p5, p6, new Scalar(0, 0, 255), 2, 8, 0);
}
Highgui.imwrite("histogram.jpg", histImage);
I dont know how to get the output after the split function.
reference:
http://docs.opencv.org/java/
http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html
In the code, the color conversion:
Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2GRAY);
should be to HSV not gray:
Imgproc.cvtColor(image, image, Imgproc.COLOR_BGR2HSV);
In your example you will only have one (gray) plane instead of the 3 HSV channels. That will give errors when you access the 2nd and 3rd plane.
Here is the code for comparing the histogram of Source image to a reference image for OpenCV 2.4.11 Java (Android).
// Assume SourceImage is a Bitmap ARGB_8888
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap refImage = BitmapFactory.decodeFile(mBaseDir + "some_reference.jpg", options);
Mat hsvRef = new Mat();
Mat hsvSource = new Mat();
Mat srcRef = new Mat(refImage.getHeight(), refImage.getWidth(), CvType.CV_8U, new Scalar(4));
Utils.bitmapToMat(refImage, srcRef);
Mat srcSource = new Mat(SourceImage.getHeight(), SourceImage.getWidth(), CvType.CV_8U, new Scalar(4));
Utils.bitmapToMat(SourceImage, srcSource);
/// Convert to HSV
Imgproc.cvtColor(srcRef, hsvRef, Imgproc.COLOR_BGR2HSV);
Imgproc.cvtColor(srcSource, hsvSource, Imgproc.COLOR_BGR2HSV);
/// Using 50 bins for hue and 60 for saturation
int hBins = 50;
int sBins = 60;
MatOfInt histSize = new MatOfInt( hBins, sBins);
// hue varies from 0 to 179, saturation from 0 to 255
MatOfFloat ranges = new MatOfFloat( 0f,180f,0f,256f );
// we compute the histogram from the 0-th and 1-st channels
MatOfInt channels = new MatOfInt(0, 1);
Mat histRef = new Mat();
Mat histSource = new Mat();
ArrayList<Mat> histImages=new ArrayList<Mat>();
histImages.add(hsvRef);
Imgproc.calcHist(histImages,
channels,
new Mat(),
histRef,
histSize,
ranges,
false);
Core.normalize(histRef,
histRef,
0,
1,
Core.NORM_MINMAX,
-1,
new Mat());
histImages=new ArrayList<Mat>();
histImages.add(hsvSource);
Imgproc.calcHist(histImages,
channels,
new Mat(),
histSource,
histSize,
ranges,
false);
Core.normalize(histSource,
histSource,
0,
1,
Core.NORM_MINMAX,
-1,
new Mat());
double resp1 = Imgproc.compareHist(histRef, histSource, 0);
double resp2 = Imgproc.compareHist(histRef, histSource, 1);
double resp3 = Imgproc.compareHist(histRef, histSource, 2);
double resp4 = Imgproc.compareHist(histRef, histSource, 3);
The next code works fine for one depth channel. You have to do just a few modifications to add the other two channels
//Calculate histogram
java.util.List<Mat> matList = new LinkedList<Mat>();
matList.add(imageIR_gray);
Mat histogram = new Mat();
MatOfFloat ranges=new MatOfFloat(0,256);
MatOfInt histSize = new MatOfInt(255);
Imgproc.calcHist(
matList,
new MatOfInt(0),
new Mat(),
histogram ,
histSize ,
ranges);
// Create space for histogram image
Mat histImage = Mat.zeros( 100, (int)histSize.get(0, 0)[0], CvType.CV_8UC1);
// Normalize histogram
Core.normalize(histogram, histogram, 1, histImage.rows() , Core.NORM_MINMAX, -1, new Mat() );
// Draw lines for histogram points
for( int i = 0; i < (int)histSize.get(0, 0)[0]; i++ )
{
Core.line(
histImage,
new org.opencv.core.Point( i, histImage.rows() ),
new org.opencv.core.Point( i, histImage.rows()-Math.round( histogram.get(i,0)[0] )) ,
new Scalar( 255, 255, 255),
1, 8, 0 );
}