File size: 10,772 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
// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
 *                                                                          *
 *   Copyright (c) 2024 Kacper Donat <kacper@kadet.net>                     *
 *                                                                          *
 *   This file is part of FreeCAD.                                          *
 *                                                                          *
 *   FreeCAD is free software: you can redistribute it and/or modify it     *
 *   under the terms of the GNU Lesser General Public License as            *
 *   published by the Free Software Foundation, either version 2.1 of the   *
 *   License, or (at your option) any later version.                        *
 *                                                                          *
 *   FreeCAD 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       *
 *   Lesser General Public License for more details.                        *
 *                                                                          *
 *   You should have received a copy of the GNU Lesser General Public       *
 *   License along with FreeCAD. If not, see                                *
 *   <https://www.gnu.org/licenses/>.                                       *
 *                                                                          *
 ***************************************************************************/

#include <sstream>

#include <Inventor/nodes/SoBaseColor.h>
#include <Inventor/nodes/SoComplexity.h>
#include <Inventor/nodes/SoCone.h>
#include <Inventor/nodes/SoCylinder.h>
#include <Inventor/nodes/SoDrawStyle.h>
#include <Inventor/nodes/SoLightModel.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoRotation.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoSphere.h>
#include <Inventor/nodes/SoTransform.h>
#include <Inventor/nodes/SoTranslation.h>
#include <Inventor/nodes/SoFontStyle.h>

#include "SoFCPlacementIndicatorKit.h"

#include "So3DAnnotation.h"
#include "SoAxisCrossKit.h"

#include <SoTextLabel.h>
#include <Utilities.h>
#include <ViewParams.h>
#include <ViewProvider.h>
#include <Base/Color.h>
#include <Base/Placement.h>
#include <Base/Vector3D.h>

namespace Gui
{

SO_KIT_SOURCE(SoFCPlacementIndicatorKit);

SoFCPlacementIndicatorKit::SoFCPlacementIndicatorKit()
{
    SO_KIT_CONSTRUCTOR(SoFCPlacementIndicatorKit);

    SO_KIT_ADD_CATALOG_ENTRY(root, SoShapeScale, false, this, "", true);

    SO_KIT_INIT_INSTANCE();

    SO_NODE_ADD_FIELD(coloredAxis, (true));
    SO_NODE_ADD_FIELD(scaleFactor, (ViewParams::instance()->getPlacementIndicatorScale()));
    SO_NODE_ADD_FIELD(axisLength, (axisLengthDefault));
    SO_NODE_ADD_FIELD(parts, (AxisCross));
    SO_NODE_ADD_FIELD(axes, (AllAxes));

    SO_NODE_DEFINE_ENUM_VALUE(Part, Axes);
    SO_NODE_DEFINE_ENUM_VALUE(Part, ArrowHeads);
    SO_NODE_DEFINE_ENUM_VALUE(Part, Labels);
    SO_NODE_DEFINE_ENUM_VALUE(Part, AxisCross);
    SO_NODE_DEFINE_ENUM_VALUE(Part, PlaneIndicator);
    SO_NODE_DEFINE_ENUM_VALUE(Part, OriginIndicator);
    SO_NODE_DEFINE_ENUM_VALUE(Part, AllParts);
    SO_NODE_SET_SF_ENUM_TYPE(parts, Part);

    SO_NODE_DEFINE_ENUM_VALUE(Axes, X);
    SO_NODE_DEFINE_ENUM_VALUE(Axes, Y);
    SO_NODE_DEFINE_ENUM_VALUE(Axes, Z);
    SO_NODE_DEFINE_ENUM_VALUE(Axes, AllAxes);
    SO_NODE_SET_SF_ENUM_TYPE(axes, Axes);

    auto root = SO_GET_ANY_PART(this, "root", SoShapeScale);
    root->scaleFactor.connectFrom(&scaleFactor);

    recomputeGeometry();
}

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

void SoFCPlacementIndicatorKit::notify(SoNotList* l)
{
    SoField* field = l->getLastField();

    if (field == &parts || field == &axes || field == &axisLength) {
        // certainly this is not the fastest way to recompute the geometry as it does recreate
        // everything from the scratch. It is however very easy to implement and this node should
        // not really change too often so the performance penalty is better than making code that
        // is harder to maintain.
        recomputeGeometry();
        return;
    }

    SoBaseKit::notify(l);
}

void SoFCPlacementIndicatorKit::recomputeGeometry()
{
    auto root = SO_GET_ANY_PART(this, "root", SoShapeScale);
    root->setPart("shape", createGeometry());
}

SoSeparator* SoFCPlacementIndicatorKit::createOriginIndicator()
{
    const uint32_t originColor = ViewParams::instance()->getOriginColor();

    auto sep = new SoSeparator();

    auto pcBaseColor = new SoBaseColor();
    pcBaseColor->rgb.setValue(Base::Color::fromPackedRGBA<SbColor>(originColor));

    auto pcSphere = new SoSphere();
    // the factor aligns radius of sphere and arrow head visually, without it, it looks too small
    constexpr float visualAdjustmentFactor = 1.2F;
    pcSphere->radius = arrowHeadRadius * visualAdjustmentFactor;

    sep->addChild(pcBaseColor);
    sep->addChild(pcSphere);

    return sep;
}

SoSeparator* SoFCPlacementIndicatorKit::createGeometry()
{
    const uint32_t neutralColor = ViewParams::instance()->getNeutralColor();

    auto sep = new SoSeparator();

    auto pcBaseColor = new SoBaseColor();
    pcBaseColor->rgb.setValue(Base::Color::fromPackedRGBA<SbColor>(neutralColor));

    auto pcLightModel = new SoLightModel();
    pcLightModel->model = SoLightModel::BASE_COLOR;

    sep->addChild(pcBaseColor);
    sep->addChild(pcLightModel);

    if (parts.getValue() & PlaneIndicator) {
        sep->addChild(createPlaneIndicator());
    }

    if (parts.getValue() & OriginIndicator) {
        sep->addChild(createOriginIndicator());
    }

    if (parts.getValue() & Axes) {
        sep->addChild(createAxes());
    }

    return sep;
}

SoSeparator* SoFCPlacementIndicatorKit::createAxes()
{
    const auto cylinderOffset = axisLength.getValue() / 2.F;

    const auto createAxis = [&](const char* label,
                                Base::Vector3d axis,
                                uint32_t packedColor,
                                const double offset) {
        Base::Color axisColor(packedColor);
        Base::Rotation rotation(Base::Vector3d::UnitY, axis);

        auto sep = new SoSeparator;

        auto pcTranslate = new SoTransform();
        pcTranslate->translation.setValue(Base::convertTo<SbVec3f>((cylinderOffset + offset) * axis));
        pcTranslate->rotation.setValue(Base::convertTo<SbRotation>(rotation));

        auto pcArrowShaft = new SoCylinder();
        pcArrowShaft->radius = axisThickness / 2.F;
        pcArrowShaft->height = axisLength;

        if (coloredAxis.getValue()) {
            auto pcBaseColor = new SoBaseColor();
            pcBaseColor->rgb.setValue(Base::convertTo<SbColor>(axisColor));

            sep->addChild(pcBaseColor);
        }

        sep->addChild(pcTranslate);
        sep->addChild(pcArrowShaft);

        if (parts.getValue() & ArrowHeads) {
            auto pcArrowHeadTranslation = new SoTranslation();
            pcArrowHeadTranslation->translation.setValue(0.0, cylinderOffset, 0.0);

            auto pcArrowHead = new SoCone();
            pcArrowHead->bottomRadius = arrowHeadRadius;
            pcArrowHead->height = arrowHeadHeight;

            auto pcArrowHeadSeparator = new SoSeparator();
            pcArrowHeadSeparator->addChild(pcArrowHeadTranslation);
            pcArrowHeadSeparator->addChild(pcArrowHead);

            sep->addChild(pcArrowHeadSeparator);
        }

        if (parts.getValue() & Labels) {
            auto pcLabelSeparator = new SoSeparator();

            auto pcLabelTranslation = new SoTranslation();
            pcLabelTranslation->translation.setValue(0.0, cylinderOffset + labelOffset, 0.0);
            pcLabelSeparator->addChild(pcLabelTranslation);

            auto pcAxisLabel = new SoFrameLabel();
            pcAxisLabel->string.setValue(label);
            pcAxisLabel->textColor.setValue(1.0, 1.0, 1.0);
            pcAxisLabel->horAlignment = SoImage::CENTER;
            pcAxisLabel->vertAlignment = SoImage::HALF;
            pcAxisLabel->border = false;
            pcAxisLabel->frame = false;
            pcAxisLabel->textUseBaseColor = true;
            pcAxisLabel->size = labelFontSize;

            pcLabelSeparator->addChild(pcAxisLabel);

            sep->addChild(pcLabelSeparator);
        }

        return sep;
    };

    double additionalAxisMargin = (parts.getValue() & OriginIndicator) ? axisThickness * 4 : 0;
    double xyOffset = (parts.getValue() & PlaneIndicator)
        ? planeIndicatorRadius + planeIndicatorMargin
        : axisMargin + additionalAxisMargin;

    auto sep = new SoSeparator;

    if (axes.getValue() & X) {
        sep->addChild(
            createAxis("X", Base::Vector3d::UnitX, ViewParams::instance()->getAxisXColor(), xyOffset)
        );
    }

    if (axes.getValue() & Y) {
        sep->addChild(
            createAxis("Y", Base::Vector3d::UnitY, ViewParams::instance()->getAxisYColor(), xyOffset)
        );
    }

    if (axes.getValue() & Z) {
        double zOffset = (parts.getValue() & PlaneIndicator) ? planeIndicatorMargin
                                                             : axisMargin + additionalAxisMargin;

        sep->addChild(
            createAxis("Z", Base::Vector3d::UnitZ, ViewParams::instance()->getAxisZColor(), zOffset)
        );
    }

    return sep;
}

SoSeparator* SoFCPlacementIndicatorKit::createPlaneIndicator()
{
    // cylinders are aligned with Y axis for some reason
    auto rotation = Base::Rotation(Base::Vector3d::UnitY, Base::Vector3d::UnitZ);

    auto pcRotation = new SoRotation();
    pcRotation->rotation = Base::convertTo<SbRotation>(rotation);

    auto pcComplexity = new SoComplexity();
    pcComplexity->value = 1.0f;

    auto pcCylinder = new SoCylinder();
    pcCylinder->height = 0.f;
    pcCylinder->radius = planeIndicatorRadius;
    pcCylinder->parts = SoCylinder::TOP;

    const auto gray = Base::Color(0.75f, 0.75f, 0.75f);

    auto pcMaterial = new SoMaterial();
    pcMaterial->diffuseColor.setValue(Base::convertTo<SbColor>(gray));
    pcMaterial->ambientColor.connectFrom(&pcMaterial->diffuseColor);
    pcMaterial->transparency = planeIndicatorTransparency;

    auto sep = new SoSeparator;

    sep->addChild(pcRotation);
    sep->addChild(pcMaterial);
    sep->addChild(pcComplexity);
    sep->addChild(pcCylinder);

    return sep;
}

SoFCPlacementIndicatorKit::~SoFCPlacementIndicatorKit() = default;

}  // namespace Gui