FreeCAD

Форк
0
/
SoAxisCrossKit.cpp 
360 строк · 11.6 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2010 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
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# ifdef FC_OS_WIN32
27
#  include <windows.h>
28
# endif
29
# ifdef FC_OS_MACOSX
30
# include <OpenGL/gl.h>
31
# else
32
# include <GL/gl.h>
33
# endif
34

35
# include <Inventor/actions/SoGetBoundingBoxAction.h>
36
# include <Inventor/actions/SoGLRenderAction.h>
37
# include <Inventor/bundles/SoMaterialBundle.h>
38
# include <Inventor/bundles/SoTextureCoordinateBundle.h>
39
# include <Inventor/elements/SoLazyElement.h>
40
# include <Inventor/elements/SoModelMatrixElement.h>
41
# include <Inventor/elements/SoViewportRegionElement.h>
42
# include <Inventor/elements/SoViewVolumeElement.h>
43
# include <Inventor/nodekits/SoShapeKit.h>
44
# include <Inventor/nodes/SoBaseColor.h>
45
# include <Inventor/nodes/SoCone.h>
46
# include <Inventor/nodes/SoCoordinate3.h>
47
# include <Inventor/nodes/SoCube.h>
48
# include <Inventor/nodes/SoFontStyle.h>
49
# include <Inventor/nodes/SoLineSet.h>
50
# include <Inventor/nodes/SoScale.h>
51
# include <Inventor/nodes/SoSeparator.h>
52
# include <Inventor/nodes/SoText2.h>
53
# include <Inventor/nodes/SoTranslation.h>
54
#endif
55

56
#include "SoAxisCrossKit.h"
57

58

59
using namespace Gui;
60

61

62
SO_KIT_SOURCE(SoShapeScale)
63

64
//  Constructor.
65
SoShapeScale::SoShapeScale()
66
{
67
    SO_KIT_CONSTRUCTOR(SoShapeScale);
68

69
    SO_KIT_ADD_FIELD(active, (true));
70
    SO_KIT_ADD_FIELD(scaleFactor, (1.0f));
71

72
    SO_KIT_ADD_CATALOG_ENTRY(topSeparator, SoSeparator, false, this, "", false);
73
    SO_KIT_ADD_CATALOG_ABSTRACT_ENTRY(shape, SoNode, SoCube, true, topSeparator, "", true);
74
    SO_KIT_ADD_CATALOG_ENTRY(scale, SoScale, false, topSeparator, shape, false);
75

76
    SO_KIT_INIT_INSTANCE();
77
}
78

79
// Destructor.
80
SoShapeScale::~SoShapeScale() = default;
81

82
void
83
SoShapeScale::initClass()
84
{
85
    SO_KIT_INIT_CLASS(SoShapeScale, SoBaseKit, "BaseKit");
86
}
87

88
void
89
SoShapeScale::GLRender(SoGLRenderAction * action)
90
{
91
    SoState * state = action->getState();
92

93
    SoScale * scale = static_cast<SoScale*>(this->getAnyPart(SbName("scale"), true));
94
    if (!this->active.getValue()) {
95
        SbVec3f v(1.0f, 1.0f, 1.0f);
96
        if (scale->scaleFactor.getValue() != v)
97
            scale->scaleFactor = v;
98
    }
99
    else {
100
        const SbViewportRegion & vp = SoViewportRegionElement::get(state);
101
        const SbViewVolume & vv = SoViewVolumeElement::get(state);
102
        SbVec3f center(0.0f, 0.0f, 0.0f);
103
        float nsize = this->scaleFactor.getValue() / float(vp.getViewportSizePixels()[1]);
104
        SoModelMatrixElement::get(state).multVecMatrix(center, center); // world coords
105
        float sf = vv.getWorldToScreenScale(center, nsize);
106
        SbVec3f v(sf, sf, sf);
107
        if (scale->scaleFactor.getValue() != v)
108
            scale->scaleFactor = v;
109
    }
110

111
    inherited::GLRender(action);
112
}
113

114
// --------------------------------------------------------------
115

116
SO_KIT_SOURCE(SoAxisCrossKit)
117

118
void
119
SoAxisCrossKit::initClass()
120
{
121
   SO_KIT_INIT_CLASS(SoAxisCrossKit,SoBaseKit, "BaseKit");
122
}
123

124
SoAxisCrossKit::SoAxisCrossKit()
125
{
126
   SO_KIT_CONSTRUCTOR(SoAxisCrossKit);
127

128
   // Add the parts to the catalog...
129
   SO_KIT_ADD_CATALOG_ENTRY(xAxis, SoShapeKit,
130
                            true, this,"", true);
131
   SO_KIT_ADD_CATALOG_ENTRY(xHead, SoShapeKit,
132
                            true, this,"", true);
133
   SO_KIT_ADD_CATALOG_ENTRY(yAxis, SoShapeKit,
134
                            true, this,"", true);
135
   SO_KIT_ADD_CATALOG_ENTRY(yHead, SoShapeKit,
136
                            true, this,"", true);
137
   SO_KIT_ADD_CATALOG_ENTRY(zAxis, SoShapeKit,
138
                            true, this,"", true);
139
   SO_KIT_ADD_CATALOG_ENTRY(zHead, SoShapeKit,
140
                            true, this,"", true);
141

142
   SO_KIT_INIT_INSTANCE();
143

144
   createAxes();
145
}
146

147
SoAxisCrossKit::~SoAxisCrossKit() = default;
148

149
// This kit is made up entirely of SoShapeKits.
150
// Since SoShapeKits do not affect state, neither does this.
151
SbBool
152
SoAxisCrossKit::affectsState() const
153
{
154
   return false;
155
}
156

157
void SoAxisCrossKit::addWriteReference(SoOutput * /*out*/, SbBool /*isfromfield*/)
158
{
159
    // this node should not be written out to a file
160
}
161

162
void SoAxisCrossKit::getBoundingBox(SoGetBoundingBoxAction * action)
163
{
164
    inherited::getBoundingBox(action);
165
    action->resetCenter();
166
    action->setCenter(SbVec3f(0,0,0), false);
167
}
168

169
// Set up parts for default configuration of the jumping jack
170
void
171
SoAxisCrossKit::createAxes()
172
{
173
   // Create the heads.
174
   auto head = new SoCone;
175
   head->bottomRadius.setValue(5);
176
   head->height.setValue(10);
177
   setPart("xHead.shape", head);
178
   setPart("yHead.shape", head);
179
   setPart("zHead.shape", head);
180

181
   // Create the axes.
182
   auto coords = new SoCoordinate3;
183
   coords->point.set1Value(0, SbVec3f(0,0,0));
184
   coords->point.set1Value(1, SbVec3f(90,0,0));
185
   setPart("xAxis.coordinate3", coords);
186
   setPart("yAxis.coordinate3", coords);
187
   setPart("zAxis.coordinate3", coords);
188

189
   auto shape = new SoLineSet;
190
   setPart("xAxis.shape", shape);
191
   setPart("yAxis.shape", shape);
192
   setPart("zAxis.shape", shape);
193

194
   // Place the axes and heads
195
   set("yAxis.transform", "rotation 0 0 1 1.5707999");
196
   set("zAxis.transform", "rotation 0 1 0 -1.5707999");
197

198
   set("xHead.transform", "translation 95 0 0");
199
   set("xHead.transform", "scaleFactor 0.5 1.5 0.5");
200
   set("xHead.transform", "rotation 0 0 -1  1.5707999");
201

202
   set("yHead.transform", "translation 0 95 0");
203
   set("yHead.transform", "scaleFactor 0.5 1.5 0.5");
204
   set("yHead.transform", "rotation 0 0 1 0");
205

206
   set("zHead.transform", "translation 0 0 95");
207
   set("zHead.transform", "scaleFactor 0.5 1.5 0.5");
208
   set("zHead.transform", "rotation 1 0 0  1.5707999");
209

210
   // Set colors & styles
211
   set("xAxis.appearance.lightModel", "model BASE_COLOR");
212
   set("xHead.appearance.lightModel", "model BASE_COLOR");
213
   set("yAxis.appearance.lightModel", "model BASE_COLOR");
214
   set("yHead.appearance.lightModel", "model BASE_COLOR");
215
   set("zAxis.appearance.lightModel", "model BASE_COLOR");
216
   set("zHead.appearance.lightModel", "model BASE_COLOR");
217
   set("xAxis.appearance.drawStyle", "lineWidth 1");
218
   set("yAxis.appearance.drawStyle", "lineWidth 1");
219
   set("zAxis.appearance.drawStyle", "lineWidth 1");
220
   set("xAxis.appearance.material", "diffuseColor 0.5 0.125 0.125");
221
   set("xHead.appearance.material", "diffuseColor 0.5 0.125 0.125");
222
   set("yAxis.appearance.material", "diffuseColor 0.125 0.5 0.125");
223
   set("yHead.appearance.material", "diffuseColor 0.125 0.5 0.125");
224
   set("zAxis.appearance.material", "diffuseColor 0.125 0.125 0.5");
225
   set("zHead.appearance.material", "diffuseColor 0.125 0.125 0.5");
226

227
   // Make unpickable
228
   set("xAxis.pickStyle", "style UNPICKABLE");
229
   set("xHead.pickStyle", "style UNPICKABLE");
230
   set("yAxis.pickStyle", "style UNPICKABLE");
231
   set("yHead.pickStyle", "style UNPICKABLE");
232
   set("zAxis.pickStyle", "style UNPICKABLE");
233
   set("zHead.pickStyle", "style UNPICKABLE");
234
}
235

236
// --------------------------------------------------------------
237

238
SO_NODE_SOURCE(SoRegPoint)
239

240
void SoRegPoint::initClass()
241
{
242
    SO_NODE_INIT_CLASS(SoRegPoint, SoShape, "Shape");
243
}
244

245
SoRegPoint::SoRegPoint()
246
{
247
    SO_NODE_CONSTRUCTOR(SoRegPoint);
248

249
    SO_NODE_ADD_FIELD(base, (SbVec3f(0,0,0)));
250
    SO_NODE_ADD_FIELD(normal, (SbVec3f(1,1,1)));
251
    SO_NODE_ADD_FIELD(length, (3.0));
252
    SO_NODE_ADD_FIELD(color, (1.0f, 0.447059f, 0.337255f));
253
    SO_NODE_ADD_FIELD(text, (""));
254

255
    root = new SoSeparator();
256
    root->ref();
257

258
    // translation
259
    auto move = new SoTranslation();
260
    move->translation.setValue(base.getValue() + normal.getValue() * length.getValue());
261
    root->addChild(move);
262

263
    // sub-group
264
    auto col = new SoBaseColor();
265
    col->rgb.setValue(this->color.getValue());
266

267
    auto font = new SoFontStyle;
268
    font->size = 14;
269

270
    auto sub = new SoSeparator();
271
    sub->addChild(col);
272
    sub->addChild(font);
273
    sub->addChild(new SoText2());
274
    root->addChild(sub);
275
}
276

277
SoRegPoint::~SoRegPoint()
278
{
279
    root->unref();
280
}
281

282
/**
283
 * Renders the probe with text label and a bullet at the base point.
284
 */
285
void SoRegPoint::GLRender(SoGLRenderAction *action)
286
{
287
    if (shouldGLRender(action))
288
    {
289
        SoState*  state = action->getState();
290
        state->push();
291
        SoMaterialBundle mb(action);
292
        SoTextureCoordinateBundle tb(action, true, false);
293
        SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR);
294
        mb.sendFirst();  // make sure we have the correct material
295

296
        SbVec3f p1 = base.getValue();
297
        SbVec3f p2 = p1 + normal.getValue() * length.getValue();
298

299
        glLineWidth(1.0f);
300
        glColor3fv(color.getValue().getValue());
301
        glBegin(GL_LINE_STRIP);
302
            glVertex3d(p1[0], p1[1], p1[2]);
303
            glVertex3d(p2[0], p2[1], p2[2]);
304
        glEnd();
305
        glPointSize(5.0f);
306
        glBegin(GL_POINTS);
307
            glVertex3fv(p1.getValue());
308
        glEnd();
309
        glPointSize(2.0f);
310
        glBegin(GL_POINTS);
311
            glVertex3fv(p2.getValue());
312
        glEnd();
313

314
        root->GLRender(action);
315
        state->pop();
316
    }
317
}
318

319
void SoRegPoint::generatePrimitives(SoAction* /*action*/)
320
{
321
}
322

323
/**
324
 * Sets the bounding box of the probe to \a box and its center to \a center.
325
 */
326
void SoRegPoint::computeBBox(SoAction *action, SbBox3f &box, SbVec3f &center)
327
{
328
    root->doAction(action);
329
    if (action->getTypeId().isDerivedFrom(SoGetBoundingBoxAction::getClassTypeId()))
330
        static_cast<SoGetBoundingBoxAction*>(action)->resetCenter();
331

332
    SbVec3f p1 = base.getValue();
333
    SbVec3f p2 = p1 + normal.getValue() * length.getValue();
334

335
    box.extendBy(p1);
336
    box.extendBy(p2);
337

338
    center = box.getCenter();
339
}
340

341
void SoRegPoint::notify(SoNotList * node)
342
{
343
    SoField * f = node->getLastField();
344
    if (f == &this->base || f == &this->normal || f == &this->length) {
345
        auto move = static_cast<SoTranslation*>(root->getChild(0));
346
        move->translation.setValue(base.getValue() + normal.getValue() * length.getValue());
347
    }
348
    else if (f == &this->color) {
349
        auto sub = static_cast<SoSeparator*>(root->getChild(1));
350
        auto col = static_cast<SoBaseColor*>(sub->getChild(0));
351
        col->rgb = this->color.getValue();
352
    }
353
    else if (f == &this->text) {
354
        auto sub = static_cast<SoSeparator*>(root->getChild(1));
355
        auto label = static_cast<SoText2*>(sub->getChild(2));
356
        label->string = this->text.getValue();
357
    }
358

359
    SoShape::notify(node);
360
}
361

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.