All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jmol.export._X3dExporter Maven / Gradle / Ivy

There is a newer version: 14.31.10
Show newest version
/* $RCSfile$
 * $Author: aherraez $
 * $Date: 2009-01-15 21:00:00 +0100 (Thu, 15 Jan 2009) $
 * $Revision: 7752 $

 *
 * Copyright (C) 2003-2009  The Jmol Development Team
 *
 * Contact: [email protected]
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*	Based on _VrmlExporter  by rhanson
		and Help from http://x3dgraphics.com/examples/X3dForWebAuthors/index.html
*/
 
package org.jmol.export;


import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.Map;

import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import org.jmol.g3d.Font3D;
import org.jmol.g3d.Graphics3D;
import org.jmol.util.Escape;
import org.jmol.viewer.Viewer;

public class _X3dExporter extends _VrmlExporter {

  public _X3dExporter() {
    useTable = new UseTable("USE='");
  }
  

  @Override
  protected void outputHeader() {
    output("\n");
    output("\n");
    output(""
      + "\n");
    output("\n");
    output("\n");
    output("\n");
    output("\n");
    output("\n");
    output("\n");
		output("\n");
    output("\n");
    output("\n");

    output("\n");
    // puts the viewer into model-rotation mode
    output("\n");
    // next is an approximation only 
    float angle = (float) (aperatureAngle * Math.PI / 180);
    viewer.getAxisAngle(viewpoint);
    output("\n");
    output("\n  \n\n");

    output("\n");
  }

  @Override
  protected void outputFooter() {
    useTable = null;
    output("\n");
    output("\n");
    output("\n");
  }

  @Override
  protected void outputAppearance(short colix, boolean isText) {  
    String def = useTable.getDef((isText ? "T" : "") + colix);
    output("");
      else
        output(color + "' transparency='" + translucencyFractionalFromColix(colix) + "'/>" );
    }
    else
      output(def +">");
    output("");
  }
  
  private void outputTransRot(Point3f pt1, Point3f pt2, int x, int y, int z) {    
    output(" ");
    outputTransRot(pt1, pt2, x, y, z, "='", "'");
  }
  
  @Override
  protected void outputCircle(Point3f pt1, Point3f pt2, float radius, short colix,
                              boolean doFill) {
    if (doFill) {

      // draw filled circle
      
      output("");
      outputCylinderChild(pt1, tempP3, colix, Graphics3D.ENDCAPS_FLAT, radius);
      output("");
      output("\n");
      
      return;
    }
    
    // draw a thin torus

    String child = useTable.getDef("C" + colix + "_" + radius);
    output("\n");
      output("");
      outputAppearance(colix, false);
      output("");
    } else {
      output(child + ">");
    }
    output("\n");
    output("\n");
  }

  @Override
  protected void outputCone(Point3f ptBase, Point3f ptTip, float radius,
                            short colix) {
    float height = ptBase.distance(ptTip);
    output("\n");
      cone = useTable.getDef(cone);
      output("");
      } else {
        output(cone + "/>");
      }
      outputAppearance(colix, false);
    } else {
      output(child + ">");
    }
    output("\n");
    output("\n");
  }

  @Override
  protected boolean outputCylinder(Point3f ptCenter, Point3f pt1, Point3f pt2,
                                short colix, byte endcaps, float radius, Point3f ptX, Point3f ptY, boolean checkRadius) {
    output("\n");
    outputCylinderChild(pt1, pt2, colix, endcaps, radius);
    output("\n\n");
    if (endcaps == Graphics3D.ENDCAPS_SPHERICAL) {
      outputSphere(pt1, radius * 1.01f, colix, true);
      outputSphere(pt2, radius * 1.01f, colix, true);
    }
    return true;
  }

  private void outputCylinderChild(Point3f pt1, Point3f pt2, short colix,
                                   byte endcaps, float radius) {
    float length = pt1.distance(pt2);
    String child = useTable.getDef("C" + colix + "_" + (int) (length * 100) + "_"
        + radius + "_" + endcaps);
    output("");
      output("");
      } else {
        output(cyl + "/>");
      }
      outputAppearance(colix, false);
    } else {
      output(child + ">");
    }
    output("");
  }
  
  @Override
  protected void outputEllipsoid(Point3f center, Point3f[] points, short colix) {
    output("");
    tempP3.set(0, 0, 0);
    outputSphereChild(tempP3, 1.0f, colix);
    output("\n");
  }

  @Override
  protected void outputSphereChild(Point3f center, float radius, short colix) {
    output("\n");
      output("");
      outputAppearance(colix, false);
    } else {
      output(child + ">");
    }
    output("\n");
    output("\n");
  }

  @Override
  protected void outputSurface(Point3f[] vertices, Vector3f[] normals,
                               short[] colixes, int[][] indices,
                               short[] polygonColixes,
                               int nVertices, int nPolygons, int nFaces, BitSet bsPolygons,
                               int faceVertexMax, short colix,
                               List colorList, Map htColixes, Point3f offset) {
    output("\n");
    outputAppearance(colix, false);
    output(" vNormals = null;
    if (normals != null) {
      vNormals = new ArrayList();
      map = getNormalMap(normals, nVertices, null, vNormals);
      output("  solid='false'\n  normalPerVertex='true'\n  normalIndex='\n");
      outputIndices(indices, map, nPolygons, bsPolygons, faceVertexMax);
      output("'\n");
    }      
    
    map = null;
    
    // colors, part 1
        
    if (colorList != null) {
      output("  colorIndex='\n");
      outputColorIndices(indices, nPolygons, bsPolygons, faceVertexMax, htColixes, colixes, polygonColixes);
      output("'\n");
    }    

    output(">\n");  // closes IndexedFaceSet opening tag
    
    // coordinates, part 2
    
    output("\n");

    // normals, part 2

    if (normals != null) {
      output("\n");
    }

    // colors, part 2

    if (colorList != null) {
      output("\n");
    }
   
    output("\n");
    output("\n");
    
  }

  @Override
  protected void outputTriangle(Point3f pt1, Point3f pt2, Point3f pt3, short colix) {
    // nucleic base
    // cartoons
    output("\n");
    output("");
    output("");
    output("\n");
    outputAppearance(colix, false);
    output("\n\n");
  }

  @Override
  protected void outputTextPixel(Point3f pt, int argb) {
    // text only
    String color = rgbFractionalFromArgb(argb, ' ');
    output("\n");
      output("");
      output("'");
    } else {
      output(child + ">");
    }
    output("\n");
    output("\n");
  }

  @Override
  void plotText(int x, int y, int z, short colix, String text, Font3D font3d) {
    if (z < 3)
      z = viewer.getFrontPlane();
    String useFontStyle = font3d.fontStyle.toUpperCase();
    String preFontFace = font3d.fontFace.toUpperCase();
    String useFontFace = (preFontFace.equals("MONOSPACED") ? "TYPEWRITER"
        : preFontFace.equals("SERIF") ? "SERIF" : "SANS");
    output("");
    // These x y z are 3D coordinates of echo or the atom the label is attached
    // to.
    output(""
        + ""
        + "");
      outputAppearance(colix, true);
      output("");
      output("");      
      } else {
        output(fontstyle + "/>");
      }
      output("");
      output("");
      output("");
    } else {
      output(child + ">");
    }
    output("\n");
    output("\n");

    /*
     * Unsolved issues: # Non-label texts: echos, measurements :: need to get
     * space coordinates, not screen coord. # Font size: not implemented; 0.4A
     * is hardcoded (resizes with zoom) Java VRML font3d.fontSize = 13.0 size
     * (numeric), but in angstroms, not pixels font3d.fontSizeNominal = 13.0 #
     * Label offsets: not implemented; hardcoded to 0.25A in each x,y,z #
     * Multi-line labels: only the first line is received # Sub/superscripts not
     * interpreted
     */
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy