src/modeleditcontext.cpp

Tue, 01 Mar 2022 17:00:19 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 01 Mar 2022 17:00:19 +0200
changeset 149
008989bc7d6e
parent 136
e8444e0d7f1a
permissions
-rw-r--r--

work on edit history

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
18
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "modeleditcontext.h"
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
20 #include "linetypes/triangle.h"
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
21 #include "linetypes/quadrilateral.h"
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 Model::EditContext::EditContext(Model& model) :
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
24 storedModel{model}
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27
149
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
28 void Model::EditContext::markObjectAsModified(ldraw::id_t id, ldraw::Object *object)
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
29 {
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
30 if (not this->modifiedObjects.contains(id))
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
31 {
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
32 QDataStream stream{&this->modifiedObjects[id], QIODevice::WriteOnly};
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
33 object->serialize(stream);
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
34 }
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
35 }
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
36
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
37 Model::EditContext::~EditContext()
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
38 {
149
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
39 mapapply(this->modifiedObjects, [&](MAP_CPARMS(this->modifiedObjects))
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
40 {
149
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
41 ldraw::Object* object = this->model().objectAt(key);
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
42 if (object != nullptr)
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
43 {
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
44 QByteArray newState;
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
45 QDataStream stream{&newState, QIODevice::WriteOnly};
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
46 object->serialize(stream);
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
47 const int position = this->model().lookup(key).row();
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
48 Q_EMIT this->model().objectStateChanged(position, value, newState);
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
49 }
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
50 });
136
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
51 this->model().editFinished();
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
52 }
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
53
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
54 ldraw::id_t Model::EditContext::append(std::unique_ptr<ldraw::Object>&& object)
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
55 {
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
56 const ldraw::id_t id = object->id;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
57 this->model().objectsById[id] = object.get();
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
58 this->model().append(std::move(object));
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
59 return id;
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
60 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
61
149
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
62 void Model::EditContext::insertFromState(int position, QByteArray &state)
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
63 {
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
64 int typeInteger;
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
65 QDataStream stream{&state, QIODevice::ReadOnly};
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
66 stream >> typeInteger;
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
67 switch (static_cast<ldraw::Object::Type>(typeInteger))
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
68 {
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
69 case ldraw::Object::Type::Empty:
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
70 this->storedModel.insert<ldraw::Empty>(position);
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
71 break;
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
72 }
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
73 }
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
74
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
75 void Model::EditContext::remove(int position)
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
76 {
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
77 this->model().remove(position);
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
78 }
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
79
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
80 auto Model::EditContext::setObjectProperty(
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
81 const ldraw::id_t id,
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
82 const ldraw::Property property,
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
83 const QVariant& value)
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
84 -> ldraw::Object::SetPropertyResult
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
85 {
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
86 ldraw::Object* const object = this->model().objectAt(id);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
87 if (object != nullptr)
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
88 {
149
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
89 this->markObjectAsModified(id, object);
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
90 const ldraw::Object::SetPropertyResult result = object->setProperty(ldraw::PropertyKeyValue{property, value});
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
91 return result;
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
92 }
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
93 else
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
94 {
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
95 return ldraw::Object::SetPropertyResult::PropertyNotHandled;
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
96 }
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
97 }
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
98
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
99 void Model::EditContext::setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value)
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 {
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
101 ldraw::Object* object = this->model().objectAt(id);
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
102 if (object != nullptr)
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
103 {
149
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
104 this->markObjectAsModified(id, object);
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
105 object->setProperty(ldraw::PropertyKeyValue{ldraw::pointProperty(pointId), QVariant::fromValue(value)});
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
106 }
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
108
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
109 void Model::EditContext::invertObject(ldraw::id_t id)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
110 {
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
111 auto it = this->model().objectsById.find(id);
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
112 if (it != this->model().objectsById.end())
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
113 {
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
114 ldraw::Object* object = it->second;
149
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
115 this->markObjectAsModified(id, object);
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
116 object->invert();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
117 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
118 }
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
119
149
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
120 void Model::EditContext::deserialize(ldraw::id_t id, QDataStream &stream)
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
121 {
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
122 auto it = this->model().objectsById.find(id);
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
123 if (it != this->model().objectsById.end())
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
124 {
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
125 ldraw::Object* object = it->second;
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
126 int typeInteger;
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
127 stream >> typeInteger;
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
128 const ldraw::Object::Type type = static_cast<ldraw::Object::Type>(typeInteger);
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
129 if (object->typeIdentifier() == type)
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
130 {
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
131 this->markObjectAsModified(id, object);
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
132 object->deserialize(stream);
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
133 }
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
134 }
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
135 }
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
136
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
137 ldraw::id_t Model::EditContext::resolve(int position) const
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
138 {
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
139 if (position >= 0 and position < this->storedModel.rowCount({}))
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
140 {
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
141 return this->storedModel.objectAt(this->storedModel.index(position))->id;
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
142 }
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
143 else
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
144 {
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
145 return ldraw::NULL_ID;
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
146 }
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
147 }
008989bc7d6e work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
148
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
149 Model& Model::EditContext::model()
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
150 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
151 return this->storedModel;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
152 }
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
153
87
93ec4d630346 added PolygonObject and refactored away a lot of boilerplate
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
154 static std::array<geom::Triangle, 2> splitTriangles(ldraw::Diagonal diagonal, const std::array<glm::vec3, 4>& points)
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
155 {
77
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
156 std::array<geom::Triangle, 2> result;
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
157 switch (diagonal)
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
158 {
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
159 case ldraw::Diagonal::Diagonal_13:
77
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
160 result = {geom::Triangle{points[0], points[1], points[2]}, {points[0], points[2], points[3]}};
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
161 break;
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
162 case ldraw::Diagonal::Diagonal_24:
77
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
163 result = {geom::Triangle{points[0], points[1], points[3]}, {points[1], points[2], points[3]}};
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
164 break;
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
165 }
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
166 return result;
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
167 }
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
168
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
169 auto ldraw::splitQuadrilateral(
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
170 Model::EditContext& editor,
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
171 ldraw::quadrilateralid_t quadrilateral_id,
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
172 ldraw::Diagonal splitType
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
173 ) -> std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>>
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
174 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
175 std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> result;
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
176 const auto resolved = editor.model().get2(quadrilateral_id);
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
177 if (resolved.object != nullptr)
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
178 {
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
179 const ldraw::Color color = resolved.object->colorIndex;
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
180 const std::array<geom::Triangle, 2> split = splitTriangles(splitType, resolved.object->points);
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
181 const int position = resolved.index.row();
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
182 editor.remove(position);
77
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
183 result = std::make_pair(
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
184 editor.insert<ldraw::Triangle>(position, split[0].points, color),
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
185 editor.insert<ldraw::Triangle>(position, split[1].points, color));
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
186 }
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
187 return result;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
188 }

mercurial