ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Annotaion.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Utils/PCLCloud.h>
11 #include <vtkSmartPointer.h>
12 
13 #include <vector>
14 
15 struct BoxLabel {
17  type = "unknown";
18  this->detail.center_x = this->detail.center_y = this->detail.center_z =
19  0;
20  this->detail.yaw = 2;
21  this->detail.length = this->detail.width = this->detail.height = 1;
22  }
23  BoxLabel(const double p1[3],
24  const double p2[3],
25  std::string type_ = "unknown") {
26  type = type_;
27  this->detail.center_x = (p1[0] + p2[0]) / 2;
28  this->detail.center_y = (p1[1] + p2[1]) / 2;
29  this->detail.center_z = (p1[2] + p2[2]) / 2;
30  this->detail.yaw = 0;
31  this->detail.length = p2[0] - p1[0];
32  this->detail.width = p2[1] - p1[1];
33  this->detail.height = p2[2] - p1[2];
34  }
35  std::string type;
36  union {
37  double data[7];
38  struct {
39  double center_x;
40  double center_y;
41  double center_z;
42  double length;
43  double width;
44  double height;
45  double yaw;
46  } detail;
47  };
48 
49  std::string toString() {
50  char buffer[200];
51  sprintf(buffer, "%s %f %f %f %f %f %f %f", type.c_str(), data[0],
52  data[1], data[2], data[3], data[4], data[5], data[6]);
53  return std::string(buffer);
54  }
55 };
56 
57 class vtkBalloonWidget;
62 
63 class vtkTransform;
64 class vtkRenderWindowInteractor;
65 class vtkActor;
66 class vtkPolyDataMapper;
67 
68 class Annotation {
69  friend class Annotaions;
70 
71 public:
77  Annotation(const std::vector<int>& slice, std::string type_);
78 
85  Annotation(const BoxLabel& label, bool visible_ = true, bool lock_ = false);
86 
93  Annotation(const PointCloudI::Ptr cloud,
94  std::vector<int>& slice,
95  std::string type_);
96 
97  ~Annotation();
98 
104 
110 
116  void picked(vtkRenderWindowInteractor* interactor);
117 
121  void unpicked();
122 
127  void adjustToAnchor();
128 
133  void setType(const std::string value);
135  std::string getType() const;
136 
137  const std::vector<int>& getSlice() const;
138 
139 protected:
140  void initial();
141 
148  void colorAnnotation(int color_index = -1);
149 
155  void setAnchorPoint(const PointCloudI::Ptr cloud,
156  const std::vector<int>& slice);
157 
164  double computeScaleAndCenterShift(double o[3], double scs[2]);
165 
166 private:
167  std::string type;
172 
174  vtkSmartPointer<vtkBoxWidgetCallback0> boxWidgetCallback0;
175  vtkSmartPointer<vtkBoxWidgetCallback1> boxWidgetCallback1;
176 
177  std::vector<double*> anchorPoints;
178 
179  std::vector<int> m_slice;
180 
181  double center[3];
182 
183  // NOTE not used
184  bool visible;
185  bool lock;
186 
187 public:
192  static std::vector<std::string>* GetTypes();
193 
199  static std::size_t GetTypeIndex(std::string type_);
200 
206  static std::string GetTypeByIndex(size_t index);
207 
215  static void ComputeOBB(const PointCloudI::Ptr cloud,
216  std::vector<int>& slice,
217  double p1[3],
218  double p2[3]);
219 
220 protected:
224  static std::vector<std::string>* types;
225 };
226 
227 class Annotaions {
228 public:
229  explicit Annotaions(vtkRenderWindowInteractor* interactor = nullptr);
230 
231  void preserve(size_t num = 0);
232  void release();
233 
234  void add(Annotation* anno);
235  void remove(Annotation* anno);
236  void clear();
237  std::size_t getSize();
238 
239  void updateLabels(Annotation* anno, bool resetFlag = false);
240 
245  void loadAnnotations(std::string filename, int mode);
246 
251  void saveAnnotations(std::string filename, int mode);
252 
253  bool getAnnotations(std::vector<int>& annos) const;
254 
260  Annotation* getAnnotation(vtkActor* actor);
261  Annotation* getAnnotation(std::size_t index);
262  void getAnnotations(const std::string& type,
263  std::vector<Annotation*>& annotations);
264  int getAnnotationIndex(Annotation* anno);
265  std::vector<Annotation*>& getAnnotations();
266 
267  inline int getLabelByIndex(std::size_t index) {
268  if (index >= m_capacity) {
269  return -1;
270  }
271  return m_labeledCloudIndex[index];
272  }
273 
274  void updateBalloonByIndex(std::size_t index);
275  void updateBalloonByAnno(Annotation* anno);
276 
277 protected:
281  std::vector<Annotation*> m_annotations;
282 
283  vtkRenderWindowInteractor* m_interactor;
285 
286  int* m_labeledCloudIndex = nullptr;
287  size_t m_capacity = 0;
288 };
std::string filename
long color_index
char type
int getLabelByIndex(std::size_t index)
Definition: Annotaion.h:267
std::size_t getSize()
Definition: Annotaion.cpp:480
Annotaions(vtkRenderWindowInteractor *interactor=nullptr)
Definition: Annotaion.cpp:406
void loadAnnotations(std::string filename, int mode)
load annotations from file
Definition: Annotaion.cpp:518
void clear()
Definition: Annotaion.cpp:469
void add(Annotation *anno)
Definition: Annotaion.cpp:445
void release()
Definition: Annotaion.cpp:428
void updateBalloonByIndex(std::size_t index)
Definition: Annotaion.cpp:705
size_t m_capacity
Definition: Annotaion.h:287
std::vector< Annotation * > m_annotations
keep all annotation from current cloud
Definition: Annotaion.h:281
Annotation * getAnnotation(vtkActor *actor)
from annotatin box actor to find annotation itself
Definition: Annotaion.cpp:669
void saveAnnotations(std::string filename, int mode)
save annotations to file
Definition: Annotaion.cpp:577
vtkSmartPointer< vtkBalloonWidget > m_balloonWidget
Definition: Annotaion.h:284
void remove(Annotation *anno)
Definition: Annotaion.cpp:458
void preserve(size_t num=0)
Definition: Annotaion.cpp:420
void updateLabels(Annotation *anno, bool resetFlag=false)
Definition: Annotaion.cpp:482
void updateBalloonByAnno(Annotation *anno)
Definition: Annotaion.cpp:717
std::vector< Annotation * > & getAnnotations()
Definition: Annotaion.cpp:667
int * m_labeledCloudIndex
Definition: Annotaion.h:286
vtkRenderWindowInteractor * m_interactor
Definition: Annotaion.h:283
int getAnnotationIndex(Annotation *anno)
Definition: Annotaion.cpp:678
BoxLabel getBoxLabel()
getBoxLabel get boxLabel from annotaion tranformation
Definition: Annotaion.cpp:215
void applyTransform(vtkSmartPointer< vtkTransform > t)
apply transform to annotation
Definition: Annotaion.cpp:231
void picked(vtkRenderWindowInteractor *interactor)
enter picked state, show boxwidget which allow to adjust annotation
Definition: Annotaion.cpp:240
static std::size_t GetTypeIndex(std::string type_)
GetTypeIndex auto add to vector map if has not.
Definition: Annotaion.cpp:362
static std::vector< std::string > * GetTypes()
get types vector pointer
Definition: Annotaion.cpp:354
static std::vector< std::string > * types
types all annotation type here
Definition: Annotaion.h:224
Annotation(const std::vector< int > &slice, std::string type_)
Annotation construct from slice which load from label file.
Definition: Annotaion.cpp:43
void initial()
Definition: Annotaion.cpp:112
vtkSmartPointer< vtkActor > getActor() const
Definition: Annotaion.cpp:333
void colorAnnotation(int color_index=-1)
color the annotation with given color
Definition: Annotaion.cpp:123
void unpicked()
disable boxWidget
Definition: Annotaion.cpp:277
void setAnchorPoint(const PointCloudI::Ptr cloud, const std::vector< int > &slice)
copy selected points as anchor to current annotation
Definition: Annotaion.cpp:167
void adjustToAnchor()
keep current orientation, re-compute the center and scale to make annotation fit to selected point we...
Definition: Annotaion.cpp:283
std::string getType() const
Definition: Annotaion.cpp:320
double computeScaleAndCenterShift(double o[3], double scs[2])
computeScaleAndCenterShift
Definition: Annotaion.cpp:335
const std::vector< int > & getSlice() const
Definition: Annotaion.cpp:322
void setType(const std::string value)
change the type of annotation, and color too
Definition: Annotaion.cpp:324
static std::string GetTypeByIndex(size_t index)
GetTypeByIndex auto add to vector map if has not.
Definition: Annotaion.cpp:372
static void ComputeOBB(const PointCloudI::Ptr cloud, std::vector< int > &slice, double p1[3], double p2[3])
ComputeOBB compute max,min [x,y,z] aligned to xyz axis.
Definition: Annotaion.cpp:381
The vtkBoxWidgetRestricted class vtkBoxWidgetRestricted restricts the rotation with Z axis.
std::string toString()
Definition: Annotaion.h:49
BoxLabel(const double p1[3], const double p2[3], std::string type_="unknown")
Definition: Annotaion.h:23
double width
Definition: Annotaion.h:43
double height
Definition: Annotaion.h:44
double length
Definition: Annotaion.h:42
struct BoxLabel::@53::@55 detail
double center_y
Definition: Annotaion.h:40
BoxLabel()
Definition: Annotaion.h:16
double center_x
Definition: Annotaion.h:39
double yaw
Definition: Annotaion.h:45
double center_z
Definition: Annotaion.h:41
double data[7]
Definition: Annotaion.h:37
std::string type
Definition: Annotaion.h:35