11 #pragma warning(disable : 4996)
32 #include <tbb/parallel_for.h>
36 #include <vtkRenderWindowInteractor.h>
42 #define DEFAULT_POINT 0
43 #define SELECTED_POINT -1
71 m_currPickedAnnotation =
nullptr;
75 m_cloudLabel =
nullptr;
79 std::ifstream input(file.c_str(), std::ios_base::in);
81 CVLog::Error(tr(
"Cannot open file : %1").arg(file.c_str()));
85 std::vector<std::string> labels;
87 while (input >> value) {
88 if (value ==
"")
continue;
89 labels.push_back(value);
92 tr(
"load %1 classes from %2").arg(labels.size()).arg(file.c_str()));
98 const std::vector<std::string>& labelList) {
99 if (labelList.empty())
return;
102 std::vector<int> tempIndex;
103 for (
auto anno : m_annoManager->getAnnotations()) {
109 for (
size_t i = 0; i < labelList.size(); ++i) {
110 if (labelList[i] ==
"") {
119 std::vector<Annotation*> toBeRemovedAnnotations;
120 for (
auto anno : m_annoManager->getAnnotations()) {
123 toBeRemovedAnnotations.push_back(anno);
126 .arg(anno->getType().c_str()));
131 m_annoManager->updateBalloonByIndex(index);
132 labelCloudByAnnotation(anno);
136 for (
size_t i = 0; i < toBeRemovedAnnotations.size(); ++i) {
144 if (!m_viewer)
return;
150 std::vector<std::string>& labelList) {
162 return m_annoManager && m_annoManager->getAnnotations(annos);
172 if (this->m_baseCloud->size() < 1) {
178 m_baseCloudId = cloud->
getViewId().toStdString();
179 vtkActor* modelActor = m_viewer->
getActorById(m_baseCloudId);
181 modelActor->SetVisibility(0);
185 m_annoManager->preserve(this->m_baseCloud->size());
190 QString annoName = fileInfo.baseName();
192 annoName = fileInfo.baseName() +
".boxes";
194 annoName = fileInfo.baseName() +
".labels";
197 QDir dir(fileInfo.absolutePath());
200 QString classesFile = dir.absoluteFilePath(
"Classets.classes");
201 if (!(QFile::exists(classesFile) &&
204 this->loadDefaultClasses();
211 m_annoManager->loadAnnotations(m_annotationFileName,
215 .arg(m_annotationFileName.c_str())
216 .arg(m_annoManager->getSize()));
219 .arg(m_annotationFileName.c_str())
220 .arg(m_annoManager->getSize()));
229 void PclAnnotationTool::refresh() {
230 m_cloudLabel =
new int[m_baseCloud->size()];
231 memset(m_cloudLabel,
DEFAULT_POINT, m_baseCloud->size() *
sizeof(
int));
232 labelCloudByAnnotations();
235 m_colorHandler.
setLabel(m_cloudLabel);
236 m_viewer->addPointCloud<
PointIntensity>(m_baseCloud, m_colorHandler,
237 m_annotationCloudId, 0);
253 connect(m_viewer, &PCLVis::interactorPickedEvent,
this,
255 connect(m_viewer, &PCLVis::interactorKeyboardEvent,
this,
257 connect(m_viewer, &PCLVis::interactorAreaPickedEvent,
this,
271 disconnect(m_viewer, &PCLVis::interactorPickedEvent,
this,
273 disconnect(m_viewer, &PCLVis::interactorKeyboardEvent,
this,
275 disconnect(m_viewer, &PCLVis::interactorAreaPickedEvent,
this,
281 CVLog::Print(
"unregister annotations tool successfully");
286 m_viewer =
reinterpret_cast<PCLVis*
>(viewer);
289 "[PclAnnotationTool::setVisualizer] viewer is Null!");
292 CVLog::Warning(
"[PclAnnotationTool::setVisualizer] viewer is Null!");
299 m_intersectMode =
true;
305 m_intersectMode =
false;
311 m_intersectMode =
false;
317 m_intersectMode =
false;
326 const std::vector<int>& new_selected_slice) {
327 if (new_selected_slice.empty() || !m_viewer)
return;
335 std::vector<int> selected_slice;
336 filterPickedSlice(new_selected_slice, selected_slice, skip);
337 if (selected_slice.empty())
return;
339 if (!m_last_selected_slice.empty()) {
340 defaultColorPoint(m_last_selected_slice);
343 if (s && c || m_intersectMode) {
345 m_last_selected_slice, selected_slice);
346 }
else if (s || m_unionMode) {
347 m_last_selected_slice =
349 }
else if (c || m_trimMode) {
350 m_last_selected_slice =
353 m_last_selected_slice = selected_slice;
356 highlightPoint(m_last_selected_slice);
361 if (m_currPickedAnnotation) {
363 m_currPickedAnnotation =
nullptr;
371 m_currPickedAnnotation = m_annoManager->getAnnotation(actor);
372 if (m_currPickedAnnotation) {
382 if (symKey ==
"Delete") {
384 if (m_currPickedAnnotation) {
386 tr(
"remove box annotation [%1] with %2 points")
387 .arg(m_currPickedAnnotation->
getType().c_str())
388 .arg(m_currPickedAnnotation->
getSlice()
394 if (!m_annoManager->getAnnotations().empty()) {
395 Annotation* anno = m_annoManager->getAnnotations().back();
396 CVLog::Print(tr(
"remove last annotation [%1] with %2 points")
410 void PclAnnotationTool::filterPickedSlice(
const std::vector<int>& inSlices,
411 std::vector<int>& outSlices,
413 if (!outSlices.empty()) {
417 for (
auto&
x : inSlices) {
419 outSlices.push_back(
x);
424 void PclAnnotationTool::fastLabelCloud(
const std::vector<int>& inSlices,
426 int num =
static_cast<int>(inSlices.size());
427 if (m_cloudLabel && num != 0) {
429 tbb::parallel_for(0, num,
434 #pragma omp parallel
for
436 for (
int dataIndex = 0; dataIndex < num; ++dataIndex)
438 { m_cloudLabel[inSlices[dataIndex]] = label; }
447 if (m_last_selected_slice.size() > 3) {
448 createAnnotationFromSelectPoints(
type);
452 if (m_currPickedAnnotation) {
454 tr(
"Change current picked annotation type from [%1] to "
456 .arg(m_currPickedAnnotation->
getType().c_str(),
461 tr(
"No box picked now! Please pick one box and try "
465 std::vector<Annotation*>& annos = m_annoManager->getAnnotations();
466 if (!annos.empty() && annos.back()->getType() !=
type) {
467 CVLog::Print(tr(
"Change last annotation type from [%1] to [%2]")
468 .arg(annos.back()->getType().c_str(),
473 tr(
"No annotation exists now! Please create one and "
482 m_lastSelectedAnnotations.clear();
483 m_annoManager->getAnnotations(
type, m_lastSelectedAnnotations);
484 if (m_lastSelectedAnnotations.empty()) {
489 m_last_selected_slice.clear();
490 for (
Annotation* anno : m_lastSelectedAnnotations) {
492 m_last_selected_slice.insert(m_last_selected_slice.end(),
493 anno->getSlice().begin(),
494 anno->getSlice().end());
495 m_annoManager->remove(anno);
499 if (m_last_selected_slice.empty()) {
503 highlightPoint(m_last_selected_slice);
510 const std::string&
type) {
514 m_annoManager->updateBalloonByAnno(anno);
515 m_annoManager->updateLabels(anno,
false);
521 m_annoManager->saveAnnotations(m_annotationFileName,
int(
m_annotationMode));
522 CVLog::Print(tr(
"Annotations file has been saved to %1")
526 void PclAnnotationTool::createAnnotationFromSelectPoints(std::string
type) {
527 if (m_last_selected_slice.size() > 3) {
535 m_annoManager->add(anno);
536 m_last_selected_slice.clear();
541 "No points selected or selected points number is less than 3"));
545 void PclAnnotationTool::labelCloudByAnnotations() {
546 if (!m_cloudLabel)
return;
547 for (
auto anno : m_annoManager->getAnnotations()) {
548 labelCloudByAnnotation(anno);
552 void PclAnnotationTool::labelCloudByAnnotation(
const Annotation* anno) {
553 if (!anno || anno->
getSlice().empty())
return;
557 void PclAnnotationTool::resetCloudByAnnotation(
const Annotation* anno) {
558 if (!m_viewer || !m_viewer->contains(m_annotationCloudId) || !anno ||
566 void PclAnnotationTool::updateCloudLabel(
const std::string&
type) {
567 if (!m_cloudLabel || m_last_selected_slice.size() < 1)
return;
572 void PclAnnotationTool::loadDefaultClasses() {
573 std::vector<std::string> labels;
575 labels.push_back(it.second);
583 void PclAnnotationTool::highlightPoint(std::vector<int>& slice) {
584 if (slice.size() < 1)
return;
589 void PclAnnotationTool::defaultColorPoint(std::vector<int>& slice) {
590 if (!m_viewer || !m_viewer->contains(m_annotationCloudId))
return;
595 m_baseCloud->size() *
sizeof(
int));
604 if (!m_viewer || m_viewer->contains(m_annotationCloudId)) {
605 m_viewer->updatePointCloud<
PointIntensity>(m_baseCloud, m_colorHandler,
606 m_annotationCloudId);
611 void PclAnnotationTool::setPointSize(
const std::string& viewID,
int viewport) {
612 if (!m_viewer)
return;
613 m_viewer->setPointCloudRenderingProperties(
614 pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, viewID, viewport);
618 for (
auto anno : m_annoManager->getAnnotations()) {
624 for (
auto anno : m_annoManager->getAnnotations()) {
630 if (!m_viewer)
return;
632 vtkActor* originActor = m_viewer->
getActorById(m_baseCloudId);
634 originActor->SetVisibility(1);
637 vtkActor* annotatedActor = m_viewer->
getActorById(m_annotationCloudId);
638 if (annotatedActor) {
639 annotatedActor->SetVisibility(0);
644 vtkActor* originActor = m_viewer->
getActorById(m_baseCloudId);
646 originActor->SetVisibility(0);
649 vtkActor* annotatedActor = m_viewer->
getActorById(m_annotationCloudId);
650 if (annotatedActor) {
651 annotatedActor->SetVisibility(1);
657 m_currPickedAnnotation =
nullptr;
658 m_last_selected_slice.clear();
662 if (!m_viewer)
return;
664 m_viewer->removePointCloud(m_annotationCloudId);
666 m_annoManager->release();
670 vtkActor* modelActor = m_viewer->
getActorById(m_baseCloudId);
672 modelActor->SetVisibility(1);
677 delete[] m_cloudLabel;
678 m_cloudLabel =
nullptr;
684 if (!m_viewer)
return;
687 labelCloudByAnnotation(anno);
692 m_annoManager->clear();
693 std::vector<int> temp;
694 defaultColorPoint(temp);
700 m_annoManager->remove(anno);
704 if (m_currPickedAnnotation) {
706 m_currPickedAnnotation =
nullptr;
709 resetCloudByAnnotation(anno);
712 if (!m_viewer)
return;
pcl::PointXYZI PointIntensity
pcl::PointCloud< PointIntensity > PointCloudI
void picked(vtkRenderWindowInteractor *interactor)
enter picked state, show boxwidget which allow to adjust annotation
static std::size_t GetTypeIndex(std::string type_)
GetTypeIndex auto add to vector map if has not.
static std::vector< std::string > * GetTypes()
get types vector pointer
vtkSmartPointer< vtkActor > getActor() const
void unpicked()
disable boxWidget
std::string getType() const
const std::vector< int > & getSlice() const
void setType(const std::string value)
change the type of annotation, and color too
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
static bool Print(const char *format,...)
Prints out a formatted message in console.
static bool Error(const char *format,...)
Display an error dialog with formatted message.
void setLabel(int *value)
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Set the input cloud to be used.
void addActorToRenderer(const vtkSmartPointer< vtkProp > &actor, int viewport=0)
void setAreaPickingEnabled(bool state)
vtkSmartPointer< vtkRenderWindowInteractor > getRenderWindowInteractor()
Get a pointer to the current interactor style used.
void setPointPickingEnabled(bool state)
bool removeActorFromRenderer(const vtkSmartPointer< vtkProp > &actor, int viewport=0)
Internal method. Adds a vtk actor to screen.
void setActorPickingEnabled(bool state)
vtkActor * getActorById(const std::string &viewId)
CC to PCL cloud converter.
PCLCloud::Ptr getAsSM(std::list< std::string > &requested_fields) const
QString getViewId() const
QString getFullPath() const
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
Generic visualizer 3D interface.
static std::map< size_t, std::string > SemanticMap