File size: 12,660 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
 *   Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net>     *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/

#include <FCConfig.h>

#if defined(FC_OS_WIN32)
# include <windows.h>
#endif

#ifdef FC_OS_MACOSX
# include <OpenGL/gl.h>
#else
# include <GL/gl.h>
#endif

#include <sstream>

#include <Inventor/actions/SoGetBoundingBoxAction.h>
#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/bundles/SoMaterialBundle.h>
#include <Inventor/bundles/SoTextureCoordinateBundle.h>
#include <Inventor/elements/SoLazyElement.h>
#include <Inventor/elements/SoModelMatrixElement.h>
#include <Inventor/elements/SoViewportRegionElement.h>
#include <Inventor/elements/SoViewVolumeElement.h>
#include <Inventor/nodekits/SoShapeKit.h>
#include <Inventor/nodes/SoBaseColor.h>
#include <Inventor/nodes/SoCone.h>
#include <Inventor/nodes/SoCoordinate3.h>
#include <Inventor/nodes/SoCube.h>
#include <Inventor/nodes/SoFontStyle.h>
#include <Inventor/nodes/SoLineSet.h>
#include <Inventor/nodes/SoScale.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoText2.h>
#include <Inventor/nodes/SoTranslation.h>

#include <Base/Color.h>
#include <Gui/ViewParams.h>

#include "SoAxisCrossKit.h"
#include "SoDevicePixelRatioElement.h"

using namespace Gui;

SO_KIT_SOURCE(SoShapeScale)

//  Constructor.
SoShapeScale::SoShapeScale()
{
    SO_KIT_CONSTRUCTOR(SoShapeScale);

    SO_KIT_ADD_FIELD(active, (true));
    SO_KIT_ADD_FIELD(scaleFactor, (1.0f));

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

    SO_KIT_INIT_INSTANCE();
}

// Destructor.
SoShapeScale::~SoShapeScale() = default;

void SoShapeScale::initClass()
{
    SO_KIT_INIT_CLASS(SoShapeScale, SoBaseKit, "BaseKit");
}

void SoShapeScale::GLRender(SoGLRenderAction* action)
{
    auto* scale = static_cast<SoScale*>(this->getAnyPart(SbName("scale"), true));
    if (!this->active.getValue()) {
        SbVec3f v(1.0f, 1.0f, 1.0f);
        if (scale->scaleFactor.getValue() != v) {
            scale->scaleFactor = v;
        }
    }
    else {
        SoState* state = action->getState();
        const SbViewportRegion& vp = SoViewportRegionElement::get(state);
        const SbViewVolume& vv = SoViewVolumeElement::get(state);

        SbVec3f center(0.0f, 0.0f, 0.0f);
        float nsize = this->scaleFactor.getValue() / float(vp.getViewportSizePixels()[0]);
        SoModelMatrixElement::get(state).multVecMatrix(center, center);  // world coords
        float sf = vv.getWorldToScreenScale(center, nsize);

        sf *= SoDevicePixelRatioElement::get(state);

        SbVec3f v(sf, sf, sf);
        if (scale->scaleFactor.getValue() != v) {
            scale->scaleFactor = v;
        }
    }

    inherited::GLRender(action);
}

// --------------------------------------------------------------

SO_KIT_SOURCE(SoAxisCrossKit)

void SoAxisCrossKit::initClass()
{
    SO_KIT_INIT_CLASS(SoAxisCrossKit, SoBaseKit, "BaseKit");
}

SoAxisCrossKit::SoAxisCrossKit()
{
    SO_KIT_CONSTRUCTOR(SoAxisCrossKit);

    // Add the parts to the catalog...
    SO_KIT_ADD_CATALOG_ENTRY(xAxis, SoShapeKit, true, this, "", true);
    SO_KIT_ADD_CATALOG_ENTRY(xHead, SoShapeKit, true, this, "", true);
    SO_KIT_ADD_CATALOG_ENTRY(yAxis, SoShapeKit, true, this, "", true);
    SO_KIT_ADD_CATALOG_ENTRY(yHead, SoShapeKit, true, this, "", true);
    SO_KIT_ADD_CATALOG_ENTRY(zAxis, SoShapeKit, true, this, "", true);
    SO_KIT_ADD_CATALOG_ENTRY(zHead, SoShapeKit, true, this, "", true);

    SO_KIT_INIT_INSTANCE();

    createAxes();
}

SoAxisCrossKit::~SoAxisCrossKit() = default;

// This kit is made up entirely of SoShapeKits.
// Since SoShapeKits do not affect state, neither does this.
SbBool SoAxisCrossKit::affectsState() const
{
    return false;
}

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

void SoAxisCrossKit::getBoundingBox(SoGetBoundingBoxAction* action)
{
    inherited::getBoundingBox(action);
    action->resetCenter();
    action->setCenter(SbVec3f(0, 0, 0), false);
}

// Set up parts for default configuration of the jumping jack
void SoAxisCrossKit::createAxes()
{
    // Create the heads.
    auto head = new SoCone;
    head->bottomRadius.setValue(5);
    head->height.setValue(10);
    setPart("xHead.shape", head);
    setPart("yHead.shape", head);
    setPart("zHead.shape", head);

    // Create the axes.
    auto coords = new SoCoordinate3;
    coords->point.set1Value(0, SbVec3f(0, 0, 0));
    coords->point.set1Value(1, SbVec3f(90, 0, 0));
    setPart("xAxis.coordinate3", coords);
    setPart("yAxis.coordinate3", coords);
    setPart("zAxis.coordinate3", coords);

    auto shape = new SoLineSet;
    setPart("xAxis.shape", shape);
    setPart("yAxis.shape", shape);
    setPart("zAxis.shape", shape);

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

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

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

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

    // Set colors & styles
    set("xAxis.appearance.lightModel", "model BASE_COLOR");
    set("xHead.appearance.lightModel", "model BASE_COLOR");
    set("yAxis.appearance.lightModel", "model BASE_COLOR");
    set("yHead.appearance.lightModel", "model BASE_COLOR");
    set("zAxis.appearance.lightModel", "model BASE_COLOR");
    set("zHead.appearance.lightModel", "model BASE_COLOR");
    set("xAxis.appearance.drawStyle", "lineWidth 1");
    set("yAxis.appearance.drawStyle", "lineWidth 1");
    set("zAxis.appearance.drawStyle", "lineWidth 1");

    unsigned long colorLong;
    Base::Color color;
    std::stringstream parameterstring;

    colorLong = Gui::ViewParams::instance()->getAxisXColor();
    color = Base::Color(static_cast<uint32_t>(colorLong));
    parameterstring << "diffuseColor " << color.r << " " << color.g << " " << color.b;
    set("xAxis.appearance.material", parameterstring.str().c_str());
    set("xHead.appearance.material", parameterstring.str().c_str());

    colorLong = Gui::ViewParams::instance()->getAxisYColor();
    color = Base::Color(static_cast<uint32_t>(colorLong));
    parameterstring << "diffuseColor " << color.r << " " << color.g << " " << color.b;
    set("yAxis.appearance.material", parameterstring.str().c_str());
    set("yHead.appearance.material", parameterstring.str().c_str());

    colorLong = Gui::ViewParams::instance()->getAxisZColor();
    color = Base::Color(static_cast<uint32_t>(colorLong));
    parameterstring << "diffuseColor " << color.r << " " << color.g << " " << color.b;
    set("zAxis.appearance.material", parameterstring.str().c_str());
    set("zHead.appearance.material", parameterstring.str().c_str());

    // Make unpickable
    set("xAxis.pickStyle", "style UNPICKABLE");
    set("xHead.pickStyle", "style UNPICKABLE");
    set("yAxis.pickStyle", "style UNPICKABLE");
    set("yHead.pickStyle", "style UNPICKABLE");
    set("zAxis.pickStyle", "style UNPICKABLE");
    set("zHead.pickStyle", "style UNPICKABLE");
}

// --------------------------------------------------------------

SO_NODE_SOURCE(SoRegPoint)

void SoRegPoint::initClass()
{
    SO_NODE_INIT_CLASS(SoRegPoint, SoShape, "Shape");
}

SoRegPoint::SoRegPoint()
{
    SO_NODE_CONSTRUCTOR(SoRegPoint);

    SO_NODE_ADD_FIELD(base, (SbVec3f(0, 0, 0)));
    SO_NODE_ADD_FIELD(normal, (SbVec3f(1, 1, 1)));
    SO_NODE_ADD_FIELD(length, (3.0));
    SO_NODE_ADD_FIELD(color, (1.0f, 0.447059f, 0.337255f));
    SO_NODE_ADD_FIELD(text, (""));

    root = new SoSeparator();
    root->ref();

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

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

    auto font = new SoFontStyle;
    font->size = 14;

    auto sub = new SoSeparator();
    sub->addChild(col);
    sub->addChild(font);
    sub->addChild(new SoText2());
    root->addChild(sub);
}

SoRegPoint::~SoRegPoint()
{
    root->unref();
}

/**
 * Renders the probe with text label and a bullet at the base point.
 */
void SoRegPoint::GLRender(SoGLRenderAction* action)
{
    if (shouldGLRender(action)) {
        SoState* state = action->getState();
        state->push();
        SoMaterialBundle mb(action);
        SoTextureCoordinateBundle tb(action, true, false);
        SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR);
        mb.sendFirst();  // make sure we have the correct material

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

        glLineWidth(1.0f);
        glColor3fv(color.getValue().getValue());
        glBegin(GL_LINE_STRIP);
        glVertex3d(p1[0], p1[1], p1[2]);
        glVertex3d(p2[0], p2[1], p2[2]);
        glEnd();
        glPointSize(5.0f);
        glBegin(GL_POINTS);
        glVertex3fv(p1.getValue());
        glEnd();
        glPointSize(2.0f);
        glBegin(GL_POINTS);
        glVertex3fv(p2.getValue());
        glEnd();

        root->GLRender(action);
        state->pop();
    }
}

void SoRegPoint::generatePrimitives(SoAction* /*action*/)
{}

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

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

    box.extendBy(p1);
    box.extendBy(p2);

    center = box.getCenter();
}

void SoRegPoint::notify(SoNotList* node)
{
    SoField* f = node->getLastField();
    if (f == &this->base || f == &this->normal || f == &this->length) {
        auto move = static_cast<SoTranslation*>(root->getChild(0));
        move->translation.setValue(base.getValue() + normal.getValue() * length.getValue());
    }
    else if (f == &this->color) {
        auto sub = static_cast<SoSeparator*>(root->getChild(1));
        auto col = static_cast<SoBaseColor*>(sub->getChild(0));
        col->rgb = this->color.getValue();
    }
    else if (f == &this->text) {
        auto sub = static_cast<SoSeparator*>(root->getChild(1));
        auto label = static_cast<SoText2*>(sub->getChild(2));
        label->string = this->text.getValue();
    }

    SoShape::notify(node);
}