Commit 9e848878 authored by David E. DeMarle's avatar David E. DeMarle
Browse files

flesh out bonds

parent e79c6596
......@@ -26,6 +26,8 @@
#include "vtkPoints.h"
#include "vtkRenderer.h"
#include "vtkUnsignedShortArray.h"
#include "vtkVector.h"
#include "vtkVectorOperators.h"
#include <algorithm>
#include <vector>
......@@ -97,16 +99,16 @@ void vtkOSPRayMoleculeMapperNode::Render(bool prepass)
if (mapper->GetRenderAtoms())
{
OSPGeometry atoms;
const vtkIdType numAtoms = molecule->GetNumberOfAtoms();
vtkPoints* allPoints = molecule->GetAtomicPositionArray();
float* mdata = new float[molecule->GetNumberOfAtoms() * 5];
float* mdata = new float[numAtoms * 5];
int* idata = (int*)mdata;
vtkUnsignedShortArray* atomicNumbers = molecule->GetAtomicNumberArray();
vtkScalarsToColors* lut = mapper->GetLookupTable();
float* _colors = new float[molecule->GetNumberOfAtoms() * 5];
float* _colors = new float[numAtoms * 4];
for (size_t i = 0; i < molecule->GetNumberOfAtoms(); i++)
for (size_t i = 0; i < numAtoms; i++)
{
mdata[i * 5 + 0] = static_cast<float>(allPoints->GetPoint(i)[0]);
mdata[i * 5 + 1] = static_cast<float>(allPoints->GetPoint(i)[1]);
......@@ -115,6 +117,8 @@ void vtkOSPRayMoleculeMapperNode::Render(bool prepass)
mdata[i * 5 + 3] = mapper->GetAtomicRadiusScaleFactor() *
mapper->GetPeriodicTable()->GetVDWRadius(atomicNumbers->GetValue(i));
// todo: make a single color array for the vtkPeriodicTable and index into that instead of
// one color per atom
idata[i * 5 + 4] = i; // index into colors array
const unsigned char* ucolor = lut->MapValue(atomicNumbers->GetTuple1(i));
_colors[i * 4 + 0] = static_cast<float>(ucolor[0] / 255.0);
......@@ -122,10 +126,10 @@ void vtkOSPRayMoleculeMapperNode::Render(bool prepass)
_colors[i * 4 + 2] = static_cast<float>(ucolor[2] / 255.0);
_colors[i * 4 + 3] = static_cast<float>(ucolor[3] / 255.0);
}
OSPData mesh = ospNewData(molecule->GetNumberOfAtoms() * 5, OSP_FLOAT, &mdata[0]);
OSPData mesh = ospNewData(numAtoms * 5, OSP_FLOAT, &mdata[0]);
ospCommit(mesh);
OSPData colors = ospNewData(molecule->GetNumberOfAtoms(), OSP_FLOAT4, &_colors[0]);
OSPData colors = ospNewData(numAtoms, OSP_FLOAT4, &_colors[0]);
ospCommit(colors);
atoms = ospNewGeometry("spheres");
......@@ -146,6 +150,103 @@ void vtkOSPRayMoleculeMapperNode::Render(bool prepass)
delete[] mdata;
delete[] _colors;
}
if (mapper->GetRenderBonds())
{
OSPGeometry bonds = ospNewGeometry("cylinders");
vtkVector3f pos1, pos2;
vtkIdType atomIds[2];
vtkVector3f bondVec;
float bondLength;
float bondRadius = mapper->GetBondRadius();
vtkVector3f bondCenter;
const vtkIdType numBonds = molecule->GetNumberOfBonds();
int width = 3 + 3 + 1 + 1; // Sxyx Exyz rad coloridx
float* mdata = new float[2 * numBonds * width];
int* idata = (int*)mdata;
vtkUnsignedShortArray* atomicNumbers = molecule->GetAtomicNumberArray();
vtkScalarsToColors* lut = mapper->GetLookupTable();
float* _colors = new float[2 * numBonds * 4];
for (vtkIdType bondInd = 0; bondInd < numBonds; ++bondInd)
{
vtkBond bond = molecule->GetBond(bondInd);
pos1 = bond.GetBeginAtom().GetPosition();
pos2 = bond.GetEndAtom().GetPosition();
atomIds[0] = bond.GetBeginAtomId();
atomIds[1] = bond.GetEndAtomId();
// Compute additional bond info
// - Normalized vector in direction of bond
bondVec = pos2 - pos1;
// - Center of bond for translation
bondCenter[0] = (pos1[0] + pos2[0]) * 0.5;
bondCenter[1] = (pos1[1] + pos2[1]) * 0.5;
bondCenter[2] = (pos1[2] + pos2[2]) * 0.5;
mdata[(bondInd * 2 + 0) * width + 0] = static_cast<float>(pos1.GetX());
mdata[(bondInd * 2 + 0) * width + 1] = static_cast<float>(pos1.GetY());
mdata[(bondInd * 2 + 0) * width + 2] = static_cast<float>(pos1.GetZ());
mdata[(bondInd * 2 + 0) * width + 3] = static_cast<float>(bondCenter.GetX());
mdata[(bondInd * 2 + 0) * width + 4] = static_cast<float>(bondCenter.GetY());
mdata[(bondInd * 2 + 0) * width + 5] = static_cast<float>(bondCenter.GetZ());
mdata[(bondInd * 2 + 0) * width + 6] = bondRadius;
// todo: make a single color array for the vtkPeriodicTable and index into that instead of
// one color per atom
idata[(bondInd * 2 + 0) * width + 7] = bondInd * 2 + 0; // index into colors array
const unsigned char* ucolor = lut->MapValue(atomicNumbers->GetTuple1(atomIds[0]));
_colors[(bondInd * 2 + 0) * 4 + 0] = static_cast<float>(ucolor[0] / 255.0);
_colors[(bondInd * 2 + 0) * 4 + 1] = static_cast<float>(ucolor[1] / 255.0);
_colors[(bondInd * 2 + 0) * 4 + 2] = static_cast<float>(ucolor[2] / 255.0);
_colors[(bondInd * 2 + 0) * 4 + 3] = static_cast<float>(ucolor[3] / 255.0);
mdata[(bondInd * 2 + 1) * width + 0] = static_cast<float>(bondCenter.GetX());
mdata[(bondInd * 2 + 1) * width + 1] = static_cast<float>(bondCenter.GetY());
mdata[(bondInd * 2 + 1) * width + 2] = static_cast<float>(bondCenter.GetZ());
mdata[(bondInd * 2 + 1) * width + 3] = static_cast<float>(pos2.GetX());
mdata[(bondInd * 2 + 1) * width + 4] = static_cast<float>(pos2.GetY());
mdata[(bondInd * 2 + 1) * width + 5] = static_cast<float>(pos2.GetZ());
mdata[(bondInd * 2 + 1) * width + 6] = bondRadius;
idata[(bondInd * 2 + 1) * width + 7] = bondInd * 2 + 1; // index into colors array
ucolor = lut->MapValue(atomicNumbers->GetTuple1(atomIds[1]));
_colors[(bondInd * 2 + 1) * 4 + 0] = static_cast<float>(ucolor[0] / 255.0);
_colors[(bondInd * 2 + 1) * 4 + 1] = static_cast<float>(ucolor[1] / 255.0);
_colors[(bondInd * 2 + 1) * 4 + 2] = static_cast<float>(ucolor[2] / 255.0);
_colors[(bondInd * 2 + 1) * 4 + 3] = static_cast<float>(ucolor[3] / 255.0);
}
OSPData _mdata = ospNewData(2 * numBonds * width, OSP_FLOAT, mdata);
ospSet1i(bonds, "bytes_per_cylinder", width * sizeof(float));
ospSet1i(bonds, "offset_v0", 0);
ospSet1i(bonds, "offset_v1", 3 * sizeof(float));
ospSet1i(bonds, "offset_radius", 6 * sizeof(float));
ospSet1i(bonds, "offset_colorID", 7 * sizeof(float));
ospCommit(_mdata);
OSPData colors = ospNewData(2 * numBonds, OSP_FLOAT4, &_colors[0]);
ospCommit(colors);
ospSetData(bonds, "cylinders", _mdata);
ospSetData(bonds, "color", colors);
ospCommit(bonds);
this->Geometries.emplace_back(bonds);
ospRelease(_mdata);
ospRelease(colors);
}
if (mapper->GetRenderLattice())
{
// OSPGeometry lattice;
// todo: take a half hour and translate vtkMoleculeMapper::UpdateLatticePolyData() to ospray
// API this->Geometries.emplace_back(lattice);
}
this->BuildTime.Modified();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment