Blender  V3.3
dupli_persistent_id.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2020 Blender Foundation. All rights reserved. */
3 
4 #include "dupli_parent_finder.hh"
5 
6 #include <climits>
7 #include <cstring>
8 #include <ostream>
9 #include <sstream>
10 
11 namespace blender::io {
12 
14 {
15  persistent_id_[0] = INT_MAX;
16 }
17 
19 {
20  for (int index = 0; index < array_length_; ++index) {
21  persistent_id_[index] = dupli_ob->persistent_id[index];
22  }
23 }
24 
25 PersistentID::PersistentID(const PIDArray &persistent_id_values)
26 {
27  persistent_id_ = persistent_id_values;
28 }
29 
31 {
32  if (persistent_id_[0] == INT_MAX || other.persistent_id_[0] == INT_MAX) {
33  /* Either one or the other is not instanced at all, so definitely not from the same instancer.
34  */
35  return false;
36  }
37 
38  /* Start at index 1 to skip the first digit. */
39  for (int index = 1; index < array_length_; ++index) {
40  const int pid_digit_a = persistent_id_[index];
41  const int pid_digit_b = other.persistent_id_[index];
42 
43  if (pid_digit_a != pid_digit_b) {
44  return false;
45  }
46 
47  if (pid_digit_a == INT_MAX) {
48  /* Both persistent IDs were identical so far, and this marks the end of the useful data. */
49  break;
50  }
51  }
52  return true;
53 }
54 
56 {
57  if (persistent_id_[0] == INT_MAX) {
58  return PersistentID();
59  }
60 
61  /* Left-shift the entire PID by 1. */
62  PIDArray new_pid_values;
63  int index;
64  for (index = 0; index < array_length_ - 1; ++index) {
65  new_pid_values[index] = persistent_id_[index + 1];
66  }
67  new_pid_values[index] = INT_MAX;
68 
69  return PersistentID(new_pid_values);
70 }
71 
73 {
74  std::stringstream stream;
75 
76  /* Find one past the last index. */
77  int index;
78  for (index = 0; index < array_length_ && persistent_id_[index] < INT_MAX; ++index) {
79  ;
80  }
81 
82  /* Iterate backward to construct the string. */
83  --index;
84  for (; index >= 0; --index) {
85  stream << persistent_id_[index];
86  if (index > 0) {
87  stream << "-";
88  }
89  }
90 
91  return stream.str();
92 }
93 
94 bool operator<(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b)
95 {
96  const PersistentID::PIDArray &pid_a = persistent_id_a.persistent_id_;
97  const PersistentID::PIDArray &pid_b = persistent_id_b.persistent_id_;
98 
99  for (int index = 0; index < PersistentID::array_length_; ++index) {
100  const int pid_digit_a = pid_a[index];
101  const int pid_digit_b = pid_b[index];
102 
103  if (pid_digit_a != pid_digit_b) {
104  return pid_digit_a < pid_digit_b;
105  }
106 
107  if (pid_a[index] == INT_MAX) {
108  break;
109  }
110  }
111  /* Both Persistent IDs were equal, so not less-than. */
112  return false;
113 }
114 
115 bool operator==(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b)
116 {
117  const PersistentID::PIDArray &pid_a = persistent_id_a.persistent_id_;
118  const PersistentID::PIDArray &pid_b = persistent_id_b.persistent_id_;
119 
120  for (int index = 0; index < PersistentID::array_length_; ++index) {
121  const int pid_digit_a = pid_a[index];
122  const int pid_digit_b = pid_b[index];
123 
124  if (pid_digit_a != pid_digit_b) {
125  return false;
126  }
127 
128  if (pid_a[index] == INT_MAX) {
129  break;
130  }
131  }
132  return true;
133 }
134 
135 std::ostream &operator<<(std::ostream &os, const PersistentID &persistent_id)
136 {
137  if (persistent_id.persistent_id_[0] == INT_MAX) {
138  return os;
139  }
140 
141  const PersistentID::PIDArray &pid_array = persistent_id.persistent_id_;
142  for (int index = 0; index < PersistentID::array_length_ && pid_array[index] < INT_MAX; ++index) {
143  if (index > 0) {
144  os << "-";
145  }
146  os << pid_array[index];
147  }
148  return os;
149 }
150 
151 } // namespace blender::io
PersistentID instancer_pid() const
std::string as_object_name_suffix() const
constexpr static int array_length_
bool is_from_same_instancer_as(const PersistentID &other) const
std::array< int, array_length_ > PIDArray
bool operator<(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b)
std::ostream & operator<<(std::ostream &os, const PersistentID &persistent_id)
bool operator==(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b)
int persistent_id[8]
Definition: BKE_duplilist.h:45