Field3D
OgIGroup.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------//
2 // Includes
3 //----------------------------------------------------------------------------//
4 
5 #include "OgIGroup.h"
6 
7 //----------------------------------------------------------------------------//
8 
9 using std::cout;
10 using std::endl;
11 
12 //----------------------------------------------------------------------------//
13 
15 
16 //----------------------------------------------------------------------------//
17 // OgIGroup implementations
18 //----------------------------------------------------------------------------//
19 
20 OgIGroup::OgIGroup(Alembic::Ogawa::IArchive &archive)
21  : OgIBase(archive.getGroup())
22 {
23  validate();
24  if (m_group) {
25  getGroupName(m_group, m_name);
26  }
27 }
28 
29 //----------------------------------------------------------------------------//
30 
31 OgGroupType OgIGroup::type() const
32 {
33  return F3DGroupType;
34 }
35 
36 //----------------------------------------------------------------------------//
37 
38 std::vector<std::string> OgIGroup::groupNames() const
39 {
40  return groupNames(F3DGroupType);
41 }
42 
43 //----------------------------------------------------------------------------//
44 
45 std::vector<std::string> OgIGroup::attributeNames() const
46 {
47  return groupNames(F3DAttributeType);
48 }
49 
50 //----------------------------------------------------------------------------//
51 
52 std::vector<std::string> OgIGroup::datasetNames() const
53 {
54  return groupNames(F3DDatasetType);
55 }
56 
57 //----------------------------------------------------------------------------//
58 
59 std::vector<std::string> OgIGroup::compressedDatasetNames() const
60 {
61  return groupNames(F3DCompressedDatasetType);
62 }
63 
64 //----------------------------------------------------------------------------//
65 
66 OgIGroup OgIGroup::findGroup(const std::string &name) const
67 {
68  Alembic::Ogawa::IGroupPtr group = findGroup(name, F3DGroupType);
69 
70  if (group) {
71  return OgIGroup(group);
72  }
73 
74  return OgIGroup();
75 }
76 
77 //----------------------------------------------------------------------------//
78 
79 Alembic::Ogawa::IGroupPtr
80 OgIGroup::findGroup(const std::string &path,
81  const OgGroupType groupType) const
82 {
83  // If not valid, return non-valid group
84  if (!isValid()) {
85  return Alembic::Ogawa::IGroupPtr();
86  }
87  // Check for recursive finding
88  if (path.find("/") != std::string::npos) {
89  return recursiveFindGroup(path, groupType);
90  }
91  // Not recursive, just a single group name
92  const std::string &name = path;
93  // If it's valid, we know we have at least 2 children.
94  // Check all children
95  for (size_t i = OGAWA_START_ID, end = m_group->getNumChildren();
96  i < end; ++i) {
97  // Is it an Ogawa group? If not, continue
98  if (!m_group->isChildGroup(i)) {
99  continue;
100  }
101  // Grab the ogawa group
102  Alembic::Ogawa::IGroupPtr group =
103  m_group->getGroup(i, false, OGAWA_THREAD);
104  // Data set 0 is the name
105  std::string groupName;
106  if (!readString(group, 0, groupName)) {
107  // This is a bad error. Print details.
108  std::cout << "OgIGroup::findGroup() couldn't read subgroup name for "
109  << "group name: " << name << std::endl;
110  return Alembic::Ogawa::IGroupPtr();
111  }
112  // Data set 1 is the type
113  OgGroupType type;
114  if (!readData(group, 1, type)) {
115  // This is a bad error. Print details.
116  std::cout << "OgIGroup::findGroup() couldn't read subgroup type for "
117  << "group name: " << name << std::endl;
118  return Alembic::Ogawa::IGroupPtr();
119  }
120  // Check that group type matches
121  if (type != groupType) {
122  // This is not an error.
123  continue;
124  }
125  // Check if name matches
126  if (groupName == name) {
127  return group;
128  }
129  }
130  // Didn't find one
131  return Alembic::Ogawa::IGroupPtr();
132 }
133 
134 //----------------------------------------------------------------------------//
135 
136 Alembic::Ogawa::IGroupPtr
137 OgIGroup::recursiveFindGroup(const std::string &path,
138  const OgGroupType groupType) const
139 {
140  // If not valid, return non-valid group
141  if (!isValid()) {
142  return Alembic::Ogawa::IGroupPtr();
143  }
144  // Find the next group name in the path
145  const size_t pos = path.find("/");
146  const std::string name = path.substr(0, pos);
147  const std::string restOfPath = path.substr(pos + 1);
148  // If the group is valid, we know we have at least 2 children.
149  // Check all children
150  for (size_t i = OGAWA_START_ID, end = m_group->getNumChildren();
151  i < end; ++i) {
152  // Is it an Ogawa group? If not, continue
153  if (!m_group->isChildGroup(i)) {
154  continue;
155  }
156  // Grab the ogawa group
157  Alembic::Ogawa::IGroupPtr group =
158  m_group->getGroup(i, false, OGAWA_THREAD);
159  // Data set 0 is the name
160  std::string groupName;
161  if (!readString(group, 0, groupName)) {
162  // This is a bad error. Print details.
163  std::cout << "OgIGroup::recursiveFindGroup() couldn't read subgroup "
164  << "name for group name: " << name << std::endl;
165  return Alembic::Ogawa::IGroupPtr();
166  }
167  // Data set 1 is the type
168  OgGroupType type;
169  if (!readData(group, 1, type)) {
170  // This is a bad error. Print details.
171  std::cout << "OgIGroup::recursiveFindGroup() couldn't read subgroup "
172  << "type for group name: " << name << std::endl;
173  return Alembic::Ogawa::IGroupPtr();
174  }
175  // Check that group type is F3DGroupType
176  if (type != F3DGroupType) {
177  // This is not an error.
178  continue;
179  }
180  // Check if name matches
181  if (groupName == name) {
182  OgIGroup subGroup(group);
183  return subGroup.findGroup(restOfPath, groupType);
184  }
185  }
186  // Didn't find one
187  cout << "Couldn't find group: " << name << endl;
188  return Alembic::Ogawa::IGroupPtr();
189 }
190 
191 //----------------------------------------------------------------------------//
192 
193 std::vector<std::string>
194 OgIGroup::groupNames(const OgGroupType groupType) const
195 {
196  // If not valid, return non-valid group
197  if (!isValid()) {
198  return std::vector<std::string>();
199  }
200  // Check all children
201  std::vector<std::string> groups;
202  for (size_t i = OGAWA_START_ID, end = m_group->getNumChildren();
203  i < end; ++i) {
204  // Is it an Ogawa group? If not, continue
205  if (!m_group->isChildGroup(i)) {
206  continue;
207  }
208  // Grab the ogawa group
209  Alembic::Ogawa::IGroupPtr group =
210  m_group->getGroup(i, false, OGAWA_THREAD);
211  // Data set 0 is the name
212  std::string groupName;
213  if (!readString(group, 0, groupName)) {
214  continue;
215  }
216  // Data set 1 is the type
217  OgGroupType type;
218  if (!readData(group, 1, type)) {
219  continue;
220  }
221  // Check that group type matches
222  if (type != groupType) {
223  continue;
224  }
225  // Add group name
226  groups.push_back(groupName);
227  }
228  // Done
229  return groups;
230 }
231 
232 //----------------------------------------------------------------------------//
233 
234 OgDataType OgIGroup::attributeType(const std::string &name) const
235 {
236  Alembic::Ogawa::IGroupPtr group = findGroup(name, F3DAttributeType);
237 
238  if (group && group->getNumChildren() > 2) {
239  return readDataType(group, 2);
240  }
241 
242  return F3DInvalidDataType;
243 }
244 
245 //----------------------------------------------------------------------------//
246 
247 OgDataType OgIGroup::datasetType(const std::string &name) const
248 {
249  Alembic::Ogawa::IGroupPtr group = findGroup(name, F3DDatasetType);
250 
251  if (group && group->getNumChildren() > 2) {
252  return readDataType(group, 2);
253  }
254 
255  return F3DInvalidDataType;
256 }
257 
258 //----------------------------------------------------------------------------//
259 
260 OgDataType OgIGroup::compressedDatasetType(const std::string &name) const
261 {
262  Alembic::Ogawa::IGroupPtr group = findGroup(name, F3DCompressedDatasetType);
263 
264  if (group && group->getNumChildren() > 2) {
265  return readDataType(group, 2);
266  }
267 
268  return F3DInvalidDataType;
269 }
270 
271 //----------------------------------------------------------------------------//
272 
273 OgIGroup::OgIGroup()
274 {
275  // Nothing
276 }
277 
278 //----------------------------------------------------------------------------//
279 
280 OgIGroup::OgIGroup(Alembic::Ogawa::IGroupPtr group)
281  : OgIBase(group)
282 {
283  validate();
284  getGroupName(m_group, m_name);
285 }
286 
287 //----------------------------------------------------------------------------//
288 
289 void OgIGroup::validate()
290 {
291  // If we don't have two children, we're invalid.
292  if (m_group && m_group->getNumChildren() < 2) {
293  m_group.reset();
294  return;
295  }
296  // If the two first children aren't data sets, we're invalid
297  if (m_group && (!m_group->isChildData(0) || !m_group->isChildData(1))) {
298  m_group.reset();
299  }
300 }
301 
302 //----------------------------------------------------------------------------//
303 
305 
306 //----------------------------------------------------------------------------//
readDataType
OgDataType readDataType(Alembic::Ogawa::IGroupPtr group, const size_t idx)
Definition: OgUtil.cpp:65
FIELD3D_NAMESPACE_SOURCE_CLOSE
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition: ns.h:60
F3DInvalidDataType
@ F3DInvalidDataType
Definition: Traits.h:160
getGroupName
bool getGroupName(Alembic::Ogawa::IGroupPtr group, std::string &name)
Definition: OgUtil.cpp:96
FIELD3D_NAMESPACE_OPEN
Definition: FieldMapping.cpp:74
readString
bool readString(Alembic::Ogawa::IGroupPtr group, const size_t idx, std::string &s)
Definition: OgUtil.cpp:32
OgDataType
OgDataType
Enumerates the various uses for Ogawa-level groups.
Definition: Traits.h:125