Blender  V2.93
dupli_parent_finder.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 #include "dupli_parent_finder.hh"
21 
22 #include "BLI_utildefines.h"
23 
24 #include <iostream>
25 
26 namespace blender::io {
27 
29 {
30  dupli_set_.insert(dupli_ob->ob);
31 
32  PersistentID dupli_pid(dupli_ob);
33  pid_to_dupli_[dupli_pid] = dupli_ob;
34  instancer_pid_to_duplis_[dupli_pid.instancer_pid()].insert(dupli_ob);
35 }
36 
37 bool DupliParentFinder::is_duplicated(const Object *object) const
38 {
39  return dupli_set_.find(object) != dupli_set_.end();
40 }
41 
43  const DupliObject *dupli_ob) const
44 {
45  if (dupli_ob->ob->parent != nullptr) {
46  const DupliObject *parent = find_duplicated_parent(dupli_ob);
47  if (parent != nullptr) {
48  return parent;
49  }
50  }
51 
52  return find_instancer(dupli_ob);
53 }
54 
55 const DupliObject *DupliParentFinder::find_duplicated_parent(const DupliObject *dupli_ob) const
56 {
57  const PersistentID dupli_pid(dupli_ob);
58  PersistentID parent_pid = dupli_pid.instancer_pid();
59 
60  const Object *parent_ob = dupli_ob->ob->parent;
61  BLI_assert(parent_ob != nullptr);
62 
63  InstancerPIDToDuplisMap::const_iterator found = instancer_pid_to_duplis_.find(parent_pid);
64  if (found == instancer_pid_to_duplis_.end()) {
65  /* Unexpected, as there should be at least one entry here, for the dupli_ob itself. */
66  return nullptr;
67  }
68 
69  for (const DupliObject *potential_parent_dupli : found->second) {
70  if (potential_parent_dupli->ob != parent_ob) {
71  continue;
72  }
73 
74  PersistentID potential_parent_pid(potential_parent_dupli);
75  if (potential_parent_pid.is_from_same_instancer_as(dupli_pid)) {
76  return potential_parent_dupli;
77  }
78  }
79  return nullptr;
80 }
81 
82 const DupliObject *DupliParentFinder::find_instancer(const DupliObject *dupli_ob) const
83 {
84  PersistentID dupli_pid(dupli_ob);
85  PersistentID parent_pid = dupli_pid.instancer_pid();
86 
87  PIDToDupliMap::const_iterator found = pid_to_dupli_.find(parent_pid);
88  if (found == pid_to_dupli_.end()) {
89  return nullptr;
90  }
91 
92  const DupliObject *instancer_dupli = found->second;
93  return instancer_dupli;
94 }
95 
96 } // namespace blender::io
#define BLI_assert(a)
Definition: BLI_assert.h:58
const DupliObject * find_suitable_export_parent(const DupliObject *dupli_ob) const
void insert(const DupliObject *dupli_ob)
bool is_duplicated(const Object *object) const
PersistentID instancer_pid() const
struct Object * ob
Definition: BKE_duplilist.h:45
struct Object * parent