Debian Codegraph @

src/Gui/ManualAlignment.h

raw
1 /***************************************************************************
2 * Copyright (c) 2012 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #ifndef GUI_MANUALALIGNMENT_H
25 #define GUI_MANUALALIGNMENT_H
26
27 #include <QObject>
28 #include <QPointer>
29
30 #include <Base/BoundBox.h>
31 #include <Base/Placement.h>
32 #include <Base/Vector3D.h>
33 #include <Gui/Document.h>
34 #include <Gui/ViewProviderDocumentObject.h>
35
36 class SbVec3f;
37 class SoPickedPoint;
38 class SoEventCallback;
39
40 namespace Gui
41 {
42 class Document;
43 class AlignmentView;
44 class View3DInventorViewer;
45
46 class PickedPoint
47 {
48 public:
49 PickedPoint() = default;
50 PickedPoint(const Base::Vector3d& p, const Base::Vector3d& n)
51 : point(p)
52 , normal(n)
53 {}
54 Base::Vector3d point;
55 Base::Vector3d normal;
56 };
57
58 /**
59 * The AlignemntGroup class is the base for fixed and movable groups.
60 * @author Werner Mayer
61 */
62 class GuiExport AlignmentGroup
63 {
64 protected:
65 AlignmentGroup();
66 ~AlignmentGroup();
67
68 public:
69 /**
70 * Add a mesh to the group.
71 */
72 void addView(App::DocumentObject*);
73 std::vector<App::DocumentObject*> getViews() const;
74 /**
75 * Checks for the view provider of one of the added views.
76 */
77 bool hasView(Gui::ViewProviderDocumentObject*) const;
78 /**
79 * Remove a previously added view by its view provider.
80 */
81 void removeView(Gui::ViewProviderDocumentObject*);
82 /**
83 * Add the group and therefore all its added view providers to the Inventor tree.
84 */
85 void addToViewer(Gui::View3DInventorViewer*) const;
86 /**
87 * Remove all the view providers from the Inventor tree.
88 */
89 void removeFromViewer(Gui::View3DInventorViewer*) const;
90 void setRandomColor();
91 /**
92 * Returns the document of the added views.
93 */
94 Gui::Document* getDocument() const;
95 /**
96 * Add a point to an array of picked points.
97 */
98 void addPoint(const PickedPoint&);
99 /**
100 * Remove last point from array of picked points.
101 */
102 void removeLastPoint();
103 /**
104 * Count the number of picked points.
105 */
106 int countPoints() const;
107 /**
108 * Return an array of picked points.
109 */
110 const std::vector<PickedPoint>& getPoints() const;
111 /**
112 * Clear all picked points.
113 */
114 void clearPoints();
115 /**
116 * Set or unset the alignable mode for the added views. If a view is not alignable it also not
117 * pickable.
118 */
119 void setAlignable(bool);
120 void moveTo(AlignmentGroup&);
121 /**
122 * Clear the list of added views.
123 */
124 void clear();
125 /**
126 * Checks whether the list of added views is empty or not.
127 */
128 bool isEmpty() const;
129 /**
130 * Return the number of added views.
131 */
132 int count() const;
133 /**
134 * Get the overall bounding box of all views.
135 */
136 Base::BoundBox3d getBoundingBox() const;
137
138 protected:
139 std::vector<PickedPoint> _pickedPoints;
140 std::vector<Gui::ViewProviderDocumentObject*> _views;
141 };
142
143 /**
144 * The FixedGroup class can be used for a fixed group of views.
145 * @author Werner Mayer
146 */
147 class GuiExport MovableGroup: public AlignmentGroup
148 {
149 public:
150 MovableGroup();
151 ~MovableGroup();
152 };
153
154 /**
155 * The FixedGroup class can be used for a fixed group of views.
156 * @author Werner Mayer
157 */
158 class GuiExport FixedGroup: public AlignmentGroup
159 {
160 public:
161 FixedGroup();
162 ~FixedGroup();
163 };
164
165 /**
166 * The MovableGroupModel class keeps an array of movable groups.
167 * @author Werner Mayer
168 */
169 class GuiExport MovableGroupModel
170 {
171 public:
172 MovableGroupModel();
173 ~MovableGroupModel();
174
175 void addGroup(const MovableGroup&);
176 void addGroups(const std::map<int, MovableGroup>&);
177 MovableGroup& activeGroup();
178 const MovableGroup& activeGroup() const;
179 void continueAlignment();
180 void clear();
181 bool isEmpty() const;
182 int count() const;
183 const MovableGroup& getGroup(int i) const;
184 Base::BoundBox3d getBoundingBox() const;
185
186 protected:
187 void removeActiveGroup();
188
189 private:
190 std::vector<MovableGroup> _groups;
191 };
192
193 /**
194 * @author Werner Mayer
195 */
196 class GuiExport ManualAlignment: public QObject
197 {
198 Q_OBJECT
199
200 protected:
201 ManualAlignment();
202 ~ManualAlignment() override;
203
204 public:
205 static ManualAlignment* instance();
206 static void destruct();
207 static bool hasInstance();
208
209 void setMinPoints(int minPoints);
210 void setFixedGroup(const FixedGroup&);
211 void setModel(const MovableGroupModel&);
212 void clearAll();
213
214 void setViewingDirections(
215 const Base::Vector3d& view1,
216 const Base::Vector3d& up1,
217 const Base::Vector3d& view2,
218 const Base::Vector3d& up2
219 );
220 void startAlignment(Base::Type mousemodel);
221 void finish();
222 void align();
223 bool canAlign() const;
224 void cancel();
225
226 const Base::Placement& getTransform() const
227 {
228 return myTransform;
229 }
230 void alignObject(App::DocumentObject*);
231
232 // Observer stuff
233 /// Checks if the given object is about to be removed
234 void slotDeletedDocument(const Gui::Document& Doc);
235 /// Checks if the given document is about to be closed
236 void slotDeletedObject(const Gui::ViewProvider& Obj);
237
238 protected:
239 bool computeAlignment(const std::vector<PickedPoint>& movPts, const std::vector<PickedPoint>& fixPts);
240 void continueAlignment();
241 void showInstructions();
242 /** @name Probe picking */
243 //@{
244 static void probePickedCallback(void* ud, SoEventCallback* n);
245 bool applyPickedProbe(Gui::ViewProviderDocumentObject*, const SoPickedPoint* pnt);
246 //@}
247
248 protected Q_SLOTS:
249 void reset();
250 void onAlign();
251 void onRemoveLastPointMoveable();
252 void onRemoveLastPointFixed();
253 void onClear();
254 void onCancel();
255
256 Q_SIGNALS:
257 void emitCanceled();
258 void emitFinished();
259
260 private:
261 SoNode* pickedPointsSubGraph(const SbVec3f& p, const SbVec3f& n, int id);
262 void closeViewer();
263
264 static ManualAlignment* _instance;
265
266 using Connection = boost::signals2::connection;
267 Connection connectApplicationDeletedDocument;
268 Connection connectDocumentDeletedObject;
269
270 FixedGroup myFixedGroup;
271 MovableGroupModel myAlignModel;
272 QPointer<Gui::AlignmentView> myViewer;
273 Gui::Document* myDocument;
274 int myPickPoints;
275 Base::Placement myTransform;
276
277 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
278 Qt::WindowStates previousWindowState;
279 #endif
280
281 class Private;
282 Private* d;
283 };
284
285 } // namespace Gui
286
287
288 #endif // GUI_MANUALALIGNMENT_H