Use a quad mesh factory

From JReality Wiki
Jump to: navigation, search

Source file: QuadMeshExample

JavaDoc: QuadMeshFactory


Run as Java webstart


A quad mesh is a special case an of an IndexedFaceSet in which the faces are derived from the vertices of a rectangular array of vertices located in 3-space. We can think of this as a rectangle in (u,v) parameter space, which is mapped into 3-dimensional space. In the tutorial example the first step is to generate these vertices and store them in an array of type double[][][], which I'll refer to as a 3-layer array (to distinguish it from a 3-dimensional (x,y,z) point). In this case the surface is a so-called swallowtail which arises in the analysis of singularities.


		double [][][] coords = new double [N][N][3];
		for( int i=0; i<N; i++) {
			double v = -.4 + .8*(i/(N-1.0));
			for (int j = 0; j<N; ++j)	{
				double u = -.3 + .6*(j/(N-1.0));
				coords[i][j][0] = 10*(u-v*v);
				coords[i][j][1]= 10*u*v;
				coords[i][j][2]= 10*(u*u-4*u*v*v);
			}
		}


Here's how to construct the quad mesh from this data:

		QuadMeshFactory factory = new QuadMeshFactory();
		factory.setVLineCount(20);		// important: the v-direction is the left-most index
		factory.setULineCount(20);		// and the u-direction the next-left-most index
		factory.setClosedInUDirection(false);	
		factory.setClosedInVDirection(false);	
		factory.setVertexCoordinates(coords);	
		factory.setGenerateFaceNormals(true);
		factory.setGenerateTextureCoordinates(true);
		factory.setGenerateEdgesFromFaces(true);
		factory.setEdgeFromQuadMesh(true);	// generate "long" edges: one for each u-, v- parameter curve


Note that the factory accepts the 3-layer array as argument to the setVertexCoordinates() method, in contrast to the IndexedFaceSetFactory. This is because the (u,v) parameters are built-in to the factory and it therefore knows how to interpret the 3 layers of the array as (v, u, [xyz]) information.


The method call factory.setEdgeFromQuadMesh(true) (in conjunction with the call etGenerateEdgesFromFaces(true)), has the effect that the edge information which is generated is structured as a set of curves, one for each constant u- or v- value. The default, inherited from IndexedFaceSetFactory would be to generate a longer list of edges, each consisting of a single line segment. The code presented here allows one later to invoke for example the method IndexedFaceSetUtility.extractEdge(double[][] curve, int which) to extract a complete u- or v- parameter curve of the surface.


Previous: Create a geometry using factories Developer Tutorial: Contents Next: Parametrized Surfaces