[4] | 1 | /********************************************************************************
|
---|
| 2 | * Volatile - Volume Visualization Software for SAGE
|
---|
| 3 | * Copyright (C) 2004 Electronic Visualization Laboratory,
|
---|
| 4 | * University of Illinois at Chicago
|
---|
| 5 | *
|
---|
| 6 | * All rights reserved.
|
---|
| 7 | *
|
---|
| 8 | * Redistribution and use in source and binary forms, with or without
|
---|
| 9 | * modification, are permitted provided that the following conditions are met:
|
---|
| 10 | *
|
---|
| 11 | * * Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * * Redistributions in binary form must reproduce the above
|
---|
| 14 | * copyright notice, this list of conditions and the following disclaimer
|
---|
| 15 | * in the documentation and/or other materials provided with the distribution.
|
---|
| 16 | * * Neither the name of the University of Illinois at Chicago nor
|
---|
| 17 | * the names of its contributors may be used to endorse or promote
|
---|
| 18 | * products derived from this software without specific prior written permission.
|
---|
| 19 | *
|
---|
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
---|
| 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
| 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
| 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
| 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 31 | *
|
---|
| 32 | * Direct questions, comments etc about Volatile to www.evl.uic.edu/cavern/forum
|
---|
| 33 | *********************************************************************************/
|
---|
| 34 |
|
---|
| 35 | //Class vVolume: Defines the 3D volume and the associated operations
|
---|
| 36 | //the inherited class should override the load/roam function
|
---|
| 37 | //This class only manages the data(doesnt do the rendering)
|
---|
| 38 | //Shalini Venkataraman, Luc Renambot
|
---|
| 39 | //Sep 7 2003
|
---|
| 40 | #include <math.h>
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 | #include <string.h>
|
---|
| 44 | #include "glUE.h"
|
---|
| 45 | #include "vVolume.h"
|
---|
| 46 | #include "VectorMath.h"
|
---|
| 47 | #include "global.h"
|
---|
| 48 |
|
---|
| 49 | //constructor
|
---|
| 50 | vVolume::vVolume(int x, int y, int z) {
|
---|
| 51 | fileY = y;
|
---|
| 52 | fileX = x;
|
---|
| 53 | fileZ = z;
|
---|
| 54 | dimY = dimX = dimZ = maxDim = 128;
|
---|
| 55 | offsetX = offsetY = offsetZ = 0;
|
---|
| 56 | spacingX = spacingY = spacingZ = 1.0f;
|
---|
| 57 | probeX = probeY = probeZ = 0;
|
---|
| 58 | curVolume = numVolumes = 0;
|
---|
| 59 | probeValue = 0;
|
---|
| 60 | isoValue = 128;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | //destructor
|
---|
| 64 | vVolume::~vVolume() {
|
---|
| 65 | for (int i=0;i<cache.size();i++) {
|
---|
| 66 | delete [] cache[i].voxelData;
|
---|
| 67 | //delete [] cache[i].gradData;
|
---|
| 68 | delete [] cache[i].hist;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | void vVolume::setLoadDims(int x, int y, int z) {
|
---|
| 73 | //now get the power of 2
|
---|
| 74 | dimY = makepow2_max(y);
|
---|
| 75 | dimX = makepow2_max(x);
|
---|
| 76 | dimZ = makepow2_max(z);
|
---|
| 77 | maxDim = dimY;
|
---|
| 78 | maxDim = MAX(dimY,dimX);
|
---|
| 79 | maxDim = MAX(maxDim,dimZ);
|
---|
| 80 | probeX = dimX/2;
|
---|
| 81 | probeY = dimY/2;
|
---|
| 82 | probeZ = dimZ/2;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | void vVolume::probe(int dx, int dy, int dz) {
|
---|
| 86 | //calculate new probe(cos the orig offset is unsigned..)
|
---|
| 87 | int newProbeX = probeX + dx;
|
---|
| 88 | int newProbeY = probeY + dy;
|
---|
| 89 | int newProbeZ = probeZ + dz;
|
---|
| 90 | if (newProbeX== dimX)
|
---|
| 91 | newProbeX = dimX-1;
|
---|
| 92 | if (newProbeY== dimY)
|
---|
| 93 | newProbeY = dimY-1;
|
---|
| 94 | if (newProbeZ== dimZ)
|
---|
| 95 | newProbeZ = dimZ-1;
|
---|
| 96 |
|
---|
| 97 | if (newProbeX < 0)
|
---|
| 98 | newProbeX = 0;
|
---|
| 99 | if (newProbeY < 0)
|
---|
| 100 | newProbeY = 0;
|
---|
| 101 | if (newProbeZ < 0)
|
---|
| 102 | newProbeZ = 0;
|
---|
| 103 | //copy to the offset
|
---|
| 104 | probeX = newProbeX;
|
---|
| 105 | probeY = newProbeY;
|
---|
| 106 | probeZ = newProbeZ;
|
---|
| 107 | probeValue = getValue(probeZ, probeY, probeX);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | int vVolume::hist2D(unsigned char *normHist)
|
---|
| 112 | {
|
---|
| 113 | float *hist = new float[H2D_DIM];
|
---|
| 114 | int i;
|
---|
| 115 | for(i=0; i<H2D_DIM; ++i){
|
---|
| 116 | hist[i] = 0;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | for(i=0; i<dimZ; ++i){
|
---|
| 120 | for(int j=0; j<dimY; ++j){
|
---|
| 121 | for(int k=0; k<dimX; ++k){
|
---|
| 122 | int vox = i*dimX*dimY + j*dimX + k;
|
---|
| 123 | hist[cache[curVolume].voxelData[vox] + cache[curVolume].gradData[vox]*256] += 1;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | float max = 0;
|
---|
| 129 | for(i = 0; i< H2D_DIM; ++i){
|
---|
| 130 | hist[i] = (float)log(hist[i]);
|
---|
| 131 | max = MAX(hist[i], max);
|
---|
| 132 | }
|
---|
| 133 | for(i=0; i< H2D_DIM; ++i){
|
---|
| 134 | normHist[i] = (unsigned char)(hist[i]/(float)max*255);
|
---|
| 135 | }
|
---|
| 136 | delete[] hist;
|
---|
| 137 | return 1;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | int vVolume::hist1D(unsigned char *normHist)
|
---|
| 141 | {
|
---|
| 142 | float *hist = new float[H2D_DIM];
|
---|
| 143 | int i;
|
---|
| 144 | for(i=0; i<H2D_DIM; ++i){
|
---|
| 145 | hist[i] = 0;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | for(i=0; i<dimZ; ++i){
|
---|
| 149 | for(int j=0; j<dimY; ++j){
|
---|
| 150 | for(int k=0; k<dimX; ++k){
|
---|
| 151 | int vox = i*dimX*dimY + j*dimX + k;
|
---|
| 152 | for (int row = 0;row<256;row++)
|
---|
| 153 | hist[cache[curVolume].voxelData[vox]+row*256] += 1;
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | float max = 0;
|
---|
| 159 | for(i = 0; i< H2D_DIM; ++i){
|
---|
| 160 | // hist[i] = (float)log(hist[i]);
|
---|
| 161 | max = MAX(hist[i], max);
|
---|
| 162 | }
|
---|
| 163 | for(i=0; i< H2D_DIM; ++i){
|
---|
| 164 | normHist[i] = (unsigned char)(hist[i]/(float)max*255);
|
---|
| 165 | }
|
---|
| 166 | delete[] hist;
|
---|
| 167 | return 1;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | void vVolume::saveVolume(const char* prefix) {
|
---|
| 171 | FILE* fp;
|
---|
| 172 | char filename[256];
|
---|
| 173 | sprintf(filename,"%s%dx%dx%d.raw",prefix,dimX,dimY,dimZ);
|
---|
| 174 | if( (fp = fopen(filename, "wb" )) != NULL ) {
|
---|
| 175 | int numwritten = fwrite(cache[curVolume].voxelData, sizeof(GLubyte), dimY*dimX*dimZ, fp );
|
---|
| 176 | printf( "vVolume::saveVolume: Wrote %d bytes to file %s\n", numwritten, filename);
|
---|
| 177 | fclose( fp );
|
---|
| 178 | }
|
---|
| 179 | else
|
---|
| 180 | printf( "vVolume::saveVolume: Problem opening the file\n" );
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void vVolume::saveGradient(const char* prefix) {
|
---|
| 184 | FILE* fp;
|
---|
| 185 | char filename[256];
|
---|
| 186 | sprintf(filename,"%s%dx%dx%d.raw",prefix,dimX,dimY,dimZ);
|
---|
| 187 | if( (fp = fopen(filename, "wb" )) != NULL ) {
|
---|
| 188 | int numwritten = fwrite(cache[curVolume].gradData, sizeof(unsigned char), dimY*dimX*dimZ, fp );
|
---|
| 189 | printf( "saveGradient: Wrote %d bytes to file %s\n", numwritten, filename);
|
---|
| 190 | fclose( fp );
|
---|
| 191 | }
|
---|
| 192 | else
|
---|
| 193 | printf( "saveGradient: Problem opening the file\n" );
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 |
|
---|