DMelt:Plots/5 Plots in 3D
Plots in 3D
DMelt contains several powerful engines to draw functions, histograms, data and various primitives in 3D. Graphics is fully interactive thus one can rotate objects, insert text etc. Many of such engines are 100% native to Java.
Several Java canvases which can be used to show math objects are shown below:
Canvas | Description |
---|---|
jhplot.HPlot3D | is used for plotting data in 3D jhplot.H2D, jhplot.P2D, jhplot.P3D, jhplot.F2D |
jhplot.HPlotXYZ | is used for plotting data and geometrical objects in 3D using OpenGL. Can be used to show jhplot.H2D, jhplot.P3D, jhplot.F2D |
jhplot.HPlot3DP | is used for plotting parametric and non-parametric functions such as jhplot.FPR |
jhplot.HVisAd | to show data in 2D/3D for visualization and analysis of numerical data using VisAd |
jhplot.HPlotMX | as before, for plotting parametric and non-parametric functions for mathematical "exhibits" |
jhplot.HJavaView | 3D geometry viewer and a mathematical visualization software from http://www.javaview.de |
Showing data in 3D (X-Y-Z plots)
There are several pads to show data in 3D: jhplot.HPlot3D (histograms, functions, data points) and jhplot.HPlot3DP (used for parametric functions). All such canvases are 100% JAVA libraries.
See HPLOT3D examples |
DMelt has many engines to render 3D graphics in fully interactive way. This is rather large topic. In this section we just show a few basic examples.
3D graphics from OFF files
Here is an example to read OFF files. In this case we read the file "teapot.off"
from jhpro.engine3d import * view=HEngine3D("teapot.off") view.setBounds(10, 10, 600, 400) view.setVisible(1)
Read about the definitions of the OFF file Section DMelt:Plots/OFF_Files. This engine does not support showing mathematical functions nor displaying axes.
There are several methods to change the color, transparency, and the solid style.
Use the method "getObject3D()" to return FacetedObject object. This class has a several methods such as:
- setFillColor(Color fillC)
- setShading(boolean shading)
- setSolidRendering(boolean solid)
Advanced 3D graphics
This is the most powerful engine to render 3D graphics and mixing mathematical objects and graphical primitives.
Scientific plots with OpenGL
DMelt includes jhplot.HPlotXYZ class which relies on on JOGL2. You can easily deploy native OpenGL engine for fast rendering of plots. This is done via the JZY3D library [1]. This library allows the generation of pictures without opening a frame, as well as multiple views of the same scene in several panels.
See hplotxy examples |
Here is an example which shows how to draw interactive 3D objects in the 3D canvas using Java and Jython. The code shown below is written in Jython/Python and contains only 10 lines.
This interactive 3D example is based on the Java class jhplot.HPlotXYZ which relies on the JZY3D package and OpenGL
from jhplot import * from java.awt import Color from java.util import Random c1=HPlotXYZ("Hplot",600,400,2,1) c1.setGTitle("Geometrical shapes") c1.visible(1); c1.setColorFill(Color.red) c1.setColorWireframe(Color.white) c1.setWireframe(1) c1.addSphere(1,1,1,2,2,20,20) c1.setColorWireframe(Color.blue) c1.setColorFill(Color.blue) c1.addDisk(1,1,-2,1,2,50,50) c1.update() c1.cd(2,1) col=Color(10,220,20,70) c1.setColorFill(col) c1.setColorWireframe(col) c1.setWireframe(0) c1.addTube(1,1,1,0.5,0.5,2,40,40) c1.update()
Use the method "export("image.png")" to save the image.
Functions in 3D
One can plot functions in 3D, overlaying any number of functions and applying custom colors. In many cases, DMelt relies on on JOGL2 via the JZY3D library [2]. Below a simple Jython example which shows this using 10 lines of code (similarly, one can rewrite this example in Java):
from jhplot import * from java.awt import Color from java.util import Random c1=HPlotXYZ("Hplot") c1.setGTitle("Two overplayed functions") c1.setNameX("X axis") c1.setNameX("Y axis") c1.visible(1); f1=F2D("sin(x/2)*cos(y/2)*y",-10,10,-10,10) c1.setTickRendererAll(0) c1.setColorSolid(False) c1.draw(f1,20,20) f2=F2D("2*sin(y/10)*cos(x/10)",-10,10,-10,10) c1.setTickRendererAll(0) c1.setColorSolid(True) c1.add(f2,20,20) c1.update()
Now we will make a more complicated function: instead of giving a string defining a function, we will build a complex function using the Python language. Our function will look as this:
if x>0: z=a*y*x+b*sin(y) if x<0: z=c*cos(x*y)+sin(y)
As you can see, it has 3 parameters a,b,c. Now let us create a small code snipped to draw such function. We put comments which explain each step:
from shplot import * from jhplot import * import math # here we make a function with 2 variables v[0] and v[1] # it will also contain 3 parameters, p[0], p[1],p[2] class cmt(ifunc): def value(self, v): d=0 if (v[0]>0): d=self.p[0]*v[1]*v[0]+self.p[1]*math.sin(v[1]) if (v[0]<0): d=self.p[2]*math.cos(v[0]*v[1])+math.sin(v[1]) return d p=cmt('My function',2,3) # create function object with 2 variables and 3 parameters print p.dimension() # print properties of this function print p.numberOfParameters() print p.parameterNames() print p.variableNames() print p.variableName(0) p.setParameters([2,-1,1]) # set parameters a=20 print p.value([3,1]) # print value of this function at x=30 f1=F2D(p,-2,2,-2,2) # convert it to the F2D function c1=HPlotXYZ("HPlotXYZ",400,400) c1.setGTitle("Complicated function") c1.setNameX("X axis") c1.setNameX("Y axis") c1.visible() c1.add(f1) c1.update()
Another way to draw functions in3D is to use jhplot.HJavaView canvas. You can plot the standard jhplot.F2D functions using jhplot.HJavaView. You can use either the "draw(F2D)" method, or "add(F2D)" method. The latter returns jv.project.PgGeometry object which can be used to modify style of the function (color, transparency). For example,
from jhplot import HJavaView,F2D c1=HJavaView() f1=F2D("x*y",-1,1,-1,1) obj=c1.add(f1) # now you can add some style c1.draw(obj)
Similarly, one can draw arrays of functions, or one can repeat "draw()" to overlay functions.
Math objects in 3D
One can draw the following mathematical objects: arrays of points with custom sizes and color, histograms and mathematical functions in 3D. All such objects can be overplayed on the same canvas. Below is a small script which plots several datasets in 3D (with blue and red points), functions as surfaces and H2D histograms:
from jhplot import * from java.awt import Color from java.util import Random c1=HPlotXYZ("3D",600,400,2,1) c1.setGTitle("Math objects in 3D") c1.setNameX("X axis") c1.setNameX("Y axis") c1.visible() c1.setTickDecimalAll(1) h1 = P2D("Simple1") h2 = P2D("Simple2") hh=H2D("Histogram",10,-2,2,10,-2,2) rand = Random() for i in range(100): h1.add(rand.nextGaussian(),rand.nextGaussian(),rand.nextGaussian()) h2.add(0.2*rand.nextGaussian(),rand.nextGaussian(),rand.nextGaussian()) for i in range(1000): hh.fill(rand.nextGaussian(),rand.nextGaussian()) f=F2D("x*sin(y)",-2,2,-2,2) c1.add(h1,10,Color.red) c1.add(h2,3,Color.blue) c1.add(f) c1.update() c1.cd(2,1) c1.setTickDecimalAll(1) f=F2D("x*x+y*y",-2,2,-2,2) c1.add(f) c1.add(hh) c1.setColorSolid(0) c1.addAsBars(hh) c1.update()
The output of this code is:
Histograms can be shown using different style. In the above example we use "rainbow" color style for the histogram. One can also show it using solid color or as a surface:
from jhplot import * from java.awt import Color from java.util import Random c1=HPlotXYZ("test",600,400) c1.setGTitle("3D function") c1.visible() c1.setTickDecimalAll(1) hh=H2D("Histogram",20,-2,2,20,-2,2) rand = Random() # fill histogram for i in range(10000): hh.fill(rand.nextGaussian(),rand.nextGaussian()) c1.setContourColor3D() c1.addAsSurface(hh) c1.update()
The output of this code is
Generally, there is significant difference between the 2 main canvases for
3D mathematical visualization. jhplot.HPlot3D does not use hardware acceleration using native OpenGL libraries, On the contrary, jhplot.HPlotXYZ class relies on Jzy3d library with JOGL, you can easily deploy native OpenGL. jhplot.HPlotXYZ is faster for interactive plotting of many mathematical objects (functions, data points, histograms).
3D visualization using VisAd
Data and mathematical objects in 3D can be shown using jhplot.HVisAd canvas that builds a frame integrated with the VisAd, a Java component library for interactive and collaborative visualization and analysis of numerical data. It provides a general display that supports interactive 3D viewer, multiple data views, direct manipulation. The display model has been adapted to Java3D.
There is dedicated VisAd section describing this topic.
Showing 3D math exhibits
Mathematical functions (parametric and non-parametric) can be shown using jhplot.HPlotMX Java class.
Here is a simple Jython script which creates a predefined paraboloid:
from jhplot import * c1=HPlotMX("vmm3d.surface.parametric.Paraboloid") c1.visible()
Here we simply call the class name that defines "Paraboloid". Please read the section DMelt:Plots/6_Math_Exhibits for more detail.