PeriDyno 1.0.0
Loading...
Searching...
No Matches
VtkPointVisualModule.cpp
Go to the documentation of this file.
2
3// framework
4#include <Node.h>
5#include <SceneGraph.h>
6#include <Topology/TriangleSet.h>
7
8#include <vtkActor.h>
9#include <vtkProperty.h>
10#include <vtkPointSource.h>
11#include <vtkRenderer.h>
12#include <vtkOpenGLPolyDataMapper.h>
13#include <vtkOpenGLRenderWindow.h>
14#include <vtkOpenGLVertexBufferObjectCache.h>
15#include <vtkOpenGLVertexBufferObjectGroup.h>
16#include <vtkPolyData.h>
17#include <vtkFloatArray.h>
18#include <vtkOpenGLVertexBufferObject.h>
19#include <vtkOpenGLIndexBufferObject.h>
20
21#include "SceneGraphFactory.h"
22
23#ifdef _WIN32
24#include <windows.h>
25#endif
26#include <cuda_gl_interop.h>
27
28using namespace dyno;
29
30class PointMapper : public vtkOpenGLPolyDataMapper
31{
32public:
34 {
35 // create psedo data, required by the vtkOpenGLPolyDataMapper to render content
36 vtkNew<vtkPoints> points;
37
39 Vec3f bbox0 = scn->getLowerBound();
40 Vec3f bbox1 = scn->getUpperBound();
41 points->InsertNextPoint(bbox0[0], bbox0[1], bbox0[2]);
42 points->InsertNextPoint(bbox1[0], bbox1[1], bbox1[2]);
43
44 vtkNew<vtkPolyData> polyData;
45 polyData->SetPoints(points);
46 SetInputData(polyData);
47 }
48
49
50 void UpdateBufferObjects(vtkRenderer *ren, vtkActor *act) override
51 {
52 if (!m_module->isDirty())
53 return;
54
55 if (!m_module->isInitialized()) return;
56
57 auto node = m_module->getParent();
58
59 if (node == NULL || !node->isVisible()) return;
60
61 auto pSet = m_module->inPointSet()->getDataPtr();// std::dynamic_pointer_cast<dyno::PointSet<dyno::DataType3f>>(node->getTopologyModule());
62 auto verts = pSet->getPoints();
63
64 cudaError_t error;
65
66 if (!m_initialized)
67 {
68 m_initialized = true;
69
70 // vertex buffer
71 vtkNew<vtkPoints> tempVertData;
72 tempVertData->SetNumberOfPoints(verts.size());
73
74 vtkOpenGLRenderWindow* renWin = vtkOpenGLRenderWindow::SafeDownCast(ren->GetRenderWindow());
75 vtkOpenGLVertexBufferObjectCache* cache = renWin->GetVBOCache();
76 this->VBOs->CacheDataArray("vertexMC", tempVertData->GetData(), cache, VTK_FLOAT);
77 this->VBOs->BuildAllVBOs(cache);
78 vtkOpenGLVertexBufferObject* vertexBuffer = this->VBOs->GetVBO("vertexMC");
79
80 // index buffer
81 std::vector<unsigned int> indexArray(verts.size());
82 for (unsigned int i = 0; i < indexArray.size(); i++)
83 indexArray[i] = i;
84
85 this->Primitives[PrimitivePoints].IBO->Upload(indexArray, vtkOpenGLIndexBufferObject::ElementArrayBuffer);
86 this->Primitives[PrimitivePoints].IBO->IndexCount = indexArray.size();
87
88 // create memory mapper for CUDA
89 error = cudaGraphicsGLRegisterBuffer(&m_cudaVBO, vertexBuffer->GetHandle(), cudaGraphicsRegisterFlagsWriteDiscard);
90 }
91
92 // copy vertex memory
93 {
94 size_t size;
95 void* cudaPtr = 0;
96
97 // upload vertex
98 error = cudaGraphicsMapResources(1, &m_cudaVBO);
99 error = cudaGraphicsResourceGetMappedPointer(&cudaPtr, &size, m_cudaVBO);
100 error = cudaMemcpy(cudaPtr, verts.begin(), verts.size() * sizeof(float) * 3, cudaMemcpyDeviceToDevice);
101 error = cudaGraphicsUnmapResources(1, &m_cudaVBO);
102 }
103 }
104
105private:
107
108 bool m_initialized = false;
109
110 cudaGraphicsResource* m_cudaVBO;
111};
112
114
116{
117 this->setName("point_renderer");
118
119 m_actor = vtkActor::New();
120 m_actor->GetProperty()->SetRepresentationToPoints();
121 m_actor->GetProperty()->RenderPointsAsSpheresOn();
122 m_actor->GetProperty()->SetPointSize(2.0);
123 m_actor->SetMapper(new PointMapper(this));
124}
125
#define IMPLEMENT_CLASS_COMMON(name, func)
Definition Object.h:73
dyno::VtkPointVisualModule * m_module
void UpdateBufferObjects(vtkRenderer *ren, vtkActor *act) override
cudaGraphicsResource * m_cudaVBO
PointMapper(VtkPointVisualModule *v)
void setName(std::string name)
Definition Module.cpp:187
static SceneGraphFactory * instance()
std::shared_ptr< SceneGraph > active()
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Vector< float, 3 > Vec3f
Definition Vector3D.h:93