Debian Codegraph @

src/Gui/AxisOrigin.cpp

raw
1 /****************************************************************************
2 * Copyright (c) 2019 Zheng Lei (realthunder) <[email protected]> *
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 #include <Inventor/nodes/SoCoordinate3.h>
24 #include <Inventor/nodes/SoSeparator.h>
25 #include <Inventor/nodes/SoSwitch.h>
26 #include <Inventor/SoPickedPoint.h>
27 #include <Inventor/nodes/SoMaterial.h>
28 #include <Inventor/nodes/SoMaterialBinding.h>
29 #include <Inventor/nodes/SoDrawStyle.h>
30 #include <Inventor/nodes/SoIndexedLineSet.h>
31 #include <Inventor/nodes/SoIndexedPointSet.h>
32 #include <Inventor/nodes/SoDrawStyle.h>
33 #include <Inventor/SoFullPath.h>
34
35 #include "Inventor/SoAutoZoomTranslation.h"
36 #include "SoFCSelection.h"
37 #include "SoFCUnifiedSelection.h"
38 #include "AxisOrigin.h"
39
40 using namespace Gui;
41
42 TYPESYSTEM_SOURCE(Gui::AxisOrigin, Base::BaseClass)
43
44 AxisOrigin::AxisOrigin() = default;
45
46 SoGroup* AxisOrigin::getNode()
47 {
48 if (node) {
49 return node;
50 }
51
52 node.reset(new SoGroup);
53 auto pMat = new SoMaterial();
54
55 const SbVec3f verts[13] = {
56 SbVec3f(0, 0, 0),
57 SbVec3f(size, 0, 0),
58 SbVec3f(0, size, 0),
59 SbVec3f(0, 0, size),
60 SbVec3f(dist, dist, 0),
61 SbVec3f(dist, pSize, 0),
62 SbVec3f(pSize, dist, 0), // XY Plane
63 SbVec3f(dist, 0, dist),
64 SbVec3f(dist, 0, pSize),
65 SbVec3f(pSize, 0, dist), // XY Plane
66 SbVec3f(0, dist, dist),
67 SbVec3f(0, pSize, dist),
68 SbVec3f(0, dist, pSize) // XY Plane
69 };
70
71 // indexes used to create the edges
72 const int32_t lines[21] = {0, 1, -1, 0, 2, -1, 0, 3, -1, 5, 4,
73 6, -1, 8, 7, 9, -1, 11, 10, 12, -1};
74
75 pMat->diffuseColor.setNum(3);
76 pMat->diffuseColor.set1Value(0, SbColor(1.0f, 0.2f, 0.2f));
77 pMat->diffuseColor.set1Value(1, SbColor(0.2f, 0.6f, 0.2f));
78 pMat->diffuseColor.set1Value(2, SbColor(0.2f, 0.2f, 1.0f));
79 pMat->diffuseColor.set1Value(4, SbColor(0.8f, 0.8f, 0.8f));
80
81 auto pCoords = new SoCoordinate3();
82 pCoords->point.setNum(3);
83 pCoords->point.setValues(0, 13, verts);
84
85 auto zoom = new SoAutoZoomTranslation;
86 zoom->scaleFactor = scale;
87
88 auto style = new SoDrawStyle();
89 style->lineWidth = lineSize;
90 style->pointSize = pointSize;
91
92 auto matBinding = new SoMaterialBinding;
93 matBinding->value = SoMaterialBinding::PER_FACE_INDEXED;
94
95 node->addChild(zoom);
96 node->addChild(style);
97 node->addChild(matBinding);
98 node->addChild(pMat);
99 node->addChild(pCoords);
100
101 #define CREATE_AXIS(_type, _key, _count, _offset, _mat) \
102 do { \
103 const char* label = _key; \
104 if (labels.size()) { \
105 auto iter = labels.find(_key); \
106 if (iter == labels.end()) \
107 break; \
108 else if (iter->second.size()) \
109 label = iter->second.c_str(); \
110 } \
111 auto pAxis = new SoFCSelection; \
112 pAxis->applySettings(); \
113 pAxis->style = SoFCSelection::EMISSIVE_DIFFUSE; \
114 pAxis->subElementName = label; \
115 nodeMap[label].reset(pAxis); \
116 node->addChild(pAxis); \
117 auto _type = new SoIndexed##_type##Set; \
118 pAxis->addChild(_type); \
119 _type->coordIndex.setNum(_count); \
120 _type->coordIndex.setValues(0, _count, lines + _offset); \
121 _type->materialIndex.setValue(_mat); \
122 } while (0)
123
124 CREATE_AXIS(Point, "O", 1, 0, 4);
125 CREATE_AXIS(Line, "X", 3, 0, 0);
126 CREATE_AXIS(Line, "Y", 3, 3, 1);
127 CREATE_AXIS(Line, "Z", 3, 6, 2);
128 CREATE_AXIS(Line, "XY", 4, 9, 2);
129 CREATE_AXIS(Line, "XZ", 4, 13, 1);
130 CREATE_AXIS(Line, "YZ", 4, 17, 0);
131 return node;
132 }
133
134 bool AxisOrigin::getElementPicked(const SoPickedPoint* pp, std::string& subname) const
135 {
136 SoPath* path = pp->getPath();
137 int length = path->getLength();
138 for (int i = 0; i < length; ++i) {
139 auto node = path->getNodeFromTail(i);
140 if (node->isOfType(SoFCSelection::getClassTypeId())) {
141 subname = static_cast<SoFCSelection*>(node)->subElementName.getValue().getString();
142 return true;
143 }
144 else if (node->isOfType(SoFCSelectionRoot::getClassTypeId())) {
145 break;
146 }
147 }
148 return false;
149 }
150
151 bool AxisOrigin::getDetailPath(const char* subname, SoFullPath* pPath, SoDetail*&) const
152 {
153 if (!node) {
154 return false;
155 }
156 if (!subname || !subname[0]) {
157 return true;
158 }
159
160 auto it = nodeMap.find(subname);
161 if (it == nodeMap.end()) {
162 return false;
163 }
164 pPath->append(node);
165 pPath->append(it->second);
166 return true;
167 }
168
169 void AxisOrigin::setLineWidth(float size)
170 {
171 if (size != lineSize) {
172 node.reset();
173 nodeMap.clear();
174 lineSize = size;
175 }
176 }
177
178 void AxisOrigin::setPointSize(float size)
179 {
180 if (pointSize != size) {
181 pointSize = size;
182 node.reset();
183 nodeMap.clear();
184 }
185 }
186
187 void AxisOrigin::setAxisLength(float size)
188 {
189 if (this->size != size) {
190 this->size = size;
191 node.reset();
192 nodeMap.clear();
193 }
194 }
195
196 void AxisOrigin::setPlane(float size, float dist)
197 {
198 if (pSize != size || this->dist != dist) {
199 pSize = size;
200 this->dist = dist;
201 node.reset();
202 nodeMap.clear();
203 }
204 }
205
206 void AxisOrigin::setScale(float scale)
207 {
208 if (this->scale != scale) {
209 this->scale = scale;
210 node.reset();
211 nodeMap.clear();
212 }
213 }
214
215 void AxisOrigin::setLabels(const std::map<std::string, std::string>& labels)
216 {
217 this->labels = labels;
218 node.reset();
219 nodeMap.clear();
220 }