// Enable depth maps by enabling the Range component and setting the appropriate pixel format.camera.ComponentSelector.SetValue(ComponentSelector_Range);camera.ComponentEnable.SetValue(true);camera.PixelFormat.SetValue(PixelFormat_Coord3D_C16);// Query the conversion factor required to convert gray values to distances:// Choose the z axis first...camera.Scan3dCoordinateSelector.SetValue(Scan3dCoordinateSelector_CoordinateC);// ... then retrieve the conversion factor.constautogray2mm=camera.Scan3dCoordinateScale.GetValue();// Configure the gray value used for indicating missing depth data.// Note: Before setting the value, the Scan3dCoordinateSelector parameter must be set to the axis the// value is to be configured for, in this case the z axis. This means that Scan3dCoordianteSelector must be set// to "CoordinateC". This has already been done a few lines above.camera.Scan3dInvalidDataValue.SetValue((double)missingDepth);// Retrieve calibration data from the camera.constautocx=camera.Scan3dPrincipalPointU.GetValue();constautocy=camera.Scan3dPrincipalPointV.GetValue();constautof=camera.Scan3dFocalLength.GetValue();// ....// Access the data.constautocontainer=ptrGrabResult->GetDataContainer();constautorangeComponent=container.GetDataComponent(0);constautowidth=rangeComponent.GetWidth();constautoheight=rangeComponent.GetHeight();// Calculate coordinates for pixel (u,v).constuint16_tg=((uint16_t*)rangeComponent.GetData())[u+v*width];constdoublez=g*gray2mm;constdoublex=(u-cx)*z/f;constdoubley=(v-cy)*z/f;
constintminDepth=(int)m_camera.DepthMin.GetValue();constintmaxDepth=(int)m_camera.DepthMax.GetValue();constdoublescale=65536.0/(maxDepth-minDepth);foreachpixel{// Set depth either to the corresponding z value or// a distance value calculated from the z value.// Clip depth if required.if(depth<minDepth)depth=minDepth;elseif(depth>maxDepth)depth=maxDepth;// Compute RGB values.constuint16_tg=(uint16_t)((depth-minDepth)*scale);constuint16_tval=g>>6&0xff;constuint16_tsel=g>>14;uint32_tres=val<<8|0xff;if(sel&0x01){res=(~res)>>8&0xffff;}if(sel&0x02){res=res<<8;}constuint8_tr=res&0xff;res=res>>8;constuint8_tg=res&0xff;res=res>>8;constuint8_tb=res&0xff;}