FreeCAD

Форк
0
/
SoPolygon.cpp 
171 строка · 5.7 Кб
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
#ifndef _PreComp_
25
#ifdef FC_OS_WIN32
26
#include <Windows.h>
27
#endif
28
#ifdef FC_OS_MACOSX
29
#include <OpenGL/gl.h>
30
#else
31
#include <GL/gl.h>
32
#endif
33
#include <algorithm>
34
#include <cfloat>
35

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/misc/SoState.h>
42
#endif
43

44
#include "SoPolygon.h"
45

46

47
using namespace MeshGui;
48

49
SO_NODE_SOURCE(SoPolygon)
50

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

56
SoPolygon::SoPolygon()
57
{
58
    SO_NODE_CONSTRUCTOR(SoPolygon);
59

60
    SO_NODE_ADD_FIELD(startIndex, (0));
61
    SO_NODE_ADD_FIELD(numVertices, (0));
62
    SO_NODE_ADD_FIELD(highlight, (false));
63
    SO_NODE_ADD_FIELD(render, (true));
64
}
65

66
/**
67
 * Renders the polygon.
68
 */
69
void SoPolygon::GLRender(SoGLRenderAction* action)
70
{
71
    if (shouldGLRender(action) && render.getValue()) {
72
        SoState* state = action->getState();
73
        const SoCoordinateElement* coords = SoCoordinateElement::getInstance(state);
74
        if (!coords) {
75
            return;
76
        }
77
        const SbVec3f* points = coords->getArrayPtr3();
78
        if (!points) {
79
            return;
80
        }
81

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

87
        int32_t len = coords->getNum();
88
        drawPolygon(points, len);
89
    }
90
}
91

92
/**
93
 * Renders the polygon.
94
 */
95
void SoPolygon::drawPolygon(const SbVec3f* points, int32_t len) const
96
{
97
    glLineWidth(3.0F);
98
    int32_t beg = startIndex.getValue();
99
    int32_t cnt = numVertices.getValue();
100
    int32_t end = beg + cnt;
101
    if (end > len) {
102
        return;  // wrong setup, too few points
103
    }
104
    // draw control mesh
105
    glBegin(GL_LINES);
106
    for (int32_t i = beg; i < end; ++i) {
107
        int32_t j = (i - beg + 1) % cnt + beg;
108
        glVertex3fv(points[i].getValue());
109
        glVertex3fv(points[j].getValue());
110
    }
111
    glEnd();
112
}
113

114
/**
115
 * Calculates picked point based on primitives generated by subclasses.
116
 */
117
void SoPolygon::rayPick(SoRayPickAction* action)
118
{
119
    // if (this->shouldRayPick(action)) {
120
    //     this->computeObjectSpaceRay(action);
121

122
    //    const SoBoundingBoxCache* bboxcache = getBoundingBoxCache();
123
    //    if (!bboxcache || !bboxcache->isValid(action->getState()) ||
124
    //        SoFCMeshObjectShape_ray_intersect(action, bboxcache->getProjectedBox())) {
125
    //        this->generatePrimitives(action);
126
    //    }
127
    //}
128
    inherited::rayPick(action);
129
}
130

131
void SoPolygon::generatePrimitives(SoAction* /*action*/)
132
{}
133

134
/**
135
 * Sets the bounding box of the mesh to \a box and its center to \a center.
136
 */
137
void SoPolygon::computeBBox(SoAction* action, SbBox3f& box, SbVec3f& center)
138
{
139
    SoState* state = action->getState();
140
    const SoCoordinateElement* coords = SoCoordinateElement::getInstance(state);
141
    if (!coords) {
142
        return;
143
    }
144
    const SbVec3f* points = coords->getArrayPtr3();
145
    if (!points) {
146
        return;
147
    }
148
    float maxX = -FLT_MAX, minX = FLT_MAX, maxY = -FLT_MAX, minY = FLT_MAX, maxZ = -FLT_MAX,
149
          minZ = FLT_MAX;
150
    int32_t len = coords->getNum();
151
    int32_t beg = startIndex.getValue();
152
    int32_t cnt = numVertices.getValue();
153
    int32_t end = beg + cnt;
154
    if (end <= len) {
155
        for (int32_t i = beg; i < end; i++) {
156
            maxX = std::max<float>(maxX, points[i][0]);
157
            minX = std::min<float>(minX, points[i][0]);
158
            maxY = std::max<float>(maxY, points[i][1]);
159
            minY = std::min<float>(minY, points[i][1]);
160
            maxZ = std::max<float>(maxZ, points[i][2]);
161
            minZ = std::min<float>(minZ, points[i][2]);
162
        }
163

164
        box.setBounds(minX, minY, minZ, maxX, maxY, maxZ);
165
        center.setValue(0.5F * (minX + maxX), 0.5F * (minY + maxY), 0.5F * (minZ + maxZ));
166
    }
167
    else {
168
        box.setBounds(SbVec3f(0, 0, 0), SbVec3f(0, 0, 0));
169
        center.setValue(0.0F, 0.0F, 0.0F);
170
    }
171
}
172

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

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

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

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