FreeCAD

Форк
0
/
SoFCShapeObject.cpp 
184 строки · 6.2 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 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
# include <algorithm>
35
# include <cfloat>
36
# include <Inventor/actions/SoGLRenderAction.h>
37
# include <Inventor/bundles/SoMaterialBundle.h>
38
# include <Inventor/bundles/SoTextureCoordinateBundle.h>
39
# include <Inventor/elements/SoCoordinateElement.h>
40
# include <Inventor/elements/SoLazyElement.h>
41
# include <Inventor/errors/SoReadError.h>
42
# include <Inventor/misc/SoState.h>
43
#endif
44

45
#include "SoFCShapeObject.h"
46

47

48
using namespace PartGui;
49

50
SO_NODE_SOURCE(SoFCControlPoints)
51

52
void SoFCControlPoints::initClass()
53
{
54
    SO_NODE_INIT_CLASS(SoFCControlPoints, SoShape, "Shape");
55
}
56

57
SoFCControlPoints::SoFCControlPoints()
58
{
59
    SO_NODE_CONSTRUCTOR(SoFCControlPoints);
60

61
    SbVec3f c(1.0f, 0.447059f, 0.337255f);
62
    SO_NODE_ADD_FIELD(numPolesU, (0));
63
    SO_NODE_ADD_FIELD(numPolesV, (0));
64
    SO_NODE_ADD_FIELD(numKnotsU, (0));
65
    SO_NODE_ADD_FIELD(numKnotsV, (0));
66
    SO_NODE_ADD_FIELD(lineColor, (c));
67
}
68

69
/**
70
 * Renders the control points.
71
 */
72
void SoFCControlPoints::GLRender(SoGLRenderAction *action)
73
{
74
    if (shouldGLRender(action))
75
    {
76
        SoState*  state = action->getState();
77
        const SoCoordinateElement * coords = SoCoordinateElement::getInstance(state);
78
        if (!coords)
79
            return;
80
        const SbVec3f * points = coords->getArrayPtr3();
81
        if (!points)
82
            return;
83

84
        SoMaterialBundle mb(action);
85
        SoTextureCoordinateBundle tb(action, true, false);
86
        SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR);
87
        mb.sendFirst();  // make sure we have the correct material
88

89
        int32_t len = coords->getNum();
90
        drawControlPoints(points, len);
91
    }
92
}
93

94
/**
95
 * Renders the control points and knots.
96
 */
97
void SoFCControlPoints::drawControlPoints(const SbVec3f * points,int32_t len) const
98
{
99
    glLineWidth(1.0f);
100
    glColor3fv(lineColor.getValue().getValue());
101

102
    uint32_t nCtU=numPolesU.getValue();
103
    uint32_t nCtV=numPolesV.getValue();
104
    uint32_t poles = nCtU * nCtV;
105
    if (poles > (uint32_t)len)
106
        return; // wrong setup, too few points
107
    // draw control mesh
108
    glBegin(GL_LINES);
109
    for (uint32_t u = 0; u < nCtU-1; ++u) {
110
        for (uint32_t v = 0; v < nCtV -1; ++v) {
111
            glVertex3fv(points[u*nCtV+v].getValue());
112
            glVertex3fv(points[u*nCtV+v+1].getValue());
113
            glVertex3fv(points[u*nCtV+v].getValue());
114
            glVertex3fv(points[(u+1)*nCtV+v].getValue());
115
        }
116
        glVertex3fv(points[(u+1)*nCtV-1].getValue());
117
        glVertex3fv(points[(u+2)*nCtV-1].getValue());
118
    }
119
    for (uint32_t v = 0; v < nCtV -1; ++v) {
120
        glVertex3fv(points[(nCtU-1)*nCtV+v].getValue());
121
        glVertex3fv(points[(nCtU-1)*nCtV+v+1].getValue());
122
    }
123
    glEnd();
124

125
    // draw poles
126
    glPointSize(5.0f);
127
    glBegin(GL_POINTS);
128
    for (uint32_t p=0; p<poles; p++) {
129
        glVertex3fv(points[p].getValue());
130
    }
131
    glEnd();
132

133
    // draw knots if available
134
    uint32_t knots=numKnotsU.getValue() * numKnotsV.getValue();
135
    uint32_t index = poles + knots;
136
    if (index > (uint32_t)len)
137
        return; // wrong setup, too few points
138
    glColor3f(1.0f, 1.0f, 0.0f);
139
    glPointSize(6.0f);
140
    glBegin(GL_POINTS);
141
    for (uint32_t p=poles; p<index; p++) {
142
        glVertex3fv(points[p].getValue());
143
    }
144
    glEnd();
145
}
146

147
void SoFCControlPoints::generatePrimitives(SoAction* /*action*/)
148
{
149
}
150

151
/**
152
 * Sets the bounding box of the mesh to \a box and its center to \a center.
153
 */
154
void SoFCControlPoints::computeBBox(SoAction *action, SbBox3f &box, SbVec3f &center)
155
{
156
    SoState*  state = action->getState();
157
    const SoCoordinateElement * coords = SoCoordinateElement::getInstance(state);
158
    if (!coords)
159
        return;
160
    const SbVec3f * points = coords->getArrayPtr3();
161
    if (!points)
162
        return;
163
    float maxX=-FLT_MAX, minX=FLT_MAX,
164
          maxY=-FLT_MAX, minY=FLT_MAX,
165
          maxZ=-FLT_MAX, minZ=FLT_MAX;
166
    int32_t len = coords->getNum();
167
    if (len > 0) {
168
        for (int32_t i=0; i<len; i++) {
169
            maxX = std::max<float>(maxX,points[i][0]);
170
            minX = std::min<float>(minX,points[i][0]);
171
            maxY = std::max<float>(maxY,points[i][1]);
172
            minY = std::min<float>(minY,points[i][1]);
173
            maxZ = std::max<float>(maxZ,points[i][2]);
174
            minZ = std::min<float>(minZ,points[i][2]);
175
        }
176

177
        box.setBounds(minX,minY,minZ,maxX,maxY,maxZ);
178
        center.setValue(0.5f*(minX+maxX),0.5f*(minY+maxY),0.5f*(minZ+maxZ));
179
    }
180
    else {
181
        box.setBounds(SbVec3f(0,0,0), SbVec3f(0,0,0));
182
        center.setValue(0.0f,0.0f,0.0f);
183
    }
184
}
185

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

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

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

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