Revision: 5228
Author:   hansonr
Date:     2006-06-16 16:26:27 -0700 (Fri, 16 Jun 2006)
ViewCVS:  http://svn.sourceforge.net/jmol/?rev=5228&view=rev

Log Message:
-----------
bob200603 dipole work -- more files follow

see http://www.stolaf.edu/people/hansonr/jmol/test/proto/dipole.htm

Modified Paths:
--------------
    branches/bob200603/Jmol/src/org/jmol/viewer/Dipole.java
    branches/bob200603/Jmol/src/org/jmol/viewer/Dipoles.java
    branches/bob200603/Jmol/src/org/jmol/viewer/DipolesRenderer.java
    branches/bob200603/Jmol/src/org/jmol/viewer/Frame.java
    branches/bob200603/Jmol/src/org/jmol/viewer/JmolConstants.java
    branches/bob200603/Jmol/src/org/jmol/viewer/Shape.java
    branches/bob200603/Jmol/src/org/jmol/viewer/Token.java
Modified: branches/bob200603/Jmol/src/org/jmol/viewer/Dipole.java
===================================================================
--- branches/bob200603/Jmol/src/org/jmol/viewer/Dipole.java     2006-06-15 
02:44:06 UTC (rev 5227)
+++ branches/bob200603/Jmol/src/org/jmol/viewer/Dipole.java     2006-06-16 
23:26:27 UTC (rev 5228)
@@ -30,29 +30,49 @@
 import org.jmol.g3d.Graphics3D;
 
 class Dipole extends Shape {
-  String dipoleID;
-  String dipoleInfo;
+  String thisID = "";
+  short mad;
+  short colix = 0;
+  short type;
+
+  Point3f origin;
   Vector3f vector;
-  Point3f origin;
+
+  String dipoleInfo = "";
   float dipoleValue;
+  float offsetSide;
   float offsetAngstroms;
   int offsetPercent;
+  int visibilityFlags;
+  int modelIndex;
+
   boolean visible;
-  int modelIndex;
-  short mad;
-  short colix;
+  boolean noCross;
+
   Atom[] atoms = new Atom[2]; //for reference only
+  Bond bond;
 
-  Dipole() {  
+  final static short DIPOLE_TYPE_UNKNOWN = 0;
+  final static short DIPOLE_TYPE_POINTS = 1;
+  final static short DIPOLE_TYPE_ATOMS = 2;
+  final static short DIPOLE_TYPE_BOND = 3;
+  final static short DIPOLE_TYPE_MOLECULAR = 4;
+  final static short DIPOLE_TYPE_POINTVECTOR = 5;
+
+  Dipole() {
   }
-  
-  Dipole(Viewer viewer, String dipoleID, Graphics3D g3d, short colix, short 
mad, boolean visible) {
+
+  Dipole(Viewer viewer, String thisID, String dipoleInfo, Graphics3D g3d,
+      short colix, short mad, boolean visible) {
     this.viewer = viewer;
-    this.dipoleID = dipoleID;
+    this.modelIndex = viewer.getDisplayModelIndex();
+    this.thisID = thisID;
+    this.dipoleInfo = dipoleInfo;
     this.g3d = g3d;
     this.colix = colix;
     this.mad = mad;
     this.visible = visible;
+    this.type = DIPOLE_TYPE_UNKNOWN;
   }
 
   void initShape() {
@@ -64,15 +84,40 @@
   void setTranslucent(boolean isTranslucent) {
     colix = Graphics3D.setTranslucent(colix, isTranslucent);
   }
-  
-  void set(Point3f pt1, Point3f pt2) {
-    origin = new Point3f(pt2);
-    vector = new Vector3f(pt1);
+
+  void set(String thisID, String dipoleInfo, Atom[] atoms, float dipoleValue,
+           short mad, float offsetAngstroms, float offsetSide, Point3f origin, 
Vector3f vector) {
+    this.thisID = thisID;
+    this.dipoleInfo = dipoleInfo;
+    this.dipoleValue = dipoleValue;
+    this.mad = mad;
+    this.offsetAngstroms = offsetAngstroms;
+    this.offsetSide = offsetSide;
+    this.vector = new Vector3f(vector);
+    this.origin = new Point3f(origin);
+    if (atoms[0] != null) {
+      this.atoms[0] = atoms[0];
+      this.atoms[1] = atoms[1];
+      centerDipole();
+    }
+  }
+
+  private void set(Point3f pt1, Point3f pt2) {
+    if (dipoleValue < 0) { 
+      origin = new Point3f(pt2);
+      vector = new Vector3f(pt1);
+      dipoleValue = -dipoleValue;
+    } else {
+      origin = new Point3f(pt1);
+      vector = new Vector3f(pt2);
+    }
     dipoleInfo = "" + origin + vector;
     vector.sub(origin);
-    vector.scale(-dipoleValue / vector.length());
-    if (dipoleValue < 0)
-      origin.sub(vector);
+    if (dipoleValue == 0)
+      dipoleValue = vector.length();
+    else
+      vector.scale(dipoleValue / vector.length());
+    this.type = DIPOLE_TYPE_POINTS;
   }
 
   void set(float value) {
@@ -86,34 +131,39 @@
   }
 
   void set(Point3f pt1, Point3f pt2, float value) {
-    set(value);
+    dipoleValue = value;
+    atoms[0] = null;
     set(pt1, pt2);
   }
-  
+
   void set(Point3f pt1, Vector3f dipole) {
     set(dipole.length());
     Point3f pt2 = new Point3f(pt1);
     pt2.add(dipole);
     set(pt1, pt2);
+    type = DIPOLE_TYPE_POINTVECTOR;
   }
-  
-  void set(Point3f pt1, Vector3f dipole, int direction) {
-    Vector3f v = new Vector3f(dipole);
-    v.scale(direction);
-    Point3f pt2 = new Point3f(pt1);
-    pt2.add(v);
-    set(dipole.length());
-    set(pt1, pt2);
-  }
-  
+
   void set(Atom atom1, Atom atom2, float value) {
+    //also from frame
     set(value);
     set(atom1.point3f, atom2.point3f);
+    offsetSide = Dipoles.DEFAULT_OFFSETSIDE;
+    mad = Dipoles.DEFAULT_MAD;
+    atoms[0] = atom1;
+    atoms[1] = atom2;
+    centerDipole();
   }
+
+  void centerDipole() {
+    float f = atoms[0].point3f.distance(atoms[1].point3f) 
+                / (2 * dipoleValue) - 0.5f;
+    origin.scaleAdd(f, vector, atoms[0].point3f);
+    bond = atoms[0].getBond(atoms[1]);
+    type = (bond == null ? Dipole.DIPOLE_TYPE_ATOMS : Dipole.DIPOLE_TYPE_BOND);
+  }
   
-  void set(int atomIndex1, int atomIndex2, float value) {
-    set(value);
-    set(frame.atoms[atomIndex1].point3f, frame.atoms[atomIndex2].point3f);
+  boolean isBondType() {
+    return (type == Dipole.DIPOLE_TYPE_ATOMS || type == 
Dipole.DIPOLE_TYPE_BOND);
   }
-
 }

Modified: branches/bob200603/Jmol/src/org/jmol/viewer/Dipoles.java
===================================================================
--- branches/bob200603/Jmol/src/org/jmol/viewer/Dipoles.java    2006-06-15 
02:44:06 UTC (rev 5227)
+++ branches/bob200603/Jmol/src/org/jmol/viewer/Dipoles.java    2006-06-16 
23:26:27 UTC (rev 5228)
@@ -32,61 +32,94 @@
 import javax.vecmath.Point3f;
 import javax.vecmath.Vector3f;
 
-
 class Dipoles extends SelectionIndependentShape {
 
-  final static short DEFAULT_MAD = 3;
-  final static float DEFAULT_OFFSET = 0.4f;
-  int dipoleCount;
+  final static short DEFAULT_MAD = 5;
+  final static float DEFAULT_OFFSETSIDE = 0.40f;
+
+  float dipoleVectorScale = 1.0f;
+  int dipoleCount = 0;
   Dipole[] dipoles = new Dipole[4];
   Dipole currentDipole;
   Dipole tempDipole;
-  Point3f startCoord;
-  boolean iHaveID;
+  Point3f startCoord = new Point3f();
+  Point3f endCoord = new Point3f();
+  float dipoleValue;
   boolean isBond;
+  boolean iHaveTwoEnds;
+  int atomIndex1;
 
   void initShape() {
-    colix = Graphics3D.ORANGE;
   }
 
   void setProperty(String propertyName, Object value, BitSet bs) {
 
-    System.out.println(propertyName + " " + value);
+    //System.out.println("dipoles setProperty " + propertyName + " " + value);
+
     if ("init" == propertyName) {
       tempDipole = new Dipole();
       tempDipole.dipoleValue = 1;
       tempDipole.mad = DEFAULT_MAD;
-      tempDipole.offsetAngstroms = DEFAULT_OFFSET;
-      tempDipole.offsetPercent = 0;
-      iHaveID = false;
+      atomIndex1 = -1;
+      tempDipole.modelIndex = -1;
+      dipoleValue = 0;
       isBond = false;
+      iHaveTwoEnds = false;
+      if (currentDipole != null)
+        System.out.println("current dipole: " + currentDipole.thisID);
       return;
     }
 
-    if ("bonds" == propertyName) {
-      viewer.getBondDipoles();
-      currentDipole = null;
-      isBond = true;
-      return;
-    }
-    
-    if ("dipoleID" == propertyName) {
-      String dipoleID = (String) value;
-      if (dipoleID == null) {
+    if ("thisID" == propertyName) {
+      if (value == null) {
         currentDipole = null;
         return;
       }
-      tempDipole = currentDipole = findDipole(dipoleID);
-      iHaveID = true;
+      String thisID = "" + (String) value;
+      currentDipole = findDipole(thisID);
+      if (currentDipole == null)
+        currentDipole = allocDipole(thisID, "");
+      System.out.println("current dipole now " + currentDipole.thisID);
+      tempDipole = currentDipole;
+      if (thisID.equals("molecular")) {
+        Vector3f v = viewer.getModelDipole();
+        System.out.println("file molecular dipole = " + v);
+        if (v == null) {
+          System.out
+              .print("No molecular dipole found in file; setting to {0 0 0}");
+          v = new Vector3f();
+        }
+        tempDipole.set(new Point3f(0, 0, 0), new Vector3f(-v.x, -v.y, -v.z));
+        tempDipole.type = Dipole.DIPOLE_TYPE_MOLECULAR;
+        tempDipole.thisID = "molecular";
+        setDipole();
+      }
       return;
     }
 
+    if ("dipoleVectorScale" == propertyName) {
+      dipoleVectorScale = ((Float) value).floatValue();
+      return;
+    }
+
+    if ("bonds" == propertyName) {
+      isBond = true;
+      currentDipole = null;
+      for (int i = dipoleCount; --i >= 0;)
+        if (isBondDipole(i))
+          return;
+      // only once if any bond dipoles are defined
+      viewer.getBondDipoles();
+      return;
+    }
+
     if ("on" == propertyName) {
       if (currentDipole != null)
         currentDipole.visible = true;
       else {
         for (int i = dipoleCount; --i >= 0;)
-          dipoles[i].visible = true;
+          if (!isBond || isBondDipole(i))
+            dipoles[i].visible = true;
       }
       return;
     }
@@ -96,222 +129,323 @@
         currentDipole.visible = false;
       else {
         for (int i = dipoleCount; --i >= 0;)
-          dipoles[i].visible = false;
+          if (!isBond || isBondDipole(i))
+            dipoles[i].visible = false;
       }
       return;
     }
 
-    if ("color" == propertyName) {
-      if (value != null) {
-        colix = Graphics3D.getColix(value);
-        if (currentDipole != null) {
-          currentDipole.colix = colix;
-        } else {
-          for (int i = dipoleCount; --i >= 0;) {
-            dipoles[i].colix = colix;
-          }
-        }
-      }
+    if ("delete" == propertyName) {
+      if (currentDipole != null)
+        deleteDipole(currentDipole);
+      else
+        clear(false);
       return;
     }
 
-    if ("translucency" == propertyName) {
-      boolean isTranslucent = ("translucent" == value);
-      if (currentDipole != null)
-        currentDipole.setTranslucent(isTranslucent);
-      else {
+    if ("clear" == propertyName) {
+      currentDipole = null;
+      clear(false);
+    }
+
+    if ("dipoleWidth" == propertyName) {
+      short mad = tempDipole.mad = (short) (((Float) value).floatValue() * 
1000);
+      if (currentDipole == null)
         for (int i = dipoleCount; --i >= 0;)
-          dipoles[i].setTranslucent(isTranslucent);
-      }
+          dipoles[i].mad = mad;
       return;
     }
 
-    if ("delete" == propertyName) {
-      if (currentDipole != null) {
-        int iCurrent;
-        for (iCurrent = dipoleCount; dipoles[--iCurrent] != currentDipole;) {
-        }
-        for (int j = iCurrent + 1; j < dipoleCount; ++j)
-          dipoles[j - 1] = dipoles[j];
-        dipoles[--dipoleCount] = null;
-        currentDipole = null;
-      } else {
+    if ("dipoleOffset" == propertyName) {
+      tempDipole.offsetAngstroms = ((Float) value).floatValue();
+      if (currentDipole == null)
         for (int i = dipoleCount; --i >= 0;)
-          dipoles[i] = null;
-        dipoleCount = 0;
-      }
+          if (!isBond || isBondDipole(i))
+            dipoles[i].offsetAngstroms = tempDipole.offsetAngstroms;
       return;
     }
 
-    if ("startSet" == propertyName) {
-      startCoord = viewer.getAtomSetCenter((BitSet) value);
-      tempDipole.set(viewer.getAtomSetCenter(bs), startCoord);
+    if ("dipoleOffsetPercent" == propertyName) {
+      tempDipole.offsetPercent = ((Integer) value).intValue();
+      if (tempDipole.dipoleValue != 0)
+        tempDipole.offsetAngstroms = tempDipole.offsetPercent / 100f
+            * tempDipole.dipoleValue;
+      if (currentDipole == null)
+        for (int i = dipoleCount; --i >= 0;)
+          if (!isBond || isBondDipole(i))
+            dipoles[i].offsetAngstroms = tempDipole.offsetPercent / 100f
+                * dipoles[i].dipoleValue;
       return;
     }
 
-    if ("endSet" == propertyName) {
-      tempDipole.set(startCoord, viewer.getAtomSetCenter((BitSet) value));
+    if ("offsetSide" == propertyName) {
+      float offsetSide = ((Float) value).floatValue();
+      if (currentDipole != null)
+        currentDipole.offsetSide = offsetSide;
+      else
+        for (int i = dipoleCount; --i >= 0;)
+          if (!isBond || isBondDipole(i))
+            dipoles[i].offsetSide = offsetSide;
       return;
     }
 
-    if ("startCoord" == propertyName) {
-      startCoord = (Point3f) value;
-      tempDipole.set(viewer.getAtomSetCenter(bs), startCoord);
+    if ("cross" == propertyName) {
+      boolean isOFF = !((Boolean) value).booleanValue();
+      if (currentDipole != null)
+        currentDipole.noCross = isOFF;
+      else
+        for (int i = dipoleCount; --i >= 0;)
+          if (!isBond || isBondDipole(i))
+            dipoles[i].noCross = isOFF;
       return;
     }
 
-    if ("endCoord" == propertyName) {
-      tempDipole.set(startCoord, (Point3f) value);
+    if ("color" == propertyName) {
+      colix = Graphics3D.getColix(value);
+      if (isBond) {
+        setColixDipole(colix, (colix != Graphics3D.UNRECOGNIZED) ? null
+            : (String) value, JmolConstants.BOND_COVALENT_MASK, bs);
+      } else if (value != null) {
+        if (currentDipole != null)
+          currentDipole.colix = colix;
+        else
+          for (int i = dipoleCount; --i >= 0;)
+            dipoles[i].colix = colix;
+      }
       return;
     }
 
-    if ("molecular" == propertyName) {
-      tempDipole.set(new Point3f(0, 0, 0), (Vector3f) value, -1);
+    if ("translucency" == propertyName) {
+      boolean isTranslucent = ("translucent" == value);
+      if (currentDipole != null)
+        currentDipole.setTranslucent(isTranslucent);
+      else
+        for (int i = dipoleCount; --i >= 0;)
+          if (!isBond || isBondDipole(i))
+            dipoles[i].setTranslucent(isTranslucent);
       return;
     }
 
-    if ("dipoleValue" == propertyName) {
-      tempDipole.set(((Float) value).floatValue());
+    if ("startSet" == propertyName) {
+      BitSet atomset = (BitSet) value;
+      startCoord = viewer.getAtomSetCenter(atomset);
+      tempDipole.set(startCoord, new Point3f(0, 0, 0), dipoleValue);
+      if (viewer.cardinalityOf(atomset) == 1)
+        atomIndex1 = viewer.firstAtomOf(atomset);
       return;
     }
 
-    if ("dipoleOffset" == propertyName) {
-      tempDipole.offsetAngstroms = ((Float) value).floatValue();
-      if (currentDipole == null) 
-        for (int i = dipoleCount; --i >= 0;)
-          dipoles[i].offsetAngstroms = tempDipole.offsetAngstroms;
+    if ("endSet" == propertyName) {
+      iHaveTwoEnds = true;
+      BitSet atomset = (BitSet) value;
+      if (atomIndex1 >= 0 && viewer.cardinalityOf(atomset) == 1) {
+        tempDipole.set(frame.atoms[atomIndex1], frame.atoms[viewer
+            .firstAtomOf(atomset)], 1);
+        currentDipole = findDipole(tempDipole.thisID, tempDipole.dipoleInfo);
+        tempDipole.thisID = currentDipole.thisID;
+        if (isSameAtoms(currentDipole, tempDipole.dipoleInfo)) {
+          tempDipole = currentDipole;
+          if (dipoleValue > 0)
+            tempDipole.dipoleValue = dipoleValue;
+        }
+      } else {
+        tempDipole.set(startCoord, viewer.getAtomSetCenter(atomset),
+            dipoleValue);
+      }
+      //NOTTTTTT!!!! currentDipole = tempDipole;
       return;
     }
 
-    if ("dipoleOffsetPercent" == propertyName) {
-      tempDipole.offsetPercent = ((Integer) value).intValue();
-      if (currentDipole == null) 
-        for (int i = dipoleCount; --i >= 0;) {
-          dipoles[i].offsetAngstroms = tempDipole.offsetPercent / 100f * 
dipoles[i].dipoleValue;
-          System.out.println(i+" "+dipoles[i].offsetAngstroms+" "+ 
tempDipole.offsetPercent + " "+dipoles[i].dipoleValue);
-        }
+    if ("startCoord" == propertyName) {
+      startCoord.set((Point3f) value);
+      tempDipole.set(startCoord, new Point3f(0, 0, 0), dipoleValue);
       return;
     }
 
-    if ("dipoleWidth" == propertyName) {
-      tempDipole.mad = (short)(((Float) value).floatValue() * 1000);
-      if (currentDipole == null) 
-        for (int i = dipoleCount; --i >= 0;)
-          dipoles[i].offsetAngstroms = tempDipole.offsetPercent / 100f * 
dipoles[i].dipoleValue;
+    if ("endCoord" == propertyName) {
+      iHaveTwoEnds = true;
+      endCoord.set((Point3f) value);
+      tempDipole.set(startCoord, endCoord, dipoleValue);
+      dumpDipoles("endCoord");
       return;
     }
 
+    if ("dipoleValue" == propertyName) {
+      dipoleValue = ((Float) value).floatValue();
+      tempDipole.set(dipoleValue);
+      if (tempDipole.offsetPercent != 0)
+        tempDipole.offsetAngstroms = tempDipole.offsetPercent / 100f
+            * tempDipole.dipoleValue;
+      return;
+    }
+
     if ("set" == propertyName) {
-      if (isBond)
+      if (isBond || !iHaveTwoEnds)
         return;
-      if (!iHaveID) {
-        String dipoleID = "dipole" + dipoleCount;
-        currentDipole = findDipole(dipoleID, tempDipole.dipoleInfo);
-        if (tempDipole.offsetPercent != 0)
-          tempDipole.offsetAngstroms = tempDipole.offsetPercent / 100f * 
tempDipole.dipoleValue;
-        setDipole(currentDipole, tempDipole);
-      }
+      setDipole();
+      setModelIndex();
       return;
     }
   }
 
-  void setDipole (Dipole newDipole, Dipole tempDipole) {
-    setDipole(newDipole, tempDipole.atoms, tempDipole.dipoleInfo,
-        tempDipole.dipoleValue, tempDipole.mad, tempDipole.offsetAngstroms,
-        tempDipole.origin, tempDipole.vector);
+  private boolean isBondDipole(int i) {
+    if (i >= dipoles.length || dipoles[i] == null)
+      return false;
+    return (dipoles[i].isBondType());
   }
 
-  void setDipole(Dipole dipole, Atom[] atoms, String dipoleInfo,
-                 float dipoleValue, short mad, float offsetAngstroms,
-                 Point3f origin, Vector3f vector) {
-    if (atoms[0] != null) {
-      dipole.atoms[0] = atoms[0];
-      dipole.atoms[1] = atoms[1];
+  private void setColixDipole(short colix, String palette, short bondTypeMask,
+                              BitSet bs) {
+    if (colix != Graphics3D.UNRECOGNIZED) {
+      BondIterator iter = frame.getBondIterator(bondTypeMask, bs);
+      while (iter.hasNext()) {
+        Dipole d = findBondDipole(iter.next());
+        if (d != null)
+          d.colix = colix;
+      }
+    } else {
+      System.out.println("setColixDipole called with palette:" + palette);
     }
-    dipole.dipoleInfo = dipoleInfo;
-    dipole.dipoleValue = dipoleValue;
-    dipole.mad = mad;
-    dipole.offsetAngstroms = offsetAngstroms;
-    dipole.origin = new Point3f(origin);
-    dipole.vector = new Vector3f(vector);
   }
-  
-  int getDipoleIndex(String dipoleID) {
-    for (int i = dipoleCount; --i >= 0; ) {
-      if (dipoles[i] != null && dipoleID.equals(dipoles[i].dipoleID))
-        return i;
-    }
-    return -1; 
+
+  private void setDipole() {
+    if (currentDipole == null)
+      currentDipole = allocDipole("", "");
+    currentDipole.set(tempDipole.thisID, tempDipole.dipoleInfo,
+        tempDipole.atoms, tempDipole.dipoleValue, tempDipole.mad,
+        tempDipole.offsetAngstroms, tempDipole.offsetSide, tempDipole.origin,
+        tempDipole.vector);
   }
-  
-  int getDipoleIndex(String dipoleID, String dipoleInfo) {
-    for (int i = dipoleCount; --i >= 0; ) {
-      if (dipoles[i] != null && dipoleInfo.equals(dipoles[i].dipoleInfo))
-        return i;
-    }
-    return getDipoleIndex(dipoleID);
+
+  private int getDipoleIndex(String dipoleInfo, String thisID) {
+    if (dipoleInfo != null && dipoleInfo.length() > 0)
+      for (int i = dipoleCount; --i >= 0;)
+        if (isSameAtoms(dipoles[i], dipoleInfo))
+          return i;
+    return getIndexFromName(thisID);
   }
-  
-  int getDipoleIndex(int atomIndex1, int atomIndex2) {
+
+  private boolean isSameAtoms(Dipole dipole, String dipoleInfo) {
+    // order-independent search for two atoms:
+    // looking for (xyz)(x'y'z') in (xyz)(x'y'z')(xyz)(x'y'z')
+    return (dipole != null && dipole.isBondType() && (dipole.dipoleInfo + 
dipole.dipoleInfo)
+        .indexOf(dipoleInfo) >= 0);
+  }
+
+  private int getDipoleIndex(int atomIndex1, int atomIndex2) {
     for (int i = dipoleCount; --i >= 0;) {
-      if (dipoles[i] != null 
+      if (dipoles[i] != null
           && dipoles[i].atoms[0] != null
           && dipoles[i].atoms[1] != null
-          && (dipoles[i].atoms[0].atomIndex == atomIndex1 
-              && dipoles[i].atoms[1].atomIndex == atomIndex2 
-          || dipoles[i].atoms[1].atomIndex == atomIndex1 
-             && dipoles[i].atoms[0].atomIndex == atomIndex2))
+          && (dipoles[i].atoms[0].atomIndex == atomIndex1
+              && dipoles[i].atoms[1].atomIndex == atomIndex2 || 
dipoles[i].atoms[1].atomIndex == atomIndex1
+              && dipoles[i].atoms[0].atomIndex == atomIndex2))
         return i;
     }
     return -1;
   }
-  
-  Dipole findDipole(String dipoleID) {
-    int dipoleIndex = getDipoleIndex(dipoleID);
-    if (dipoleIndex >= 0) {
-      return dipoles[dipoleIndex];
+
+  private void deleteDipole(Dipole dipole) {
+    if (dipole == null)
+      return;
+    if (currentDipole == dipole)
+      currentDipole = null;
+    int i;
+    for (i = dipoleCount; dipoles[--i] != dipole;) {
     }
-    return allocDipole(dipoleID);
+    if (i < 0)
+      return;
+    for (int j = i + 1; j < dipoleCount; ++j)
+      dipoles[j - 1] = dipoles[j];
+    dipoles[--dipoleCount] = null;
   }
 
-  Dipole findDipole(int atomIndex1, int atomIndex2) {
-    int dipoleIndex = getDipoleIndex(atomIndex1, atomIndex2);
+  private Dipole findDipole(String thisID) {
+    int dipoleIndex = getIndexFromName(thisID);
     if (dipoleIndex >= 0) {
       return dipoles[dipoleIndex];
     }
-    return allocDipole("");
+    return null;
   }
 
-  Dipole findDipole(Atom atom1, Atom atom2) {
+  Dipole findDipole(Atom atom1, Atom atom2, boolean doAllocate) {
     int dipoleIndex = getDipoleIndex(atom1.atomIndex, atom2.atomIndex);
     if (dipoleIndex >= 0) {
       return dipoles[dipoleIndex];
     }
-    return allocDipole("");
+    return (doAllocate ? allocDipole("", "") : null);
   }
 
-  Dipole findDipole(String dipoleID, String dipoleInfo) {
-    int dipoleIndex = getDipoleIndex(dipoleID, dipoleInfo);
+  private Dipole findBondDipole(Bond bond) {
+    Dipole d = findDipole(bond.atom1, bond.atom2, false);
+    return (d == null || d.atoms[0] == null ? null : d);
+  }
+
+  private Dipole findDipole(String thisID, String dipoleInfo) {
+    // must be able to identify a dipole from its ID only SECONDARILY,
+    // as we want one dipole per bond. So we look for coord ID.
+    int dipoleIndex = getDipoleIndex(dipoleInfo, thisID);
     if (dipoleIndex >= 0) {
+      if (thisID.length() > 0)
+        dipoles[dipoleIndex].thisID = thisID;
       return dipoles[dipoleIndex];
     }
-    return allocDipole(dipoleID);
+    return allocDipole(thisID, dipoleInfo);
   }
 
-  Dipole allocDipole(String dipoleID) { 
-    dipoles = (Dipole[])Util.ensureLength(dipoles, dipoleCount + 1);
-    if (dipoleID.length() == 0)
-      dipoleID = "dipole" + (dipoleCount + 1);
-    return dipoles[dipoleCount++] = new Dipole(viewer, dipoleID, g3d, colix, 
DEFAULT_MAD, true);
+  private Dipole allocDipole(String thisID, String dipoleInfo) {
+    dipoles = (Dipole[]) Util.ensureLength(dipoles, dipoleCount + 1);
+    if (thisID == null || thisID.length() == 0)
+      thisID = "dipole" + (dipoleCount + 1);
+    Dipole d = dipoles[dipoleCount++] = new Dipole(viewer, thisID, dipoleInfo,
+        g3d, colix, DEFAULT_MAD, true);
+    return d;
   }
 
+  private void dumpDipoles(String msg) {
+    for (int i = dipoleCount; --i >= 0;) {
+      Dipole dipole = dipoles[i];
+      System.out.println("\n\n" + msg + " dump dipole " + i + " " + dipole
+          + " " + dipole.thisID + " " + dipole.dipoleInfo + " "
+          + dipole.visibilityFlags + " mad=" + dipole.mad + " vis="
+          + dipole.visible + "\n orig" + dipole.origin + " " + " vect"
+          + dipole.vector + " val=" + dipole.dipoleValue);
+    }
+    if (currentDipole != null)
+      System.out.println(" current = " + currentDipole + currentDipole.origin);
+    if (tempDipole != null)
+      System.out.println(" temp = " + tempDipole + " " + tempDipole.origin);
+  }
+
+  void clear(boolean clearBondDipolesOnly) {
+    if (clearBondDipolesOnly) {
+      for (int i = dipoleCount; --i >= 0;)
+        if (isBondDipole(i))
+          deleteDipole(dipoles[i]);
+      return;
+    }
+    for (int i = dipoleCount; --i >= 0;)
+      if (!isBond || isBondDipole(i))
+        deleteDipole(dipoles[i]);
+  }
+
+  int getIndexFromName(String thisID) {
+    if (thisID == null)
+      return -1;
+    for (int i = dipoleCount; --i >= 0;) {
+      if (dipoles[i] != null && thisID.equals(dipoles[i].thisID))
+        return i;
+    }
+    return -1;
+  }
+
   Vector getShapeDetail() {
-    Vector V=new Vector();
+    Vector V = new Vector();
     Hashtable atomInfo;
     for (int i = 0; i < dipoleCount; i++) {
       Hashtable info = new Hashtable();
       Dipole dipole = dipoles[i];
-      info.put("ID", dipole.dipoleID);
+      info.put("ID", dipole.thisID);
       info.put("vector", dipole.vector);
       info.put("origin", dipole.origin);
       if (dipole.atoms[0] != null) {
@@ -326,8 +460,33 @@
         info.put("magnitude", new Float(dipole.vector.length()));
       }
       V.add(info);
-    }   
+    }
     return V;
   }
+
+  void setModelIndex() {
+    if (currentDipole == null)
+      return;
+    currentDipole.visible = true;
+    currentDipole.modelIndex = viewer.getDisplayModelIndex();
+  }
+
+  void setVisibilityFlags(BitSet bs) {
+    /*
+     * set all fixed objects visible; others based on model being displayed
+     * 
+     */
+    for (int i = dipoleCount; --i >= 0;) {
+      Dipole dipole = dipoles[i];
+      dipole.visibilityFlags = ((dipole.modelIndex < 0 || bs
+          .get(dipole.modelIndex))
+          && dipole.mad != 0
+          && dipole.visible
+          && dipole.origin != null
+          && dipole.vector != null
+          && dipole.vector.length() != 0
+          && dipole.dipoleValue != 0 ? myVisibilityFlag : 0);
+    }
+    dumpDipoles("setVis");
+  }
 }
-

Modified: branches/bob200603/Jmol/src/org/jmol/viewer/DipolesRenderer.java
===================================================================
--- branches/bob200603/Jmol/src/org/jmol/viewer/DipolesRenderer.java    
2006-06-15 02:44:06 UTC (rev 5227)
+++ branches/bob200603/Jmol/src/org/jmol/viewer/DipolesRenderer.java    
2006-06-16 23:26:27 UTC (rev 5228)
@@ -26,41 +26,39 @@
 package org.jmol.viewer;
 
 import org.jmol.g3d.*;
+
 import javax.vecmath.*;
 
 class DipolesRenderer extends ShapeRenderer {
 
-  
+  float dipoleVectorScale;
 
-  float dipoleVectorScale;
   void render() {
     Dipoles dipoles = (Dipoles) shape;
-    dipoleVectorScale = viewer.getDipoleScale();
+    dipoleVectorScale = dipoles.dipoleVectorScale;
     for (int i = dipoles.dipoleCount; --i >= 0;) {
       Dipole dipole = dipoles.dipoles[i];
-      //System.out.println("DipolesRenderer" + dipoleVectorScale + " " + i + " 
"
-        //  + dipole.mad + dipole.visible + dipole.vector + " " + 
dipole.origin);
-      if (dipole.mad == 0 || !dipole.visible || dipole.origin == null
-          || dipole.vector == null || dipole.vector.length() == 0)
-        continue;
-      if (transform(dipole))
+      if (dipole.visibilityFlags != 0 && transform(dipole))
         renderDipoleVector(dipole);
     }
   }
 
+  final Vector3f offset = new Vector3f();
+  final Point3i[] screens = new Point3i[6];
+  final Point3f[] points = new Point3f[6];
+  {
+    for (int i = 0; i < 6; i++) {
+      screens[i] = new Point3i();
+      points[i] = new Point3f();
+    }
+  }
+  final static int cylinderBase = 0;
+  final static int cross = 1;
+  final static int crossEnd = 2;
+  final static int center = 3;
+  final static int arrowHeadBase = 4;
+  final static int arrowHeadTip = 5;
 
-  final Vector3f offset = new Vector3f();
-  final Point3f pointCylinderBase = new Point3f();
-  final Point3f pointCross = new Point3f();
-  final Point3f pointCrossEnd = new Point3f();
-  final Point3f pointArrowHeadBase = new Point3f();
-  final Point3f pointArrowHeadTip = new Point3f();
-  
-  final Point3i screenCylinderBase = new Point3i();
-  final Point3i screenCross = new Point3i();
-  final Point3i screenCrossEnd = new Point3i();
-  final Point3i screenArrowHeadBase = new Point3i();
-  final Point3i screenArrowHeadTip = new Point3i();
   int diameter;
   int headWidthPixels;
   int crossWidthPixels;
@@ -74,44 +72,71 @@
     Vector3f vector = dipole.vector;
     offset.set(vector);
     offset.scale(dipole.offsetAngstroms / dipole.dipoleValue);
-/*
-     System.out.println("render dipole " + " value=" + dipole.dipoleValue
-       + " vector=" + dipole.vector
-       + " offset=" + offset + " offsetAngstroms=" + dipole.offsetAngstroms);
- 
- */ 
-    pointCylinderBase.set(dipole.origin);
-    pointCylinderBase.add(offset);
-    pointCross.scaleAdd(dipoleVectorScale * crossOffset, vector,
-        pointCylinderBase);
-    pointCrossEnd.scaleAdd(dipoleVectorScale * (crossOffset + crossWidth),
-        vector, pointCylinderBase);
-    pointArrowHeadBase.scaleAdd(dipoleVectorScale * arrowHeadOffset, vector,
-        pointCylinderBase);
-    pointArrowHeadTip.scaleAdd(dipoleVectorScale, vector, pointCylinderBase);
+    if (dipoleVectorScale < 0)
+      offset.add(vector);
+    System.out.println("dipole render scale offset" + dipoleVectorScale + " 
val=" + dipole.dipoleValue+ 
+        offset + vector);
+    points[cylinderBase].set(dipole.origin);
+    points[cylinderBase].add(offset);
+    points[cross].scaleAdd(dipoleVectorScale * crossOffset, vector,
+        points[cylinderBase]);
+    points[crossEnd].scaleAdd(dipoleVectorScale * (crossOffset + crossWidth),
+        vector, points[cylinderBase]);
+    points[center]
+        .scaleAdd(dipoleVectorScale / 2, vector, points[cylinderBase]);
+    points[arrowHeadBase].scaleAdd(dipoleVectorScale * arrowHeadOffset, vector,
+        points[cylinderBase]);
+    points[arrowHeadTip].scaleAdd(dipoleVectorScale, vector,
+        points[cylinderBase]);
 
-    viewer.transformPoint(pointCylinderBase, screenCylinderBase);
-    viewer.transformPoint(pointCross, screenCross);
-    viewer.transformPoint(pointCrossEnd, screenCrossEnd);
-    viewer.transformPoint(pointArrowHeadBase, screenArrowHeadBase);
-    viewer.transformPoint(pointArrowHeadTip, screenArrowHeadTip);
+    if (dipole.atoms[0] != null) {
+      offset.set(points[center]);
+      offset.cross(offset, vector);
+      if (offset.length() == 0) {
+        offset.set(points[center].x + 0.2345f, points[center].y + 0.1234f,
+            points[center].z + 0.4321f);
+        offset.cross(offset, vector);
+      }
+      offset.scale(dipole.offsetSide / offset.length());
+      for (int i = 0; i < 6; i++)
+        points[i].add(offset);
+    }
+    for (int i = 0; i < 6; i++)
+      viewer.transformPoint(points[i], screens[i]);
     short mad = dipole.mad;
-    diameter = (mad <= 20) ? mad : viewer.scaleToScreen(screenCylinderBase.z,
-        mad);
+    diameter = viewer.scaleToScreen(screens[center].z, mad);
     headWidthPixels = (int) (diameter * arrowHeadWidthFactor);
     if (headWidthPixels < diameter + 10)
       headWidthPixels = diameter + 10;
     crossWidthPixels = headWidthPixels;
     return true;
   }
-  
+
   void renderDipoleVector(Dipole dipole) {
-    short colix = dipole.colix;
-    g3d.fillCylinder(colix, Graphics3D.ENDCAPS_OPEN, diameter,
-                 screenCylinderBase, screenArrowHeadBase);
-    g3d.fillCylinder(colix, Graphics3D.ENDCAPS_FLAT, crossWidthPixels,
-        screenCross, screenCrossEnd);
-    g3d.fillCone(colix, Graphics3D.ENDCAPS_NONE, headWidthPixels,
-                 screenArrowHeadBase, screenArrowHeadTip);
+    short colixA = (dipole.bond == null ? dipole.colix : Graphics3D
+        .inheritColix(dipole.colix, dipole.bond.colix));
+    short colixB = colixA;
+    if (dipole.atoms[0] != null) {
+      colixA = Graphics3D.inheritColix(colixA, dipole.atoms[0].colixAtom);
+      colixB = Graphics3D.inheritColix(colixB, dipole.atoms[1].colixAtom);
+    }
+    if (colixA == 0)
+      colixA = Graphics3D.ORANGE;
+    if (colixB == 0)
+      colixB = Graphics3D.ORANGE;
+    if (dipoleVectorScale < 0) {
+      short c = colixA;
+      colixA = colixB;
+      colixB = c;
+    }
+    g3d.fillCylinder(colixA, Graphics3D.ENDCAPS_OPEN, diameter,
+        screens[cylinderBase], screens[center]);
+    g3d.fillCylinder(colixB, Graphics3D.ENDCAPS_OPEN, diameter,
+        screens[center], screens[arrowHeadBase]);
+    if (!dipole.noCross)
+      g3d.fillCylinder(colixA, Graphics3D.ENDCAPS_FLAT, crossWidthPixels,
+          screens[cross], screens[crossEnd]);
+    g3d.fillCone(colixB, Graphics3D.ENDCAPS_NONE, headWidthPixels,
+        screens[arrowHeadBase], screens[arrowHeadTip]);
   }
 }

Modified: branches/bob200603/Jmol/src/org/jmol/viewer/Frame.java
===================================================================
--- branches/bob200603/Jmol/src/org/jmol/viewer/Frame.java      2006-06-15 
02:44:06 UTC (rev 5227)
+++ branches/bob200603/Jmol/src/org/jmol/viewer/Frame.java      2006-06-16 
23:26:27 UTC (rev 5228)
@@ -2532,6 +2532,7 @@
       return;
     loadShape(JmolConstants.SHAPE_DIPOLES);
     Dipoles dipoles = (Dipoles) shapes[JmolConstants.SHAPE_DIPOLES];
+    dipoles.clear(true);
     for (int i = bondCount; --i >= 0;) {
       if (!bonds[i].isCovalent())
         continue;
@@ -2540,7 +2541,7 @@
       float c1 = partialCharges[atom1.atomIndex];
       float c2 = partialCharges[atom2.atomIndex];
       if (c1 != c2) {
-        Dipole dipole = dipoles.findDipole(atom1, atom2);
+        Dipole dipole = dipoles.findDipole(atom1, atom2, true);
         float value = (c1 - c2) / 2f * atom1.point3f.distance(atom2.point3f)
             / E_ANG_PER_DEBYE;
         if (value < 0) {
@@ -2550,8 +2551,8 @@
           dipole.set(atom1, atom2, value);
           //System.out.println(atom1.getInfo() + atom2.getInfo() + value);
         }
+        dipole.type = Dipole.DIPOLE_TYPE_BOND;
       }
-
     }
   }
 

Modified: branches/bob200603/Jmol/src/org/jmol/viewer/JmolConstants.java
===================================================================
--- branches/bob200603/Jmol/src/org/jmol/viewer/JmolConstants.java      
2006-06-15 02:44:06 UTC (rev 5227)
+++ branches/bob200603/Jmol/src/org/jmol/viewer/JmolConstants.java      
2006-06-16 23:26:27 UTC (rev 5228)
@@ -39,7 +39,7 @@
   // for now, just update this by hand
   // perhaps use ant filter later ... but mth doesn't like it :-(
   public final static String copyright = "(C) 2006 Jmol Development";
-  public final static String version = "10.x.13b(branch bob200603)";
+  public final static String version = "10.x.14(branch bob200603)";
   public final static String cvsDate = "$Date$";
   public final static String date = cvsDate.substring(7, 23);
 
@@ -2186,12 +2186,13 @@
   public final static int SHAPE_HOVER      = 21;
   public final static int SHAPE_POLYHEDRA  = 22;
   public final static int SHAPE_PRUEBA     = 23;
-  public final static int SHAPE_MIN_MESH_COLLECTION = 24; ///////////////
-  public final static int SHAPE_PMESH      = 24;
-  public final static int SHAPE_SASURFACE  = 25;
-  public final static int SHAPE_ISOSURFACE = 26;
-  public final static int SHAPE_DRAW       = 27;
-  public final static int SHAPE_DIPOLES    = 28;
+  public final static int SHAPE_MIN_NAMED_OBJECT = 24;
+  public final static int SHAPE_DIPOLES    = 24;
+  public final static int SHAPE_MIN_MESH_COLLECTION = 25;
+  public final static int SHAPE_PMESH      = 25;
+  public final static int SHAPE_SASURFACE  = 26;
+  public final static int SHAPE_ISOSURFACE = 27;
+  public final static int SHAPE_DRAW       = 28;
   public final static int SHAPE_MAX        = 29;
   
   //handled in Balls.java: (It's ok that this is SHAPE_MAX)
@@ -2206,7 +2207,7 @@
     "Strands", "MeshRibbon", "Ribbons",
     "Rockets", "Stars",
     "Axes", "Bbcage", "Uccage", "Frank", "Echo", "Hover", "Polyhedra", 
-    "Prueba", "Pmesh", "Sasurface", "Isosurface", "Draw", "Dipoles"
+    "Prueba", "Dipoles", "Pmesh", "Sasurface", "Isosurface", "Draw"
   };
 
   ////////////////////////////////////////////////////////////////

Modified: branches/bob200603/Jmol/src/org/jmol/viewer/Shape.java
===================================================================
--- branches/bob200603/Jmol/src/org/jmol/viewer/Shape.java      2006-06-15 
02:44:06 UTC (rev 5227)
+++ branches/bob200603/Jmol/src/org/jmol/viewer/Shape.java      2006-06-16 
23:26:27 UTC (rev 5228)
@@ -84,4 +84,7 @@
   Vector getShapeDetail() {
     return null;
   }
+  
+  void setVisibilityFlags(BitSet bs) {
+  }
 }

Modified: branches/bob200603/Jmol/src/org/jmol/viewer/Token.java
===================================================================
--- branches/bob200603/Jmol/src/org/jmol/viewer/Token.java      2006-06-15 
02:44:06 UTC (rev 5227)
+++ branches/bob200603/Jmol/src/org/jmol/viewer/Token.java      2006-06-16 
23:26:27 UTC (rev 5228)
@@ -184,7 +184,7 @@
   final static int isosurface   = command | 94 | negnums | showparam | 
colorparam | embeddedExpression;
   final static int draw         = command | 95 | embeddedExpression;
   final static int getproperty  = command | 96;
-  final static int dipole       = command | 97 | embeddedExpression;
+  final static int dipole       = command | 97 | negnums | embeddedExpression;
   final static int conformation = command | 98;
   
 


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Jmol-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jmol-commits

Reply via email to