-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenegraph.cpp
77 lines (65 loc) · 1.71 KB
/
scenegraph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <algorithm>
#include "scenegraph.h"
using namespace std;
using namespace std::tr1;
bool SgTransformNode::accept(SgNodeVisitor& visitor) {
if (!visitor.visit(*this))
return false;
for (int i = 0, n = children_.size(); i < n; ++i) {
if (!children_[i]->accept(visitor))
return false;
}
return visitor.postVisit(*this);
}
void SgTransformNode::addChild(shared_ptr<SgNode> child) {
children_.push_back(child);
}
void SgTransformNode::removeChild(shared_ptr<SgNode> child) {
children_.erase(find(children_.begin(), children_.end(), child));
}
bool SgShapeNode::accept(SgNodeVisitor& visitor) {
if (!visitor.visit(*this))
return false;
return visitor.postVisit(*this);
}
class RbtAccumVisitor : public SgNodeVisitor {
protected:
vector<RigTForm> rbtStack_;
SgTransformNode& target_;
bool found_;
public:
RbtAccumVisitor(SgTransformNode& target)
: target_(target)
, found_(false) {}
const RigTForm getAccumulatedRbt(int offsetFromStackTop = 0) {
RigTForm ans ;
for (int i=rbtStack_.size()-1-offsetFromStackTop; i>=0; i--) {
ans = rbtStack_[i] * ans;
}
return ans;
}
virtual bool visit(SgTransformNode& node) {
if (!found_) {
rbtStack_.push_back(node.getRbt());
}
if (node==target_) {
found_ = true;
return false;
}
return true;
}
virtual bool postVisit(SgTransformNode& node) {
if (!found_) {
rbtStack_.pop_back();
}
return true;
}
};
RigTForm getPathAccumRbt(
shared_ptr<SgTransformNode> source,
shared_ptr<SgTransformNode> destination,
int offsetFromDestination) {
RbtAccumVisitor accum(*destination);
source->accept(accum);
return accum.getAccumulatedRbt(offsetFromDestination);
}