From: <ha...@us...> - 2007-04-18 17:38:37
|
Revision: 7444 http://svn.sourceforge.net/jmol/?rev=7444&view=rev Author: hansonr Date: 2007-04-18 10:38:26 -0700 (Wed, 18 Apr 2007) Log Message: ----------- Jvxl implementing color index Modified Paths: -------------- trunk/Jmol/src/org/openscience/jvxl/Jvxl.java trunk/Jmol/src/org/openscience/jvxl/data/MeshData.java trunk/Jmol/src/org/openscience/jvxl/readers/JvxlReader.java trunk/Jmol/src/org/openscience/jvxl/readers/Parameters.java trunk/Jmol/src/org/openscience/jvxl/readers/SurfaceGenerator.java trunk/Jmol/src/org/openscience/jvxl/readers/VoxelReader.java trunk/Jmol/src/org/openscience/jvxl/util/ColorEncoder.java Modified: trunk/Jmol/src/org/openscience/jvxl/Jvxl.java =================================================================== --- trunk/Jmol/src/org/openscience/jvxl/Jvxl.java 2007-04-18 17:35:25 UTC (rev 7443) +++ trunk/Jmol/src/org/openscience/jvxl/Jvxl.java 2007-04-18 17:38:26 UTC (rev 7444) @@ -257,7 +257,7 @@ // compose the surface - SurfaceGenerator sg = new SurfaceGenerator(new ColorEncoder()); + SurfaceGenerator sg = new SurfaceGenerator(new ColorEncoder(), null, null); // input file Modified: trunk/Jmol/src/org/openscience/jvxl/data/MeshData.java =================================================================== --- trunk/Jmol/src/org/openscience/jvxl/data/MeshData.java 2007-04-18 17:35:25 UTC (rev 7443) +++ trunk/Jmol/src/org/openscience/jvxl/data/MeshData.java 2007-04-18 17:38:26 UTC (rev 7444) @@ -116,6 +116,7 @@ import java.util.BitSet; import javax.vecmath.Point3f; + import org.openscience.jvxl.util.*; public class MeshData { @@ -123,7 +124,7 @@ public int polygonCount; public Point3f[] vertices; - public int[] vertexColors; + public short[] vertexColixes; public int vertexCount; public float[] vertexValues; public int[][] polygonIndexes; @@ -172,6 +173,10 @@ polygonIndexes[polygonCount++] = new int[] {vertexA, vertexB, vertexC, check}; } + public BitSet[] getSurfaceSet() { + return (surfaceSet == null ? getSurfaceSet(0) : surfaceSet); + } + public BitSet[] getSurfaceSet(int level) { if (level == 0) { surfaceSet = new BitSet[100]; @@ -258,6 +263,14 @@ private void mergeSets(int a, int b) { surfaceSet[a].or(surfaceSet[b]); surfaceSet[b] = null; - } + } + + public void invalidateSurfaceSet(int i) { + for (int j = surfaceSet[i].length(); --j >= 0;) + if (surfaceSet[i].get(j)) + vertexValues[j] = Float.NaN; + surfaceSet[i] = null; + } + } Modified: trunk/Jmol/src/org/openscience/jvxl/readers/JvxlReader.java =================================================================== --- trunk/Jmol/src/org/openscience/jvxl/readers/JvxlReader.java 2007-04-18 17:35:25 UTC (rev 7443) +++ trunk/Jmol/src/org/openscience/jvxl/readers/JvxlReader.java 2007-04-18 17:38:26 UTC (rev 7444) @@ -469,7 +469,7 @@ fractionPtr = 0; int vertexCount = meshData.vertexCount; - int[] colors = meshData.vertexColors; + short[] colixes = meshData.vertexColixes; fractionData = new StringBuffer(); strFractionTemp = (isJvxl ? jvxlColorDataRead : ""); if (isJvxl && strFractionTemp.length() == 0) { @@ -494,11 +494,20 @@ float colorRange = params.valueMappedToBlue - params.valueMappedToRed; float contourPlaneMinimumValue = Float.MAX_VALUE; float contourPlaneMaximumValue = -Float.MAX_VALUE; - if (colors == null || colors.length < vertexCount) - meshData.vertexColors = colors = new int[vertexCount]; + if (colixes == null || colixes.length < vertexCount) + meshData.vertexColixes = colixes = new short[vertexCount]; int n = (params.isContoured ? contourVertexCount : vertexCount); String data = jvxlColorDataRead; int cpt = 0; + short colixNeg = 0, colixPos = 0; + if (params.colorBySign) { + colixPos = ColorEncoder + .getColorIndex(params.isColorReversed ? params.colorNeg + : params.colorPos); + colixNeg = ColorEncoder + .getColorIndex(params.isColorReversed ? params.colorPos + : params.colorNeg); + } for (int i = 0; i < n; i++) { float fraction, value; if (jvxlDataIsPrecisionColor) { @@ -527,10 +536,10 @@ if (params.isContoured) { marchingSquares.setContourData(i, value); } else if (params.colorBySign) { - colors[i] = ((params.isColorReversed ? value > 0 : value <= 0) ? params.colorNeg - : params.colorPos); + colixes[i] = ((params.isColorReversed ? value > 0 : value <= 0) ? colixNeg + : colixPos); } else { - colors[i] = getColorFromPalette(value); + colixes[i] = getColorIndexFromPalette(value); } } if (params.mappedDataMin == Float.MAX_VALUE) { Modified: trunk/Jmol/src/org/openscience/jvxl/readers/Parameters.java =================================================================== --- trunk/Jmol/src/org/openscience/jvxl/readers/Parameters.java 2007-04-18 17:35:25 UTC (rev 7443) +++ trunk/Jmol/src/org/openscience/jvxl/readers/Parameters.java 2007-04-18 17:38:26 UTC (rev 7444) @@ -114,6 +114,7 @@ import javax.vecmath.Point4f; + class Parameters { float mappedDataMin; @@ -143,8 +144,6 @@ boolean colorBySets; int colorNeg; int colorPos; - int minColor; - int maxColor; int colorPhase; float resolution; @@ -182,9 +181,5 @@ valueMappedToRed = mappedDataMin; valueMappedToBlue = mappedDataMax; } - - minColor = (isColorReversed ? colorPos : colorNeg); - maxColor = (isColorReversed ? colorNeg : colorPos); - } } Modified: trunk/Jmol/src/org/openscience/jvxl/readers/SurfaceGenerator.java =================================================================== --- trunk/Jmol/src/org/openscience/jvxl/readers/SurfaceGenerator.java 2007-04-18 17:35:25 UTC (rev 7443) +++ trunk/Jmol/src/org/openscience/jvxl/readers/SurfaceGenerator.java 2007-04-18 17:38:26 UTC (rev 7444) @@ -124,20 +124,43 @@ public class SurfaceGenerator { - ColorEncoder colorEncoder; - Parameters params; - MeshData meshData = new MeshData(); - JvxlData jvxlData = new JvxlData(); - VolumeData volumeData; + private ColorEncoder colorEncoder; + private JvxlData jvxlData; + private MeshData meshData; + private Parameters params; + private VolumeData volumeData; - public SurfaceGenerator(ColorEncoder colorEncoder) { + public SurfaceGenerator(ColorEncoder colorEncoder, MeshData meshData, + JvxlData jvxlData) { params = new Parameters(); + this.colorEncoder = (colorEncoder == null ? new ColorEncoder() : colorEncoder); + this.meshData = (meshData == null ? new MeshData() : meshData); + this.jvxlData = (jvxlData == null ? new JvxlData() : jvxlData); initializeIsosurface(); params.colorPos = colorEncoder.getColorPositive(); params.colorNeg = colorEncoder.getColorNegative(); - this.colorEncoder = colorEncoder; } + public ColorEncoder getColorEncoder() { + return colorEncoder; + } + + public JvxlData getJvxlData() { + return jvxlData; + } + + public MeshData getMeshData() { + return meshData; + } + + public Parameters getParams() { + return params; + } + + public VolumeData getVolumeData() { + return volumeData; + } + private int state; private final static int STATE_INITIALIZED = 1; @@ -149,83 +172,91 @@ int colorPtr; VoxelReader voxelReader; - public void setProperty(String propertyName, Object value) { + /** + * setProperty is the main interface for surface generation. + * + * @param propertyName + * @param value + * @return True if handled; False if not + * + */ + public boolean setProperty(String propertyName, Object value) { if ("init" == propertyName) { initializeIsosurface(); - return; + return true; } if ("fileIndex" == propertyName) { params.fileIndex = ((Integer) value).intValue(); if (params.fileIndex < 1) params.fileIndex = 1; - return; + return true; } if ("blockData" == propertyName) { boolean TF = ((Boolean) value).booleanValue(); params.blockCubeData = TF; - return; + return true; } if ("title" == propertyName) { if (value == null) { params.title = null; - return; + return true; } else if (value instanceof String[]) { params.title = (String[]) value; } - return; + return true; } if ("cutoff" == propertyName) { params.cutoff = ((Float) value).floatValue(); params.isPositiveOnly = false; - return; + return true; } if ("cutoffPositive" == propertyName) { params.cutoff = ((Float) value).floatValue(); params.isPositiveOnly = true; - return; + return true; } /// color options if ("insideOut" == propertyName) { params.insideOut = true; - return; + return true; } if ("sign" == propertyName) { params.isCutoffAbsolute = true; params.colorBySign = true; colorPtr = 0; - return; + return true; } if ("red" == propertyName) { params.valueMappedToRed = ((Float) value).floatValue(); - return; + return true; } if ("blue" == propertyName) { params.valueMappedToBlue = ((Float) value).floatValue(); params.rangeDefined = true; - return; + return true; } if ("reverseColor" == propertyName) { params.isColorReversed = true; - return; + return true; } if ("setColorScheme" == propertyName) { String colorScheme = (String) value; colorEncoder.setColorScheme(colorScheme); - return; + return true; } if ("plane" == propertyName) { @@ -234,7 +265,7 @@ params.thePlane.z = 1; //{0 0 0 w} becomes {0 0 1 w} params.isContoured = true; ++state; - return; + return true; } if ("contour" == propertyName) { @@ -244,12 +275,12 @@ params.nContours = n; else params.thisContour = -n; - return; + return true; } if ("progressive" == propertyName) { params.isXLowToHigh = true; - return; + return true; } if ("phase" == propertyName) { @@ -262,21 +293,21 @@ Logger.warn(" invalid color phase: " + color); params.colorPhase = 1; } - return; + return true; } if ("readData" == propertyName) { if (++state != STATE_DATA_READ) - return; + return true; if ((voxelReader = setData(value)) == null) { Logger.error("Could not set the data"); - return; + return true; } if (params.colorBySign) params.isBicolorMap = true; if (!voxelReader.createIsosurface()) { Logger.error("Could not create isosurface"); - return; + return true; } if (jvxlData.jvxlDataIs2dContour) voxelReader.colorIsosurface(); @@ -287,18 +318,18 @@ voxelReader.jvxlUpdateInfo(); voxelReader.discardTempData(false); params.mappedDataMin = Float.MAX_VALUE; - return; + return true; } if ("mapColor" == propertyName) { if (++state != STATE_DATA_COLORED) - return; + return true; if (value instanceof String && ((String)value).equalsIgnoreCase("sets")) { meshData.getSurfaceSet(0); params.colorBySets = true; } else if ((voxelReader = setData(value)) == null) { Logger.error("Could not set the mapping data"); - return; + return true; } if (params.thePlane != null) { voxelReader.createIsosurface(); //for the plane @@ -309,8 +340,9 @@ voxelReader.colorIsosurface(); voxelReader.jvxlUpdateInfo(); voxelReader.discardTempData(true); - return; + return true; } + return false; } public Object getProperty(String property, int index) { @@ -326,10 +358,6 @@ return jvxlData.jvxlInfoLine; if (property == "jvxlSurfaceData") return JvxlReader.jvxlGetFile(jvxlData, params.title, "", false, 1, null, null); - if (property == "volumeData") - return volumeData; - if (property == "meshData") - return meshData; return null; } Modified: trunk/Jmol/src/org/openscience/jvxl/readers/VoxelReader.java =================================================================== --- trunk/Jmol/src/org/openscience/jvxl/readers/VoxelReader.java 2007-04-18 17:35:25 UTC (rev 7443) +++ trunk/Jmol/src/org/openscience/jvxl/readers/VoxelReader.java 2007-04-18 17:38:26 UTC (rev 7444) @@ -54,7 +54,7 @@ * */ - private ColorEncoder colorEncoder; + protected ColorEncoder colorEncoder; protected Parameters params; protected MeshData meshData; @@ -65,12 +65,12 @@ protected boolean isXLowToHigh = false; //can be overridden in some readers by --progressive VoxelReader(SurfaceGenerator sg) { - this.colorEncoder = sg.colorEncoder; - this.params = sg.params; + this.colorEncoder = sg.getColorEncoder(); + this.params = sg.getParams(); isXLowToHigh = params.isXLowToHigh; - this.meshData = sg.meshData; - this.jvxlData = sg.jvxlData; - setVolumeData(sg.volumeData); + this.meshData = sg.getMeshData(); + this.jvxlData = sg.getJvxlData(); + setVolumeData(sg.getVolumeData()); } final static float ANGSTROMS_PER_BOHR = 0.5291772f; @@ -397,14 +397,14 @@ if (params.colorPhase == 0) params.colorPhase = 1; int vertexCount = meshData.vertexCount; - int[] colors = meshData.vertexColors; + short[] colixes = meshData.vertexColixes; colorFractionBase = defaultColorFractionBase; colorFractionRange = defaultColorFractionRange; params.setMapRanges(this); float min = params.mappedDataMin; float max = params.mappedDataMax; - if (colors == null) - meshData.vertexColors = colors = new int[vertexCount]; + if (colixes == null) + meshData.vertexColixes = colixes = new short[vertexCount]; StringBuffer list = null, list1 = null; //colorBySign is true when colorByPhase is true, but not vice-versa //old: boolean saveColorData = !(params.colorByPhase && !params.isBicolorMap && !params.colorBySign); //sorry! @@ -417,8 +417,14 @@ char[] remainder = new char[1]; boolean writePrecisionColor = (jvxlDataIsPrecisionColor || params.isContoured); int lastVertex = (contourVertexCount > 0 ? contourVertexCount : vertexCount); + short minColorIndex = -1; + short maxColorIndex = 0; + if (params.isBicolorMap && !params.isContoured || params.colorBySign) { + minColorIndex = ColorEncoder.getColorIndex(params.isColorReversed ? params.colorPos : params.colorNeg); + maxColorIndex = ColorEncoder.getColorIndex(params.isColorReversed ? params.colorNeg : params.colorPos); + } for (int i = 0; i < vertexCount; i += incr) { - float value = getVertexColorValue(i); + float value = getVertexColorValue(i, minColorIndex, maxColorIndex); if (i < lastVertex) { char ch; if (writePrecisionColor) { @@ -444,7 +450,7 @@ jvxlData.mappedDataMax = params.mappedDataMax; } - private float getVertexColorValue(int vertexIndex) { + private float getVertexColorValue(int vertexIndex, short minColorIndex, short maxColorIndex) { float value, datum; /* but RETURNS the actual value, not the truncated one * right, so what we are doing here is setting a range within the @@ -463,11 +469,11 @@ datum = value = marchingSquares.getInterpolatedPixelValue(meshData.vertices[vertexIndex]); else datum = value = volumeData.lookupInterpolatedVoxelValue(meshData.vertices[vertexIndex]); - if (params.isBicolorMap && !params.isContoured || params.colorBySign) { + if (minColorIndex >= 0) { if (value <= 0) - meshData.vertexColors[vertexIndex] = params.minColor; + meshData.vertexColixes[vertexIndex] = minColorIndex; else if (value > 0) - meshData.vertexColors[vertexIndex] = params.maxColor; + meshData.vertexColixes[vertexIndex] = maxColorIndex; if (!params.isContoured) datum = (value > 0 ? 0.999f : -0.999f); } else { @@ -475,7 +481,7 @@ value = params.valueMappedToRed; if (value >= params.valueMappedToBlue) value = params.valueMappedToBlue; - meshData.vertexColors[vertexIndex] = getColorFromPalette(value); + meshData.vertexColixes[vertexIndex] = getColorIndexFromPalette(value); } return datum; } @@ -557,11 +563,11 @@ return max; } - protected int getColorFromPalette(float value) { + protected short getColorIndexFromPalette(float value) { if (params.isColorReversed) - return colorEncoder.getColorFromPalette(-value, -params.valueMappedToBlue, + return colorEncoder.getColorIndexFromPalette(-value, -params.valueMappedToBlue, -params.valueMappedToRed); - return colorEncoder.getColorFromPalette(value, params.valueMappedToRed, + return colorEncoder.getColorIndexFromPalette(value, params.valueMappedToRed, params.valueMappedToBlue); } Modified: trunk/Jmol/src/org/openscience/jvxl/util/ColorEncoder.java =================================================================== --- trunk/Jmol/src/org/openscience/jvxl/util/ColorEncoder.java 2007-04-18 17:35:25 UTC (rev 7443) +++ trunk/Jmol/src/org/openscience/jvxl/util/ColorEncoder.java 2007-04-18 17:38:26 UTC (rev 7444) @@ -23,6 +23,15 @@ */ package org.openscience.jvxl.util; +import java.util.Hashtable; + +/* + * + * just a simple class using crude color encoding + * + * + */ + public class ColorEncoder { public ColorEncoder() { } @@ -43,24 +52,61 @@ return BLUE; } - public int getColorFromPalette(float val, float lo, float hi) { + public short getColorIndexFromPalette(float val, float lo, float hi) { + int c = 0; switch (palette) { case ROYGB: - return argbsRoygbScale[quantize(val, lo, hi, argbsRoygbScale.length)]; + c = argbsRoygbScale[quantize(val, lo, hi, argbsRoygbScale.length)]; + break; case BGYOR: - return argbsRoygbScale[quantize(-val, -hi, -lo, argbsRoygbScale.length)]; + c = argbsRoygbScale[quantize(-val, -hi, -lo, argbsRoygbScale.length)]; + break; case LOW: - return argbsRoygbScale[quantize(val, lo, hi, ihalf)]; + c = argbsRoygbScale[quantize(val, lo, hi, ihalf)]; + break; case HIGH: - return argbsRoygbScale[ihalf + quantize(val, lo, hi, ihalf)]; + c = argbsRoygbScale[ihalf + quantize(val, lo, hi, ihalf)]; + break; case RWB: - return argbsRwbScale[quantize(val, lo, hi, argbsRwbScale.length)]; + c = argbsRwbScale[quantize(val, lo, hi, argbsRwbScale.length)]; + break; case BWR: - return argbsRwbScale[quantize(-val, -hi, -lo, argbsRwbScale.length)]; + c = argbsRwbScale[quantize(-val, -hi, -lo, argbsRwbScale.length)]; + break; + default: + c = GRAY; } - return GRAY; + return getColorIndex(c); } + static Hashtable htColorMap = new Hashtable(); + + public static short getColorIndex(int c) { + Integer ic = new Integer(c); + Integer cx = (Integer) htColorMap.get(ic); + if (cx == null) + cx = allocateColorIndex(ic); + return cx.shortValue(); + } + + public static int getColorFromIndex(short colix) { + if (colix < 0) + return GRAY; + Integer ic = (Integer) htColorMap.get(new Integer(colix)); + return (ic == null ? 0 : ic.intValue()); + } + + static int nColors; + + synchronized private static Integer allocateColorIndex(Integer ic) { + Integer cx = new Integer(nColors++); + htColorMap.put(ic, cx); + htColorMap.put(cx, ic); + //System.out.println("the color index of " + Integer.toHexString(ic.intValue()) + " is " + cx + " decoded as " + + // Integer.toHexString(getColorFromIndex(cx.shortValue()))); + return cx; + } + private static int quantize(float val, float lo, float hi, int segmentCount) { float range = hi - lo; if (range <= 0 || Float.isNaN(val)) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |