I have been trying to recreate the structure light functions by following the structured light Graycode tutorials on OpenCv for Unity (it's a port of OpenCV java)
https://docs.opencv.org/4.1.2/dc/da9/tutorial_decode_graycode_pattern.html
The tutorials seem straightforward, but I think they may be out of date now. For instance, there is no more "decode" function in the Graycode part of the API
https://docs.opencv.org/trunk/d1/dec/classcv_1_1structured__light_1_1GrayCodePattern.html
Mat disparityMap;
bool decoded = graycode->decode(captured_pattern, disparityMap, blackImages, whiteImages,
structured_light::DECODE_3D_UNDERWORLD);
Instead i think i might need to use getProjPixel() instead somehow? I can't find any other examples using this online thought.
This was my rough thought of maybe how to use it
for(int x = 0; x < 1920; x++)
{
for(int y = 0; y < 1080; y++)
{
Point thepoint= new Point(-1,-1);
grayCode.getProjPixel(photosCam1, x, y, thepoint );
projPix.Add(thepoint);
}
}
But i don't totally know what i would do next to get a pointcloud (and ultimately a mesh for unity) from this, or if i am even going the right direction
Unfortunately, it looks like the answer is just that the Java port of OpenCV is incomplete when it comes to the structure light module. So there basically is no way to actually decode the captured images for java (or the subsequent ports to unity)
https://answers.opencv.org/question/222527/structured-light-module-missing-decode-function/?comment=222534#post-id-222534
too bad!
I'm trying to write a code in Java with CPLEX but I have a problem. I'm new to CPLEX and Java as well.
I've studied for a few days and I could understand very simple CPLEX examples but I have to deal with something much more difficult now.
I was trying to write expressions like 1000*(k-Sigma y^k(k from 1)) but I couldn't make it work so I googled for this topic but I couldn't find a way to figure this out.
What I understood so far is I need an array like this
IloNumVar[] y = cplex.numVarArray(?, ?, Double.MAX_VALUE?);
I read the manual from the IBM website but I still don't understand what variables I should put inside brackets.
And I need a for loop for the summation so I wrote like
for(int k = 1; k <= bus; k++) {
objective.addTerm(1000, k-y[k]);
}
Of course it's not working. I guess something's wrong with 'k' but have no idea how to fix this.
Please take a look at the following page:
CPLEX Java API
You can see the following method header:
IloNumVar[] numVarArray(int n, double lb, double ub)
where the params are:
n - number of variables
lb - lower bound
ub - upper bound
If you use it like this:
IloNumVar[] y = cplex.numVarArray(5, 0, Double.MAX_VALUE);
you are going to create an array of 5 elements (variables). Each of them has to be a non-negative number.
Please specify more clearly the 2nd part of your question.
I have written a code in JESS. I am now running the code from Java. I have run to this problem that I want to have the engine.execute("") command in a for loop.
My example code is :
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
r.executeCommand("(answer(ident headers.get(i)(text patientData.get(j).get(i))");
}
}
where answer is a deftemplate in JESS with two slots. I am reading a text file in Java and inserting the values in the slots of the deftemplate.
Thanks,
Ali
If answer is a deftemplate with slots ident and text, the following snippet looks like the construction of a fact:
(answer(ident headers.get(i)(text patientData.get(j).get(i))
A bare fact does not represent a Jess command that can be executed via Rete.executeCommand.
You shouldn't use the deprecated method executeCommand; use eval.
Your parentheses aren't balanced properly, 6 opening parentheses against 4 closing ones.
Assuming that headers is a List<String> in your Java application, you cannot simply throw a Java method call at Jess, which doesn't understand Java syntax in the first place and (presumably) doesn't know about the pojo header at all.
The same is true for the pojo patientData.
While you might pass objects from your Java program to the Jess engine, this is absolutely impossible for simple variables like i and j.
Considering all of this (most of which is easy to learn from the excellent Jess manual), using
r.eval( "(assert (answer (ident \"" +
headers.get(i) +
"\")(text \"" +
patientData.get(j).get(i) +
"\")))" );
might have a chance of succeeding and indeed insert a new fact into working memory.
I want to make the format of my code (Java) look like this:
for (i = 0; i < 3; i++)
{
System.out.println(i);
System.out.println(i+1);
}
above is for code that have brackets.
For the code that have only one line:
for (i = 0; i < 3; i++) System.out.println(i);
I can do the first one but moving the System.out.println(i); to the same line as 'for', I cannot find the place to set.
Thank you in advance.
[Addition] I mean how to setting in preference so that when I press [Ctrl + Shift + F] it will format as what I want. Sorry for my bad explanation.
There seems to be no option. Someone submitted it as request in 2005 but it was never implemented https://bugs.eclipse.org/bugs/show_bug.cgi?id=104910
So your only option seems to be to enable the //#formatter:on (respectively :off) and do them around your for statements and format them manually. But this seems like too much work so I would just let Eclipse format it like it already does.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I've searched for this, but couldn't find an answer and for whatever reason I was too ashamed to ask professor, due to that feeling when hundreds of people stare at you...
Anyhow, my question is what's the importance of having brackets? Is it OK if I omit them? Example:
for (int i = 0; i < size; i++) {
a += b;
}
vs
for (int i = 0; i < size; i++)
a += b;
I know both of them will work, but if I omit the brackets (which I tend to do a lot, due to visibility) will that change anything, anything at all? As I said, I know it works, I tested it dozen of times, but now some of my uni assignments are getting larger, and for some reason I have irrational fear that in the long run, this my cause some problems? Is there a reason to fear that?
It won't change anything at all apart from the maintainability of your code. I've seen code like this:
for (int i = 0; i < size; i++)
a += b;
System.out.println("foo");
which means this:
for (int i = 0; i < size; i++)
a += b;
System.out.println("foo");
... but which should have been this:
for (int i = 0; i < size; i++) {
a += b;
System.out.println("foo");
}
Personally I always include the brackets to reduce the possibility of confusion when reading or modifying the code.
The coding conventions at every company I've worked for have required this - which is not to say that some other companies don't have different conventions...
And just in case you think it would never make a difference: I had to fix a bug once which was pretty much equivalent to the code above. It was remarkably hard to spot... (admittedly this was years ago, before I'd started unit testing, which would no doubt have made it easier to diagnose).
Using braces makes the code more maintainable and understandable. So you should consider them by default.
I sometimes skip using braces on guard clauses to make the code more compact. My requirement for this is that they're if statements that are followed by a jump statement, like return or throw. Also, I keep them in the same line to draw attention to the idiom, e.g:.
if (!isActive()) return;
They also apply to code inside loops:
for (...) {
if (shouldSkip()) continue;
...
}
And to other jump-conditions from methods that are not necessarily at the top of the method body.
Some languages (like Perl or Ruby) have a kind of conditional statement, where braces don't apply:
return if (!isActive());
// or, more interestingly
return unless (isActive());
I consider it to be equivalent to what I just described, but explicitly supported by the language.
There is no difference. The main problem with the second version is you might end up writing this:
for (...)
do_something();
do_something_else();
when you update that method, thinking that do_something_else() is called inside the loop. (And that leads to head-scratching debug sessions.)
There is a second problem that the brace version doesn't have, and its possibly even harder to spot:
for (int i=0; i<3; i++);
System.out.println("Why on earth does this print just once?");
So keep the braces unless you have a good reason, it is just a few keystrokes more.
I think that loosing curly braces is good, if you are also using auto-format, because than your indentation is always correct, so it will be easy to spot any errors that way.
Saying that leaving the curly braces out is bad, weird or unreadable is just wrong, as whole language is based on that idea, and it's pretty popular (python).
But I have to say that without using a formatter it can be dangerous.
For most cases, the answers mentioned so far are correct. But there are some disadvantages to it from the security perspective of things. Having worked in a payments team, security is a much stronger factor that motives such decisions. Lets say you have the following code:
if( "Prod".equals(stage) )
callBankFunction ( creditCardInput )
else
callMockBankFunction ( creditCardInput )
Now lets say you have this code is not working due to some internal problem. You want to check the input. So you make the following change:
if( "Prod".equals(stage) )
callBankFunction ( creditCardInput )
else
callMockBankFunction ( creditCardInput )
Logger.log( creditCardInput )
Say you fix the problem and deploy this code (and maybe the reviewer & you think this won't cause a problem since its not inside the 'Prod' condition). Magically, your production logs now print customer credit card information that is visible to all the personnel who can see the logs. God forbid if any of them (with malicious intent) gets hold of this data.
Thus not giving a brace and a little careless coding can often lead to breach of secure information. It is also classified as a vulnerability in JAVA by CERT - Software Engineering Institure, CMU.
If you have a single statement you can omit the brackets, for more that one statements brackets is necessary for declaring a block of code.
When you use brackets you are declaring a block of code :
{
//Block of code
}
The brackets should be used also with only one statement when you are in a situation of nested statement for improve readability, so for example :
for( ; ; )
if(a == b)
doSomething()
it is more readable written with brackets also if not necessary :
for( ; ; ) {
if(a == b) {
doSomething()
}
}
If you use brackets your code is more readable.
And if you need to add some operator in same block you can avoid possible errors
Using the brackets future proofs the code against later modifications. I've seen cases where brackets were omitted and someone later added some code and didn't put the brackets in at that time. The result was that the code they added didn't go inside the section they thought it did. So I think the answer is that its good practice in light of future changes to the code. I've seen software groups adopt that as a standard, i.e. always requiring brackets even with single line blocks for that reason.
using redundant braces to claim that code is more maintainable raises the following question: if the guys writing, wondering about and further maintaining the code have issues like the ones described before (indentation related or readability related) perhaps they should not program at all...
Nowadays, it is very easy to re-indent codes to find out which block of codes is in which if or for/while. If you insist that re-indenting is hard to do, then brackets placed at wrong indentation can confuse you equally badly.
for(int i = 0; i < 100; i++) { if(i < 10) {
doSomething();
} else { for(int j = 0; j < 5; j++) {
doSomethingElse();
}
}}
If you do this everywhere, your brain is going to break down in no time. Even with brackets, you are depending on indentation to visually find the start and end of code blocks.
If indentation is important, then you should already write your code in correct indentation, so other people don't need to re-indent your codes to read correctly.
If you want to argue that the previous example is too fake/deliberate, and that the brackets are there to capture careless indentation problem (especially when you copy/paste codes), then consider this:
for(int i = 0; i < 100; i++) {
if(i < 10) {
doSomething();
}
else {
for(int j = 0; j < 5; j++) {
doSomethingElse();
}
}
Yes, it looks less serious than the previous example, but you can still get confused by such indentation.
IMHO, it is the responsibility of the person writing the code to check through the code and make sure things are indented correctly before they proceed to do other things.
More support for the "always braces" group from me. If you omit braces for single-statement loops/branches, put the statement on the same line as the control-statement,
if (condition) doSomething();
for(int i = 0; i < arr.length; ++i) arr[i] += b;
that way it's harder to forget inserting braces when the body is expanded. Still, use curlies anyway.
If you remove braces, it will only read the first line of instruction. Any additional lines will not be read. If you have more than 1 line of instruction to be executed pls use curly brace - or else exception will be thrown.
Result wise , it is the same thing.
Only two things to consider.
- Code Maintainability
- Loosely coupled code. (may execute
something else. because you haven't specified the scope for the loop. )
Note: In my observation, if it is loop with in a loop. Inner Loop without braces is also safe. Result will not vary.
If you have only one statement inside the loop it is same.
For example see the following code:
for(int i=0;i<4;i++)
System.out.println("shiva");
we have only one statement in above code. so no issue
for(int i=0;i<4;i++)
System.out.println("shiva");
System.out.println("End");
Here we are having two statements but only first statement comes into inside the loop but not the second statement.
If you have multiple statements under single loop you must use braces.
it should be a reflex to reformat the code as well... that is of course for professional programmers in professional teams
It's probably best to use the curly braces everywhere for the simple fact that debugging this would be an extreme nuisance. But other wise, one line of code doesn't necessarily need the bracket. Hope this helps!