DMelt:Plots/6 Math Exhibits

From HandWiki
Member

3D math exhibits

In addition to the HPlot, HPlot2D, HPlot3D canvaces, DataMelt offers another canvas for for visualizing and experimenting with a variety of mathematical objects or "exhibits." This class is called jhplot.HPlotMX jhplot.HPlotMX

The class is based on 3D-XplorMath program of the 3DXM Consortium. DataMelt uses the base code of this program but adds a support for vector graphics i.e. one can export images to EPS, SVG or PDF formats. Thus, all images are fully scalable and are ready to be included to publications. If you need a similar program but with animation support (but without vector graphics export), use the DataMelt IDE ("See the Plot menu).

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". The output of this script is shown below:

DMelt example: Show predefined parametric functions from HPlotMX

A similar example can be rewritten in BeanShell, Groovy or Java languages. Here is the same example in the Groovy scripting. Make a file "test.groovy", and run it in DataMelt:

import jhplot.*
c1=new HPlotMX("vmm3d.surface.parametric.Paraboloid")
c1.visible()

In the above example we simply call the class name that defines a paraboloid function, see vmm3d.surface.parametric.Paraboloid vmm3d.surface.parametric.Paraboloid class. There are about 60 predefined parametric mathematical functions that can be called to display. They are listed in the description of parametric functions in the vmm3d/surface/parametric/package-summary vmm3d/surface/parametric/package-summary.

From the above list, one can pick up any math object and path its name as java.lang.String java.lang.String. Alternatively, one can construct mathematical exhibit by creating the object itself:

from jhplot import *
from vmm3d.surface.parametric import Torus, KuenSurface 
 
c1=HPlotMX()
ks=KuenSurface()  #  create Kuen Surface
c1.draw(ks)
# ks=Torus()      #  create torus 
# c1.draw(ks)
c1.visible()

The output is mathematical object called "Kuen Surface" shown below:

DMelt example: Create 3D Kuen Surface using interactive HPlotMX

As usual, use the method "export" to create an image file. For example, we can create EPS, PDF and SVG figures using the vector formats as:

c1.export("KuenSurface.eps")
c1.export("KuenSurface.pdf")
c1.export("KuenSurface.svg")

Showing custom functions

Now we consider how to draw custom parametric or non-parametric functions, instead of showing predefined functions. First we create a function of torus using pure-Java approach:

Using Java to build custom function

Here is an example showing how to create a 3D surface plot using Java programming language:

from jhplot import *
from vmm3d.surface.parametric import SurfaceParametric 
from vmm3d.core import RealParamAnimateable,RealParam 
from vmm3d.core3D import Vector3D,View3DLit 
from java.lang import Math
 


class torus(SurfaceParametric):
   global aa,bb,cc
   aa=RealParam("genericParam.aa",1.75)
   bb=RealParam("genericParam.bb",0.4)
   cc=RealParam("genericParam.cc",0.4)
 
   def __init__(self):
                self.setU(0,2*3.142) # min and max for U
                self.setV(0,2*3.142)  # min and max for V
                self.setPatchCount(8,8) # number of divisions in X nad Y
                self.setViewpoint(10,-10,10)
                self.setDefaultWindow(-2.5,2.5,-2.5,2.5)
                self.setDefaultOrientation(View3DLit.NO_ORIENTATION);
                self.addParameter(aa)
                self.addParameter(bb)
                self.addParameter(cc)
   def surfacePoint(self, u, v):
       AA=aa.getValue()
       BB=bb.getValue()
       CC=cc.getValue()
       x = (AA + BB * Math.cos(u))* Math.cos(v)
       y = (AA + BB * Math.cos(u))* Math.sin(v)
       z = CC * Math.sin(u)
       return Vector3D(x,y,z)

c1=HPlotMX() 
c1.draw(torus())
c1.visible()
# export to some image (png,eps,pdf,jpeg...)
# c1.export(Editor.DocMasterName()+".png");

You can compile it to jar files, put it to "lib/user" directory, restart DataMelt and run it using the same approach as before.

The defined torus will be shown below:

DMelt example: Create 3D torus using interactive HPlotMX

Using Jython to build custom function

Instead of defining the function as a Java code, one can define it inside the same Jython script that is used to show the canvas with the object. Here is a simple Jython code create torus in 3D:

from jhplot import *
from vmm3d.surface.parametric import SurfaceParametric 
from vmm3d.core import RealParamAnimateable,RealParam 
from vmm3d.core3D import Vector3D,View3DLit 
from java.lang import Math
 


class torus(SurfaceParametric):
   global aa,bb,cc
   aa=RealParam("genericParam.aa",1.75)
   bb=RealParam("genericParam.bb",0.4)
   cc=RealParam("genericParam.cc",0.4)
 
   def __init__(self):
                self.setU(0,2*3.142) # min and max for U
                self.setV(0,2*3.142)  # min and max for V
                self.setPatchCount(8,8) # number of divisions in X nad Y
                self.setViewpoint(10,-10,10)
                self.setDefaultWindow(-2.5,2.5,-2.5,2.5)
                self.setDefaultOrientation(View3DLit.NO_ORIENTATION);
                self.addParameter(aa)
                self.addParameter(bb)
                self.addParameter(cc)
   def surfacePoint(self, u, v):
       AA=aa.getValue()
       BB=bb.getValue()
       CC=cc.getValue()
       x = (AA + BB * Math.cos(u))* Math.cos(v)
       y = (AA + BB * Math.cos(u))* Math.sin(v)
       z = CC * Math.sin(u)
       return Vector3D(x,y,z)

c1=HPlotMX() 
c1.draw(torus())
c1.visible()
# export to some image (png,eps,pdf,jpeg...)
# c1.export(Editor.DocMasterName()+".png");

Run this Jython/Python script and you will see the identical torus. You can change any parameters. For example, inserting "setV(0,2)", we will generate only a part of the torus as shown bellow:

DMelt example: Create 3D torus using interactive HPlotMX

You can reduce the number of patches used to draw the torus, making this picture rendering faster.

Using Groovy to build custom function

Here is the same method to create a torus using Groovy scripting:

import jhplot.*
import vmm3d.surface.parametric. *
import vmm3d.core.*
import vmm3d.core3D.*
import java.lang.Math


public class Torus extends SurfaceParametric {

        private RealParam aa = new RealParam("genericParam.aa","1.75");
        private RealParam bb = new RealParam("genericParam.bb","0.4");
        private RealParam cc = new RealParam("genericParam.cc","0.4");


        public Torus() {
                uPatchCount.setValueAndDefault(12);
                vPatchCount.setValueAndDefault(32);
                umin.reset("0");
                umax.reset("2 pi");
                vmin.reset("0");
                vmax.reset("2 pi");
                setDefaultViewpoint(new Vector3D(10,-10,10));
                setDefaultWindow(-2.5,2.5,-2.5,2.5);
                setDefaultOrientation(View3DLit.NO_ORIENTATION);
                addParameter(aa);
                addParameter(bb);
                addParameter(cc);
        }

        public Vector3D surfacePoint(double u, double v) {
            double AA = aa.getValue();
            double BB = bb.getValue();
            double CC = cc.getValue();
            double x = (AA + BB * Math.cos(u))* Math.cos(v);
                double y = (AA + BB * Math.cos(u))* Math.sin(v);
                double z = CC * Math.sin(u);
                return new Vector3D(x,y,z);
        }

}
 

c1=new HPlotMX()
c1.draw(new Torus())
c1.visible()
// c1.export(Editor.DocMasterName()+".png");