Skip to content

Commit d16c181

Browse files
author
Marcin Rogowski
committed
Initial commit == ngsmith/calvr_plugins
0 parents  commit d16c181

22 files changed

+11529
-0
lines changed

CMakeLists.txt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
SET(LIB_NAME KinectDemo)
2+
3+
SET(PLUGIN_HEADERS
4+
KinectDemo.h
5+
Skeleton.h
6+
MCylinder.h
7+
${REMOTEKINECT_INCLUDE_DIR}/protocol/skeletonframe.pb.h
8+
${REMOTEKINECT_INCLUDE_DIR}/protocol/skeleton.pb.h
9+
${REMOTEKINECT_INCLUDE_DIR}/protocol/colormap.pb.h
10+
${REMOTEKINECT_INCLUDE_DIR}/protocol/depthmap.pb.h
11+
${REMOTEKINECT_INCLUDE_DIR}/protocol/pointcloud.pb.h
12+
${REMOTEKINECT_INCLUDE_DIR}/protocol/packing.pb.h
13+
kUtils.h
14+
SelectableItem.h
15+
CloudManager.h
16+
SkeletonManager.h
17+
)
18+
19+
ADD_LIBRARY(${LIB_NAME}
20+
"SHARED"
21+
${PLUGIN_HEADERS}
22+
KinectDemo.cpp
23+
Skeleton.cpp
24+
MCylinder.cpp
25+
${REMOTEKINECT_INCLUDE_DIR}/protocol/skeletonframe.pb.cc
26+
${REMOTEKINECT_INCLUDE_DIR}/protocol/skeleton.pb.cc
27+
${REMOTEKINECT_INCLUDE_DIR}/protocol/colormap.pb.cc
28+
${REMOTEKINECT_INCLUDE_DIR}/protocol/depthmap.pb.cc
29+
${REMOTEKINECT_INCLUDE_DIR}/protocol/pointcloud.pb.cc
30+
${REMOTEKINECT_INCLUDE_DIR}/protocol/packing.pb.cc
31+
kUtils.cpp
32+
SelectableItem.cpp
33+
CloudManager.cpp
34+
# SkeletonManager.cpp
35+
)
36+
37+
SET(CMAKE_BUILD_TYPE Release)
38+
39+
ADD_DEFINITIONS(-std=c++0x -O3)
40+
41+
42+
FIND_PACKAGE(Snappy)
43+
FIND_PACKAGE(Protobuf)
44+
FIND_PACKAGE(ZMQ)
45+
FIND_PACKAGE(UUID)
46+
FIND_PACKAGE(RemoteKinect)
47+
48+
49+
INCLUDE_DIRECTORIES(${OSG_INCLUDE_DIR})
50+
INCLUDE_DIRECTORIES(${MXML_INCLUDE_DIR})
51+
INCLUDE_DIRECTORIES(${SPACENAV_INCLUDE_DIR})
52+
INCLUDE_DIRECTORIES(${SNAPPY_INCLUDE_DIR})
53+
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
54+
INCLUDE_DIRECTORIES(${ZMQ_INCLUDE_DIR})
55+
INCLUDE_DIRECTORIES(${REMOTEKINECT_INCLUDE_DIR})
56+
INCLUDE_DIRECTORIES(${CalVR_Plugins_SOURCE_DIR}/calit2/KinectDemo/)
57+
58+
TARGET_LINK_LIBRARIES(${LIB_NAME} ${OSG_LIBRARIES})
59+
TARGET_LINK_LIBRARIES(${LIB_NAME} ${MXML_LIBRARY})
60+
TARGET_LINK_LIBRARIES(${LIB_NAME} ${SNAPPY_LIBRARY})
61+
TARGET_LINK_LIBRARIES(${LIB_NAME} ${PROTOBUF_LIBRARY})
62+
TARGET_LINK_LIBRARIES(${LIB_NAME} ${ZMQ_LIBRARY})
63+
TARGET_LINK_LIBRARIES(${LIB_NAME} ${UUID_LIBRARY})
64+
TARGET_LINK_LIBRARIES(${LIB_NAME} ${SPACENAV_LIBRARY})
65+
TARGET_LINK_LIBRARIES(${LIB_NAME} ${VRPN_LIBRARY})
66+
67+
68+
ADD_CALVR_LIBRARIES(${LIB_NAME})
69+
70+
INSTALL(TARGETS ${LIB_NAME} DESTINATION lib/plugins)

CloudManager.cpp

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
#include "CloudManager.h"
2+
3+
#include <cvrKernel/ComController.h>
4+
#include <sstream>
5+
#include <algorithm>
6+
#include <cstdio>
7+
8+
#include <sys/syscall.h>
9+
#include <sys/stat.h>
10+
11+
using namespace cvr;
12+
using namespace std;
13+
14+
CloudManager::CloudManager()
15+
{
16+
useKColor = true;
17+
should_quit = false;
18+
19+
//initialPointScale = 0.001;
20+
initialPointScale = ConfigManager::getFloat("Plugin.KinectDemo.KinectDefaultOn.KinectPointSize",0.0f);
21+
pgm1 = new osg::Program;
22+
pgm1->setName("Sphere");
23+
std::string shaderPath = ConfigManager::getEntry("Plugin.Points.ShaderPath");
24+
pgm1->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile(shaderPath + "/Sphere.vert")));
25+
pgm1->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile(shaderPath + "/Sphere.frag")));
26+
pgm1->addShader(osg::Shader::readShaderFile(osg::Shader::GEOMETRY, osgDB::findDataFile(shaderPath + "/Sphere.geom")));
27+
pgm1->setParameter(GL_GEOMETRY_VERTICES_OUT_EXT, 4);
28+
pgm1->setParameter(GL_GEOMETRY_INPUT_TYPE_EXT, GL_POINTS);
29+
pgm1->setParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP);
30+
minDistHSV = 700;
31+
maxDistHSV = 5000;
32+
33+
kinectgrp = new osg::Group();
34+
osg::StateSet* state = kinectgrp->getOrCreateStateSet();
35+
state->setAttribute(pgm1);
36+
state->addUniform(new osg::Uniform("pointScale", initialPointScale));
37+
state->addUniform(new osg::Uniform("globalAlpha", 1.0f));
38+
float pscale = 1.0;
39+
osg::Uniform* _scaleUni = new osg::Uniform("pointScale", 1.0f * pscale);
40+
kinectgrp->getOrCreateStateSet()->addUniform(_scaleUni);
41+
42+
_root = new osg::MatrixTransform();
43+
SceneManager::instance()->getObjectsRoot()->addChild(_root);
44+
_root->addChild(kinectgrp);
45+
46+
}
47+
48+
CloudManager::~CloudManager()
49+
{
50+
}
51+
52+
bool CloudManager::isCacheDone()
53+
{
54+
return _cacheDone;
55+
}
56+
57+
void CloudManager::update()
58+
{
59+
static bool frameLoading = false;
60+
bool cDone;
61+
if(true)
62+
{
63+
if(cvr::ComController::instance()->isMaster())
64+
{
65+
cDone = _cacheDone;
66+
int numSlaves = cvr::ComController::instance()->getNumSlaves();
67+
bool sDone[numSlaves];
68+
cvr::ComController::instance()->readSlaves(sDone,sizeof(bool));
69+
for(int i = 0; i < numSlaves; i++)
70+
{
71+
cDone = cDone && sDone[i];
72+
}
73+
cvr::ComController::instance()->sendSlaves(&cDone,sizeof(bool));
74+
}
75+
else
76+
{
77+
cDone = _cacheDone;
78+
cvr::ComController::instance()->sendMaster(&cDone,sizeof(bool));
79+
cvr::ComController::instance()->readMaster(&cDone,sizeof(bool));
80+
}
81+
82+
if(!cDone)
83+
{
84+
//std::cerr << "Waiting for load to finish." << std::endl;
85+
return;
86+
}
87+
88+
cvr::ComController::instance()->sync();
89+
//Add not here?
90+
91+
92+
return;
93+
}
94+
if(cDone)
95+
{
96+
std::cerr << "Load Finished." << std::endl;
97+
//Add loaded node to root
98+
99+
}
100+
else
101+
{
102+
// std::cerr << "Waiting for GPU load finish." << std::endl;
103+
}
104+
105+
}
106+
107+
void CloudManager::run()
108+
{
109+
//Do functions
110+
cerr << ".";
111+
if(kinectgrp != NULL)
112+
{
113+
114+
zmq::context_t context2(1);
115+
cloudT_socket = NULL;
116+
cloudT_socket = new SubSocket<RemoteKinect::PointCloud> (context2, ConfigManager::getEntry("Plugin.KinectDemo.KinectServer.PointCloud"));
117+
118+
packet = new RemoteKinect::PointCloud();
119+
while(!should_quit)
120+
{
121+
122+
//printf(".");
123+
if(cloudT_socket != NULL)
124+
{
125+
// printf("NotNull");
126+
127+
if (cloudT_socket->recv(*packet))
128+
{
129+
float r, g, b, a;
130+
osg::Vec3Array* kinectVertices = new osg::Vec3Array;
131+
// kinectVertices->empty();
132+
osg::Vec3Array* normals = new osg::Vec3Array;
133+
osg::Vec4Array* kinectColours = new osg::Vec4Array;
134+
// kinectColours->empty();
135+
// cerr << ".";
136+
//int size = packet->points_size();
137+
//printf("Points %i\n",size);
138+
if(true)
139+
{
140+
for (int i = 0; i < packet->points_size(); i++)
141+
{
142+
/*
143+
osg::Vec3f ppos((packet->points(i).x() / 1000) + Skeleton::camPos.x(),
144+
(packet->points(i).z() / -1000) + Skeleton::camPos.y(),
145+
(packet->points(i).y() / 1000) + Skeleton::camPos.z());
146+
*/
147+
osg::Vec3f ppos((packet->points(i).x() / 1000),
148+
(packet->points(i).z() / -1000),
149+
(packet->points(i).y() / 1000));
150+
kinectVertices->push_back(ppos);
151+
//useKColor
152+
if (useKColor)
153+
{
154+
r = (packet->points(i).r() / 255.);
155+
g = (packet->points(i).g() / 255.);
156+
b = (packet->points(i).b() / 255.);
157+
a = 1;
158+
kinectColours->push_back(osg::Vec4f(r, g, b, a));
159+
}
160+
else
161+
{
162+
kinectColours->push_back(getColorRGB(packet->points(i).z()));
163+
}
164+
}
165+
//cerr << kinectVertices->size();
166+
167+
osg::Geode* kgeode = new osg::Geode();
168+
kgeode->setCullingActive(false);
169+
osg::Geometry* nodeGeom = new osg::Geometry();
170+
osg::StateSet* state = nodeGeom->getOrCreateStateSet();
171+
nodeGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, kinectVertices->size()));
172+
osg::VertexBufferObject* vboP = nodeGeom->getOrCreateVertexBufferObject();
173+
vboP->setUsage(GL_STREAM_DRAW);
174+
nodeGeom->setUseDisplayList(true);
175+
nodeGeom->setUseVertexBufferObjects(true);
176+
nodeGeom->setVertexArray(kinectVertices);
177+
nodeGeom->setColorArray(kinectColours);
178+
nodeGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
179+
kgeode->addDrawable(nodeGeom);
180+
kgeode->dirtyBound();
181+
//if (kinectgrp != NULL) _root->removeChild(kinectgrp);
182+
kinectgrp->removeChild(0, 1);
183+
kinectgrp->addChild(kgeode);
184+
185+
186+
}
187+
}
188+
if(should_quit)
189+
{
190+
pgm1->ref();
191+
kinectgrp->removeChild(0, 1);
192+
_root->removeChild(kinectgrp);
193+
kinectgrp = NULL;
194+
195+
}
196+
}
197+
}
198+
}
199+
//When Finished
200+
_cacheDone = true;
201+
202+
203+
204+
205+
std::cerr << "All frames loaded." << std::endl;
206+
207+
}
208+
209+
void CloudManager::quit()
210+
{
211+
212+
should_quit = true;
213+
// pgm1->ref();
214+
// kinectgrp->removeChild(0, 1);
215+
// _root->removeChild(kinectgrp);
216+
// kinectgrp = NULL;
217+
218+
}
219+
220+
osg::Vec4f CloudManager::getColorRGB(int dist)
221+
{
222+
if (distanceColorMap.count(dist) == 0) // that can be commented out after precomputing completely if the range of Z is known (and it is set on the server side)
223+
{
224+
float r, g, b;
225+
float h = depth_to_hue(minDistHSV, dist, maxDistHSV);
226+
HSVtoRGB(&r, &g, &b, h, 1, 1);
227+
distanceColorMap[dist] = osg::Vec4f(r, g, b, 1);
228+
}
229+
230+
return distanceColorMap[dist];
231+
}

CloudManager.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//#ifndef _ANIMATION_MANAGER_MULT
2+
//#define _ANIMATION_MANAGER_MULT
3+
4+
#include <osgDB/ReadFile>
5+
#include <osgDB/FileUtils>
6+
#include <cvrKernel/SceneManager.h>
7+
#include <cvrConfig/ConfigManager.h>
8+
#include <cvrKernel/PluginHelper.h>
9+
#include <cvrKernel/CVRPlugin.h>
10+
#include <cvrMenu/MenuCheckbox.h>
11+
#include <cvrMenu/MenuButton.h>
12+
#include <cvrMenu/MenuRangeValue.h>
13+
14+
#include <shared/PubSub.h>
15+
#include <protocol/skeletonframe.pb.h>
16+
#include <protocol/colormap.pb.h>
17+
#include <protocol/depthmap.pb.h>
18+
#include <protocol/pointcloud.pb.h>
19+
#include <zmq.hpp>
20+
#include "kUtils.h"
21+
//#include "Skeleton.h"
22+
#include <unordered_map>
23+
24+
25+
#include <iostream>
26+
#include <string>
27+
#include <map>
28+
29+
#include <OpenThreads/Thread>
30+
31+
32+
// zmq::context_t contextCloud(1);
33+
34+
class CloudManager : public OpenThreads::Thread {
35+
36+
public:
37+
38+
CloudManager();
39+
~CloudManager();
40+
//contextCloud(1);
41+
SubSocket<RemoteKinect::PointCloud>* cloudT_socket;
42+
void update();
43+
bool isCacheDone();
44+
RemoteKinect::PointCloud* packet;
45+
46+
virtual void run();
47+
void quit();
48+
protected:
49+
50+
// osg::ref_ptr<osg::Vec4Array> kinectColours;
51+
// osg::ref_ptr<osg::Vec3Array> kinectVertices;
52+
bool _cacheDone;
53+
bool useKColor;
54+
bool should_quit;
55+
osg::Program* pgm1;
56+
osg::Group* kinectgrp;
57+
osg::MatrixTransform* _root;
58+
float initialPointScale;
59+
int minDistHSV, maxDistHSV;
60+
int minDistHSVDepth, maxDistHSVDepth;
61+
std::unordered_map<float, osg::Vec4f> distanceColorMap;
62+
osg::Vec4f getColorRGB(int dist);
63+
float distanceMIN, distanceMAX;
64+
float _sphereRadius;
65+
};
66+
67+
//#endif

0 commit comments

Comments
 (0)