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.Map;

import javajs.awt.Font;
import javajs.util.A4;
import javajs.util.Lst;
import javajs.util.P3;
import javajs.util.PT;
import javajs.util.T3;

import org.jmol.java.BS;
import org.jmol.util.GData;
import org.jmol.viewer.Viewer;

public class _X3dExporter extends _VrmlExporter {

  public _X3dExporter() {
    super();
    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 vwr into model-rotation mode
    output("\n");
    // next is an approximation only
    float angle = getViewpoint();
    output("\n");
    output("\n  \n\n");
    commentChar = null;
    outputInitialTransform();
  }
  
  @Override
  protected void outputAttrPt(String attr, T3 pt) {
    output(" " + attr + "='" + pt.x + " " + pt.y + " " + pt.z + "'");
  }

  @Override
  protected void pushMatrix() {
    output("\n");
  }

  @Override
  protected void outputAttr(String attr, float x, float y, float z) {
    output(" " + attr + "='" + round(x) + " " + round(y) + " " + round(z) + "'");
  }
  
  @Override
  protected void outputRotation(A4 a) {
    output(" rotation='" + a.x + " " + a.y + " " + a.z + " " + a.angle + "'");
  }


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

  @Override
  protected void outputAppearance(short colix, boolean isText) {  
    String def = getDef((isText ? "T" : "") + colix);
    output("");
      else
        output(color + "' transparency='" + translucencyFractionalFromColix(colix) + "'/>" );
    }
    else
      output(def +">");
    output("");
  }
  
  @Override
  protected void outputChildShapeStart() {
    outputShapeStart();
  }

  @Override
  protected void outputShapeStart() {
    output("");
    outputFaceSetStart();
  }

  @Override
  protected void outputChildStart() {
    // not used!
  }

  @Override
  protected void outputChildClose() {
    // not used!
  }

  @Override
  protected void outputDefChildFaceSet(String child) {
    if (child != null)
      output("DEF='" + child + "'");
  }

  @Override
  protected void outputFaceSetStart() {
    output("\n");
  }

  @Override
  protected void outputUseChildClose(String child) {
    output(child + "/>");
  }

  @Override
  protected void outputChildShapeClose() {
    outputShapeClose();
  }

  @Override
  protected void outputShapeClose() {
    output("\n");
  }

  @Override
  protected void outputCloseTag() {
    output(">\n");
  }


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


  @Override
  protected void outputCircle(P3 pt1, P3 pt2, float radius, short colix,
                              boolean doFill) {
    
    // not fixed -- still duplicated in X3d
    if (doFill) {

      // draw filled circle
      pushMatrix();
        output("translation='");
        tempV1.ave(tempP3, pt1);
        output(tempV1);
        output("'>");
        pushMatrix();
          output ("rotation='1 0 0 1.5708'");
          float height = pt1.distance(pt2);
          outputAttr("scale", radius, height, radius);
          output(">");
          outputCylinderChildScaled(colix, GData.ENDCAPS_FLAT);
        popMatrix();
        output("");
      popMatrix();
      
      return;
    }
    
    // draw a thin torus

    String child = getDef("C" + colix + "_" + radius);
    pushMatrix();
      outputTransRot(tempP3, pt1, 0, 0, 1);
      tempP3.set(1, 1, 1);
      tempP3.scale(radius);
      outputAttr("scale", tempP3.x, tempP3.y, tempP3.z);
      output(">\n");
        pushMatrix();
          output("");
          outputAppearance(colix, false);
          output("");
        popMatrix();
      } else {
        output(child + ">");
      }
      output("\n");
    popMatrix();
  }

  @Override
  protected void outputGeometry(T3[] vertices, T3[] normals,
                              short[] colixes, int[][] indices,
                              short[] polygonColixes,
                              int nVertices, int nPolygons,
                              BS bsPolygons,
                              int faceVertexMax, Lst colorList, Map htColixes, P3 offset) {
    
    output(" creaseAngle='0.5'\n");

    if (polygonColixes != null)
      output(" colorPerVertex='false'\n");

    // coordinates, part 1

    output("coordIndex='\n");
    int[] map = new int[nVertices];
    getCoordinateMap(vertices, map, null);
    outputIndices(indices, map, nPolygons, bsPolygons, faceVertexMax);
    output("'\n");

    // normals, part 1  
    
    Lst vNormals = null;
    if (normals != null) {
      vNormals = new  Lst();
      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");
    }
  }

  @Override
  protected void outputTextPixel(P3 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, Font font3d) {
//    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(fontChild + ">");
//    }
//    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
//     */
  }

  //  @Override
  //  protected void outputCone(P3 ptBase, P3 ptTip, float radius,
  //                            short colix) {
  //    float height = ptBase.distance(ptTip);
  //    pushMatrix();
  //      outputTransRot(ptBase, ptTip, 0, 1, 0);
  //      outputAttr("scale", radius, height, radius);
  //      outputCloseTag();
  //      outputShapeStart();
  //      String child = getDef("c");
  //      if (child.charAt(0) == '_') {
  //        outputDefChildFaceSet(child);
  //        outputConeGeometry(true);
  //        outputCloseFaceSet();
  //      } else {
  //        outputChildClose(child);
  //      }
  //      outputAppearance(colix, false);
  //      outputShapeClose();
  //    popMatrix();
  //  }
  //
  //  @Override
  //  protected boolean outputCylinder(P3 ptCenter, P3 pt1, P3 pt2,
  //                                short colix, byte endcaps, float radius, P3 ptX, P3 ptY, boolean checkRadius) {
  //    float height = pt1.distance(pt2);
  //    pushMatrix();
  //      if (ptX == null) {
  //        outputTransRot(pt1, pt2, 0, 1, 0);
  //        outputAttr("scale", radius, height, radius);
  //      } else {
  //        outputAttrPt("translation", ptCenter);
  //        outputQuaternionFrame(ptCenter, ptY, pt1, ptX, 2, 2, 2);
  //        pt1.set(0, 0, -0.5f);
  //        pt2.set(0, 0, 0.5f);
  //      }
  //      outputCloseTag();
  //      outputCylinderChildScaled(colix, endcaps);
  //    popMatrix();
  ////    if (endcaps == GData.ENDCAPS_SPHERICAL) {
  ////      outputSphere(pt1, radius * 1.01f, colix, true);
  ////      outputSphere(pt2, radius * 1.01f, colix, true);
  ////    }
  //    return true;
  //  }
  //
  //  @Override
  //  protected void outputCylinderChildScaled(short colix,
  //                                   byte endcaps) {
  //    outputShapeStart();
  //    String child = getDef("C" + "_" + endcaps);
  //    if (child.charAt(0) == '_') {
  //      outputDefChildFaceSet(child);
  //      outputCylinderGeometry(endcaps);
  //      outputCloseFaceSet();
  //    } else {
  //      outputUseChildClose(child);
  //    }
  //    outputAppearance(colix, false);
  //    outputShapeClose();
  //  }

//  @Override
//  protected void outputSurface(T3[] vertices, T3[] normals,
//                               short[] colixes, int[][] indices,
//                               short[] polygonColixes,
//                               int nVertices, int nPolygons, int nTriangles, BS bsPolygons,
//                               int faceVertexMax, short colix,
//                               Lst colorList, Map htColixes, P3 offset) {
//    output("");
//    outputAppearance(colix, false);
//    output("\n");
//  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy