I am calling GraphViz from Java via JNI to lay out graphs. When I call function gvRenderData from GraphViz's library gvc, which renders the graph layout into a malloc'ed atring (instead of doing file I/O), unfortunately I also see the rendered graph appear on the console. So in fact gvRenderData also seems to write to stdout. This is a real nuisance for graphs with thousands of nodes. I have tried passing args like -o/dev/null into the GraphViz context, but to no avail.
Does anyone know how to turn off that unwanted behavior? I use GraphViz 2.40.1 under Linux.
Here are the basics of my code. No surprises there. Native interfaces have been generated with SWIG.
SWIGTYPE_p_GVC_t gvCtx = gvc.gvContext();
String[] argv = {"dot", "-y", "-q"};
gvc.gvParseArgs(gvCtx, argv.length, argv);
SWIGTYPE_p_graph_t graph = cgraph.agmemread(dot);
gvc.gvLayout(gvCtx, graph, "dot");
String[] rendered = new String[1];
gvc.gvRenderData(gvCtx, graph, "json", rendered, new long[1]);
Related
I am using Processing IDE to generate hundred short video clips for a computer vision project. Right now, I am using Python to create a .pde file and run it. This looks roughly like:
PATH = "/my/local/director/"
list_of_variables = [1, 2, etc.]
for i in list_of_variables:
naming = "p5_{:02d}_myfile".format(i)
os.mkdir(PATH + naming)
with open(PATH + naming + "/" + naming + ".pde", 'w') as pdefile:
pdefile.write("contents of file go here " + i ";\n")
pdefile.write("saveFrame(\"frames/######.tif\");\n")
subprocess.Popen(["processing-ide", "--sketch=" + PATH + naming, "--run"], stdout=subprocess.DEVNULL)
subprocess.call(["ffmpeg", "-i", PATH + naming + "/frames/%06d.tif", PATH + naming + "out.mp4"], stdout=subprocess.DEVNULL)
shutil.rmtree(PATH + naming + "/frames/")
Every time code is executed, Processing IDE opens a preview window to show what is happening. Is there an option I can pass in the execution step or in the .pde file creation that will prevent the preview window from showing. This is taking a long time, and I'm hoping this would speed things up.
Note: Yes, I have considered that there are better options for generating these videos. In retrospect, I should have used OpenCV in Python to speed things up, but that isn't the thrust of this question.
#KevinWorkman's advice is great! If you really really really need to do this with Processing and headless, that's the way forward.
What would the code look like for a sketch ?
If it's simple drawing instructions you could rewrite it in multiple ways.
One option would be simply use an offscreen PGraphics buffer, without the whole PApplet part.
Here's a basic example:
import processing.core.PGraphicsJava2D;
public class PGraphicsBufferTest {
public static void main(String[] args) {
PGraphicsJava2D pg = new PGraphicsJava2D();
// set dimensions
pg.setSize(300, 300);
// set as offline (shouldn't expect a PApplet parent)
pg.setPrimary(false);
// draw
pg.beginDraw();
pg.background(0);
pg.ellipse(150, 150, 250, 250);
pg.endDraw();
// save to disk: in absence of parent PApplet, must use absolute path
pg.save("/path/to/dataset/sequence/frame.png");
}
}
If you save this as PGraphicsBufferTest.java (and have Processing's core.jar in the same folder), as an example you could:
compile: javac -cp core.jar PGraphicsBufferTest.java
run: java -cp .:core.jar PGraphicsBufferTest
This may still briefly generate a window, but hopefully will be a simpler/faster setup.
(I deliberately chose Java2D because it's already part of core.jar. You can use PGraphics2D of course, just remember to also add the OpenGL dependencies (gluegen-rt, jogl), and set the classpath (-cp), but also the native path (-Djava.library.path) arguments)
For example, there's an older pyprocessing pip package which uses Pyglet (OpenGL wrapper).
Because Pyglet can run headless, hopefully, so would pyprocessing.
I haven't tested this myself, however if it does work, you can keep a very similar syntax.
If it's simple 2D drawing instruction and this if for a computer vision project, by all means, OpenCV's drawing functions should suffice.
There are other Python packages that offer 2D drawing features with headless configurations (Pillow's ImageDraw module comes to mind).
Another option I can think that is just as, if not even more so overcomplicated than what you're already doing, is to use openFrameworks. It's C++, but inspired by Processing therefore very similar (e.g. setup() = setup(), draw() = update() + draw(), line(x1,y1,x2,y2) = ofDrawLine(x1,y1,x2,y2), etc.) and can run headless.
What I mean by "map" is that I have a "main" function which calls many other programs inside, and I want to be able to see which file runs first, second, third, etc. Basically, I want to be able to see the list and order of dependencies in this large OOP design program (which the creator did not make a UML class diagram for) to help decipher the code. Surely such a functionality must exist in popular IDE's? I'm mostly dealing with C++ and MATLAB so I'm more concerned with these two specifically, but please list any IDE's you know of that have this functionality. I'd prefer something visual and not just running through a debugger and breakpoints a thousand times.
In MATLAB, I don't believe there's a built-in way to do this visually, but you can get the information you need from the profiler using the FunctionTable returned by profile('info').
The parent/child relationships in the table essentially define a directed graph which you can interact with visually or otherwise in MATLAB if you convert it to a digraph object.
For example, to map the program execution of kmeans:
profile on
kmeans(rand(100,2),5);
p = profile('info');
t = struct2table(p.FunctionTable);
g = digraph(false(height(t)), t); % Create the graph with nodes and no edges
% Add the edges
for ii = 1:g.numnodes
for jj = 1:numel(g.Nodes.Children{ii})
g = g.addedge(ii, g.Nodes.Children{ii}(jj).Index);
end
end
plot(g,'NodeLabel',g.Nodes.FunctionName,'Layout','layered');
Produces:
The file each function comes from is also accessible via the FileName field of the FunctionTable so if the distinction between functions and the files they came from is important you could use this information to color or simplify the graph accordingly.
I need to get the list of co-ordinates for my box2d world- I'm trying to get a wrap around effect so that particles that go off the side of the screen appear on the opposite side. box2d is not well documented for Processing and the only example I could find was in java (I know its the parent language but it needs translating). This is here.
I think the action is here:
private function updateWorld(e:Event):void {
world.Step(1/30,10,10);
world.ClearForces();
for (var b:b2Body=world.GetBodyList(); b; b=b.GetNext()) {
if (b.GetType()==b2Body.b2_dynamicBody) {
if (b.GetJointList()==null) {
if (b.GetPosition().x*worldScale>640) {
b.SetPosition(new b2Vec2(0,b.GetPosition().y));
}
if (b.GetPosition().x*worldScale<0) {
b.SetPosition(new b2Vec2(640/worldScale,b.GetPosition().y));
}
}
}
}
world.DrawDebugData();
}
So I have tried translating this although I get stuck at the point of world.GetBodyList
I assume world is the instantiated box2d world I have created. I that is so, Processing doesn't seem to recognise this. Basically how to I just get an array of all the particle co-ordinates. Should be easier....
Anything you can do in Java, you can do in Processing. But the code you posted is not Java. It's C++. (Edit: George pointed out that it looks like ActionScript. Either way, it's not Java!)
Box2D is the original library, written in C++. Your code is an example of using that library.
JBox2D is a Java wrapper of the C++ library, so you can write Java code that interacts with the C++ library.
Processing is written in Java, so you can use JBox2D in Processing.
Processing also has a few libraries that simplify JBox2D, like Daniel Shiffman's Box2D for Processing or BoxWrap2d.
You don't need to use these libraries in order to use JBox2D from Processing.
You can find the documentation for JBox2D here.
To answer your question, yes, the world variable would be the instance of World you created when you set up your physics environment.
If you're using one of the Processing libraries, the creation of the World instance might be hidden from you. You might have to use JBox2D directly yourself.
I'd like to know what works in Processing 3 but doesn't work or isn't supported (yet) in Processing.js? Seems like many of the new examples in Processing 3's GUI don't work once converted to js.
I'm using this tool to convert: http://processingjs.org/tools/processing-helper.html
You're going to have a hard time tracking down everything that breaks between Processing 3 and Processing.js. They are two separate projects maintained by two separate groups of people.
The best thing you can do is try something, see specifically what breaks, and then try to find a workaround. Take each example one at a time, try to get it working, and post a question here if you get stuck on something specific.
That being said, one place to start looking for things that might not work is the Changes in 3.0 page on Processing's GitHub.
Specifically, anything involving the new surface variable is not going to work in Processing.js. Similarly, the new settings() function won't work either. Some additional functions in PVector also won't work.
Here is a link for a beta JavaScript mode for Processing 3, but you might be better off just waiting for Processing.js to catch up with Processing 3. In the meantime, take the examples one at a time, the workarounds shouldn't be too complicated to figure out.
Processing 3 (P3) is a java library, while processing.js (PJS) is a JS library, so each library will use their respective language's methods. As a basic example, a function in PJS will be declared like function myFunction () {} or in some cases var myFunction = function () {}; while P3 would look like void myFunction () {}. Another difference is strong type, in JS you can simply declare any type of variable with var myVariable = 0; but in java and therefore P3, you need to use int myVariable = 0; or boolean myBoolean = false;. Of course these aren't the only differences, but I hope they give you an idea of the differences in porting something from PJS to P3; while the library is very similar in both languages and can do many of the same things, it is mostly a difference between languages not libraries.
I'm trying to use two libraries:
D3 (a Javascript graphics library)
NVD3 (a graph-specific D3 extension)
I'm also trying to use the Require Optimizer, and that's where things get rough. D3 is AMD-compliant, so if it detects that you're using Require it will define a module and NOT export a global d3 variable. NVD3 has no AMD support, but Require has "shims" for that case.
However, if I "shim" NVD3 and give that shim a dependency of D3, it doesn't work, because NVD3 expects there to be a global d3 variable, which D3 won't make in a Require environment. So, to get around this issue I made a new module (d3Shim), which simply requires D3, then registers it as a global variable:
define(['d3'], function(d3) {
return window.d3 = d3;
});
I made NVD3 depend on d3Shim, and then everything worked ... in normal Require-land. When I tried to use the require-optimizer to merge everything together in a single file I found NVD3 started breaking again, because of a lack of a d3 variable.
It turns out the optimizer does this for shims:
*shimmed code here*
define(*shim path*, [], function(){});
So, it doesn't matter what dependencies I have, the NVD3 code will run before the d3shim module can do its magic.
My question is, is there any way I can use these two libraries with the Require Optimizer, without having to alter either file? If necessary I can always edit NVD3's code to make it an AMD module, but for obvious reasons it's bad practice to edit 3rd party libraries, so I'd prefer a non-editing solution.