--- a/src/modeleditcontext.cpp Sat Mar 07 01:25:37 2020 +0200 +++ b/src/modeleditcontext.cpp Mon Mar 09 22:12:50 2020 +0200 @@ -31,12 +31,21 @@ return id; } +void Model::EditContext::remove(int position) +{ + this->model().remove(position); +} + void Model::EditContext::setObjectProperty( - ldraw::Object* object, + ldraw::id_t id, ldraw::Property property, const QVariant& value) { - object->setProperty(property, value); + ldraw::Object* object = this->model().objectAt(id); + if (object != nullptr) + { + object->setProperty(property, value); + } } void Model::EditContext::invertObject(ldraw::id_t id) @@ -54,17 +63,61 @@ return this->storedModel; } +namespace +{ + using PropertyTriplet = std::tuple<ldraw::Property, ldraw::Property, ldraw::Property>; +} + +static std::array<PropertyTriplet, 2> diagonalToPointProperties(ldraw::Diagonal diagonal) +{ + std::array<PropertyTriplet, 2> result; + switch (diagonal) + { + case ldraw::Diagonal::Diagonal_13: + result = { + std::make_tuple(ldraw::Property::Point1, ldraw::Property::Point2, ldraw::Property::Point3), + std::make_tuple(ldraw::Property::Point1, ldraw::Property::Point3, ldraw::Property::Point4) + }; + break; + case ldraw::Diagonal::Diagonal_24: + result = { + std::make_tuple(ldraw::Property::Point1, ldraw::Property::Point2, ldraw::Property::Point4), + std::make_tuple(ldraw::Property::Point2, ldraw::Property::Point3, ldraw::Property::Point4) + }; + break; + } + return result; +} + auto ldraw::splitQuadrilateral( Model::EditContext& editor, ldraw::quadrilateralid_t quadrilateral_id, - ldraw::QuadrilateralSplit splitType + ldraw::Diagonal splitType ) -> std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> { std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> result; - const ldraw::Quadrilateral* quadrilateral = editor.model().get(quadrilateral_id); + QModelIndex index; + const ldraw::Quadrilateral* quadrilateral = editor.model().get(quadrilateral_id, &index); if (quadrilateral != nullptr) { - + Q_ASSERT(index.isValid()); + int position = index.row(); + editor.remove(position); + std::vector<ldraw::triangleid_t> triangles; + triangles.reserve(2); + const std::array<PropertyTriplet, 2> split = diagonalToPointProperties(splitType); + for (const PropertyTriplet& properties : split) + { + const ldraw::triangleid_t triangle = editor.insert<ldraw::Triangle>( + position, + quadrilateral->getProperty(std::get<0>(properties)).value<glm::vec3>(), + quadrilateral->getProperty(std::get<1>(properties)).value<glm::vec3>(), + quadrilateral->getProperty(std::get<2>(properties)).value<glm::vec3>(), + ldraw::Color{quadrilateral->getProperty(ldraw::Property::Color).toInt()}); + triangles.push_back(triangle); + position += 1; + } + result = {triangles[0], triangles[1]}; } return result; }