fw4spl
fwData/src/fwData/Landmarks.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwData/Landmarks.hpp"
8 
9 #include "fwData/Exception.hpp"
10 #include "fwData/registry/macros.hpp"
11 
12 #include <fwCom/Signal.hpp>
13 #include <fwCom/Signal.hxx>
14 
15 fwDataRegisterMacro( ::fwData::Landmarks );
16 
17 namespace fwData
18 {
19 
20 const ::fwCom::Signals::SignalKeyType Landmarks::s_GROUP_ADDED_SIG = "groupAdded";
21 const ::fwCom::Signals::SignalKeyType Landmarks::s_GROUP_REMOVED_SIG = "groupemoved";
22 const ::fwCom::Signals::SignalKeyType Landmarks::s_POINT_ADDED_SIG = "pointAdded";
23 const ::fwCom::Signals::SignalKeyType Landmarks::s_POINT_REMOVED_SIG = "pointRemoved";
24 const ::fwCom::Signals::SignalKeyType Landmarks::s_POINT_INSERTED_SIG = "pointInserted";
25 const ::fwCom::Signals::SignalKeyType Landmarks::s_GROUP_MODIFIED_SIG = "groupModified";
26 const ::fwCom::Signals::SignalKeyType Landmarks::s_POINT_MODIFIED_SIG = "pointModified";
27 const ::fwCom::Signals::SignalKeyType Landmarks::s_GROUP_RENAMED_SIG = "groupRenamed";
28 const ::fwCom::Signals::SignalKeyType Landmarks::s_POINT_SELECTED_SIG = "pointSelected";
29 const ::fwCom::Signals::SignalKeyType Landmarks::s_POINT_DESELECTED_SIG = "pointDeselected";
30 
31 //------------------------------------------------------------------------------
32 
34 {
35  newSignal<GroupAddedSignalType>(s_GROUP_ADDED_SIG);
36  newSignal<GroupRemovedSignalType>(s_GROUP_REMOVED_SIG);
37  newSignal<PointAddedSignalType>(s_POINT_ADDED_SIG);
38  newSignal<PointRemovedSignalType>(s_POINT_REMOVED_SIG);
39  newSignal<PointInsertedSignalType>(s_POINT_INSERTED_SIG);
40  newSignal<GroupModifiedSignalType>(s_GROUP_MODIFIED_SIG);
41  newSignal<PointModifiedSigType>(s_POINT_MODIFIED_SIG);
42  newSignal<GroupRenamedSignalType>(s_GROUP_RENAMED_SIG);
43  newSignal<PointSelectedSignalType>(s_POINT_SELECTED_SIG);
44  newSignal<PointDeselectedSignalType>(s_POINT_DESELECTED_SIG);
45 }
46 
47 //------------------------------------------------------------------------------
48 
50 {
51 }
52 
53 //------------------------------------------------------------------------------
54 
55 void Landmarks::shallowCopy(const Object::csptr& _source )
56 {
57  Landmarks::csptr other = Landmarks::dynamicConstCast(_source);
58  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
59  "Unable to copy" + (_source ? _source->getClassname() : std::string("<NULL>"))
60  + " to " + this->getClassname()), !bool(other) );
61  this->fieldShallowCopy( _source );
62 
63  m_landmarks = other->m_landmarks;
64 }
65 
66 //------------------------------------------------------------------------------
67 
68 void Landmarks::cachedDeepCopy(const Object::csptr& _source, DeepCopyCacheType& cache)
69 {
70  Landmarks::csptr other = Landmarks::dynamicConstCast(_source);
71  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
72  "Unable to copy" + (_source ? _source->getClassname() : std::string("<NULL>"))
73  + " to " + this->getClassname()), !bool(other) );
74  this->fieldDeepCopy( _source, cache );
75 
76  m_landmarks = other->m_landmarks;
77 }
78 
79 //------------------------------------------------------------------------------
80 
81 void Landmarks::addGroup(const std::string& name, const Landmarks::ColorType& color, const Landmarks::SizeType size,
82  const Landmarks::Shape shape, const bool visibility)
83 {
84  LandmarksGroup group(color, size, shape, visibility);
85  const auto iter = m_landmarks.find(name);
86  FW_RAISE_EXCEPTION_IF(::fwData::Exception("Group '" + name + "' already exists"), iter != m_landmarks.end());
87  m_landmarks.insert(std::make_pair(name, group));
88 }
89 
90 //------------------------------------------------------------------------------
91 
92 const Landmarks::GroupNameContainer Landmarks::getGroupNames() const
93 {
94  Landmarks::GroupNameContainer names;
95 
96  std::transform(
97  m_landmarks.begin(),
98  m_landmarks.end(),
99  std::back_inserter(names),
100  [](const LandmarksContainer::value_type& pair){return pair.first; });
101 
102  return names;
103 }
104 
105 //------------------------------------------------------------------------------
106 
107 const Landmarks::LandmarksGroup& Landmarks::getGroup(const std::string& name) const
108 {
109  const auto iter = m_landmarks.find(name);
110  FW_RAISE_EXCEPTION_IF(::fwData::Exception("Group '" + name + "' does not exist"), iter == m_landmarks.end());
111  const Landmarks::LandmarksGroup& group = iter->second;
112  return group;
113 }
114 
115 //------------------------------------------------------------------------------
116 
118 {
119  auto iter = m_landmarks.find(name);
120  FW_RAISE_EXCEPTION_IF(::fwData::Exception("Group '" + name + "' does not exist"), iter == m_landmarks.end());
121 
122  Landmarks::LandmarksGroup& group = iter->second;
123  return group;
124 }
125 
126 //------------------------------------------------------------------------------
127 
128 void Landmarks::renameGroup(const std::string& oldName, const std::string& newName)
129 {
130  const auto iter = m_landmarks.find(oldName);
131  FW_RAISE_EXCEPTION_IF(::fwData::Exception("Group '" + oldName + "' does not exist"), iter == m_landmarks.end());
132  const auto iter2 = m_landmarks.find(newName);
133  FW_RAISE_EXCEPTION_IF(::fwData::Exception("Group '" + newName + "' already exists"), iter2 != m_landmarks.end());
134 
135  const Landmarks::LandmarksGroup group = iter->second;
136  m_landmarks.insert(std::make_pair(newName, group));
137  m_landmarks.erase(oldName);
138 }
139 
140 //------------------------------------------------------------------------------
141 
142 void Landmarks::removeGroup(const std::string& name)
143 {
144  const auto iter = m_landmarks.find(name);
145  FW_RAISE_EXCEPTION_IF(::fwData::Exception("Group '" + name + "' does not exist"), iter == m_landmarks.end());
146 
147  m_landmarks.erase(name);
148 }
149 
150 //------------------------------------------------------------------------------
151 
152 void Landmarks::setGroupColor(const std::string& name, const Landmarks::ColorType& color)
153 {
154  Landmarks::LandmarksGroup& group = this->getGroup(name);
155  group.m_color = color;
156 }
157 
158 //------------------------------------------------------------------------------
159 
160 void Landmarks::setGroupSize(const std::string& name, const Landmarks::SizeType size)
161 {
162  Landmarks::LandmarksGroup& group = this->getGroup(name);
163  group.m_size = size;
164 }
165 
166 //------------------------------------------------------------------------------
167 
168 void Landmarks::setGroupShape(const std::string& name, const Landmarks::Shape shape)
169 {
170  Landmarks::LandmarksGroup& group = this->getGroup(name);
171  group.m_shape = shape;
172 }
173 
174 //------------------------------------------------------------------------------
175 
176 void Landmarks::setGroupVisibility(const std::string& name, const bool visibility)
177 {
178  Landmarks::LandmarksGroup& group = this->getGroup(name);
179  group.m_visibility = visibility;
180 }
181 
182 //------------------------------------------------------------------------------
183 
184 void Landmarks::addPoint(const std::string& name, const Landmarks::PointType& point)
185 {
186  Landmarks::LandmarksGroup& group = this->getGroup(name);
187  group.m_points.push_back(point);
188 }
189 
190 //------------------------------------------------------------------------------
191 
192 void Landmarks::insertPoint(const std::string& name, const size_t index, const Landmarks::PointType& point)
193 {
194  Landmarks::LandmarksGroup& group = this->getGroup(name);
195  auto iter = group.m_points.begin() + static_cast< PointContainer::difference_type >(index);
196  group.m_points.insert(iter, point);
197 }
198 
199 //------------------------------------------------------------------------------
200 
201 const Landmarks::PointType& Landmarks::getPoint(const std::string& name, size_t index) const
202 {
203  const Landmarks::LandmarksGroup& group = this->getGroup(name);
204  return group.m_points.at(index);
205 }
206 
207 //------------------------------------------------------------------------------
208 
209 Landmarks::PointType& Landmarks::getPoint(const std::string& name, size_t index)
210 {
211  Landmarks::LandmarksGroup& group = this->getGroup(name);
212  return group.m_points.at(index);
213 }
214 
215 //------------------------------------------------------------------------------
216 
217 const Landmarks::PointContainer& Landmarks::getPoints(const std::string& name) const
218 {
219  const Landmarks::LandmarksGroup& group = this->getGroup(name);
220  return group.m_points;
221 }
222 
223 //------------------------------------------------------------------------------
224 
225 void Landmarks::removePoint(const std::string& name, size_t index)
226 {
227  Landmarks::LandmarksGroup& group = this->getGroup(name);
228 
229  FW_RAISE_EXCEPTION_IF(std::out_of_range("index out of range in group '" + name + "'"),
230  index >= group.m_points.size());
231 
232  auto iter = group.m_points.begin() + static_cast< PointContainer::difference_type >(index);
233  group.m_points.erase(iter);
234 }
235 
236 //------------------------------------------------------------------------------
237 
238 void Landmarks::clearPoints(const std::string& name)
239 {
240  Landmarks::LandmarksGroup& group = this->getGroup(name);
241  group.m_points.clear();
242 }
243 
244 //------------------------------------------------------------------------------
245 
247 {
248  size_t nb = 0;
249  for (const auto& elt: m_landmarks)
250  {
251  const LandmarksGroup group = elt.second;
252  nb += group.m_points.size();
253  }
254  return nb;
255 }
256 
257 //------------------------------------------------------------------------------
258 
259 size_t Landmarks::getNumberOfPoints(const std::string& name) const
260 {
261  const LandmarksGroup& group = this->getGroup(name);
262  return group.m_points.size();
263 }
264 
265 //------------------------------------------------------------------------------
266 
267 } // namespace fwData
268 
FWDATA_API void cachedDeepCopy(const Object::csptr &_source, DeepCopyCacheType &cache) override
Defines deep copy.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_POINT_INSERTED_SIG
Type of signal when a group is added.
FWDATA_API void setGroupSize(const std::string &name, const SizeType size)
Set the group&#39;s landmark size.
This class defines a list of 3D points inside groups.
FWDATA_API const LandmarksGroup & getGroup(const std::string &name) const
Get the group properties.
FWDATA_API void clearPoints(const std::string &name)
Remove all the group&#39;s points.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_POINT_DESELECTED_SIG
Type of signal when a group is added.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_GROUP_ADDED_SIG
Type of signal when a group is added.
FWDATA_API void removeGroup(const std::string &name)
Remove the group.
virtual const std::string & getClassname() const override
return full object&#39;s classname with its namespace, i.e. fwCore::BaseObject
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Implements data exception class.
FWDATA_API const PointContainer & getPoints(const std::string &name) const
Get all group&#39;s points.
FWDATA_API void addPoint(const std::string &name, const PointType &point)
Add a point to the group.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_GROUP_REMOVED_SIG
Type of signal when a group is added.
FWDATA_API void shallowCopy(const Object::csptr &_source) override
Defines shallow copy.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_POINT_SELECTED_SIG
Type of signal when a group is added.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_POINT_ADDED_SIG
Type of signal when a group is added.
FWDATA_API Landmarks(::fwData::Object::Key key)
Constructor.
FWDATA_API void setGroupColor(const std::string &name, const ColorType &color)
Set the group&#39;s color.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_GROUP_RENAMED_SIG
Type of signal when a group is added.
FWDATA_API void fieldDeepCopy(const ::fwData::Object::csptr &source)
A deep copy of fields (objects in m_children)
FWDATA_API void removePoint(const std::string &name, size_t index)
Remove the point at the given index.
FWDATA_API const GroupNameContainer getGroupNames() const
Return all group names.
FWDATA_API size_t getNumberOfPoints() const
Return the number of points in the entire structure.
virtual FWDATA_API ~Landmarks()
Destructor.
FWDATA_API void renameGroup(const std::string &oldName, const std::string &newName)
Rename the group.
Contains the representation of the data objects used in the framework.
FWDATA_API const PointType & getPoint(const std::string &name, size_t index) const
Get the point at the given index.
FWDATA_API void fieldShallowCopy(const ::fwData::Object::csptr &source)
A shallow copy of fields (objects in m_children)
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_POINT_MODIFIED_SIG
Type of signal when a group is added.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_GROUP_MODIFIED_SIG
Type of signal when a group is added.
FWDATA_API void addGroup(const std::string &name, const ColorType &color={{1.0f, 1.0f, 1.0f, 1.0f}}, const SizeType size=1.0f, const Shape shape=Shape::SPHERE, const bool visibility=true)
Add a new landmark group.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_POINT_REMOVED_SIG
Type of signal when a group is added.
FWDATA_API void insertPoint(const std::string &name, const size_t index, const PointType &point)
Insert a point to the group at the given index.
FWDATA_API void setGroupVisibility(const std::string &name, const bool visibility)
Set the group&#39;s visibility.
FWDATA_API void setGroupShape(const std::string &name, const Shape shape)
Set the group&#39;s shape (SPHERE or CUBE)