Skip to content

Commit 4afc0d4

Browse files
committed
added merge objects command
1 parent acbdc9e commit 4afc0d4

File tree

9 files changed

+166
-3
lines changed

9 files changed

+166
-3
lines changed

source/annotatorlib/include/AnnotatorLib/Annotation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ class ANNOTATORLIB_API Annotation {
205205

206206
Annotation(shared_ptr<Annotation> a, shared_ptr<AnnotatorLib::Frame> f,
207207
bool isTemporary = false);
208+
Annotation(shared_ptr<Annotation> a, shared_ptr<AnnotatorLib::Object> o,
209+
bool isTemporary = false);
208210

209211
Annotation(unsigned long id, const shared_ptr<AnnotatorLib::Frame> &frame,
210212
const shared_ptr<Object> &obj,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2017 Annotator Team
2+
#ifndef MERGEOBJECTS_H
3+
#define MERGEOBJECTS_H
4+
5+
#include <AnnotatorLib/Commands/Command.h>
6+
#include <memory>
7+
8+
namespace AnnotatorLib {
9+
10+
class Session;
11+
class Object;
12+
class Class;
13+
14+
namespace Commands {
15+
16+
class ANNOTATORLIB_API MergeObjects : public Command {
17+
public:
18+
MergeObjects() = delete;
19+
20+
MergeObjects(std::shared_ptr<Session> session, std::shared_ptr<Object> first,
21+
std::shared_ptr<Object> second);
22+
23+
~MergeObjects() {}
24+
25+
virtual bool execute(Session *informSession = 0) override;
26+
27+
virtual bool undo(Session *informSession = 0) override;
28+
29+
protected:
30+
std::shared_ptr<Session> session;
31+
std::shared_ptr<Object> first;
32+
std::shared_ptr<Object> second;
33+
};
34+
}
35+
}
36+
#endif // MERGEOBJECTS_H

source/annotatorlib/include/AnnotatorLib/Commands/UpdateObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ class ANNOTATORLIB_API UpdateObject : public Command {
3636
};
3737
}
3838
}
39-
#endif // UPDATEANNOTATION_H
39+
#endif // UPDATEOBJECT_H

source/annotatorlib/include/AnnotatorLib/Session.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ class ANNOTATORLIB_API Session {
128128
return frames;
129129
}
130130

131+
/**
132+
* @brief getFrames
133+
* returns frames within range if exists
134+
* @param first the first frame
135+
* @param last the last frame
136+
* @return
137+
*/
138+
virtual std::unordered_map<unsigned long, std::shared_ptr<Frame>> getFrames(
139+
unsigned long first, unsigned long last);
140+
131141
/**
132142
* @brief Will add the given frame and all annotations, objects
133143
* within this frame to this session. Checks for duplicates.

source/annotatorlib/source/Annotation.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,20 @@ Annotation::Annotation(shared_ptr<Frame> frame, shared_ptr<Object> obj,
3434
: Annotation(genId(frame, obj), frame, obj, type) {}
3535

3636
Annotation::Annotation(shared_ptr<Annotation> a, shared_ptr<Frame> frame,
37-
bool isInterpolated)
37+
bool isTemporary)
3838
: Annotation(genId(frame, a->getObject()), frame, a->getObject(),
39-
a->getType(), isInterpolated) {
39+
a->getType(), isTemporary) {
40+
this->setPosition(a->getX(), a->getY(), a->getWidth(), a->getHeight());
41+
for (std::shared_ptr<Attribute> attribute :
42+
a->getAttributesWithoutDefaults()) {
43+
this->addAttribute(std::make_shared<Attribute>(attribute));
44+
}
45+
}
46+
47+
Annotation::Annotation(shared_ptr<Annotation> a, shared_ptr<Object> o,
48+
bool isTemporary)
49+
: Annotation(genId(a->getFrame(), o), a->getFrame(), o, a->getType(),
50+
isTemporary) {
4051
this->setPosition(a->getX(), a->getY(), a->getWidth(), a->getHeight());
4152
for (std::shared_ptr<Attribute> attribute :
4253
a->getAttributesWithoutDefaults()) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2017 Annotator Team
2+
#include <AnnotatorLib/Class.h>
3+
#include <AnnotatorLib/Commands/MergeObjects.h>
4+
#include <AnnotatorLib/Object.h>
5+
#include <AnnotatorLib/Session.h>
6+
7+
AnnotatorLib::Commands::MergeObjects::MergeObjects(
8+
std::shared_ptr<Session> session, shared_ptr<Object> first,
9+
shared_ptr<Object> second)
10+
: session(session) {
11+
this->first = first;
12+
this->second = second;
13+
}
14+
15+
bool AnnotatorLib::Commands::MergeObjects::execute(
16+
AnnotatorLib::Session *informSession) {
17+
bool success = false;
18+
first->setActive(true);
19+
for (auto &pair : second->getAnnotations()) {
20+
auto annotation = Annotation::make_shared(pair.second.lock(), first);
21+
success = session->addAnnotation(annotation, true);
22+
session->removeAnnotation(pair.second.lock()->getId(), true);
23+
}
24+
25+
for (auto &attribute : second->getAttributes()) {
26+
first->addAttribute(attribute);
27+
second->removeAttribute(attribute);
28+
}
29+
30+
first->setActive(false);
31+
32+
session->removeObject(second->getId());
33+
34+
if (informSession) {
35+
informSession->updateObject(first);
36+
informSession->updateObject(second);
37+
}
38+
return success;
39+
}
40+
41+
bool AnnotatorLib::Commands::MergeObjects::undo(
42+
AnnotatorLib::Session *informSession) {
43+
return false;
44+
}

source/annotatorlib/source/Session.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ shared_ptr<Class> Session::getClass(unsigned long id) const {
128128
return shared_ptr<Class>(nullptr);
129129
}
130130

131+
std::unordered_map<unsigned long, std::shared_ptr<Frame>> Session::getFrames(
132+
unsigned long first, unsigned long last) {
133+
std::unordered_map<unsigned long, std::shared_ptr<Frame>> ret_frames;
134+
for (auto& pair : frames) {
135+
if (pair.first >= first && pair.first <= last) {
136+
ret_frames.insert(pair);
137+
}
138+
}
139+
return ret_frames;
140+
}
141+
131142
bool Session::addFrame(shared_ptr<Frame> frame, bool add_associated_objects) {
132143
if (frame.get() == nullptr) return false;
133144
auto result = frames.insert(std::make_pair(frame->getId(), frame));

source/tests/annotatorlib-test/commands_test.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2016-2017 Annotator Team
22
#include <AnnotatorLib/Annotation.h>
33
#include <AnnotatorLib/Commands/CompressObject.h>
4+
#include <AnnotatorLib/Commands/MergeObjects.h>
45
#include <AnnotatorLib/Commands/NewAnnotation.h>
56
#include <AnnotatorLib/Commands/NewAttribute.h>
67
#include <AnnotatorLib/Commands/RemoveAnnotation.h>
@@ -203,6 +204,40 @@ TEST_F(commands_test, compressObject) {
203204
ASSERT_EQ(obj->getAnnotations().size(), 2ul);
204205
}
205206

207+
TEST_F(commands_test, mergeObjects) {
208+
std::shared_ptr<Session> session = std::make_shared<Session>();
209+
shared_ptr<Object> first = std::make_shared<Object>();
210+
211+
for (unsigned long i = 0; i < 5; ++i) {
212+
shared_ptr<Frame> frame_i = std::make_shared<Frame>(i);
213+
session->addAnnotation(Annotation::make_shared(frame_i, first));
214+
session->getAnnotation(frame_i, first)->setPosition(i * 10, 40, 20, 20);
215+
ASSERT_EQ(session->getAnnotations().size(), i + 1);
216+
ASSERT_EQ(session->getFrames().size(), i + 1);
217+
}
218+
ASSERT_EQ(session->getFrames().size(), 5ul);
219+
ASSERT_EQ(first->getAnnotations().size(), 5ul);
220+
221+
shared_ptr<Object> second = std::make_shared<Object>();
222+
223+
for (unsigned long i = 5; i < 10; ++i) {
224+
shared_ptr<Frame> frame_i = std::make_shared<Frame>(i);
225+
session->addAnnotation(Annotation::make_shared(frame_i, second));
226+
session->getAnnotation(frame_i, second)->setPosition(i * 10, 40, 20, 20);
227+
ASSERT_EQ(session->getAnnotations().size(), i + 1);
228+
ASSERT_EQ(session->getFrames().size(), i + 1);
229+
}
230+
ASSERT_EQ(session->getFrames().size(), 10ul);
231+
ASSERT_EQ(second->getAnnotations().size(), 5ul);
232+
233+
Commands::MergeObjects *cmd =
234+
new Commands::MergeObjects(session, first, second);
235+
session->execute(shared_ptr<Commands::Command>(cmd));
236+
237+
ASSERT_EQ(first->getAnnotations().size(), 10ul);
238+
ASSERT_EQ(second->getAnnotations().size(), 0ul);
239+
}
240+
206241
TEST_F(commands_test, newAttribute) {
207242
std::shared_ptr<Session> session = std::make_shared<Session>();
208243
ASSERT_EQ(session->getAnnotations().size(), (unsigned long)0);

source/tests/annotatorlib-test/session_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ TEST_F(session_test, getObject) {
2626
ASSERT_TRUE(o2->getName() == "testname");
2727
ASSERT_TRUE(success);
2828
}
29+
30+
TEST_F(session_test, getFrames) {
31+
std::shared_ptr<Session> session = std::make_shared<Session>();
32+
shared_ptr<Object> o = std::make_shared<Object>();
33+
for (unsigned long i = 0; i < 5; ++i) {
34+
shared_ptr<Frame> frame_i = std::make_shared<Frame>(i);
35+
session->addAnnotation(Annotation::make_shared(frame_i, o));
36+
session->getAnnotation(frame_i, o)->setPosition(i * 10, 40, 20, 20);
37+
ASSERT_EQ(session->getAnnotations().size(), i + 1);
38+
ASSERT_EQ(session->getFrames().size(), i + 1);
39+
}
40+
ASSERT_EQ(session->getFrames(1, 3).size(), 3);
41+
ASSERT_EQ(session->getFrames(4, 10).size(), 1);
42+
}

0 commit comments

Comments
 (0)