From: <ha...@us...> - 2007-10-07 20:03:32
|
Revision: 8377 http://jmol.svn.sourceforge.net/jmol/?rev=8377&view=rev Author: hansonr Date: 2007-10-07 13:03:29 -0700 (Sun, 07 Oct 2007) Log Message: ----------- 11.3.31 VERSION # new feature: set atom properties using {xxxx}.x = y # # {atomno=3}.x = 3 # {(*)[2]}.xyz = {1 2 3} # for(var i = 1; i < 3; i = i + 1); {(*)[i]}.x = 5;end for; # new feature: set atom properties using an array instead of a DATA statement: # {*}.x = array(2,3,4,5,6}; # {atomno < 4}.xyz = array({3 4 5},{4 5 6},{5 6 7}) Modified Paths: -------------- trunk/Jmol/src/org/jmol/modelset/ModelManager.java trunk/Jmol/src/org/jmol/util/Parser.java trunk/Jmol/src/org/jmol/viewer/Eval.java trunk/Jmol/src/org/jmol/viewer/Jmol.properties trunk/Jmol/src/org/jmol/viewer/Viewer.java Added Paths: ----------- trunk/Jmol/src/org/jmol/viewer/DataManager.java Modified: trunk/Jmol/src/org/jmol/modelset/ModelManager.java =================================================================== --- trunk/Jmol/src/org/jmol/modelset/ModelManager.java 2007-10-07 14:32:02 UTC (rev 8376) +++ trunk/Jmol/src/org/jmol/modelset/ModelManager.java 2007-10-07 20:03:29 UTC (rev 8377) @@ -1404,9 +1404,22 @@ modelSet.setAtomCoord(atomIndex, x, y, z); } - public void setAtomCoord(BitSet bs, int tokType, Point3f xyz) { - for (int i = getAtomCount(); --i >= 0;) - if (bs.get(i)) { + public void setAtomCoord(BitSet bs, int tokType, Object xyzValues) { + Point3f xyz = (xyzValues instanceof Point3f ? (Point3f) xyzValues : null); + String[] values = (xyzValues instanceof String[] ? (String[]) xyzValues : null); + int nValues = (values == null ? Integer.MAX_VALUE : values.length); + if (xyz == null && values == null || nValues == 0) + return; + int n = 0; + int atomCount = getAtomCount(); + for (int i = 0; i < atomCount; i++) + if (bs.get(i) && n < nValues) { + if (values != null) { + Object o = Escape.unescapePoint(values[n++]); + if (!(o instanceof Point3f)) + break; + xyz = (Point3f) o; + } switch (tokType) { case Token.xyz: modelSet.setAtomCoord(i, xyz.x, xyz.y, xyz.z); Modified: trunk/Jmol/src/org/jmol/util/Parser.java =================================================================== --- trunk/Jmol/src/org/jmol/util/Parser.java 2007-10-07 14:32:02 UTC (rev 8376) +++ trunk/Jmol/src/org/jmol/util/Parser.java 2007-10-07 20:03:29 UTC (rev 8377) @@ -45,7 +45,10 @@ * @param data the (sparce) array to fill */ public static void parseFloatArray(String str, BitSet bs, float[] data) { - String[] tokens = getTokens(str); + parseFloatArray(getTokens(str), bs, data); + } + + public static void parseFloatArray(String[] tokens, BitSet bs, float[] data) { int len = data.length; int nTokens = tokens.length; int n = 0; @@ -90,6 +93,8 @@ int iData; if (isMatch) { iData = parseInt(tokens[fieldMatch - 1]); + //in the fieldMatch column we have an integer pointing into matchData + //we replace that number then with the corresponding number in matchData if (iData == Integer.MIN_VALUE || iData < 0 || iData >= len || (iData = matchData[iData]) < 0) continue; Added: trunk/Jmol/src/org/jmol/viewer/DataManager.java =================================================================== --- trunk/Jmol/src/org/jmol/viewer/DataManager.java (rev 0) +++ trunk/Jmol/src/org/jmol/viewer/DataManager.java 2007-10-07 20:03:29 UTC (rev 8377) @@ -0,0 +1,173 @@ +/* $RCSfile$ + * $Author: hansonr $ + * $Date: 2007-10-03 20:53:36 -0500 (Wed, 03 Oct 2007) $ + * $Revision: 8351 $ + * + * Copyright (C) 2003-2005 Miguel, Jmol Development Team + * + * Contact: jmo...@li... + * + * 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. + */ +package org.jmol.viewer; + +import java.util.BitSet; +import java.util.Enumeration; +import java.util.Hashtable; + +import org.jmol.util.ArrayUtil; +import org.jmol.util.Escape; +import org.jmol.util.Parser; + +/* + * a class for storing and retrieving user data, + * including atom-related and color-related data + * + */ + +class DataManager { + + private Hashtable dataValues = new Hashtable(); + + DataManager() { + } + + void clear() { + dataValues.clear(); + } + + void setData(String type, Object[] data, int atomCount, + int matchField, int field) { + //Eval + /* + * data[0] -- label + * data[1] -- string or float[] + * data[2] -- selection bitset or int[] atomMap when field < 0 + * + * matchField = data must match atomNo in this column, >= 1 + * field = column containing the data, >= 1: + * 0 ==> values are a simple list; clear the data + * Integer.MAX_VALUE ==> values are a simple list; don't clear the data + * Integer.MIN_VALUE ==> one SINGLE data value should be used for all selected atoms + */ + if (data[2] != null) { + boolean createNew = (matchField != 0 + || field != Integer.MIN_VALUE && field != Integer.MAX_VALUE); + Object[] oldData = (Object[]) dataValues.get(type); + float[] f = (oldData == null || createNew ? new float[atomCount] + : ArrayUtil.ensureLength(((float[]) oldData[1]), atomCount)); + String stringData = (String) data[1]; + BitSet bs; + String[] strData = null; + if (field == Integer.MIN_VALUE && + (strData = Parser.getTokens(stringData)).length > 1) + field = 0; + if (field == 0 || field == Integer.MAX_VALUE) { + bs = (BitSet) data[2]; + if (strData == null) + strData = Parser.getTokens(stringData); + Parser.parseFloatArray(strData, bs, f); + } else if (matchField == 0) { + bs = (BitSet) data[2]; + Parser.parseFloatArrayFromMatchAndField(stringData, bs, 0, null, field, f); + } else { + bs = new BitSet(); + int[] iData = (int[]) data[2]; + Parser.parseFloatArrayFromMatchAndField(stringData, null, matchField, iData, field, f); + for (int i = iData.length; --i >= 0; ) + bs.set(iData[i]); + } + if (oldData != null && oldData[2] instanceof BitSet && !createNew) + bs.or((BitSet)(oldData[2])); + data[2] = bs; + data[1] = f; + } + dataValues.put(type, data); + } + + Object[] getData(String type) { + if (dataValues == null) + return null; + if (type.equalsIgnoreCase("types")) { + String[] info = new String[2]; + info[0] = "types"; + info[1] = ""; + int n = 0; + Enumeration e = (dataValues.keys()); + while (e.hasMoreElements()) + info[1] += (n++ == 0 ? "," : "") + e.nextElement(); + return info; + } + return (Object[]) dataValues.get(type); + } + + float[] getDataFloat(String label) { + if (dataValues == null) + return null; + Object[] data = getData(label); + if (data == null || !(data[1] instanceof float[])) + return null; + return (float[]) data[1]; + } + + float getDataFloat(String label, int atomIndex) { + if (dataValues != null) { + Object[] data = getData(label); + if (data != null && data[1] instanceof float[]) { + float[] f = (float[]) data[1]; + if (atomIndex < f.length) + return f[atomIndex]; + } + } + return Float.NaN; + } + + void getDataState(StringBuffer s, StringBuffer sfunc) { + if (dataValues == null) + return; + Enumeration e = (dataValues.keys()); + int n = 0; + while (e.hasMoreElements()) { + String name = (String) e.nextElement(); + if (name.indexOf("property_") == 0) { + if (n == 0) + s.append("function _setDataState();\n"); + n++; + Object data = ((Object[]) dataValues.get(name))[1]; + if (data instanceof float[]) { + BitSet bs = (BitSet)((Object[]) dataValues.get(name))[2]; + s.append("\n select " + Escape.escape(bs)) + .append(";\n DATA \"").append(name).append("\""); + float[] f = (float[]) data; + int j = 0; + for (int i = 0; i < f.length; i++) + if (bs.get(i)) + s.append((j++ % 10) == 0 ? "\n " : " ").append(f[i]); + s.append("\n"); + } else { + s.append("\n DATA \"").append(name).append("\""); + s.append(data); + } + s.append(" end \"").append(name).append("\";\n"); + } + } + if (n == 0) + return; + sfunc.append(" _setDataState\n"); + s.append("end function;\n\n"); + } + + +} Modified: trunk/Jmol/src/org/jmol/viewer/Eval.java =================================================================== --- trunk/Jmol/src/org/jmol/viewer/Eval.java 2007-10-07 14:32:02 UTC (rev 8376) +++ trunk/Jmol/src/org/jmol/viewer/Eval.java 2007-10-07 20:03:29 UTC (rev 8377) @@ -3778,7 +3778,7 @@ BitSet bsAtoms = new BitSet(atomCount); if (matchField > 0) { atomMap = new int[atomCount + 1]; - for (int j = 0; j < atomCount; j++) + for (int j = 0; j <= atomCount; j++) atomMap[j] = -1; for (int j = 0; j < atomCount; j++) { if (!bs.get(j)) @@ -6373,11 +6373,10 @@ bs = (BitSet) t.value; } if (propertyName.startsWith("property_")) { - int n = viewer.getAtomCount(); - String s = (tv.tok == Token.integer ? "" + tv.intValue : "" + tv.value); viewer.setData(propertyName, - new Object[] { propertyName, s, bs }, n, 0, - Integer.MIN_VALUE); + new Object[] { propertyName, Token.sValue(tv), bs }, + viewer.getAtomCount(), 0, + tv.tok == Token.list ? Integer.MAX_VALUE : Integer.MIN_VALUE); return; } setBitsetProperty(bs, tokProperty, Token.iValue(tv), Token.fValue(tv), tv); @@ -7129,7 +7128,7 @@ case Token.xyz: case Token.fracXyz: case Token.vibXyz: - viewer.setAtomCoord(bs, tok, (Point3f) tokenValue.value); + viewer.setAtomCoord(bs, tok, tokenValue.value); break; case Token.color: viewer.setShapeProperty(JmolConstants.SHAPE_BALLS, "color", new Integer(iValue), bs); Modified: trunk/Jmol/src/org/jmol/viewer/Jmol.properties =================================================================== --- trunk/Jmol/src/org/jmol/viewer/Jmol.properties 2007-10-07 14:32:02 UTC (rev 8376) +++ trunk/Jmol/src/org/jmol/viewer/Jmol.properties 2007-10-07 20:03:29 UTC (rev 8377) @@ -22,7 +22,15 @@ # {atomno=3}.x = 3 # {(*)[2]}.xyz = {1 2 3} # for(var i = 1; i < 3; i = i + 1); {(*)[i]}.x = 5;end for; +# {atomno=3}.property_whatever = 2.0 +# new feature: set atom properties using an array instead of a DATA statement: + +# {*}.x = array(2,3,4,5,6); +# {*}.property_mydata = array(1.0,5.5,4.4,3.3); +# {*}.property_mydata = "3 4 5 6 7 8"; +# {atomno < 4}.xyz = array({3 4 5},{4 5 6},{5 6 7}) + # code optimization for within() and setAtomBits() # ----------------------------------------------------------------------------- Modified: trunk/Jmol/src/org/jmol/viewer/Viewer.java =================================================================== --- trunk/Jmol/src/org/jmol/viewer/Viewer.java 2007-10-07 14:32:02 UTC (rev 8376) +++ trunk/Jmol/src/org/jmol/viewer/Viewer.java 2007-10-07 20:03:29 UTC (rev 8377) @@ -58,7 +58,6 @@ import java.util.BitSet; import java.util.Properties; import java.util.Vector; -import java.util.Enumeration; import javax.vecmath.Point3f; import javax.vecmath.Vector3f; import javax.vecmath.Point4f; @@ -116,6 +115,7 @@ return compiler; } private Eval eval; + private DataManager dataManager; private FileManager fileManager; private ModelManager modelManager; public MouseManager mouseManager; @@ -178,6 +178,7 @@ propertyManager = new PropertyManager(this); tempManager = new TempArray(); fileManager = new FileManager(this, modelAdapter); + dataManager = new DataManager(); repaintManager = new RepaintManager(this); compiler = new Compiler(this); eval = new Eval(this); @@ -1754,7 +1755,7 @@ tempManager.clear(); //setRefreshing(true); refresh(0, "Viewer:clear()"); - setData(null, null, 0, 0, 0); + dataManager.clear(); System.gc(); } @@ -2265,7 +2266,9 @@ s.append(fileManager.getState(sfunc)); // numerical values s.append(global.getState(sfunc)); - getDataState(s,sfunc); + + dataManager.getDataState(s,sfunc); + // definitions, connections, atoms, bonds, labels, echos, shapes s.append(modelManager.getState(sfunc)); // color scheme @@ -2285,57 +2288,10 @@ return modelManager.getStructureState(); } - Hashtable dataValues = new Hashtable(); - - void setData(String type, Object[] data, int atomCount, - int matchField, int field) { - //Eval - /* - * data[0] -- label - * data[1] -- string or float[] - * data[2] -- selection bitset or int[] atomMap when field < 0 - * - * matchField = Integer.MIN_VALUE ==> one SINGLE data value should be used for all selected atoms - */ - if (type == null) { - dataValues.clear(); - return; - } - if (data[2] != null) { - float[] f = new float[atomCount]; - String stringData = (String) data[1]; - if (field == 0) - Parser.parseFloatArray(stringData, (BitSet) data[2], f); - else if (matchField == 0) - Parser.parseFloatArrayFromMatchAndField(stringData, (BitSet) data[2], - 0, null, field, f); - else - Parser.parseFloatArrayFromMatchAndField(stringData, null, matchField, - (int[]) data[2], field, f); - data[1] = f; - } - dataValues.put(type, data); - } - - Object[] getData(String type) { - if (dataValues == null) - return null; - if (type.equalsIgnoreCase("types")) { - String[] info = new String[2]; - info[0] = "types"; - info[1] = ""; - int n = 0; - Enumeration e = (dataValues.keys()); - while (e.hasMoreElements()) - info[1] += (n++ == 0 ? "," : "") + e.nextElement(); - return info; - } - return (Object[]) dataValues.get(type); - } - void setCurrentColorRange(String label) { float[] data = getDataFloat(label); - BitSet bs = (data == null ? null : (BitSet) ((Object[]) getData(label))[2]); + BitSet bs = (data == null ? null + : (BitSet) ((Object[]) dataManager.getData(label))[2]); setCurrentColorRange(data, bs); } @@ -2351,58 +2307,23 @@ return colorManager.getCurrentColorRange(); } + void setData(String type, Object[] data, int atomCount, int matchField, + int field) { + dataManager.setData(type, data, atomCount, matchField, field); + } + + Object[] getData(String type) { + return dataManager.getData(type); + } + float[] getDataFloat(String label) { - if (dataValues == null) - return null; - Object[] data = getData(label); - if (data == null || !(data[1] instanceof float[])) - return null; - return (float[]) data[1]; + return dataManager.getDataFloat(label); } float getDataFloat(String label, int atomIndex) { - if (dataValues != null) { - Object[] data = getData(label); - if (data != null && data[1] instanceof float[]) { - float[] f = (float[]) data[1]; - if (atomIndex < f.length) - return f[atomIndex]; - } - } - return Float.NaN; + return dataManager.getDataFloat(label, atomIndex); } - private void getDataState(StringBuffer s, StringBuffer sfunc) { - if (dataValues == null) - return; - Enumeration e = (dataValues.keys()); - int n = 0; - while (e.hasMoreElements()) { - String name = (String) e.nextElement(); - if (name.indexOf("property_") == 0) { - if (n == 0) - s.append("function _setDataState();\n"); - n++; - Object data = ((Object[]) dataValues.get(name))[1]; - s.append("DATA \"").append(name).append("\""); - if (data instanceof float[]) { - s.append("\n"); - float[] f = (float[]) data; - for (int i = 0; i < f.length; i++) - s.append(" ").append(f[i]); - s.append("\n"); - } else { - s.append("").append(data); - } - s.append("end \"").append(name).append("\";\n"); - } - } - if (n == 0) - return; - sfunc.append(" _setDataState\n"); - s.append("end function;\n\n"); - } - public String getAltLocListInModel(int modelIndex) { return modelManager.getAltLocListInModel(modelIndex); } @@ -5670,8 +5591,8 @@ modelManager.setAtomProperty(bs, tok, iValue, fValue); } - void setAtomCoord(BitSet bs, int tokType, Point3f xyz) { - modelManager.setAtomCoord(bs, tokType, xyz); + void setAtomCoord(BitSet bs, int tokType, Object xyzValues) { + modelManager.setAtomCoord(bs, tokType, xyzValues); } public void setAtomCoordRelative(int atomIndex, float x, float y, float z) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |