File size: 7,228 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
/***************************************************************************
 *   Copyright (c) 2013 Jan Rheinländer                                    *
 *                                   <jrheinlaender@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 <Inventor/nodes/SoCone.h>
#include <Inventor/nodes/SoCube.h>
#include <Inventor/nodes/SoCylinder.h>
#include <Inventor/nodes/SoTranslation.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoRotation.h>


#include "FemGuiTools.h"


namespace FemGui::GuiTools
{

#define PLACEMENT_CHILDREN 2
#define CONE_CHILDREN 2

void createPlacement(SoSeparator* sep, const SbVec3f& base, const SbRotation& r)
{
    SoTranslation* trans = new SoTranslation();
    trans->translation.setValue(base);
    sep->addChild(trans);
    SoRotation* rot = new SoRotation();
    rot->rotation.setValue(r);
    sep->addChild(rot);
}

void updatePlacement(const SoSeparator* sep, const int idx, const SbVec3f& base, const SbRotation& r)
{
    SoTranslation* trans = static_cast<SoTranslation*>(sep->getChild(idx));
    trans->translation.setValue(base);
    SoRotation* rot = static_cast<SoRotation*>(sep->getChild(idx + 1));
    rot->rotation.setValue(r);
}

void createCone(SoSeparator* sep, const double height, const double radius)
{
    // Adjust cone so that the tip is on base
    SoTranslation* trans = new SoTranslation();
    trans->translation.setValue(SbVec3f(0, -height / 2, 0));
    sep->addChild(trans);
    SoCone* cone = new SoCone();
    cone->height.setValue(height);
    cone->bottomRadius.setValue(radius);
    sep->addChild(cone);
}

SoSeparator* createCone(const double height, const double radius)
{
    // Create a new cone node
    SoSeparator* sep = new SoSeparator();
    createCone(sep, height, radius);
    return sep;
}

void updateCone(const SoNode* node, const int idx, const double height, const double radius)
{
    const SoSeparator* sep = static_cast<const SoSeparator*>(node);
    SoTranslation* trans = static_cast<SoTranslation*>(sep->getChild(idx));
    trans->translation.setValue(SbVec3f(0, -height / 2, 0));
    SoCone* cone = static_cast<SoCone*>(sep->getChild(idx + 1));
    cone->height.setValue(height);
    cone->bottomRadius.setValue(radius);
}

void createCylinder(SoSeparator* sep, const double height, const double radius)
{
    SoCylinder* cyl = new SoCylinder();
    cyl->height.setValue(height);
    cyl->radius.setValue(radius);
    sep->addChild(cyl);
}

SoSeparator* createCylinder(const double height, const double radius)
{
    // Create a new cylinder node
    SoSeparator* sep = new SoSeparator();
    createCylinder(sep, height, radius);
    return sep;
}

void updateCylinder(const SoNode* node, const int idx, const double height, const double radius)
{
    const SoSeparator* sep = static_cast<const SoSeparator*>(node);
    SoCylinder* cyl = static_cast<SoCylinder*>(sep->getChild(idx));
    cyl->height.setValue(height);
    cyl->radius.setValue(radius);
}

void createCube(SoSeparator* sep, const double width, const double length, const double height)
{
    SoCube* cube = new SoCube();
    cube->width.setValue(width);
    cube->depth.setValue(length);
    cube->height.setValue(height);
    sep->addChild(cube);
}

SoSeparator* createCube(const double width, const double length, const double height)
{
    SoSeparator* sep = new SoSeparator();
    createCube(sep, width, length, height);
    return sep;
}

void updateCube(const SoNode* node, const int idx, const double width, const double length, const double height)
{
    const SoSeparator* sep = static_cast<const SoSeparator*>(node);
    SoCube* cube = static_cast<SoCube*>(sep->getChild(idx));
    cube->width.setValue(width);
    cube->depth.setValue(length);
    cube->height.setValue(height);
}

void createArrow(SoSeparator* sep, const double length, const double radius)
{
    createCone(sep, radius, radius / 2);
    createPlacement(sep, SbVec3f(0, -radius / 2 - (length - radius) / 2, 0), SbRotation());
    createCylinder(sep, length - radius, radius / 5);
}

SoSeparator* createArrow(const double length, const double radius)
{
    SoSeparator* sep = new SoSeparator();
    createArrow(sep, length, radius);
    return sep;
}

void updateArrow(const SoNode* node, const int idx, const double length, const double radius)
{
    const SoSeparator* sep = static_cast<const SoSeparator*>(node);
    updateCone(sep, idx, radius, radius / 2);
    updatePlacement(
        sep,
        idx + CONE_CHILDREN,
        SbVec3f(0, -radius / 2 - (length - radius) / 2, 0),
        SbRotation()
    );
    updateCylinder(sep, idx + CONE_CHILDREN + PLACEMENT_CHILDREN, length - radius, radius / 5);
}

void createFixed(SoSeparator* sep, const double height, const double width, const bool gap)
{
    createCone(sep, height - width / 4, height - width / 4);
    createPlacement(
        sep,
        SbVec3f(0, -(height - width / 4) / 2 - width / 8 - (gap ? 1.0 : 0.1) * width / 8, 0),
        SbRotation()
    );
    createCube(sep, width, width, width / 4);
}

SoSeparator* createFixed(const double height, const double width, const bool gap)
{
    SoSeparator* sep = new SoSeparator();
    createFixed(sep, height, width, gap);
    return sep;
}

void updateFixed(const SoNode* node, const int idx, const double height, const double width, const bool gap)
{
    const SoSeparator* sep = static_cast<const SoSeparator*>(node);
    updateCone(sep, idx, height - width / 4, height - width / 4);
    updatePlacement(
        sep,
        idx + CONE_CHILDREN,
        SbVec3f(0, -(height - width / 4) / 2 - width / 8 - (gap ? 1.0 : 0.0) * width / 8, 0),
        SbRotation()
    );
    updateCube(sep, idx + CONE_CHILDREN + PLACEMENT_CHILDREN, width, width, width / 4);
}

}  // namespace FemGui::GuiTools