Ignore:
Timestamp:
Mar 6, 2012, 3:05:04 AM (13 years ago)
Author:
ldelgass
Message:

another vector -> std::vector

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/nanovis/VolumeRenderer.cpp

    r2798 r2804  
    1818#include <time.h>
    1919#include <sys/time.h>
    20 #include "nanovis.h"
     20
    2121#include <R2/R2string.h>
    2222#include <R2/R2FilePath.h>
     23
     24#include "nanovis.h"
     25
    2326#include "VolumeRenderer.h"
    2427#include "VolumeInterpolator.h"
     
    2932#define NUMDIGITS       6
    3033
    31 VolumeRenderer::VolumeRenderer():
     34VolumeRenderer::VolumeRenderer() :
    3235    slice_mode(false),
    3336    volume_mode(true)
     
    7780    int volume_id;
    7881    int slice_id;
    79     SortElement(float _z, int _v, int _s):
    80         z(_z), volume_id(_v), slice_id(_s){}
     82    SortElement(float _z, int _v, int _s) :
     83        z(_z), volume_id(_v), slice_id(_s)
     84    {}
    8185};
    8286
    8387int slice_sort(const void* a, const void* b){
    84   if((*((SortElement*)a)).z > (*((SortElement*)b)).z)
    85       return 1;
    86    else
    87       return -1;
    88 }
    89 
     88    if((*((SortElement*)a)).z > (*((SortElement*)b)).z)
     89        return 1;
     90    else
     91        return -1;
     92}
    9093
    9194void
     
    102105    }
    103106    // Determine the volumes that are to be rendered.
    104     vector<Volume *> volumes;
     107    std::vector<Volume *> volumes;
    105108    Tcl_HashEntry *hPtr;
    106109    Tcl_HashSearch iter;
     
    109112        Volume* volPtr;
    110113        volPtr = (Volume *)Tcl_GetHashValue(hPtr);
    111         if(!volPtr->visible()) {
     114        if (!volPtr->visible()) {
    112115            continue;                   // Skip this volume
    113116        }
     
    139142            n_slices <<= 1;
    140143        }
    141        
     144
    142145        //volume start location
    143146        Vector3 loc = volPtr->location();
    144147        Vector4 shift_4d(loc.x, loc.y, loc.z, 0);
    145        
     148
    146149        double x0 = 0;
    147150        double y0 = 0;
    148151        double z0 = 0;
    149        
     152
    150153        Mat4x4 model_view_no_trans, model_view_trans;
    151154        Mat4x4 model_view_no_trans_inverse, model_view_trans_inverse;
    152        
     155
    153156        double zNear, zFar;
    154        
     157
    155158        //initialize volume plane with world coordinates
    156159        Plane volume_planes[6];
     
    161164        volume_planes[4].set_coeffs( 0,  0,  1, -z0);
    162165        volume_planes[5].set_coeffs( 0,  0, -1,  z0+1);
    163        
     166
    164167        //get modelview matrix with no translation
    165168        glPushMatrix();
     
    167170                 volPtr->aspect_ratio_height,
    168171                 volPtr->aspect_ratio_depth);
    169        
     172
    170173        glEnable(GL_DEPTH_TEST);
    171        
     174
    172175        GLfloat mv_no_trans[16];
    173176        glGetFloatv(GL_MODELVIEW_MATRIX, mv_no_trans);
    174        
     177
    175178        model_view_no_trans = Mat4x4(mv_no_trans);
    176179        model_view_no_trans_inverse = model_view_no_trans.inverse();
    177        
     180
    178181        glPopMatrix();
    179        
     182
    180183        //get modelview matrix with translation
    181184        glPushMatrix();
     
    186189        GLfloat mv_trans[16];
    187190        glGetFloatv(GL_MODELVIEW_MATRIX, mv_trans);
    188        
     191
    189192        model_view_trans = Mat4x4(mv_trans);
    190193        model_view_trans_inverse = model_view_trans.inverse();
    191        
     194
    192195        //draw volume bounding box with translation (the correct location in
    193196        //space)
     
    200203        }
    201204        glPopMatrix();
    202        
     205
    203206        //draw labels
    204207        glPushMatrix();
     
    208211        }
    209212        glPopMatrix();
    210        
     213
    211214        //transform volume_planes to eye coordinates.
    212         for(size_t j = 0; j < 6; j++) {
     215        for (size_t j = 0; j < 6; j++) {
    213216            volume_planes[j].transform(model_view_no_trans);
    214217        }
    215218        get_near_far_z(mv_no_trans, zNear, zFar);
    216        
     219
    217220        //compute actual rendering slices
    218221        float z_step = fabs(zNear-zFar)/n_slices;               
    219222        size_t n_actual_slices;
    220        
     223
    221224        if (volPtr->data_enabled()) {
    222225            n_actual_slices = (int)(fabs(zNear-zFar)/z_step + 1);
     
    227230        }
    228231        actual_slices[i] = n_actual_slices;
    229        
     232
    230233        Vector4 vert1 = (Vector4(-10, -10, -0.5, 1));
    231234        Vector4 vert2 = (Vector4(-10, +10, -0.5, 1));
    232235        Vector4 vert3 = (Vector4(+10, +10, -0.5, 1));
    233236        Vector4 vert4 = (Vector4(+10, -10, -0.5, 1));
    234        
    235        
     237
    236238        // Render cutplanes first with depth test enabled.  They will mark the
    237239        // image with their depth values.  Then we render other volume slices.
     
    240242
    241243        ConvexPolygon static_poly;
    242         for(int j = 0; j < volPtr->get_cutplane_count(); j++) {
    243             if(!volPtr->cutplane_is_enabled(j)) {
     244        for (int j = 0; j < volPtr->get_cutplane_count(); j++) {
     245            if (!volPtr->cutplane_is_enabled(j)) {
    244246                continue;
    245247            }
     
    247249            int axis = volPtr->get_cutplane(j)->orient;
    248250           
    249             if(axis==3){
     251            if (axis == 3) {
    250252                vert1 = Vector4(-10, -10, offset, 1);
    251253                vert2 = Vector4(-10, +10, offset, 1);
     
    253255                vert4 = Vector4(+10, -10, offset, 1);
    254256                //continue;
    255             } else if(axis==1){
     257            } else if (axis == 1) {
    256258                vert1 = Vector4(offset, -10, -10, 1);
    257259                vert2 = Vector4(offset, +10, -10, 1);
     
    259261                vert4 = Vector4(offset, -10, +10, 1);
    260262                //continue;
    261             } else if(axis==2){
     263            } else if (axis == 2) {
    262264                vert1 = Vector4(-10, offset, -10, 1);
    263265                vert2 = Vector4(+10, offset, -10, 1);
     
    266268                //continue;
    267269            }
    268            
     270
    269271            vert1 = model_view_no_trans.transform(vert1);
    270272            vert2 = model_view_no_trans.transform(vert2);
    271273            vert3 = model_view_no_trans.transform(vert3);
    272274            vert4 = model_view_no_trans.transform(vert4);
    273            
     275
    274276            ConvexPolygon* p = &static_poly;
    275277            p->vertices.clear();
    276            
     278
    277279            p->append_vertex(vert1);
    278280            p->append_vertex(vert2);
    279281            p->append_vertex(vert3);
    280282            p->append_vertex(vert4);
    281            
    282             for(size_t k = 0; k < 6; k++){
     283
     284            for (size_t k = 0; k < 6; k++) {
    283285                p->clip(volume_planes[k], true);
    284286            }
    285            
     287
    286288            p->transform(model_view_no_trans_inverse);
    287289            p->transform(model_view_trans);
    288            
     290
    289291            glPushMatrix();
    290292            glScalef(volPtr->aspect_ratio_width, volPtr->aspect_ratio_height,
    291293                     volPtr->aspect_ratio_depth);
    292            
     294
    293295            activate_volume_shader(volPtr, true);
    294296            glPopMatrix();
    295            
     297
    296298            glEnable(GL_DEPTH_TEST);
    297299            glDisable(GL_BLEND);
    298            
     300
    299301            glBegin(GL_POLYGON);
    300302            p->Emit(true);
    301303            glEnd();
    302304            glDisable(GL_DEPTH_TEST);
    303            
     305
    304306            deactivate_volume_shader();
    305307        } //done cutplanes
    306308       
    307        
    308309        //Now do volume rendering
    309        
     310
    310311        vert1 = (Vector4(-10, -10, -0.5, 1));
    311312        vert2 = (Vector4(-10, +10, -0.5, 1));
    312313        vert3 = (Vector4(+10, +10, -0.5, 1));
    313314        vert4 = (Vector4(+10, -10, -0.5, 1));
    314        
     315
    315316        size_t counter = 0;
    316        
     317
    317318        //transform slices and store them
    318319        float slice_z;
    319320        for (size_t j = 0; j < n_actual_slices; j++) {
    320321            slice_z = zFar + j * z_step;        //back to front
    321            
     322
    322323            ConvexPolygon *poly = new ConvexPolygon();
    323324            polys[i][counter] = poly;
    324325            counter++;
    325            
     326
    326327            poly->vertices.clear();
    327328            poly->set_id(i);
    328            
     329
    329330            //Setting Z-coordinate
    330331            vert1.z = slice_z;
     
    332333            vert3.z = slice_z;
    333334            vert4.z = slice_z;
    334            
     335
    335336            poly->append_vertex(vert1);
    336337            poly->append_vertex(vert2);
    337338            poly->append_vertex(vert3);
    338339            poly->append_vertex(vert4);
    339            
    340             for(size_t k = 0; k < 6; k++) {
     340
     341            for (size_t k = 0; k < 6; k++) {
    341342                poly->clip(volume_planes[k], true);
    342343            }
    343            
     344
    344345            poly->transform(model_view_no_trans_inverse);
    345346            poly->transform(model_view_trans);
    346            
    347             if(poly->vertices.size()>=3)
    348                 total_rendered_slices++; 
     347
     348            if (poly->vertices.size() >= 3)
     349                total_rendered_slices++;
    349350        }
    350        
    351351    } //iterate all volumes
    352352    TRACE("end loop\n");
    353    
     353
    354354    // We sort all the polygons according to their eye-space depth, from
    355355    // farthest to the closest.  This step is critical for correct blending
    356    
     356
    357357    SortElement* slices = (SortElement*)
    358358        malloc(sizeof(SortElement) * total_rendered_slices);
    359    
     359
    360360    size_t counter = 0;
    361     for (size_t i = 0; i < volumes.size(); i++){
    362         for (size_t j = 0; j < actual_slices[i]; j++){
    363             if(polys[i][j]->vertices.size() >= 3){
     361    for (size_t i = 0; i < volumes.size(); i++) {
     362        for (size_t j = 0; j < actual_slices[i]; j++) {
     363            if (polys[i][j]->vertices.size() >= 3) {
    364364                slices[counter] = SortElement(polys[i][j]->vertices[0].z, i, j);
    365365                counter++;
     
    367367        }
    368368    }
    369    
     369
    370370    //sort them
    371371    qsort(slices, total_rendered_slices, sizeof(SortElement), slice_sort);
    372    
     372
    373373    //Now we are ready to render all the slices from back to front
    374374    glEnable(GL_DEPTH_TEST);
    375375    glEnable(GL_BLEND);
    376    
    377     for(size_t i = 0; i < total_rendered_slices; i++){
     376
     377    for (size_t i = 0; i < total_rendered_slices; i++) {
    378378        Volume* volPtr;
    379379
     
    381381        int slice_index = slices[i].slice_id;
    382382        ConvexPolygon* cur = polys[volume_index][slice_index];
    383        
     383
    384384        volPtr = volumes[volume_index];
    385385        glPushMatrix();
    386386        glScalef(volPtr->aspect_ratio_width, volPtr->aspect_ratio_height,
    387387                 volPtr->aspect_ratio_depth);
    388        
     388
    389389#ifdef notdef
    390390        TRACE("shading slice: volume %s addr=%x slice=%d, volume=%d\n",
     
    393393        activate_volume_shader(volPtr, false);
    394394        glPopMatrix();
    395        
     395
    396396        glBegin(GL_POLYGON);
    397         cur->Emit(true); 
     397        cur->Emit(true);
    398398        glEnd();
    399399
    400400        deactivate_volume_shader();
    401401    }
    402    
     402
    403403    glDisable(GL_DEPTH_TEST);
    404404    glDisable(GL_BLEND);
    405    
     405
    406406    //Deallocate all the memory used
    407     for(size_t i = 0; i < volumes.size(); i++){
    408         for(size_t j=0; j <actual_slices[i]; j++){
     407    for (size_t i = 0; i < volumes.size(); i++) {
     408        for (size_t j = 0; j <actual_slices[i]; j++) {
    409409            delete polys[i][j];
    410410        }
     
    431431    glColor4d(r, g, b, 1.0);
    432432    glLineWidth(line_width);
    433    
     433
    434434    glBegin(GL_LINE_LOOP);
    435435    {
     
    440440    }
    441441    glEnd();
    442    
     442
    443443    glBegin(GL_LINE_LOOP);
    444444    {
     
    449449    }
    450450    glEnd();
    451    
    452451   
    453452    glBegin(GL_LINE_LOOP);
     
    459458    }
    460459    glEnd();
    461    
     460
    462461    glBegin(GL_LINE_LOOP);
    463462    {
     
    538537}
    539538
    540 
    541 
    542539void
    543540VolumeRenderer::activate_volume_shader(Volume* volPtr, bool slice_mode)
     
    552549    }
    553550}
    554  
    555551
    556552void VolumeRenderer::deactivate_volume_shader()
     
    561557}
    562558
    563 
    564559void VolumeRenderer::get_near_far_z(Mat4x4 mv, double &zNear, double &zFar)
    565560{
    566 
    567561  double x0 = 0;
    568562  double y0 = 0;
     
    587581  vertex[7][0]=x1; vertex[7][1]=y1; vertex[7][2]=z1; vertex[7][3]=1.0;
    588582
    589   for(int i=0;i<8;i++) {
     583  for (int i = 0; i < 8; i++) {
    590584    Vector4 tmp = mv.transform(Vector4(vertex[i][0], vertex[i][1], vertex[i][2], vertex[i][3]));
    591585    tmp.perspective_devide();
     
    603597void VolumeRenderer::switch_slice_mode() { slice_mode = (!slice_mode); }
    604598void VolumeRenderer::switch_volume_mode() { volume_mode = (!volume_mode); }
    605 
    606599
    607600bool
     
    624617        return false;
    625618    }
    626    
     619
    627620    if (fread(&bfType, sizeof(short int), 1, f) != 1) {
    628621        ERROR("can't read %lu bytes from font file \"%s\"\n",
     
    630623        goto error;
    631624    }
    632    
     625
    633626    /* check if file is a bitmap */
    634627    if (bfType != 19778) {
     
    636629        goto error;
    637630    }
    638    
     631
    639632    /* get the file size */
    640633    /* skip file size and reserved fields of bitmap file header */
    641634    fseek(f, 8, SEEK_CUR);
    642    
     635
    643636    /* get the position of the actual bitmap data */
    644637    if (fread(&bfOffBits, sizeof(int), 1, f) != 1) {
     
    647640    }
    648641    //TRACE("Data at Offset: %ld\n", bfOffBits);
    649    
     642
    650643    /* skip size of bitmap info header */
    651644    fseek(f, 4, SEEK_CUR);
    652    
     645
    653646    /* get the width of the bitmap */
    654647    if (fread(&width, sizeof(int), 1, f) != 1) {
     
    657650    }
    658651    //TRACE("Width of Bitmap: %d\n", texture->width);
    659    
     652
    660653    /* get the height of the bitmap */
    661654    if (fread(&height, sizeof(int), 1, f) != 1) {
     
    664657    }
    665658    //TRACE("Height of Bitmap: %d\n", texture->height);
    666    
     659
    667660    /* get the number of planes (must be set to 1) */
    668661    if (fread(&biPlanes, sizeof(short int), 1, f) != 1) {
     
    674667        goto error;
    675668    }
    676    
     669
    677670    /* get the number of bits per pixel */
    678671    if (fread(&biBitCount, sizeof(short int), 1, f) != 1) {
     
    680673        goto error;
    681674    }
    682    
     675
    683676    //TRACE("Bits per Pixel: %d\n", biBitCount);
    684677    if (biBitCount != 24) {
     
    686679        goto error;
    687680    }
    688 
    689681
    690682    /* calculate the size of the image in bytes */
     
    715707    data_with_alpha = (unsigned char*)
    716708        malloc(width*height*4*sizeof(unsigned char));
    717     for(int i=0; i<height; i++){
    718         for(int j=0; j<width; j++){
     709    for (int i = 0; i < height; i++) {
     710        for (int j = 0; j < width; j++) {
    719711            unsigned char r, g, b, a;
    720712            r = data[3*(i*width+j)];
    721713            g = data[3*(i*width+j)+1];
    722714            b = data[3*(i*width+j)+2];
    723            
    724             if(r==0 && g==0 && b==0)
     715
     716            if (r==0 && g==0 && b==0)
    725717                a = 0;
    726718            else
    727719                a = 255;
    728            
     720
    729721            data_with_alpha[4*(i*width+j)] = r;
    730722            data_with_alpha[4*(i*width+j) + 1] = g;
    731723            data_with_alpha[4*(i*width+j) + 2] = b;
    732724            data_with_alpha[4*(i*width+j) + 3] = a;
    733            
    734725        }
    735726    }
    736727    free(data);
    737    
     728
    738729    //create opengl texture
    739730    glGenTextures(1, &font_texture);
     
    755746}
    756747
    757 
    758 
    759748void
    760749VolumeRenderer::draw_label(Volume* vol)
    761750{
    762 
    763751    //glEnable(GL_TEXTURE_2D);
    764752    glDisable(GL_TEXTURE_2D);
    765753    glEnable(GL_DEPTH_TEST);
    766    
     754
    767755    //x
    768756    glColor3f(0.5, 0.5, 0.5);
    769    
     757
    770758    int length = vol->label[0].size();
    771759    glPushMatrix();
    772    
     760
    773761    glTranslatef(.5*vol->aspect_ratio_width, vol->aspect_ratio_height,
    774762                 -0.1*vol->aspect_ratio_depth);
    775763    glRotatef(180, 0, 0, 1);
    776764    glRotatef(90, 1, 0, 0);
    777    
     765
    778766    glScalef(0.0008, 0.0008, 0.0008);
    779767    for(int i=0; i<length; i++){
     
    782770    }
    783771    glPopMatrix();
    784    
     772
    785773    //y
    786774    length = vol->label[1].size();
     
    796784    }
    797785    glPopMatrix();
    798    
    799    
     786
    800787    //z
    801788    length = vol->label[2].size();
     
    803790    glTranslatef(0., 1.*vol->aspect_ratio_height, 0.5*vol->aspect_ratio_depth);
    804791    glRotatef(90, 0, 1, 0);
    805    
     792
    806793    glScalef(0.0008, 0.0008, 0.0008);
    807794    for(int i=0; i<length; i++){
     
    810797    }
    811798    glPopMatrix();
    812    
     799
    813800    glDisable(GL_TEXTURE_2D);
    814801}
    815  
    816  
    817802
    818803void
     
    841826}
    842827
    843 void 
     828void
    844829VolumeRenderer::glPrint(char* string, int set)
    845830{
    846     if(set>1) {
     831    if (set > 1) {
    847832        set=1;
    848833    }
     
    851836    glCallLists(strlen(string), GL_BYTE, string);
    852837}
    853 
Note: See TracChangeset for help on using the changeset viewer.