gen_region.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2021 by
  4. * The Salk Institute for Biological Studies
  5. *
  6. * Use of this source code is governed by an MIT-style
  7. * license that can be found in the LICENSE file or at
  8. * https://opensource.org/licenses/MIT.
  9. *
  10. ******************************************************************************/
  11. #include <sstream>
  12. #include "api/pybind11_stl_include.h"
  13. #include "api/python_export_utils.h"
  14. #include "gen_region.h"
  15. #include "api/region.h"
  16. #include "api/region.h"
  17. namespace MCell {
  18. namespace API {
  19. void GenRegion::check_semantics() const {
  20. }
  21. void GenRegion::set_initialized() {
  22. if (is_set(left_node)) {
  23. left_node->set_initialized();
  24. }
  25. if (is_set(right_node)) {
  26. right_node->set_initialized();
  27. }
  28. initialized = true;
  29. }
  30. void GenRegion::set_all_attributes_as_default_or_unset() {
  31. class_name = "Region";
  32. node_type = RegionNodeType::UNSET;
  33. left_node = nullptr;
  34. right_node = nullptr;
  35. }
  36. std::shared_ptr<Region> GenRegion::copy_region() const {
  37. std::shared_ptr<Region> res = std::make_shared<Region>(DefaultCtorArgType());
  38. res->class_name = class_name;
  39. res->node_type = node_type;
  40. res->left_node = left_node;
  41. res->right_node = right_node;
  42. return res;
  43. }
  44. std::shared_ptr<Region> GenRegion::deepcopy_region(py::dict) const {
  45. std::shared_ptr<Region> res = std::make_shared<Region>(DefaultCtorArgType());
  46. res->class_name = class_name;
  47. res->node_type = node_type;
  48. res->left_node = is_set(left_node) ? left_node->deepcopy_region() : nullptr;
  49. res->right_node = is_set(right_node) ? right_node->deepcopy_region() : nullptr;
  50. return res;
  51. }
  52. bool GenRegion::__eq__(const Region& other) const {
  53. return
  54. node_type == other.node_type &&
  55. (
  56. (is_set(left_node)) ?
  57. (is_set(other.left_node) ?
  58. (left_node->__eq__(*other.left_node)) :
  59. false
  60. ) :
  61. (is_set(other.left_node) ?
  62. false :
  63. true
  64. )
  65. ) &&
  66. (
  67. (is_set(right_node)) ?
  68. (is_set(other.right_node) ?
  69. (right_node->__eq__(*other.right_node)) :
  70. false
  71. ) :
  72. (is_set(other.right_node) ?
  73. false :
  74. true
  75. )
  76. ) ;
  77. }
  78. bool GenRegion::eq_nonarray_attributes(const Region& other, const bool ignore_name) const {
  79. return
  80. node_type == other.node_type &&
  81. (
  82. (is_set(left_node)) ?
  83. (is_set(other.left_node) ?
  84. (left_node->__eq__(*other.left_node)) :
  85. false
  86. ) :
  87. (is_set(other.left_node) ?
  88. false :
  89. true
  90. )
  91. ) &&
  92. (
  93. (is_set(right_node)) ?
  94. (is_set(other.right_node) ?
  95. (right_node->__eq__(*other.right_node)) :
  96. false
  97. ) :
  98. (is_set(other.right_node) ?
  99. false :
  100. true
  101. )
  102. ) ;
  103. }
  104. std::string GenRegion::to_str(const bool all_details, const std::string ind) const {
  105. std::stringstream ss;
  106. ss << get_object_name() << ": " <<
  107. "node_type=" << node_type << ", " <<
  108. "\n" << ind + " " << "left_node=" << "(" << ((left_node != nullptr) ? left_node->to_str(all_details, ind + " ") : "null" ) << ")" << ", " << "\n" << ind + " " <<
  109. "right_node=" << "(" << ((right_node != nullptr) ? right_node->to_str(all_details, ind + " ") : "null" ) << ")";
  110. return ss.str();
  111. }
  112. py::class_<Region> define_pybinding_Region(py::module& m) {
  113. return py::class_<Region, std::shared_ptr<Region>>(m, "Region", "Represents region construted from 1 or more multiple, usually unnamed?")
  114. .def(
  115. py::init<
  116. const RegionNodeType,
  117. std::shared_ptr<Region>,
  118. std::shared_ptr<Region>
  119. >(),
  120. py::arg("node_type") = RegionNodeType::UNSET,
  121. py::arg("left_node") = nullptr,
  122. py::arg("right_node") = nullptr
  123. )
  124. .def("check_semantics", &Region::check_semantics)
  125. .def("__copy__", &Region::copy_region)
  126. .def("__deepcopy__", &Region::deepcopy_region, py::arg("memo"))
  127. .def("__str__", &Region::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  128. .def("__eq__", &Region::__eq__, py::arg("other"))
  129. .def("__add__", &Region::__add__, py::arg("other"), "Computes union of two regions, use with Python operator '+'.\n- other\n")
  130. .def("__sub__", &Region::__sub__, py::arg("other"), "Computes difference of two regions, use with Python operator '-'.\n- other\n")
  131. .def("__mul__", &Region::__mul__, py::arg("other"), "Computes intersection of two regions, use with Python operator '*'.\n- other\n")
  132. .def("dump", &Region::dump)
  133. .def_property("node_type", &Region::get_node_type, &Region::set_node_type, "When this values is LeafGeometryObject, then this object is of class GeometryObject,\nwhen LeafSurfaceRegion, then it is of class SurfaceRegion.\n")
  134. .def_property("left_node", &Region::get_left_node, &Region::set_left_node, "Internal, do not use. When node_type is not Leaf, this is the left operand")
  135. .def_property("right_node", &Region::get_right_node, &Region::set_right_node, "Internal, do not use. When node_type is not Leaf, this is the right operand")
  136. ;
  137. }
  138. std::string GenRegion::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  139. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  140. return ctx.get_exported_name(this);
  141. }
  142. std::string exported_name = "region_" + std::to_string(ctx.postinc_counter("region"));
  143. if (!export_even_if_already_exported()) {
  144. ctx.add_exported(this, exported_name);
  145. }
  146. bool str_export = export_as_string_without_newlines();
  147. std::string nl = "";
  148. std::string ind = " ";
  149. std::stringstream ss;
  150. if (!str_export) {
  151. nl = "\n";
  152. ind = " ";
  153. ss << exported_name << " = ";
  154. }
  155. ss << "m.Region(" << nl;
  156. if (node_type != RegionNodeType::UNSET) {
  157. ss << ind << "node_type = " << node_type << "," << nl;
  158. }
  159. if (is_set(left_node)) {
  160. ss << ind << "left_node = " << left_node->export_to_python(out, ctx) << "," << nl;
  161. }
  162. if (is_set(right_node)) {
  163. ss << ind << "right_node = " << right_node->export_to_python(out, ctx) << "," << nl;
  164. }
  165. ss << ")" << nl << nl;
  166. if (!str_export) {
  167. out << ss.str();
  168. return exported_name;
  169. }
  170. else {
  171. return ss.str();
  172. }
  173. }
  174. } // namespace API
  175. } // namespace MCell