avtOpenGLMoleculeRenderer.C 51.7 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
bonnell's avatar
bonnell committed
3
* Copyright (c) 2000 - 2016, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6 7
* All rights reserved.
*
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
hrchilds's avatar
hrchilds committed
9 10 11 12 13 14 15 16 17 18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
19 20 21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
hrchilds's avatar
hrchilds committed
22 23 24 25 26
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
27 28 29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
hrchilds's avatar
hrchilds committed
30 31 32 33 34 35 36 37 38
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

hrchilds's avatar
hrchilds committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// ************************************************************************* //
//                          avtOpenGLMoleculeRenderer.C                      //
// ************************************************************************* //

#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkDataArray.h>
#include <vtkDataSet.h>
#include <vtkLookupTable.h>
#include <vtkMath.h>
#include <vtkMatrix4x4.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkRectilinearGrid.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkUnsignedIntArray.h>
57 58 59

#include "avtOpenGLMoleculeRenderer.h"

bonnell's avatar
bonnell committed
60
#include <avtOpenGLExtensionManager.h>
61 62 63
#include <avtCallback.h>
#include <avtColorTables.h>
#include <avtViewInfo.h>
hrchilds's avatar
hrchilds committed
64
#include <ImproperUseException.h>
65 66 67 68
#include <InvalidColortableException.h>
#include <LightList.h>
#include <MoleculeAttributes.h>

hrchilds's avatar
hrchilds committed
69 70 71 72
#include "AtomicProperties.h"
#include <LineAttributes.h>

#include <avtLookupTable.h>
73 74
#include "avtOpenGLAtomTexturer2D.h"
#include "avtOpenGLAtomTexturer3D.h"
hrchilds's avatar
hrchilds committed
75 76 77 78

#include <DebugStream.h>
#include "matrix.c"

79 80 81 82 83 84
#include <string>
#include <vector>

using std::string;
using std::vector;

hrchilds's avatar
hrchilds committed
85 86
#define SHORTEN_BONDS

hrchilds's avatar
hrchilds committed
87 88 89 90
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

hrchilds's avatar
hrchilds committed
91 92 93 94 95 96 97
static int sphere_quality_levels[4][2] = {
    {6,3},
    {12,6},
    {24,12},
    {48,24}
};

98 99 100 101 102 103 104
static int circle_quality_levels[4] = {
    6,
    10,
    18,
    30
};

hrchilds's avatar
hrchilds committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
static int cylinder_quality_levels[4] = {
    3,
    5,
    9,
    17
};
 

// ****************************************************************************
//  Constructor: avtOpenGLMoleculeRenderer::avtOpenGLMoleculeRenderer
//
//  Purpose:
//    Initialize the texture memory and the OpenGL texture ID.
//
//  Programmer:  Jeremy Meredith
//  Creation:    February  3, 2006
//
//  Modifications:
//    Brad Whitlock, Mon Mar 27 14:51:24 PST 2006
//    Added tex.
//
126 127 128
//    Jeremy Meredith, Thu Apr 22 14:11:20 EDT 2010
//    Added 2D mode.
//
hrchilds's avatar
hrchilds committed
129 130 131 132 133 134 135 136 137 138 139 140 141
// ****************************************************************************
avtOpenGLMoleculeRenderer::avtOpenGLMoleculeRenderer()
{
    immediatemode = true;
    displaylistid = 0;

    colortablename="";
    numcolors=0;
    colors=NULL;
    ct_is_discrete=false;
    levelsLUT = 0;

    spheres_calculated = false;
142
    circles_calculated = false;
hrchilds's avatar
hrchilds committed
143 144
    cylinders_calculated = false;

145 146
    tex2D = (void *)new avtOpenGLAtomTexturer2D;
    tex3D = (void *)new avtOpenGLAtomTexturer3D;
hrchilds's avatar
hrchilds committed
147 148 149 150 151 152 153 154 155 156 157 158 159
}


// ****************************************************************************
//  Destructor: avtOpenGLMoleculeRenderer::~avtOpenGLMoleculeRenderer
//
//  Programmer:  Jeremy Meredith
//  Creation:    February  3, 2006
//
//  Modifications:
//    Brad Whitlock, Mon Mar 27 14:51:24 PST 2006
//    Added tex.
//
160 161 162
//    Jeremy Meredith, Thu Apr 22 14:11:20 EDT 2010
//    Added 2D mode.
//
hrchilds's avatar
hrchilds committed
163 164 165 166 167 168 169 170 171 172 173 174 175
// ****************************************************************************
avtOpenGLMoleculeRenderer::~avtOpenGLMoleculeRenderer()
{
    if (colors)
        delete[] colors;
    colors = NULL;

    if (displaylistid != 0)
    {
        glDeleteLists(displaylistid, 1);
        displaylistid = 0;
    }

176 177
    delete ((avtOpenGLAtomTexturer2D *)tex2D);
    delete ((avtOpenGLAtomTexturer3D *)tex3D);
hrchilds's avatar
hrchilds committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
}



// ****************************************************************************
//  Method:  avtOpenGLMoleculeRenderer::InvalidateColors
//
//  Purpose:
//    
//
//  Arguments:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    March 23, 2006
//
// ****************************************************************************
void
avtOpenGLMoleculeRenderer::InvalidateColors()
{
    numcolors = 0;
    if (colors)
        delete[] colors;
    colors = NULL;
    colortablename = "";

    if (displaylistid != 0)
    {
        glDeleteLists(displaylistid, 1);
        displaylistid = 0;
    }
}

// ****************************************************************************
// Method: avtOpenGLMoleculeRenderer::SetLevelsLUT
//
// Purpose: 
//   Sets a lookup table to be used for resseq coloring.
//
// Programmer: Brad Whitlock
// Creation:   Wed Mar 29 11:58:51 PDT 2006
//
// Modifications:
//   
// ****************************************************************************

void
avtOpenGLMoleculeRenderer::SetLevelsLUT(avtLookupTable *lut)
{
    levelsLUT = lut;
}

// ****************************************************************************
//  Method:  SetColor3ubv
//
//  Purpose:
//    Okay, so it's a little silly right now, but there was some
//    debate about using glMaterialColor(...), so having things call
//    this instead of the GL call directly made trying it easier.
//    It's staying because I might still change my mind.
//
//  Arguments:
//    c          the pointer to the uchar color
//
//  Programmer:  Jeremy Meredith
//  Creation:    March 23, 2006
//
// ****************************************************************************
static inline void
SetColor3ubv(const unsigned char *c)
{
    glColor3ubv(c);
}

// ****************************************************************************
//  Method: avtOpenGLMoleculeRenderer::DrawAtomsAsSpheres
//
//  Purpose:
//    Draw the atoms as spheres.
//
//  Programmer:  Jeremy Meredith
//  Creation:    February  3, 2006
//
//  Modifications:
//    Brad Whitlock, Mon Mar 27 17:39:40 PST 2006
//    Added imposter rendering.
//
hrchilds's avatar
hrchilds committed
265 266 267 268 269
//    Jeremy Meredith, Mon Aug 28 18:18:17 EDT 2006
//    Bonds are now line segments cells, and atoms are both points and
//    vertex cells.  This means we cannot look at cell data when looking
//    for atom arrays.  Also, account for model number directory prefix.
//
270 271 272 273 274
//    Jeremy Meredith, Thu Mar 12 17:32:34 EDT 2009
//    Allow any variable starting with "element" to be treated as
//    an atomic number -- this makes creating new element expressions
//    to override ones in the file possible.
//
275 276 277 278 279 280
//    Jeremy Meredith, Wed May 20 11:49:18 EDT 2009
//    MAX_ELEMENT_NUMBER now means the actual max element number, not the
//    total number of known elements in visit.  Added a fake "0" element
//    which means "unknown", and hydrogen now starts at 1.  This 
//    also means we don't have to correct for 1-origin atomic numbers.
//
281 282 283 284
//    Jeremy Meredith, Wed Jan 27 10:41:02 EST 2010
//    Only draw atoms where there is a Vertex cell type.  Don't assume
//    all points are atoms (since we now support dangling bonds).
//
fogal1's avatar
fogal1 committed
285 286 287
//    John Schreiner, Fri Feb 12 19:15:11 MST 2010
//    Fixed glTexCoord being called before glVertex for imposter rendering.
//
288 289 290
//    Jeremy Meredith, Thu Apr 22 14:11:20 EDT 2010
//    Added 2D mode.
//
291 292 293
//    Jeremy Meredith, Fri Jul 22 10:08:56 EDT 2011
//    Support non-float primary arrays.
//
hrchilds's avatar
hrchilds committed
294 295 296 297 298 299 300
// ****************************************************************************

void
avtOpenGLMoleculeRenderer::DrawAtomsAsSpheres(vtkPolyData *data,
                                              const MoleculeAttributes &atts)
{
    vtkPoints *points = data->GetPoints();
301
    int numpoints = data->GetNumberOfPoints();
302
    int numverts = data->GetNumberOfVerts();
hrchilds's avatar
hrchilds committed
303

hrchilds's avatar
hrchilds committed
304
    vtkDataArray *primary = data->GetPointData()->GetScalars();
305 306 307
    // TODO: allow cell-centered variables to (probably as an option)
    //       be drawn on atoms; some use cases -- like connected components
    //       -- do viably label both atoms and bonds.
hrchilds's avatar
hrchilds committed
308
    if (!primary)
hrchilds's avatar
hrchilds committed
309 310 311 312 313
    {
        // Let's just assume we don't want to plot the spheres for
        // a cell-centered variable
        return;
    }
314 315 316
    float *scalar = (float*)primary->GetVoidPointer(0);
    vector<float> scalar_storage;
    if (!primary->IsA("vtkFloatArray"))
hrchilds's avatar
hrchilds committed
317
    {
318 319 320 321 322
        int n = numpoints;
        scalar_storage.resize(n);
        for (int i=0; i<n; i++)
            scalar_storage[i] = primary->GetComponent(i,0);
        scalar = &(scalar_storage[0]);
hrchilds's avatar
hrchilds committed
323
    }
hrchilds's avatar
hrchilds committed
324

hrchilds's avatar
hrchilds committed
325 326
    string primaryname = primary->GetName();
    bool primary_is_element = (primaryname == "element" ||
327 328
                    (primaryname.length() > 7 &&
                     primaryname.substr(0,7) == "element") ||
hrchilds's avatar
hrchilds committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
                    (primaryname.length() > 8 &&
                     primaryname.substr(primaryname.length()-8) == "/element"));
    bool primary_is_resseq = (primaryname == "resseq" ||
                    (primaryname.length() > 7 &&
                     primaryname.substr(primaryname.length()-7) == "/resseq"));
    bool primary_is_restype = (primaryname == "restype" ||
                    (primaryname.length() > 8 &&
                     primaryname.substr(primaryname.length()-8) == "/restype"));

    vtkDataArray *element = primary_is_element ? primary : 
                                   data->GetPointData()->GetArray("element");
    if (element && !element->IsA("vtkFloatArray"))
    {
        debug4 << "avtOpenGLMoleculeRenderer: found a non-float array\n";
        return;
    }
    float *elementnos = element ? (float*)element->GetVoidPointer(0) : NULL;


    bool color_by_levels = (primary_is_element ||
                            primary_is_restype ||
                            primary_is_resseq)     && ct_is_discrete;
    bool color_by_element = color_by_levels && primary_is_element;
hrchilds's avatar
hrchilds committed
352 353 354 355 356 357 358 359 360 361 362 363 364

    float *radiusvar = NULL;
    bool sbv  = atts.GetScaleRadiusBy() == MoleculeAttributes::Variable;
    bool sbar = atts.GetScaleRadiusBy() == MoleculeAttributes::Atomic;
    bool sbcr = atts.GetScaleRadiusBy() == MoleculeAttributes::Covalent;
    float radiusscale = atts.GetRadiusScaleFactor();

    if (sbv)
    {
        if (atts.GetRadiusVariable() == "default")
            radiusvar = scalar;
        else
        {
hrchilds's avatar
hrchilds committed
365
            vtkDataArray *radius_array = data->GetPointData()->GetArray(
hrchilds's avatar
hrchilds committed
366 367 368 369 370 371 372
                                          atts.GetRadiusVariable().c_str());
            if (!radius_array)
            {
                // This shouldn't have gotten this far if it couldn't
                // read the variable like we asked.
                EXCEPTION1(ImproperUseException, "Couldn't read radius variable");
            }
hrchilds's avatar
hrchilds committed
373 374 375 376 377
            if (radius_array && !radius_array->IsA("vtkFloatArray"))
            {
                debug4 << "avtOpenGLMoleculeRenderer: found a non-float array\n";
                return;
            }
hrchilds's avatar
hrchilds committed
378
            radiusvar = (float*)radius_array->GetVoidPointer(0);
hrchilds's avatar
hrchilds committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
        }
    }

    //
    // Do initial setup for imposter rendering.
    //
    float ptOffsets[4][3] = {
        {-1., -1., 0.}, {1., -1., 0.},{1., 1., 0.}, {-1., 1., 0.}
    };
    const float texCoords[4][2] = {{0., 0.}, {1.,0.}, {1.,1.}, {0.,1.}};
    bool imposter = atts.GetDrawAtomsAs() == MoleculeAttributes::ImposterAtoms;
    int vmode = 0;
    if(imposter)
    {
        // If we're doing imposter rendering then set up sphere texturing
        // and disable lighting.
395 396 397 398
        if (is2D)
            ((avtOpenGLAtomTexturer2D *)tex2D)->BeginSphereTexturing();
        else
            ((avtOpenGLAtomTexturer3D *)tex3D)->BeginSphereTexturing();
hrchilds's avatar
hrchilds committed
399 400 401 402
        glDisable(GL_LIGHTING);

        // Based on the selected sphere texturing mode, there might be a
        // little more setup required.
403 404 405 406 407 408
        if (is2D)
            vmode = 1;
        else
            vmode = (((avtOpenGLAtomTexturer3D *)tex3D)->GetMode() ==
                     avtOpenGLAtomTexturer3D::TextureMode) ? 1 : 2;

hrchilds's avatar
hrchilds committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
        if(vmode == 1)
        {
            float modelview[4][4], imv[4][4];
            glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)modelview);
            // Zero out the translation.
            modelview[3][0] = 0.;
            modelview[3][1] = 0.;
            modelview[3][2] = 0.;
            // Invert the matrix so we get the inverse rotations needed.
            matrix_invert(modelview, imv);
            // Multiply the imv times the ptOffsets.
            matrix_mul_point(ptOffsets[0], ptOffsets[0], imv);
            matrix_mul_point(ptOffsets[1], ptOffsets[1], imv);
            matrix_mul_point(ptOffsets[2], ptOffsets[2], imv);
            matrix_mul_point(ptOffsets[3], ptOffsets[3], imv);
        }
    }

427 428 429 430
    if (is2D && vmode==0)
        glBegin(GL_TRIANGLES);
    else
        glBegin(GL_QUADS);
431 432
    vtkIdType *vertptr = data->GetVerts()->GetPointer();
    for (int ix=0; ix<numverts; ix++, vertptr += (1+*vertptr))
hrchilds's avatar
hrchilds committed
433
    {
434 435 436 437 438
        if (*vertptr != 1)
            continue;

        int atom = *(vertptr+1);

hrchilds's avatar
hrchilds committed
439 440
        int element_number = 0;
        if (element)
441
            element_number = int(elementnos[atom]);
hrchilds's avatar
hrchilds committed
442

443 444
        if (element_number < 0 || element_number > MAX_ELEMENT_NUMBER)
            element_number = 0;
hrchilds's avatar
hrchilds committed
445 446 447 448 449 450 451 452

        // Determine radius
        float radius = atts.GetRadiusFixed();
        if (element && sbar)
            radius = atomic_radius[element_number] * radiusscale;
        else if (element && sbcr)
            radius = covalent_radius[element_number] * radiusscale;
        else if (radiusvar && sbv)
453
            radius = radiusvar[atom] * radiusscale;
hrchilds's avatar
hrchilds committed
454 455 456 457 458 459 460 461 462

        // Determine color
        if (color_by_element)
        {
            int level = element_number % numcolors;
            SetColor3ubv(&colors[4*level]);
        }
        else if (color_by_levels)
        {
463
            int level = int(scalar[atom]) - (primary_is_resseq ? 1 : 0);
hrchilds's avatar
hrchilds committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
            if(levelsLUT != 0)
            {
                const unsigned char *rgb = 
                    levelsLUT->GetLookupTable()->MapValue(level);
                SetColor3ubv(rgb);
            }
            else
            {
                level = level % numcolors;
                SetColor3ubv(&colors[4*level]);
            }
        }
        else
        {
            float alpha;
            if (varmax == varmin)
                alpha = 0.5;
            else
482
                alpha = (scalar[atom] - varmin) / (varmax - varmin);
hrchilds's avatar
hrchilds committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
            
            int color = int((float(numcolors)-.01) * alpha);
            if (color < 0)
                color = 0;
            if (color > numcolors-1)
                color = numcolors-1;
            SetColor3ubv(&colors[4*color]);
        }

        if(vmode == 2)
        {
            // Plot squares. Use the same point for each of the vertices;
            // a vertex program will figure out the real coordinates.
            glNormal3f(radius, radius, radius);
            for(int j = 0; j < 4; ++j)
            {
                glTexCoord2fv(texCoords[j]);
fogal1's avatar
fogal1 committed
500
                glVertex3dv(points->GetPoint(atom));
hrchilds's avatar
hrchilds committed
501 502 503 504 505 506 507
            }
        }
        else if(vmode == 1)
        {
            // Plot squares
            for(int j = 0; j < 4; ++j)
            {
508
                double *pt = points->GetPoint(atom);
hrchilds's avatar
hrchilds committed
509
                float vert[3];
hrchilds's avatar
hrchilds committed
510 511 512
                vert[0] = pt[0] + ptOffsets[j][0] * radius;
                vert[1] = pt[1] + ptOffsets[j][1] * radius;
                vert[2] = pt[2] + ptOffsets[j][2] * radius;
hrchilds's avatar
hrchilds committed
513
                glTexCoord2fv(texCoords[j]);
fogal1's avatar
fogal1 committed
514
                glVertex3fv(vert);
hrchilds's avatar
hrchilds committed
515 516 517 518 519
            }
        }
        else
        {
            // Plot spheres
520
            double *pt = points->GetPoint(atom);
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
            if (is2D)
            {
                DrawCircleAsTriangles(pt[0],
                                      pt[1],
                                      radius,
                                      atts.GetAtomSphereQuality());
            }
            else
            {
                DrawSphereAsQuads(pt[0],
                                  pt[1],
                                  pt[2],
                                  radius,
                                  atts.GetAtomSphereQuality());
            }
hrchilds's avatar
hrchilds committed
536 537 538 539 540 541 542 543
        }
    }
    glEnd();

    // If we're imposter rendering then enable lighting once more and turn
    // off sphere texturing.
    if(imposter)
    {
544 545 546 547 548 549 550 551 552
        if (is2D)
        {
            ((avtOpenGLAtomTexturer2D *)tex2D)->EndSphereTexturing();
        }
        else
        {
            ((avtOpenGLAtomTexturer3D *)tex3D)->EndSphereTexturing();
            glEnable(GL_LIGHTING);
        }
hrchilds's avatar
hrchilds committed
553 554 555 556 557 558 559 560 561 562 563 564 565
    }
}

// ****************************************************************************
//  Method: avtOpenGLMoleculeRenderer::DrawBonds
//
//  Purpose:
//    Draw the bonds, either as line segments or as cylinders.
//
//  Programmer:  Jeremy Meredith
//  Creation:    February  3, 2006
//
//  Modifications:
hrchilds's avatar
hrchilds committed
566 567 568 569
//    Jeremy Meredith, Mon Aug 28 18:18:17 EDT 2006
//    Bonds are now line segments cells, and atoms are both points and
//    vertex cells.  This means we cannot look at cell data when looking
//    for atom arrays.  Also, account for model number directory prefix.
hrchilds's avatar
hrchilds committed
570
//
hrchilds's avatar
hrchilds committed
571 572 573
//    Jeremy Meredith, Wed Apr 18 10:23:53 EDT 2007
//    Don't shorten bonds if we're not drawing the atoms.
//
574 575 576 577 578 579 580 581 582 583 584 585
//    Jeremy Meredith, Fri Oct 17 12:20:25 EDT 2008
//    Small cleanups for shortened-bond rendering:
//    - Don't shorten bonds if we're drawing as normal geometry, either;
//      at very small atom sizes (e.g. bond radius==atom radius), it's not
//      what we want, and at larger sizes it shouldn't matter.  It's still
//      necessary for imposter atoms, though.
//    - If the radius of an atom is bigger than half the bond length, don't
//      draw that half of the bond. If we did, then we'd have that section
//      overlap when we're shortening bonds.
//    - Don't shorten the bonds if it's a cell-centered variable, because
//      in that case we're not drawing the atoms at all.
//
586 587 588 589 590
//    Jeremy Meredith, Thu Mar 12 17:32:34 EDT 2009
//    Allow any variable starting with "element" to be treated as
//    an atomic number -- this makes creating new element expressions
//    to override ones in the file possible.
//
591 592 593 594 595 596
//    Jeremy Meredith, Wed May 20 11:49:18 EDT 2009
//    MAX_ELEMENT_NUMBER now means the actual max element number, not the
//    total number of known elements in visit.  Added a fake "0" element
//    which means "unknown", and hydrogen now starts at 1.  This 
//    also means we don't have to correct for 1-origin atomic numbers.
//
597 598 599 600 601
//    Jeremy Meredith, Wed Jan 27 10:41:02 EST 2010
//    Draw a dangling, capped cylinder (or dangling line) for bonds
//    where one half has no vertex cell.  Also, don't draw any 
//    bond if neither adjacent point has a vertex cell.
//
602 603 604
//    Jeremy Meredith, Thu Apr 22 14:11:20 EDT 2010
//    Added 2D mode.
//
605 606 607 608 609
//    Jeremy Meredith, Fri Jul 22 10:08:56 EDT 2011
//    Support non-float primary arrays.
//    Correctly cap bonds when we're using a cell-centered variable,
//    as we aren't currently drawing atoms in that case.
//
hrchilds's avatar
hrchilds committed
610
// ****************************************************************************
hrchilds's avatar
hrchilds committed
611

hrchilds's avatar
hrchilds committed
612 613 614 615 616
void
avtOpenGLMoleculeRenderer::DrawBonds(vtkPolyData *data,
                                     const MoleculeAttributes &atts)
{
    vtkPoints *points = data->GetPoints();
617
    int numpoints = data->GetNumberOfPoints();
hrchilds's avatar
hrchilds committed
618
    int numverts = data->GetNumberOfVerts();
hrchilds's avatar
hrchilds committed
619
    vtkCellArray *lines = data->GetLines();
620
    int numlines = data->GetNumberOfLines();
hrchilds's avatar
hrchilds committed
621 622
    vtkIdType *segments = lines->GetPointer();

hrchilds's avatar
hrchilds committed
623 624 625 626 627 628 629
    bool primary_is_cell_centered = false;
    vtkDataArray *primary = data->GetPointData()->GetScalars();
    if (!primary)
    {
        primary = data->GetCellData()->GetScalars();
        primary_is_cell_centered = true;
    }
hrchilds's avatar
hrchilds committed
630 631
    if (!primary)
    {
hrchilds's avatar
hrchilds committed
632 633 634
        // eh? no variable at all?  that's a logic error....
        EXCEPTION1(ImproperUseException, "Expected a variable of some sort.");
    }
635 636 637 638

    float *scalar = (float*)primary->GetVoidPointer(0);
    vector<float> scalar_storage;
    if (!primary->IsA("vtkFloatArray"))
hrchilds's avatar
hrchilds committed
639
    {
640 641 642 643 644
        int n = primary_is_cell_centered ? (numlines+numverts) : numpoints;
        scalar_storage.resize(n);
        for (int i=0; i<n; i++)
            scalar_storage[i] = primary->GetComponent(i,0);
        scalar = &(scalar_storage[0]);
hrchilds's avatar
hrchilds committed
645 646
    }

hrchilds's avatar
hrchilds committed
647 648
    string primaryname = primary->GetName();
    bool primary_is_element = (primaryname == "element" ||
649 650
                    (primaryname.length() > 7 &&
                     primaryname.substr(0,7) == "element") ||
hrchilds's avatar
hrchilds committed
651 652 653 654 655 656 657 658 659 660 661 662
                    (primaryname.length() > 8 &&
                     primaryname.substr(primaryname.length()-8) == "/element"));
    bool primary_is_resseq = (primaryname == "resseq" ||
                    (primaryname.length() > 7 &&
                     primaryname.substr(primaryname.length()-7) == "/resseq"));
    bool primary_is_restype = (primaryname == "restype" ||
                    (primaryname.length() > 8 &&
                     primaryname.substr(primaryname.length()-8) == "/restype"));

    vtkDataArray *element = primary_is_element ? primary : 
                                   data->GetPointData()->GetArray("element");
    if (element && !element->IsA("vtkFloatArray"))
hrchilds's avatar
hrchilds committed
663
    {
hrchilds's avatar
hrchilds committed
664 665
        debug4 << "avtOpenGLMoleculeRenderer: found a non-float array\n";
        return;
hrchilds's avatar
hrchilds committed
666
    }
hrchilds's avatar
hrchilds committed
667 668 669 670 671 672
    float *elementnos = element ? (float*)element->GetVoidPointer(0) : NULL;

    bool color_by_levels = (primary_is_element ||
                            primary_is_restype ||
                            primary_is_resseq)     && ct_is_discrete;
    bool color_by_element = color_by_levels && primary_is_element;
hrchilds's avatar
hrchilds committed
673

hrchilds's avatar
hrchilds committed
674 675 676
    //
    // Get radius variable
    //
hrchilds's avatar
hrchilds committed
677
#ifdef SHORTEN_BONDS
hrchilds's avatar
hrchilds committed
678 679 680 681 682
    float *radiusvar = NULL;
    bool sbv  = atts.GetScaleRadiusBy() == MoleculeAttributes::Variable;
    bool sbar = atts.GetScaleRadiusBy() == MoleculeAttributes::Atomic;
    bool sbcr = atts.GetScaleRadiusBy() == MoleculeAttributes::Covalent;
    float radiusscale = atts.GetRadiusScaleFactor();
hrchilds's avatar
hrchilds committed
683 684

    if (sbv)
hrchilds's avatar
hrchilds committed
685 686
    {
        if (atts.GetRadiusVariable() == "default")
hrchilds's avatar
hrchilds committed
687 688 689 690 691 692
        {
            if (primary_is_cell_centered)
            {
                EXCEPTION1(ImproperUseException,
                           "Radius variable cannot be cell-centered.");
            }
hrchilds's avatar
hrchilds committed
693
            radiusvar = scalar;
hrchilds's avatar
hrchilds committed
694
        }
hrchilds's avatar
hrchilds committed
695 696
        else
        {
hrchilds's avatar
hrchilds committed
697
            vtkDataArray *radius_array = data->GetPointData()->GetArray(
hrchilds's avatar
hrchilds committed
698 699 700 701 702 703 704
                                          atts.GetRadiusVariable().c_str());
            if (!radius_array)
            {
                // This shouldn't have gotten this far if it couldn't
                // read the variable like we asked.
                EXCEPTION1(ImproperUseException, "Couldn't read radius variable");
            }
hrchilds's avatar
hrchilds committed
705 706 707 708 709
            if (radius_array && !radius_array->IsA("vtkFloatArray"))
            {
                debug4 << "avtOpenGLMoleculeRenderer: found a non-float array\n";
                return;
            }
hrchilds's avatar
hrchilds committed
710 711 712 713 714
            radiusvar = (float*)radius_array->GetVoidPointer(0);
        }
    }
#endif

hrchilds's avatar
hrchilds committed
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    if (atts.GetDrawBondsAs() == MoleculeAttributes::CylinderBonds)
    {
        glBegin(GL_QUADS);
    }
    else //  (atts.GetDrawBondsAs() == MoleculeAttributes::Wireframe)
    {
        glDisable(GL_LIGHTING);
        glLineWidth(LineWidth2Int(Int2LineWidth(atts.GetBondLineWidth())));
        if (atts.GetBondLineStyle() != 0)
        {
            glEnable(GL_LINE_STIPPLE);
            glLineStipple(1, LineStyle2StipplePattern(Int2LineStyle(
                                                  atts.GetBondLineStyle())));
        }
        else
        {
            glDisable(GL_LINE_STIPPLE);
        }
            
        glBegin(GL_LINES);
    }

737 738 739 740 741 742 743 744 745
    // We only want to draw a bond-half if its adjacent atom is a "real" atom.
    vector<bool> hasVertex(numpoints,false);
    vtkIdType *vertptr = data->GetVerts()->GetPointer();
    for (int i=0; i<data->GetNumberOfVerts(); i++, vertptr += (1+*vertptr))
    {
        int atom = *(vertptr+1);
        hasVertex[atom] = true;
    }

746
    vtkIdType *segptr = segments;
hrchilds's avatar
hrchilds committed
747
    for (int i=0; i<data->GetNumberOfLines(); i++)
hrchilds's avatar
hrchilds committed
748
    {
hrchilds's avatar
hrchilds committed
749
        if (*segptr == 2)
hrchilds's avatar
hrchilds committed
750
        {
hrchilds's avatar
hrchilds committed
751 752
            int v0 = *(segptr+1);
            int v1 = *(segptr+2);
hrchilds's avatar
hrchilds committed
753

hrchilds's avatar
hrchilds committed
754 755 756 757
            double pt_0[3];
            double pt_1[3];
            points->GetPoint(v0, pt_0);
            points->GetPoint(v1, pt_1);
hrchilds's avatar
hrchilds committed
758

hrchilds's avatar
hrchilds committed
759 760 761
            double pt_mid[3] = {(pt_0[0]+pt_1[0])/2.,
                                (pt_0[1]+pt_1[1])/2.,
                                (pt_0[2]+pt_1[2])/2.};
hrchilds's avatar
hrchilds committed
762

hrchilds's avatar
hrchilds committed
763
#ifdef SHORTEN_BONDS
hrchilds's avatar
hrchilds committed
764
            double dpt[3] = {pt_1[0]-pt_0[0], pt_1[1]-pt_0[1], pt_1[2]-pt_0[2]};
765
            double dptlen = sqrt(dpt[0]*dpt[0]+dpt[1]*dpt[1]+dpt[2]*dpt[2]);
hrchilds's avatar
hrchilds committed
766
            vtkMath::Normalize(dpt);
hrchilds's avatar
hrchilds committed
767 768
#endif

hrchilds's avatar
hrchilds committed
769 770
            for (int half=0; half<=1; half++)
            {
771 772
                int atom     = (half==0) ? v0 : v1;
                int otherAtom= (half==0) ? v1 : v0;
hrchilds's avatar
hrchilds committed
773 774
                double *pt_a = (half==0) ? pt_0 : pt_mid;
                double *pt_b = (half==0) ? pt_mid : pt_1;
hrchilds's avatar
hrchilds committed
775

776 777 778
                if (!hasVertex[atom])
                    continue;

hrchilds's avatar
hrchilds committed
779 780 781
                int element_number = 0;
                if (element)
                {
782
                    element_number = int(elementnos[atom]);
hrchilds's avatar
hrchilds committed
783 784
                }

785 786
                if (element_number < 0 || element_number > MAX_ELEMENT_NUMBER)
                    element_number = 0;
hrchilds's avatar
hrchilds committed
787

hrchilds's avatar
hrchilds committed
788
#ifdef SHORTEN_BONDS
789 790
                if (!primary_is_cell_centered &&
                    atts.GetDrawAtomsAs() == MoleculeAttributes::ImposterAtoms)
hrchilds's avatar
hrchilds committed
791
                {
hrchilds's avatar
hrchilds committed
792 793 794 795 796 797 798 799 800
                    // Determine radius
                    float atom_radius = atts.GetRadiusFixed();
                    if (element && sbar)
                        atom_radius = atomic_radius[element_number] * radiusscale;
                    else if (element && sbcr)
                        atom_radius = covalent_radius[element_number] * radiusscale;
                    else if (radiusvar && sbv)
                        atom_radius = radiusvar[i] * radiusscale;

801 802 803
                    if (atom_radius > dptlen/2.)
                        continue;

hrchilds's avatar
hrchilds committed
804 805 806 807 808 809 810 811 812 813 814 815 816 817
                    const float fudge = 0.9;

                    if (half == 0)
                    {
                        pt_a[0] += atom_radius * dpt[0] * fudge;
                        pt_a[1] += atom_radius * dpt[1] * fudge;
                        pt_a[2] += atom_radius * dpt[2] * fudge;
                    }
                    else
                    {
                        pt_b[0] -= atom_radius * dpt[0] * fudge;
                        pt_b[1] -= atom_radius * dpt[1] * fudge;
                        pt_b[2] -= atom_radius * dpt[2] * fudge;
                    }
hrchilds's avatar
hrchilds committed
818 819 820
                }
#endif

hrchilds's avatar
hrchilds committed
821 822 823 824 825 826 827 828 829
                float radius = atts.GetBondRadius();

                // Determine color
                if (atts.GetColorBonds() == MoleculeAttributes::SingleColor)
                {
                    SetColor3ubv(atts.GetBondSingleColor().GetColor());
                }
                else // (atts.GetColorBonds() == MoleculeAttributes::ColorByAtom)
                {
hrchilds's avatar
hrchilds committed
830 831 832 833 834 835
                    float scalarval;
                    if (primary_is_cell_centered)
                        scalarval = scalar[i + numverts];
                    else
                        scalarval = scalar[atom];

hrchilds's avatar
hrchilds committed
836 837 838 839 840 841 842
                    if (color_by_element)
                    {
                        int level = element_number % numcolors;
                        SetColor3ubv(&colors[4*level]);
                    }
                    else if (color_by_levels)
                    {
hrchilds's avatar
hrchilds committed
843
                        int level = int(scalarval) - (primary_is_resseq ? 1 : 0);
hrchilds's avatar
hrchilds committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
                        if(levelsLUT != 0)
                        {
                            const unsigned char *rgb = 
                            levelsLUT->GetLookupTable()->MapValue(level);
                            SetColor3ubv(rgb);
                        }
                        else
                        {
                            level = level % numcolors;
                            SetColor3ubv(&colors[4*level]);
                        }
                    }
                    else
                    {
                        float alpha;
                        if (varmax == varmin)
                            alpha = 0.5;
                        else
hrchilds's avatar
hrchilds committed
862
                            alpha = (scalarval - varmin) / (varmax - varmin);
hrchilds's avatar
hrchilds committed
863 864 865 866 867 868 869 870 871 872 873 874
            
                        int color = int((float(numcolors)-.01) * alpha);
                        if (color < 0)
                            color = 0;
                        if (color > numcolors-1)
                            color = numcolors-1;
                        SetColor3ubv(&colors[4*color]);
                    }
                }

                if (atts.GetDrawBondsAs() == MoleculeAttributes::CylinderBonds )
                {
875 876 877 878 879
                    if (is2D)
                    {
                        DrawRectangleBetweenTwoPoints(pt_a, pt_b, radius);
                    }
                    else
880
                    {
881 882 883 884 885 886 887 888
                        DrawCylinderBetweenTwoPoints(pt_a, pt_b, radius,
                                                 atts.GetBondCylinderQuality());
                        if (!hasVertex[otherAtom])
                        {
                            DrawCylinderCap(pt_a, pt_b, half,
                                            radius,
                                            atts.GetBondCylinderQuality());
                        }
889 890 891 892 893 894 895 896
                        // TODO: modify this next test if we allow drawing
                        //       atoms with a primary cell-centered variable
                        if (primary_is_cell_centered)
                        {
                            DrawCylinderCap(pt_0, pt_1, 1-half,
                                            radius,
                                            atts.GetBondCylinderQuality());
                        }
897
                    }
hrchilds's avatar
hrchilds committed
898 899 900
                }
                else // == MoleculeAttributes::Wireframe
                {
hrchilds's avatar
hrchilds committed
901 902
                    glVertex3dv(pt_a);
                    glVertex3dv(pt_b);
hrchilds's avatar
hrchilds committed
903 904 905
                }
            }
        }
hrchilds's avatar
hrchilds committed
906 907

        segptr += (*segptr) + 1;
hrchilds's avatar
hrchilds committed
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
    }
    glEnd();
}



// ****************************************************************************
//  Method:  avtOpenGLMoleculeRenderer::Render
//
//  Purpose:
//    Render one image
//
//  Arguments:
//    ds      : the data set to render
//
//  Programmer:  Jeremy Meredith
//  Creation:    February  3, 2006
//
//  Modifications:
//    Brad Whitlock, Mon Mar 27 14:56:06 PST 2006
//    I made sure that we use immediate mode when we need to when we use 
//    imposter rendering,
//
fogal1's avatar
fogal1 committed
931 932 933 934
//    John Schreiner, Fri Feb 12 19:19:34 MST 2010
//    Removed width/height hints for atom imposters that aren't required
//    anymore.  Also don't use depth hint when drawing bonds (always use depth).
//
935 936 937
//    Jeremy Meredith, Thu Apr 22 14:11:20 EDT 2010
//    Added 2D mode.
//
hrchilds's avatar
hrchilds committed
938 939 940 941 942 943 944 945 946
// ****************************************************************************

void
avtOpenGLMoleculeRenderer::Render(vtkPolyData *data,
                                 const MoleculeAttributes &atts,
                                 bool immediateModeRendering,
                                 float _varmin, float _varmax,
                                 float _ambient_coeff,
                                 float _spec_coeff, float _spec_power,
947 948
                                 float _spec_r, float _spec_g, float _spec_b,
                                 bool _is2D)
hrchilds's avatar
hrchilds committed
949 950 951 952 953 954 955 956 957
{
    if (!data->GetCellData()->GetScalars() &&
        !data->GetPointData()->GetScalars())
        return;

    // If we're drawing imposter quads then we need to be in immediate mode
    // for certain imposter methods.
    if(atts.GetDrawAtomsAs() == MoleculeAttributes::ImposterAtoms)
    {
958 959 960
        if(is2D ||
           ((avtOpenGLAtomTexturer3D *)tex3D)->GetMode() ==
                                          avtOpenGLAtomTexturer3D::TextureMode)
hrchilds's avatar
hrchilds committed
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
        {
            immediateModeRendering = true;
        }
    }

    if (immediateModeRendering)
    {
        if (displaylistid != 0)
        {
            glDeleteLists(displaylistid, 1);
            displaylistid = 0;
        }
    }
    else
    {
        // Check to see if we need to regenerate lists
        if (displaylistid != 0 &&
            atts_for_displaylist != atts)
        {
            glDeleteLists(displaylistid, 1);
            displaylistid = 0;
        }

        // If the list we have is valid, just call it
        if (displaylistid != 0)
        {
            glCallList(displaylistid);
            return;
        }

        atts_for_displaylist = atts;
        displaylistid = glGenLists(1);
        glNewList(displaylistid, GL_COMPILE);
    }

    varmin = _varmin;
    varmax = _varmax;

    immediatemode = immediateModeRendering;
1000
    is2D          = _is2D;
hrchilds's avatar
hrchilds committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    ambient_coeff = _ambient_coeff;
    spec_coeff    = _spec_coeff;
    spec_power    = _spec_power;
    spec_r        = _spec_r;
    spec_g        = _spec_g;
    spec_b        = _spec_b;

    glPushAttrib(GL_COLOR_BUFFER_BIT |
                 GL_DEPTH_BUFFER_BIT |
                 GL_ENABLE_BIT);
    glDisable(GL_BLEND);

1013 1014 1015 1016
    if (is2D)
        glDisable(GL_LIGHTING);
    else
        glEnable(GL_LIGHTING);
hrchilds's avatar
hrchilds committed
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

#if 0
    glDisable(GL_COLOR_MATERIAL);
#else
    float diff[] = {1,1,1,1};
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diff);
    if (ambient_coeff == 0)
    {
        float amb[] = {ambient_coeff, ambient_coeff, ambient_coeff, 1};
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
        glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
    }
    else
    {
        float amb[] = {ambient_coeff, ambient_coeff, ambient_coeff, 1};
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    }
    glEnable(GL_COLOR_MATERIAL);
#endif
    float spec[] = {spec_r * spec_coeff,
                    spec_g * spec_coeff,
                    spec_b * spec_coeff,
                    1};
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &spec_power);
    glShadeModel(GL_SMOOTH);

    TRY
    {
        SetColors(data, atts);
hrchilds's avatar
hrchilds committed
1048
        bool doBonds = atts.GetDrawBondsAs() != MoleculeAttributes::NoBonds;
hrchilds's avatar
hrchilds committed
1049

hrchilds's avatar
hrchilds committed
1050
        if (atts.GetDrawAtomsAs() == MoleculeAttributes::SphereAtoms)
hrchilds's avatar
hrchilds committed
1051 1052 1053
        {
            DrawAtomsAsSpheres(data,atts);
        }
hrchilds's avatar
hrchilds committed
1054 1055 1056 1057
        else if(atts.GetDrawAtomsAs() == MoleculeAttributes::ImposterAtoms)
        {
            DrawAtomsAsSpheres(data,atts);
        }
hrchilds's avatar
hrchilds committed
1058

hrchilds's avatar
hrchilds committed
1059
        if (doBonds)
hrchilds's avatar
hrchilds committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
        {
            DrawBonds(data,atts);
        }
    }
    CATCH2(VisItException, e)
    {
        // Note: we should not need to intercept this here, except
        // that it's otherwise uncaught, as of version 1.5.
        // Also note: if we decide to let the exception reach up
        // the call chain, we STILL need at least catch it here,
        // make sure we've cleaned up OpenGL correctly
        // (e.g. popattrib and endlist), and then re-throw.
        avtCallback::IssueWarning(e.Message().c_str());
    }
    ENDTRY

    glPopAttrib();


    if (immediateModeRendering)
    {
    }
    else
    {
        glEndList();
        glCallList(displaylistid);
    }
}


// ****************************************************************************
//  Method:  avtOpenGLMoleculeRenderer::SetColors
//
//  Purpose:
//    Set up the color table.
//
//  Arguments:
//    data    : the data set to render
//
//  Programmer:  Jeremy Meredith
//  Creation:    February 14, 2006
//
//  Modifications:
hrchilds's avatar
hrchilds committed
1103 1104 1105 1106
//    Jeremy Meredith, Mon Aug 28 18:18:17 EDT 2006
//    Bonds are now line segments cells, and atoms are both points and
//    vertex cells.  This means we cannot look at cell data when looking
//    for atom arrays.  Also, account for model number directory prefix.
hrchilds's avatar
hrchilds committed
1107
//
1108 1109 1110 1111 1112
//    Jeremy Meredith, Thu Mar 12 17:32:34 EDT 2009
//    Allow any variable starting with "element" to be treated as
//    an atomic number -- this makes creating new element expressions
//    to override ones in the file possible.
//
hrchilds's avatar
hrchilds committed
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
// ****************************************************************************

void
avtOpenGLMoleculeRenderer::SetColors(vtkPolyData *data,
                                     const MoleculeAttributes &atts)
{
    int new_numcolors = -1;

    avtColorTables *ct = avtColorTables::Instance();
    const int opacity = 255;

    //
    // Detect if we're using the default color table or a color table
    // that does not exist anymore.
    //
    string new_colortablename;
    string varName = "";
hrchilds's avatar
hrchilds committed
1130
    if (data->GetPointData()->GetScalars())
hrchilds's avatar
hrchilds committed
1131
    {
hrchilds's avatar
hrchilds committed
1132
        varName = data->GetPointData()->GetScalars()->GetName();
hrchilds's avatar
hrchilds committed
1133
    }
hrchilds's avatar
hrchilds committed
1134
    else if (data->GetCellData()->GetScalars())
hrchilds's avatar
hrchilds committed
1135
    {
hrchilds's avatar
hrchilds committed
1136
        varName = data->GetCellData()->GetScalars()->GetName();
hrchilds's avatar
hrchilds committed
1137 1138 1139 1140 1141 1142 1143
    }

    if (varName == "")
    {
        new_colortablename = "";
        new_numcolors = 0;
    }
hrchilds's avatar
hrchilds committed
1144
    else if (varName == "element" ||
1145
             (varName.length()>7 && varName.substr(0,7)=="element") ||
hrchilds's avatar
hrchilds committed
1146
             (varName.length()>8 && varName.substr(varName.length()-8)=="/element"))
hrchilds's avatar
hrchilds committed
1147 1148 1149 1150 1151 1152 1153
    {
        new_colortablename = atts.GetElementColorTable();
        if (new_colortablename == "Default")
            new_colortablename = string(ct->GetDefaultDiscreteColorTable());

        new_numcolors = 110;
    }
hrchilds's avatar
hrchilds committed
1154 1155
    else if (varName == "resseq" ||
             (varName.length()>7 && varName.substr(varName.length()-7)=="/resseq"))
hrchilds's avatar
hrchilds committed
1156 1157 1158 1159 1160 1161 1162
    {
        new_colortablename = atts.GetResidueSequenceColorTable();
        if (new_colortablename == "Default")
            new_colortablename = string(ct->GetDefaultDiscreteColorTable());

        new_numcolors = 256;
    }
hrchilds's avatar
hrchilds committed
1163 1164
    else if (varName == "restype" ||
             (varName.length()>8 && varName.substr(varName.length()-8)=="/restype"))
hrchilds's avatar
hrchilds committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
    {
        new_colortablename = atts.GetResidueTypeColorTable();
        if (new_colortablename == "Default")
            new_colortablename = string(ct->GetDefaultDiscreteColorTable());

        new_numcolors = NumberOfKnownResidues();
    }
    else
    {
        new_colortablename = atts.GetContinuousColorTable();
        if (new_colortablename == "Default")
            new_colortablename = string(ct->GetDefaultContinuousColorTable());

        new_numcolors = 256;
    }

    if (!ct->ColorTableExists(new_colortablename.c_str()))
    {
        EXCEPTION1(InvalidColortableException, new_colortablename);
    }

    //
    // Skip the work if we don't need to do it!
    //
    if (new_numcolors      == numcolors   &&
        new_colortablename == colortablename)
    {
        return;
    } 

    numcolors      = new_numcolors;
    colortablename = new_colortablename;

    if (colors)
        delete[] colors;
    
    colors = new unsigned char[numcolors * 4];
    unsigned char *cptr = colors;

    //
    // Add a color for each subset name.
    //
    if (ct->IsDiscrete(colortablename.c_str()))
    {
        // The CT is discrete, get its color color control points.
        ct_is_discrete = true;
        for (int i = 0; i < numcolors; ++i)
        {
            unsigned char rgb[3] = {0,0,0};
            ct->GetControlPointColor(colortablename.c_str(), i, rgb);
            *cptr++ = rgb[0];
            *cptr++ = rgb[1];
            *cptr++ = rgb[2];
            *cptr++ = opacity;
        }
    }
    else
    {
        // The CT is continuous, sample the CT so we have a unique color
        // for each element.
        ct_is_discrete = false;
        unsigned char *rgb = ct->GetSampledColors(colortablename.c_str(), numcolors);
        if (rgb)
        {
            for (int i = 0; i < numcolors; ++i)
            {
                int j = i * 3;
                *cptr++ = rgb[j];
                *cptr++ = rgb[j+1];
                *cptr++ = rgb[j+2];
                *cptr++ = opacity;
            }
            delete [] rgb;
        }
    }
}

// ****************************************************************************
//  Method:  avtOpenGLMoleculeRenderer::CalculateSpherePts
//
//  Purpose:
//    Precalculate points for sphere geometry.
//
//  Programmer:  Jeremy Meredith
//  Creation:    February 10, 2006
//
//  Modifications:
//
// ****************************************************************************

void avtOpenGLMoleculeRenderer::CalculateSpherePts()
{
    if (spheres_calculated)
        return;

    spheres_calculated = true;

    for (int detail=0; detail<MAX_DETAIL_LEVELS; detail++)
    {
        int qdetail = sphere_quality_levels[detail][0];
        int hdetail = sphere_quality_levels[detail][1];
        sphere_pts[detail] = new float[(qdetail+1)*(hdetail+1)*3];

        for (int a=0; a<=qdetail; a++)
        {
            float theta = 2*M_PI * float(a) / float(qdetail);
            for (int b=0; b<=hdetail; b++)
            {
                float phi = M_PI * float(b) / float(hdetail);

                float dx = sin(phi) * cos(theta);
                float dy = sin(phi) * sin(theta);
                float dz = cos(phi);

                sphere_pts[detail][(a * (hdetail+1) + b)*3 + 0] = dx;
                sphere_pts[detail][(a * (hdetail+1) + b)*3 + 1] = dy;
                sphere_pts[detail][(a * (hdetail+1) + b)*3 + 2] = dz;
            }
        }
    }
}

1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
// ****************************************************************************
//  Method:  avtOpenGLMoleculeRenderer::CalculateCirclePts
//
//  Purpose:
//    Precalculate points for circle geometry.
//
//  Programmer:  Jeremy Meredith
//  Creation:    April 22, 2010
//
//  Modifications:
//
// ****************************************************************************

void avtOpenGLMoleculeRenderer::CalculateCirclePts()
{
    if (circles_calculated)
        return;

    circles_calculated = true;

    for (int detail=0; detail<MAX_DETAIL_LEVELS; detail++)
    {
        int cdetail = circle_quality_levels[detail];
        circle_pts[detail] = new float[(cdetail+1)*2];

        for (int a=0; a<=cdetail; a++)
        {
            float theta = 2*M_PI * float(a) / float(cdetail);
            float dx = cos(theta);
            float dy = sin(theta);
            circle_pts[detail][a*2 + 0] = dx;
            circle_pts[detail][a*2 + 1] = dy;
        }
    }
}

hrchilds's avatar
hrchilds committed
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
// ****************************************************************************
//  Method:  avtOpenGLMoleculeRenderer::CalculateCylPts
//
//  Purpose:
//    Precalculate points for cylinder geometry.
//
//  Programmer:  Jeremy Meredith
//  Creation:    February 10, 2006
//
//  Modifications:
//
// ****************************************************************************

void avtOpenGLMoleculeRenderer::CalculateCylPts()
{
    if (cylinders_calculated)
        return;

    cylinders_calculated = true;

    for (int detail=0; detail<MAX_DETAIL_LEVELS; detail++)
    {
        int cdetail = cylinder_quality_levels[detail];
        cyl_pts[detail] = new float[(cdetail+1)*4];

        for (int b=0; b<=cdetail; b++)
        {
            float theta = 2*M_PI * float(b) / float(cdetail);

            float dx = cos(theta);
            float dy = sin(theta);
            float dz = 0;

            cyl_pts[detail][b*4+0] = dx;
            cyl_pts[detail][b*4+1] = dy;
            cyl_pts[detail][b*4+2] = dz;
            cyl_pts[detail][b*4+3] = 0;
        }
    }
}

// ****************************************************************************
//  Method:  avtOpenGLMoleculeRenderer::DrawSphereAsQuads
//
//  Purpose:
//    Make the OpenGL calls to draw a sphere with the
//    given center, radius, and detail level.
//
//  Programmer:  Jeremy Meredith
//  Creation:    February 10, 2006
//
//  Modifications:
//
// ****************************************************************************
void
avtOpenGLMoleculeRenderer::DrawSphereAsQuads(float x0,
                                             float y0,
                                             float z0,
                                             float r,
                                             int detail)
{
    CalculateSpherePts();

    int qdetail = sphere_quality_levels[detail][0];
    int hdetail = sphere_quality_levels[detail][1];

    for (int a=0; a<qdetail; a++)
    {
        int a0 = a;
        int a1 = a+1;
        for (int b=0; b<hdetail; b++)
        {
            int b0 = b;
            int b1 = b+1;

            float *v00, *v01, *v10, *v11;

            v00 = &(sphere_pts[detail][(a0 * (hdetail+1) + b0)*3]);
            v01 = &(sphere_pts[detail][(a0 * (hdetail+1) + b1)*3]);
            v10 = &(sphere_pts[detail][(a1 * (hdetail+1) + b0)*3]);
            v11 = &(sphere_pts[detail][(a1 * (hdetail+1) + b1)*3]);

            glNormal3fv(v00);
            glVertex3f(x0 + r*v00[0], y0 + r*v00[1], z0 + r*v00[2]);

            glNormal3fv(v01);
            glVertex3f(x0 + r*v01[0], y0 + r*v01[1], z0 + r*v01[2]);

            glNormal3fv(v11);
            glVertex3f(x0 + r*v11[0], y0 + r*v11[1], z0 + r*v11[2]);

            glNormal3fv(v10);
            glVertex3f(x0 + r*v10[0], y0 + r*v10[1], z0 + r*v10[2]);
        }
    }
}

1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
// ****************************************************************************
//  Method:  avtOpenGLMoleculeRenderer::DrawCircleAsTriangles
//
//  Purpose:
//    Make the OpenGL calls to draw a circle with the
//    given center, radius, and detail level.  Z==0 is assumed.
//
//  Programmer:  Jeremy Meredith
//  Creation:    April 22, 2010
//
//  Modifications:
//
// ****************************************************************************
void
avtOpenGLMoleculeRenderer::DrawCircleAsTriangles(float x0,
                                                 float y0,
                                                 float r,
                                                 int detail)
{
    CalculateCirclePts();

    int cdetail = circle_quality_levels[detail];

    for (int a=0; a<cdetail; a++)
    {
        int a0 = a;
        int a1 = a+1;

        float *v0 = &(circle_pts[detail][a0*2]);
        float *v1 = &(circle_pts[detail][a1*2]);

        glVertex2f(x0          , y0          );
        glVertex2f(x0 + r*v0[0], y0 + r*v0[1]);
        glVertex2f(x0 + r*v1[0], y0 + r*v1[1]);
    }
}

// ****************************************************************************
//  Method:  avtOpenGLMoleculeRenderer::DrawRectangleBetweenTwoPoints
//
//  Purpose:
//    Make the OpenGL calls to draw a rectangle with the given begin
//    and end points and radius.  Assumes drawing in the z==0 plane.
//
//  Programmer:  Jeremy Meredith
//  Creation:    April 22, 2010
//
//  Modifications:
//
// ****************************************************************************
void
avtOpenGLMoleculeRenderer::DrawRectangleBetweenTwoPoints(double *p0,
                                                         double *p1,
                                                         float r)
{
bonnell's avatar
bonnell committed
1475
    float v[3] = {(float)(p0[1]-p1[1]), (float)(p1[0]-p0[0]), 0.f};
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
    float v_len = vtkMath::Normalize(v);
    if (v_len == 0)
        return;

    glVertex2f(p0[0] + r*v[0], p0[1] + r*v[1]);
    glVertex2f(p1[0] + r*v[0], p1[1] + r*v[1]);
    glVertex2f(p1[0] - r*v[0], p1[1] - r*v[1]);
    glVertex2f(p0[0] - r*v[0], p0[1] - r*v[1]);
}




hrchilds's avatar
hrchilds committed
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
// ****************************************************************************
//  Method:  avtOpenGLMoleculeRenderer::DrawCylinderBetweenTwoPoints
//
//  Purpose:
//    Make the OpenGL calls to draw a cylinder with the given begin
//    and end points, radius, and detail level.
//
//  Programmer:  Jeremy Meredith
//  Creation:    February 10, 2006
//
//  Modifications:
hrchilds's avatar
hrchilds committed
1500 1501
//    Jeremy Meredith, Mon Aug 28 18:25:02 EDT 2006
//    Point locations are now doubles, not floats.
hrchilds's avatar
hrchilds committed
1502 1503 1504
//
// ****************************************************************************
void
hrchilds's avatar
hrchilds committed
1505 1506
avtOpenGLMoleculeRenderer::DrawCylinderBetweenTwoPoints(double *p0,
                                                        double *p1,
hrchilds's avatar
hrchilds committed
1507 1508 1509 1510 1511
                                                        float r,
                                                        int detail)
{
    CalculateCylPts();

whitlocb's avatar
whitlocb committed
1512 1513 1514
    float vc[3] = {static_cast<float>(p1[0]-p0[0]),
                   static_cast<float>(p1[1]-p0[1]),
                   static_cast<float>(p1[2]-p0[2])};
hrchilds's avatar
hrchilds committed
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
    float va[3];
    float vb[3];

    float vc_len = vtkMath::Normalize(vc);
    if (vc_len == 0)
        return;

    vtkMath::Perpendiculars(vc, va,vb, 0);

    float v0[4];
    float v1[4];
    int cdetail = cylinder_quality_levels[detail];
    for (int b=0; b<cdetail; b++)
    {
        int b0 = b;
        int b1 = b+1;

        float *u0, *u1;
        u0 = &(cyl_pts[detail][b0*4]);
        u1 = &(cyl_pts[detail][b1*4]);

        v0[0] = va[0]*u0[0] + vb[0]*u0[1];
        v0[1] = va[1]*u0[0] + vb[1]*u0[1];
        v0[2] = va[2]*u0[0] + vb[2]*u0[1];

        v1[0] = va[0]*u1[0] + vb[0]*u1[1];
        v1[1] = va[1]*u1[0] + vb[1]*u1[1];
        v1[2] = va[2]*u1[0] + vb[2]*u1[1];

        glNormal3fv(v0);
        glVertex3f(p1[0] + r*v0[0], p1[1] + r*v0[1], p1[2] + r*v0[2]);

        glNormal3fv(v0);
        glVertex3f(p0[0] + r*v0[0], p0[1] + r*v0[1], p0[2] + r*v0[2]);

        glNormal3fv(v1);
        glVertex3f(p0[0] + r*v1[0], p0[1] + r*v1[1], p0[2] + r*v1[2]);

        glNormal3fv(v1);
        glVertex3f(p1[0] + r*v1[0], p1[1] + r*v1[1], p1[2] + r*v1[2]);
    }
}


1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
// ****************************************************************************
//  Method:  avtOpenGLMoleculeRenderer::DrawCylinderCap
//
//  Purpose:
//    Make the OpenGL calls to draw a cylinder cap with the given begin
//    and end points, radius, and detail level.  The cap is drawn at the
//    second point; the first point is used for orientation.
//
//  Programmer:  Jeremy Meredith
//  Creation:    January 25, 2010
//
//  Modifications:
//
// ****************************************************************************
void
avtOpenGLMoleculeRenderer::DrawCylinderCap(double *p0,
                                           double *p1,
                                           int half,
                                           float r,
                                           int detail)
{
    glEnd();
    glBegin(GL_TRIANGLE_FAN);

    CalculateCylPts();

whitlocb's avatar
whitlocb committed
1585 1586 1587
    float vc[3] = {static_cast<float>(p1[0]-p0[0]),
                   static_cast<float>(p1[1]-p0[1]),
                   static_cast<float>(p1[2]-p0[2])};
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
    float va[3];
    float vb[3];

    float vc_len = vtkMath::Normalize(vc);
    if (vc_len == 0)
        return;

    vtkMath::Perpendiculars(vc, va,vb, 0);

    float v0[4];

    if (half==0)
    {
        glNormal3fv(vc);
        glVertex3dv(p1);
    }
    else
    {
        //glNormal3f(-vc[0],-vc[1],-vc[2]);
        glNormal3fv(vc);
        glVertex3dv(p0);
    }

    int cdetail = cylinder_quality_levels[detail];
    for (int b=0; b<=cdetail; b++)
    {
        int b0 = b;

        float *u0;
        u0 = &(cyl_pts[detail][b0*4]);

        v0[0] = va[0]*u0[0] + vb[0]*u0[1];
        v0[1] = va[1]*u0[0] + vb[1]*u0[1];
        v0[2] = va[2]*u0[0] + vb[2]*u0[1];

        if (half==0)
        {
            glVertex3f(p1[0] + r*v0[0], p1[1] + r*v0[1], p1[2] + r*v0[2]);
        }
        else
        {
            glVertex3f(p0[0] + r*v0[0], p0[1] + r*v0[1], p0[2] + r*v0[2]);
        }
    }

    glEnd();
    glBegin(GL_QUADS);
}