13 #include <OpenGL/gl.h> 16 #include <hrpModel/ModelLoaderUtil.h> 17 #include "hrpsys/util/Project.h" 18 #include "hrpsys/util/VectorConvert.h" 19 #include "hrpsys/util/GLcamera.h" 20 #include "hrpsys/util/GLbody.h" 21 #include "hrpsys/util/GLlink.h" 22 #include "hrpsys/util/GLutil.h" 31 "implementation_id",
"VirtualCamera",
32 "type_name",
"VirtualCamera",
33 "description",
"virtual camera component",
34 "version", HRPSYS_PACKAGE_VERSION,
36 "category",
"example",
37 "activity_type",
"DataFlowComponent",
40 "lang_type",
"compile",
42 "conf.default.rangerMaxAngle",
"0.25",
43 "conf.default.rangerMinAngle",
"-0.25",
44 "conf.default.rangerAngularRes",
"0.01",
45 "conf.default.rangerMaxRange",
"5.0",
46 "conf.default.rangerMinRange",
"0.5",
47 "conf.default.generateRange",
"1",
48 "conf.default.generatePointCloud",
"0",
49 "conf.default.generatePointCloudStep",
"1",
50 "conf.default.pcFormat",
"xyz",
51 "conf.default.generateMovie",
"0",
52 "conf.default.debugLevel",
"0",
53 "conf.default.project",
"",
54 "conf.default.camera",
"",
63 m_sceneStateIn(
"state", m_sceneState),
64 m_basePosIn(
"basePos", m_basePos),
65 m_baseRpyIn(
"baseRpy", m_baseRpy),
67 m_imageOut(
"image", m_image),
68 m_rangeOut(
"range", m_range),
69 m_cloudOut(
"cloud", m_cloud),
70 m_poseSensorOut(
"poseSensor", m_poseSensor),
73 m_window(&m_scene, &m_log),
75 m_generateRange(true),
76 m_generatePointCloud(false),
77 m_generateMovie(false),
78 m_isGeneratingMovie(false),
94 std::cout <<
m_profile.instance_name <<
": onInitialize()" << std::endl;
164 std::cout <<
m_profile.instance_name<<
": onActivated(" << ec_id <<
")" << std::endl;
170 CORBA::Object_ptr
obj =
naming.resolve(
"ModelLoader");
171 if (!CORBA::is_nil(obj)){
172 std::cout <<
"found ModelLoader on localhost:2809" << std::endl;
174 std::string nameServer = rtcManager.
getConfig()[
"corba.nameservers"];
175 int comPos = nameServer.find(
",");
177 comPos = nameServer.length();
179 nameServer = nameServer.substr(0, comPos);
180 naming.init(nameServer.c_str());
184 std::cerr <<
"Project file is not specified." << std::endl;
185 return RTC::RTC_ERROR;
192 std::string bodyName, cameraName;
193 if (pos == std::string::npos){
194 std::cerr <<
"can't find a separator in the camera name" << std::endl;
195 return RTC::RTC_ERROR;
199 std::cout <<
"body:" << bodyName <<
", camera:" << cameraName << std::endl;
202 std::vector<std::pair<std::string, OpenHRP::BodyInfo_var> > binfos;
205 for (std::map<std::string, ModelItem>::iterator it=prj.
models().begin();
206 it != prj.
models().end(); it++){
208 OpenHRP::BodyInfo_var binfo;
209 OpenHRP::ModelLoader::ModelLoadOption opt;
210 opt.readImage =
true;
211 opt.AABBdata.length(0);
212 opt.AABBtype = OpenHRP::ModelLoader::AABB_NUM;
213 binfo = ml->getBodyInfoEx(it->second.url.c_str(), opt);
214 if (CORBA::is_nil(binfo)){
215 std::cerr <<
"failed to load model[" << it->second.url <<
"]" 217 return RTC::RTC_ERROR;
221 if (it->first == bodyName){
222 OpenHRP::LinkInfoSequence_var lis = binfo->links();
223 for (
unsigned int i=0;
i<lis->length();
i++){
224 const OpenHRP::SensorInfoSequence& sensors = lis[
i].sensors;
225 for (
unsigned int j=0;
j<sensors.length();
j++){
226 const OpenHRP::SensorInfo& si = sensors[
j];
227 std::string
type(si.type);
228 std::string
name(si.name);
229 if (type ==
"Vision" && name == cameraName){
230 w = si.specValues[4];
231 h = si.specValues[5];
237 std::cout <<
"invalid image size:" << w <<
"x" <<
h << std::endl;
238 return RTC::RTC_ERROR;
241 binfos.push_back(std::make_pair(it->first, binfo));
246 for (
unsigned int i=0;
i<binfos.size();
i++){
251 body->setName(binfos[
i].first);
252 m_scene.WorldBase::addBody(body);
255 if (binfos[
i].first == bodyName){
260 std::cerr <<
"can't find a robot named " << bodyName << std::endl;
264 std::cerr <<
"VirtualCamera: can't find camera(" 265 << cameraName <<
")" << std::endl;
266 return RTC::RTC_ERROR;
272 m_image.data.image.format = Img::CF_RGB;
280 std::cout <<
m_profile.instance_name<<
": onDeactivated(" << ec_id <<
")" << std::endl;
284 void capture(
int w,
int h,
unsigned char *o_buffer)
286 glReadBuffer(GL_BACK);
287 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
288 for (
int i=0;
i<
h;
i++){
289 glReadPixels(0,(h-1-
i),w,1,GL_RGB,GL_UNSIGNED_BYTE,
296 if (
m_debugLevel > 0) std::cout <<
m_profile.instance_name<<
": onExecute(" << ec_id <<
") " << std::endl;
305 std::string
name(state.name);
310 state.basePose.position.y,
311 state.basePose.position.z);
313 state.basePose.orientation.p,
314 state.basePose.orientation.y);
339 for (std::map<std::string, RTCGLbody *>::iterator it=
m_bodies.begin();
356 R(0,0) = T[0];
R(0,1) = T[4];
R(0,2) = T[8];
357 R(1,0) = T[1];
R(1,1) = T[5];
R(1,2) = T[9];
358 R(2,0) = T[2];
R(2,1) = T[6];
R(2,2) = T[10];
374 std::string fname(
m_profile.instance_name);
378 CV_FOURCC(
'D',
'I',
'V',
'X'),
389 dst[
i*3 ] =
m_image.data.image.raw_data[
i*3+2];
390 dst[
i*3+1] =
m_image.data.image.raw_data[
i*3+1];
391 dst[
i*3+2] =
m_image.data.image.raw_data[
i*3 ];
411 std::cout <<
"VirtualCamera::onExecute() : total:" 414 std::cout <<
", render:" 419 std::cout <<
", range2d:" << dt.
sec()*1e3+dt.
usec()/1e3;
423 std::cout <<
", range3d:" << dt.
sec()*1e3+dt.
usec()/1e3;
425 std::cout <<
"[ms]" << std::endl;
436 glReadPixels(0, h/2, w, 1, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
440 RangerConfig &rc =
m_range.config;
441 double max = ((
int)(fovx/2/rc.angularRes))*rc.angularRes;
442 if (rc.maxAngle > max) rc.maxAngle =
max;
443 if (rc.minAngle < -max) rc.minAngle = -
max;
444 unsigned int nrange = round((rc.maxAngle - rc.minAngle)/rc.angularRes)+1;
447 #define THETA(x) (-atan(((x)-w/2)*2*tan(fovx/2)/w)) 448 #define RANGE(d,th) (-far*near/((d)*(far-near)-far)/cos(th)) 449 double dth, alpha, th, th_old =
THETA(w-1);
450 double r, r_old =
RANGE(depth[w-1], th_old);
453 for (
unsigned int i=0;
i<nrange;
i++){
454 angle = rc.minAngle + rc.angularRes*
i;
457 r =
RANGE(depth[idx], th);
462 alpha = atan2(r*sin(dth), fabs(r_old - r*cos(dth)));
464 if (r != 0 && r_old != 0 && alpha > 0.01){
465 m_range.ranges[
i] = ((th - angle)*r_old + (angle - th_old)*r)/dth;
466 }
else if (th - angle < dth/2){
472 ||
m_range.ranges[
i] < rc.minRange){
483 std::cout << angle <<
" " << depth[idx] <<
" " <<
m_range.ranges[
i] <<
" " 484 <<
m_range.ranges[
i]*cos(angle) <<
" " <<
m_range.ranges[
i]*sin(angle) << std::endl;
497 bool colored =
false;
504 std::cerr <<
"unknown point cloud format:[" <<
m_pcFormat <<
"]" << std::endl;
508 m_cloud.fields[0].data_type = PointCloudTypes::FLOAT32;
512 m_cloud.fields[1].data_type = PointCloudTypes::FLOAT32;
516 m_cloud.fields[2].data_type = PointCloudTypes::FLOAT32;
521 m_cloud.fields[3].data_type = PointCloudTypes::UINT8;
525 m_cloud.fields[4].data_type = PointCloudTypes::UINT8;
529 m_cloud.fields[5].data_type = PointCloudTypes::UINT8;
540 double zs = w/(2*tan(fovx/2));
541 unsigned int npoints=0;
542 float *
ptr = (
float *)
m_cloud.data.get_buffer();
543 unsigned char *rgb =
m_image.data.image.raw_data.get_buffer();
544 glReadPixels(0,0, w, h, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
547 float d = depth[
i*w+
j];
551 ptr[2] = far*near/(d*(far-near)-far);
552 ptr[0] = -(
j-w/2)*ptr[2]/zs;
553 ptr[1] = -(
i-h/2)*ptr[2]/zs;
555 unsigned char *
c = (
unsigned char *)(ptr + 3);
556 int offset = ((h-1-
i)*w+
j)*3;
558 c[1] = rgb[offset+1];
559 c[2] = rgb[offset+2];
611 RTC::Create<VirtualCamera>,
612 RTC::Delete<VirtualCamera>);
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
bool m_generatePointCloud
double * getAbsTransform()
RTC::TimedOrientation3D m_baseRpy
void VirtualCameraInit(RTC::Manager *manager)
std::map< std::string, RTCGLbody * > m_bodies
InPort< RTC::TimedOrientation3D > m_baseRpyIn
png_infop png_charp png_int_32 png_int_32 int * type
VirtualCamera(RTC::Manager *manager)
Constructor.
void showFloorGrid(bool flag)
virtual ~VirtualCamera()
Destructor.
virtual RTC::ReturnCode_t onInitialize()
HRPMODEL_API OpenHRP::ModelLoader_var getModelLoader(CosNaming::NamingContext_var cxt)
void setPosition(double x, double y, double z)
bool parse(const std::string &filename)
void capture(int w, int h, unsigned char *o_buffer)
png_infop png_charpp name
bool init(int w=0, int h=0, bool resizable=true)
HRPMODEL_API bool loadBodyFromBodyInfo(BodyPtr body, OpenHRP::BodyInfo_ptr bodyInfo, bool loadGeometryForCollisionDetection=false, Link *(*f)()=NULL)
void loadShapeFromBodyInfo(GLbody *body, BodyInfo_var i_binfo, GLshape *(*shapeFactory)())
OutPort< RangeData > m_rangeOut
coil::Properties & getProperties()
static Manager & instance()
Img::TimedCameraImage m_image
bool addOutPort(const char *name, OutPortBase &outport)
void setRotation(const double *R)
InPort< RTC::TimedPoint3D > m_basePosIn
int gettimeofday(struct timeval *tv, struct timezone *tz)
coil::Properties & getConfig()
hrp::Link * GLlinkFactory()
ExecutionContextHandle_t UniqueId
int m_generatePointCloudStep
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
def j(str, encoding="cp932")
OutPort< Img::TimedCameraImage > m_imageOut
GLcamera * findCamera(const char *i_name)
std::map< std::string, ModelItem > & models()
CvVideoWriter * m_videoWriter
HRP_UTIL_EXPORT Vector3 rpyFromRot(const Matrix33 &m)
InPort< OpenHRP::SceneState > m_sceneStateIn
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
void setPosture(const double *i_angles)
void setCamera(GLcamera *i_camera)
OutPort< TimedPose3D > m_poseSensorOut
virtual bool write(DataType &value)
OpenHRP::SceneState m_sceneState
PointCloudTypes::PointCloud m_cloud
OutPort< PointCloudTypes::PointCloud > m_cloudOut
static const char * virtualcamera_spec[]
InPort< RTC::TimedDoubleSeq > m_qIn
bool addInPort(const char *name, InPortBase &inport)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
RTC::TimedPoint3D m_basePos
std::string m_projectName
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)