7 #include "PlusConfigure.h" 21 #include <vtkImageData.h> 22 #include <vtkObjectFactory.h> 27 #define REALSENSE_DEFAULT_FRAME_WIDTH 640 28 #define REALSENSE_DEFAULT_FRAME_HEIGHT 480 29 #define REALSENSE_DEFAULT_FRAME_RATE 30 32 class vtkPlusIntelRealSense::vtkInternal
43 virtual ~vtkInternal()
49 RSFrameConfig(rs2_stream st, std::string sn,
int width,
int height,
int fr)
51 this->StreamType = st;
52 this->SourceName = sn;
57 rs2_stream StreamType;
58 std::string SourceName;
62 unsigned int FrameRate;
66 bool UseRealSenseColorizer =
false;
67 bool AlignDepthStream =
false;
77 rs2::pipeline_profile Profile;
80 PlusStatus SetStreamToAlign(
const std::vector<rs2::stream_profile>& streams);
86 PlusStatus vtkPlusIntelRealSense::vtkInternal::SetDepthScaleToMm(rs2::device dev)
89 for (rs2::sensor& sensor : dev.query_sensors())
92 if (rs2::depth_sensor dpt = sensor.as<rs2::depth_sensor>())
94 this->DepthScaleToMm = dpt.get_depth_scale();
98 LOG_ERROR(
"Failed to get depth scale from Intel RealSense.");
103 PlusStatus vtkPlusIntelRealSense::vtkInternal::SetStreamToAlign(
const std::vector<rs2::stream_profile>& streams)
108 rs2_stream align_to = RS2_STREAM_ANY;
109 bool depth_stream_found =
false;
110 bool color_stream_found =
false;
111 for (rs2::stream_profile sp : streams)
113 rs2_stream profile_stream = sp.stream_type();
114 if (profile_stream != RS2_STREAM_DEPTH)
116 if (!color_stream_found)
117 align_to = profile_stream;
119 if (profile_stream == RS2_STREAM_COLOR)
121 color_stream_found =
true;
126 depth_stream_found =
true;
130 if (!depth_stream_found)
132 LOG_ERROR(
"Failed to align RealSense streams. No Intel RealSense Depth stream available. Please proivde a depth stream, or set AlignDepthStream to FALSE.");
136 if (align_to == RS2_STREAM_ANY)
138 LOG_ERROR(
"Failed to align RealSense streams. No Intel RealSense stream found to align with Depth. Please proivde an RGB stream, or set AlignDepthStream to FALSE");
141 this->AlignTo = align_to;
147 : Internal(new vtkInternal(this))
176 XML_READ_BOOL_ATTRIBUTE_NONMEMBER_OPTIONAL(UseRealSenseColorizer, this->Internal->UseRealSenseColorizer, deviceConfig);
177 XML_READ_BOOL_ATTRIBUTE_NONMEMBER_OPTIONAL(AlignDepthStream, this->Internal->AlignDepthStream, deviceConfig);
179 XML_FIND_NESTED_ELEMENT_REQUIRED(dataSourcesElement, deviceConfig,
"DataSources");
180 for (
int nestedElementIndex = 0; nestedElementIndex < dataSourcesElement->GetNumberOfNestedElements(); nestedElementIndex++)
182 vtkXMLDataElement* dataElement = dataSourcesElement->GetNestedElement(nestedElementIndex);
183 if (STRCASECMP(dataElement->GetName(),
"DataSource") != 0)
189 if (dataElement->GetAttribute(
"Type") != NULL && STRCASECMP(dataElement->GetAttribute(
"Type"),
"Video") == 0)
193 const char* toolId = dataElement->GetAttribute(
"Id");
197 LOG_ERROR(
"Failed to initialize IntelRealSense DataSource: Id is missing");
202 rs2_stream sourceType;
203 XML_READ_ENUM2_ATTRIBUTE_NONMEMBER_REQUIRED(FrameType, sourceType, dataElement,
"RGB", RS2_STREAM_COLOR,
"DEPTH", RS2_STREAM_DEPTH);
205 XML_READ_VECTOR_ATTRIBUTE_NONMEMBER_OPTIONAL(
int, 2, FrameSize, frameSize, dataElement);
207 XML_READ_SCALAR_ATTRIBUTE_NONMEMBER_OPTIONAL(
int, FrameRate, frameRate, dataElement);
208 vtkInternal::RSFrameConfig
source(sourceType, toolId, frameSize[0], frameSize[1], frameRate);
209 this->Internal->VideoSources.push_back(
source);
213 LOG_ERROR(
"DataSource with unknown Type.");
226 if (this->Internal->UseRealSenseColorizer)
228 deviceConfig->SetAttribute(
"UseRealSenseColorizer",
"TRUE");
232 deviceConfig->SetAttribute(
"UseRealSenseColorizer",
"FALSE");
236 if (this->Internal->AlignDepthStream)
238 deviceConfig->SetAttribute(
"AlignDepthStream",
"TRUE");
242 deviceConfig->SetAttribute(
"AlignDepthStream",
"FALSE");
252 this->Internal->Config.disable_all_streams();
253 std::vector<vtkInternal::RSFrameConfig>::iterator it;
254 for (it = begin(this->Internal->VideoSources); it != end(this->Internal->VideoSources); it++)
257 switch (it->StreamType)
259 case RS2_STREAM_COLOR:
260 format = RS2_FORMAT_RGB8;
262 case RS2_STREAM_DEPTH:
263 format = RS2_FORMAT_Z16;
271 this->Internal->Config.enable_stream(
284 this->Internal->Profile = this->Internal->Pipe.start(this->Internal->Config);
288 LOG_ERROR(
"Failed to start IntelRealSense streams. Check your RealSense camera supports the stream types, resolutions & frame rates you are trying to use. RealSense API gave the error: '" 298 if (this->Internal->SetDepthScaleToMm(this->Internal->Profile.get_device()) ==
PLUS_FAIL)
303 this->Internal->Pipe.stop();
311 delete this->Internal->Align;
312 this->Internal->Align =
nullptr;
319 this->FrameNumber = 0;
324 this->Internal->Profile = this->Internal->Pipe.start(this->Internal->Config);
328 LOG_ERROR(
"Failed to start IntelRealSense streams. Check your RealSense camera supports the stream types, resolutions & frame rates you are trying to use. RealSense API gave the error: '" 338 if (this->Internal->AlignDepthStream)
341 if (this->Internal->SetStreamToAlign(this->Internal->Profile.get_streams()) ==
PLUS_FAIL)
345 this->Internal->Align =
new rs2::align(this->Internal->AlignTo);
354 this->Internal->Pipe.stop();
369 rs2::frameset frames = this->Internal->Pipe.wait_for_frames();
372 if (this->Internal->AlignDepthStream)
374 frames = this->Internal->Align->process(frames);
378 std::vector<vtkInternal::RSFrameConfig>::iterator it;
379 for (it = begin(this->Internal->VideoSources); it != end(this->Internal->VideoSources); it++)
382 if (it->Source ==
nullptr)
384 LOG_WARNING(
"vtkPlusIntelRealSense::InternalUpdate Unable to grab video source '" << it->SourceName <<
"'. Skipping frame.");
389 if (it->Source->GetNumberOfItems() == 0 && it->StreamType == RS2_STREAM_COLOR)
391 LOG_INFO(
"setting up color frame");
392 it->Source->SetImageType(US_IMG_RGB_COLOR);
393 it->Source->SetPixelType(VTK_UNSIGNED_CHAR);
394 it->Source->SetNumberOfScalarComponents(3);
395 it->Source->SetInputFrameSize(it->Width, it->Height, 1);
397 else if (it->Source->GetNumberOfItems() == 0 && it->StreamType == RS2_STREAM_DEPTH)
399 if (this->Internal->UseRealSenseColorizer)
402 it->Source->SetImageType(US_IMG_RGB_COLOR);
403 it->Source->SetPixelType(VTK_UNSIGNED_CHAR);
404 it->Source->SetNumberOfScalarComponents(3);
405 it->Source->SetInputFrameSize(it->Width, it->Height, 1);
410 LOG_INFO(
"setting up depth frame");
411 it->Source->SetImageType(US_IMG_BRIGHTNESS);
412 it->Source->SetPixelType(VTK_TYPE_UINT16);
413 it->Source->SetNumberOfScalarComponents(1);
414 it->Source->SetInputFrameSize(it->Width, it->Height, 1);
419 rs2::frame frame = frames.first(it->StreamType);
422 LOG_ERROR(
"Failed to get IntelRealSense frame");
427 if (it->StreamType == RS2_STREAM_COLOR)
429 FrameSizeType frameSizeColor = { it->Width, it->Height, 1 };
430 if (it->Source->AddItem((
void *)frame.get_data(), it->Source->GetInputImageOrientation(), frameSizeColor, VTK_UNSIGNED_CHAR, 3, US_IMG_RGB_COLOR, 0, this->FrameNumber) ==
PLUS_FAIL)
432 LOG_ERROR(
"vtkPlusIntelRealSense::InternalUpdate Unable to send RGB image. Skipping frame.");
436 else if (it->StreamType == RS2_STREAM_DEPTH)
438 FrameSizeType frameSizeDepth = { it->Width, it->Height, 1 };
439 if (this->Internal->UseRealSenseColorizer)
441 rs2::colorizer color;
442 color.set_option(RS2_OPTION_HISTOGRAM_EQUALIZATION_ENABLED, 1);
443 color.set_option(RS2_OPTION_MIN_DISTANCE, 0.6);
444 color.set_option(RS2_OPTION_MAX_DISTANCE, 1.0);
445 rs2::video_frame vfr = color.colorize(frame);
446 if (it->Source->AddItem((
void *)vfr.get_data(), it->Source->GetInputImageOrientation(), frameSizeDepth, VTK_UNSIGNED_CHAR, 3, US_IMG_RGB_COLOR, 0, this->FrameNumber) ==
PLUS_FAIL)
448 LOG_ERROR(
"vtkPlusIntelRealSense::InternalUpdate Unable to send RGB image. Skipping frame.");
454 if (it->Source->AddItem((
void *)frame.get_data(), it->Source->GetInputImageOrientation(), frameSizeDepth, VTK_TYPE_UINT16, 1, US_IMG_BRIGHTNESS, 0, this->FrameNumber) ==
PLUS_FAIL)
456 LOG_ERROR(
"vtkPlusIntelRealSense::InternalUpdate Unable to send DEPTH image. Skipping frame.");
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
virtual PlusStatus InternalConnect()
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING(deviceConfig, rootConfigElement)
#define REALSENSE_DEFAULT_FRAME_RATE
DataSourceContainer VideoSources
bool RequireImageOrientationInConfiguration
Interface class to Intel RealSense cameras.
virtual PlusStatus NotifyConfigured()
PlusStatus InternalStartRecording()
double InternalUpdateRate
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *config)
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement)
PhidgetLCD_Font int * width
#define REALSENSE_DEFAULT_FRAME_HEIGHT
bool StartThreadForInternalUpdates
PhidgetLCD_Font int int * height
virtual void PrintSelf(ostream &os, vtkIndent indent)
vtkStandardNewMacro(vtkPlusIntelRealSense)
#define REALSENSE_DEFAULT_FRAME_WIDTH
virtual PlusStatus InternalDisconnect()
PlusStatus GetVideoSource(const char *aSourceId, vtkPlusDataSource *&aVideoSource)
PlusStatus InternalStopRecording()
virtual PlusStatus InternalUpdate()
PlusStatus WriteConfiguration(vtkXMLDataElement *config)
Interface to a 3D positioning tool, video source, or generalized data stream.