//---------------------------------------------------------------------------- vesSourceData::Ptr vesKiwiDataConversionTools::ConvertColors(vtkUnsignedCharArray* colors) { if (colors && colors->GetNumberOfComponents() == 3) { unsigned char rgb[3]; const size_t nTuples = colors->GetNumberOfTuples(); vesSourceDataC3f::Ptr colorSourceData(new vesSourceDataC3f()); for (size_t i = 0; i < nTuples; ++i) { colors->GetTupleValue(i, rgb); vesVertexDataC3f color; color.m_color = vesVector3f(rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0); colorSourceData->pushBack(color); } return colorSourceData; } else if (colors && colors->GetNumberOfComponents() == 4) { // Ignore alpha for now. // Currently, the shaders expect a 3 component rgb array unsigned char rgb[4]; const size_t nTuples = colors->GetNumberOfTuples(); vesSourceDataC3f::Ptr colorSourceData(new vesSourceDataC3f()); for (size_t i = 0; i < nTuples; ++i) { colors->GetTupleValue(i, rgb); vesVertexDataC3f color; color.m_color = vesVector3f(rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0); colorSourceData->pushBack(color); } return colorSourceData; } return vesSourceDataC3f::Ptr(); }
vesTransformNode::vesTransformNode() : vesGroupNode() { this->m_implementation = new vesTransformPrivate(); this->m_center = vesVector3f (0,0,0); this->m_rotation = vesVector4f(0,0,1,0); this->m_scale = vesVector3f(1,1,1); this->m_scaleOrientation = vesVector4f(0,0,1,0); this->m_translation = vesVector3f(0,0,0); this->m_referenceFrame = Relative; }
vesActor::vesActor() : vesNode() { // \todo: Create a default apperance. this->m_implementation = new vesTransformPrivate(); this->m_center = vesVector3f (0,0,0); this->m_rotation = vesVector4f(0,0,1,0); this->m_scale = vesVector3f(1,1,1); this->m_scaleOrientation = vesVector4f(0,0,1,0); this->m_translation = vesVector3f(0,0,0); this->m_referenceFrame = Relative; }
//---------------------------------------------------------------------------- vesKiwiViewerApp::vesKiwiViewerApp() { this->Internal = new vesInternal(); this->Internal->CameraSpinner->setInteractor(this->cameraInteractor()); this->resetScene(); this->addBuiltinDataset("Utah Teapot", "teapot.vtp"); this->addBuiltinDataset("Stanford Bunny", "bunny.vtp"); this->addBuiltinDataset("NLM Visible Woman Hand", "visible-woman-hand.vtp"); this->addBuiltinDataset("NA-MIC Knee Atlas", "AppendedKneeData.vtp"); this->addBuiltinDataset("ROS C Turtle", "cturtle.vtp"); this->Internal->BuiltinDatasetCameraParameters.back().setParameters( vesVector3f(0.,0.,1.), vesVector3f(0.,-1.,0.)); this->addBuiltinDataset("Mount St. Helens", "MountStHelen.vtp"); this->addBuiltinDataset("Space Shuttle", "shuttle.vtp"); //http://visibleearth.nasa.gov/view.php?id=57730 //this->addBuiltinDataset("NASA Blue Marble", "textured_sphere.vtp"); //this->Internal->BuiltinDatasetCameraParameters.back().setParameters( // vesVector3f(1.,0.,0.), vesVector3f(0.,0.,1.)); this->addBuiltinDataset("Buckyball", "Buckyball.vtp"); this->addBuiltinDataset("Caffeine", "caffeine.pdb"); this->addBuiltinDataset("Head CT Image", "head.vti"); this->Internal->BuiltinDatasetCameraParameters.back().setParameters( vesVector3f(1.,0.,0.), vesVector3f(0.,0.,-1.)); this->addBuiltinDataset("KiwiViewer Logo", "kiwi.png"); this->initBlinnPhongShader( vesBuiltinShaders::vesBlinnPhong_vert(), vesBuiltinShaders::vesBlinnPhong_frag()); this->initToonShader( vesBuiltinShaders::vesToonShader_vert(), vesBuiltinShaders::vesToonShader_frag()); this->initGouraudShader( vesBuiltinShaders::vesShader_vert(), vesBuiltinShaders::vesShader_frag()); this->initGouraudTextureShader( vesBuiltinShaders::vesGouraudTexture_vert(), vesBuiltinShaders::vesGouraudTexture_frag()); this->initTextureShader( vesBuiltinShaders::vesBackgroundTexture_vert(), vesBuiltinShaders::vesBackgroundTexture_frag()); this->initClipShader( vesBuiltinShaders::vesClipPlane_vert(), vesBuiltinShaders::vesClipPlane_frag()); this->setShadingModel("Gouraud"); }
//---------------------------------------------------------------------------- int vesKiwiBrainAtlasRepresentation::findTappedModel(int displayX, int displayY) { vesSharedPtr<vesRenderer> ren = this->renderer(); displayY = ren->height() - displayY; vesVector3d rayPoint0 = ren->computeDisplayToWorld(vesVector3f(displayX, displayY, /*focalDepth=*/0.0)).cast<double>(); vesVector3d rayPoint1 = ren->computeDisplayToWorld(vesVector3f(displayX, displayY, /*focalDepth=*/1.0)).cast<double>(); int tappedModel = -1; double minDist = -1; double extraDist = 0; for (size_t i = 0; i < this->Internal->Locators.size(); ++i) { if (!this->Internal->ModelStatus[i]) continue; vesVector3d pickRayPoint0 = rayPoint0; extraDist = 0.0; if (static_cast<int>(i) == this->Internal->SkullRepIndex || static_cast<int>(i) == this->Internal->SkinRepIndex) { // set rayPoint0 to intersection of ray with plane so that the ray // only intersects with the non-clipped portion of these models if (this->Internal->Plane && this->Internal->Plane->EvaluateFunction (rayPoint0.data()) > 0) { double t; vesVector3d planeIntersection; this->Internal->Plane->IntersectWithLine(rayPoint0.data(), rayPoint1.data(), t, planeIntersection.data()); pickRayPoint0 = planeIntersection; vesVector3d rayToPlane = pickRayPoint0 - rayPoint0; extraDist = rayToPlane.norm(); } } double dist = PickDataSet(this->Internal->Locators[i], pickRayPoint0, rayPoint1); if (dist >= 0) dist += extraDist; if (dist >= 0 && (minDist == -1 || dist < minDist)) { tappedModel = i; minDist = dist; } } return tappedModel; }
//---------------------------------------------------------------------------- void vesKiwiImageWidgetRepresentation::scrollImageSlice(double deltaX, double deltaY) { deltaY *= -1; vesSharedPtr<vesRenderer> ren = this->renderer(); vesVector3f viewFocus = ren->camera()->focalPoint(); vesVector3f viewFocusDisplay = ren->computeWorldToDisplay(viewFocus); float focalDepth = viewFocusDisplay[2]; this->Internal->LastTouchPosition += vesVector2f(deltaX, deltaY); vesVector3f worldTouchPosition = ren->computeDisplayToWorld(vesVector3f(this->Internal->LastTouchPosition[0], this->Internal->LastTouchPosition[1], focalDepth)); worldTouchPosition -= this->Internal->GrabOffset.cast<float>(); int flatDimension = this->Internal->SelectedImageDimension; vesVector3f planeNormal(0, 0, 0); planeNormal[flatDimension] = 1.0; int extent[6]; vesVector3d spacing; vesVector3d origin; this->imageData()->GetOrigin(origin.data()); this->imageData()->GetSpacing(spacing.data()); this->imageData()->GetExtent(extent); double distanceAlongInteractionAxis = worldTouchPosition.dot(planeNormal); double distanceFromOriginAlongAxis = distanceAlongInteractionAxis - origin[flatDimension]; int slicesFromOrigin = distanceFromOriginAlongAxis / spacing[flatDimension] - extent[flatDimension*2]*spacing[flatDimension]; this->scheduleSetSliceIndex(flatDimension, slicesFromOrigin); }
void vesKiwiDataConversionTools::GenericConvertTriangles(vtkPolyData* input, vesSharedPtr<vesGeometryData> output) { vesSourceDataP3N3f::Ptr sourceData (new vesSourceDataP3N3f()); double inPoint[3]; for (int i = 0; i < input->GetNumberOfPoints(); ++i){ input->GetPoint(i, inPoint); vesVertexDataP3N3f vertexData; vertexData.m_position = vesVector3f(inPoint[0], inPoint[1], inPoint[2]); sourceData->pushBack(vertexData); } // copy triangles in place to ves structure vtkCellArray* polys = input->GetPolys(); vtkIdType num; vtkIdType* vertices; vesSharedPtr< vesIndices<T> > indicesObj = std::tr1::static_pointer_cast< vesIndices<T> > (output->triangles()->getVesIndices()); typename vesIndices<T>::Indices* triangleIndices = indicesObj->indices(); triangleIndices->clear(); triangleIndices->resize(polys->GetNumberOfCells()); T* outIndex = &triangleIndices->front(); for (int i = 0; i < polys->GetNumberOfCells(); ++i) { // there are 4 elements for each triangle cell in the array (count, i1, i2, i3) polys->GetCell(4*i, num, vertices); *outIndex++ = vertices[0]; *outIndex++ = vertices[1]; *outIndex++ = vertices[2]; } if (input->GetPointData()->GetNormals()) { vtkDataArray* normals = input->GetPointData()->GetNormals(); for (int i = 0; i < input->GetNumberOfPoints(); ++i) { sourceData->arrayReference()[i].m_normal[0] = normals->GetTuple(i)[0]; sourceData->arrayReference()[i].m_normal[1] = normals->GetTuple(i)[1]; sourceData->arrayReference()[i].m_normal[2] = normals->GetTuple(i)[2]; } } else { output->computeNormals<T>(); } output->computeBounds(); output->addSource(sourceData); }
//---------------------------------------------------------------------------- void vesKiwiCameraInteractor::pan(vesVector2d p0, vesVector2d p1) { // calculate the focal depth so we'll know how far to move vesCamera::Ptr camera = mRenderer->camera(); vesVector3f viewFocus = camera->focalPoint(); vesVector3f viewPoint = camera->position(); vesVector3f viewFocusDisplay = mRenderer->computeWorldToDisplay(viewFocus); float focalDepth = viewFocusDisplay[2]; // map change into world coordinates vesVector3f oldPickPoint = mRenderer->computeDisplayToWorld(vesVector3f(p0[0], p0[1], focalDepth)); vesVector3f newPickPoint = mRenderer->computeDisplayToWorld(vesVector3f(p1[0], p1[1], focalDepth)); vesVector3f motionVector = oldPickPoint - newPickPoint; vesVector3f newViewFocus = viewFocus + motionVector; vesVector3f newViewPoint = viewPoint + motionVector; camera->setFocalPoint(newViewFocus); camera->setPosition(newViewPoint); }
//---------------------------------------------------------------------------- void vesKiwiDataConversionTools::SetVertexColors( vtkUnsignedCharArray* colors, vesSharedPtr<vesGeometryData> geometryData) { assert(geometryData); assert(colors); assert(colors->GetNumberOfComponents() == 3); unsigned char rgb[3]; const size_t nTuples = colors->GetNumberOfTuples(); vesSourceDataC3f::Ptr colorSourceData (new vesSourceDataC3f()); for (size_t i = 0; i < nTuples; ++i) { colors->GetTupleValue(i, rgb); vesVertexDataC3f color; color.m_color = vesVector3f(rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0); colorSourceData->pushBack(color); } geometryData->addSource(colorSourceData); }
//---------------------------------------------------------------------------- void vesKiwiDataConversionTools::SetVertexColors(vtkDataArray* scalars, vtkScalarsToColors* scalarsToColors, vesSharedPtr<vesGeometryData> geometryData) { assert(scalars); assert(scalars->GetNumberOfComponents() == 1); assert(geometryData); double rgb[3]; const size_t nTuples = scalars->GetNumberOfTuples(); vesSourceDataC3f::Ptr colorSourceData (new vesSourceDataC3f()); for (size_t i = 0; i < nTuples; ++i) { scalarsToColors->GetColor(scalars->GetComponent(i, 0), rgb); vesVertexDataC3f color; color.m_color = vesVector3f(rgb[0], rgb[1], rgb[2]); colorSourceData->pushBack(color); } geometryData->addSource(colorSourceData); }
//---------------------------------------------------------------------------- vesSourceData::Ptr vesKiwiDataConversionTools::ConvertScalarsToColors(vtkDataArray* scalars, vtkScalarsToColors* scalarsToColors) { if (!scalars || !scalarsToColors) { return vesSourceDataC3f::Ptr(); } scalarsToColors->SetVectorModeToMagnitude(); vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::Take( scalarsToColors->MapScalars(scalars, VTK_COLOR_MODE_MAP_SCALARS, -1)); unsigned char rgb[4]; const size_t nTuples = colors->GetNumberOfTuples(); vesSourceDataC3f::Ptr colorSourceData(new vesSourceDataC3f()); for (size_t i = 0; i < nTuples; ++i) { colors->GetTupleValue(i, rgb); vesVertexDataC3f color; color.m_color = vesVector3f(rgb[0]/255.0, rgb[1]/255.0, rgb[2]/255.0); colorSourceData->pushBack(color); } return colorSourceData; }
vesCameraParameters() { this->setParameters(vesVector3f(0.0, 0.0, -1.0), vesVector3f(0.0, 1.0, 0.0)); }
//---------------------------------------------------------------------------- void vesKiwiBrainAtlasRepresentation::loadData(const std::string& filename) { this->Internal->Locators.clear(); std::ifstream f; std::string modelInfoFile = filename; std::string dataDir = vtksys::SystemTools::GetFilenamePath(modelInfoFile); std::cout << "Reading model info file: " << modelInfoFile << std::endl; f.open(modelInfoFile.c_str()); while (!f.eof()) { std::string anatomicalName; std::string modelFile; double color[3]; f >> anatomicalName; if (!anatomicalName.size()) { break; } f >> color[0] >> color[1] >> color[2] >> modelFile; modelFile = dataDir + "/" + modelFile; vesKiwiDataLoader dataLoader; vtkSmartPointer<vtkPolyData> polyData = vtkPolyData::SafeDownCast(dataLoader.loadDataset(modelFile)); if (!polyData) { std::cout << "Failed to read: " << modelFile << std::endl; continue; } vesSharedPtr<vesShaderProgram> shader = this->Internal->GeometryShader; double opacity = 1.0; int binNumber = 2; if (anatomicalName == "Skin" || anatomicalName == "skull_bone") { shader = this->Internal->ClipShader; } vesKiwiPolyDataRepresentation::Ptr rep = vesKiwiPolyDataRepresentation::Ptr(new vesKiwiPolyDataRepresentation); rep->initializeWithShader(shader); rep->setPolyData(polyData); rep->setColor(color[0], color[1], color[2], opacity); rep->setBinNumber(binNumber); this->Internal->AllReps.push_back(rep); vtkSmartPointer<vtkCellLocator> locator = vtkSmartPointer<vtkCellLocator>::New(); locator->SetDataSet(polyData); locator->BuildLocator(); this->Internal->Locators.push_back(locator); this->Internal->AnatomicalNames.push_back(anatomicalName); this->Internal->AnatomicalModels.push_back(rep); this->Internal->ModelStatus.push_back(true); this->Internal->ModelSceneStatus.push_back(true); this->Internal->Colors.push_back(vesVector3f(color[0], color[1], color[2])); double center[3]; vesVector3f anchor; vtkBoundingBox bounds; bounds.SetBounds(polyData->GetBounds()); bounds.GetCenter(center); anchor[0] = center[0]; anchor[1] = center[1]; anchor[2] = center[2]; double modelRadius = bounds.GetDiagonalLength() / 2.0; this->Internal->AnnotationAnchors.push_back(anchor); this->Internal->AnchorOffsets.push_back(modelRadius); if (anatomicalName == "Skin") { this->Internal->SkinRepIndex = this->Internal->AnatomicalModels.size() - 1; this->Internal->SkinRep = rep; rep->setBinNumber(5); } else if (anatomicalName == "skull_bone") { this->Internal->SkullRepIndex = this->Internal->AnatomicalModels.size() - 1; } } }
//---------------------------------------------------------------------------- bool vesKiwiImageWidgetRepresentation::handleSingleTouchDown(int displayX, int displayY) { if (!this->Internal->InteractionEnabled) { return false; } // calculate the focal depth so we'll know how far to move vesSharedPtr<vesRenderer> ren = this->renderer(); // flip Y coordinate displayY = ren->height() - displayY; this->Internal->LastTouchPosition = vesVector2f(displayX, displayY); std::tr1::shared_ptr<vesCamera> camera = ren->camera(); vesVector3f cameraFocalPoint = camera->focalPoint(); vesVector3f cameraPosition = camera->position(); vesVector3f displayFocus = ren->computeWorldToDisplay(cameraFocalPoint); float focalDepth = displayFocus[2]; vesVector3f rayPoint0 = cameraPosition; vesVector3f rayPoint1 = ren->computeDisplayToWorld(vesVector3f(displayX, displayY, focalDepth)); vesVector3f touchWorldPosition = rayPoint1; vesVector3f rayDirection = rayPoint1 - rayPoint0; rayDirection.normalize(); rayPoint1 += rayDirection*1e6; vtkNew<vtkAppendPolyData> appendFilter; std::vector<int> cellIdToPlaneId; for (int i = 0; i < 3; ++i) { if (this->planeVisibility(i)) { appendFilter->AddInputData(this->Internal->SliceReps[i]->imagePlanePolyData()); cellIdToPlaneId.push_back(i); } } appendFilter->Update(); vtkNew<vtkCellLocator> locator; locator->SetDataSet(appendFilter->GetOutput()); locator->BuildLocator(); double p0[3] = {rayPoint0[0], rayPoint0[1], rayPoint0[2]}; double p1[3] = {rayPoint1[0], rayPoint1[1], rayPoint1[2]}; double pickPoint[3]; double t; double paramCoords[3]; vtkIdType cellId = -1; int subId; int result = locator->IntersectWithLine(p0, p1, 0.0, t, pickPoint, paramCoords, subId, cellId); if (result == 1) { this->Internal->SelectedImageDimension = cellIdToPlaneId[cellId]; this->interactionOn(); int currentDimension = this->Internal->SelectedImageDimension; int currentSliceIndex = this->Internal->CurrentSliceIndices[this->Internal->SelectedImageDimension]; int extent[6]; vesVector3d spacing; vesVector3d origin; this->imageData()->GetOrigin(origin.data()); this->imageData()->GetSpacing(spacing.data()); this->imageData()->GetExtent(extent); double sliceDistanceAlongAxis = spacing[currentDimension] * (currentSliceIndex + extent[currentDimension*2]); origin[currentDimension] += sliceDistanceAlongAxis; this->Internal->GrabOffset = touchWorldPosition.cast<double>() - origin; } else { this->Internal->SelectedImageDimension = -1; this->interactionOff(); } return this->interactionIsActive(); }
void resetView() { mTestApp->setCameraPosition(vesVector3f(-0.25, 0.25, 0.25)); mTestApp->setCameraFocalPoint(vesVector3f(0.0, 0.1, 0.0)); }
//----------------------------------------------------------------------------- vesSharedPtr<vesGeometryData> vesKiwiDataConversionTools::ConvertPVWebData(vesSharedPtr<vesPVWebDataSet> dataset) { vesSharedPtr<vesGeometryData> geometryData = vesSharedPtr<vesGeometryData>(new vesGeometryData); geometryData->setName("PolyData"); #ifdef VES_USE_CURL const int numberOfVerts = dataset->m_numberOfVerts; if (dataset->m_datasetType == 'M') { // verts and normals vesSourceDataP3N3f::Ptr sourceData(new vesSourceDataP3N3f()); vesVertexDataP3N3f vertexData; for (int i = 0; i < numberOfVerts; ++i) { float* vertex = dataset->vertices() + i*3; float* normal = dataset->normals() + i*3; vertexData.m_position[0] = vertex[0]; vertexData.m_position[1] = vertex[1]; vertexData.m_position[2] = vertex[2]; vertexData.m_normal[0] = normal[0]; vertexData.m_normal[1] = normal[1]; vertexData.m_normal[2] = normal[2]; sourceData->pushBack(vertexData); } geometryData->addSource(sourceData); // triangles vesSharedPtr<vesIndices<unsigned int> > triangleIndices = vesSharedPtr<vesIndices<unsigned int> >(new vesIndices<unsigned int>()); vesPrimitive::Ptr trianglesPrimitive = vesPrimitive::Ptr(new vesPrimitive()); trianglesPrimitive->setIndexCount(3); trianglesPrimitive->setIndicesValueType(vesPrimitiveIndicesValueType::UnsignedInt); trianglesPrimitive->setPrimitiveType(vesPrimitiveRenderType::Triangles); trianglesPrimitive->setVesIndices(triangleIndices); geometryData->addPrimitive(trianglesPrimitive); for (int i = 0; i < dataset->m_numberOfIndices/3; ++i) { short* indices = dataset->indices() + i*3; triangleIndices->pushBackIndices(indices[0], indices[1], indices[2]); } } else if (dataset->m_datasetType == 'L') { // verts vesSourceDataP3f::Ptr vertexSourceData(new vesSourceDataP3f()); vesVertexDataP3f vertexData; for (int i = 0; i < numberOfVerts; ++i) { float* vertex = dataset->vertices() + i*3; vertexData.m_position[0] = vertex[0]; vertexData.m_position[1] = vertex[1]; vertexData.m_position[2] = vertex[2]; vertexSourceData->pushBack(vertexData); } geometryData->addSource(vertexSourceData); // lines vesSharedPtr<vesIndices<unsigned int> > lineIndices = vesSharedPtr<vesIndices<unsigned int> >(new vesIndices<unsigned int>()); vesPrimitive::Ptr linesPrimitive = vesPrimitive::Ptr(new vesPrimitive()); linesPrimitive->setIndexCount(2); linesPrimitive->setIndicesValueType(vesPrimitiveIndicesValueType::UnsignedInt); linesPrimitive->setPrimitiveType(vesPrimitiveRenderType::Lines); linesPrimitive->setVesIndices(lineIndices); geometryData->addPrimitive(linesPrimitive); for (int i = 0; i < dataset->m_numberOfIndices/2; ++i) { short* indices = dataset->indices() + i*2; lineIndices->pushBackIndices(indices[0], indices[1]); } } else if (dataset->m_datasetType == 'P') { // verts vesSourceDataP3f::Ptr vertexSourceData(new vesSourceDataP3f()); vesVertexDataP3f vertexData; for (int i = 0; i < numberOfVerts; ++i) { float* vertex = dataset->vertices() + i*3; vertexData.m_position[0] = vertex[0]; vertexData.m_position[1] = vertex[1]; vertexData.m_position[2] = vertex[2]; vertexSourceData->pushBack(vertexData); } geometryData->addSource(vertexSourceData); vesPrimitive::Ptr pointPrimitive (new vesPrimitive()); pointPrimitive->setPrimitiveType(vesPrimitiveRenderType::Points); pointPrimitive->setIndexCount(1); geometryData->addPrimitive(pointPrimitive); } // colors float opacity = 1.0; if (dataset->m_transparency) { opacity = 0.4; } vesSourceDataC3f::Ptr colorSourceData(new vesSourceDataC3f()); vesVertexDataC3f vertColor; for (size_t i = 0; i < numberOfVerts; ++i) { unsigned char* color = dataset->colors() + i*4; vertColor.m_color = vesVector3f(color[0]/255.0, color[1]/255.0, color[2]/255.0); colorSourceData->pushBack(vertColor); } geometryData->addSource(colorSourceData); #endif // VES_USE_CURL return geometryData; }